Add testcase to assert Null fields serialization without JSONParserConfiguration

This commit is contained in:
hboggavarapu 2025-05-24 21:54:12 +05:30
parent dadc3e59dc
commit a381060f81

View File

@ -4025,9 +4025,23 @@ public class JSONObjectTest {
JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration();
RecursiveBean bean = new RecursiveBean(null); RecursiveBean bean = new RecursiveBean(null);
JSONObject jsonObject = new JSONObject(bean, jsonParserConfiguration.withUseNativeNulls(true)); JSONObject jsonObject = new JSONObject(bean, jsonParserConfiguration.withUseNativeNulls(true));
String textStr = jsonObject.toString(); assertTrue("name key should be present", jsonObject.has("name"));
assertTrue("name(uninitialized field) should be serialized", textStr.contains("\"name\"")); assertTrue("ref key should be present", jsonObject.has("ref"));
assertTrue("ref(uninitialized field) should be serialized", textStr.contains("\"ref\"")); assertTrue("ref2 key should be present", jsonObject.has("ref2"));
assertTrue("ref2(uninitialized field) should be serialized", textStr.contains("\"ref2\""));
} }
/**
* Tests the behavior of the {@link JSONObject} when parsing a bean with null fields
* without using a custom {@link JSONParserConfiguration}.
*
* <p>This test ensures that uninitialized fields in the bean are not serialized
* into the resulting JSON object, and the object remains empty.</p>
*/
@Test
public void jsonObjectParseNullFieldsWithoutParserConfiguration() {
RecursiveBean bean = new RecursiveBean(null);
JSONObject jsonObject = new JSONObject(bean);
assertTrue("JSONObject should be empty", jsonObject.isEmpty());
}
} }