Skip to content

Commit 3dce557

Browse files
committed
fixed keeping null as string
1 parent d7593fb commit 3dce557

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/main/java/org/json/XML.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ private static boolean parse(XMLTokener x, JSONObject context, String name, XMLP
428428
config.isKeepNumberAsString()
429429
? ((String) token)
430430
: obj);
431+
} else if (obj == JSONObject.NULL) {
432+
jsonObject.accumulate(config.getcDataTagName(),
433+
config.isKeepStrings() ? ((String) token) : obj);
431434
} else {
432435
jsonObject.accumulate(config.getcDataTagName(), stringToValue((String) token));
433436
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,18 @@ public void testToJSONArray_jsonOutput_withKeepBooleanAsString() {
794794
Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected);
795795
}
796796

797+
/**
798+
* null is "null" when keepStrings == true
799+
*/
800+
@Test
801+
public void testToJSONArray_jsonOutput_null_withKeepString() {
802+
final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\"01\"/><title>null</title></root>";
803+
final JSONObject expected = new JSONObject("{\"root\":{\"item\":{\"id\":\"01\"},\"id\":[\"01\",\"1\",\"00\",\"0\"],\"title\":\"null\"}}");
804+
final JSONObject actualJsonOutput = XML.toJSONObject(originalXml,
805+
new XMLParserConfiguration().withKeepStrings(true));
806+
Util.compareActualVsExpectedJsonObjects(actualJsonOutput,expected);
807+
}
808+
797809
/**
798810
* Test keepStrings behavior when setting keepBooleanAsString, keepNumberAsString
799811
*/

0 commit comments

Comments
 (0)