bael-2437 (#5972)
This commit is contained in:
parent
5e02becb5e
commit
12bf5e9f3b
|
@ -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<String, Object> 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<String, Object> getDetails() {
|
||||
return details;
|
||||
}
|
||||
|
||||
@JsonAnySetter
|
||||
public void setDetail(String key, Object value) {
|
||||
details.put(key, value);
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<String, Object> 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<String, Object> getDetails() {
|
||||
return details;
|
||||
}
|
||||
|
||||
public void setDetails(Map<String, Object> details) {
|
||||
this.details = details;
|
||||
}
|
||||
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "Pear yPhone 72",
|
||||
"category": "cellphone",
|
||||
"details": {
|
||||
"displayAspectRatio": "97:3",
|
||||
"audioConnector": "none"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"name": "Pear yPhone 72",
|
||||
"category": "cellphone",
|
||||
"displayAspectRatio": "97:3",
|
||||
"audioConnector": "none"
|
||||
}
|
Loading…
Reference in New Issue