From 90db0721cd9b3595c7e2392dbc133c006b813f7b Mon Sep 17 00:00:00 2001 From: Doug Hoard Date: Fri, 4 Nov 2022 11:37:47 -0400 Subject: [PATCH 1/3] 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(); From 5ff17d195a7c448f4341dcd3b1beb5dd4a51f466 Mon Sep 17 00:00:00 2001 From: Doug Hoard Date: Tue, 8 Nov 2022 23:04:40 -0500 Subject: [PATCH 2/3] Fixed Javadoc typo --- gson/src/main/java/com/google/gson/JsonObject.java | 2 +- 1 file changed, 1 insertion(+), 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 992ecce508..fdf240d235 100644 --- a/gson/src/main/java/com/google/gson/JsonObject.java +++ b/gson/src/main/java/com/google/gson/JsonObject.java @@ -148,7 +148,7 @@ public Set keySet() { /** * Returns the number of key/value pairs in the object. * - * @return the number of key/value pairs in the objezct. + * @return the number of key/value pairs in the object. * @since 2.7 */ public int size() { From f35f2a68b305f13d2d79ddd351cd8bba35921afc Mon Sep 17 00:00:00 2001 From: Doug Hoard Date: Wed, 9 Nov 2022 08:26:39 -0500 Subject: [PATCH 3/3] Changed test to use assertTrue() and assertFalse() --- gson/src/test/java/com/google/gson/JsonObjectTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gson/src/test/java/com/google/gson/JsonObjectTest.java b/gson/src/test/java/com/google/gson/JsonObjectTest.java index d1dcabf969..6e1339c7c4 100644 --- a/gson/src/test/java/com/google/gson/JsonObjectTest.java +++ b/gson/src/test/java/com/google/gson/JsonObjectTest.java @@ -225,13 +225,13 @@ public void testSize() { @Test public void testIsEmpty() { JsonObject o = new JsonObject(); - assertEquals(true, o.isEmpty()); + assertTrue(o.isEmpty()); o.add("Hello", new JsonPrimitive(1)); - assertEquals(false, o.isEmpty()); + assertFalse(o.isEmpty()); o.remove("Hello"); - assertEquals(true, o.isEmpty()); + assertTrue(o.isEmpty()); } @Test