Skip to content

Commit 14771a3

Browse files
authored
Merge pull request eugenp#6051 from mchugunov/BAEL-2475-deserialize-immutable-objects
BAEL-2475: Deserialize immutable objects
2 parents 13ab8a1 + a094e49 commit 14771a3

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}

0 commit comments

Comments
 (0)