Skip to content

Conversation

@harshith8854
Copy link
Contributor

This commit includes changes to consider JSONParserConfiguration.isUseNativeNulls() to decide if a null field to be serialized or not corresponding to issue #982
Also, includes the test case to test the same.

@harshith8854 harshith8854 changed the title Use JSONParserConfiguration to decide on serializing null fields into JSONObject #982 Use JSONParserConfiguration to decide on serializing Null fields into JSONObject #982 May 23, 2025
@stleary
Copy link
Owner

stleary commented May 23, 2025

@harshith8854 SonarCloud code analysis is still experimental; you can ignore those results.

JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
RecursiveBean bean = new RecursiveBean(null);
JSONObject jsonObject = new JSONObject(bean, jsonParserConfiguration.withUseNativeNulls(true));
String textStr = jsonObject.toString();
Copy link
Owner

Choose a reason for hiding this comment

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

Instead of converting to text, the jsonObject should be checked directly. For example,

assertTrue("missing name key", jsonObject.has("name"));
assertEquals("wrong name value", jsonObject.optString("name", "wrong"), null);

@stleary
Copy link
Owner

stleary commented May 24, 2025

What problem does this code solve?
Adds the ability to retain null-valued bean properties in JSONObjects

Does the code still compile with Java6?
Yes

Risks
Low

Changes to the API?
New API was added to include JSONParserConfiguration when parsing beans

Will this require a new release?
No

Should the documentation be updated?
No

Does it break the unit tests?
No, new unit tests were added

Was any code refactored in this commit?
SonarQube reported some pre-existing items, a separate issue has been opened (#984)

Review status
APPROVED

3-day comment window started

Note: There were 2 minor comments for the unit tests. Please fix them if you have time. Otherwise, I will include a fix in the #984 issue

@harshith8854
Copy link
Contributor Author

Sure, let me work on them

@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
C Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@stleary stleary merged commit 72a1a48 into stleary:master May 31, 2025
7 of 8 checks passed
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.

2 participants