Skip to content

Commit

Permalink
Added JsonObject method isEmpty() (google#2233)
Browse files Browse the repository at this point in the history
* Added isEmpty()

* Fixed Javadoc typo

* Changed test to use assertTrue() and assertFalse()

(cherry picked from commit ceb3b87)
  • Loading branch information
dhoard authored and tibor-universe committed Jan 18, 2023
1 parent 87f757b commit 6b29f04
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions gson/src/main/java/com/google/gson/JsonObject.java
Expand Up @@ -155,6 +155,16 @@ 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.
*
Expand Down
12 changes: 12 additions & 0 deletions gson/src/test/java/com/google/gson/JsonObjectTest.java
Expand Up @@ -222,6 +222,18 @@ public void testSize() {
assertEquals(1, o.size());
}

@Test
public void testIsEmpty() {
JsonObject o = new JsonObject();
assertTrue(o.isEmpty());

o.add("Hello", new JsonPrimitive(1));
assertFalse(o.isEmpty());

o.remove("Hello");
assertTrue(o.isEmpty());
}

@Test
public void testDeepCopy() {
JsonObject original = new JsonObject();
Expand Down

0 comments on commit 6b29f04

Please sign in to comment.