Expose JsonObject size.

This commit is contained in:
Jake Wharton 2016-06-10 00:46:32 -04:00
parent ebad966efd
commit c16be41e77
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();