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;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user