diff --git a/src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java b/src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java index 82441eb33f..0450a9977f 100644 --- a/src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java +++ b/src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java @@ -282,8 +282,18 @@ public List findParents(String fieldName, List foundSoFar) public void serialize(JsonGenerator g, SerializerProvider provider) throws IOException { + SerializationConfig config = provider.getConfig(); g.writeStartObject(this); for (Map.Entry en : _children.entrySet()) { + + // check if WRITE_EMPTY_JSON_ARRAYS feature is disabled, + // if the feature is disabled, then should not write an empty array + // to the output, so continue to the next element in the iteration + if (!config.isEnabled(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS) && en.getValue() instanceof ArrayNode + && ((ArrayNode) en.getValue()).size() == 0) { + continue; + } + g.writeFieldName(en.getKey()); /* 17-Feb-2009, tatu: Can we trust that all nodes will always * extend BaseJsonNode? Or if not, at least implement @@ -300,8 +310,18 @@ public void serializeWithType(JsonGenerator g, SerializerProvider provider, TypeSerializer typeSer) throws IOException { + SerializationConfig config = provider.getConfig(); typeSer.writeTypePrefixForObject(this, g); for (Map.Entry en : _children.entrySet()) { + + // check if WRITE_EMPTY_JSON_ARRAYS feature is disabled, + // if the feature is disabled, then should not write an empty array + // to the output, so continue to the next element in the iteration + if (!config.isEnabled(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS) && en.getValue() instanceof ArrayNode + && ((ArrayNode) en.getValue()).size() == 0) { + continue; + } + g.writeFieldName(en.getKey()); ((BaseJsonNode) en.getValue()).serialize(g, provider); } diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestDefaultForArrays.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestDefaultForArrays.java index f7ce743c9c..154630de10 100644 --- a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestDefaultForArrays.java +++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestDefaultForArrays.java @@ -1,5 +1,10 @@ package com.fasterxml.jackson.databind.jsontype; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping; @@ -66,6 +71,24 @@ public void testNodeInArray() throws Exception Object ob = result[0]; assertTrue(ob instanceof JsonNode); } + + public void testNodeInEmptyArray() throws Exception { + Map> outerMap = new HashMap>(); + outerMap.put("inner", new ArrayList()); + ObjectMapper m = new ObjectMapper().disable(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS); + JsonNode tree = m.convertValue(outerMap, JsonNode.class); + + String json = m.writeValueAsString(tree); + assertEquals("{}", json); + + JsonNode node = new ObjectMapper().readTree("{\"a\":[]}"); + + m.enableDefaultTyping(DefaultTyping.JAVA_LANG_OBJECT); + Object[] obs = new Object[] { node }; + json = m.writeValueAsString(obs); + Object[] result = m.readValue(json, Object[].class); + assertEquals("{}", result[0].toString()); + } // test for [JACKSON-845] public void testArraysOfArrays() throws Exception