BAEL-2475: Add sample code and tests
This commit is contained in:
parent
8d4badcea7
commit
66016332f8
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue