Skip to content

Added JsonObject method isEmpty() - #2233

Merged
eamonnmcmanus merged 3 commits into
google:masterfrom
dhoard:jsonobject-is-empty
Nov 13, 2022
Merged

eamonnmcmanus merged 3 commits into
google:masterfrom
dhoard:jsonobject-is-empty

Conversation

@dhoard

@dhoard dhoard commented Nov 4, 2022

Copy link
Copy Markdown
Contributor

Code change

Added JsonObject method isEmpty().

Rationale

There are scenarios where there is a need to check whether a JsonObject contains any key/value pairs.

This mirrors the existing functionality of JsonArray.isEmpty()

@Marcono1234 Marcono1234 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this pull request!

Could you please for completeness sake also add a small test to JsonObjectTest?

Your argument about consistency with JsonArray.isEmpty() is reasonable, but on the other hand the question might be how many Map convenience methods we want to add, see also related #1667 (comment).

@dhoard
dhoard force-pushed the jsonobject-is-empty branch from 268cee7 to ec19d71 Compare November 6, 2022 14:18
@dhoard

dhoard commented Nov 6, 2022

Copy link
Copy Markdown
Contributor Author

Your argument about consistency with JsonArray.isEmpty() is reasonable, but on the other hand the question might be how many Map convenience methods we want to add, see also related #1667 (comment).

I feel that isEmpty() and clear() ( mentioned in #1667 (comment) ) make sense because they are around the object's internal state.

Other Map methods are around conversion, which I see as a different aspect, and agree they can become unwieldy.

@dhoard
dhoard force-pushed the jsonobject-is-empty branch from ec19d71 to 90db072 Compare November 6, 2022 15:31

@mariocandela mariocandela left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about it ?

@eamonnmcmanus

Copy link
Copy Markdown
Member

Given that we already have both JsonArray.isEmpty() and JsonObject.size(), I agree that this method makes sense for consistency. Additionally, it appears that most of Google's internal uses of JsonObject.size() in non-test code are comparing the size against 0.

@eamonnmcmanus
eamonnmcmanus merged commit ceb3b87 into google:master Nov 13, 2022
tibor-universe pushed a commit to getuniverse/gson that referenced this pull request Jan 20, 2023
* Added isEmpty()

* Fixed Javadoc typo

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

(cherry picked from commit ceb3b87)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants