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()
  • Loading branch information
dhoard authored Nov 13, 2022
1 parent 92b7ae0 commit ceb3b87
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 ceb3b87

Please sign in to comment.