Repository: stleary/JSON-java (2025-07-06T20:45:52Z)

Issue:
Regression XML parsing null with keepStrings
<t>null</t> used to be {"t":"null"} but now its returning {"t":null} with useStrings

I expected everything to String as it was in the previous version([20250107](https://github.com/stleary/JSON-java/releases/tag/20250107)


Discussion:
@babita-verma Sorry for not getting to this sooner. A complete code fragment will be helpful; otherwise, I will try to work with your initial post. 
   We have this test which fails after the version upgrade. What you think think should happen in this case? when we use  useStrings as true ?

 void testValueConversion() {
    // This test case does not cover functionality in XMLUtil.
    // Instead, it covers the ASSUMED configuration of the conversion method.

    final var convertedJson = XML.toJSONObject("<t>null</t>", true);
    // Parse expected value first to ensure semantic equality, not format equality.
    final var expectedJson = new JSONObject("{\"t\": \"null\"}");

    assertThat(convertedJson.toString()).isEqualTo(expectedJson.toString());
  }
@babita-verma Thanks for catching this. I think the previous behavior was per design, the current behavior is a regression. Looks like we forgot to check for null, when adding support for numberAsString, and booleanAsString. This will be fixed in a future commit. FYI: @marilynel 

![Image](https://github.com/user-attachments/assets/7f350681-98f7-424c-80ac-c735919aa583)
Thanks for confirming. The code confused me whether it was intentional to only stringify number and Boolean. Wait for the fix 



Patch:
diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java
index 4bf475935..3eb948c77 100644
--- a/src/main/java/org/json/XML.java
+++ b/src/main/java/org/json/XML.java
@@ -428,6 +428,9 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
                                                 config.isKeepNumberAsString()
                                                         ? ((String) token)
                                                         : obj);
+                                    } else if (obj == JSONObject.NULL) {
+                                        jsonObject.accumulate(config.getcDataTagName(),
+                                                config.isKeepStrings() ? ((String) token) : obj);
                                     } else {
                                         jsonObject.accumulate(config.getcDataTagName(), stringToValue((String) token));
                                     }


Test patch:
diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java
index 938c7c806..ca1980c8a 100755
--- a/src/test/java/org/json/junit/XMLConfigurationTest.java
+++ b/src/test/java/org/json/junit/XMLConfigurationTest.java
@@ -775,8 +775,8 @@ public void testToJSONArray_jsonOutput() {
      */
     @Test
     public void testToJSONArray_jsonOutput_withKeepNumberAsString() {
-        final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
-        final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":true}}");
+        final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><id>null</id><item id=\"01\"/><title>True</title></root>";
+        final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\",null],\"title\":true}}");
         final JSONObject actualJsonOutput = XML.toJSONObject(originalXml,
                 new XMLParserConfiguration().withKeepNumberAsString(true));
         Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected);
@@ -787,13 +787,25 @@ public void testToJSONArray_jsonOutput_withKeepNumberAsString() {
      */
     @Test
     public void testToJSONArray_jsonOutput_withKeepBooleanAsString() {
-        final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>True</title></root>";
-        final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":\"True\"}}");
+        final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><id>null</id><item id=\"01\"/><title>True</title></root>";
+        final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0,null],\"title\":\"True\"}}");
         final JSONObject actualJsonOutput = XML.toJSONObject(originalXml,
                 new XMLParserConfiguration().withKeepBooleanAsString(true));
         Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected);
     }
 
+    /**
+     * null is "null" when keepStrings == true
+     */
+    @Test
+    public void testToJSONArray_jsonOutput_null_withKeepString() {
+        final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>null</title></root>";
+        final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"null\"}}");
+        final JSONObject actualJsonOutput = XML.toJSONObject(originalXml,
+                new XMLParserConfiguration().withKeepStrings(true));
+        Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected);
+    }
+
     /**
      * Test keepStrings behavior when setting keepBooleanAsString, keepNumberAsString
      */