Skip to content

Commit 6dcd82a

Browse files
authored
Merge pull request #93 from meiskalt7/xsiNilToNull
add test for xsi:nil to null conversion
2 parents 437ce10 + fa173fa commit 6dcd82a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.json.JSONException;
1818
import org.json.JSONObject;
1919
import org.json.XML;
20+
import org.json.XMLParserConfiguration;
2021
import org.junit.Rule;
2122
import org.junit.Test;
2223
import org.junit.rules.TemporaryFolder;
@@ -856,4 +857,27 @@ public void testUnescape() {
856857

857858
}
858859

860+
/**
861+
* test passes when xsi:nil="true" converting to null (JSON specification-like nil conversion enabled)
862+
*/
863+
@Test
864+
public void testToJsonWithNullWhenNilConversionEnabled() {
865+
final String originalXml = "<root><id xsi:nil=\"true\"/></root>";
866+
final String expectedJsonString = "{\"root\":{\"id\":null}}";
867+
868+
final JSONObject json = XML.toJSONObject(originalXml, new XMLParserConfiguration(false, "content", true));
869+
assertEquals(expectedJsonString, json.toString());
870+
}
871+
872+
/**
873+
* test passes when xsi:nil="true" not converting to null (JSON specification-like nil conversion disabled)
874+
*/
875+
@Test
876+
public void testToJsonWithNullWhenNilConversionDisabled() {
877+
final String originalXml = "<root><id xsi:nil=\"true\"/></root>";
878+
final String expectedJsonString = "{\"root\":{\"id\":{\"xsi:nil\":true}}}";
879+
880+
final JSONObject json = XML.toJSONObject(originalXml, new XMLParserConfiguration());
881+
assertEquals(expectedJsonString, json.toString());
882+
}
859883
}

0 commit comments

Comments
 (0)