mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
Merge pull request #467 from meiskalt7/addConfigurationForXsiNilConversionToNull
add configuration for xsi:nil="true" conversion to null
This commit is contained in:
commit
00e0e6c0a2
2 changed files with 72 additions and 34 deletions
23
XML.java
23
XML.java
|
@ -65,6 +65,11 @@ public class XML {
|
||||||
/** The Character '/'. */
|
/** The Character '/'. */
|
||||||
public static final Character SLASH = '/';
|
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
|
* Creates an iterator for navigating Code Points in a string instead of
|
||||||
* characters. Once Java7 support is dropped, this can be replaced with
|
* characters. Once Java7 support is dropped, this can be replaced with
|
||||||
|
@ -328,6 +333,7 @@ public class XML {
|
||||||
tagName = (String) token;
|
tagName = (String) token;
|
||||||
token = null;
|
token = null;
|
||||||
jsonobject = new JSONObject();
|
jsonobject = new JSONObject();
|
||||||
|
boolean nilAttributeFound = false;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
token = x.nextToken();
|
token = x.nextToken();
|
||||||
|
@ -341,8 +347,17 @@ public class XML {
|
||||||
if (!(token instanceof String)) {
|
if (!(token instanceof String)) {
|
||||||
throw x.syntaxError("Missing value");
|
throw x.syntaxError("Missing value");
|
||||||
}
|
}
|
||||||
jsonobject.accumulate(string,
|
|
||||||
config.keepStrings ? ((String)token) : stringToValue((String) token));
|
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));
|
||||||
|
}
|
||||||
token = null;
|
token = null;
|
||||||
} else {
|
} else {
|
||||||
jsonobject.accumulate(string, "");
|
jsonobject.accumulate(string, "");
|
||||||
|
@ -354,7 +369,9 @@ public class XML {
|
||||||
if (x.nextToken() != GT) {
|
if (x.nextToken() != GT) {
|
||||||
throw x.syntaxError("Misshaped tag");
|
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);
|
context.accumulate(tagName, jsonobject);
|
||||||
} else {
|
} else {
|
||||||
context.accumulate(tagName, "");
|
context.accumulate(tagName, "");
|
||||||
|
|
|
@ -44,12 +44,17 @@ public class XMLParserConfiguration {
|
||||||
* processing.
|
* processing.
|
||||||
*/
|
*/
|
||||||
public final String cDataTagName;
|
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".
|
* Default parser configuration. Does not keep strings, and the CDATA Tag Name is "content".
|
||||||
*/
|
*/
|
||||||
public XMLParserConfiguration () {
|
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.
|
* <code>false</code> to try and convert XML string values into a JSON value.
|
||||||
*/
|
*/
|
||||||
public XMLParserConfiguration (final boolean keepStrings) {
|
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.
|
* to use that value as the JSONObject key name to process as CDATA.
|
||||||
*/
|
*/
|
||||||
public XMLParserConfiguration (final String cDataTagName) {
|
public XMLParserConfiguration (final String cDataTagName) {
|
||||||
this(false, cDataTagName);
|
this(false, cDataTagName, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -80,7 +85,23 @@ public class XMLParserConfiguration {
|
||||||
* to use that value as the JSONObject key name to process as CDATA.
|
* to use that value as the JSONObject key name to process as CDATA.
|
||||||
*/
|
*/
|
||||||
public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName) {
|
public XMLParserConfiguration (final boolean keepStrings, final String cDataTagName) {
|
||||||
this.keepStrings = keepStrings;
|
this.keepStrings = keepStrings;
|
||||||
this.cDataTagName = cDataTagName;
|
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