1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Season's Greetings

This commit is contained in:
Douglas Crockford 2010-12-25 14:12:07 -08:00
parent f284477cff
commit 87c4b1d59d
13 changed files with 562 additions and 566 deletions

View file

@ -41,7 +41,7 @@ SOFTWARE.
* The names for the elements in the JSONObjects can be taken from the names * The names for the elements in the JSONObjects can be taken from the names
* in the first row. * in the first row.
* @author JSON.org * @author JSON.org
* @version 2009-09-11 * @version 2010-12-24
*/ */
public class CDL { public class CDL {
@ -135,6 +135,43 @@ public class CDL {
} }
/** /**
* Produce a comma delimited text row from a JSONArray. Values containing
* the comma character will be quoted. Troublesome characters may be
* removed.
* @param ja A JSONArray of strings.
* @return A string ending in NEWLINE.
*/
public static String rowToString(JSONArray ja) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ja.length(); i += 1) {
if (i > 0) {
sb.append(',');
}
Object object = ja.opt(i);
if (object != null) {
String string = object.toString();
if (string.length() > 0 && (string.indexOf(',') >= 0 ||
string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 ||
string.indexOf(0) >= 0 || string.charAt(0) == '"')) {
sb.append('"');
int length = string.length();
for (int j = 0; j < length; j += 1) {
char c = string.charAt(j);
if (c >= ' ' && c != '"') {
sb.append(c);
}
}
sb.append('"');
} else {
sb.append(string);
}
}
}
sb.append('\n');
return sb.toString();
}
/**
* Produce a JSONArray of JSONObjects from a comma delimited text string, * Produce a JSONArray of JSONObjects from a comma delimited text string,
* using the first row as a source of names. * using the first row as a source of names.
* @param string The comma delimited text. * @param string The comma delimited text.
@ -197,43 +234,6 @@ public class CDL {
} }
/**
* Produce a comma delimited text row from a JSONArray. Values containing
* the comma character will be quoted. Troublesome characters may be
* removed.
* @param ja A JSONArray of strings.
* @return A string ending in NEWLINE.
*/
public static String rowToString(JSONArray ja) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ja.length(); i += 1) {
if (i > 0) {
sb.append(',');
}
Object o = ja.opt(i);
if (o != null) {
String s = o.toString();
if (s.length() > 0 && (s.indexOf(',') >= 0 || s.indexOf('\n') >= 0 ||
s.indexOf('\r') >= 0 || s.indexOf(0) >= 0 ||
s.charAt(0) == '"')) {
sb.append('"');
int length = s.length();
for (int j = 0; j < length; j += 1) {
char c = s.charAt(j);
if (c >= ' ' && c != '"') {
sb.append(c);
}
}
sb.append('"');
} else {
sb.append(s);
}
}
}
sb.append('\n');
return sb.toString();
}
/** /**
* Produce a comma delimited text from a JSONArray of JSONObjects. The * Produce a comma delimited text from a JSONArray of JSONObjects. The
* first row will be a list of names obtained by inspecting the first * first row will be a list of names obtained by inspecting the first

View file

@ -28,7 +28,7 @@ SOFTWARE.
* Convert a web browser cookie specification to a JSONObject and back. * Convert a web browser cookie specification to a JSONObject and back.
* JSON and Cookies are both notations for name/value pairs. * JSON and Cookies are both notations for name/value pairs.
* @author JSON.org * @author JSON.org
* @version 2008-09-18 * @version 2010-12-24
*/ */
public class Cookie { public class Cookie {
@ -48,8 +48,8 @@ public class Cookie {
char c; char c;
String s = string.trim(); String s = string.trim();
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
int len = s.length(); int length = s.length();
for (int i = 0; i < len; i += 1) { for (int i = 0; i < length; i += 1) {
c = s.charAt(i); c = s.charAt(i);
if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') { if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') {
sb.append('%'); sb.append('%');
@ -79,29 +79,29 @@ public class Cookie {
* @throws JSONException * @throws JSONException
*/ */
public static JSONObject toJSONObject(String string) throws JSONException { public static JSONObject toJSONObject(String string) throws JSONException {
String n; String name;
JSONObject o = new JSONObject(); JSONObject jo = new JSONObject();
Object v; Object value;
JSONTokener x = new JSONTokener(string); JSONTokener x = new JSONTokener(string);
o.put("name", x.nextTo('=')); jo.put("name", x.nextTo('='));
x.next('='); x.next('=');
o.put("value", x.nextTo(';')); jo.put("value", x.nextTo(';'));
x.next(); x.next();
while (x.more()) { while (x.more()) {
n = unescape(x.nextTo("=;")); name = unescape(x.nextTo("=;"));
if (x.next() != '=') { if (x.next() != '=') {
if (n.equals("secure")) { if (name.equals("secure")) {
v = Boolean.TRUE; value = Boolean.TRUE;
} else { } else {
throw x.syntaxError("Missing '=' in cookie parameter."); throw x.syntaxError("Missing '=' in cookie parameter.");
} }
} else { } else {
v = unescape(x.nextTo(';')); value = unescape(x.nextTo(';'));
x.next(); x.next();
} }
o.put(n, v); jo.put(name, value);
} }
return o; return jo;
} }
@ -111,29 +111,29 @@ public class Cookie {
* If the JSONObject contains "expires", "domain", "path", or "secure" * If the JSONObject contains "expires", "domain", "path", or "secure"
* members, they will be appended to the cookie specification string. * members, they will be appended to the cookie specification string.
* All other members are ignored. * All other members are ignored.
* @param o A JSONObject * @param jo A JSONObject
* @return A cookie specification string * @return A cookie specification string
* @throws JSONException * @throws JSONException
*/ */
public static String toString(JSONObject o) throws JSONException { public static String toString(JSONObject jo) throws JSONException {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
sb.append(escape(o.getString("name"))); sb.append(escape(jo.getString("name")));
sb.append("="); sb.append("=");
sb.append(escape(o.getString("value"))); sb.append(escape(jo.getString("value")));
if (o.has("expires")) { if (jo.has("expires")) {
sb.append(";expires="); sb.append(";expires=");
sb.append(o.getString("expires")); sb.append(jo.getString("expires"));
} }
if (o.has("domain")) { if (jo.has("domain")) {
sb.append(";domain="); sb.append(";domain=");
sb.append(escape(o.getString("domain"))); sb.append(escape(jo.getString("domain")));
} }
if (o.has("path")) { if (jo.has("path")) {
sb.append(";path="); sb.append(";path=");
sb.append(escape(o.getString("path"))); sb.append(escape(jo.getString("path")));
} }
if (o.optBoolean("secure")) { if (jo.optBoolean("secure")) {
sb.append(";secure"); sb.append(";secure");
} }
return sb.toString(); return sb.toString();
@ -142,28 +142,28 @@ public class Cookie {
/** /**
* Convert <code>%</code><i>hh</i> sequences to single characters, and * Convert <code>%</code><i>hh</i> sequences to single characters, and
* convert plus to space. * convert plus to space.
* @param s A string that may contain * @param string A string that may contain
* <code>+</code>&nbsp;<small>(plus)</small> and * <code>+</code>&nbsp;<small>(plus)</small> and
* <code>%</code><i>hh</i> sequences. * <code>%</code><i>hh</i> sequences.
* @return The unescaped string. * @return The unescaped string.
*/ */
public static String unescape(String s) { public static String unescape(String string) {
int len = s.length(); int length = string.length();
StringBuffer b = new StringBuffer(); StringBuffer sb = new StringBuffer();
for (int i = 0; i < len; ++i) { for (int i = 0; i < length; ++i) {
char c = s.charAt(i); char c = string.charAt(i);
if (c == '+') { if (c == '+') {
c = ' '; c = ' ';
} else if (c == '%' && i + 2 < len) { } else if (c == '%' && i + 2 < length) {
int d = JSONTokener.dehexchar(s.charAt(i + 1)); int d = JSONTokener.dehexchar(string.charAt(i + 1));
int e = JSONTokener.dehexchar(s.charAt(i + 2)); int e = JSONTokener.dehexchar(string.charAt(i + 2));
if (d >= 0 && e >= 0) { if (d >= 0 && e >= 0) {
c = (char)(d * 16 + e); c = (char)(d * 16 + e);
i += 2; i += 2;
} }
} }
b.append(c); sb.append(c);
} }
return b.toString(); return sb.toString();
} }
} }

View file

@ -29,7 +29,7 @@ import java.util.Iterator;
/** /**
* Convert a web browser cookie list string to a JSONObject and back. * Convert a web browser cookie list string to a JSONObject and back.
* @author JSON.org * @author JSON.org
* @version 2008-09-18 * @version 2010-12-24
*/ */
public class CookieList { public class CookieList {
@ -47,15 +47,15 @@ public class CookieList {
* @throws JSONException * @throws JSONException
*/ */
public static JSONObject toJSONObject(String string) throws JSONException { public static JSONObject toJSONObject(String string) throws JSONException {
JSONObject o = new JSONObject(); JSONObject jo = new JSONObject();
JSONTokener x = new JSONTokener(string); JSONTokener x = new JSONTokener(string);
while (x.more()) { while (x.more()) {
String name = Cookie.unescape(x.nextTo('=')); String name = Cookie.unescape(x.nextTo('='));
x.next('='); x.next('=');
o.put(name, Cookie.unescape(x.nextTo(';'))); jo.put(name, Cookie.unescape(x.nextTo(';')));
x.next(); x.next();
} }
return o; return jo;
} }
@ -64,24 +64,24 @@ public class CookieList {
* of name/value pairs. The names are separated from the values by '='. * of name/value pairs. The names are separated from the values by '='.
* The pairs are separated by ';'. The characters '%', '+', '=', and ';' * The pairs are separated by ';'. The characters '%', '+', '=', and ';'
* in the names and values are replaced by "%hh". * in the names and values are replaced by "%hh".
* @param o A JSONObject * @param jo A JSONObject
* @return A cookie list string * @return A cookie list string
* @throws JSONException * @throws JSONException
*/ */
public static String toString(JSONObject o) throws JSONException { public static String toString(JSONObject jo) throws JSONException {
boolean b = false; boolean b = false;
Iterator keys = o.keys(); Iterator keys = jo.keys();
String s; String string;
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
while (keys.hasNext()) { while (keys.hasNext()) {
s = keys.next().toString(); string = keys.next().toString();
if (!o.isNull(s)) { if (!jo.isNull(string)) {
if (b) { if (b) {
sb.append(';'); sb.append(';');
} }
sb.append(Cookie.escape(s)); sb.append(Cookie.escape(string));
sb.append("="); sb.append("=");
sb.append(Cookie.escape(o.getString(s))); sb.append(Cookie.escape(jo.getString(string)));
b = true; b = true;
} }
} }

View file

@ -29,7 +29,7 @@ import java.util.Iterator;
/** /**
* Convert an HTTP header to a JSONObject and back. * Convert an HTTP header to a JSONObject and back.
* @author JSON.org * @author JSON.org
* @version 2008-09-18 * @version 2010-12-24
*/ */
public class HTTP { public class HTTP {
@ -69,27 +69,27 @@ public class HTTP {
* @throws JSONException * @throws JSONException
*/ */
public static JSONObject toJSONObject(String string) throws JSONException { public static JSONObject toJSONObject(String string) throws JSONException {
JSONObject o = new JSONObject(); JSONObject jo = new JSONObject();
HTTPTokener x = new HTTPTokener(string); HTTPTokener x = new HTTPTokener(string);
String t; String token;
t = x.nextToken(); token = x.nextToken();
if (t.toUpperCase().startsWith("HTTP")) { if (token.toUpperCase().startsWith("HTTP")) {
// Response // Response
o.put("HTTP-Version", t); jo.put("HTTP-Version", token);
o.put("Status-Code", x.nextToken()); jo.put("Status-Code", x.nextToken());
o.put("Reason-Phrase", x.nextTo('\0')); jo.put("Reason-Phrase", x.nextTo('\0'));
x.next(); x.next();
} else { } else {
// Request // Request
o.put("Method", t); jo.put("Method", token);
o.put("Request-URI", x.nextToken()); jo.put("Request-URI", x.nextToken());
o.put("HTTP-Version", x.nextToken()); jo.put("HTTP-Version", x.nextToken());
} }
// Fields // Fields
@ -97,10 +97,10 @@ public class HTTP {
while (x.more()) { while (x.more()) {
String name = x.nextTo(':'); String name = x.nextTo(':');
x.next(':'); x.next(':');
o.put(name, x.nextTo('\0')); jo.put(name, x.nextTo('\0'));
x.next(); x.next();
} }
return o; return jo;
} }
@ -119,41 +119,41 @@ public class HTTP {
* }</pre> * }</pre>
* Any other members of the JSONObject will be output as HTTP fields. * Any other members of the JSONObject will be output as HTTP fields.
* The result will end with two CRLF pairs. * The result will end with two CRLF pairs.
* @param o A JSONObject * @param jo A JSONObject
* @return An HTTP header string. * @return An HTTP header string.
* @throws JSONException if the object does not contain enough * @throws JSONException if the object does not contain enough
* information. * information.
*/ */
public static String toString(JSONObject o) throws JSONException { public static String toString(JSONObject jo) throws JSONException {
Iterator keys = o.keys(); Iterator keys = jo.keys();
String s; String string;
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
if (o.has("Status-Code") && o.has("Reason-Phrase")) { if (jo.has("Status-Code") && jo.has("Reason-Phrase")) {
sb.append(o.getString("HTTP-Version")); sb.append(jo.getString("HTTP-Version"));
sb.append(' '); sb.append(' ');
sb.append(o.getString("Status-Code")); sb.append(jo.getString("Status-Code"));
sb.append(' '); sb.append(' ');
sb.append(o.getString("Reason-Phrase")); sb.append(jo.getString("Reason-Phrase"));
} else if (o.has("Method") && o.has("Request-URI")) { } else if (jo.has("Method") && jo.has("Request-URI")) {
sb.append(o.getString("Method")); sb.append(jo.getString("Method"));
sb.append(' '); sb.append(' ');
sb.append('"'); sb.append('"');
sb.append(o.getString("Request-URI")); sb.append(jo.getString("Request-URI"));
sb.append('"'); sb.append('"');
sb.append(' '); sb.append(' ');
sb.append(o.getString("HTTP-Version")); sb.append(jo.getString("HTTP-Version"));
} else { } else {
throw new JSONException("Not enough material for an HTTP header."); throw new JSONException("Not enough material for an HTTP header.");
} }
sb.append(CRLF); sb.append(CRLF);
while (keys.hasNext()) { while (keys.hasNext()) {
s = keys.next().toString(); string = keys.next().toString();
if (!s.equals("HTTP-Version") && !s.equals("Status-Code") && if (!string.equals("HTTP-Version") && !string.equals("Status-Code") &&
!s.equals("Reason-Phrase") && !s.equals("Method") && !string.equals("Reason-Phrase") && !string.equals("Method") &&
!s.equals("Request-URI") && !o.isNull(s)) { !string.equals("Request-URI") && !jo.isNull(string)) {
sb.append(s); sb.append(string);
sb.append(": "); sb.append(": ");
sb.append(o.getString(s)); sb.append(jo.getString(string));
sb.append(CRLF); sb.append(CRLF);
} }
} }

View file

@ -28,16 +28,16 @@ SOFTWARE.
* The HTTPTokener extends the JSONTokener to provide additional methods * The HTTPTokener extends the JSONTokener to provide additional methods
* for the parsing of HTTP headers. * for the parsing of HTTP headers.
* @author JSON.org * @author JSON.org
* @version 2008-09-18 * @version 2010-12-24
*/ */
public class HTTPTokener extends JSONTokener { public class HTTPTokener extends JSONTokener {
/** /**
* Construct an HTTPTokener from a string. * Construct an HTTPTokener from a string.
* @param s A source string. * @param string A source string.
*/ */
public HTTPTokener(String s) { public HTTPTokener(String string) {
super(s); super(string);
} }

View file

@ -78,7 +78,7 @@ import java.util.Map;
* </ul> * </ul>
* @author JSON.org * @author JSON.org
* @version 2009-04-14 * @version 2010-12-24
*/ */
public class JSONArray { public class JSONArray {
@ -103,45 +103,33 @@ public class JSONArray {
*/ */
public JSONArray(JSONTokener x) throws JSONException { public JSONArray(JSONTokener x) throws JSONException {
this(); this();
char c = x.nextClean(); if (x.nextClean() != '[') {
char q;
if (c == '[') {
q = ']';
} else if (c == '(') {
q = ')';
} else {
throw x.syntaxError("A JSONArray text must start with '['"); throw x.syntaxError("A JSONArray text must start with '['");
} }
if (x.nextClean() == ']') { if (x.nextClean() != ']') {
return; x.back();
} for (;;) {
x.back(); if (x.nextClean() == ',') {
for (;;) { x.back();
if (x.nextClean() == ',') { this.myArrayList.add(JSONObject.NULL);
x.back(); } else {
this.myArrayList.add(null); x.back();
} else { this.myArrayList.add(x.nextValue());
x.back(); }
this.myArrayList.add(x.nextValue()); switch (x.nextClean()) {
} case ';':
c = x.nextClean(); case ',':
switch (c) { if (x.nextClean() == ']') {
case ';': return;
case ',': }
if (x.nextClean() == ']') { x.back();
return; break;
} case ']':
x.back(); return;
break; default:
case ']': throw x.syntaxError("Expected a ',' or ']'");
case ')': }
if (q != c) { }
throw x.syntaxError("Expected a '" + new Character(q) + "'");
}
return;
default:
throw x.syntaxError("Expected a ',' or ']'");
}
} }
} }
@ -167,8 +155,7 @@ public class JSONArray {
if (collection != null) { if (collection != null) {
Iterator iter = collection.iterator(); Iterator iter = collection.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
Object o = iter.next(); this.myArrayList.add(JSONObject.wrap(iter.next()));
this.myArrayList.add(JSONObject.wrap(o));
} }
} }
} }
@ -200,11 +187,11 @@ public class JSONArray {
* @throws JSONException If there is no value for the index. * @throws JSONException If there is no value for the index.
*/ */
public Object get(int index) throws JSONException { public Object get(int index) throws JSONException {
Object o = opt(index); Object object = opt(index);
if (o == null) { if (object == null) {
throw new JSONException("JSONArray[" + index + "] not found."); throw new JSONException("JSONArray[" + index + "] not found.");
} }
return o; return object;
} }
@ -215,20 +202,20 @@ public class JSONArray {
* @param index The index must be between 0 and length() - 1. * @param index The index must be between 0 and length() - 1.
* @return The truth. * @return The truth.
* @throws JSONException If there is no value for the index or if the * @throws JSONException If there is no value for the index or if the
* value is not convertable to boolean. * value is not convertible to boolean.
*/ */
public boolean getBoolean(int index) throws JSONException { public boolean getBoolean(int index) throws JSONException {
Object o = get(index); Object object = get(index);
if (o.equals(Boolean.FALSE) || if (object.equals(Boolean.FALSE) ||
(o instanceof String && (object instanceof String &&
((String)o).equalsIgnoreCase("false"))) { ((String)object).equalsIgnoreCase("false"))) {
return false; return false;
} else if (o.equals(Boolean.TRUE) || } else if (object.equals(Boolean.TRUE) ||
(o instanceof String && (object instanceof String &&
((String)o).equalsIgnoreCase("true"))) { ((String)object).equalsIgnoreCase("true"))) {
return true; return true;
} }
throw new JSONException("JSONArray[" + index + "] is not a Boolean."); throw new JSONException("JSONArray[" + index + "] is not a boolean.");
} }
@ -241,11 +228,11 @@ public class JSONArray {
* be converted to a number. * be converted to a number.
*/ */
public double getDouble(int index) throws JSONException { public double getDouble(int index) throws JSONException {
Object o = get(index); Object object = get(index);
try { try {
return o instanceof Number ? return object instanceof Number ?
((Number)o).doubleValue() : ((Number)object).doubleValue() :
Double.valueOf((String)o).doubleValue(); Double.parseDouble((String)object);
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONArray[" + index + throw new JSONException("JSONArray[" + index +
"] is not a number."); "] is not a number.");
@ -258,14 +245,18 @@ public class JSONArray {
* *
* @param index The index must be between 0 and length() - 1. * @param index The index must be between 0 and length() - 1.
* @return The value. * @return The value.
* @throws JSONException If the key is not found or if the value cannot * @throws JSONException If the key is not found or if the value is not a number.
* be converted to a number.
* if the value cannot be converted to a number.
*/ */
public int getInt(int index) throws JSONException { public int getInt(int index) throws JSONException {
Object o = get(index); Object object = get(index);
return o instanceof Number ? try {
((Number)o).intValue() : (int)getDouble(index); return object instanceof Number ?
((Number)object).intValue() :
Integer.parseInt((String)object);
} catch (Exception e) {
throw new JSONException("JSONArray[" + index +
"] is not a number.");
}
} }
@ -277,9 +268,9 @@ public class JSONArray {
* value is not a JSONArray * value is not a JSONArray
*/ */
public JSONArray getJSONArray(int index) throws JSONException { public JSONArray getJSONArray(int index) throws JSONException {
Object o = get(index); Object object = get(index);
if (o instanceof JSONArray) { if (object instanceof JSONArray) {
return (JSONArray)o; return (JSONArray)object;
} }
throw new JSONException("JSONArray[" + index + throw new JSONException("JSONArray[" + index +
"] is not a JSONArray."); "] is not a JSONArray.");
@ -294,9 +285,9 @@ public class JSONArray {
* value is not a JSONObject * value is not a JSONObject
*/ */
public JSONObject getJSONObject(int index) throws JSONException { public JSONObject getJSONObject(int index) throws JSONException {
Object o = get(index); Object object = get(index);
if (o instanceof JSONObject) { if (object instanceof JSONObject) {
return (JSONObject)o; return (JSONObject)object;
} }
throw new JSONException("JSONArray[" + index + throw new JSONException("JSONArray[" + index +
"] is not a JSONObject."); "] is not a JSONObject.");
@ -312,9 +303,15 @@ public class JSONArray {
* be converted to a number. * be converted to a number.
*/ */
public long getLong(int index) throws JSONException { public long getLong(int index) throws JSONException {
Object o = get(index); Object object = get(index);
return o instanceof Number ? try {
((Number)o).longValue() : (long)getDouble(index); return object instanceof Number ?
((Number)object).longValue() :
Long.parseLong((String)object);
} catch (Exception e) {
throw new JSONException("JSONArray[" + index +
"] is not a number.");
}
} }
@ -553,8 +550,8 @@ public class JSONArray {
* @return A String value. * @return A String value.
*/ */
public String optString(int index, String defaultValue) { public String optString(int index, String defaultValue) {
Object o = opt(index); Object object = opt(index);
return o != null ? o.toString() : defaultValue; return object != null ? object.toString() : defaultValue;
} }

View file

@ -3,12 +3,9 @@ package org.json;
/** /**
* The JSONException is thrown by the JSON.org classes when things are amiss. * The JSONException is thrown by the JSON.org classes when things are amiss.
* @author JSON.org * @author JSON.org
* @version 2008-09-18 * @version 2010-12-24
*/ */
public class JSONException extends Exception { public class JSONException extends Exception {
/**
*
*/
private static final long serialVersionUID = 0; private static final long serialVersionUID = 0;
private Throwable cause; private Throwable cause;
@ -20,9 +17,9 @@ public class JSONException extends Exception {
super(message); super(message);
} }
public JSONException(Throwable t) { public JSONException(Throwable cause) {
super(t.getMessage()); super(cause.getMessage());
this.cause = t; this.cause = cause;
} }
public Throwable getCause() { public Throwable getCause() {

View file

@ -154,7 +154,7 @@ public class JSONML {
break; break;
} }
// attribute = value // attribute = value
attribute = (String)token; attribute = (String)token;
if (!arrayForm && (attribute == "tagName" || attribute == "childNode")) { if (!arrayForm && (attribute == "tagName" || attribute == "childNode")) {
@ -259,7 +259,6 @@ public class JSONML {
return (JSONArray)parse(x, true, null); return (JSONArray)parse(x, true, null);
} }
/** /**
* Convert a well-formed (but not necessarily valid) XML string into a * Convert a well-formed (but not necessarily valid) XML string into a
@ -277,6 +276,8 @@ public class JSONML {
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);
} }
/** /**
* Convert a well-formed (but not necessarily valid) XML string into a * Convert a well-formed (but not necessarily valid) XML string into a
* JSONObject using the JsonML transform. Each XML tag is represented as * JSONObject using the JsonML transform. Each XML tag is represented as
@ -302,15 +303,15 @@ public class JSONML {
* @throws JSONException * @throws JSONException
*/ */
public static String toString(JSONArray ja) throws JSONException { public static String toString(JSONArray ja) throws JSONException {
Object e;
int i; int i;
JSONObject jo; JSONObject jo;
String k; String key;
Iterator keys; Iterator keys;
int length; int length;
Object object;
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
String tagName; String tagName;
String v; String value;
// Emit <tagName // Emit <tagName
@ -320,24 +321,24 @@ public class JSONML {
sb.append('<'); sb.append('<');
sb.append(tagName); sb.append(tagName);
e = ja.opt(1); object = ja.opt(1);
if (e instanceof JSONObject) { if (object instanceof JSONObject) {
i = 2; i = 2;
jo = (JSONObject)e; jo = (JSONObject)object;
// Emit the attributes // Emit the attributes
keys = jo.keys(); keys = jo.keys();
while (keys.hasNext()) { while (keys.hasNext()) {
k = keys.next().toString(); key = keys.next().toString();
XML.noSpace(k); XML.noSpace(key);
v = jo.optString(k); value = jo.optString(key);
if (v != null) { if (value != null) {
sb.append(' '); sb.append(' ');
sb.append(XML.escape(k)); sb.append(XML.escape(key));
sb.append('='); sb.append('=');
sb.append('"'); sb.append('"');
sb.append(XML.escape(v)); sb.append(XML.escape(value));
sb.append('"'); sb.append('"');
} }
} }
@ -354,15 +355,15 @@ public class JSONML {
} else { } else {
sb.append('>'); sb.append('>');
do { do {
e = ja.get(i); object = ja.get(i);
i += 1; i += 1;
if (e != null) { if (object != null) {
if (e instanceof String) { if (object instanceof String) {
sb.append(XML.escape(e.toString())); sb.append(XML.escape(object.toString()));
} else if (e instanceof JSONObject) { } else if (object instanceof JSONObject) {
sb.append(toString((JSONObject)e)); sb.append(toString((JSONObject)object));
} else if (e instanceof JSONArray) { } else if (object instanceof JSONArray) {
sb.append(toString((JSONArray)e)); sb.append(toString((JSONArray)object));
} }
} }
} while (i < length); } while (i < length);
@ -385,14 +386,14 @@ public class JSONML {
*/ */
public static String toString(JSONObject jo) throws JSONException { public static String toString(JSONObject jo) throws JSONException {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
Object e;
int i; int i;
JSONArray ja; JSONArray ja;
String k; String key;
Iterator keys; Iterator keys;
int len; int length;
Object object;
String tagName; String tagName;
String v; String value;
//Emit <tagName //Emit <tagName
@ -409,16 +410,16 @@ public class JSONML {
keys = jo.keys(); keys = jo.keys();
while (keys.hasNext()) { while (keys.hasNext()) {
k = keys.next().toString(); key = keys.next().toString();
if (!k.equals("tagName") && !k.equals("childNodes")) { if (!key.equals("tagName") && !key.equals("childNodes")) {
XML.noSpace(k); XML.noSpace(key);
v = jo.optString(k); value = jo.optString(key);
if (v != null) { if (value != null) {
sb.append(' '); sb.append(' ');
sb.append(XML.escape(k)); sb.append(XML.escape(key));
sb.append('='); sb.append('=');
sb.append('"'); sb.append('"');
sb.append(XML.escape(v)); sb.append(XML.escape(value));
sb.append('"'); sb.append('"');
} }
} }
@ -432,16 +433,16 @@ public class JSONML {
sb.append('>'); sb.append('>');
} else { } else {
sb.append('>'); sb.append('>');
len = ja.length(); length = ja.length();
for (i = 0; i < len; i += 1) { for (i = 0; i < length; i += 1) {
e = ja.get(i); object = ja.get(i);
if (e != null) { if (object != null) {
if (e instanceof String) { if (object instanceof String) {
sb.append(XML.escape(e.toString())); sb.append(XML.escape(object.toString()));
} else if (e instanceof JSONObject) { } else if (object instanceof JSONObject) {
sb.append(toString((JSONObject)e)); sb.append(toString((JSONObject)object));
} else if (e instanceof JSONArray) { } else if (object instanceof JSONArray) {
sb.append(toString((JSONArray)e)); sb.append(toString((JSONArray)object));
} }
} }
} }

View file

@ -59,10 +59,11 @@ import java.util.TreeSet;
* The generic <code>get()</code> and <code>opt()</code> methods return an * The generic <code>get()</code> and <code>opt()</code> methods return an
* object, which you can cast or query for type. There are also typed * object, which you can cast or query for type. There are also typed
* <code>get</code> and <code>opt</code> methods that do type checking and type * <code>get</code> and <code>opt</code> methods that do type checking and type
* coercion for you. * coercion for you. The opt methods differ from the get methods in that they
* do not throw. Instead, they return a specified value, such as null.
* <p> * <p>
* The <code>put</code> methods adds values to an object. For example, <pre> * The <code>put</code> methods add or replace values in an object. For example,
* myString = new JSONObject().put("JSON", "Hello, World!").toString();</pre> * <pre>myString = new JSONObject().put("JSON", "Hello, World!").toString();</pre>
* produces the string <code>{"JSON": "Hello, World"}</code>. * produces the string <code>{"JSON": "Hello, World"}</code>.
* <p> * <p>
* The texts produced by the <code>toString</code> methods strictly conform to * The texts produced by the <code>toString</code> methods strictly conform to
@ -86,7 +87,7 @@ import java.util.TreeSet;
* <li>Numbers may have the <code>0x-</code> <small>(hex)</small> prefix.</li> * <li>Numbers may have the <code>0x-</code> <small>(hex)</small> prefix.</li>
* </ul> * </ul>
* @author JSON.org * @author JSON.org
* @version 2010-12-20 * @version 2010-12-24
*/ */
public class JSONObject { public class JSONObject {
@ -106,7 +107,6 @@ public class JSONObject {
return this; return this;
} }
/** /**
* A Null object is equal to the null value and to itself. * A Null object is equal to the null value and to itself.
* @param object An object to test for nullness. * @param object An object to test for nullness.
@ -117,7 +117,6 @@ public class JSONObject {
return object == null || object == this; return object == null || object == this;
} }
/** /**
* Get the "null" string value. * Get the "null" string value.
* @return The string "null". * @return The string "null".
@ -368,15 +367,14 @@ public class JSONObject {
public JSONObject accumulate(String key, Object value) public JSONObject accumulate(String key, Object value)
throws JSONException { throws JSONException {
testValidity(value); testValidity(value);
Object o = opt(key); Object object = opt(key);
if (o == null) { if (object == null) {
put(key, value instanceof JSONArray ? put(key, value instanceof JSONArray ?
new JSONArray().put(value) : new JSONArray().put(value) : value);
value); } else if (object instanceof JSONArray) {
} else if (o instanceof JSONArray) { ((JSONArray)object).put(value);
((JSONArray)o).put(value);
} else { } else {
put(key, new JSONArray().put(o).put(value)); put(key, new JSONArray().put(object).put(value));
} }
return this; return this;
} }
@ -395,11 +393,11 @@ public class JSONObject {
*/ */
public JSONObject append(String key, Object value) throws JSONException { public JSONObject append(String key, Object value) throws JSONException {
testValidity(value); testValidity(value);
Object o = opt(key); Object object = opt(key);
if (o == null) { if (object == null) {
put(key, new JSONArray().put(value)); put(key, new JSONArray().put(value));
} else if (o instanceof JSONArray) { } else if (object instanceof JSONArray) {
put(key, ((JSONArray)o).put(value)); put(key, ((JSONArray)object).put(value));
} else { } else {
throw new JSONException("JSONObject[" + key + throw new JSONException("JSONObject[" + key +
"] is not a JSONArray."); "] is not a JSONArray.");
@ -421,16 +419,17 @@ public class JSONObject {
// Shave off trailing zeros and decimal point, if possible. // Shave off trailing zeros and decimal point, if possible.
String s = Double.toString(d); String string = Double.toString(d);
if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) { if (string.indexOf('.') > 0 && string.indexOf('e') < 0 &&
while (s.endsWith("0")) { string.indexOf('E') < 0) {
s = s.substring(0, s.length() - 1); while (string.endsWith("0")) {
string = string.substring(0, string.length() - 1);
} }
if (s.endsWith(".")) { if (string.endsWith(".")) {
s = s.substring(0, s.length() - 1); string = string.substring(0, string.length() - 1);
} }
} }
return s; return string;
} }
@ -439,15 +438,18 @@ public class JSONObject {
* *
* @param key A key string. * @param key A key string.
* @return The object associated with the key. * @return The object associated with the key.
* @throws JSONException if the key is not found. * @throws JSONException if the key is not found.
*/ */
public Object get(String key) throws JSONException { public Object get(String key) throws JSONException {
Object o = opt(key); if (key == null) {
if (o == null) { throw new JSONException("Null key.");
}
Object object = opt(key);
if (object == null) {
throw new JSONException("JSONObject[" + quote(key) + throw new JSONException("JSONObject[" + quote(key) +
"] not found."); "] not found.");
} }
return o; return object;
} }
@ -456,18 +458,18 @@ public class JSONObject {
* *
* @param key A key string. * @param key A key string.
* @return The truth. * @return The truth.
* @throws JSONException * @throws JSONException
* if the value is not a Boolean or the String "true" or "false". * if the value is not a Boolean or the String "true" or "false".
*/ */
public boolean getBoolean(String key) throws JSONException { public boolean getBoolean(String key) throws JSONException {
Object o = get(key); Object object = get(key);
if (o.equals(Boolean.FALSE) || if (object.equals(Boolean.FALSE) ||
(o instanceof String && (object instanceof String &&
((String)o).equalsIgnoreCase("false"))) { ((String)object).equalsIgnoreCase("false"))) {
return false; return false;
} else if (o.equals(Boolean.TRUE) || } else if (object.equals(Boolean.TRUE) ||
(o instanceof String && (object instanceof String &&
((String)o).equalsIgnoreCase("true"))) { ((String)object).equalsIgnoreCase("true"))) {
return true; return true;
} }
throw new JSONException("JSONObject[" + quote(key) + throw new JSONException("JSONObject[" + quote(key) +
@ -483,11 +485,11 @@ public class JSONObject {
* if the value is not a Number object and cannot be converted to a number. * if the value is not a Number object and cannot be converted to a number.
*/ */
public double getDouble(String key) throws JSONException { public double getDouble(String key) throws JSONException {
Object o = get(key); Object object = get(key);
try { try {
return o instanceof Number ? return object instanceof Number ?
((Number)o).doubleValue() : ((Number)object).doubleValue() :
Double.valueOf((String)o).doubleValue(); Double.parseDouble((String)object);
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key) + throw new JSONException("JSONObject[" + quote(key) +
"] is not a number."); "] is not a number.");
@ -504,11 +506,11 @@ public class JSONObject {
* be converted to an integer. * be converted to an integer.
*/ */
public int getInt(String key) throws JSONException { public int getInt(String key) throws JSONException {
Object o = get(key); Object object = get(key);
try { try {
return o instanceof Number ? return object instanceof Number ?
((Number)o).intValue() : ((Number)object).intValue() :
Integer.parseInt((String)o); Integer.parseInt((String)object);
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key) + throw new JSONException("JSONObject[" + quote(key) +
"] is not an int."); "] is not an int.");
@ -521,13 +523,13 @@ public class JSONObject {
* *
* @param key A key string. * @param key A key string.
* @return A JSONArray which is the value. * @return A JSONArray which is the value.
* @throws JSONException if the key is not found or * @throws JSONException if the key is not found or
* if the value is not a JSONArray. * if the value is not a JSONArray.
*/ */
public JSONArray getJSONArray(String key) throws JSONException { public JSONArray getJSONArray(String key) throws JSONException {
Object o = get(key); Object object = get(key);
if (o instanceof JSONArray) { if (object instanceof JSONArray) {
return (JSONArray)o; return (JSONArray)object;
} }
throw new JSONException("JSONObject[" + quote(key) + throw new JSONException("JSONObject[" + quote(key) +
"] is not a JSONArray."); "] is not a JSONArray.");
@ -539,13 +541,13 @@ public class JSONObject {
* *
* @param key A key string. * @param key A key string.
* @return A JSONObject which is the value. * @return A JSONObject which is the value.
* @throws JSONException if the key is not found or * @throws JSONException if the key is not found or
* if the value is not a JSONObject. * if the value is not a JSONObject.
*/ */
public JSONObject getJSONObject(String key) throws JSONException { public JSONObject getJSONObject(String key) throws JSONException {
Object o = get(key); Object object = get(key);
if (o instanceof JSONObject) { if (object instanceof JSONObject) {
return (JSONObject)o; return (JSONObject)object;
} }
throw new JSONException("JSONObject[" + quote(key) + throw new JSONException("JSONObject[" + quote(key) +
"] is not a JSONObject."); "] is not a JSONObject.");
@ -561,11 +563,11 @@ public class JSONObject {
* be converted to a long. * be converted to a long.
*/ */
public long getLong(String key) throws JSONException { public long getLong(String key) throws JSONException {
Object o = get(key); Object object = get(key);
try { try {
return o instanceof Number ? return object instanceof Number ?
((Number)o).longValue() : ((Number)object).longValue() :
Long.parseLong((String)o); Long.parseLong((String)object);
} catch (Exception e) { } catch (Exception e) {
throw new JSONException("JSONObject[" + quote(key) + throw new JSONException("JSONObject[" + quote(key) +
"] is not a long."); "] is not a long.");
@ -583,12 +585,12 @@ public class JSONObject {
if (length == 0) { if (length == 0) {
return null; return null;
} }
Iterator i = jo.keys(); Iterator iterator = jo.keys();
String[] names = new String[length]; String[] names = new String[length];
int j = 0; int i = 0;
while (i.hasNext()) { while (iterator.hasNext()) {
names[j] = (String)i.next(); names[i] = (String)iterator.next();
j += 1; i += 1;
} }
return names; return names;
} }
@ -652,18 +654,16 @@ public class JSONObject {
Object value = opt(key); Object value = opt(key);
if (value == null) { if (value == null) {
put(key, 1); put(key, 1);
} else if (value instanceof Integer) {
put(key, ((Integer)value).intValue() + 1);
} else if (value instanceof Long) {
put(key, ((Long)value).longValue() + 1);
} else if (value instanceof Double) {
put(key, ((Double)value).doubleValue() + 1);
} else if (value instanceof Float) {
put(key, ((Float)value).floatValue() + 1);
} else { } else {
if (value instanceof Integer) { throw new JSONException("Unable to increment [" + quote(key) + "].");
put(key, ((Integer)value).intValue() + 1);
} else if (value instanceof Long) {
put(key, ((Long)value).longValue() + 1);
} else if (value instanceof Double) {
put(key, ((Double)value).doubleValue() + 1);
} else if (value instanceof Float) {
put(key, ((Float)value).floatValue() + 1);
} else {
throw new JSONException("Unable to increment [" + key + "].");
}
} }
return this; return this;
} }
@ -718,29 +718,30 @@ public class JSONObject {
/** /**
* Produce a string from a Number. * Produce a string from a Number.
* @param n A Number * @param number A Number
* @return A String. * @return A String.
* @throws JSONException If n is a non-finite number. * @throws JSONException If n is a non-finite number.
*/ */
public static String numberToString(Number n) public static String numberToString(Number number)
throws JSONException { throws JSONException {
if (n == null) { if (number == null) {
throw new JSONException("Null pointer"); throw new JSONException("Null pointer");
} }
testValidity(n); testValidity(number);
// Shave off trailing zeros and decimal point, if possible. // Shave off trailing zeros and decimal point, if possible.
String s = n.toString(); String string = number.toString();
if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) { if (string.indexOf('.') > 0 && string.indexOf('e') < 0 &&
while (s.endsWith("0")) { string.indexOf('E') < 0) {
s = s.substring(0, s.length() - 1); while (string.endsWith("0")) {
string = string.substring(0, string.length() - 1);
} }
if (s.endsWith(".")) { if (string.endsWith(".")) {
s = s.substring(0, s.length() - 1); string = string.substring(0, string.length() - 1);
} }
} }
return s; return string;
} }
@ -811,9 +812,7 @@ public class JSONObject {
*/ */
public double optDouble(String key, double defaultValue) { public double optDouble(String key, double defaultValue) {
try { try {
Object o = opt(key); return getDouble(key);
return o instanceof Number ? ((Number)o).doubleValue() :
new Double((String)o).doubleValue();
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
} }
@ -876,8 +875,8 @@ public class JSONObject {
* @return A JSONObject which is the value. * @return A JSONObject which is the value.
*/ */
public JSONObject optJSONObject(String key) { public JSONObject optJSONObject(String key) {
Object o = opt(key); Object object = opt(key);
return o instanceof JSONObject ? (JSONObject)o : null; return object instanceof JSONObject ? (JSONObject)object : null;
} }
@ -901,9 +900,9 @@ public class JSONObject {
* If the value is a string, an attempt will be made to evaluate it as * If the value is a string, an attempt will be made to evaluate it as
* a number. * a number.
* *
* @param key A key string. * @param key A key string.
* @param defaultValue The default. * @param defaultValue The default.
* @return An object which is the value. * @return An object which is the value.
*/ */
public long optLong(String key, long defaultValue) { public long optLong(String key, long defaultValue) {
try { try {
@ -936,8 +935,8 @@ public class JSONObject {
* @return A string which is the value. * @return A string which is the value.
*/ */
public String optString(String key, String defaultValue) { public String optString(String key, String defaultValue) {
Object o = opt(key); Object object = opt(key);
return NULL.equals(o) ? defaultValue : o.toString(); return NULL.equals(object) ? defaultValue : object.toString();
} }
@ -1150,10 +1149,10 @@ public class JSONObject {
char b; char b;
char c = 0; char c = 0;
String hhhh;
int i; int i;
int len = string.length(); int len = string.length();
StringBuffer sb = new StringBuffer(len + 4); StringBuffer sb = new StringBuffer(len + 4);
String t;
sb.append('"'); sb.append('"');
for (i = 0; i < len; i += 1) { for (i = 0; i < len; i += 1) {
@ -1189,8 +1188,8 @@ public class JSONObject {
default: default:
if (c < ' ' || (c >= '\u0080' && c < '\u00a0') || if (c < ' ' || (c >= '\u0080' && c < '\u00a0') ||
(c >= '\u2000' && c < '\u2100')) { (c >= '\u2000' && c < '\u2100')) {
t = "000" + Integer.toHexString(c); hhhh = "000" + Integer.toHexString(c);
sb.append("\\u" + t.substring(t.length() - 4)); sb.append("\\u" + hhhh.substring(hhhh.length() - 4));
} else { } else {
sb.append(c); sb.append(c);
} }
@ -1223,20 +1222,20 @@ public class JSONObject {
/** /**
* Try to convert a string into a number, boolean, or null. If the string * Try to convert a string into a number, boolean, or null. If the string
* can't be converted, return the string. * can't be converted, return the string.
* @param s A String. * @param string A String.
* @return A simple JSON value. * @return A simple JSON value.
*/ */
public static Object stringToValue(String s) { public static Object stringToValue(String string) {
if (s.equals("")) { if (string.equals("")) {
return s; return string;
} }
if (s.equalsIgnoreCase("true")) { if (string.equalsIgnoreCase("true")) {
return Boolean.TRUE; return Boolean.TRUE;
} }
if (s.equalsIgnoreCase("false")) { if (string.equalsIgnoreCase("false")) {
return Boolean.FALSE; return Boolean.FALSE;
} }
if (s.equalsIgnoreCase("null")) { if (string.equalsIgnoreCase("null")) {
return JSONObject.NULL; return JSONObject.NULL;
} }
@ -1249,21 +1248,21 @@ public class JSONObject {
* non-JSON forms as long as it accepts all correct JSON forms. * non-JSON forms as long as it accepts all correct JSON forms.
*/ */
char b = s.charAt(0); char b = string.charAt(0);
if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') { if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') {
if (b == '0' && s.length() > 2 && if (b == '0' && string.length() > 2 &&
(s.charAt(1) == 'x' || s.charAt(1) == 'X')) { (string.charAt(1) == 'x' || string.charAt(1) == 'X')) {
try { try {
return new Integer(Integer.parseInt(s.substring(2), 16)); return new Integer(Integer.parseInt(string.substring(2), 16));
} catch (Exception ignore) { } catch (Exception ignore) {
} }
} }
try { try {
if (s.indexOf('.') > -1 || if (string.indexOf('.') > -1 ||
s.indexOf('e') > -1 || s.indexOf('E') > -1) { string.indexOf('e') > -1 || string.indexOf('E') > -1) {
return Double.valueOf(s); return Double.valueOf(string);
} else { } else {
Long myLong = new Long(s); Long myLong = new Long(string);
if (myLong.longValue() == myLong.intValue()) { if (myLong.longValue() == myLong.intValue()) {
return new Integer(myLong.intValue()); return new Integer(myLong.intValue());
} else { } else {
@ -1273,12 +1272,12 @@ public class JSONObject {
} catch (Exception ignore) { } catch (Exception ignore) {
} }
} }
return s; return string;
} }
/** /**
* Throw an exception if the object is an NaN or infinite number. * Throw an exception if the object is a NaN or infinite number.
* @param o The object to test. * @param o The object to test.
* @throws JSONException If o is a non-finite number. * @throws JSONException If o is a non-finite number.
*/ */
@ -1383,40 +1382,40 @@ public class JSONObject {
* @throws JSONException If the object contains an invalid number. * @throws JSONException If the object contains an invalid number.
*/ */
String toString(int indentFactor, int indent) throws JSONException { String toString(int indentFactor, int indent) throws JSONException {
int j; int i;
int n = length(); int length = this.length();
if (n == 0) { if (length == 0) {
return "{}"; return "{}";
} }
Iterator keys = sortedKeys(); Iterator keys = sortedKeys();
StringBuffer sb = new StringBuffer("{");
int newindent = indent + indentFactor; int newindent = indent + indentFactor;
Object o; Object object;
if (n == 1) { StringBuffer sb = new StringBuffer("{");
o = keys.next(); if (length == 1) {
sb.append(quote(o.toString())); object = keys.next();
sb.append(quote(object.toString()));
sb.append(": "); sb.append(": ");
sb.append(valueToString(this.map.get(o), indentFactor, sb.append(valueToString(this.map.get(object), indentFactor,
indent)); indent));
} else { } else {
while (keys.hasNext()) { while (keys.hasNext()) {
o = keys.next(); object = keys.next();
if (sb.length() > 1) { if (sb.length() > 1) {
sb.append(",\n"); sb.append(",\n");
} else { } else {
sb.append('\n'); sb.append('\n');
} }
for (j = 0; j < newindent; j += 1) { for (i = 0; i < newindent; i += 1) {
sb.append(' '); sb.append(' ');
} }
sb.append(quote(o.toString())); sb.append(quote(object.toString()));
sb.append(": "); sb.append(": ");
sb.append(valueToString(this.map.get(o), indentFactor, sb.append(valueToString(this.map.get(object), indentFactor,
newindent)); newindent));
} }
if (sb.length() > 1) { if (sb.length() > 1) {
sb.append('\n'); sb.append('\n');
for (j = 0; j < indent; j += 1) { for (i = 0; i < indent; i += 1) {
sb.append(' '); sb.append(' ');
} }
} }
@ -1452,16 +1451,16 @@ public class JSONObject {
return "null"; return "null";
} }
if (value instanceof JSONString) { if (value instanceof JSONString) {
Object o; Object object;
try { try {
o = ((JSONString)value).toJSONString(); object = ((JSONString)value).toJSONString();
} catch (Exception e) { } catch (Exception e) {
throw new JSONException(e); throw new JSONException(e);
} }
if (o instanceof String) { if (object instanceof String) {
return (String)o; return (String)object;
} }
throw new JSONException("Bad value from toJSONString: " + o); throw new JSONException("Bad value from toJSONString: " + object);
} }
if (value instanceof Number) { if (value instanceof Number) {
return numberToString((Number) value); return numberToString((Number) value);
@ -1553,12 +1552,12 @@ public class JSONObject {
if (object == null) { if (object == null) {
return NULL; return NULL;
} }
if (object instanceof JSONObject || object instanceof JSONArray || if (object instanceof JSONObject || object instanceof JSONArray ||
NULL.equals(object) || object instanceof JSONString || NULL.equals(object) || object instanceof JSONString ||
object instanceof Byte || object instanceof Character || object instanceof Byte || object instanceof Character ||
object instanceof Short || object instanceof Integer || object instanceof Short || object instanceof Integer ||
object instanceof Long || object instanceof Boolean || object instanceof Long || object instanceof Boolean ||
object instanceof Float || object instanceof Double || object instanceof Float || object instanceof Double ||
object instanceof String) { object instanceof String) {
return object; return object;
} }
@ -1597,26 +1596,26 @@ public class JSONObject {
*/ */
public Writer write(Writer writer) throws JSONException { public Writer write(Writer writer) throws JSONException {
try { try {
boolean b = false; boolean commanate = false;
Iterator keys = keys(); Iterator keys = keys();
writer.write('{'); writer.write('{');
while (keys.hasNext()) { while (keys.hasNext()) {
if (b) { if (commanate) {
writer.write(','); writer.write(',');
} }
Object k = keys.next(); Object key = keys.next();
writer.write(quote(k.toString())); writer.write(quote(key.toString()));
writer.write(':'); writer.write(':');
Object v = this.map.get(k); Object value = this.map.get(key);
if (v instanceof JSONObject) { if (value instanceof JSONObject) {
((JSONObject)v).write(writer); ((JSONObject)value).write(writer);
} else if (v instanceof JSONArray) { } else if (value instanceof JSONArray) {
((JSONArray)v).write(writer); ((JSONArray)value).write(writer);
} else { } else {
writer.write(valueToString(v)); writer.write(valueToString(value));
} }
b = true; commanate = true;
} }
writer.write('}'); writer.write('}');
return writer; return writer;

View file

@ -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-20 * @version 2010-12-24
*/ */
public class JSONTokener { public class JSONTokener {
@ -207,17 +207,17 @@ public class JSONTokener {
return ""; return "";
} }
char[] buffer = new char[n]; char[] chars = new char[n];
int pos = 0; int pos = 0;
while (pos < n) { while (pos < n) {
buffer[pos] = next(); chars[pos] = next();
if (end()) { if (end()) {
throw syntaxError("Substring bounds error"); throw syntaxError("Substring bounds error");
} }
pos += 1; pos += 1;
} }
return new String(buffer); return new String(chars);
} }
@ -301,14 +301,14 @@ public class JSONTokener {
/** /**
* Get the text up but not including the specified character or the * Get the text up but not including the specified character or the
* end of line, whichever comes first. * end of line, whichever comes first.
* @param d A delimiter character. * @param delimiter A delimiter character.
* @return A string. * @return A string.
*/ */
public String nextTo(char d) throws JSONException { public String nextTo(char delimiter) throws JSONException {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
for (;;) { for (;;) {
char c = next(); char c = next();
if (c == d || c == 0 || c == '\n' || c == '\r') { if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
if (c != 0) { if (c != 0) {
back(); back();
} }
@ -351,7 +351,7 @@ public class JSONTokener {
*/ */
public Object nextValue() throws JSONException { public Object nextValue() throws JSONException {
char c = nextClean(); char c = nextClean();
String s; String string;
switch (c) { switch (c) {
case '"': case '"':
@ -361,7 +361,6 @@ public class JSONTokener {
back(); back();
return new JSONObject(this); return new JSONObject(this);
case '[': case '[':
case '(':
back(); back();
return new JSONArray(this); return new JSONArray(this);
} }
@ -382,11 +381,11 @@ public class JSONTokener {
} }
back(); back();
s = sb.toString().trim(); string = sb.toString().trim();
if (s.equals("")) { if (string.equals("")) {
throw syntaxError("Missing value"); throw syntaxError("Missing value");
} }
return JSONObject.stringToValue(s); return JSONObject.stringToValue(string);
} }
@ -440,6 +439,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 " + this.line + "]"; return " at " + index + " [character " + this.character + " line " +
this.line + "]";
} }
} }

View file

@ -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 2010-03-11 * @version 2010-12-24
*/ */
public class JSONWriter { public class JSONWriter {
private static final int maxdepth = 20; private static final int maxdepth = 20;
@ -103,12 +103,12 @@ public class JSONWriter {
/** /**
* Append a value. * Append a value.
* @param s A string value. * @param string A string value.
* @return this * @return this
* @throws JSONException If the value is out of sequence. * @throws JSONException If the value is out of sequence.
*/ */
private JSONWriter append(String s) throws JSONException { private JSONWriter append(String string) throws JSONException {
if (s == null) { if (string == null) {
throw new JSONException("Null pointer"); throw new JSONException("Null pointer");
} }
if (this.mode == 'o' || this.mode == 'a') { if (this.mode == 'o' || this.mode == 'a') {
@ -116,7 +116,7 @@ public class JSONWriter {
if (this.comma && this.mode == 'a') { if (this.comma && this.mode == 'a') {
this.writer.write(','); this.writer.write(',');
} }
this.writer.write(s); this.writer.write(string);
} catch (IOException e) { } catch (IOException e) {
throw new JSONException(e); throw new JSONException(e);
} }
@ -150,17 +150,17 @@ public class JSONWriter {
/** /**
* End something. * End something.
* @param m Mode * @param mode Mode
* @param c Closing character * @param c Closing character
* @return this * @return this
* @throws JSONException If unbalanced. * @throws JSONException If unbalanced.
*/ */
private JSONWriter end(char m, char c) throws JSONException { private JSONWriter end(char mode, char c) throws JSONException {
if (this.mode != m) { if (this.mode != mode) {
throw new JSONException(m == 'a' ? "Misplaced endArray." : throw new JSONException(mode == 'a' ? "Misplaced endArray." :
"Misplaced endObject."); "Misplaced endObject.");
} }
this.pop(m); this.pop(mode);
try { try {
this.writer.write(c); this.writer.write(c);
} catch (IOException e) { } catch (IOException e) {
@ -193,22 +193,22 @@ public class JSONWriter {
/** /**
* Append a key. The key will be associated with the next value. In an * Append a key. The key will be associated with the next value. In an
* object, every value must be preceded by a key. * object, every value must be preceded by a key.
* @param s A key string. * @param string A key string.
* @return this * @return this
* @throws JSONException If the key is out of place. For example, keys * @throws JSONException If the key is out of place. For example, keys
* do not belong in arrays or if the key is null. * do not belong in arrays or if the key is null.
*/ */
public JSONWriter key(String s) throws JSONException { public JSONWriter key(String string) throws JSONException {
if (s == null) { if (string == null) {
throw new JSONException("Null key."); throw new JSONException("Null key.");
} }
if (this.mode == 'k') { if (this.mode == 'k') {
try { try {
stack[top - 1].putOnce(s, Boolean.TRUE); stack[top - 1].putOnce(string, Boolean.TRUE);
if (this.comma) { if (this.comma) {
this.writer.write(','); this.writer.write(',');
} }
this.writer.write(JSONObject.quote(s)); this.writer.write(JSONObject.quote(string));
this.writer.write(':'); this.writer.write(':');
this.comma = false; this.comma = false;
this.mode = 'o'; this.mode = 'o';
@ -259,7 +259,8 @@ public class JSONWriter {
throw new JSONException("Nesting error."); throw new JSONException("Nesting error.");
} }
this.top -= 1; this.top -= 1;
this.mode = this.top == 0 ? 'd' : this.stack[this.top - 1] == null ? 'a' : 'k'; this.mode = this.top == 0 ?
'd' : this.stack[this.top - 1] == null ? 'a' : 'k';
} }
/** /**
@ -311,13 +312,12 @@ public class JSONWriter {
/** /**
* Append an object value. * Append an object value.
* @param o The object to append. It can be null, or a Boolean, Number, * @param object The object to append. It can be null, or a Boolean, Number,
* String, JSONObject, or JSONArray, or an object with a toJSONString() * String, JSONObject, or JSONArray, or an object that implements JSONString.
* method.
* @return this * @return this
* @throws JSONException If the value is out of sequence. * @throws JSONException If the value is out of sequence.
*/ */
public JSONWriter value(Object o) throws JSONException { public JSONWriter value(Object object) throws JSONException {
return this.append(JSONObject.valueToString(o)); return this.append(JSONObject.valueToString(object));
} }
} }

254
XML.java
View file

@ -31,7 +31,7 @@ import java.util.Iterator;
* This provides static methods to convert an XML text into a JSONObject, * This provides static methods to convert an XML text into a JSONObject,
* and to covert a JSONObject into an XML text. * and to covert a JSONObject into an XML text.
* @author JSON.org * @author JSON.org
* @version 2010-12-23 * @version 2010-12-24
*/ */
public class XML { public class XML {
@ -75,7 +75,7 @@ public class XML {
*/ */
public static String escape(String string) { public static String escape(String string) {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
for (int i = 0, len = string.length(); i < len; i++) { for (int i = 0, length = string.length(); i < length; i++) {
char c = string.charAt(i); char c = string.charAt(i);
switch (c) { switch (c) {
case '&': case '&':
@ -128,10 +128,10 @@ public class XML {
String name) throws JSONException { String name) throws JSONException {
char c; char c;
int i; int i;
String n; JSONObject jsonobject = null;
JSONObject o = null; String string;
String s; String tagName;
Object t; Object token;
// Test for and skip past these forms: // Test for and skip past these forms:
// <!-- ... --> // <!-- ... -->
@ -143,11 +143,11 @@ public class XML {
// <= // <=
// << // <<
t = x.nextToken(); token = x.nextToken();
// <! // <!
if (t == BANG) { if (token == BANG) {
c = x.next(); c = x.next();
if (c == '-') { if (c == '-') {
if (x.next() == '-') { if (x.next() == '-') {
@ -156,12 +156,12 @@ public class XML {
} }
x.back(); x.back();
} else if (c == '[') { } else if (c == '[') {
t = x.nextToken(); token = x.nextToken();
if (t.equals("CDATA")) { if (token.equals("CDATA")) {
if (x.next() == '[') { if (x.next() == '[') {
s = x.nextCDATA(); string = x.nextCDATA();
if (s.length() > 0) { if (string.length() > 0) {
context.accumulate("content", s); context.accumulate("content", string);
} }
return false; return false;
} }
@ -170,108 +170,111 @@ public class XML {
} }
i = 1; i = 1;
do { do {
t = x.nextMeta(); token = x.nextMeta();
if (t == null) { if (token == null) {
throw x.syntaxError("Missing '>' after '<!'."); throw x.syntaxError("Missing '>' after '<!'.");
} else if (t == LT) { } else if (token == LT) {
i += 1; i += 1;
} else if (t == GT) { } else if (token == GT) {
i -= 1; i -= 1;
} }
} while (i > 0); } while (i > 0);
return false; return false;
} else if (t == QUEST) { } else if (token == QUEST) {
// <? // <?
x.skipPast("?>"); x.skipPast("?>");
return false; return false;
} else if (t == SLASH) { } else if (token == SLASH) {
// Close tag </ // Close tag </
t = x.nextToken(); token = x.nextToken();
if (name == null) { if (name == null) {
throw x.syntaxError("Mismatched close tag" + t); throw x.syntaxError("Mismatched close tag " + token);
} }
if (!t.equals(name)) { if (!token.equals(name)) {
throw x.syntaxError("Mismatched " + name + " and " + t); throw x.syntaxError("Mismatched " + name + " and " + token);
} }
if (x.nextToken() != GT) { if (x.nextToken() != GT) {
throw x.syntaxError("Misshaped close tag"); throw x.syntaxError("Misshaped close tag");
} }
return true; return true;
} else if (t instanceof Character) { } else if (token instanceof Character) {
throw x.syntaxError("Misshaped tag"); throw x.syntaxError("Misshaped tag");
// Open tag < // Open tag <
} else { } else {
n = (String)t; tagName = (String)token;
t = null; token = null;
o = new JSONObject(); jsonobject = new JSONObject();
for (;;) { for (;;) {
if (t == null) { if (token == null) {
t = x.nextToken(); token = x.nextToken();
} }
// attribute = value // attribute = value
if (t instanceof String) { if (token instanceof String) {
s = (String)t; string = (String)token;
t = x.nextToken(); token = x.nextToken();
if (t == EQ) { if (token == EQ) {
t = x.nextToken(); token = x.nextToken();
if (!(t instanceof String)) { if (!(token instanceof String)) {
throw x.syntaxError("Missing value"); throw x.syntaxError("Missing value");
} }
o.accumulate(s, XML.stringToValue((String)t)); jsonobject.accumulate(string,
t = null; XML.stringToValue((String)token));
token = null;
} else { } else {
o.accumulate(s, ""); jsonobject.accumulate(string, "");
} }
// Empty tag <.../> // Empty tag <.../>
} else if (t == SLASH) { } else if (token == SLASH) {
if (x.nextToken() != GT) { if (x.nextToken() != GT) {
throw x.syntaxError("Misshaped tag"); throw x.syntaxError("Misshaped tag");
} }
if (o.length() > 0) { if (jsonobject.length() > 0) {
context.accumulate(n, o); context.accumulate(tagName, jsonobject);
} else { } else {
context.accumulate(n, ""); context.accumulate(tagName, "");
} }
return false; return false;
// Content, between <...> and </...> // Content, between <...> and </...>
} else if (t == GT) { } else if (token == GT) {
for (;;) { for (;;) {
t = x.nextContent(); token = x.nextContent();
if (t == null) { if (token == null) {
if (n != null) { if (tagName != null) {
throw x.syntaxError("Unclosed tag " + n); throw x.syntaxError("Unclosed tag " + tagName);
} }
return false; return false;
} else if (t instanceof String) { } else if (token instanceof String) {
s = (String)t; string = (String)token;
if (s.length() > 0) { if (string.length() > 0) {
o.accumulate("content", XML.stringToValue(s)); jsonobject.accumulate("content",
XML.stringToValue(string));
} }
// Nested element // Nested element
} else if (t == LT) { } else if (token == LT) {
if (parse(x, o, n)) { if (parse(x, jsonobject, tagName)) {
if (o.length() == 0) { if (jsonobject.length() == 0) {
context.accumulate(n, ""); context.accumulate(tagName, "");
} else if (o.length() == 1 && } else if (jsonobject.length() == 1 &&
o.opt("content") != null) { jsonobject.opt("content") != null) {
context.accumulate(n, o.opt("content")); context.accumulate(tagName,
jsonobject.opt("content"));
} else { } else {
context.accumulate(n, o); context.accumulate(tagName, jsonobject);
} }
return false; return false;
} }
@ -354,142 +357,141 @@ public class XML {
* @throws JSONException * @throws JSONException
*/ */
public static JSONObject toJSONObject(String string) throws JSONException { public static JSONObject toJSONObject(String string) throws JSONException {
JSONObject o = new JSONObject(); JSONObject jo = new JSONObject();
XMLTokener x = new XMLTokener(string); XMLTokener x = new XMLTokener(string);
while (x.more() && x.skipPast("<")) { while (x.more() && x.skipPast("<")) {
parse(x, o, null); parse(x, jo, null);
} }
return o; return jo;
} }
/** /**
* Convert a JSONObject into a well-formed, element-normal XML string. * Convert a JSONObject into a well-formed, element-normal XML string.
* @param o A JSONObject. * @param object A JSONObject.
* @return A string. * @return A string.
* @throws JSONException * @throws JSONException
*/ */
public static String toString(Object o) throws JSONException { public static String toString(Object object) throws JSONException {
return toString(o, null); return toString(object, null);
} }
/** /**
* Convert a JSONObject into a well-formed, element-normal XML string. * Convert a JSONObject into a well-formed, element-normal XML string.
* @param o A JSONObject. * @param object A JSONObject.
* @param tagName The optional name of the enclosing tag. * @param tagName The optional name of the enclosing tag.
* @return A string. * @return A string.
* @throws JSONException * @throws JSONException
*/ */
public static String toString(Object o, String tagName) public static String toString(Object object, String tagName)
throws JSONException { throws JSONException {
StringBuffer b = new StringBuffer(); StringBuffer sb = new StringBuffer();
int i; int i;
JSONArray ja; JSONArray ja;
JSONObject jo; JSONObject jo;
String k; String key;
Iterator keys; Iterator keys;
int len; int length;
String s; String string;
Object v; Object value;
if (o instanceof JSONObject) { if (object instanceof JSONObject) {
// Emit <tagName> // Emit <tagName>
if (tagName != null) { if (tagName != null) {
b.append('<'); sb.append('<');
b.append(tagName); sb.append(tagName);
b.append('>'); sb.append('>');
} }
// Loop thru the keys. // Loop thru the keys.
jo = (JSONObject)o; jo = (JSONObject)object;
keys = jo.keys(); keys = jo.keys();
while (keys.hasNext()) { while (keys.hasNext()) {
k = keys.next().toString(); key = keys.next().toString();
v = jo.opt(k); value = jo.opt(key);
if (v == null) { if (value == null) {
v = ""; value = "";
} }
if (v instanceof String) { if (value instanceof String) {
s = (String)v; string = (String)value;
} else { } else {
s = null; string = null;
} }
// Emit content in body // Emit content in body
if (k.equals("content")) { if (key.equals("content")) {
if (v instanceof JSONArray) { if (value instanceof JSONArray) {
ja = (JSONArray)v; ja = (JSONArray)value;
len = ja.length(); length = ja.length();
for (i = 0; i < len; i += 1) { for (i = 0; i < length; i += 1) {
if (i > 0) { if (i > 0) {
b.append('\n'); sb.append('\n');
} }
b.append(escape(ja.get(i).toString())); sb.append(escape(ja.get(i).toString()));
} }
} else { } else {
b.append(escape(v.toString())); sb.append(escape(value.toString()));
} }
// Emit an array of similar keys // Emit an array of similar keys
} else if (v instanceof JSONArray) { } else if (value instanceof JSONArray) {
ja = (JSONArray)v; ja = (JSONArray)value;
len = ja.length(); length = ja.length();
for (i = 0; i < len; i += 1) { for (i = 0; i < length; i += 1) {
v = ja.get(i); value = ja.get(i);
if (v instanceof JSONArray) { if (value instanceof JSONArray) {
b.append('<'); sb.append('<');
b.append(k); sb.append(key);
b.append('>'); sb.append('>');
b.append(toString(v)); sb.append(toString(value));
b.append("</"); sb.append("</");
b.append(k); sb.append(key);
b.append('>'); sb.append('>');
} else { } else {
b.append(toString(v, k)); sb.append(toString(value, key));
} }
} }
} else if (v.equals("")) { } else if (value.equals("")) {
b.append('<'); sb.append('<');
b.append(k); sb.append(key);
b.append("/>"); sb.append("/>");
// Emit a new tag <k> // Emit a new tag <k>
} else { } else {
b.append(toString(v, k)); sb.append(toString(value, key));
} }
} }
if (tagName != null) { if (tagName != null) {
// Emit the </tagname> close tag // Emit the </tagname> close tag
b.append("</"); sb.append("</");
b.append(tagName); sb.append(tagName);
b.append('>'); sb.append('>');
} }
return b.toString(); return sb.toString();
// XML does not have good support for arrays. If an array appears in a place // XML does not have good support for arrays. If an array appears in a place
// where XML is lacking, synthesize an <array> element. // where XML is lacking, synthesize an <array> element.
} else if (o instanceof JSONArray) { } else if (object instanceof JSONArray) {
ja = (JSONArray)o; ja = (JSONArray)object;
len = ja.length(); length = ja.length();
for (i = 0; i < len; ++i) { for (i = 0; i < length; i += 1) {
v = ja.opt(i); sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName));
b.append(toString(v, (tagName == null) ? "array" : tagName));
} }
return b.toString(); return sb.toString();
} else { } else {
s = (o == null) ? "null" : escape(o.toString()); string = (object == null) ? "null" : escape(object.toString());
return (tagName == null) ? "\"" + s + "\"" : return (tagName == null) ? "\"" + string + "\"" :
(s.length() == 0) ? "<" + tagName + "/>" : (string.length() == 0) ? "<" + tagName + "/>" :
"<" + tagName + ">" + s + "</" + tagName + ">"; "<" + tagName + ">" + string + "</" + tagName + ">";
} }
} }
} }

View file

@ -28,7 +28,7 @@ SOFTWARE.
* The XMLTokener extends the JSONTokener to provide additional methods * The XMLTokener extends the JSONTokener to provide additional methods
* for the parsing of XML texts. * for the parsing of XML texts.
* @author JSON.org * @author JSON.org
* @version 2010-01-30 * @version 2010-12-24
*/ */
public class XMLTokener extends JSONTokener { public class XMLTokener extends JSONTokener {
@ -120,11 +120,11 @@ public class XMLTokener extends JSONTokener {
/** /**
* Return the next entity. These entities are translated to Characters: * Return the next entity. These entities are translated to Characters:
* <code>&amp; &apos; &gt; &lt; &quot;</code>. * <code>&amp; &apos; &gt; &lt; &quot;</code>.
* @param a An ampersand character. * @param ampersand An ampersand character.
* @return A Character or an entity String if the entity is not recognized. * @return A Character or an entity String if the entity is not recognized.
* @throws JSONException If missing ';' in XML entity. * @throws JSONException If missing ';' in XML entity.
*/ */
public Object nextEntity(char a) throws JSONException { public Object nextEntity(char ampersand) throws JSONException {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
for (;;) { for (;;) {
char c = next(); char c = next();
@ -136,9 +136,9 @@ public class XMLTokener extends JSONTokener {
throw syntaxError("Missing ';' in XML entity: &" + sb); throw syntaxError("Missing ';' in XML entity: &" + sb);
} }
} }
String s = sb.toString(); String string = sb.toString();
Object e = entity.get(s); Object object = entity.get(string);
return e != null ? e : a + s + ";"; return object != null ? object : ampersand + string + ";";
} }
@ -304,15 +304,15 @@ public class XMLTokener extends JSONTokener {
int i; int i;
int j; int j;
int offset = 0; int offset = 0;
int n = to.length(); int length = to.length();
char[] circle = new char[n]; char[] circle = new char[length];
/* /*
* First fill the circle buffer with as many characters as are in the * First fill the circle buffer with as many characters as are in the
* to string. If we reach an early end, bail. * to string. If we reach an early end, bail.
*/ */
for (i = 0; i < n; i += 1) { for (i = 0; i < length; i += 1) {
c = next(); c = next();
if (c == 0) { if (c == 0) {
return false; return false;
@ -328,14 +328,14 @@ public class XMLTokener extends JSONTokener {
/* /*
* Compare the circle buffer with the to string. * Compare the circle buffer with the to string.
*/ */
for (i = 0; i < n; i += 1) { for (i = 0; i < length; i += 1) {
if (circle[j] != to.charAt(i)) { if (circle[j] != to.charAt(i)) {
b = false; b = false;
break; break;
} }
j += 1; j += 1;
if (j >= n) { if (j >= length) {
j -= n; j -= length;
} }
} }
/* /*
@ -357,8 +357,8 @@ public class XMLTokener extends JSONTokener {
*/ */
circle[offset] = c; circle[offset] = c;
offset += 1; offset += 1;
if (offset >= n) { if (offset >= length) {
offset -= n; offset -= length;
} }
} }
} }