Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JsonObject method isEmpty() #2233

Merged
merged 3 commits into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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$
*/
dhoard marked this conversation as resolved.
Show resolved Hide resolved
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