From 90db0721cd9b3595c7e2392dbc133c006b813f7b Mon Sep 17 00:00:00 2001 From: Doug Hoard Date: Fri, 4 Nov 2022 11:37:47 -0400 Subject: [PATCH] Added isEmpty() --- gson/src/main/java/com/google/gson/JsonObject.java | 12 +++++++++++- .../test/java/com/google/gson/JsonObjectTest.java | 12 ++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) 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();