diff --git a/lombok-modules/lombok-2/lombok.config b/lombok-modules/lombok-2/lombok.config index f68b99fb85..f33d9d905f 100644 --- a/lombok-modules/lombok-2/lombok.config +++ b/lombok-modules/lombok-2/lombok.config @@ -1,2 +1,2 @@ config.stopBubbling = true -lombok.experimental.flagUsage = warning +lombok.experimental.flagUsage = warning \ No newline at end of file diff --git a/lombok-modules/lombok-2/pom.xml b/lombok-modules/lombok-2/pom.xml index c92feed739..23c907b4a1 100644 --- a/lombok-modules/lombok-2/pom.xml +++ b/lombok-modules/lombok-2/pom.xml @@ -20,6 +20,15 @@ ${lombok.version} provided + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + 2.14.1 + + \ No newline at end of file diff --git a/lombok-modules/lombok-2/src/main/java/com/baeldung/lombok/jackson/Employee.java b/lombok-modules/lombok-2/src/main/java/com/baeldung/lombok/jackson/Employee.java new file mode 100644 index 0000000000..48df7de757 --- /dev/null +++ b/lombok-modules/lombok-2/src/main/java/com/baeldung/lombok/jackson/Employee.java @@ -0,0 +1,39 @@ +package com.baeldung.lombok.jackson; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; + +@Data +@Builder(builderClassName = "EmployeeBuilder") +@JsonDeserialize(builder = Employee.EmployeeBuilder.class) +@AllArgsConstructor +public class Employee { + + private int identity; + private String firstName; + + @JsonPOJOBuilder(buildMethodName = "createEmployee", withPrefix = "construct") + public static class EmployeeBuilder { + + private int idValue; + private String nameValue; + + public EmployeeBuilder constructId(int id) { + idValue = id; + return this; + } + + public EmployeeBuilder constructName(String name) { + nameValue = name; + return this; + } + + public Employee createEmployee() { + return new Employee(idValue, nameValue); + } + } +} diff --git a/lombok-modules/lombok-2/src/main/java/com/baeldung/lombok/jackson/Fruit.java b/lombok-modules/lombok-2/src/main/java/com/baeldung/lombok/jackson/Fruit.java new file mode 100644 index 0000000000..bfe547b076 --- /dev/null +++ b/lombok-modules/lombok-2/src/main/java/com/baeldung/lombok/jackson/Fruit.java @@ -0,0 +1,15 @@ +package com.baeldung.lombok.jackson; + +import lombok.Builder; +import lombok.Data; +import lombok.extern.jackson.Jacksonized; + +@Data +@Builder +@Jacksonized +public class Fruit { + + private String name; + private int id; + +} diff --git a/lombok-modules/lombok-2/src/test/java/com/baeldung/lombok/jackson/LombokWithJacksonEmployeeUnitTest.java b/lombok-modules/lombok-2/src/test/java/com/baeldung/lombok/jackson/LombokWithJacksonEmployeeUnitTest.java new file mode 100644 index 0000000000..52a964c734 --- /dev/null +++ b/lombok-modules/lombok-2/src/test/java/com/baeldung/lombok/jackson/LombokWithJacksonEmployeeUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.lombok.jackson; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import org.junit.jupiter.api.Test; +import com.fasterxml.jackson.databind.ObjectMapper; + +class LombokWithJacksonEmployeeUnitTest { + + @Test + void withEmployeeObject_thenSerializeSucessfully() throws IOException { + Employee employee = Employee.builder() + .identity(5) + .firstName("Bob") + .build(); + + String json = newObjectMapper().writeValueAsString(employee); + assertEquals("{\"identity\":5,\"firstName\":\"Bob\"}", json); + } + + @Test + public void withEmployeeJSON_thenDeserializeSucessfully() throws IOException { + String json = "{\"id\":5,\"name\":\"Bob\"}"; + Employee employee = newObjectMapper().readValue(json, Employee.class); + + assertEquals(5, employee.getIdentity()); + assertEquals("Bob", employee.getFirstName()); + } + + private ObjectMapper newObjectMapper() { + return new ObjectMapper(); + } + +} diff --git a/lombok-modules/lombok-2/src/test/java/com/baeldung/lombok/jackson/LombokWithJacksonFruitUnitTest.java b/lombok-modules/lombok-2/src/test/java/com/baeldung/lombok/jackson/LombokWithJacksonFruitUnitTest.java new file mode 100644 index 0000000000..a8ecb05fbf --- /dev/null +++ b/lombok-modules/lombok-2/src/test/java/com/baeldung/lombok/jackson/LombokWithJacksonFruitUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.lombok.jackson; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import org.junit.jupiter.api.Test; +import com.fasterxml.jackson.databind.ObjectMapper; + +class LombokWithJacksonFruitUnitTest { + + @Test + void withFruitObject_thenSerializeSucessfully() throws IOException { + Fruit fruit = Fruit.builder() + .id(101) + .name("Apple") + .build(); + + String json = newObjectMapper().writeValueAsString(fruit); + assertEquals("{\"name\":\"Apple\",\"id\":101}", json); + } + + @Test + public void withFruitJSON_thenDeserializeSucessfully() throws IOException { + String json = "{\"name\":\"Apple\",\"id\":101}"; + Fruit fruit = newObjectMapper().readValue(json, Fruit.class); + assertEquals(new Fruit("Apple", 101), fruit); + } + + private ObjectMapper newObjectMapper() { + return new ObjectMapper(); + } + +}