Skip to content

Commit

Permalink
Added isEmpty()
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoard committed Nov 6, 2022
1 parent ff96296 commit ec19d71
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();
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();
Expand Down

0 comments on commit ec19d71

Please sign in to comment.