Skip to content

Commit 3f6dceb

Browse files
chrisoberleKevinGilmore
authored andcommitted
BAEL-1187 - Mapping Nested Values with Jackson (eugenp#2750)
* BAEL-1187 - Mapping Nested Values with Jackson * minor naming and formatting changes to align with standards * update formatting and jackson version
1 parent d6d6a9e commit 3f6dceb

4 files changed

Lines changed: 166 additions & 1 deletion

File tree

‎jackson/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129

130130
<properties>
131131
<!-- marshalling -->
132-
<jackson.version>2.9.0</jackson.version>
132+
<jackson.version>2.9.2</jackson.version>
133133

134134
<!-- util -->
135135
<guava.version>19.0</guava.version>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.jackson.deserialization.nested;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.IOException;
6+
7+
import org.junit.Test;
8+
9+
import com.fasterxml.jackson.databind.JsonNode;
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
import com.fasterxml.jackson.databind.module.SimpleModule;
12+
13+
public class DeserializeWithNestedPropertiesUnitTest {
14+
15+
private String SOURCE_JSON = "{\"id\":\"957c43f2-fa2e-42f9-bf75-6e3d5bb6960a\",\"name\":\"The Best Product\",\"brand\":{\"id\":\"9bcd817d-0141-42e6-8f04-e5aaab0980b6\",\"name\":\"ACME Products\",\"owner\":{\"id\":\"b21a80b1-0c09-4be3-9ebd-ea3653511c13\",\"name\":\"Ultimate Corp, Inc.\"}}}";
16+
17+
@Test
18+
public void whenUsingJacksonAnnotations_thenOk() throws IOException {
19+
Product product = new ObjectMapper().readerFor(Product.class)
20+
.readValue(SOURCE_JSON);
21+
22+
assertEquals(product.getName(), "The Best Product");
23+
assertEquals(product.getBrandName(), "ACME Products");
24+
assertEquals(product.getOwnerName(), "Ultimate Corp, Inc.");
25+
}
26+
27+
@Test
28+
public void whenUsingJacksonJsonNode_thenOk() throws IOException {
29+
JsonNode productNode = new ObjectMapper().readTree(SOURCE_JSON);
30+
31+
Product product = new Product();
32+
product.setId(productNode.get("id")
33+
.textValue());
34+
product.setName(productNode.get("name")
35+
.textValue());
36+
product.setBrandName(productNode.get("brand")
37+
.get("name")
38+
.textValue());
39+
product.setOwnerName(productNode.get("brand")
40+
.get("owner")
41+
.get("name")
42+
.textValue());
43+
44+
assertEquals(product.getName(), "The Best Product");
45+
assertEquals(product.getBrandName(), "ACME Products");
46+
assertEquals(product.getOwnerName(), "Ultimate Corp, Inc.");
47+
}
48+
49+
@Test
50+
public void whenUsingJacksonDeserializerManuallyRegistered_thenOk() throws IOException {
51+
ObjectMapper mapper = new ObjectMapper();
52+
SimpleModule module = new SimpleModule();
53+
module.addDeserializer(Product.class, new ProductDeserializer());
54+
mapper.registerModule(module);
55+
56+
Product product = mapper.readValue(SOURCE_JSON, Product.class);
57+
assertEquals(product.getName(), "The Best Product");
58+
assertEquals(product.getBrandName(), "ACME Products");
59+
assertEquals(product.getOwnerName(), "Ultimate Corp, Inc.");
60+
}
61+
62+
@Test
63+
public void whenUsingJacksonDeserializerAutoRegistered_thenOk() throws IOException {
64+
ObjectMapper mapper = new ObjectMapper();
65+
Product product = mapper.readValue(SOURCE_JSON, Product.class);
66+
assertEquals(product.getName(), "The Best Product");
67+
assertEquals(product.getBrandName(), "ACME Products");
68+
assertEquals(product.getOwnerName(), "Ultimate Corp, Inc.");
69+
}
70+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.jackson.deserialization.nested;
2+
3+
import java.util.Map;
4+
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
7+
8+
@JsonDeserialize(using = ProductDeserializer.class)
9+
public class Product {
10+
11+
private String id;
12+
private String name;
13+
private String brandName;
14+
private String ownerName;
15+
16+
public String getId() {
17+
return id;
18+
}
19+
20+
public void setId(String id) {
21+
this.id = id;
22+
}
23+
24+
public String getName() {
25+
return name;
26+
}
27+
28+
public void setName(String name) {
29+
this.name = name;
30+
}
31+
32+
public String getBrandName() {
33+
return brandName;
34+
}
35+
36+
public void setBrandName(String brandName) {
37+
this.brandName = brandName;
38+
}
39+
40+
public String getOwnerName() {
41+
return ownerName;
42+
}
43+
44+
public void setOwnerName(String ownerName) {
45+
this.ownerName = ownerName;
46+
}
47+
48+
@SuppressWarnings("unchecked")
49+
@JsonProperty("brand")
50+
private void unpackNested(Map<String, Object> brand) {
51+
this.brandName = (String) brand.get("name");
52+
Map<String, String> owner = (Map<String, String>) brand.get("owner");
53+
this.ownerName = owner.get("name");
54+
}
55+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.jackson.deserialization.nested;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.core.JsonParser;
6+
import com.fasterxml.jackson.core.JsonProcessingException;
7+
import com.fasterxml.jackson.databind.DeserializationContext;
8+
import com.fasterxml.jackson.databind.JsonNode;
9+
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
10+
11+
@SuppressWarnings("serial")
12+
public class ProductDeserializer extends StdDeserializer<Product> {
13+
14+
public ProductDeserializer() {
15+
this(null);
16+
}
17+
18+
public ProductDeserializer(Class<?> vc) {
19+
super(vc);
20+
}
21+
22+
@Override
23+
public Product deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
24+
JsonNode productNode = jp.getCodec()
25+
.readTree(jp);
26+
Product product = new Product();
27+
product.setId(productNode.get("id")
28+
.textValue());
29+
product.setName(productNode.get("name")
30+
.textValue());
31+
product.setBrandName(productNode.get("brand")
32+
.get("name")
33+
.textValue());
34+
product.setOwnerName(productNode.get("brand")
35+
.get("owner")
36+
.get("name")
37+
.textValue());
38+
return product;
39+
}
40+
}

0 commit comments

Comments
 (0)