diff --git a/gson/src/main/java/com/google/gson/JsonObject.java b/gson/src/main/java/com/google/gson/JsonObject.java index 60dac41c4a..992ecce508 100644 --- a/gson/src/main/java/com/google/gson/JsonObject.java +++ b/gson/src/main/java/com/google/gson/JsonObject.java @@ -148,13 +148,23 @@ public Set keySet() { /** * Returns the number of key/value pairs in the object. * - * @return the number of key/value pairs in the object. + * @return the number of key/value pairs in the objezct. * @since 2.7 */ public int size() { return members.size(); } + /** + * Returns true if the number of key/value pairs in the object is zero. + * + * @return true if the number of key/value pairs in the object is zero. + * @since $next-version$ + */ + public boolean isEmpty() { + return members.size() == 0; + } + /** * 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 a0109ba863..d1dcabf969 100644 --- a/gson/src/test/java/com/google/gson/JsonObjectTest.java +++ b/gson/src/test/java/com/google/gson/JsonObjectTest.java @@ -222,6 +222,18 @@ public void testSize() { assertEquals(1, o.size()); } + @Test + public void testIsEmpty() { + JsonObject o = new JsonObject(); + assertEquals(true, o.isEmpty()); + + o.add("Hello", new JsonPrimitive(1)); + assertEquals(false, o.isEmpty()); + + o.remove("Hello"); + assertEquals(true, o.isEmpty()); + } + @Test public void testDeepCopy() { JsonObject original = new JsonObject();