Skip to content

Commit

Permalink
Implement #679
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Apr 2, 2015
1 parent 881a5dd commit 759aef0
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 11 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION
Expand Up @@ -10,6 +10,7 @@ Project: jackson-databind
#348: ObjectMapper.valueToTree does not work with @JsonRawValue
(reported by Chris P, pimlottc@github)
#649: Make `BeanDeserializer` use new `parser.nextFieldName()` and `.hasTokenId()` methods
#679: Add `isEmpty()` implementation for `JsonNode` serializers
#696: Copy constructor does not preserve `_injectableValues`
(reported by Charles A)
#700: Cannot Change Default Abstract Type Mapper from LinkedHashMap
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/fasterxml/jackson/databind/JsonNode.java
Expand Up @@ -37,8 +37,8 @@
* and {@link ObjectMapper#treeAsTokens(TreeNode)}
*/
public abstract class JsonNode
implements TreeNode, Iterable<JsonNode>,
JsonSerializable // since 2.5; bit tricky if anyone is sub-classing but...
extends JsonSerializable.Base // i.e. implements JsonSerializable
implements TreeNode, Iterable<JsonNode>
{
/*
/**********************************************************
Expand Down
25 changes: 22 additions & 3 deletions src/main/java/com/fasterxml/jackson/databind/JsonSerializable.java
Expand Up @@ -15,9 +15,9 @@
* so -- if class is a bean, it can be serialized without
* implementing this interface.
*<p>
* NOTE: Jackson 2.0 added another method (from former "JsonSerializableWithType"),
* which is required for proper handling of case where additional type information
* is needed.
* Note that while it is possible to just directly implement {@link JsonSerializable},
* actual implementations are strongly recommended to instead extend
* {@link JsonSerializable.Base}.
*/
public interface JsonSerializable
{
Expand All @@ -44,4 +44,23 @@ public interface JsonSerializable
*/
public void serializeWithType(JsonGenerator gen, SerializerProvider serializers,
TypeSerializer typeSer) throws IOException;

/**
* Base class with minimal implementation, as well as couple of extension methods
* that core Jackson databinding makes use of.
* Use of this base class is strongly recommended over directly implementing
* {@link JsonSerializable}.
*
* @since 2.6
*/
public static class Base
{
/**
* Method that may be called on instance to determine if it is considered
* "empty" for purposes of serialization filtering or not.
*/
public boolean isEmpty(SerializerProvider serializers) {
return false;
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java
Expand Up @@ -43,6 +43,17 @@ public ArrayNode deepCopy()
return ret;
}

/*
/**********************************************************
/* Overrides for JsonSerializable.Base
/**********************************************************
*/

@Override
public boolean isEmpty(SerializerProvider serializers) {
return _children.isEmpty();
}

/*
/**********************************************************
/* Implementation of core JsonNode API
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java
@@ -1,8 +1,7 @@
package com.fasterxml.jackson.databind.node;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.util.RawValue;

Expand Down Expand Up @@ -61,6 +60,17 @@ public ObjectNode deepCopy()
return ret;
}

/*
/**********************************************************
/* Overrides for JsonSerializable.Base
/**********************************************************
*/

@Override
public boolean isEmpty(SerializerProvider serializers) {
return _children.isEmpty();
}

/*
/**********************************************************
/* Implementation of core JsonNode API
Expand Down
Expand Up @@ -37,14 +37,27 @@ public class SerializableSerializer
protected SerializableSerializer() { super(JsonSerializable.class); }

@Override
public void serialize(JsonSerializable value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
value.serialize(jgen, provider);
public boolean isEmpty(SerializerProvider serializers, JsonSerializable value) {
if (value instanceof JsonSerializable.Base) {
return ((JsonSerializable.Base) value).isEmpty(serializers);
}
return false;
}

@Deprecated
public boolean isEmpty(JsonSerializable value) {
return isEmpty(null, value);
}

@Override
public void serialize(JsonSerializable value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
value.serialize(gen, serializers);
}

@Override
public final void serializeWithType(JsonSerializable value, JsonGenerator jgen, SerializerProvider provider,
public final void serializeWithType(JsonSerializable value, JsonGenerator gen, SerializerProvider serializers,
TypeSerializer typeSer) throws IOException {
value.serializeWithType(jgen, provider, typeSer);
value.serializeWithType(gen, serializers, typeSer);
}

@Override
Expand Down
Expand Up @@ -4,6 +4,7 @@
import java.util.*;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
Expand Down Expand Up @@ -37,6 +38,15 @@ public DataImpl(JsonNode n) {
public ObjectNode value() { return root; }
*/
}

static class ObNodeWrapper {
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public ObjectNode node;

public ObNodeWrapper(ObjectNode n) {
node = n;
}
}

/*
/**********************************************************
Expand Down Expand Up @@ -366,4 +376,13 @@ public void testSimplePath() throws Exception
assertTrue(rnode.isObject());
assertEquals(3, rnode.path("a").intValue());
}

public void testNonEmptySerialization() throws Exception
{
ObNodeWrapper w = new ObNodeWrapper(MAPPER.createObjectNode()
.put("a", 3));
assertEquals("{\"node\":{\"a\":3}}", MAPPER.writeValueAsString(w));
w = new ObNodeWrapper(MAPPER.createObjectNode());
assertEquals("{}", MAPPER.writeValueAsString(w));
}
}

0 comments on commit 759aef0

Please sign in to comment.