-
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 2 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,5 @@ | ||
package org.json; | ||
kumar529 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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"