Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prune out empty JSON arrays in JsonNode #1245

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,18 @@ public List<JsonNode> findParents(String fieldName, List<JsonNode> foundSoFar)
public void serialize(JsonGenerator g, SerializerProvider provider)
throws IOException
{
SerializationConfig config = provider.getConfig();
g.writeStartObject(this);
for (Map.Entry<String, JsonNode> 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
Expand All @@ -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<String, JsonNode> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -66,6 +71,24 @@ public void testNodeInArray() throws Exception
Object ob = result[0];
assertTrue(ob instanceof JsonNode);
}

public void testNodeInEmptyArray() throws Exception {
Map<String, List<String>> outerMap = new HashMap<String, List<String>>();
outerMap.put("inner", new ArrayList<String>());
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
Expand Down