mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
add configuration for xsi:nil="true" conversion to null
This commit is contained in:
parent
12bbe8cd9a
commit
d5b278539e
2 changed files with 72 additions and 34 deletions
21
XML.java
21
XML.java
|
@ -65,6 +65,11 @@ public class XML {
|
|||
/** The Character '/'. */
|
||||
public static final Character SLASH = '/';
|
||||
|
||||
/**
|
||||
* Null attrubute name
|
||||
*/
|
||||
public static final String NULL_ATTR = "xsi:nil";
|
||||
|
||||
/**
|
||||
* Creates an iterator for navigating Code Points in a string instead of
|
||||
* characters. Once Java7 support is dropped, this can be replaced with
|
||||
|
@ -328,6 +333,7 @@ public class XML {
|
|||
tagName = (String) token;
|
||||
token = null;
|
||||
jsonobject = new JSONObject();
|
||||
boolean nilAttributeFound = false;
|
||||
for (;;) {
|
||||
if (token == null) {
|
||||
token = x.nextToken();
|
||||
|
@ -341,8 +347,17 @@ public class XML {
|
|||
if (!(token instanceof String)) {
|
||||
throw x.syntaxError("Missing value");
|
||||
}
|
||||
|
||||
if (config.convertNilAttributeToNull
|
||||
&& NULL_ATTR.equals(string)
|
||||
&& Boolean.parseBoolean((String) token)) {
|
||||
nilAttributeFound = true;
|
||||
} else if (!nilAttributeFound) {
|
||||
jsonobject.accumulate(string,
|
||||
config.keepStrings ? ((String)token) : stringToValue((String) token));
|
||||
config.keepStrings
|
||||
? ((String) token)
|
||||
: stringToValue((String) token));
|
||||
}
|
||||
token = null;
|
||||
} else {
|
||||
jsonobject.accumulate(string, "");
|
||||
|
@ -354,7 +369,9 @@ public class XML {
|
|||
if (x.nextToken() != GT) {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
}
|
||||
if (jsonobject.length() > 0) {
|
||||
if (nilAttributeFound) {
|
||||
context.accumulate(tagName, JSONObject.NULL);
|
||||
} else if (jsonobject.length() > 0) {
|
||||
context.accumulate(tagName, jsonobject);
|
||||
} else {
|
||||
context.accumulate(tagName, "");
|
||||
|
|
|
@ -44,12 +44,17 @@ public class XMLParserConfiguration {
|
|||
* processing.
|
||||
*/
|
||||
public final String cDataTagName;
|
||||
/**
|
||||
* When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
|
||||
* should be kept as attribute(false), or they should be converted to null(true)
|
||||
*/
|
||||
public final boolean convertNilAttributeToNull;
|
||||
|
||||
/**
|
||||
* Default parser configuration. Does not keep strings, and the CDATA Tag Name is "content".
|
||||
*/
|
||||
public XMLParserConfiguration () {
|
||||
this(false, "content");
|
||||
this(false, "content", false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -58,7 +63,7 @@ public class XMLParserConfiguration {
|
|||
* <code>false</code> to try and convert XML string values into a JSON value.
|
||||
*/
|
||||
public XMLParserConfiguration (final boolean keepStrings) {
|
||||
this(keepStrings, "content");
|
||||
this(keepStrings, "content", false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -69,7 +74,7 @@ public class XMLParserConfiguration {
|
|||
* to use that value as the JSONObject key name to process as CDATA.
|
||||
*/
|
||||
public XMLParserConfiguration (final String cDataTagName) {
|
||||
this(false, cDataTagName);
|
||||
this(false, cDataTagName, false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -82,5 +87,21 @@ public class XMLParserConfiguration {
|
|||
public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName) {
|
||||
this.keepStrings = keepStrings;
|
||||
this.cDataTagName = cDataTagName;
|
||||
this.convertNilAttributeToNull = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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}.
|
||||
*/
|
||||
public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName, final boolean convertNilAttributeToNull) {
|
||||
this.keepStrings = keepStrings;
|
||||
this.cDataTagName = cDataTagName;
|
||||
this.convertNilAttributeToNull = convertNilAttributeToNull;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue