Skip to content

Commit

Permalink
Fix #2204
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 11, 2018
1 parent 8ec1e5e commit 644b5ce
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 8 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Project: jackson-databind
#2126: `DeserializationContext.instantiationException()` throws `InvalidDefinitionException`
#2153: Add `JsonMapper` to replace generic `ObjectMapper` usage
#2187: Make `JsonNode.toString()` use shared `ObjectMapper` to produce valid json
#2204: Add `JsonNode.isEmpty()` as convenience alias

2.9.8 (not yet released)

Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/fasterxml/jackson/databind/JsonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ protected JsonNode() { }
@Override
public int size() { return 0; }

/**
* Convenience method that is functionally same as:
*<pre>
* size() == 0
*<pre>
* for all node types.
*
* @since 2.10
*/
public boolean isEmpty() { return size() == 0; }

@Override
public final boolean isValueNode()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,17 @@ public int size() {
return _children.size();
}

@Override // since 2.10
public boolean isEmpty() { return _children.isEmpty(); }

@Override
public Iterator<JsonNode> elements() {
return _children.iterator();
}

@Override
public JsonNode get(int index) {
if (index >= 0 && index < _children.size()) {
if ((index >= 0) && (index < _children.size())) {
return _children.get(index);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ public abstract void serializeWithType(JsonGenerator jgen, SerializerProvider pr
*/

@Override
public final String toString() {
public String toString() {
return InternalNodeMapper.nodeToString(this);
}

@Override
public final String toPrettyString() {
public String toPrettyString() {
return InternalNodeMapper.nodeToPrettyString(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public int size() {
return _children.size();
}

@Override // since 2.10
public boolean isEmpty() { return _children.isEmpty(); }

@Override
public Iterator<JsonNode> elements() {
return _children.values().iterator();
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/fasterxml/jackson/databind/node/ValueNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,18 @@ public void serializeWithType(JsonGenerator g, SerializerProvider provider,
}

/*
**********************************************************************
* Navigation methods
**********************************************************************
/**********************************************************************
/* Basic property access
/**********************************************************************
*/

@Override
public boolean isEmpty() { return true; }

/*
/**********************************************************************
/* Navigation methods
/**********************************************************************
*/

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ public void testDirectCreation() throws IOException
assertStandardEquals(n);
assertFalse(n.elements().hasNext());
assertFalse(n.fieldNames().hasNext());
assertTrue(n.isEmpty());
TextNode text = TextNode.valueOf("x");
n.add(text);
assertEquals(1, n.size());
assertFalse(n.isEmpty());
assertFalse(0 == n.hashCode());
assertTrue(n.elements().hasNext());
// no field names for arrays
Expand Down Expand Up @@ -149,6 +151,7 @@ public void testAdds()
n.add(new BigDecimal("0.2"));
n.add(BigInteger.TEN);
assertEquals(9, n.size());
assertFalse(n.isEmpty());

assertNotNull(n.insertArray(0));
assertNotNull(n.insertObject(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ protected void assertNodeNumbers(JsonNode n, int expInt, double expDouble)
assertEquals((long) expInt, n.asLong(19L));
assertEquals(expDouble, n.asDouble());
assertEquals(expDouble, n.asDouble(-19.25));
}

assertTrue(n.isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void testSimpleObject() throws Exception
assertFalse(root.isArray());
assertTrue(root.isObject());
assertEquals(2, root.size());
assertFalse(root.isEmpty());

Iterator<JsonNode> it = root.iterator();
assertNotNull(it);
Expand Down Expand Up @@ -127,7 +128,8 @@ public void testBasics()
{
ObjectNode n = new ObjectNode(JsonNodeFactory.instance);
assertStandardEquals(n);

assertTrue(n.isEmpty());

assertFalse(n.elements().hasNext());
assertFalse(n.fields().hasNext());
assertFalse(n.fieldNames().hasNext());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void testBasicsWithNullNode() throws Exception
assertEquals(BigInteger.ZERO, n.bigIntegerValue());

assertEquals(0, n.size());
assertTrue(n.isEmpty());
assertFalse(n.elements().hasNext());
assertFalse(n.fieldNames().hasNext());
// path is never null; but does point to missing node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public void testText()
assertStandardEquals(empty);
assertSame(TextNode.EMPTY_STRING_NODE, empty);

assertEquals(0, empty.size());
assertTrue(empty.isEmpty());

assertNodeNumbers(TextNode.valueOf("-3"), -3, -3.0);
assertNodeNumbers(TextNode.valueOf("17.75"), 17, 17.75);

Expand Down

0 comments on commit 644b5ce

Please sign in to comment.