Skip to content

Commit

Permalink
Brings in changes from PR stleary#70 to be updated to HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
yaccob authored and John J. Aylward committed Jul 18, 2016
1 parent 04181fb commit 42e0944
Showing 1 changed file with 62 additions and 3 deletions.
65 changes: 62 additions & 3 deletions JSONML.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ private static Object parse(
XMLTokener x,
boolean arrayForm,
JSONArray ja
) throws JSONException {
return parse(x, arrayForm, ja, false);
}

/**
* Parse XML values and store them in a JSONArray.
* @param x The XMLTokener containing the source string.
* @param arrayForm true if array form, false if object form.
* @param ja The JSONArray that is containing the current tag or null
* if we are at the outermost level.
* @param keepStrings Don't type-convert text nodes and attibute values
* @return A JSONArray if the value is the outermost tag, otherwise null.
* @throws JSONException
*/
private static Object parse(
XMLTokener x,
boolean arrayForm,
JSONArray ja,
boolean keepStrings
) throws JSONException {
String attribute;
char c;
Expand Down Expand Up @@ -174,7 +193,7 @@ private static Object parse(
if (!(token instanceof String)) {
throw x.syntaxError("Missing value");
}
newjo.accumulate(attribute, JSONObject.stringToValue((String)token));
newjo.accumulate(attribute, keepStrings ? token :JSONObject.stringToValue((String)token));
token = null;
} else {
newjo.accumulate(attribute, "");
Expand Down Expand Up @@ -204,7 +223,7 @@ private static Object parse(
if (token != XML.GT) {
throw x.syntaxError("Misshaped tag");
}
closeTag = (String)parse(x, arrayForm, newja);
closeTag = (String)parse(x, arrayForm, newja, keepStrings);
if (closeTag != null) {
if (!closeTag.equals(tagName)) {
throw x.syntaxError("Mismatched '" + tagName +
Expand All @@ -227,7 +246,7 @@ private static Object parse(
} else {
if (ja != null) {
ja.put(token instanceof String
? JSONObject.stringToValue((String)token)
? keepStrings ? token :JSONObject.stringToValue((String)token)
: token);
}
}
Expand All @@ -252,6 +271,46 @@ public static JSONArray toJSONArray(String string) throws JSONException {
}


/**
* Convert a well-formed (but not necessarily valid) XML string into a
* JSONArray using the JsonML transform. Each XML tag is represented as
* a JSONArray in which the first element is the tag name. If the tag has
* attributes, then the second element will be JSONObject containing the
* name/value pairs. If the tag contains children, then strings and
* JSONArrays will represent the child tags.
* As opposed to toJSONArray this method does not attempt to convert
* any text node or attribute value to any type
* but just leaves it as a string.
* Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code> are ignored.
* @param string The source string.
* @return A JSONArray containing the structured data from the XML string.
* @throws JSONException
*/
public static JSONArray toJsonML(String string) throws JSONException {
return toJsonML(new XMLTokener(string));
}


/**
* Convert a well-formed (but not necessarily valid) XML string into a
* JSONArray using the JsonML transform. Each XML tag is represented as
* a JSONArray in which the first element is the tag name. If the tag has
* attributes, then the second element will be JSONObject containing the
* name/value pairs. If the tag contains children, then strings and
* JSONArrays will represent the child content and tags.
* As opposed to toJSONArray this method does not attempt to convert
* any text node or attribute value to any type
* but just leaves it as a string.
* Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code> are ignored.
* @param x An XMLTokener.
* @return A JSONArray containing the structured data from the XML string.
* @throws JSONException
*/
public static JSONArray toJsonML(XMLTokener x) throws JSONException {
return (JSONArray)parse(x, true, null, true);
}


/**
* Convert a well-formed (but not necessarily valid) XML string into a
* JSONArray using the JsonML transform. Each XML tag is represented as
Expand Down

0 comments on commit 42e0944

Please sign in to comment.