mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
?:
This commit is contained in:
parent
9bb970c6bc
commit
2a39f47197
3 changed files with 308 additions and 302 deletions
505
JSONML.java
505
JSONML.java
|
@ -32,7 +32,7 @@ import java.util.Iterator;
|
||||||
* JSONObject, and to covert a JSONArray or JSONObject into an XML text using
|
* JSONObject, and to covert a JSONArray or JSONObject into an XML text using
|
||||||
* the JsonML transform.
|
* the JsonML transform.
|
||||||
* @author JSON.org
|
* @author JSON.org
|
||||||
* @version 2011-10-05
|
* @version 2011-11-24
|
||||||
*/
|
*/
|
||||||
public class JSONML {
|
public class JSONML {
|
||||||
|
|
||||||
|
@ -48,16 +48,16 @@ public class JSONML {
|
||||||
private static Object parse(
|
private static Object parse(
|
||||||
XMLTokener x,
|
XMLTokener x,
|
||||||
boolean arrayForm,
|
boolean arrayForm,
|
||||||
JSONArray ja
|
JSONArray ja
|
||||||
) throws JSONException {
|
) throws JSONException {
|
||||||
String attribute;
|
String attribute;
|
||||||
char c;
|
char c;
|
||||||
String closeTag = null;
|
String closeTag = null;
|
||||||
int i;
|
int i;
|
||||||
JSONArray newja = null;
|
JSONArray newja = null;
|
||||||
JSONObject newjo = null;
|
JSONObject newjo = null;
|
||||||
Object token;
|
Object token;
|
||||||
String tagName = null;
|
String tagName = null;
|
||||||
|
|
||||||
// Test for and skip past these forms:
|
// Test for and skip past these forms:
|
||||||
// <!-- ... -->
|
// <!-- ... -->
|
||||||
|
@ -69,165 +69,166 @@ public class JSONML {
|
||||||
if (!x.more()) {
|
if (!x.more()) {
|
||||||
throw x.syntaxError("Bad XML");
|
throw x.syntaxError("Bad XML");
|
||||||
}
|
}
|
||||||
token = x.nextContent();
|
token = x.nextContent();
|
||||||
if (token == XML.LT) {
|
if (token == XML.LT) {
|
||||||
token = x.nextToken();
|
token = x.nextToken();
|
||||||
if (token instanceof Character) {
|
if (token instanceof Character) {
|
||||||
if (token == XML.SLASH) {
|
if (token == XML.SLASH) {
|
||||||
|
|
||||||
// Close tag </
|
// Close tag </
|
||||||
|
|
||||||
token = x.nextToken();
|
token = x.nextToken();
|
||||||
if (!(token instanceof String)) {
|
if (!(token instanceof String)) {
|
||||||
throw new JSONException(
|
throw new JSONException(
|
||||||
"Expected a closing name instead of '" +
|
"Expected a closing name instead of '" +
|
||||||
token + "'.");
|
token + "'.");
|
||||||
}
|
}
|
||||||
if (x.nextToken() != XML.GT) {
|
if (x.nextToken() != XML.GT) {
|
||||||
throw x.syntaxError("Misshaped close tag");
|
throw x.syntaxError("Misshaped close tag");
|
||||||
}
|
}
|
||||||
return token;
|
return token;
|
||||||
} else if (token == XML.BANG) {
|
} else if (token == XML.BANG) {
|
||||||
|
|
||||||
// <!
|
// <!
|
||||||
|
|
||||||
c = x.next();
|
c = x.next();
|
||||||
if (c == '-') {
|
if (c == '-') {
|
||||||
if (x.next() == '-') {
|
if (x.next() == '-') {
|
||||||
x.skipPast("-->");
|
x.skipPast("-->");
|
||||||
}
|
}
|
||||||
x.back();
|
x.back();
|
||||||
} else if (c == '[') {
|
} else if (c == '[') {
|
||||||
token = x.nextToken();
|
token = x.nextToken();
|
||||||
if (token.equals("CDATA") && x.next() == '[') {
|
if (token.equals("CDATA") && x.next() == '[') {
|
||||||
if (ja != null) {
|
if (ja != null) {
|
||||||
ja.put(x.nextCDATA());
|
ja.put(x.nextCDATA());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw x.syntaxError("Expected 'CDATA['");
|
throw x.syntaxError("Expected 'CDATA['");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
i = 1;
|
i = 1;
|
||||||
do {
|
do {
|
||||||
token = x.nextMeta();
|
token = x.nextMeta();
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
throw x.syntaxError("Missing '>' after '<!'.");
|
throw x.syntaxError("Missing '>' after '<!'.");
|
||||||
} else if (token == XML.LT) {
|
} else if (token == XML.LT) {
|
||||||
i += 1;
|
i += 1;
|
||||||
} else if (token == XML.GT) {
|
} else if (token == XML.GT) {
|
||||||
i -= 1;
|
i -= 1;
|
||||||
}
|
}
|
||||||
} while (i > 0);
|
} while (i > 0);
|
||||||
}
|
}
|
||||||
} else if (token == XML.QUEST) {
|
} else if (token == XML.QUEST) {
|
||||||
|
|
||||||
// <?
|
// <?
|
||||||
|
|
||||||
x.skipPast("?>");
|
x.skipPast("?>");
|
||||||
} else {
|
} else {
|
||||||
throw x.syntaxError("Misshaped tag");
|
throw x.syntaxError("Misshaped tag");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Open tag <
|
// Open tag <
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (!(token instanceof String)) {
|
if (!(token instanceof String)) {
|
||||||
throw x.syntaxError("Bad tagName '" + token + "'.");
|
throw x.syntaxError("Bad tagName '" + token + "'.");
|
||||||
}
|
}
|
||||||
tagName = (String)token;
|
tagName = (String)token;
|
||||||
newja = new JSONArray();
|
newja = new JSONArray();
|
||||||
newjo = new JSONObject();
|
newjo = new JSONObject();
|
||||||
if (arrayForm) {
|
if (arrayForm) {
|
||||||
newja.put(tagName);
|
newja.put(tagName);
|
||||||
if (ja != null) {
|
if (ja != null) {
|
||||||
ja.put(newja);
|
ja.put(newja);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
newjo.put("tagName", tagName);
|
newjo.put("tagName", tagName);
|
||||||
if (ja != null) {
|
if (ja != null) {
|
||||||
ja.put(newjo);
|
ja.put(newjo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
token = null;
|
token = null;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
token = x.nextToken();
|
token = x.nextToken();
|
||||||
}
|
}
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
throw x.syntaxError("Misshaped tag");
|
throw x.syntaxError("Misshaped tag");
|
||||||
}
|
}
|
||||||
if (!(token instanceof String)) {
|
if (!(token instanceof String)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// attribute = value
|
// attribute = value
|
||||||
|
|
||||||
attribute = (String)token;
|
attribute = (String)token;
|
||||||
if (!arrayForm && (attribute == "tagName" || attribute == "childNode")) {
|
if (!arrayForm && (attribute == "tagName" || attribute == "childNode")) {
|
||||||
throw x.syntaxError("Reserved attribute.");
|
throw x.syntaxError("Reserved attribute.");
|
||||||
}
|
}
|
||||||
token = x.nextToken();
|
token = x.nextToken();
|
||||||
if (token == XML.EQ) {
|
if (token == XML.EQ) {
|
||||||
token = x.nextToken();
|
token = x.nextToken();
|
||||||
if (!(token instanceof String)) {
|
if (!(token instanceof String)) {
|
||||||
throw x.syntaxError("Missing value");
|
throw x.syntaxError("Missing value");
|
||||||
}
|
}
|
||||||
newjo.accumulate(attribute, XML.stringToValue((String)token));
|
newjo.accumulate(attribute, XML.stringToValue((String)token));
|
||||||
token = null;
|
token = null;
|
||||||
} else {
|
} else {
|
||||||
newjo.accumulate(attribute, "");
|
newjo.accumulate(attribute, "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (arrayForm && newjo.length() > 0) {
|
if (arrayForm && newjo.length() > 0) {
|
||||||
newja.put(newjo);
|
newja.put(newjo);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Empty tag <.../>
|
// Empty tag <.../>
|
||||||
|
|
||||||
if (token == XML.SLASH) {
|
if (token == XML.SLASH) {
|
||||||
if (x.nextToken() != XML.GT) {
|
if (x.nextToken() != XML.GT) {
|
||||||
throw x.syntaxError("Misshaped tag");
|
throw x.syntaxError("Misshaped tag");
|
||||||
}
|
}
|
||||||
if (ja == null) {
|
if (ja == null) {
|
||||||
if (arrayForm) {
|
if (arrayForm) {
|
||||||
return newja;
|
return newja;
|
||||||
} else {
|
} else {
|
||||||
return newjo;
|
return newjo;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Content, between <...> and </...>
|
// Content, between <...> and </...>
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (token != XML.GT) {
|
if (token != XML.GT) {
|
||||||
throw x.syntaxError("Misshaped tag");
|
throw x.syntaxError("Misshaped tag");
|
||||||
}
|
}
|
||||||
closeTag = (String)parse(x, arrayForm, newja);
|
closeTag = (String)parse(x, arrayForm, newja);
|
||||||
if (closeTag != null) {
|
if (closeTag != null) {
|
||||||
if (!closeTag.equals(tagName)) {
|
if (!closeTag.equals(tagName)) {
|
||||||
throw x.syntaxError("Mismatched '" + tagName +
|
throw x.syntaxError("Mismatched '" + tagName +
|
||||||
"' and '" + closeTag + "'");
|
"' and '" + closeTag + "'");
|
||||||
}
|
}
|
||||||
tagName = null;
|
tagName = null;
|
||||||
if (!arrayForm && newja.length() > 0) {
|
if (!arrayForm && newja.length() > 0) {
|
||||||
newjo.put("childNodes", newja);
|
newjo.put("childNodes", newja);
|
||||||
}
|
}
|
||||||
if (ja == null) {
|
if (ja == null) {
|
||||||
if (arrayForm) {
|
if (arrayForm) {
|
||||||
return newja;
|
return newja;
|
||||||
} else {
|
} else {
|
||||||
return newjo;
|
return newjo;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (ja != null) {
|
if (ja != null) {
|
||||||
ja.put(token instanceof String ?
|
ja.put(token instanceof String
|
||||||
XML.stringToValue((String)token) : token);
|
? XML.stringToValue((String)token)
|
||||||
}
|
: token);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,7 +246,7 @@ public class JSONML {
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public static JSONArray toJSONArray(String string) throws JSONException {
|
public static JSONArray toJSONArray(String string) throws JSONException {
|
||||||
return toJSONArray(new XMLTokener(string));
|
return toJSONArray(new XMLTokener(string));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -262,7 +263,7 @@ public class JSONML {
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public static JSONArray toJSONArray(XMLTokener x) throws JSONException {
|
public static JSONArray toJSONArray(XMLTokener x) throws JSONException {
|
||||||
return (JSONArray)parse(x, true, null);
|
return (JSONArray)parse(x, true, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -280,7 +281,7 @@ public class JSONML {
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public static JSONObject toJSONObject(XMLTokener x) throws JSONException {
|
public static JSONObject toJSONObject(XMLTokener x) throws JSONException {
|
||||||
return (JSONObject)parse(x, false, null);
|
return (JSONObject)parse(x, false, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -298,7 +299,7 @@ public class JSONML {
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public static JSONObject toJSONObject(String string) throws JSONException {
|
public static JSONObject toJSONObject(String string) throws JSONException {
|
||||||
return toJSONObject(new XMLTokener(string));
|
return toJSONObject(new XMLTokener(string));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -309,75 +310,75 @@ public class JSONML {
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public static String toString(JSONArray ja) throws JSONException {
|
public static String toString(JSONArray ja) throws JSONException {
|
||||||
int i;
|
int i;
|
||||||
JSONObject jo;
|
JSONObject jo;
|
||||||
String key;
|
String key;
|
||||||
Iterator keys;
|
Iterator keys;
|
||||||
int length;
|
int length;
|
||||||
Object object;
|
Object object;
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
String tagName;
|
String tagName;
|
||||||
String value;
|
String value;
|
||||||
|
|
||||||
// Emit <tagName
|
// Emit <tagName
|
||||||
|
|
||||||
tagName = ja.getString(0);
|
tagName = ja.getString(0);
|
||||||
XML.noSpace(tagName);
|
XML.noSpace(tagName);
|
||||||
tagName = XML.escape(tagName);
|
tagName = XML.escape(tagName);
|
||||||
sb.append('<');
|
sb.append('<');
|
||||||
sb.append(tagName);
|
sb.append(tagName);
|
||||||
|
|
||||||
object = ja.opt(1);
|
object = ja.opt(1);
|
||||||
if (object instanceof JSONObject) {
|
if (object instanceof JSONObject) {
|
||||||
i = 2;
|
i = 2;
|
||||||
jo = (JSONObject)object;
|
jo = (JSONObject)object;
|
||||||
|
|
||||||
// Emit the attributes
|
// Emit the attributes
|
||||||
|
|
||||||
keys = jo.keys();
|
keys = jo.keys();
|
||||||
while (keys.hasNext()) {
|
while (keys.hasNext()) {
|
||||||
key = keys.next().toString();
|
key = keys.next().toString();
|
||||||
XML.noSpace(key);
|
XML.noSpace(key);
|
||||||
value = jo.optString(key);
|
value = jo.optString(key);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
sb.append(' ');
|
sb.append(' ');
|
||||||
sb.append(XML.escape(key));
|
sb.append(XML.escape(key));
|
||||||
sb.append('=');
|
sb.append('=');
|
||||||
sb.append('"');
|
sb.append('"');
|
||||||
sb.append(XML.escape(value));
|
sb.append(XML.escape(value));
|
||||||
sb.append('"');
|
sb.append('"');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
i = 1;
|
i = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Emit content in body
|
//Emit content in body
|
||||||
|
|
||||||
length = ja.length();
|
length = ja.length();
|
||||||
if (i >= length) {
|
if (i >= length) {
|
||||||
sb.append('/');
|
sb.append('/');
|
||||||
sb.append('>');
|
sb.append('>');
|
||||||
} else {
|
} else {
|
||||||
sb.append('>');
|
sb.append('>');
|
||||||
do {
|
do {
|
||||||
object = ja.get(i);
|
object = ja.get(i);
|
||||||
i += 1;
|
i += 1;
|
||||||
if (object != null) {
|
if (object != null) {
|
||||||
if (object instanceof String) {
|
if (object instanceof String) {
|
||||||
sb.append(XML.escape(object.toString()));
|
sb.append(XML.escape(object.toString()));
|
||||||
} else if (object instanceof JSONObject) {
|
} else if (object instanceof JSONObject) {
|
||||||
sb.append(toString((JSONObject)object));
|
sb.append(toString((JSONObject)object));
|
||||||
} else if (object instanceof JSONArray) {
|
} else if (object instanceof JSONArray) {
|
||||||
sb.append(toString((JSONArray)object));
|
sb.append(toString((JSONArray)object));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (i < length);
|
} while (i < length);
|
||||||
sb.append('<');
|
sb.append('<');
|
||||||
sb.append('/');
|
sb.append('/');
|
||||||
sb.append(tagName);
|
sb.append(tagName);
|
||||||
sb.append('>');
|
sb.append('>');
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -390,27 +391,27 @@ public class JSONML {
|
||||||
* @return An XML string.
|
* @return An XML string.
|
||||||
* @throws JSONException
|
* @throws JSONException
|
||||||
*/
|
*/
|
||||||
public static String toString(JSONObject jo) throws JSONException {
|
public static String toString(JSONObject jo) throws JSONException {
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
int i;
|
int i;
|
||||||
JSONArray ja;
|
JSONArray ja;
|
||||||
String key;
|
String key;
|
||||||
Iterator keys;
|
Iterator keys;
|
||||||
int length;
|
int length;
|
||||||
Object object;
|
Object object;
|
||||||
String tagName;
|
String tagName;
|
||||||
String value;
|
String value;
|
||||||
|
|
||||||
//Emit <tagName
|
//Emit <tagName
|
||||||
|
|
||||||
tagName = jo.optString("tagName");
|
tagName = jo.optString("tagName");
|
||||||
if (tagName == null) {
|
if (tagName == null) {
|
||||||
return XML.escape(jo.toString());
|
return XML.escape(jo.toString());
|
||||||
}
|
}
|
||||||
XML.noSpace(tagName);
|
XML.noSpace(tagName);
|
||||||
tagName = XML.escape(tagName);
|
tagName = XML.escape(tagName);
|
||||||
sb.append('<');
|
sb.append('<');
|
||||||
sb.append(tagName);
|
sb.append(tagName);
|
||||||
|
|
||||||
//Emit the attributes
|
//Emit the attributes
|
||||||
|
|
||||||
|
@ -418,47 +419,47 @@ public class JSONML {
|
||||||
while (keys.hasNext()) {
|
while (keys.hasNext()) {
|
||||||
key = keys.next().toString();
|
key = keys.next().toString();
|
||||||
if (!key.equals("tagName") && !key.equals("childNodes")) {
|
if (!key.equals("tagName") && !key.equals("childNodes")) {
|
||||||
XML.noSpace(key);
|
XML.noSpace(key);
|
||||||
value = jo.optString(key);
|
value = jo.optString(key);
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
sb.append(' ');
|
sb.append(' ');
|
||||||
sb.append(XML.escape(key));
|
sb.append(XML.escape(key));
|
||||||
sb.append('=');
|
sb.append('=');
|
||||||
sb.append('"');
|
sb.append('"');
|
||||||
sb.append(XML.escape(value));
|
sb.append(XML.escape(value));
|
||||||
sb.append('"');
|
sb.append('"');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Emit content in body
|
//Emit content in body
|
||||||
|
|
||||||
ja = jo.optJSONArray("childNodes");
|
ja = jo.optJSONArray("childNodes");
|
||||||
if (ja == null) {
|
if (ja == null) {
|
||||||
sb.append('/');
|
sb.append('/');
|
||||||
sb.append('>');
|
sb.append('>');
|
||||||
} else {
|
} else {
|
||||||
sb.append('>');
|
sb.append('>');
|
||||||
length = ja.length();
|
length = ja.length();
|
||||||
for (i = 0; i < length; i += 1) {
|
for (i = 0; i < length; i += 1) {
|
||||||
object = ja.get(i);
|
object = ja.get(i);
|
||||||
if (object != null) {
|
if (object != null) {
|
||||||
if (object instanceof String) {
|
if (object instanceof String) {
|
||||||
sb.append(XML.escape(object.toString()));
|
sb.append(XML.escape(object.toString()));
|
||||||
} else if (object instanceof JSONObject) {
|
} else if (object instanceof JSONObject) {
|
||||||
sb.append(toString((JSONObject)object));
|
sb.append(toString((JSONObject)object));
|
||||||
} else if (object instanceof JSONArray) {
|
} else if (object instanceof JSONArray) {
|
||||||
sb.append(toString((JSONArray)object));
|
sb.append(toString((JSONArray)object));
|
||||||
} else {
|
} else {
|
||||||
sb.append(object.toString());
|
sb.append(object.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sb.append('<');
|
sb.append('<');
|
||||||
sb.append('/');
|
sb.append('/');
|
||||||
sb.append(tagName);
|
sb.append(tagName);
|
||||||
sb.append('>');
|
sb.append('>');
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -36,7 +36,7 @@ SOFTWARE.
|
||||||
* it. It is used by the JSONObject and JSONArray constructors to parse
|
* it. It is used by the JSONObject and JSONArray constructors to parse
|
||||||
* JSON source strings.
|
* JSON source strings.
|
||||||
* @author JSON.org
|
* @author JSON.org
|
||||||
* @version 2010-12-24
|
* @version 2011-11-24
|
||||||
*/
|
*/
|
||||||
public class JSONTokener {
|
public class JSONTokener {
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ public class JSONTokener {
|
||||||
private int index;
|
private int index;
|
||||||
private int line;
|
private int line;
|
||||||
private char previous;
|
private char previous;
|
||||||
private Reader reader;
|
private final Reader reader;
|
||||||
private boolean usePrevious;
|
private boolean usePrevious;
|
||||||
|
|
||||||
|
|
||||||
|
@ -55,8 +55,9 @@ public class JSONTokener {
|
||||||
* @param reader A reader.
|
* @param reader A reader.
|
||||||
*/
|
*/
|
||||||
public JSONTokener(Reader reader) {
|
public JSONTokener(Reader reader) {
|
||||||
this.reader = reader.markSupported() ?
|
this.reader = reader.markSupported()
|
||||||
reader : new BufferedReader(reader);
|
? reader
|
||||||
|
: new BufferedReader(reader);
|
||||||
this.eof = false;
|
this.eof = false;
|
||||||
this.usePrevious = false;
|
this.usePrevious = false;
|
||||||
this.previous = 0;
|
this.previous = 0;
|
||||||
|
@ -64,13 +65,13 @@ public class JSONTokener {
|
||||||
this.character = 1;
|
this.character = 1;
|
||||||
this.line = 1;
|
this.line = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a JSONTokener from an InputStream.
|
* Construct a JSONTokener from an InputStream.
|
||||||
*/
|
*/
|
||||||
public JSONTokener(InputStream inputStream) throws JSONException {
|
public JSONTokener(InputStream inputStream) throws JSONException {
|
||||||
this(new InputStreamReader(inputStream));
|
this(new InputStreamReader(inputStream));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -90,7 +91,7 @@ public class JSONTokener {
|
||||||
* the next number or identifier.
|
* the next number or identifier.
|
||||||
*/
|
*/
|
||||||
public void back() throws JSONException {
|
public void back() throws JSONException {
|
||||||
if (usePrevious || index <= 0) {
|
if (this.usePrevious || this.index <= 0) {
|
||||||
throw new JSONException("Stepping back two steps is not supported");
|
throw new JSONException("Stepping back two steps is not supported");
|
||||||
}
|
}
|
||||||
this.index -= 1;
|
this.index -= 1;
|
||||||
|
@ -118,9 +119,9 @@ public class JSONTokener {
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean end() {
|
public boolean end() {
|
||||||
return eof && !usePrevious;
|
return this.eof && !this.usePrevious;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -130,11 +131,11 @@ public class JSONTokener {
|
||||||
* @return true if not yet at the end of the source.
|
* @return true if not yet at the end of the source.
|
||||||
*/
|
*/
|
||||||
public boolean more() throws JSONException {
|
public boolean more() throws JSONException {
|
||||||
next();
|
this.next();
|
||||||
if (end()) {
|
if (this.end()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
back();
|
this.back();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,11 +156,11 @@ public class JSONTokener {
|
||||||
} catch (IOException exception) {
|
} catch (IOException exception) {
|
||||||
throw new JSONException(exception);
|
throw new JSONException(exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c <= 0) { // End of stream
|
if (c <= 0) { // End of stream
|
||||||
this.eof = true;
|
this.eof = true;
|
||||||
c = 0;
|
c = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.index += 1;
|
this.index += 1;
|
||||||
if (this.previous == '\r') {
|
if (this.previous == '\r') {
|
||||||
|
@ -184,9 +185,9 @@ public class JSONTokener {
|
||||||
* @throws JSONException if the character does not match.
|
* @throws JSONException if the character does not match.
|
||||||
*/
|
*/
|
||||||
public char next(char c) throws JSONException {
|
public char next(char c) throws JSONException {
|
||||||
char n = next();
|
char n = this.next();
|
||||||
if (n != c) {
|
if (n != c) {
|
||||||
throw syntaxError("Expected '" + c + "' and instead saw '" +
|
throw this.syntaxError("Expected '" + c + "' and instead saw '" +
|
||||||
n + "'");
|
n + "'");
|
||||||
}
|
}
|
||||||
return n;
|
return n;
|
||||||
|
@ -211,9 +212,9 @@ public class JSONTokener {
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
|
|
||||||
while (pos < n) {
|
while (pos < n) {
|
||||||
chars[pos] = next();
|
chars[pos] = this.next();
|
||||||
if (end()) {
|
if (this.end()) {
|
||||||
throw syntaxError("Substring bounds error");
|
throw this.syntaxError("Substring bounds error");
|
||||||
}
|
}
|
||||||
pos += 1;
|
pos += 1;
|
||||||
}
|
}
|
||||||
|
@ -228,7 +229,7 @@ public class JSONTokener {
|
||||||
*/
|
*/
|
||||||
public char nextClean() throws JSONException {
|
public char nextClean() throws JSONException {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
char c = next();
|
char c = this.next();
|
||||||
if (c == 0 || c > ' ') {
|
if (c == 0 || c > ' ') {
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
@ -251,14 +252,14 @@ public class JSONTokener {
|
||||||
char c;
|
char c;
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
for (;;) {
|
for (;;) {
|
||||||
c = next();
|
c = this.next();
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 0:
|
case 0:
|
||||||
case '\n':
|
case '\n':
|
||||||
case '\r':
|
case '\r':
|
||||||
throw syntaxError("Unterminated string");
|
throw this.syntaxError("Unterminated string");
|
||||||
case '\\':
|
case '\\':
|
||||||
c = next();
|
c = this.next();
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 'b':
|
case 'b':
|
||||||
sb.append('\b');
|
sb.append('\b');
|
||||||
|
@ -276,7 +277,7 @@ public class JSONTokener {
|
||||||
sb.append('\r');
|
sb.append('\r');
|
||||||
break;
|
break;
|
||||||
case 'u':
|
case 'u':
|
||||||
sb.append((char)Integer.parseInt(next(4), 16));
|
sb.append((char)Integer.parseInt(this.next(4), 16));
|
||||||
break;
|
break;
|
||||||
case '"':
|
case '"':
|
||||||
case '\'':
|
case '\'':
|
||||||
|
@ -285,7 +286,7 @@ public class JSONTokener {
|
||||||
sb.append(c);
|
sb.append(c);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw syntaxError("Illegal escape.");
|
throw this.syntaxError("Illegal escape.");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -307,10 +308,10 @@ public class JSONTokener {
|
||||||
public String nextTo(char delimiter) throws JSONException {
|
public String nextTo(char delimiter) throws JSONException {
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
for (;;) {
|
for (;;) {
|
||||||
char c = next();
|
char c = this.next();
|
||||||
if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
|
if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
|
||||||
if (c != 0) {
|
if (c != 0) {
|
||||||
back();
|
this.back();
|
||||||
}
|
}
|
||||||
return sb.toString().trim();
|
return sb.toString().trim();
|
||||||
}
|
}
|
||||||
|
@ -329,11 +330,11 @@ public class JSONTokener {
|
||||||
char c;
|
char c;
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
for (;;) {
|
for (;;) {
|
||||||
c = next();
|
c = this.next();
|
||||||
if (delimiters.indexOf(c) >= 0 || c == 0 ||
|
if (delimiters.indexOf(c) >= 0 || c == 0 ||
|
||||||
c == '\n' || c == '\r') {
|
c == '\n' || c == '\r') {
|
||||||
if (c != 0) {
|
if (c != 0) {
|
||||||
back();
|
this.back();
|
||||||
}
|
}
|
||||||
return sb.toString().trim();
|
return sb.toString().trim();
|
||||||
}
|
}
|
||||||
|
@ -350,18 +351,18 @@ public class JSONTokener {
|
||||||
* @return An object.
|
* @return An object.
|
||||||
*/
|
*/
|
||||||
public Object nextValue() throws JSONException {
|
public Object nextValue() throws JSONException {
|
||||||
char c = nextClean();
|
char c = this.nextClean();
|
||||||
String string;
|
String string;
|
||||||
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '"':
|
case '"':
|
||||||
case '\'':
|
case '\'':
|
||||||
return nextString(c);
|
return this.nextString(c);
|
||||||
case '{':
|
case '{':
|
||||||
back();
|
this.back();
|
||||||
return new JSONObject(this);
|
return new JSONObject(this);
|
||||||
case '[':
|
case '[':
|
||||||
back();
|
this.back();
|
||||||
return new JSONArray(this);
|
return new JSONArray(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -377,13 +378,13 @@ public class JSONTokener {
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
|
while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
|
||||||
sb.append(c);
|
sb.append(c);
|
||||||
c = next();
|
c = this.next();
|
||||||
}
|
}
|
||||||
back();
|
this.back();
|
||||||
|
|
||||||
string = sb.toString().trim();
|
string = sb.toString().trim();
|
||||||
if (string.equals("")) {
|
if (string.equals("")) {
|
||||||
throw syntaxError("Missing value");
|
throw this.syntaxError("Missing value");
|
||||||
}
|
}
|
||||||
return JSONObject.stringToValue(string);
|
return JSONObject.stringToValue(string);
|
||||||
}
|
}
|
||||||
|
@ -402,11 +403,11 @@ public class JSONTokener {
|
||||||
int startIndex = this.index;
|
int startIndex = this.index;
|
||||||
int startCharacter = this.character;
|
int startCharacter = this.character;
|
||||||
int startLine = this.line;
|
int startLine = this.line;
|
||||||
reader.mark(Integer.MAX_VALUE);
|
this.reader.mark(Integer.MAX_VALUE);
|
||||||
do {
|
do {
|
||||||
c = next();
|
c = this.next();
|
||||||
if (c == 0) {
|
if (c == 0) {
|
||||||
reader.reset();
|
this.reader.reset();
|
||||||
this.index = startIndex;
|
this.index = startIndex;
|
||||||
this.character = startCharacter;
|
this.character = startCharacter;
|
||||||
this.line = startLine;
|
this.line = startLine;
|
||||||
|
@ -417,10 +418,10 @@ public class JSONTokener {
|
||||||
throw new JSONException(exc);
|
throw new JSONException(exc);
|
||||||
}
|
}
|
||||||
|
|
||||||
back();
|
this.back();
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make a JSONException to signal a syntax error.
|
* Make a JSONException to signal a syntax error.
|
||||||
|
@ -429,7 +430,7 @@ public class JSONTokener {
|
||||||
* @return A JSONException object, suitable for throwing
|
* @return A JSONException object, suitable for throwing
|
||||||
*/
|
*/
|
||||||
public JSONException syntaxError(String message) {
|
public JSONException syntaxError(String message) {
|
||||||
return new JSONException(message + toString());
|
return new JSONException(message + this.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -439,7 +440,7 @@ public class JSONTokener {
|
||||||
* @return " at {index} [character {character} line {line}]"
|
* @return " at {index} [character {character} line {line}]"
|
||||||
*/
|
*/
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return " at " + index + " [character " + this.character + " line " +
|
return " at " + this.index + " [character " + this.character + " line " +
|
||||||
this.line + "]";
|
this.line + "]";
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -54,7 +54,7 @@ SOFTWARE.
|
||||||
* <p>
|
* <p>
|
||||||
* This can sometimes be easier than using a JSONObject to build a string.
|
* This can sometimes be easier than using a JSONObject to build a string.
|
||||||
* @author JSON.org
|
* @author JSON.org
|
||||||
* @version 2011-11-14
|
* @version 2011-11-24
|
||||||
*/
|
*/
|
||||||
public class JSONWriter {
|
public class JSONWriter {
|
||||||
private static final int maxdepth = 200;
|
private static final int maxdepth = 200;
|
||||||
|
@ -157,8 +157,9 @@ public class JSONWriter {
|
||||||
*/
|
*/
|
||||||
private JSONWriter end(char mode, char c) throws JSONException {
|
private JSONWriter end(char mode, char c) throws JSONException {
|
||||||
if (this.mode != mode) {
|
if (this.mode != mode) {
|
||||||
throw new JSONException(mode == 'a' ? "Misplaced endArray." :
|
throw new JSONException(mode == 'a'
|
||||||
"Misplaced endObject.");
|
? "Misplaced endArray."
|
||||||
|
: "Misplaced endObject.");
|
||||||
}
|
}
|
||||||
this.pop(mode);
|
this.pop(mode);
|
||||||
try {
|
try {
|
||||||
|
@ -259,8 +260,11 @@ public class JSONWriter {
|
||||||
throw new JSONException("Nesting error.");
|
throw new JSONException("Nesting error.");
|
||||||
}
|
}
|
||||||
this.top -= 1;
|
this.top -= 1;
|
||||||
this.mode = this.top == 0 ?
|
this.mode = this.top == 0
|
||||||
'd' : this.stack[this.top - 1] == null ? 'a' : 'k';
|
? 'd'
|
||||||
|
: this.stack[this.top - 1] == null
|
||||||
|
? 'a'
|
||||||
|
: 'k';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue