BAEL-2475: Changes after first review
This commit is contained in:
parent
2e436ce3e8
commit
a094e49a62
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,15 +1,15 @@
|
|||||||
package com.baeldung.jackson.deserialization.immutable;
|
package com.baeldung.jackson.deserialization.immutable;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
|
||||||
|
|
||||||
|
@JsonDeserialize(builder = Person.Builder.class)
|
||||||
public class Person {
|
public class Person {
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
private final int age;
|
private final Integer age;
|
||||||
|
|
||||||
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
|
private Person(String name, Integer age) {
|
||||||
public Person(@JsonProperty("name") String name, @JsonProperty("age") int age) {
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.age = age;
|
this.age = age;
|
||||||
}
|
}
|
||||||
@ -18,7 +18,27 @@ public class Person {
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAge() {
|
public Integer getAge() {
|
||||||
return age;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,30 +11,28 @@ public class ImmutableObjectDeserializationUnitTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenPublicConstructorIsUsed_thenObjectIsDeserialized() throws IOException {
|
public void whenPublicConstructorIsUsed_thenObjectIsDeserialized() throws IOException {
|
||||||
final String json = "{\"name\":\"Frank\",\"age\":50}";
|
final String json = "{\"name\":\"Frank\",\"id\":5000}";
|
||||||
Person person = new ObjectMapper().readValue(json, Person.class);
|
Employee employee = new ObjectMapper().readValue(json, Employee.class);
|
||||||
|
|
||||||
assertEquals("Frank", person.getName());
|
assertEquals("Frank", employee.getName());
|
||||||
assertEquals(50, person.getAge());
|
assertEquals(5000, employee.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenBuilderIsUsedAndFieldIsNull_thenObjectIsDeserialized() throws IOException {
|
public void whenBuilderIsUsedAndFieldIsNull_thenObjectIsDeserialized() throws IOException {
|
||||||
final String json = "{\"name\":\"Frank\",\"age\":50}";
|
final String json = "{\"name\":\"Frank\"}";
|
||||||
MaritalAwarePerson person = new ObjectMapper().readValue(json, MaritalAwarePerson.class);
|
Person person = new ObjectMapper().readValue(json, Person.class);
|
||||||
|
|
||||||
assertEquals("Frank", person.getName());
|
assertEquals("Frank", person.getName());
|
||||||
assertEquals(50, person.getAge());
|
assertNull(person.getAge());
|
||||||
assertNull(person.getMarried());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenBuilderIsUsedAndAllFieldsPresent_thenObjectIsDeserialized() throws IOException {
|
public void whenBuilderIsUsedAndAllFieldsPresent_thenObjectIsDeserialized() throws IOException {
|
||||||
final String json = "{\"name\":\"Frank\",\"age\":50,\"married\":true}";
|
final String json = "{\"name\":\"Frank\",\"age\":50}";
|
||||||
MaritalAwarePerson person = new ObjectMapper().readValue(json, MaritalAwarePerson.class);
|
Person person = new ObjectMapper().readValue(json, Person.class);
|
||||||
|
|
||||||
assertEquals("Frank", person.getName());
|
assertEquals("Frank", person.getName());
|
||||||
assertEquals(50, person.getAge());
|
assertEquals(50, (int) person.getAge());
|
||||||
assertTrue(person.getMarried());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user