Skip to content

Commit a381060

Browse files
committed
Add testcase to assert Null fields serialization without JSONParserConfiguration
1 parent dadc3e5 commit a381060

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4025,9 +4025,23 @@ public void jsonObjectParseNullFieldsWithParserConfiguration() {
40254025
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
40264026
RecursiveBean bean = new RecursiveBean(null);
40274027
JSONObject jsonObject = new JSONObject(bean, jsonParserConfiguration.withUseNativeNulls(true));
4028-
String textStr = jsonObject.toString();
4029-
assertTrue("name(uninitialized field) should be serialized", textStr.contains("\"name\""));
4030-
assertTrue("ref(uninitialized field) should be serialized", textStr.contains("\"ref\""));
4031-
assertTrue("ref2(uninitialized field) should be serialized", textStr.contains("\"ref2\""));
4028+
assertTrue("name key should be present", jsonObject.has("name"));
4029+
assertTrue("ref key should be present", jsonObject.has("ref"));
4030+
assertTrue("ref2 key should be present", jsonObject.has("ref2"));
40324031
}
4032+
4033+
/**
4034+
* Tests the behavior of the {@link JSONObject} when parsing a bean with null fields
4035+
* without using a custom {@link JSONParserConfiguration}.
4036+
*
4037+
* <p>This test ensures that uninitialized fields in the bean are not serialized
4038+
* into the resulting JSON object, and the object remains empty.</p>
4039+
*/
4040+
@Test
4041+
public void jsonObjectParseNullFieldsWithoutParserConfiguration() {
4042+
RecursiveBean bean = new RecursiveBean(null);
4043+
JSONObject jsonObject = new JSONObject(bean);
4044+
assertTrue("JSONObject should be empty", jsonObject.isEmpty());
4045+
}
4046+
40334047
}

0 commit comments

Comments
 (0)