From a381060f81725743732ea7e1d8d67f080ee3f9ce Mon Sep 17 00:00:00 2001 From: hboggavarapu Date: Sat, 24 May 2025 21:54:12 +0530 Subject: [PATCH] Add testcase to assert Null fields serialization without JSONParserConfiguration --- .../java/org/json/junit/JSONObjectTest.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 02196a5..061f185 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -4025,9 +4025,23 @@ public class JSONObjectTest { JSONParserConfiguration jsonParserConfiguration = new JSONParserConfiguration(); RecursiveBean bean = new RecursiveBean(null); JSONObject jsonObject = new JSONObject(bean, jsonParserConfiguration.withUseNativeNulls(true)); - String textStr = jsonObject.toString(); - assertTrue("name(uninitialized field) should be serialized", textStr.contains("\"name\"")); - assertTrue("ref(uninitialized field) should be serialized", textStr.contains("\"ref\"")); - assertTrue("ref2(uninitialized field) should be serialized", textStr.contains("\"ref2\"")); + assertTrue("name key should be present", jsonObject.has("name")); + assertTrue("ref key should be present", jsonObject.has("ref")); + assertTrue("ref2 key should be present", jsonObject.has("ref2")); } + + /** + * Tests the behavior of the {@link JSONObject} when parsing a bean with null fields + * without using a custom {@link JSONParserConfiguration}. + * + *

This test ensures that uninitialized fields in the bean are not serialized + * into the resulting JSON object, and the object remains empty.

+ */ + @Test + public void jsonObjectParseNullFieldsWithoutParserConfiguration() { + RecursiveBean bean = new RecursiveBean(null); + JSONObject jsonObject = new JSONObject(bean); + assertTrue("JSONObject should be empty", jsonObject.isEmpty()); + } + }