Skip to content

Commit

Permalink
Expose JsonObject size.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Jun 10, 2016
1 parent ebad966 commit c16be41
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions gson/src/main/java/com/google/gson/JsonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ public Set<Map.Entry<String, JsonElement>> entrySet() {
return members.entrySet();
}

/**
* Returns the number of key/value pairs in the object.
*
* @return the number of key/value pairs in the object.
*/
public int size() {
return members.size();
}

/**
* Convenience method to check if a member with the specified name is present in this object.
*
Expand Down
14 changes: 14 additions & 0 deletions gson/src/test/java/com/google/gson/JsonObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ public void testEqualsNonEmptyObject() {
assertFalse(b.equals(a));
}

public void testSize() {
JsonObject o = new JsonObject();
assertEquals(0, o.size());

o.add("Hello", new JsonPrimitive(1));
assertEquals(1, o.size());

o.add("Hi", new JsonPrimitive(1));
assertEquals(2, o.size());

o.remove("Hello");
assertEquals(1, o.size());
}

public void testDeepCopy() {
JsonObject original = new JsonObject();
JsonArray firstEntry = new JsonArray();
Expand Down

0 comments on commit c16be41

Please sign in to comment.