Skip to content

Commit

Permalink
Adding support for Jackson JsonNode
Browse files Browse the repository at this point in the history
  • Loading branch information
monitorjbl committed Jul 5, 2018
1 parent 36f3930 commit cdef1cd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Expand Up @@ -9,6 +9,7 @@
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
Expand Down Expand Up @@ -342,6 +343,9 @@ void writeObject(Object obj) throws IOException {
} else {
new JsonWriter(jgen, result, currentMatch, currentPath, path, property, serializerProvider).write(name, val);
}
} else if(val instanceof JsonNode) {
// Let Jackson deal with these, they're special
serializerProvider.defaultSerializeValue(val, jgen);
} else {
new JsonWriter(jgen, result, currentMatch, currentPath, path, property, serializerProvider).write(name, val);
}
Expand Down
Expand Up @@ -3,6 +3,10 @@
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
Expand Down Expand Up @@ -1090,4 +1094,46 @@ public void testSerializationOrder() throws Exception {
assertEquals("date", keys.get(4));
}

@Test
public void testJacksonJsonNodeSupport_object() throws Exception {
TestObject ref = new TestObject();
ObjectNode node1 = sut.createObjectNode();
ObjectNode node2 = sut.createObjectNode();
node2.set("stringfield", new TextNode("hello"));
node1.set("jacksonObject", node2);
ref.setJsonNode(node1);

String serialized = sut.writeValueAsString(JsonView.with(ref));
Map<String, Object> obj = sut.readValue(serialized, LinkedHashMap.class);

assertNotNull(obj.get("jsonNode"));
assertTrue(obj.get("jsonNode") instanceof Map);

Map<String, Object> jsonNode = (Map<String, Object>) obj.get("jsonNode");
assertTrue(jsonNode.get("jacksonObject") instanceof Map);
assertEquals("hello", ((Map) jsonNode.get("jacksonObject")).get("stringfield"));
}

@Test
public void testJacksonJsonNodeSupport_textNode() throws Exception {
TestObject ref = new TestObject();
ref.setJsonNode(new TextNode("asdf"));

String serialized = sut.writeValueAsString(JsonView.with(ref));
Map<String, Object> obj = sut.readValue(serialized, LinkedHashMap.class);

assertEquals(obj.get("jsonNode"), "asdf");
}

@Test
public void testJacksonJsonNodeSupport_nullNode() throws Exception {
TestObject ref = new TestObject();
ref.setJsonNode(NullNode.getInstance());

String serialized = sut.writeValueAsString(JsonView.with(ref));
Map<String, Object> obj = sut.readValue(serialized, LinkedHashMap.class);

assertNull(obj.get("jsonNode"));
}

}
10 changes: 10 additions & 0 deletions json-view/src/test/java/com/monitorjbl/json/model/TestObject.java
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.math.BigDecimal;
Expand Down Expand Up @@ -60,6 +61,7 @@ public enum TestEnum {VALUE_A, VALUE_B}
private String jsonPropNoValue;
private UUID uuid;
private TestObject recursion;
private JsonNode jsonNode;

public String getStr1() {
return str1;
Expand Down Expand Up @@ -301,6 +303,14 @@ public void setRecursion(TestObject recursion) {
this.recursion = recursion;
}

public JsonNode getJsonNode() {
return jsonNode;
}

public void setJsonNode(JsonNode jsonNode) {
this.jsonNode = jsonNode;
}

public String getStaticValue() {
return "TEST";
}
Expand Down

0 comments on commit cdef1cd

Please sign in to comment.