From 310f18fcdc288f04d083a844d90576dfff8db814 Mon Sep 17 00:00:00 2001 From: Rahul Kumar Date: Thu, 3 Sep 2020 11:17:10 +0530 Subject: [PATCH] Addressed comment --- src/main/java/org/json/XML.java | 4 +- .../java/org/json/XMLParserConfiguration.java | 40 ++++++++++++++++--- src/test/java/org/json/junit/XMLTest.java | 6 +-- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/json/XML.java b/src/main/java/org/json/XML.java index 004a1f8..805a5c3 100644 --- a/src/main/java/org/json/XML.java +++ b/src/main/java/org/json/XML.java @@ -360,9 +360,9 @@ public class XML { && NULL_ATTR.equals(string) && Boolean.parseBoolean((String) token)) { nilAttributeFound = true; - } else if(config.xsiTypeMap != null + } else if(config.getXsiTypeMap() != null && !config.getXsiTypeMap().isEmpty() && TYPE_ATTR.equals(string)) { - xmlXsiTypeConverter = config.xsiTypeMap.get(token); + xmlXsiTypeConverter = config.getXsiTypeMap().get(token); } else if (!nilAttributeFound) { jsonObject.accumulate(string, config.isKeepStrings() diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java index e958cf3..625d463 100644 --- a/src/main/java/org/json/XMLParserConfiguration.java +++ b/src/main/java/org/json/XMLParserConfiguration.java @@ -23,6 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +import java.util.Collections; import java.util.Map; @@ -62,7 +63,7 @@ public class XMLParserConfiguration { /** * This will allow type conversion for values in XML if xsi:type attribute is defined */ - public Map> xsiTypeMap; + private Map> xsiTypeMap; /** * Default parser configuration. Does not keep strings (tries to implicitly convert @@ -72,6 +73,7 @@ public class XMLParserConfiguration { this.keepStrings = false; this.cDataTagName = "content"; this.convertNilAttributeToNull = false; + this.xsiTypeMap = Collections.emptyMap(); } /** @@ -148,16 +150,15 @@ public class XMLParserConfiguration { * false to parse values with attribute xsi:nil="true" as {"xsi:nil":true}. * @param xsiTypeMap new HashMap>() to parse values with attribute * xsi:type="integer" as integer, xsi:type="string" as string - * null to use default behaviour. */ - public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, + private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, final boolean convertNilAttributeToNull, final Map> xsiTypeMap ) { this.keepStrings = keepStrings; this.cDataTagName = cDataTagName; this.convertNilAttributeToNull = convertNilAttributeToNull; - this.xsiTypeMap = xsiTypeMap; + this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap); } - + /** * Provides a new instance of the same configuration. */ @@ -171,7 +172,8 @@ public class XMLParserConfiguration { return new XMLParserConfiguration( this.keepStrings, this.cDataTagName, - this.convertNilAttributeToNull + this.convertNilAttributeToNull, + this.xsiTypeMap ); } @@ -253,4 +255,30 @@ public class XMLParserConfiguration { newConfig.convertNilAttributeToNull = newVal; return newConfig; } + + /** + * When parsing the XML into JSON, specifies that the values with attribute xsi:type + * will be converted to target type defined to client in this configuration + * Map> to parse values with attribute + * xsi:type="integer" as integer, xsi:type="string" as string + * @return {@link #xsiTypeMap} unmodifiable configuration map. + */ + public Map> getXsiTypeMap() { + return this.xsiTypeMap; + } + + /** + * When parsing the XML into JSON, specifies that the values with attribute xsi:type + * will be converted to target type defined to client in this configuration + * Map> to parse values with attribute + * xsi:type="integer" as integer, xsi:type="string" as string + * @param xsiTypeMap new HashMap>() to parse values with attribute + * xsi:type="integer" as integer, xsi:type="string" as string + * @return The existing configuration will not be modified. A new configuration is returned. + */ + public XMLParserConfiguration withXsiTypeMap(final Map> xsiTypeMap) { + XMLParserConfiguration newConfig = this.clone(); + newConfig.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap); + return newConfig; + } } diff --git a/src/test/java/org/json/junit/XMLTest.java b/src/test/java/org/json/junit/XMLTest.java index a244856..1cfd6b5 100644 --- a/src/test/java/org/json/junit/XMLTest.java +++ b/src/test/java/org/json/junit/XMLTest.java @@ -1008,8 +1008,7 @@ public class XMLTest { return Integer.valueOf(value); } }); - JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration(false, - "content", false, xsiTypeMap)); + JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withXsiTypeMap(xsiTypeMap)); Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson); } @@ -1030,8 +1029,7 @@ public class XMLTest { return Integer.valueOf(value); } }); - JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration(false, - "content", false, xsiTypeMap)); + JSONObject actualJson = XML.toJSONObject(originalXml, new XMLParserConfiguration().withXsiTypeMap(xsiTypeMap)); Util.compareActualVsExpectedJsonObjects(actualJson,expectedJson); }