diff --git a/src/main/java/org/json/XMLXsiTypeConverter.java b/src/main/java/org/json/XMLXsiTypeConverter.java index 48e4ac1..e97fc94 100644 --- a/src/main/java/org/json/XMLXsiTypeConverter.java +++ b/src/main/java/org/json/XMLXsiTypeConverter.java @@ -1,5 +1,66 @@ package org.json; +/* +Copyright (c) 2002 JSON.org +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +The Software shall be used for Good, not Evil. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +/** + * Type conversion configuration interface to be used with xsi:type attributes. + *
+ * 

XML Sample

+ * {@code + * + * 12345 + * 54321 + * + * } + *

JSON Output

+ * {@code + * { + * "root" : { + * "asString" : "12345", + * "asInt": 54321 + * } + * } + * } + * + *

Usage

+ * {@code + * Map> xsiTypeMap = new HashMap>(); + * xsiTypeMap.put("string", new XMLXsiTypeConverter() { + * @Override public String convert(final String value) { + * return value; + * } + * }); + * xsiTypeMap.put("integer", new XMLXsiTypeConverter() { + * @Override public Integer convert(final String value) { + * return Integer.valueOf(value); + * } + * }); + * } + *
+ * @author kumar529 + * @param + */ public interface XMLXsiTypeConverter { T convert(String value); } diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index 82c0b39..a244856 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1013,4 +1013,26 @@ public class XMLTest { Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson); } + @Test + public void testToJsonWithXSITypeWhenTypeConversionEnabled() { + String originalXml = "1234554321"; + String expectedJsonString = "{\"root\":{\"asString\":\"12345\",\"asInt\":54321}}"; + JSONObject expectedJson = new JSONObject(expectedJsonString); + Map> xsiTypeMap = new HashMap>(); + xsiTypeMap.put("string", new XMLXsiTypeConverter() { + @Override public String convert(final String value) { + return value; + } + }); + xsiTypeMap.put("integer", new XMLXsiTypeConverter() { + @Override public Integer convert(final String value) { + return Integer.valueOf(value); + } + }); + JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration(false, + "content", false, xsiTypeMap)); + Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson); + } + }