@@ -38,13 +38,16 @@ of this software and associated documentation files (the "Software"), to deal
3838import java .io .InputStreamReader ;
3939import java .io .Reader ;
4040import java .io .StringReader ;
41+ import java .util .HashMap ;
42+ import java .util .Map ;
4143
4244import org .json .JSONArray ;
4345import org .json .JSONException ;
4446import org .json .JSONObject ;
4547import org .json .JSONTokener ;
4648import org .json .XML ;
4749import org .json .XMLParserConfiguration ;
50+ import org .json .XMLXsiTypeConverter ;
4851import org .junit .Rule ;
4952import org .junit .Test ;
5053import org .junit .rules .TemporaryFolder ;
@@ -972,5 +975,97 @@ public void testIssue537CaseSensitiveHexUnEscapeDirect(){
972975
973976 assertEquals ("Case insensitive Entity unescape" , expectedStr , actualStr );
974977 }
975-
976- }
978+
979+ /**
980+ * test passes when xsi:type="java.lang.String" not converting to string
981+ */
982+ @ Test
983+ public void testToJsonWithTypeWhenTypeConversionDisabled () {
984+ String originalXml = "<root><id xsi:type=\" string\" >1234</id></root>" ;
985+ String expectedJsonString = "{\" root\" :{\" id\" :{\" xsi:type\" :\" string\" ,\" content\" :1234}}}" ;
986+ JSONObject expectedJson = new JSONObject (expectedJsonString );
987+ JSONObject actualJson = XML .toJSONObject (originalXml , new XMLParserConfiguration ());
988+ Util .compareActualVsExpectedJsonObjects (actualJson ,expectedJson );
989+ }
990+
991+ /**
992+ * test passes when xsi:type="java.lang.String" converting to String
993+ */
994+ @ Test
995+ public void testToJsonWithTypeWhenTypeConversionEnabled () {
996+ String originalXml = "<root><id1 xsi:type=\" string\" >1234</id1>"
997+ + "<id2 xsi:type=\" integer\" >1234</id2></root>" ;
998+ String expectedJsonString = "{\" root\" :{\" id2\" :1234,\" id1\" :\" 1234\" }}" ;
999+ JSONObject expectedJson = new JSONObject (expectedJsonString );
1000+ Map <String , XMLXsiTypeConverter <?>> xsiTypeMap = new HashMap <String , XMLXsiTypeConverter <?>>();
1001+ xsiTypeMap .put ("string" , new XMLXsiTypeConverter <String >() {
1002+ @ Override public String convert (final String value ) {
1003+ return value ;
1004+ }
1005+ });
1006+ xsiTypeMap .put ("integer" , new XMLXsiTypeConverter <Integer >() {
1007+ @ Override public Integer convert (final String value ) {
1008+ return Integer .valueOf (value );
1009+ }
1010+ });
1011+ JSONObject actualJson = XML .toJSONObject (originalXml , new XMLParserConfiguration ().withXsiTypeMap (xsiTypeMap ));
1012+ Util .compareActualVsExpectedJsonObjects (actualJson ,expectedJson );
1013+ }
1014+
1015+ @ Test
1016+ public void testToJsonWithXSITypeWhenTypeConversionEnabled () {
1017+ String originalXml = "<root><asString xsi:type=\" string\" >12345</asString><asInt "
1018+ + "xsi:type=\" integer\" >54321</asInt></root>" ;
1019+ String expectedJsonString = "{\" root\" :{\" asString\" :\" 12345\" ,\" asInt\" :54321}}" ;
1020+ JSONObject expectedJson = new JSONObject (expectedJsonString );
1021+ Map <String , XMLXsiTypeConverter <?>> xsiTypeMap = new HashMap <String , XMLXsiTypeConverter <?>>();
1022+ xsiTypeMap .put ("string" , new XMLXsiTypeConverter <String >() {
1023+ @ Override public String convert (final String value ) {
1024+ return value ;
1025+ }
1026+ });
1027+ xsiTypeMap .put ("integer" , new XMLXsiTypeConverter <Integer >() {
1028+ @ Override public Integer convert (final String value ) {
1029+ return Integer .valueOf (value );
1030+ }
1031+ });
1032+ JSONObject actualJson = XML .toJSONObject (originalXml , new XMLParserConfiguration ().withXsiTypeMap (xsiTypeMap ));
1033+ Util .compareActualVsExpectedJsonObjects (actualJson ,expectedJson );
1034+ }
1035+
1036+ @ Test
1037+ public void testToJsonWithXSITypeWhenTypeConversionNotEnabledOnOne () {
1038+ String originalXml = "<root><asString xsi:type=\" string\" >12345</asString><asInt>54321</asInt></root>" ;
1039+ String expectedJsonString = "{\" root\" :{\" asString\" :\" 12345\" ,\" asInt\" :54321}}" ;
1040+ JSONObject expectedJson = new JSONObject (expectedJsonString );
1041+ Map <String , XMLXsiTypeConverter <?>> xsiTypeMap = new HashMap <String , XMLXsiTypeConverter <?>>();
1042+ xsiTypeMap .put ("string" , new XMLXsiTypeConverter <String >() {
1043+ @ Override public String convert (final String value ) {
1044+ return value ;
1045+ }
1046+ });
1047+ JSONObject actualJson = XML .toJSONObject (originalXml , new XMLParserConfiguration ().withXsiTypeMap (xsiTypeMap ));
1048+ Util .compareActualVsExpectedJsonObjects (actualJson ,expectedJson );
1049+ }
1050+
1051+ @ Test
1052+ public void testXSITypeMapNotModifiable () {
1053+ Map <String , XMLXsiTypeConverter <?>> xsiTypeMap = new HashMap <String , XMLXsiTypeConverter <?>>();
1054+ XMLParserConfiguration config = new XMLParserConfiguration ().withXsiTypeMap (xsiTypeMap );
1055+ xsiTypeMap .put ("string" , new XMLXsiTypeConverter <String >() {
1056+ @ Override public String convert (final String value ) {
1057+ return value ;
1058+ }
1059+ });
1060+ assertEquals ("Config Conversion Map size is expected to be 0" , 0 , config .getXsiTypeMap ().size ());
1061+
1062+ try {
1063+ config .getXsiTypeMap ().put ("boolean" , new XMLXsiTypeConverter <Boolean >() {
1064+ @ Override public Boolean convert (final String value ) {
1065+ return Boolean .valueOf (value );
1066+ }
1067+ });
1068+ fail ("Expected to be unable to modify the config" );
1069+ } catch (Exception ignored ) { }
1070+ }
1071+ }
0 commit comments