From 12bf5e9f3bbb631dab6c7f84472f15f7e96d83e9 Mon Sep 17 00:00:00 2001 From: fanatixan Date: Sat, 22 Dec 2018 18:13:09 +0100 Subject: [PATCH] bael-2437 (#5972) --- .../dynamicobject/ProductJsonAnySetter.java | 39 +++++++++++ .../dynamicobject/ProductJsonNode.java | 35 ++++++++++ .../dynamicobject/ProductMap.java | 35 ++++++++++ .../DynamicObjectDeserializationUnitTest.java | 69 +++++++++++++++++++ .../deserialize-dynamic-object/embedded.json | 8 +++ .../deserialize-dynamic-object/flat.json | 6 ++ 6 files changed, 192 insertions(+) create mode 100644 jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductJsonAnySetter.java create mode 100644 jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductJsonNode.java create mode 100644 jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductMap.java create mode 100644 jackson/src/test/java/com/baeldung/jackson/deserialization/dynamicobject/DynamicObjectDeserializationUnitTest.java create mode 100644 jackson/src/test/resources/deserialize-dynamic-object/embedded.json create mode 100644 jackson/src/test/resources/deserialize-dynamic-object/flat.json diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductJsonAnySetter.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductJsonAnySetter.java new file mode 100644 index 0000000000..13c21e37af --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductJsonAnySetter.java @@ -0,0 +1,39 @@ +package com.baeldung.jackson.deserialization.dynamicobject; + +import java.util.LinkedHashMap; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonAnySetter; + +public class ProductJsonAnySetter { + + private String name; + private String category; + private Map details = new LinkedHashMap<>(); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public Map getDetails() { + return details; + } + + @JsonAnySetter + public void setDetail(String key, Object value) { + details.put(key, value); + } + +} diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductJsonNode.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductJsonNode.java new file mode 100644 index 0000000000..729d542df3 --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductJsonNode.java @@ -0,0 +1,35 @@ +package com.baeldung.jackson.deserialization.dynamicobject; + +import com.fasterxml.jackson.databind.JsonNode; + +public class ProductJsonNode { + + private String name; + private String category; + private JsonNode details; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public JsonNode getDetails() { + return details; + } + + public void setDetails(JsonNode details) { + this.details = details; + } + +} diff --git a/jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductMap.java b/jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductMap.java new file mode 100644 index 0000000000..65d225f49b --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/deserialization/dynamicobject/ProductMap.java @@ -0,0 +1,35 @@ +package com.baeldung.jackson.deserialization.dynamicobject; + +import java.util.Map; + +public class ProductMap { + + private String name; + private String category; + private Map details; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public Map getDetails() { + return details; + } + + public void setDetails(Map details) { + this.details = details; + } + +} diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/dynamicobject/DynamicObjectDeserializationUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/deserialization/dynamicobject/DynamicObjectDeserializationUnitTest.java new file mode 100644 index 0000000000..d49677c1a2 --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/deserialization/dynamicobject/DynamicObjectDeserializationUnitTest.java @@ -0,0 +1,69 @@ +package com.baeldung.jackson.deserialization.dynamicobject; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import java.util.Scanner; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class DynamicObjectDeserializationUnitTest { + + private ObjectMapper objectMapper; + + @BeforeEach + void setup() { + objectMapper = new ObjectMapper(); + } + + private String readResource(String path) { + try (Scanner scanner = new Scanner(getClass().getResourceAsStream(path), "UTF-8")) { + return scanner.useDelimiter("\\A").next(); + } + } + + @Test + void givenJsonString_whenParsingToJsonNode_thenItMustContainDynamicProperties() throws JsonParseException, JsonMappingException, IOException { + // given + String json = readResource("/deserialize-dynamic-object/embedded.json"); + + // when + ProductJsonNode product = objectMapper.readValue(json, ProductJsonNode.class); + + // then + assertThat(product.getName()).isEqualTo("Pear yPhone 72"); + assertThat(product.getDetails().get("audioConnector").asText()).isEqualTo("none"); + } + + @Test + void givenJsonString_whenParsingToMap_thenItMustContainDynamicProperties() throws JsonParseException, JsonMappingException, IOException { + // given + String json = readResource("/deserialize-dynamic-object/embedded.json"); + + // when + ProductMap product = objectMapper.readValue(json, ProductMap.class); + + // then + assertThat(product.getName()).isEqualTo("Pear yPhone 72"); + assertThat(product.getDetails().get("audioConnector")).isEqualTo("none"); + } + + @Test + void givenJsonString_whenParsingWithJsonAnySetter_thenItMustContainDynamicProperties() throws JsonParseException, JsonMappingException, IOException { + // given + String json = readResource("/deserialize-dynamic-object/flat.json"); + + // when + ProductJsonAnySetter product = objectMapper.readValue(json, ProductJsonAnySetter.class); + + // then + assertThat(product.getName()).isEqualTo("Pear yPhone 72"); + assertThat(product.getDetails().get("audioConnector")).isEqualTo("none"); + } + +} diff --git a/jackson/src/test/resources/deserialize-dynamic-object/embedded.json b/jackson/src/test/resources/deserialize-dynamic-object/embedded.json new file mode 100644 index 0000000000..e4f11f7f1f --- /dev/null +++ b/jackson/src/test/resources/deserialize-dynamic-object/embedded.json @@ -0,0 +1,8 @@ +{ + "name": "Pear yPhone 72", + "category": "cellphone", + "details": { + "displayAspectRatio": "97:3", + "audioConnector": "none" + } +} \ No newline at end of file diff --git a/jackson/src/test/resources/deserialize-dynamic-object/flat.json b/jackson/src/test/resources/deserialize-dynamic-object/flat.json new file mode 100644 index 0000000000..799ea9339d --- /dev/null +++ b/jackson/src/test/resources/deserialize-dynamic-object/flat.json @@ -0,0 +1,6 @@ +{ + "name": "Pear yPhone 72", + "category": "cellphone", + "displayAspectRatio": "97:3", + "audioConnector": "none" +} \ No newline at end of file