File tree Expand file tree Collapse file tree
main/java/com/baeldung/jackson/deserialization/immutable
test/java/com/baeldung/jackson/deserialization/immutable Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .jackson .deserialization .immutable ;
2+
3+ import com .fasterxml .jackson .annotation .JsonCreator ;
4+ import com .fasterxml .jackson .annotation .JsonProperty ;
5+
6+ public class Employee {
7+
8+ private final long id ;
9+ private final String name ;
10+
11+ @ JsonCreator (mode = JsonCreator .Mode .PROPERTIES )
12+ public Employee (@ JsonProperty ("id" ) long id , @ JsonProperty ("name" ) String name ) {
13+ this .id = id ;
14+ this .name = name ;
15+ }
16+
17+ public long getId () {
18+ return id ;
19+ }
20+
21+ public String getName () {
22+ return name ;
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .jackson .deserialization .immutable ;
2+
3+ import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
4+ import com .fasterxml .jackson .databind .annotation .JsonPOJOBuilder ;
5+
6+ @ JsonDeserialize (builder = Person .Builder .class )
7+ public class Person {
8+
9+ private final String name ;
10+ private final Integer age ;
11+
12+ private Person (String name , Integer age ) {
13+ this .name = name ;
14+ this .age = age ;
15+ }
16+
17+ public String getName () {
18+ return name ;
19+ }
20+
21+ public Integer getAge () {
22+ return age ;
23+ }
24+
25+ @ JsonPOJOBuilder
26+ static class Builder {
27+ String name ;
28+ Integer age ;
29+
30+ Builder withName (String name ) {
31+ this .name = name ;
32+ return this ;
33+ }
34+
35+ Builder withAge (Integer age ) {
36+ this .age = age ;
37+ return this ;
38+ }
39+
40+ Person build () {
41+ return new Person (name , age );
42+ }
43+ }
44+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .jackson .deserialization .immutable ;
2+
3+ import com .fasterxml .jackson .databind .ObjectMapper ;
4+ import org .junit .Test ;
5+
6+ import java .io .IOException ;
7+
8+ import static org .junit .Assert .*;
9+
10+ public class ImmutableObjectDeserializationUnitTest {
11+
12+ @ Test
13+ public void whenPublicConstructorIsUsed_thenObjectIsDeserialized () throws IOException {
14+ final String json = "{\" name\" :\" Frank\" ,\" id\" :5000}" ;
15+ Employee employee = new ObjectMapper ().readValue (json , Employee .class );
16+
17+ assertEquals ("Frank" , employee .getName ());
18+ assertEquals (5000 , employee .getId ());
19+ }
20+
21+ @ Test
22+ public void whenBuilderIsUsedAndFieldIsNull_thenObjectIsDeserialized () throws IOException {
23+ final String json = "{\" name\" :\" Frank\" }" ;
24+ Person person = new ObjectMapper ().readValue (json , Person .class );
25+
26+ assertEquals ("Frank" , person .getName ());
27+ assertNull (person .getAge ());
28+ }
29+
30+ @ Test
31+ public void whenBuilderIsUsedAndAllFieldsPresent_thenObjectIsDeserialized () throws IOException {
32+ final String json = "{\" name\" :\" Frank\" ,\" age\" :50}" ;
33+ Person person = new ObjectMapper ().readValue (json , Person .class );
34+
35+ assertEquals ("Frank" , person .getName ());
36+ assertEquals (50 , (int ) person .getAge ());
37+ }
38+ }
You can’t perform that action at this time.
0 commit comments