From c16be41e77bb53a4b639cb864c9a6e4d0f8df7c2 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Fri, 10 Jun 2016 00:46:32 -0400 Subject: [PATCH] Expose JsonObject size. --- gson/src/main/java/com/google/gson/JsonObject.java | 9 +++++++++ .../test/java/com/google/gson/JsonObjectTest.java | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/gson/src/main/java/com/google/gson/JsonObject.java b/gson/src/main/java/com/google/gson/JsonObject.java index 78c7a177..62101918 100644 --- a/gson/src/main/java/com/google/gson/JsonObject.java +++ b/gson/src/main/java/com/google/gson/JsonObject.java @@ -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. * diff --git a/gson/src/test/java/com/google/gson/JsonObjectTest.java b/gson/src/test/java/com/google/gson/JsonObjectTest.java index 9423a24d..32d9d360 100644 --- a/gson/src/test/java/com/google/gson/JsonObjectTest.java +++ b/gson/src/test/java/com/google/gson/JsonObjectTest.java @@ -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();