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 90db072
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion gson/src/main/java/com/google/gson/JsonObject.java
Expand Up @@ -148,13 +148,23 @@ public Set<String> 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.
*
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();
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 90db072

Please sign in to comment.