From 66016332f82446179aaae1ed5bd41c15b26773c0 Mon Sep 17 00:00:00 2001 From: Mikhail Chugunov Date: Wed, 2 Jan 2019 19:09:00 +0300 Subject: [PATCH 1/3] BAEL-2475: Add sample code and tests --- .../immutable/MaritalAwarePerson.java | 58 +++++++++++++++++++ .../deserialization/immutable/Person.java | 24 ++++++++ ...mmutableObjectDeserializationUnitTest.java | 40 +++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/MaritalAwarePerson.java create mode 100644 jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Person.java create mode 100644 jackson/src/test/java/com/baeldung/jackson/deserialization/immutable/ImmutableObjectDeserializationUnitTest.java diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/MaritalAwarePerson.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/MaritalAwarePerson.java new file mode 100644 index 0000000000..cb593b3bb7 --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/MaritalAwarePerson.java @@ -0,0 +1,58 @@ +package com.baeldung.jackson.deserialization.immutable; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; + +@JsonDeserialize(builder = MaritalAwarePerson.Builder.class) +public class MaritalAwarePerson { + + private final String name; + private final int age; + private final Boolean married; + + private MaritalAwarePerson(String name, int age, Boolean married) { + this.name = name; + this.age = age; + this.married = married; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } + + public Boolean getMarried() { + return married; + } + + @JsonPOJOBuilder + static class Builder { + String name; + int age; + Boolean married; + + Builder withName(String name) { + this.name = name; + return this; + } + + Builder withAge(int age) { + this.age = age; + return this; + } + + Builder withMarried(boolean married) { + this.married = married; + return this; + } + + public MaritalAwarePerson build() { + return new MaritalAwarePerson(name, age, married); + } + + + } +} diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Person.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Person.java new file mode 100644 index 0000000000..0214f93ca9 --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Person.java @@ -0,0 +1,24 @@ +package com.baeldung.jackson.deserialization.immutable; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +public class Person { + + private final String name; + private final int age; + + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) + public Person(@JsonProperty("name") String name, @JsonProperty("age") int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } +} diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/immutable/ImmutableObjectDeserializationUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/deserialization/immutable/ImmutableObjectDeserializationUnitTest.java new file mode 100644 index 0000000000..348ece4a09 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/deserialization/immutable/ImmutableObjectDeserializationUnitTest.java @@ -0,0 +1,40 @@ +package com.baeldung.jackson.deserialization.immutable; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; + +import java.io.IOException; + +import static org.junit.Assert.*; + +public class ImmutableObjectDeserializationUnitTest { + + @Test + public void testPublicConstructor() throws IOException { + final String json = "{\"name\":\"Frank\",\"age\":50}"; + Person person = new ObjectMapper().readValue(json, Person.class); + + assertEquals("Frank", person.getName()); + assertEquals(50, person.getAge()); + } + + @Test + public void testBuilderNullField() throws IOException { + final String json = "{\"name\":\"Frank\",\"age\":50}"; + MaritalAwarePerson person = new ObjectMapper().readValue(json, MaritalAwarePerson.class); + + assertEquals("Frank", person.getName()); + assertEquals(50, person.getAge()); + assertNull(person.getMarried()); + } + + @Test + public void testBuilderAllFields() throws IOException { + final String json = "{\"name\":\"Frank\",\"age\":50,\"married\":true}"; + MaritalAwarePerson person = new ObjectMapper().readValue(json, MaritalAwarePerson.class); + + assertEquals("Frank", person.getName()); + assertEquals(50, person.getAge()); + assertTrue(person.getMarried()); + } +} From 2e436ce3e8e31c6791257f624b9661b4ba3368b3 Mon Sep 17 00:00:00 2001 From: Mikhail Chugunov Date: Wed, 2 Jan 2019 19:14:09 +0300 Subject: [PATCH 2/3] BAEL-2475: Rename tests --- .../immutable/ImmutableObjectDeserializationUnitTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/immutable/ImmutableObjectDeserializationUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/deserialization/immutable/ImmutableObjectDeserializationUnitTest.java index 348ece4a09..5c33a2da26 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/immutable/ImmutableObjectDeserializationUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/deserialization/immutable/ImmutableObjectDeserializationUnitTest.java @@ -10,7 +10,7 @@ import static org.junit.Assert.*; public class ImmutableObjectDeserializationUnitTest { @Test - public void testPublicConstructor() throws IOException { + public void whenPublicConstructorIsUsed_thenObjectIsDeserialized() throws IOException { final String json = "{\"name\":\"Frank\",\"age\":50}"; Person person = new ObjectMapper().readValue(json, Person.class); @@ -19,7 +19,7 @@ public class ImmutableObjectDeserializationUnitTest { } @Test - public void testBuilderNullField() throws IOException { + public void whenBuilderIsUsedAndFieldIsNull_thenObjectIsDeserialized() throws IOException { final String json = "{\"name\":\"Frank\",\"age\":50}"; MaritalAwarePerson person = new ObjectMapper().readValue(json, MaritalAwarePerson.class); @@ -29,7 +29,7 @@ public class ImmutableObjectDeserializationUnitTest { } @Test - public void testBuilderAllFields() throws IOException { + public void whenBuilderIsUsedAndAllFieldsPresent_thenObjectIsDeserialized() throws IOException { final String json = "{\"name\":\"Frank\",\"age\":50,\"married\":true}"; MaritalAwarePerson person = new ObjectMapper().readValue(json, MaritalAwarePerson.class); From a094e49a6292aaad494f6d66194066c69a81c43f Mon Sep 17 00:00:00 2001 From: Mikhail Chugunov Date: Wed, 9 Jan 2019 19:53:12 +0300 Subject: [PATCH 3/3] BAEL-2475: Changes after first review --- .../deserialization/immutable/Employee.java | 24 ++++++++ .../immutable/MaritalAwarePerson.java | 58 ------------------- .../deserialization/immutable/Person.java | 32 ++++++++-- ...mmutableObjectDeserializationUnitTest.java | 22 ++++--- 4 files changed, 60 insertions(+), 76 deletions(-) create mode 100644 jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Employee.java delete mode 100644 jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/MaritalAwarePerson.java diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Employee.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Employee.java new file mode 100644 index 0000000000..44b10ee39b --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Employee.java @@ -0,0 +1,24 @@ +package com.baeldung.jackson.deserialization.immutable; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +public class Employee { + + private final long id; + private final String name; + + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) + public Employee(@JsonProperty("id") long id, @JsonProperty("name") String name) { + this.id = id; + this.name = name; + } + + public long getId() { + return id; + } + + public String getName() { + return name; + } +} diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/MaritalAwarePerson.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/MaritalAwarePerson.java deleted file mode 100644 index cb593b3bb7..0000000000 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/MaritalAwarePerson.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.baeldung.jackson.deserialization.immutable; - -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; -import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; - -@JsonDeserialize(builder = MaritalAwarePerson.Builder.class) -public class MaritalAwarePerson { - - private final String name; - private final int age; - private final Boolean married; - - private MaritalAwarePerson(String name, int age, Boolean married) { - this.name = name; - this.age = age; - this.married = married; - } - - public String getName() { - return name; - } - - public int getAge() { - return age; - } - - public Boolean getMarried() { - return married; - } - - @JsonPOJOBuilder - static class Builder { - String name; - int age; - Boolean married; - - Builder withName(String name) { - this.name = name; - return this; - } - - Builder withAge(int age) { - this.age = age; - return this; - } - - Builder withMarried(boolean married) { - this.married = married; - return this; - } - - public MaritalAwarePerson build() { - return new MaritalAwarePerson(name, age, married); - } - - - } -} diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Person.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Person.java index 0214f93ca9..d9041720b6 100644 --- a/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Person.java +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/immutable/Person.java @@ -1,15 +1,15 @@ package com.baeldung.jackson.deserialization.immutable; -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; +@JsonDeserialize(builder = Person.Builder.class) public class Person { private final String name; - private final int age; + private final Integer age; - @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) - public Person(@JsonProperty("name") String name, @JsonProperty("age") int age) { + private Person(String name, Integer age) { this.name = name; this.age = age; } @@ -18,7 +18,27 @@ public class Person { return name; } - public int getAge() { + public Integer getAge() { return age; } + + @JsonPOJOBuilder + static class Builder { + String name; + Integer age; + + Builder withName(String name) { + this.name = name; + return this; + } + + Builder withAge(Integer age) { + this.age = age; + return this; + } + + Person build() { + return new Person(name, age); + } + } } diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/immutable/ImmutableObjectDeserializationUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/deserialization/immutable/ImmutableObjectDeserializationUnitTest.java index 5c33a2da26..1252179e3a 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/immutable/ImmutableObjectDeserializationUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/deserialization/immutable/ImmutableObjectDeserializationUnitTest.java @@ -11,30 +11,28 @@ public class ImmutableObjectDeserializationUnitTest { @Test public void whenPublicConstructorIsUsed_thenObjectIsDeserialized() throws IOException { - final String json = "{\"name\":\"Frank\",\"age\":50}"; - Person person = new ObjectMapper().readValue(json, Person.class); + final String json = "{\"name\":\"Frank\",\"id\":5000}"; + Employee employee = new ObjectMapper().readValue(json, Employee.class); - assertEquals("Frank", person.getName()); - assertEquals(50, person.getAge()); + assertEquals("Frank", employee.getName()); + assertEquals(5000, employee.getId()); } @Test public void whenBuilderIsUsedAndFieldIsNull_thenObjectIsDeserialized() throws IOException { - final String json = "{\"name\":\"Frank\",\"age\":50}"; - MaritalAwarePerson person = new ObjectMapper().readValue(json, MaritalAwarePerson.class); + final String json = "{\"name\":\"Frank\"}"; + Person person = new ObjectMapper().readValue(json, Person.class); assertEquals("Frank", person.getName()); - assertEquals(50, person.getAge()); - assertNull(person.getMarried()); + assertNull(person.getAge()); } @Test public void whenBuilderIsUsedAndAllFieldsPresent_thenObjectIsDeserialized() throws IOException { - final String json = "{\"name\":\"Frank\",\"age\":50,\"married\":true}"; - MaritalAwarePerson person = new ObjectMapper().readValue(json, MaritalAwarePerson.class); + final String json = "{\"name\":\"Frank\",\"age\":50}"; + Person person = new ObjectMapper().readValue(json, Person.class); assertEquals("Frank", person.getName()); - assertEquals(50, person.getAge()); - assertTrue(person.getMarried()); + assertEquals(50, (int) person.getAge()); } }