Merge pull request #872 from google/jw/object-size

Expose JsonObject size.
This commit is contained in:
Jesse Wilson 2016-06-09 23:06:50 -07:00 committed by GitHub
commit 5f63fcec98
2 changed files with 23 additions and 0 deletions

View File

@ -132,6 +132,15 @@ public final class JsonObject extends JsonElement {
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.
*

View File

@ -158,6 +158,20 @@ public class JsonObjectTest extends TestCase {
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();