-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added type conversion support #540
Changes from 3 commits
250f74e
61c1a88
0a8091c
900a8cc
310f18f
ed9658d
56d4130
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,9 @@ of this software and associated documentation files (the "Software"), to deal | |
SOFTWARE. | ||
*/ | ||
|
||
import java.util.Map; | ||
|
||
|
||
/** | ||
* Configuration object for the XML parser. The configuration is immutable. | ||
* @author AylwardJ | ||
|
@@ -56,6 +59,11 @@ public class XMLParserConfiguration { | |
*/ | ||
private boolean convertNilAttributeToNull; | ||
|
||
/** | ||
* This will allow type conversion for values in XML if xsi:type attribute is defined | ||
*/ | ||
public Map<String, XMLXsiTypeConverter<?>> xsiTypeMap; | ||
|
||
/** | ||
* Default parser configuration. Does not keep strings (tries to implicitly convert | ||
* values), and the CDATA Tag Name is "content". | ||
kumar529 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -106,9 +114,7 @@ public XMLParserConfiguration (final String cDataTagName) { | |
*/ | ||
@Deprecated | ||
public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName) { | ||
this.keepStrings = keepStrings; | ||
this.cDataTagName = cDataTagName; | ||
this.convertNilAttributeToNull = false; | ||
this(keepStrings, cDataTagName, false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do not modify deprecated functions, or use them in your tests. |
||
} | ||
|
||
/** | ||
|
@@ -125,9 +131,27 @@ public XMLParserConfiguration (final boolean keepStrings, final String cDataTagN | |
*/ | ||
@Deprecated | ||
public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, final boolean convertNilAttributeToNull) { | ||
this(keepStrings, cDataTagName, convertNilAttributeToNull, null); | ||
} | ||
|
||
/** | ||
* Configure the parser to use custom settings. | ||
* @param keepStrings <code>true</code> to parse all values as string. | ||
* <code>false</code> to try and convert XML string values into a JSON value. | ||
* @param cDataTagName <code>null</code> to disable CDATA processing. Any other value | ||
* to use that value as the JSONObject key name to process as CDATA. | ||
* @param convertNilAttributeToNull <code>true</code> to parse values with attribute xsi:nil="true" as null. | ||
* <code>false</code> to parse values with attribute xsi:nil="true" as {"xsi:nil":true}. | ||
* @param xsiTypeMap <code>new HashMap<String, XMLXsiTypeConverter<?>>()</code> to parse values with attribute | ||
* xsi:type="integer" as integer, xsi:type="string" as string | ||
* <code>null</code> to use default behaviour. | ||
*/ | ||
public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be marked private instead of public. this.xsiTypeMap = xsiTypeMap to: this.xsiTypeMap = Collections.unmodifiableMap(new HashMap<String, XMLXsiTypeConverter<?>>(xsiTypeMap)); Our expected configuration should look something like this: XMLParserConfiguration xmlConfig = new XMLParserConfiguration().withKeepStrings(false).WithXsiTypeMap(myXsiMap);
// use the config After marking this private, also update the "clone" method (currently line 165). Be sure to follow the comment that is included in the "clone" method. If changing the constructor above to do the shallow clone/unmodifiable map wrapping, you can probably just update the clone to call this private constructor. Lastly, be sure to create the new "with" method that will take the XSI:Type conversion map. |
||
final boolean convertNilAttributeToNull, final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap ) { | ||
this.keepStrings = keepStrings; | ||
this.cDataTagName = cDataTagName; | ||
this.convertNilAttributeToNull = convertNilAttributeToNull; | ||
this.xsiTypeMap = xsiTypeMap; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.json; | ||
kumar529 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/* | ||
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. | ||
* <pre> | ||
* <h1>XML Sample</h1> | ||
* {@code | ||
* <root> | ||
* <asString xsi:type="string">12345</asString> | ||
* <asInt xsi:type="integer">54321</asInt> | ||
* </root> | ||
* } | ||
* <h1>JSON Output</h1> | ||
* {@code | ||
* { | ||
* "root" : { | ||
* "asString" : "12345", | ||
* "asInt": 54321 | ||
* } | ||
* } | ||
* } | ||
* | ||
* <h1>Usage</h1> | ||
* {@code | ||
* Map<String, XMLXsiTypeConverter<?>> xsiTypeMap = new HashMap<String, XMLXsiTypeConverter<?>>(); | ||
* xsiTypeMap.put("string", new XMLXsiTypeConverter<String>() { | ||
* @Override public String convert(final String value) { | ||
* return value; | ||
* } | ||
* }); | ||
* xsiTypeMap.put("integer", new XMLXsiTypeConverter<Integer>() { | ||
* @Override public Integer convert(final String value) { | ||
* return Integer.valueOf(value); | ||
* } | ||
* }); | ||
* } | ||
* </pre> | ||
* @author kumar529 | ||
* @param <T> | ||
*/ | ||
public interface XMLXsiTypeConverter<T> { | ||
T convert(String value); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I finally had time for a closer look at the configuration changes. With the new changes in #543, this should be private with a Getter and no Setter. Preferably the getter should return an "Unmodifiable Map". To prevent wrapping the collection every time the getter is called, storing the unmodifiable map in this variable would be acceptable.
Note in the Getter javadoc that the map is "unmodifiable"