From 3dce55794f2805563918ff13bbbd04750cd1ab90 Mon Sep 17 00:00:00 2001 From: marilynel Date: Sun, 6 Jul 2025 12:37:05 -0800 Subject: [PATCH 1/2] fixed keeping null as string --- src/main/java/org/json/XML.java | 3 +++ .../java/org/json/junit/XMLConfigurationTest.java | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java index 4bf4759..3eb948c 100644 --- a/src/main/java/org/json/XML.java +++ b/src/main/java/org/json/XML.java @@ -428,6 +428,9 @@ public class XML { 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)); } diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java index 938c7c8..4ad06c1 100755 --- a/src/test/java/org/json/junit/XMLConfigurationTest.java +++ b/src/test/java/org/json/junit/XMLConfigurationTest.java @@ -794,6 +794,18 @@ public class XMLConfigurationTest { Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected); } + /** + * null is "null" when keepStrings == true + */ + @Test + public void testToJSONArray_jsonOutput_null_withKeepString() { + final String originalXml = "011000null"; + 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 */ From 7bb3df8ebf22f8cf427574a859a55e021be1e785 Mon Sep 17 00:00:00 2001 From: marilynel Date: Sun, 6 Jul 2025 12:41:44 -0800 Subject: [PATCH 2/2] added test details --- src/test/java/org/json/junit/XMLConfigurationTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/org/json/junit/XMLConfigurationTest.java b/src/test/java/org/json/junit/XMLConfigurationTest.java index 4ad06c1..ca1980c 100755 --- a/src/test/java/org/json/junit/XMLConfigurationTest.java +++ b/src/test/java/org/json/junit/XMLConfigurationTest.java @@ -775,8 +775,8 @@ public class XMLConfigurationTest { */ @Test public void testToJSONArray_jsonOutput_withKeepNumberAsString() { - final String originalXml = "011000True"; - final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":true}}"); + final String originalXml = "011000nullTrue"; + 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,8 +787,8 @@ public class XMLConfigurationTest { */ @Test public void testToJSONArray_jsonOutput_withKeepBooleanAsString() { - final String originalXml = "011000True"; - final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",1,\"00\",0],\"title\":\"True\"}}"); + final String originalXml = "011000nullTrue"; + 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);