mirror of
https://github.com/stleary/JSON-java.git
synced 2026-01-24 00:03:17 -05:00
Compare commits
50 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 | ||
|
|
44275e3b3c | ||
|
|
5f2e77f9dd | ||
|
|
a109f581c8 | ||
|
|
33ae025e78 | ||
|
|
564ad2c2fb | ||
|
|
dfd19116d3 | ||
|
|
105426b53f | ||
|
|
7886c96204 | ||
|
|
91c6f09be8 | ||
|
|
a07d391eae | ||
|
|
e7f4eb5f67 | ||
|
|
b0a9507add | ||
|
|
6757e04c0a | ||
|
|
09b6af4712 | ||
|
|
637c1fe2b9 | ||
|
|
1448163981 | ||
|
|
5ddc515679 | ||
|
|
1b06a802cf | ||
|
|
0e13241528 | ||
|
|
e239e1967a | ||
|
|
ceba8e8c3d | ||
|
|
4e77383472 | ||
|
|
25b5aa7ef2 | ||
|
|
409eb9f292 | ||
|
|
0afd26623c |
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-07-22
|
||||
* @version 2016-02-08
|
||||
*/
|
||||
public class JSONArray implements Iterable<Object> {
|
||||
|
||||
@@ -151,13 +151,12 @@ public class JSONArray implements Iterable<Object> {
|
||||
* @param collection
|
||||
* A Collection.
|
||||
*/
|
||||
public JSONArray(Collection<Object> collection) {
|
||||
public JSONArray(Collection<?> collection) {
|
||||
this.myArrayList = new ArrayList<Object>();
|
||||
if (collection != null) {
|
||||
Iterator<Object> iter = collection.iterator();
|
||||
while (iter.hasNext()) {
|
||||
this.myArrayList.add(JSONObject.wrap(iter.next()));
|
||||
}
|
||||
for (Object o: collection){
|
||||
this.myArrayList.add(JSONObject.wrap(o));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -594,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;
|
||||
}
|
||||
}
|
||||
@@ -746,7 +747,7 @@ public class JSONArray implements Iterable<Object> {
|
||||
* A Collection value.
|
||||
* @return this.
|
||||
*/
|
||||
public JSONArray put(Collection<Object> value) {
|
||||
public JSONArray put(Collection<?> value) {
|
||||
this.put(new JSONArray(value));
|
||||
return this;
|
||||
}
|
||||
@@ -799,7 +800,7 @@ public class JSONArray implements Iterable<Object> {
|
||||
* A Map value.
|
||||
* @return this.
|
||||
*/
|
||||
public JSONArray put(Map<String, Object> value) {
|
||||
public JSONArray put(Map<?, ?> value) {
|
||||
this.put(new JSONObject(value));
|
||||
return this;
|
||||
}
|
||||
@@ -848,7 +849,7 @@ public class JSONArray implements Iterable<Object> {
|
||||
* @throws JSONException
|
||||
* If the index is negative or if the value is not finite.
|
||||
*/
|
||||
public JSONArray put(int index, Collection<Object> value) throws JSONException {
|
||||
public JSONArray put(int index, Collection<?> value) throws JSONException {
|
||||
this.put(index, new JSONArray(value));
|
||||
return this;
|
||||
}
|
||||
@@ -920,7 +921,7 @@ public class JSONArray implements Iterable<Object> {
|
||||
* If the index is negative or if the the value is an invalid
|
||||
* number.
|
||||
*/
|
||||
public JSONArray put(int index, Map<String, Object> value) throws JSONException {
|
||||
public JSONArray put(int index, Map<?, ?> value) throws JSONException {
|
||||
this.put(index, new JSONObject(value));
|
||||
return this;
|
||||
}
|
||||
@@ -1084,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
|
||||
@@ -1091,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;
|
||||
|
||||
34
JSONException.java
Executable file → Normal file
34
JSONException.java
Executable file → Normal file
@@ -4,11 +4,11 @@ package org.json;
|
||||
* The JSONException is thrown by the JSON.org classes when things are amiss.
|
||||
*
|
||||
* @author JSON.org
|
||||
* @version 2014-05-03
|
||||
* @version 2015-12-09
|
||||
*/
|
||||
public class JSONException extends RuntimeException {
|
||||
/** Serialization ID */
|
||||
private static final long serialVersionUID = 0;
|
||||
private Throwable cause;
|
||||
|
||||
/**
|
||||
* Constructs a JSONException with an explanatory message.
|
||||
@@ -16,28 +16,30 @@ public class JSONException extends RuntimeException {
|
||||
* @param message
|
||||
* Detail about the reason for the exception.
|
||||
*/
|
||||
public JSONException(String message) {
|
||||
public JSONException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new JSONException with the specified cause.
|
||||
* @param cause The cause.
|
||||
* Constructs a JSONException with an explanatory message and cause.
|
||||
*
|
||||
* @param message
|
||||
* Detail about the reason for the exception.
|
||||
* @param cause
|
||||
* The cause.
|
||||
*/
|
||||
public JSONException(Throwable cause) {
|
||||
super(cause.getMessage());
|
||||
this.cause = cause;
|
||||
public JSONException(final String message, final Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the cause of this exception or null if the cause is nonexistent
|
||||
* or unknown.
|
||||
*
|
||||
* @return the cause of this exception or null if the cause is nonexistent
|
||||
* or unknown.
|
||||
* Constructs a new JSONException with the specified cause.
|
||||
*
|
||||
* @param cause
|
||||
* The cause.
|
||||
*/
|
||||
@Override
|
||||
public Throwable getCause() {
|
||||
return this.cause;
|
||||
public JSONException(final Throwable cause) {
|
||||
super(cause.getMessage(), cause);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
74
JSONObject.java
Executable file → Normal file
74
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-07-22
|
||||
* @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,17 +238,14 @@ public class JSONObject {
|
||||
* @param map
|
||||
* A map object that can be used to initialize the contents of
|
||||
* the JSONObject.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public JSONObject(Map<String, Object> map) {
|
||||
public JSONObject(Map<?, ?> map) {
|
||||
this.map = new HashMap<String, Object>();
|
||||
if (map != null) {
|
||||
Iterator<Entry<String, Object>> i = map.entrySet().iterator();
|
||||
while (i.hasNext()) {
|
||||
Entry<String, Object> entry = i.next();
|
||||
Object value = entry.getValue();
|
||||
for (final Entry<?, ?> e : map.entrySet()) {
|
||||
final Object value = e.getValue();
|
||||
if (value != null) {
|
||||
this.map.put(entry.getKey(), wrap(value));
|
||||
this.map.put(String.valueOf(e.getKey()), wrap(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -907,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;
|
||||
}
|
||||
}
|
||||
@@ -1204,7 +1200,7 @@ public class JSONObject {
|
||||
* @return this.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public JSONObject put(String key, Collection<Object> value) throws JSONException {
|
||||
public JSONObject put(String key, Collection<?> value) throws JSONException {
|
||||
this.put(key, new JSONArray(value));
|
||||
return this;
|
||||
}
|
||||
@@ -1268,7 +1264,7 @@ public class JSONObject {
|
||||
* @return this.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public JSONObject put(String key, Map<String, Object> value) throws JSONException {
|
||||
public JSONObject put(String key, Map<?, ?> value) throws JSONException {
|
||||
this.put(key, new JSONObject(value));
|
||||
return this;
|
||||
}
|
||||
@@ -1484,7 +1480,6 @@ public class JSONObject {
|
||||
* @return A simple JSON value.
|
||||
*/
|
||||
public static Object stringToValue(String string) {
|
||||
Double d;
|
||||
if (string.equals("")) {
|
||||
return string;
|
||||
}
|
||||
@@ -1503,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) {
|
||||
@@ -1663,13 +1658,11 @@ public class JSONObject {
|
||||
return value.toString();
|
||||
}
|
||||
if (value instanceof Map) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> map = (Map<String, Object>) value;
|
||||
Map<?, ?> map = (Map<?, ?>) value;
|
||||
return new JSONObject(map).toString();
|
||||
}
|
||||
if (value instanceof Collection) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Collection<Object> coll = (Collection<Object>) value;
|
||||
Collection<?> coll = (Collection<?>) value;
|
||||
return new JSONArray(coll).toString();
|
||||
}
|
||||
if (value.getClass().isArray()) {
|
||||
@@ -1707,16 +1700,14 @@ public class JSONObject {
|
||||
}
|
||||
|
||||
if (object instanceof Collection) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Collection<Object> coll = (Collection<Object>) object;
|
||||
Collection<?> coll = (Collection<?>) object;
|
||||
return new JSONArray(coll);
|
||||
}
|
||||
if (object.getClass().isArray()) {
|
||||
return new JSONArray(object);
|
||||
}
|
||||
if (object instanceof Map) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> map = (Map<String, Object>) object;
|
||||
Map<?, ?> map = (Map<?, ?>) object;
|
||||
return new JSONObject(map);
|
||||
}
|
||||
Package objectPackage = object.getClass().getPackage();
|
||||
@@ -1755,14 +1746,11 @@ public class JSONObject {
|
||||
} else if (value instanceof JSONArray) {
|
||||
((JSONArray) value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Map) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> map = (Map<String, Object>) value;
|
||||
Map<?, ?> map = (Map<?, ?>) value;
|
||||
new JSONObject(map).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Collection) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Collection<Object> coll = (Collection<Object>) value;
|
||||
new JSONArray(coll).write(writer, indentFactor,
|
||||
indent);
|
||||
Collection<?> coll = (Collection<?>) value;
|
||||
new JSONArray(coll).write(writer, indentFactor, indent);
|
||||
} else if (value.getClass().isArray()) {
|
||||
new JSONArray(value).write(writer, indentFactor, indent);
|
||||
} else if (value instanceof Number) {
|
||||
@@ -1795,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;
|
||||
|
||||
24
README
Executable file → Normal file
24
README
Executable file → Normal file
@@ -61,4 +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.
|
||||
|
||||
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
|
||||
|
||||
290
XML.java
Executable file → Normal file
290
XML.java
Executable file → Normal file
@@ -1,7 +1,7 @@
|
||||
package org.json;
|
||||
|
||||
/*
|
||||
Copyright (c) 2002 JSON.org
|
||||
Copyright (c) 2015 JSON.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -27,49 +27,54 @@ 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 2014-05-03
|
||||
* @version 2016-01-30
|
||||
*/
|
||||
@SuppressWarnings("boxing")
|
||||
public class XML {
|
||||
|
||||
/** The Character '&'. */
|
||||
public static final Character AMP = '&';
|
||||
public static final Character AMP = '&';
|
||||
|
||||
/** The Character '''. */
|
||||
public static final Character APOS = '\'';
|
||||
public static final Character APOS = '\'';
|
||||
|
||||
/** The Character '!'. */
|
||||
public static final Character BANG = '!';
|
||||
public static final Character BANG = '!';
|
||||
|
||||
/** The Character '='. */
|
||||
public static final Character EQ = '=';
|
||||
public static final Character EQ = '=';
|
||||
|
||||
/** The Character '>'. */
|
||||
public static final Character GT = '>';
|
||||
public static final Character GT = '>';
|
||||
|
||||
/** The Character '<'. */
|
||||
public static final Character LT = '<';
|
||||
public static final Character LT = '<';
|
||||
|
||||
/** The Character '?'. */
|
||||
public static final Character QUEST = '?';
|
||||
|
||||
/** The Character '"'. */
|
||||
public static final Character QUOT = '"';
|
||||
public static final Character QUOT = '"';
|
||||
|
||||
/** The Character '/'. */
|
||||
public static final Character SLASH = '/';
|
||||
|
||||
/**
|
||||
* 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,42 +119,46 @@ 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 {
|
||||
char c;
|
||||
int i;
|
||||
private static boolean parse(XMLTokener x, JSONObject context, String name)
|
||||
throws JSONException {
|
||||
char c;
|
||||
int i;
|
||||
JSONObject jsonobject = null;
|
||||
String string;
|
||||
String tagName;
|
||||
Object token;
|
||||
String string;
|
||||
String tagName;
|
||||
Object token;
|
||||
|
||||
// Test for and skip past these forms:
|
||||
// <!-- ... -->
|
||||
// <! ... >
|
||||
// <![ ... ]]>
|
||||
// <? ... ?>
|
||||
// Report errors for these forms:
|
||||
// <>
|
||||
// <=
|
||||
// <<
|
||||
// Test for and skip past these forms:
|
||||
// <!-- ... -->
|
||||
// <! ... >
|
||||
// <![ ... ]]>
|
||||
// <? ... ?>
|
||||
// Report errors for these forms:
|
||||
// <>
|
||||
// <=
|
||||
// <<
|
||||
|
||||
token = x.nextToken();
|
||||
|
||||
// <!
|
||||
// <!
|
||||
|
||||
if (token == BANG) {
|
||||
c = x.next();
|
||||
@@ -184,13 +195,12 @@ public class XML {
|
||||
return false;
|
||||
} else if (token == QUEST) {
|
||||
|
||||
// <?
|
||||
|
||||
// <?
|
||||
x.skipPast("?>");
|
||||
return false;
|
||||
} else if (token == SLASH) {
|
||||
|
||||
// Close tag </
|
||||
// Close tag </
|
||||
|
||||
token = x.nextToken();
|
||||
if (name == null) {
|
||||
@@ -207,10 +217,10 @@ public class XML {
|
||||
} else if (token instanceof Character) {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
|
||||
// Open tag <
|
||||
// Open tag <
|
||||
|
||||
} else {
|
||||
tagName = (String)token;
|
||||
tagName = (String) token;
|
||||
token = null;
|
||||
jsonobject = new JSONObject();
|
||||
for (;;) {
|
||||
@@ -218,10 +228,9 @@ public class XML {
|
||||
token = x.nextToken();
|
||||
}
|
||||
|
||||
// attribute = value
|
||||
|
||||
// attribute = value
|
||||
if (token instanceof String) {
|
||||
string = (String)token;
|
||||
string = (String) token;
|
||||
token = x.nextToken();
|
||||
if (token == EQ) {
|
||||
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) {
|
||||
@@ -259,20 +267,19 @@ public class XML {
|
||||
}
|
||||
return false;
|
||||
} else if (token instanceof String) {
|
||||
string = (String)token;
|
||||
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 {
|
||||
@@ -288,63 +295,32 @@ 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;
|
||||
}
|
||||
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;
|
||||
return JSONObject.stringToValue(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,91 +333,91 @@ public class XML {
|
||||
return jo;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert a JSONObject into a well-formed, element-normal XML string.
|
||||
* @param object A JSONObject.
|
||||
* @return A string.
|
||||
* @throws JSONException
|
||||
*
|
||||
* @param object
|
||||
* A JSONObject.
|
||||
* @return A string.
|
||||
* @throws JSONException
|
||||
*/
|
||||
public static String toString(Object object) throws JSONException {
|
||||
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;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
JSONArray ja;
|
||||
JSONObject jo;
|
||||
String key;
|
||||
Iterator<String> keys;
|
||||
String string;
|
||||
Object value;
|
||||
|
||||
if (object instanceof JSONObject) {
|
||||
|
||||
// Emit <tagName>
|
||||
|
||||
// Emit <tagName>
|
||||
if (tagName != null) {
|
||||
sb.append('<');
|
||||
sb.append(tagName);
|
||||
sb.append('>');
|
||||
}
|
||||
|
||||
// Loop thru the keys.
|
||||
|
||||
jo = (JSONObject)object;
|
||||
// Loop thru the keys.
|
||||
jo = (JSONObject) object;
|
||||
keys = jo.keys();
|
||||
while (keys.hasNext()) {
|
||||
key = keys.next();
|
||||
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
|
||||
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) {
|
||||
ja = (JSONArray) value;
|
||||
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()));
|
||||
}
|
||||
|
||||
// Emit an array of similar keys
|
||||
// Emit an array of similar keys
|
||||
|
||||
} 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) {
|
||||
ja = (JSONArray) value;
|
||||
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)) {
|
||||
@@ -449,7 +425,7 @@ public class XML {
|
||||
sb.append(key);
|
||||
sb.append("/>");
|
||||
|
||||
// Emit a new tag <k>
|
||||
// Emit a new tag <k>
|
||||
|
||||
} else {
|
||||
sb.append(toString(value, key));
|
||||
@@ -457,34 +433,36 @@ public class XML {
|
||||
}
|
||||
if (tagName != null) {
|
||||
|
||||
// Emit the </tagname> close tag
|
||||
|
||||
// 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.
|
||||
}
|
||||
|
||||
} else {
|
||||
if (object != null) {
|
||||
if (object.getClass().isArray()) {
|
||||
object = new JSONArray(object);
|
||||
}
|
||||
|
||||
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));
|
||||
ja = (JSONArray) object;
|
||||
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();
|
||||
} else {
|
||||
string = (object == null) ? "null" : escape(object.toString());
|
||||
return (tagName == null) ? "\"" + string + "\"" :
|
||||
(string.length() == 0) ? "<" + tagName + "/>" :
|
||||
"<" + tagName + ">" + string + "</" + tagName + ">";
|
||||
}
|
||||
}
|
||||
|
||||
string = (object == null) ? "null" : escape(object.toString());
|
||||
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