diff --git a/CDL.java b/CDL.java index d76dd68..a6b1787 100755 --- a/CDL.java +++ b/CDL.java @@ -41,7 +41,7 @@ SOFTWARE. * The names for the elements in the JSONObjects can be taken from the names * in the first row. * @author JSON.org - * @version 2009-09-11 + * @version 2010-12-24 */ 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, * using the first row as a source of names. * @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 * first row will be a list of names obtained by inspecting the first diff --git a/Cookie.java b/Cookie.java index 85b992e..9cf5ce2 100755 --- a/Cookie.java +++ b/Cookie.java @@ -28,7 +28,7 @@ SOFTWARE. * Convert a web browser cookie specification to a JSONObject and back. * JSON and Cookies are both notations for name/value pairs. * @author JSON.org - * @version 2008-09-18 + * @version 2010-12-24 */ public class Cookie { @@ -48,8 +48,8 @@ public class Cookie { char c; String s = string.trim(); StringBuffer sb = new StringBuffer(); - int len = s.length(); - for (int i = 0; i < len; i += 1) { + int length = s.length(); + for (int i = 0; i < length; i += 1) { c = s.charAt(i); if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') { sb.append('%'); @@ -79,29 +79,29 @@ public class Cookie { * @throws JSONException */ public static JSONObject toJSONObject(String string) throws JSONException { - String n; - JSONObject o = new JSONObject(); - Object v; + String name; + JSONObject jo = new JSONObject(); + Object value; JSONTokener x = new JSONTokener(string); - o.put("name", x.nextTo('=')); + jo.put("name", x.nextTo('=')); x.next('='); - o.put("value", x.nextTo(';')); + jo.put("value", x.nextTo(';')); x.next(); while (x.more()) { - n = unescape(x.nextTo("=;")); + name = unescape(x.nextTo("=;")); if (x.next() != '=') { - if (n.equals("secure")) { - v = Boolean.TRUE; + if (name.equals("secure")) { + value = Boolean.TRUE; } else { throw x.syntaxError("Missing '=' in cookie parameter."); } } else { - v = unescape(x.nextTo(';')); + value = unescape(x.nextTo(';')); 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" * members, they will be appended to the cookie specification string. * All other members are ignored. - * @param o A JSONObject + * @param jo A JSONObject * @return A cookie specification string * @throws JSONException */ - public static String toString(JSONObject o) throws JSONException { + public static String toString(JSONObject jo) throws JSONException { StringBuffer sb = new StringBuffer(); - sb.append(escape(o.getString("name"))); + sb.append(escape(jo.getString("name"))); sb.append("="); - sb.append(escape(o.getString("value"))); - if (o.has("expires")) { + sb.append(escape(jo.getString("value"))); + if (jo.has("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(escape(o.getString("domain"))); + sb.append(escape(jo.getString("domain"))); } - if (o.has("path")) { + if (jo.has("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"); } return sb.toString(); @@ -142,28 +142,28 @@ public class Cookie { /** * Convert %hh sequences to single characters, and * convert plus to space. - * @param s A string that may contain + * @param string A string that may contain * + (plus) and * %hh sequences. * @return The unescaped string. */ - public static String unescape(String s) { - int len = s.length(); - StringBuffer b = new StringBuffer(); - for (int i = 0; i < len; ++i) { - char c = s.charAt(i); + public static String unescape(String string) { + int length = string.length(); + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < length; ++i) { + char c = string.charAt(i); if (c == '+') { c = ' '; - } else if (c == '%' && i + 2 < len) { - int d = JSONTokener.dehexchar(s.charAt(i + 1)); - int e = JSONTokener.dehexchar(s.charAt(i + 2)); + } else if (c == '%' && i + 2 < length) { + int d = JSONTokener.dehexchar(string.charAt(i + 1)); + int e = JSONTokener.dehexchar(string.charAt(i + 2)); if (d >= 0 && e >= 0) { c = (char)(d * 16 + e); i += 2; } } - b.append(c); + sb.append(c); } - return b.toString(); + return sb.toString(); } } diff --git a/CookieList.java b/CookieList.java index 8f651f5..7f4fe07 100755 --- a/CookieList.java +++ b/CookieList.java @@ -29,7 +29,7 @@ import java.util.Iterator; /** * Convert a web browser cookie list string to a JSONObject and back. * @author JSON.org - * @version 2008-09-18 + * @version 2010-12-24 */ public class CookieList { @@ -47,15 +47,15 @@ public class CookieList { * @throws JSONException */ public static JSONObject toJSONObject(String string) throws JSONException { - JSONObject o = new JSONObject(); + JSONObject jo = new JSONObject(); JSONTokener x = new JSONTokener(string); while (x.more()) { String name = Cookie.unescape(x.nextTo('=')); x.next('='); - o.put(name, Cookie.unescape(x.nextTo(';'))); + jo.put(name, Cookie.unescape(x.nextTo(';'))); 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 '='. * The pairs are separated by ';'. The characters '%', '+', '=', and ';' * in the names and values are replaced by "%hh". - * @param o A JSONObject + * @param jo A JSONObject * @return A cookie list string * @throws JSONException */ - public static String toString(JSONObject o) throws JSONException { + public static String toString(JSONObject jo) throws JSONException { boolean b = false; - Iterator keys = o.keys(); - String s; + Iterator keys = jo.keys(); + String string; StringBuffer sb = new StringBuffer(); while (keys.hasNext()) { - s = keys.next().toString(); - if (!o.isNull(s)) { + string = keys.next().toString(); + if (!jo.isNull(string)) { if (b) { sb.append(';'); } - sb.append(Cookie.escape(s)); + sb.append(Cookie.escape(string)); sb.append("="); - sb.append(Cookie.escape(o.getString(s))); + sb.append(Cookie.escape(jo.getString(string))); b = true; } } diff --git a/HTTP.java b/HTTP.java index 6624708..0ce7a21 100755 --- a/HTTP.java +++ b/HTTP.java @@ -29,7 +29,7 @@ import java.util.Iterator; /** * Convert an HTTP header to a JSONObject and back. * @author JSON.org - * @version 2008-09-18 + * @version 2010-12-24 */ public class HTTP { @@ -69,27 +69,27 @@ public class HTTP { * @throws JSONException */ public static JSONObject toJSONObject(String string) throws JSONException { - JSONObject o = new JSONObject(); + JSONObject jo = new JSONObject(); HTTPTokener x = new HTTPTokener(string); - String t; + String token; - t = x.nextToken(); - if (t.toUpperCase().startsWith("HTTP")) { + token = x.nextToken(); + if (token.toUpperCase().startsWith("HTTP")) { // Response - o.put("HTTP-Version", t); - o.put("Status-Code", x.nextToken()); - o.put("Reason-Phrase", x.nextTo('\0')); + jo.put("HTTP-Version", token); + jo.put("Status-Code", x.nextToken()); + jo.put("Reason-Phrase", x.nextTo('\0')); x.next(); } else { // Request - o.put("Method", t); - o.put("Request-URI", x.nextToken()); - o.put("HTTP-Version", x.nextToken()); + jo.put("Method", token); + jo.put("Request-URI", x.nextToken()); + jo.put("HTTP-Version", x.nextToken()); } // Fields @@ -97,10 +97,10 @@ public class HTTP { while (x.more()) { String name = x.nextTo(':'); x.next(':'); - o.put(name, x.nextTo('\0')); + jo.put(name, x.nextTo('\0')); x.next(); } - return o; + return jo; } @@ -119,41 +119,41 @@ public class HTTP { * } * Any other members of the JSONObject will be output as HTTP fields. * The result will end with two CRLF pairs. - * @param o A JSONObject + * @param jo A JSONObject * @return An HTTP header string. * @throws JSONException if the object does not contain enough * information. */ - public static String toString(JSONObject o) throws JSONException { - Iterator keys = o.keys(); - String s; + public static String toString(JSONObject jo) throws JSONException { + Iterator keys = jo.keys(); + String string; StringBuffer sb = new StringBuffer(); - if (o.has("Status-Code") && o.has("Reason-Phrase")) { - sb.append(o.getString("HTTP-Version")); + if (jo.has("Status-Code") && jo.has("Reason-Phrase")) { + sb.append(jo.getString("HTTP-Version")); sb.append(' '); - sb.append(o.getString("Status-Code")); + sb.append(jo.getString("Status-Code")); sb.append(' '); - sb.append(o.getString("Reason-Phrase")); - } else if (o.has("Method") && o.has("Request-URI")) { - sb.append(o.getString("Method")); + sb.append(jo.getString("Reason-Phrase")); + } else if (jo.has("Method") && jo.has("Request-URI")) { + sb.append(jo.getString("Method")); sb.append(' '); sb.append('"'); - sb.append(o.getString("Request-URI")); + sb.append(jo.getString("Request-URI")); sb.append('"'); sb.append(' '); - sb.append(o.getString("HTTP-Version")); + sb.append(jo.getString("HTTP-Version")); } else { throw new JSONException("Not enough material for an HTTP header."); } sb.append(CRLF); while (keys.hasNext()) { - s = keys.next().toString(); - if (!s.equals("HTTP-Version") && !s.equals("Status-Code") && - !s.equals("Reason-Phrase") && !s.equals("Method") && - !s.equals("Request-URI") && !o.isNull(s)) { - sb.append(s); + string = keys.next().toString(); + if (!string.equals("HTTP-Version") && !string.equals("Status-Code") && + !string.equals("Reason-Phrase") && !string.equals("Method") && + !string.equals("Request-URI") && !jo.isNull(string)) { + sb.append(string); sb.append(": "); - sb.append(o.getString(s)); + sb.append(jo.getString(string)); sb.append(CRLF); } } diff --git a/HTTPTokener.java b/HTTPTokener.java index 410a77c..f62b3d5 100755 --- a/HTTPTokener.java +++ b/HTTPTokener.java @@ -28,16 +28,16 @@ SOFTWARE. * The HTTPTokener extends the JSONTokener to provide additional methods * for the parsing of HTTP headers. * @author JSON.org - * @version 2008-09-18 + * @version 2010-12-24 */ public class HTTPTokener extends JSONTokener { /** * Construct an HTTPTokener from a string. - * @param s A source string. + * @param string A source string. */ - public HTTPTokener(String s) { - super(s); + public HTTPTokener(String string) { + super(string); } diff --git a/JSONArray.java b/JSONArray.java index 091161a..5d8841f 100755 --- a/JSONArray.java +++ b/JSONArray.java @@ -78,7 +78,7 @@ import java.util.Map; * * @author JSON.org - * @version 2009-04-14 + * @version 2010-12-24 */ public class JSONArray { @@ -103,45 +103,33 @@ public class JSONArray { */ public JSONArray(JSONTokener x) throws JSONException { this(); - char c = x.nextClean(); - char q; - if (c == '[') { - q = ']'; - } else if (c == '(') { - q = ')'; - } else { + if (x.nextClean() != '[') { throw x.syntaxError("A JSONArray text must start with '['"); } - if (x.nextClean() == ']') { - return; - } - x.back(); - for (;;) { - if (x.nextClean() == ',') { - x.back(); - this.myArrayList.add(null); - } else { - x.back(); - this.myArrayList.add(x.nextValue()); - } - c = x.nextClean(); - switch (c) { - case ';': - case ',': - if (x.nextClean() == ']') { - return; - } - x.back(); - break; - case ']': - case ')': - if (q != c) { - throw x.syntaxError("Expected a '" + new Character(q) + "'"); - } - return; - default: - throw x.syntaxError("Expected a ',' or ']'"); - } + if (x.nextClean() != ']') { + x.back(); + for (;;) { + if (x.nextClean() == ',') { + x.back(); + this.myArrayList.add(JSONObject.NULL); + } else { + x.back(); + this.myArrayList.add(x.nextValue()); + } + switch (x.nextClean()) { + case ';': + case ',': + if (x.nextClean() == ']') { + return; + } + x.back(); + break; + case ']': + return; + default: + throw x.syntaxError("Expected a ',' or ']'"); + } + } } } @@ -167,8 +155,7 @@ public class JSONArray { if (collection != null) { Iterator iter = collection.iterator(); while (iter.hasNext()) { - Object o = iter.next(); - this.myArrayList.add(JSONObject.wrap(o)); + this.myArrayList.add(JSONObject.wrap(iter.next())); } } } @@ -200,11 +187,11 @@ public class JSONArray { * @throws JSONException If there is no value for the index. */ public Object get(int index) throws JSONException { - Object o = opt(index); - if (o == null) { + Object object = opt(index); + if (object == null) { 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. * @return The truth. * @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 { - Object o = get(index); - if (o.equals(Boolean.FALSE) || - (o instanceof String && - ((String)o).equalsIgnoreCase("false"))) { + Object object = get(index); + if (object.equals(Boolean.FALSE) || + (object instanceof String && + ((String)object).equalsIgnoreCase("false"))) { return false; - } else if (o.equals(Boolean.TRUE) || - (o instanceof String && - ((String)o).equalsIgnoreCase("true"))) { + } else if (object.equals(Boolean.TRUE) || + (object instanceof String && + ((String)object).equalsIgnoreCase("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. */ public double getDouble(int index) throws JSONException { - Object o = get(index); + Object object = get(index); try { - return o instanceof Number ? - ((Number)o).doubleValue() : - Double.valueOf((String)o).doubleValue(); + return object instanceof Number ? + ((Number)object).doubleValue() : + Double.parseDouble((String)object); } catch (Exception e) { throw new JSONException("JSONArray[" + index + "] is not a number."); @@ -258,14 +245,18 @@ public class JSONArray { * * @param index The index must be between 0 and length() - 1. * @return The value. - * @throws JSONException If the key is not found or if the value cannot - * be converted to a number. - * if the value cannot be converted to a number. + * @throws JSONException If the key is not found or if the value is not a number. */ public int getInt(int index) throws JSONException { - Object o = get(index); - return o instanceof Number ? - ((Number)o).intValue() : (int)getDouble(index); + Object object = get(index); + try { + 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 */ public JSONArray getJSONArray(int index) throws JSONException { - Object o = get(index); - if (o instanceof JSONArray) { - return (JSONArray)o; + Object object = get(index); + if (object instanceof JSONArray) { + return (JSONArray)object; } throw new JSONException("JSONArray[" + index + "] is not a JSONArray."); @@ -294,9 +285,9 @@ public class JSONArray { * value is not a JSONObject */ public JSONObject getJSONObject(int index) throws JSONException { - Object o = get(index); - if (o instanceof JSONObject) { - return (JSONObject)o; + Object object = get(index); + if (object instanceof JSONObject) { + return (JSONObject)object; } throw new JSONException("JSONArray[" + index + "] is not a JSONObject."); @@ -312,9 +303,15 @@ public class JSONArray { * be converted to a number. */ public long getLong(int index) throws JSONException { - Object o = get(index); - return o instanceof Number ? - ((Number)o).longValue() : (long)getDouble(index); + Object object = get(index); + try { + 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. */ public String optString(int index, String defaultValue) { - Object o = opt(index); - return o != null ? o.toString() : defaultValue; + Object object = opt(index); + return object != null ? object.toString() : defaultValue; } diff --git a/JSONException.java b/JSONException.java index 45e3b8d..b498d4b 100755 --- a/JSONException.java +++ b/JSONException.java @@ -3,12 +3,9 @@ package org.json; /** * The JSONException is thrown by the JSON.org classes when things are amiss. * @author JSON.org - * @version 2008-09-18 + * @version 2010-12-24 */ public class JSONException extends Exception { - /** - * - */ private static final long serialVersionUID = 0; private Throwable cause; @@ -20,9 +17,9 @@ public class JSONException extends Exception { super(message); } - public JSONException(Throwable t) { - super(t.getMessage()); - this.cause = t; + public JSONException(Throwable cause) { + super(cause.getMessage()); + this.cause = cause; } public Throwable getCause() { diff --git a/JSONML.java b/JSONML.java index 440e4ac..95c284f 100755 --- a/JSONML.java +++ b/JSONML.java @@ -154,7 +154,7 @@ public class JSONML { break; } -// attribute = value +// attribute = value attribute = (String)token; if (!arrayForm && (attribute == "tagName" || attribute == "childNode")) { @@ -259,7 +259,6 @@ public class JSONML { return (JSONArray)parse(x, true, null); } - /** * 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 { return (JSONObject)parse(x, false, null); } + + /** * Convert a well-formed (but not necessarily valid) XML string into a * JSONObject using the JsonML transform. Each XML tag is represented as @@ -302,15 +303,15 @@ public class JSONML { * @throws JSONException */ public static String toString(JSONArray ja) throws JSONException { - Object e; int i; JSONObject jo; - String k; + String key; Iterator keys; int length; + Object object; StringBuffer sb = new StringBuffer(); String tagName; - String v; + String value; // Emit '); do { - e = ja.get(i); + object = ja.get(i); i += 1; - if (e != null) { - if (e instanceof String) { - sb.append(XML.escape(e.toString())); - } else if (e instanceof JSONObject) { - sb.append(toString((JSONObject)e)); - } else if (e instanceof JSONArray) { - sb.append(toString((JSONArray)e)); + if (object != null) { + if (object instanceof String) { + sb.append(XML.escape(object.toString())); + } else if (object instanceof JSONObject) { + sb.append(toString((JSONObject)object)); + } else if (object instanceof JSONArray) { + sb.append(toString((JSONArray)object)); } } } while (i < length); @@ -385,14 +386,14 @@ public class JSONML { */ public static String toString(JSONObject jo) throws JSONException { StringBuffer sb = new StringBuffer(); - Object e; int i; JSONArray ja; - String k; + String key; Iterator keys; - int len; + int length; + Object object; String tagName; - String v; + String value; //Emit '); } else { sb.append('>'); - len = ja.length(); - for (i = 0; i < len; i += 1) { - e = ja.get(i); - if (e != null) { - if (e instanceof String) { - sb.append(XML.escape(e.toString())); - } else if (e instanceof JSONObject) { - sb.append(toString((JSONObject)e)); - } else if (e instanceof JSONArray) { - sb.append(toString((JSONArray)e)); + length = ja.length(); + for (i = 0; i < length; i += 1) { + object = ja.get(i); + if (object != null) { + if (object instanceof String) { + sb.append(XML.escape(object.toString())); + } else if (object instanceof JSONObject) { + sb.append(toString((JSONObject)object)); + } else if (object instanceof JSONArray) { + sb.append(toString((JSONArray)object)); } } } diff --git a/JSONObject.java b/JSONObject.java index da4fb21..0c57050 100755 --- a/JSONObject.java +++ b/JSONObject.java @@ -59,10 +59,11 @@ import java.util.TreeSet; * The generic get() and opt() methods return an * object, which you can cast or query for type. There are also typed * get and opt 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. *

- * The put methods adds values to an object. For example,

- *     myString = new JSONObject().put("JSON", "Hello, World!").toString();
+ * The put methods add or replace values in an object. For example, + *
myString = new JSONObject().put("JSON", "Hello, World!").toString();
* produces the string {"JSON": "Hello, World"}. *

* The texts produced by the toString methods strictly conform to @@ -86,7 +87,7 @@ import java.util.TreeSet; *

  • Numbers may have the 0x- (hex) prefix.
  • * * @author JSON.org - * @version 2010-12-20 + * @version 2010-12-24 */ public class JSONObject { @@ -106,7 +107,6 @@ public class JSONObject { return this; } - /** * A Null object is equal to the null value and to itself. * @param object An object to test for nullness. @@ -117,7 +117,6 @@ public class JSONObject { return object == null || object == this; } - /** * Get the "null" string value. * @return The string "null". @@ -368,15 +367,14 @@ public class JSONObject { public JSONObject accumulate(String key, Object value) throws JSONException { testValidity(value); - Object o = opt(key); - if (o == null) { + Object object = opt(key); + if (object == null) { put(key, value instanceof JSONArray ? - new JSONArray().put(value) : - value); - } else if (o instanceof JSONArray) { - ((JSONArray)o).put(value); + new JSONArray().put(value) : value); + } else if (object instanceof JSONArray) { + ((JSONArray)object).put(value); } else { - put(key, new JSONArray().put(o).put(value)); + put(key, new JSONArray().put(object).put(value)); } return this; } @@ -395,11 +393,11 @@ public class JSONObject { */ public JSONObject append(String key, Object value) throws JSONException { testValidity(value); - Object o = opt(key); - if (o == null) { + Object object = opt(key); + if (object == null) { put(key, new JSONArray().put(value)); - } else if (o instanceof JSONArray) { - put(key, ((JSONArray)o).put(value)); + } else if (object instanceof JSONArray) { + put(key, ((JSONArray)object).put(value)); } else { throw new JSONException("JSONObject[" + key + "] is not a JSONArray."); @@ -421,16 +419,17 @@ public class JSONObject { // Shave off trailing zeros and decimal point, if possible. - String s = Double.toString(d); - if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) { - while (s.endsWith("0")) { - s = s.substring(0, s.length() - 1); + String string = Double.toString(d); + if (string.indexOf('.') > 0 && string.indexOf('e') < 0 && + string.indexOf('E') < 0) { + while (string.endsWith("0")) { + string = string.substring(0, string.length() - 1); } - if (s.endsWith(".")) { - s = s.substring(0, s.length() - 1); + if (string.endsWith(".")) { + string = string.substring(0, string.length() - 1); } } - return s; + return string; } @@ -439,15 +438,18 @@ public class JSONObject { * * @param key A key string. * @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 { - Object o = opt(key); - if (o == null) { + if (key == null) { + throw new JSONException("Null key."); + } + Object object = opt(key); + if (object == null) { throw new JSONException("JSONObject[" + quote(key) + "] not found."); } - return o; + return object; } @@ -456,18 +458,18 @@ public class JSONObject { * * @param key A key string. * @return The truth. - * @throws JSONException + * @throws JSONException * if the value is not a Boolean or the String "true" or "false". */ public boolean getBoolean(String key) throws JSONException { - Object o = get(key); - if (o.equals(Boolean.FALSE) || - (o instanceof String && - ((String)o).equalsIgnoreCase("false"))) { + Object object = get(key); + if (object.equals(Boolean.FALSE) || + (object instanceof String && + ((String)object).equalsIgnoreCase("false"))) { return false; - } else if (o.equals(Boolean.TRUE) || - (o instanceof String && - ((String)o).equalsIgnoreCase("true"))) { + } else if (object.equals(Boolean.TRUE) || + (object instanceof String && + ((String)object).equalsIgnoreCase("true"))) { return true; } 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. */ public double getDouble(String key) throws JSONException { - Object o = get(key); + Object object = get(key); try { - return o instanceof Number ? - ((Number)o).doubleValue() : - Double.valueOf((String)o).doubleValue(); + return object instanceof Number ? + ((Number)object).doubleValue() : + Double.parseDouble((String)object); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a number."); @@ -504,11 +506,11 @@ public class JSONObject { * be converted to an integer. */ public int getInt(String key) throws JSONException { - Object o = get(key); + Object object = get(key); try { - return o instanceof Number ? - ((Number)o).intValue() : - Integer.parseInt((String)o); + return object instanceof Number ? + ((Number)object).intValue() : + Integer.parseInt((String)object); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not an int."); @@ -521,13 +523,13 @@ public class JSONObject { * * @param key A key string. * @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. */ public JSONArray getJSONArray(String key) throws JSONException { - Object o = get(key); - if (o instanceof JSONArray) { - return (JSONArray)o; + Object object = get(key); + if (object instanceof JSONArray) { + return (JSONArray)object; } throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONArray."); @@ -539,13 +541,13 @@ public class JSONObject { * * @param key A key string. * @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. */ public JSONObject getJSONObject(String key) throws JSONException { - Object o = get(key); - if (o instanceof JSONObject) { - return (JSONObject)o; + Object object = get(key); + if (object instanceof JSONObject) { + return (JSONObject)object; } throw new JSONException("JSONObject[" + quote(key) + "] is not a JSONObject."); @@ -561,11 +563,11 @@ public class JSONObject { * be converted to a long. */ public long getLong(String key) throws JSONException { - Object o = get(key); + Object object = get(key); try { - return o instanceof Number ? - ((Number)o).longValue() : - Long.parseLong((String)o); + return object instanceof Number ? + ((Number)object).longValue() : + Long.parseLong((String)object); } catch (Exception e) { throw new JSONException("JSONObject[" + quote(key) + "] is not a long."); @@ -583,12 +585,12 @@ public class JSONObject { if (length == 0) { return null; } - Iterator i = jo.keys(); + Iterator iterator = jo.keys(); String[] names = new String[length]; - int j = 0; - while (i.hasNext()) { - names[j] = (String)i.next(); - j += 1; + int i = 0; + while (iterator.hasNext()) { + names[i] = (String)iterator.next(); + i += 1; } return names; } @@ -652,18 +654,16 @@ public class JSONObject { Object value = opt(key); if (value == null) { 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 { - 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 { - throw new JSONException("Unable to increment [" + key + "]."); - } + throw new JSONException("Unable to increment [" + quote(key) + "]."); } return this; } @@ -718,29 +718,30 @@ public class JSONObject { /** * Produce a string from a Number. - * @param n A Number + * @param number A Number * @return A String. * @throws JSONException If n is a non-finite number. */ - public static String numberToString(Number n) + public static String numberToString(Number number) throws JSONException { - if (n == null) { + if (number == null) { throw new JSONException("Null pointer"); } - testValidity(n); + testValidity(number); // Shave off trailing zeros and decimal point, if possible. - String s = n.toString(); - if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0) { - while (s.endsWith("0")) { - s = s.substring(0, s.length() - 1); + String string = number.toString(); + if (string.indexOf('.') > 0 && string.indexOf('e') < 0 && + string.indexOf('E') < 0) { + while (string.endsWith("0")) { + string = string.substring(0, string.length() - 1); } - if (s.endsWith(".")) { - s = s.substring(0, s.length() - 1); + if (string.endsWith(".")) { + 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) { try { - Object o = opt(key); - return o instanceof Number ? ((Number)o).doubleValue() : - new Double((String)o).doubleValue(); + return getDouble(key); } catch (Exception e) { return defaultValue; } @@ -876,8 +875,8 @@ public class JSONObject { * @return A JSONObject which is the value. */ public JSONObject optJSONObject(String key) { - Object o = opt(key); - return o instanceof JSONObject ? (JSONObject)o : null; + Object object = opt(key); + 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 * a number. * - * @param key A key string. - * @param defaultValue The default. - * @return An object which is the value. + * @param key A key string. + * @param defaultValue The default. + * @return An object which is the value. */ public long optLong(String key, long defaultValue) { try { @@ -936,8 +935,8 @@ public class JSONObject { * @return A string which is the value. */ public String optString(String key, String defaultValue) { - Object o = opt(key); - return NULL.equals(o) ? defaultValue : o.toString(); + Object object = opt(key); + return NULL.equals(object) ? defaultValue : object.toString(); } @@ -1150,10 +1149,10 @@ public class JSONObject { char b; char c = 0; + String hhhh; int i; int len = string.length(); StringBuffer sb = new StringBuffer(len + 4); - String t; sb.append('"'); for (i = 0; i < len; i += 1) { @@ -1189,8 +1188,8 @@ public class JSONObject { default: if (c < ' ' || (c >= '\u0080' && c < '\u00a0') || (c >= '\u2000' && c < '\u2100')) { - t = "000" + Integer.toHexString(c); - sb.append("\\u" + t.substring(t.length() - 4)); + hhhh = "000" + Integer.toHexString(c); + sb.append("\\u" + hhhh.substring(hhhh.length() - 4)); } else { sb.append(c); } @@ -1223,20 +1222,20 @@ public class JSONObject { /** * Try to convert a string into a number, boolean, or null. If the string * can't be converted, return the string. - * @param s A String. + * @param string A String. * @return A simple JSON value. */ - public static Object stringToValue(String s) { - if (s.equals("")) { - return s; + public static Object stringToValue(String string) { + if (string.equals("")) { + return string; } - if (s.equalsIgnoreCase("true")) { + if (string.equalsIgnoreCase("true")) { return Boolean.TRUE; } - if (s.equalsIgnoreCase("false")) { + if (string.equalsIgnoreCase("false")) { return Boolean.FALSE; } - if (s.equalsIgnoreCase("null")) { + if (string.equalsIgnoreCase("null")) { return JSONObject.NULL; } @@ -1249,21 +1248,21 @@ public class JSONObject { * 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' && s.length() > 2 && - (s.charAt(1) == 'x' || s.charAt(1) == 'X')) { + if (b == '0' && string.length() > 2 && + (string.charAt(1) == 'x' || string.charAt(1) == 'X')) { try { - return new Integer(Integer.parseInt(s.substring(2), 16)); + return new Integer(Integer.parseInt(string.substring(2), 16)); } catch (Exception ignore) { } } try { - if (s.indexOf('.') > -1 || - s.indexOf('e') > -1 || s.indexOf('E') > -1) { - return Double.valueOf(s); + if (string.indexOf('.') > -1 || + string.indexOf('e') > -1 || string.indexOf('E') > -1) { + return Double.valueOf(string); } else { - Long myLong = new Long(s); + Long myLong = new Long(string); if (myLong.longValue() == myLong.intValue()) { return new Integer(myLong.intValue()); } else { @@ -1273,12 +1272,12 @@ public class JSONObject { } 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. * @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. */ String toString(int indentFactor, int indent) throws JSONException { - int j; - int n = length(); - if (n == 0) { + int i; + int length = this.length(); + if (length == 0) { return "{}"; } Iterator keys = sortedKeys(); - StringBuffer sb = new StringBuffer("{"); int newindent = indent + indentFactor; - Object o; - if (n == 1) { - o = keys.next(); - sb.append(quote(o.toString())); + Object object; + StringBuffer sb = new StringBuffer("{"); + if (length == 1) { + object = keys.next(); + sb.append(quote(object.toString())); sb.append(": "); - sb.append(valueToString(this.map.get(o), indentFactor, + sb.append(valueToString(this.map.get(object), indentFactor, indent)); } else { while (keys.hasNext()) { - o = keys.next(); + object = keys.next(); if (sb.length() > 1) { sb.append(",\n"); } else { sb.append('\n'); } - for (j = 0; j < newindent; j += 1) { + for (i = 0; i < newindent; i += 1) { sb.append(' '); } - sb.append(quote(o.toString())); + sb.append(quote(object.toString())); sb.append(": "); - sb.append(valueToString(this.map.get(o), indentFactor, + sb.append(valueToString(this.map.get(object), indentFactor, newindent)); } if (sb.length() > 1) { sb.append('\n'); - for (j = 0; j < indent; j += 1) { + for (i = 0; i < indent; i += 1) { sb.append(' '); } } @@ -1452,16 +1451,16 @@ public class JSONObject { return "null"; } if (value instanceof JSONString) { - Object o; + Object object; try { - o = ((JSONString)value).toJSONString(); + object = ((JSONString)value).toJSONString(); } catch (Exception e) { throw new JSONException(e); } - if (o instanceof String) { - return (String)o; + if (object instanceof String) { + return (String)object; } - throw new JSONException("Bad value from toJSONString: " + o); + throw new JSONException("Bad value from toJSONString: " + object); } if (value instanceof Number) { return numberToString((Number) value); @@ -1553,12 +1552,12 @@ public class JSONObject { if (object == null) { return NULL; } - if (object instanceof JSONObject || object instanceof JSONArray || + if (object instanceof JSONObject || object instanceof JSONArray || NULL.equals(object) || object instanceof JSONString || - object instanceof Byte || object instanceof Character || - object instanceof Short || object instanceof Integer || - object instanceof Long || object instanceof Boolean || - object instanceof Float || object instanceof Double || + object instanceof Byte || object instanceof Character || + object instanceof Short || object instanceof Integer || + object instanceof Long || object instanceof Boolean || + object instanceof Float || object instanceof Double || object instanceof String) { return object; } @@ -1597,26 +1596,26 @@ public class JSONObject { */ public Writer write(Writer writer) throws JSONException { try { - boolean b = false; + boolean commanate = false; Iterator keys = keys(); writer.write('{'); while (keys.hasNext()) { - if (b) { + if (commanate) { writer.write(','); } - Object k = keys.next(); - writer.write(quote(k.toString())); + Object key = keys.next(); + writer.write(quote(key.toString())); writer.write(':'); - Object v = this.map.get(k); - if (v instanceof JSONObject) { - ((JSONObject)v).write(writer); - } else if (v instanceof JSONArray) { - ((JSONArray)v).write(writer); + Object value = this.map.get(key); + if (value instanceof JSONObject) { + ((JSONObject)value).write(writer); + } else if (value instanceof JSONArray) { + ((JSONArray)value).write(writer); } else { - writer.write(valueToString(v)); + writer.write(valueToString(value)); } - b = true; + commanate = true; } writer.write('}'); return writer; diff --git a/JSONTokener.java b/JSONTokener.java index a6c5849..f7d8bdf 100755 --- a/JSONTokener.java +++ b/JSONTokener.java @@ -36,7 +36,7 @@ SOFTWARE. * it. It is used by the JSONObject and JSONArray constructors to parse * JSON source strings. * @author JSON.org - * @version 2010-12-20 + * @version 2010-12-24 */ public class JSONTokener { @@ -207,17 +207,17 @@ public class JSONTokener { return ""; } - char[] buffer = new char[n]; + char[] chars = new char[n]; int pos = 0; while (pos < n) { - buffer[pos] = next(); + chars[pos] = next(); if (end()) { throw syntaxError("Substring bounds error"); } 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 * end of line, whichever comes first. - * @param d A delimiter character. + * @param delimiter A delimiter character. * @return A string. */ - public String nextTo(char d) throws JSONException { + public String nextTo(char delimiter) throws JSONException { StringBuffer sb = new StringBuffer(); for (;;) { char c = next(); - if (c == d || c == 0 || c == '\n' || c == '\r') { + if (c == delimiter || c == 0 || c == '\n' || c == '\r') { if (c != 0) { back(); } @@ -351,7 +351,7 @@ public class JSONTokener { */ public Object nextValue() throws JSONException { char c = nextClean(); - String s; + String string; switch (c) { case '"': @@ -361,7 +361,6 @@ public class JSONTokener { back(); return new JSONObject(this); case '[': - case '(': back(); return new JSONArray(this); } @@ -382,11 +381,11 @@ public class JSONTokener { } back(); - s = sb.toString().trim(); - if (s.equals("")) { + string = sb.toString().trim(); + if (string.equals("")) { 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}]" */ public String toString() { - return " at " + index + " [character " + this.character + " line " + this.line + "]"; + return " at " + index + " [character " + this.character + " line " + + this.line + "]"; } } \ No newline at end of file diff --git a/JSONWriter.java b/JSONWriter.java index 3622a5b..b0d7d41 100755 --- a/JSONWriter.java +++ b/JSONWriter.java @@ -54,7 +54,7 @@ SOFTWARE. *

    * This can sometimes be easier than using a JSONObject to build a string. * @author JSON.org - * @version 2010-03-11 + * @version 2010-12-24 */ public class JSONWriter { private static final int maxdepth = 20; @@ -103,12 +103,12 @@ public class JSONWriter { /** * Append a value. - * @param s A string value. + * @param string A string value. * @return this * @throws JSONException If the value is out of sequence. */ - private JSONWriter append(String s) throws JSONException { - if (s == null) { + private JSONWriter append(String string) throws JSONException { + if (string == null) { throw new JSONException("Null pointer"); } if (this.mode == 'o' || this.mode == 'a') { @@ -116,7 +116,7 @@ public class JSONWriter { if (this.comma && this.mode == 'a') { this.writer.write(','); } - this.writer.write(s); + this.writer.write(string); } catch (IOException e) { throw new JSONException(e); } @@ -150,17 +150,17 @@ public class JSONWriter { /** * End something. - * @param m Mode + * @param mode Mode * @param c Closing character * @return this * @throws JSONException If unbalanced. */ - private JSONWriter end(char m, char c) throws JSONException { - if (this.mode != m) { - throw new JSONException(m == 'a' ? "Misplaced endArray." : + private JSONWriter end(char mode, char c) throws JSONException { + if (this.mode != mode) { + throw new JSONException(mode == 'a' ? "Misplaced endArray." : "Misplaced endObject."); } - this.pop(m); + this.pop(mode); try { this.writer.write(c); } catch (IOException e) { @@ -193,22 +193,22 @@ public class JSONWriter { /** * Append a key. The key will be associated with the next value. In an * object, every value must be preceded by a key. - * @param s A key string. + * @param string A key string. * @return this * @throws JSONException If the key is out of place. For example, keys * do not belong in arrays or if the key is null. */ - public JSONWriter key(String s) throws JSONException { - if (s == null) { + public JSONWriter key(String string) throws JSONException { + if (string == null) { throw new JSONException("Null key."); } if (this.mode == 'k') { try { - stack[top - 1].putOnce(s, Boolean.TRUE); + stack[top - 1].putOnce(string, Boolean.TRUE); if (this.comma) { this.writer.write(','); } - this.writer.write(JSONObject.quote(s)); + this.writer.write(JSONObject.quote(string)); this.writer.write(':'); this.comma = false; this.mode = 'o'; @@ -259,7 +259,8 @@ public class JSONWriter { throw new JSONException("Nesting error."); } 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. - * @param o The object to append. It can be null, or a Boolean, Number, - * String, JSONObject, or JSONArray, or an object with a toJSONString() - * method. + * @param object The object to append. It can be null, or a Boolean, Number, + * String, JSONObject, or JSONArray, or an object that implements JSONString. * @return this * @throws JSONException If the value is out of sequence. */ - public JSONWriter value(Object o) throws JSONException { - return this.append(JSONObject.valueToString(o)); + public JSONWriter value(Object object) throws JSONException { + return this.append(JSONObject.valueToString(object)); } } diff --git a/XML.java b/XML.java index 6d9f78e..59d20d0 100755 --- a/XML.java +++ b/XML.java @@ -31,7 +31,7 @@ import java.util.Iterator; * This provides static methods to convert an XML text into a JSONObject, * and to covert a JSONObject into an XML text. * @author JSON.org - * @version 2010-12-23 + * @version 2010-12-24 */ public class XML { @@ -75,7 +75,7 @@ public class XML { */ public static String escape(String string) { 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); switch (c) { case '&': @@ -128,10 +128,10 @@ public class XML { String name) throws JSONException { char c; int i; - String n; - JSONObject o = null; - String s; - Object t; + JSONObject jsonobject = null; + String string; + String tagName; + Object token; // Test for and skip past these forms: // @@ -143,11 +143,11 @@ public class XML { // <= // << - t = x.nextToken(); + token = x.nextToken(); // 0) { - context.accumulate("content", s); + string = x.nextCDATA(); + if (string.length() > 0) { + context.accumulate("content", string); } return false; } @@ -170,108 +170,111 @@ public class XML { } i = 1; do { - t = x.nextMeta(); - if (t == null) { + token = x.nextMeta(); + if (token == null) { throw x.syntaxError("Missing '>' after ' 0); return false; - } else if (t == QUEST) { + } else if (token == QUEST) { // "); return false; - } else if (t == SLASH) { + } else if (token == SLASH) { // Close tag - } else if (t == SLASH) { + } else if (token == SLASH) { if (x.nextToken() != GT) { throw x.syntaxError("Misshaped tag"); } - if (o.length() > 0) { - context.accumulate(n, o); + if (jsonobject.length() > 0) { + context.accumulate(tagName, jsonobject); } else { - context.accumulate(n, ""); + context.accumulate(tagName, ""); } return false; // Content, between <...> and - } else if (t == GT) { + } else if (token == GT) { for (;;) { - t = x.nextContent(); - if (t == null) { - if (n != null) { - throw x.syntaxError("Unclosed tag " + n); + token = x.nextContent(); + if (token == null) { + if (tagName != null) { + throw x.syntaxError("Unclosed tag " + tagName); } return false; - } else if (t instanceof String) { - s = (String)t; - if (s.length() > 0) { - o.accumulate("content", XML.stringToValue(s)); + } else if (token instanceof String) { + string = (String)token; + if (string.length() > 0) { + jsonobject.accumulate("content", + XML.stringToValue(string)); } // Nested element - } else if (t == LT) { - if (parse(x, o, n)) { - if (o.length() == 0) { - context.accumulate(n, ""); - } else if (o.length() == 1 && - o.opt("content") != null) { - context.accumulate(n, o.opt("content")); + } else if (token == LT) { + if (parse(x, jsonobject, tagName)) { + if (jsonobject.length() == 0) { + context.accumulate(tagName, ""); + } else if (jsonobject.length() == 1 && + jsonobject.opt("content") != null) { + context.accumulate(tagName, + jsonobject.opt("content")); } else { - context.accumulate(n, o); + context.accumulate(tagName, jsonobject); } return false; } @@ -354,142 +357,141 @@ public class XML { * @throws JSONException */ public static JSONObject toJSONObject(String string) throws JSONException { - JSONObject o = new JSONObject(); + JSONObject jo = new JSONObject(); XMLTokener x = new XMLTokener(string); 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. - * @param o A JSONObject. + * @param object A JSONObject. * @return A string. * @throws JSONException */ - public static String toString(Object o) throws JSONException { - return toString(o, null); + public static String toString(Object object) throws JSONException { + return toString(object, null); } /** * 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. * @return A string. * @throws JSONException */ - public static String toString(Object o, String tagName) + public static String toString(Object object, String tagName) throws JSONException { - StringBuffer b = new StringBuffer(); + StringBuffer sb = new StringBuffer(); int i; JSONArray ja; JSONObject jo; - String k; + String key; Iterator keys; - int len; - String s; - Object v; - if (o instanceof JSONObject) { + int length; + String string; + Object value; + if (object instanceof JSONObject) { // Emit if (tagName != null) { - b.append('<'); - b.append(tagName); - b.append('>'); + sb.append('<'); + sb.append(tagName); + sb.append('>'); } // Loop thru the keys. - jo = (JSONObject)o; + jo = (JSONObject)object; keys = jo.keys(); while (keys.hasNext()) { - k = keys.next().toString(); - v = jo.opt(k); - if (v == null) { - v = ""; + key = keys.next().toString(); + value = jo.opt(key); + if (value == null) { + value = ""; } - if (v instanceof String) { - s = (String)v; + if (value instanceof String) { + string = (String)value; } else { - s = null; + string = null; } // Emit content in body - if (k.equals("content")) { - if (v instanceof JSONArray) { - ja = (JSONArray)v; - len = ja.length(); - for (i = 0; i < len; i += 1) { + if (key.equals("content")) { + if (value instanceof JSONArray) { + ja = (JSONArray)value; + length = ja.length(); + for (i = 0; i < length; i += 1) { if (i > 0) { - b.append('\n'); + sb.append('\n'); } - b.append(escape(ja.get(i).toString())); + sb.append(escape(ja.get(i).toString())); } } else { - b.append(escape(v.toString())); + sb.append(escape(value.toString())); } // Emit an array of similar keys - } else if (v instanceof JSONArray) { - ja = (JSONArray)v; - len = ja.length(); - for (i = 0; i < len; i += 1) { - v = ja.get(i); - if (v instanceof JSONArray) { - b.append('<'); - b.append(k); - b.append('>'); - b.append(toString(v)); - b.append("'); + } else if (value instanceof JSONArray) { + ja = (JSONArray)value; + length = ja.length(); + for (i = 0; i < length; i += 1) { + value = ja.get(i); + if (value instanceof JSONArray) { + sb.append('<'); + sb.append(key); + sb.append('>'); + sb.append(toString(value)); + sb.append("'); } else { - b.append(toString(v, k)); + sb.append(toString(value, key)); } } - } else if (v.equals("")) { - b.append('<'); - b.append(k); - b.append("/>"); + } else if (value.equals("")) { + sb.append('<'); + sb.append(key); + sb.append("/>"); // Emit a new tag } else { - b.append(toString(v, k)); + sb.append(toString(value, key)); } } if (tagName != null) { // Emit the close tag - 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 // where XML is lacking, synthesize an element. - } else if (o instanceof JSONArray) { - ja = (JSONArray)o; - len = ja.length(); - for (i = 0; i < len; ++i) { - v = ja.opt(i); - b.append(toString(v, (tagName == null) ? "array" : tagName)); + } else if (object instanceof JSONArray) { + ja = (JSONArray)object; + length = ja.length(); + for (i = 0; i < length; i += 1) { + sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName)); } - return b.toString(); + return sb.toString(); } else { - s = (o == null) ? "null" : escape(o.toString()); - return (tagName == null) ? "\"" + s + "\"" : - (s.length() == 0) ? "<" + tagName + "/>" : - "<" + tagName + ">" + s + ""; + string = (object == null) ? "null" : escape(object.toString()); + return (tagName == null) ? "\"" + string + "\"" : + (string.length() == 0) ? "<" + tagName + "/>" : + "<" + tagName + ">" + string + ""; } } } \ No newline at end of file diff --git a/XMLTokener.java b/XMLTokener.java index 47ff3f2..8762eae 100755 --- a/XMLTokener.java +++ b/XMLTokener.java @@ -28,7 +28,7 @@ SOFTWARE. * The XMLTokener extends the JSONTokener to provide additional methods * for the parsing of XML texts. * @author JSON.org - * @version 2010-01-30 + * @version 2010-12-24 */ public class XMLTokener extends JSONTokener { @@ -120,11 +120,11 @@ public class XMLTokener extends JSONTokener { /** * Return the next entity. These entities are translated to Characters: * & ' > < ". - * @param a An ampersand character. + * @param ampersand An ampersand character. * @return A Character or an entity String if the entity is not recognized. * @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(); for (;;) { char c = next(); @@ -136,9 +136,9 @@ public class XMLTokener extends JSONTokener { throw syntaxError("Missing ';' in XML entity: &" + sb); } } - String s = sb.toString(); - Object e = entity.get(s); - return e != null ? e : a + s + ";"; + String string = sb.toString(); + Object object = entity.get(string); + return object != null ? object : ampersand + string + ";"; } @@ -304,15 +304,15 @@ public class XMLTokener extends JSONTokener { int i; int j; int offset = 0; - int n = to.length(); - char[] circle = new char[n]; + int length = to.length(); + char[] circle = new char[length]; /* * First fill the circle buffer with as many characters as are in the * 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(); if (c == 0) { return false; @@ -328,14 +328,14 @@ public class XMLTokener extends JSONTokener { /* * 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)) { b = false; break; } j += 1; - if (j >= n) { - j -= n; + if (j >= length) { + j -= length; } } /* @@ -357,8 +357,8 @@ public class XMLTokener extends JSONTokener { */ circle[offset] = c; offset += 1; - if (offset >= n) { - offset -= n; + if (offset >= length) { + offset -= length; } } }