mirror of
https://github.com/stleary/JSON-java.git
synced 2026-01-24 00:03:17 -05:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
07a0f60b6e | ||
|
|
60349ece54 | ||
|
|
ba2585fe6c | ||
|
|
93c79ca566 | ||
|
|
62486fdea4 | ||
|
|
97f1b2744f | ||
|
|
c2b3f2bdb1 | ||
|
|
3007fc8ebe | ||
|
|
39b1c0cb66 | ||
|
|
07b2d65e30 | ||
|
|
5b67330b71 | ||
|
|
9950350f12 | ||
|
|
2b2fac3eb1 | ||
|
|
e2a0bb16a2 | ||
|
|
a971736f5b | ||
|
|
757fd566ab | ||
|
|
e6f5047742 | ||
|
|
8688494876 | ||
|
|
48302592cf | ||
|
|
03dd662e72 | ||
|
|
23cf659730 | ||
|
|
ec8f649467 | ||
|
|
39e3ccc671 | ||
|
|
2ec538f420 | ||
|
|
cadba9400c | ||
|
|
105426b53f | ||
|
|
7886c96204 | ||
|
|
91c6f09be8 |
2
CDL.java
Executable file → Normal file
2
CDL.java
Executable file → Normal file
@@ -41,7 +41,7 @@ SOFTWARE.
|
||||
* The names for the elements in the JSONObjects can be taken from the names
|
||||
* in the first row.
|
||||
* @author JSON.org
|
||||
* @version 2015-05-01
|
||||
* @version 2015-12-09
|
||||
*/
|
||||
public class CDL {
|
||||
|
||||
|
||||
2
Cookie.java
Executable file → Normal file
2
Cookie.java
Executable file → Normal file
@@ -28,7 +28,7 @@ SOFTWARE.
|
||||
* Convert a web browser cookie specification to a JSONObject and back.
|
||||
* JSON and Cookies are both notations for name/value pairs.
|
||||
* @author JSON.org
|
||||
* @version 2014-05-03
|
||||
* @version 2015-12-09
|
||||
*/
|
||||
public class Cookie {
|
||||
|
||||
|
||||
2
CookieList.java
Executable file → Normal file
2
CookieList.java
Executable file → Normal file
@@ -29,7 +29,7 @@ import java.util.Iterator;
|
||||
/**
|
||||
* Convert a web browser cookie list string to a JSONObject and back.
|
||||
* @author JSON.org
|
||||
* @version 2014-05-03
|
||||
* @version 2015-12-09
|
||||
*/
|
||||
public class CookieList {
|
||||
|
||||
|
||||
2
HTTP.java
Executable file → Normal file
2
HTTP.java
Executable file → Normal file
@@ -29,7 +29,7 @@ import java.util.Iterator;
|
||||
/**
|
||||
* Convert an HTTP header to a JSONObject and back.
|
||||
* @author JSON.org
|
||||
* @version 2014-05-03
|
||||
* @version 2015-12-09
|
||||
*/
|
||||
public class HTTP {
|
||||
|
||||
|
||||
2
HTTPTokener.java
Executable file → Normal file
2
HTTPTokener.java
Executable file → Normal file
@@ -28,7 +28,7 @@ SOFTWARE.
|
||||
* The HTTPTokener extends the JSONTokener to provide additional methods
|
||||
* for the parsing of HTTP headers.
|
||||
* @author JSON.org
|
||||
* @version 2014-05-03
|
||||
* @version 2015-12-09
|
||||
*/
|
||||
public class HTTPTokener extends JSONTokener {
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ import java.util.Map;
|
||||
* </ul>
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2015-10-29
|
||||
* @version 2016-02-08
|
||||
*/
|
||||
public class JSONArray implements Iterable<Object> {
|
||||
|
||||
@@ -593,7 +593,9 @@ public class JSONArray implements Iterable<Object> {
|
||||
return myE;
|
||||
}
|
||||
return Enum.valueOf(clazz, val.toString());
|
||||
} catch (IllegalArgumentException | NullPointerException e) {
|
||||
} catch (IllegalArgumentException e) {
|
||||
return defaultValue;
|
||||
} catch (NullPointerException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
@@ -1083,6 +1085,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
* <p>
|
||||
* Warning: This method assumes that the data structure is acyclical.
|
||||
*
|
||||
* @param writer
|
||||
* Writes the serialized JSON
|
||||
* @param indentFactor
|
||||
* The number of spaces to add to each level of indentation.
|
||||
* @param indent
|
||||
@@ -1090,7 +1094,7 @@ public class JSONArray implements Iterable<Object> {
|
||||
* @return The writer.
|
||||
* @throws JSONException
|
||||
*/
|
||||
Writer write(Writer writer, int indentFactor, int indent)
|
||||
public Writer write(Writer writer, int indentFactor, int indent)
|
||||
throws JSONException {
|
||||
try {
|
||||
boolean commanate = false;
|
||||
|
||||
2
JSONException.java
Executable file → Normal file
2
JSONException.java
Executable file → Normal file
@@ -4,7 +4,7 @@ package org.json;
|
||||
* The JSONException is thrown by the JSON.org classes when things are amiss.
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2015-10-18
|
||||
* @version 2015-12-09
|
||||
*/
|
||||
public class JSONException extends RuntimeException {
|
||||
/** Serialization ID */
|
||||
|
||||
6
JSONML.java
Executable file → Normal file
6
JSONML.java
Executable file → Normal file
@@ -33,7 +33,7 @@ import java.util.Iterator;
|
||||
* the JsonML transform.
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2014-05-03
|
||||
* @version 2016-01-30
|
||||
*/
|
||||
public class JSONML {
|
||||
|
||||
@@ -174,7 +174,7 @@ public class JSONML {
|
||||
if (!(token instanceof String)) {
|
||||
throw x.syntaxError("Missing value");
|
||||
}
|
||||
newjo.accumulate(attribute, XML.stringToValue((String)token));
|
||||
newjo.accumulate(attribute, JSONObject.stringToValue((String)token));
|
||||
token = null;
|
||||
} else {
|
||||
newjo.accumulate(attribute, "");
|
||||
@@ -227,7 +227,7 @@ public class JSONML {
|
||||
} else {
|
||||
if (ja != null) {
|
||||
ja.put(token instanceof String
|
||||
? XML.stringToValue((String)token)
|
||||
? JSONObject.stringToValue((String)token)
|
||||
: token);
|
||||
}
|
||||
}
|
||||
|
||||
39
JSONObject.java
Executable file → Normal file
39
JSONObject.java
Executable file → Normal file
@@ -30,7 +30,8 @@ import java.io.Writer;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.math.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
@@ -92,7 +93,7 @@ import java.util.Set;
|
||||
* </ul>
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2015-10-29
|
||||
* @version 2016-02-08
|
||||
*/
|
||||
public class JSONObject {
|
||||
/**
|
||||
@@ -165,10 +166,6 @@ public class JSONObject {
|
||||
* A JSONObject.
|
||||
* @param names
|
||||
* An array of strings.
|
||||
* @throws JSONException
|
||||
* @exception JSONException
|
||||
* If a value is a non-finite number or if a name is
|
||||
* duplicated.
|
||||
*/
|
||||
public JSONObject(JSONObject jo, String[] names) {
|
||||
this();
|
||||
@@ -241,7 +238,6 @@ public class JSONObject {
|
||||
* @param map
|
||||
* A map object that can be used to initialize the contents of
|
||||
* the JSONObject.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public JSONObject(Map<?, ?> map) {
|
||||
this.map = new HashMap<String, Object>();
|
||||
@@ -905,7 +901,9 @@ public class JSONObject {
|
||||
return myE;
|
||||
}
|
||||
return Enum.valueOf(clazz, val.toString());
|
||||
} catch (IllegalArgumentException | NullPointerException e) {
|
||||
} catch (IllegalArgumentException e) {
|
||||
return defaultValue;
|
||||
} catch (NullPointerException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
@@ -1482,7 +1480,6 @@ public class JSONObject {
|
||||
* @return A simple JSON value.
|
||||
*/
|
||||
public static Object stringToValue(String string) {
|
||||
Double d;
|
||||
if (string.equals("")) {
|
||||
return string;
|
||||
}
|
||||
@@ -1501,23 +1498,23 @@ public class JSONObject {
|
||||
* produced, then the value will just be a string.
|
||||
*/
|
||||
|
||||
char b = string.charAt(0);
|
||||
if ((b >= '0' && b <= '9') || b == '-') {
|
||||
char initial = string.charAt(0);
|
||||
if ((initial >= '0' && initial <= '9') || initial == '-') {
|
||||
try {
|
||||
if (string.indexOf('.') > -1 || string.indexOf('e') > -1
|
||||
|| string.indexOf('E') > -1) {
|
||||
d = Double.valueOf(string);
|
||||
|| string.indexOf('E') > -1
|
||||
|| "-0".equals(string)) {
|
||||
Double d = Double.valueOf(string);
|
||||
if (!d.isInfinite() && !d.isNaN()) {
|
||||
return d;
|
||||
}
|
||||
} else {
|
||||
Long myLong = new Long(string);
|
||||
if (string.equals(myLong.toString())) {
|
||||
if (myLong == myLong.intValue()) {
|
||||
return myLong.intValue();
|
||||
} else {
|
||||
return myLong;
|
||||
if (myLong.longValue() == myLong.intValue()) {
|
||||
return Integer.valueOf(myLong.intValue());
|
||||
}
|
||||
return myLong;
|
||||
}
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
@@ -1786,10 +1783,16 @@ public class JSONObject {
|
||||
* <p>
|
||||
* Warning: This method assumes that the data structure is acyclical.
|
||||
*
|
||||
* @param writer
|
||||
* Writes the serialized JSON
|
||||
* @param indentFactor
|
||||
* The number of spaces to add to each level of indentation.
|
||||
* @param indent
|
||||
* The indention of the top level.
|
||||
* @return The writer.
|
||||
* @throws JSONException
|
||||
*/
|
||||
Writer write(Writer writer, int indentFactor, int indent)
|
||||
public Writer write(Writer writer, int indentFactor, int indent)
|
||||
throws JSONException {
|
||||
try {
|
||||
boolean commanate = false;
|
||||
|
||||
0
JSONString.java
Executable file → Normal file
0
JSONString.java
Executable file → Normal file
2
JSONStringer.java
Executable file → Normal file
2
JSONStringer.java
Executable file → Normal file
@@ -54,7 +54,7 @@ import java.io.StringWriter;
|
||||
* <p>
|
||||
* This can sometimes be easier than using a JSONObject to build a string.
|
||||
* @author JSON.org
|
||||
* @version 2008-09-18
|
||||
* @version 2015-12-09
|
||||
*/
|
||||
public class JSONStringer extends JSONWriter {
|
||||
/**
|
||||
|
||||
2
JSONWriter.java
Executable file → Normal file
2
JSONWriter.java
Executable file → Normal file
@@ -54,7 +54,7 @@ SOFTWARE.
|
||||
* <p>
|
||||
* This can sometimes be easier than using a JSONObject to build a string.
|
||||
* @author JSON.org
|
||||
* @version 2011-11-24
|
||||
* @version 2015-12-09
|
||||
*/
|
||||
public class JSONWriter {
|
||||
private static final int maxdepth = 200;
|
||||
|
||||
18
README
Executable file → Normal file
18
README
Executable file → Normal file
@@ -61,14 +61,26 @@ JSONML.java: JSONML provides support for converting between JSONML and XML.
|
||||
|
||||
XMLTokener.java: XMLTokener extends JSONTokener for parsing XML text.
|
||||
|
||||
Unit tests are maintained in a separate project. Contributing developers can test JSON-java pull requests with the code in this project: https://github.com/stleary/JSON-Java-unit-test
|
||||
Unit tests are maintained in a separate project. Contributing developers can test
|
||||
JSON-java pull requests with the code in this project:
|
||||
https://github.com/stleary/JSON-Java-unit-test
|
||||
|
||||
Numeric types in this package comply with ECMA-404: The JSON Data Interchange Format
|
||||
(http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf) and
|
||||
RFC 7159: The JavaScript Object Notation (JSON) Data Interchange Format
|
||||
(https://tools.ietf.org/html/rfc7159#section-6).
|
||||
This package fully supports Integer, Long, and Double Java types. Partial support
|
||||
for BigInteger and BigDecimal values in JSONObject and JSONArray objects is provided
|
||||
in the form of get(), opt(), and put() API methods.
|
||||
|
||||
Release history:
|
||||
|
||||
20151123 JSONObject and JSONArray initialization with generics. Contains the
|
||||
latest code as of 23 Nov, 2015.
|
||||
|
||||
20150729 Checkpoint for Maven central repository release. Contains the latest code as of 29 July, 2015.
|
||||
20150729 Checkpoint for Maven central repository release. Contains the latest code
|
||||
as of 29 July, 2015.
|
||||
|
||||
JSON-java releases can be found by searching the Maven repository for groupId "org.json" and artifactId "json". For example:
|
||||
JSON-java releases can be found by searching the Maven repository for groupId "org.json"
|
||||
and artifactId "json". For example:
|
||||
https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.json%22%20AND%20a%3A%22json%22
|
||||
|
||||
182
XML.java
Executable file → Normal file
182
XML.java
Executable file → Normal file
@@ -27,11 +27,13 @@ SOFTWARE.
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* This provides static methods to convert an XML text into a JSONObject,
|
||||
* and to covert a JSONObject into an XML text.
|
||||
* This provides static methods to convert an XML text into a JSONObject, and to
|
||||
* covert a JSONObject into an XML text.
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2015-10-18
|
||||
* @version 2016-01-30
|
||||
*/
|
||||
@SuppressWarnings("boxing")
|
||||
public class XML {
|
||||
|
||||
/** The Character '&'. */
|
||||
@@ -63,13 +65,16 @@ public class XML {
|
||||
|
||||
/**
|
||||
* Replace special characters with XML escapes:
|
||||
*
|
||||
* <pre>
|
||||
* & <small>(ampersand)</small> is replaced by &amp;
|
||||
* < <small>(less than)</small> is replaced by &lt;
|
||||
* > <small>(greater than)</small> is replaced by &gt;
|
||||
* " <small>(double quote)</small> is replaced by &quot;
|
||||
* </pre>
|
||||
* @param string The string to be escaped.
|
||||
*
|
||||
* @param string
|
||||
* The string to be escaped.
|
||||
* @return The escaped string.
|
||||
*/
|
||||
public static String escape(String string) {
|
||||
@@ -100,9 +105,11 @@ public class XML {
|
||||
}
|
||||
|
||||
/**
|
||||
* Throw an exception if the string contains whitespace.
|
||||
* Whitespace is not allowed in tagNames and attributes.
|
||||
* @param string A string.
|
||||
* Throw an exception if the string contains whitespace. Whitespace is not
|
||||
* allowed in tagNames and attributes.
|
||||
*
|
||||
* @param string
|
||||
* A string.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public static void noSpace(String string) throws JSONException {
|
||||
@@ -112,22 +119,26 @@ public class XML {
|
||||
}
|
||||
for (i = 0; i < length; i += 1) {
|
||||
if (Character.isWhitespace(string.charAt(i))) {
|
||||
throw new JSONException("'" + string +
|
||||
"' contains a space character.");
|
||||
throw new JSONException("'" + string
|
||||
+ "' contains a space character.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Scan the content following the named tag, attaching it to the context.
|
||||
* @param x The XMLTokener containing the source string.
|
||||
* @param context The JSONObject that will include the new material.
|
||||
* @param name The tag name.
|
||||
*
|
||||
* @param x
|
||||
* The XMLTokener containing the source string.
|
||||
* @param context
|
||||
* The JSONObject that will include the new material.
|
||||
* @param name
|
||||
* The tag name.
|
||||
* @return true if the close tag is processed.
|
||||
* @throws JSONException
|
||||
*/
|
||||
private static boolean parse(XMLTokener x, JSONObject context,
|
||||
String name) throws JSONException {
|
||||
private static boolean parse(XMLTokener x, JSONObject context, String name)
|
||||
throws JSONException {
|
||||
char c;
|
||||
int i;
|
||||
JSONObject jsonobject = null;
|
||||
@@ -185,7 +196,6 @@ public class XML {
|
||||
} else if (token == QUEST) {
|
||||
|
||||
// <?
|
||||
|
||||
x.skipPast("?>");
|
||||
return false;
|
||||
} else if (token == SLASH) {
|
||||
@@ -219,7 +229,6 @@ public class XML {
|
||||
}
|
||||
|
||||
// attribute = value
|
||||
|
||||
if (token instanceof String) {
|
||||
string = (String) token;
|
||||
token = x.nextToken();
|
||||
@@ -229,15 +238,15 @@ public class XML {
|
||||
throw x.syntaxError("Missing value");
|
||||
}
|
||||
jsonobject.accumulate(string,
|
||||
XML.stringToValue((String)token));
|
||||
JSONObject.stringToValue((String) token));
|
||||
token = null;
|
||||
} else {
|
||||
jsonobject.accumulate(string, "");
|
||||
}
|
||||
|
||||
// Empty tag <.../>
|
||||
|
||||
} else if (token == SLASH) {
|
||||
// Empty tag <.../>
|
||||
if (x.nextToken() != GT) {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
}
|
||||
@@ -248,9 +257,8 @@ public class XML {
|
||||
}
|
||||
return false;
|
||||
|
||||
// Content, between <...> and </...>
|
||||
|
||||
} else if (token == GT) {
|
||||
// Content, between <...> and </...>
|
||||
for (;;) {
|
||||
token = x.nextContent();
|
||||
if (token == null) {
|
||||
@@ -262,17 +270,16 @@ public class XML {
|
||||
string = (String) token;
|
||||
if (string.length() > 0) {
|
||||
jsonobject.accumulate("content",
|
||||
XML.stringToValue(string));
|
||||
JSONObject.stringToValue(string));
|
||||
}
|
||||
|
||||
// Nested element
|
||||
|
||||
} else if (token == LT) {
|
||||
// Nested element
|
||||
if (parse(x, jsonobject, tagName)) {
|
||||
if (jsonobject.length() == 0) {
|
||||
context.accumulate(tagName, "");
|
||||
} else if (jsonobject.length() == 1 &&
|
||||
jsonobject.opt("content") != null) {
|
||||
} else if (jsonobject.length() == 1
|
||||
&& jsonobject.opt("content") != null) {
|
||||
context.accumulate(tagName,
|
||||
jsonobject.opt("content"));
|
||||
} else {
|
||||
@@ -289,62 +296,31 @@ public class XML {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Try to convert a string into a number, boolean, or null. If the string
|
||||
* can't be converted, return the string. This is much less ambitious than
|
||||
* JSONObject.stringToValue, especially because it does not attempt to
|
||||
* convert plus forms, octal forms, hex forms, or E forms lacking decimal
|
||||
* points.
|
||||
* @param string A String.
|
||||
* @return A simple JSON value.
|
||||
* This method has been deprecated in favor of the
|
||||
* {@link JSONObject.stringToValue(String)} method. Use it instead.
|
||||
*
|
||||
* @deprecated Use {@link JSONObject#stringToValue(String)} instead.
|
||||
* @param string
|
||||
* @return JSON value of this string or the string
|
||||
*/
|
||||
public static Object stringToValue(String string) {
|
||||
if ("true".equalsIgnoreCase(string)) {
|
||||
return Boolean.TRUE;
|
||||
return JSONObject.stringToValue(string);
|
||||
}
|
||||
if ("false".equalsIgnoreCase(string)) {
|
||||
return Boolean.FALSE;
|
||||
}
|
||||
if ("null".equalsIgnoreCase(string)) {
|
||||
return JSONObject.NULL;
|
||||
}
|
||||
|
||||
// If it might be a number, try converting it, first as a Long, and then as a
|
||||
// Double. If that doesn't work, return the string.
|
||||
|
||||
try {
|
||||
char initial = string.charAt(0);
|
||||
if (initial == '-' || (initial >= '0' && initial <= '9')) {
|
||||
Long value = new Long(string);
|
||||
if (value.toString().equals(string)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
try {
|
||||
Double value = new Double(string);
|
||||
if (value.toString().equals(string)) {
|
||||
return value;
|
||||
}
|
||||
} catch (Exception ignoreAlso) {
|
||||
}
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert a well-formed (but not necessarily valid) XML string into a
|
||||
* JSONObject. Some information may be lost in this transformation
|
||||
* because JSON is a data format and XML is a document format. XML uses
|
||||
* elements, attributes, and content text, while JSON uses unordered
|
||||
* collections of name/value pairs and arrays of values. JSON does not
|
||||
* does not like to distinguish between elements and attributes.
|
||||
* Sequences of similar elements are represented as JSONArrays. Content
|
||||
* text may be placed in a "content" member. Comments, prologs, DTDs, and
|
||||
* <code><[ [ ]]></code> are ignored.
|
||||
* @param string The source string.
|
||||
* JSONObject. Some information may be lost in this transformation because
|
||||
* JSON is a data format and XML is a document format. XML uses elements,
|
||||
* attributes, and content text, while JSON uses unordered collections of
|
||||
* name/value pairs and arrays of values. JSON does not does not like to
|
||||
* distinguish between elements and attributes. Sequences of similar
|
||||
* elements are represented as JSONArrays. Content text may be placed in a
|
||||
* "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
|
||||
* are ignored.
|
||||
*
|
||||
* @param string
|
||||
* The source string.
|
||||
* @return A JSONObject containing the structured data from the XML string.
|
||||
* @throws JSONException
|
||||
*/
|
||||
@@ -357,10 +333,11 @@ public class XML {
|
||||
return jo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert a JSONObject into a well-formed, element-normal XML string.
|
||||
* @param object A JSONObject.
|
||||
*
|
||||
* @param object
|
||||
* A JSONObject.
|
||||
* @return A string.
|
||||
* @throws JSONException
|
||||
*/
|
||||
@@ -368,29 +345,29 @@ public class XML {
|
||||
return toString(object, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert a JSONObject into a well-formed, element-normal XML string.
|
||||
* @param object A JSONObject.
|
||||
* @param tagName The optional name of the enclosing tag.
|
||||
*
|
||||
* @param object
|
||||
* A JSONObject.
|
||||
* @param tagName
|
||||
* The optional name of the enclosing tag.
|
||||
* @return A string.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public static String toString(Object object, String tagName)
|
||||
throws JSONException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int i;
|
||||
JSONArray ja;
|
||||
JSONObject jo;
|
||||
String key;
|
||||
Iterator<String> keys;
|
||||
int length;
|
||||
String string;
|
||||
Object value;
|
||||
|
||||
if (object instanceof JSONObject) {
|
||||
|
||||
// Emit <tagName>
|
||||
|
||||
if (tagName != null) {
|
||||
sb.append('<');
|
||||
sb.append(tagName);
|
||||
@@ -398,7 +375,6 @@ public class XML {
|
||||
}
|
||||
|
||||
// Loop thru the keys.
|
||||
|
||||
jo = (JSONObject) object;
|
||||
keys = jo.keys();
|
||||
while (keys.hasNext()) {
|
||||
@@ -406,20 +382,22 @@ public class XML {
|
||||
value = jo.opt(key);
|
||||
if (value == null) {
|
||||
value = "";
|
||||
} else if (value.getClass().isArray()) {
|
||||
value = new JSONArray(value);
|
||||
}
|
||||
string = value instanceof String ? (String) value : null;
|
||||
|
||||
// Emit content in body
|
||||
|
||||
if ("content".equals(key)) {
|
||||
if (value instanceof JSONArray) {
|
||||
ja = (JSONArray) value;
|
||||
length = ja.length();
|
||||
for (i = 0; i < length; i += 1) {
|
||||
int i = 0;
|
||||
for (Object val : ja) {
|
||||
if (i > 0) {
|
||||
sb.append('\n');
|
||||
}
|
||||
sb.append(escape(ja.get(i).toString()));
|
||||
sb.append(escape(val.toString()));
|
||||
i++;
|
||||
}
|
||||
} else {
|
||||
sb.append(escape(value.toString()));
|
||||
@@ -429,19 +407,17 @@ public class XML {
|
||||
|
||||
} else if (value instanceof JSONArray) {
|
||||
ja = (JSONArray) value;
|
||||
length = ja.length();
|
||||
for (i = 0; i < length; i += 1) {
|
||||
value = ja.get(i);
|
||||
if (value instanceof JSONArray) {
|
||||
for (Object val : ja) {
|
||||
if (val instanceof JSONArray) {
|
||||
sb.append('<');
|
||||
sb.append(key);
|
||||
sb.append('>');
|
||||
sb.append(toString(value));
|
||||
sb.append(toString(val));
|
||||
sb.append("</");
|
||||
sb.append(key);
|
||||
sb.append('>');
|
||||
} else {
|
||||
sb.append(toString(value, key));
|
||||
sb.append(toString(val, key));
|
||||
}
|
||||
}
|
||||
} else if ("".equals(value)) {
|
||||
@@ -458,17 +434,14 @@ public class XML {
|
||||
if (tagName != null) {
|
||||
|
||||
// Emit the </tagname> close tag
|
||||
|
||||
sb.append("</");
|
||||
sb.append(tagName);
|
||||
sb.append('>');
|
||||
}
|
||||
return sb.toString();
|
||||
|
||||
// XML does not have good support for arrays. If an array appears in a place
|
||||
// where XML is lacking, synthesize an <array> element.
|
||||
|
||||
}
|
||||
|
||||
if (object != null) {
|
||||
if (object.getClass().isArray()) {
|
||||
object = new JSONArray(object);
|
||||
@@ -476,17 +449,20 @@ public class XML {
|
||||
|
||||
if (object instanceof JSONArray) {
|
||||
ja = (JSONArray) object;
|
||||
length = ja.length();
|
||||
for (i = 0; i < length; i += 1) {
|
||||
sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName));
|
||||
for (Object val : ja) {
|
||||
// XML does not have good support for arrays. If an array
|
||||
// appears in a place where XML is lacking, synthesize an
|
||||
// <array> element.
|
||||
sb.append(toString(val, tagName == null ? "array" : tagName));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
string = (object == null) ? "null" : escape(object.toString());
|
||||
return (tagName == null) ? "\"" + string + "\"" :
|
||||
(string.length() == 0) ? "<" + tagName + "/>" :
|
||||
"<" + tagName + ">" + string + "</" + tagName + ">";
|
||||
return (tagName == null) ? "\"" + string + "\""
|
||||
: (string.length() == 0) ? "<" + tagName + "/>" : "<" + tagName
|
||||
+ ">" + string + "</" + tagName + ">";
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
2
XMLTokener.java
Executable file → Normal file
2
XMLTokener.java
Executable file → Normal file
@@ -28,7 +28,7 @@ SOFTWARE.
|
||||
* The XMLTokener extends the JSONTokener to provide additional methods
|
||||
* for the parsing of XML texts.
|
||||
* @author JSON.org
|
||||
* @version 2014-05-03
|
||||
* @version 2015-12-09
|
||||
*/
|
||||
public class XMLTokener extends JSONTokener {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user