diff --git a/api/src/main/java/org/json/CDL.java b/api/src/main/java/org/json/CDL.java deleted file mode 100644 index 6d9edbf9f..000000000 --- a/api/src/main/java/org/json/CDL.java +++ /dev/null @@ -1,261 +0,0 @@ -package org.json; - -/* -Copyright (c) 2002 JSON.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -The Software shall be used for Good, not Evil. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -/** - * This provides static methods to convert comma delimited text into a - * JSONArray, and to covert a JSONArray into comma delimited text. Comma - * delimited text is a very popular format for data interchange. It is - * understood by most database, spreadsheet, and organizer programs. - *
- * Each row of text represents a row in a table or a data record. Each row - * ends with a NEWLINE character. Each row contains one or more values. - * Values are separated by commas. A value can contain any character except - * for comma, unless is is wrapped in single quotes or double quotes. - *
- * The first row usually contains the names of the columns. - *
- * A comma delimited list can be converted into a JSONArray of JSONObjects. - * The names for the elements in the JSONObjects can be taken from the names - * in the first row. - * @author JSON.org - * @version 2 - */ -public class CDL { - - /** - * Get the next value. The value can be wrapped in quotes. The value can - * be empty. - * @param x A JSONTokener of the source text. - * @return The value string, or null if empty. - * @throws JSONException if the quoted string is badly formed. - */ - private static String getValue(JSONTokener x) throws JSONException { - char c; - do { - c = x.next(); - } while (c <= ' ' && c != 0); - switch (c) { - case 0: - return null; - case '"': - case '\'': - return x.nextString(c); - case ',': - x.back(); - return ""; - default: - x.back(); - return x.nextTo(','); - } - } - - /** - * Produce a JSONArray of strings from a row of comma delimited values. - * @param x A JSONTokener of the source text. - * @return A JSONArray of strings. - * @throws JSONException - */ - public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException { - JSONArray ja = new JSONArray(); - for (;;) { - String value = getValue(x); - if (value == null) { - return null; - } - ja.put(value); - for (;;) { - char c = x.next(); - if (c == ',') { - break; - } - if (c != ' ') { - if (c == '\n' || c == '\r' || c == 0) { - return ja; - } - throw x.syntaxError("Bad character '" + c + "' (" + - (int)c + ")."); - } - } - } - } - - /** - * Produce a JSONObject from a row of comma delimited text, using a - * parallel JSONArray of strings to provides the names of the elements. - * @param names A JSONArray of names. This is commonly obtained from the - * first row of a comma delimited text file using the rowToJSONArray - * method. - * @param x A JSONTokener of the source text. - * @return A JSONObject combining the names and values. - * @throws JSONException - */ - public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x) - throws JSONException { - JSONArray ja = rowToJSONArray(x); - return ja != null ? ja.toJSONObject(names) : null; - } - - /** - * 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. - * @return A JSONArray of JSONObjects. - * @throws JSONException - */ - public static JSONArray toJSONArray(String string) throws JSONException { - return toJSONArray(new JSONTokener(string)); - } - - /** - * Produce a JSONArray of JSONObjects from a comma delimited text string, - * using the first row as a source of names. - * @param x The JSONTokener containing the comma delimited text. - * @return A JSONArray of JSONObjects. - * @throws JSONException - */ - public static JSONArray toJSONArray(JSONTokener x) throws JSONException { - return toJSONArray(rowToJSONArray(x), x); - } - - /** - * Produce a JSONArray of JSONObjects from a comma delimited text string - * using a supplied JSONArray as the source of element names. - * @param names A JSONArray of strings. - * @param string The comma delimited text. - * @return A JSONArray of JSONObjects. - * @throws JSONException - */ - public static JSONArray toJSONArray(JSONArray names, String string) - throws JSONException { - return toJSONArray(names, new JSONTokener(string)); - } - - /** - * Produce a JSONArray of JSONObjects from a comma delimited text string - * using a supplied JSONArray as the source of element names. - * @param names A JSONArray of strings. - * @param x A JSONTokener of the source text. - * @return A JSONArray of JSONObjects. - * @throws JSONException - */ - public static JSONArray toJSONArray(JSONArray names, JSONTokener x) - throws JSONException { - if (names == null || names.length() == 0) { - return null; - } - JSONArray ja = new JSONArray(); - for (;;) { - JSONObject jo = rowToJSONObject(names, x); - if (jo == null) { - break; - } - ja.put(jo); - } - if (ja.length() == 0) { - return null; - } - return ja; - } - - - /** - * Produce a comma delimited text row from a JSONArray. Values containing - * the comma character will be quoted. - * @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.indexOf(',') >= 0) { - if (s.indexOf('"') >= 0) { - sb.append('\''); - sb.append(s); - sb.append('\''); - } else { - sb.append('"'); - sb.append(s); - 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 - * JSONObject. - * @param ja A JSONArray of JSONObjects. - * @return A comma delimited text. - * @throws JSONException - */ - public static String toString(JSONArray ja) throws JSONException { - JSONObject jo = ja.optJSONObject(0); - if (jo != null) { - JSONArray names = jo.names(); - if (names != null) { - return rowToString(names) + toString(names, ja); - } - } - return null; - } - - /** - * Produce a comma delimited text from a JSONArray of JSONObjects using - * a provided list of names. The list of names is not included in the - * output. - * @param names A JSONArray of strings. - * @param ja A JSONArray of JSONObjects. - * @return A comma delimited text. - * @throws JSONException - */ - public static String toString(JSONArray names, JSONArray ja) - throws JSONException { - if (names == null || names.length() == 0) { - return null; - } - StringBuffer sb = new StringBuffer(); - for (int i = 0; i < ja.length(); i += 1) { - JSONObject jo = ja.optJSONObject(i); - if (jo != null) { - sb.append(rowToString(jo.toJSONArray(names))); - } - } - return sb.toString(); - } -} diff --git a/api/src/main/java/org/json/Cookie.java b/api/src/main/java/org/json/Cookie.java deleted file mode 100644 index b752461fd..000000000 --- a/api/src/main/java/org/json/Cookie.java +++ /dev/null @@ -1,169 +0,0 @@ -package org.json; - -/* -Copyright (c) 2002 JSON.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -The Software shall be used for Good, not Evil. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -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 2 - */ -public class Cookie { - - /** - * Produce a copy of a string in which the characters '+', '%', '=', ';' - * and control characters are replaced with "%hh". This is a gentle form - * of URL encoding, attempting to cause as little distortion to the - * string as possible. The characters '=' and ';' are meta characters in - * cookies. By convention, they are escaped using the URL-encoding. This is - * only a convention, not a standard. Often, cookies are expected to have - * encoded values. We encode '=' and ';' because we must. We encode '%' and - * '+' because they are meta characters in URL encoding. - * @param string The source string. - * @return The escaped result. - */ - public static String escape(String string) { - char c; - String s = string.trim(); - StringBuffer sb = new StringBuffer(); - int len = s.length(); - for (int i = 0; i < len; i += 1) { - c = s.charAt(i); - if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') { - sb.append('%'); - sb.append(Character.forDigit((char)((c >>> 4) & 0x0f), 16)); - sb.append(Character.forDigit((char)(c & 0x0f), 16)); - } else { - sb.append(c); - } - } - return sb.toString(); - } - - - /** - * Convert a cookie specification string into a JSONObject. The string - * will contain a name value pair separated by '='. The name and the value - * will be unescaped, possibly converting '+' and '%' sequences. The - * cookie properties may follow, separated by ';', also represented as - * name=value (except the secure property, which does not have a value). - * The name will be stored under the key "name", and the value will be - * stored under the key "value". This method does not do checking or - * validation of the parameters. It only converts the cookie string into - * a JSONObject. - * @param string The cookie specification string. - * @return A JSONObject containing "name", "value", and possibly other - * members. - * @throws JSONException - */ - public static JSONObject toJSONObject(String string) throws JSONException { - String n; - JSONObject o = new JSONObject(); - Object v; - JSONTokener x = new JSONTokener(string); - o.put("name", x.nextTo('=')); - x.next('='); - o.put("value", x.nextTo(';')); - x.next(); - while (x.more()) { - n = unescape(x.nextTo("=;")); - if (x.next() != '=') { - if (n.equals("secure")) { - v = Boolean.TRUE; - } else { - throw x.syntaxError("Missing '=' in cookie parameter."); - } - } else { - v = unescape(x.nextTo(';')); - x.next(); - } - o.put(n, v); - } - return o; - } - - - /** - * Convert a JSONObject into a cookie specification string. The JSONObject - * must contain "name" and "value" members. - * 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 - * @return A cookie specification string - * @throws JSONException - */ - public static String toString(JSONObject o) throws JSONException { - StringBuffer sb = new StringBuffer(); - - sb.append(escape(o.getString("name"))); - sb.append("="); - sb.append(escape(o.getString("value"))); - if (o.has("expires")) { - sb.append(";expires="); - sb.append(o.getString("expires")); - } - if (o.has("domain")) { - sb.append(";domain="); - sb.append(escape(o.getString("domain"))); - } - if (o.has("path")) { - sb.append(";path="); - sb.append(escape(o.getString("path"))); - } - if (o.optBoolean("secure")) { - sb.append(";secure"); - } - return sb.toString(); - } - - /** - * Convert {@code %}hh sequences to single characters, and - * convert plus to space. - * @param s A string that may contain - * {@code +} (plus) and - * {@code %}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); - 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)); - if (d >= 0 && e >= 0) { - c = (char)(d * 16 + e); - i += 2; - } - } - b.append(c); - } - return b.toString(); - } -} diff --git a/api/src/main/java/org/json/CookieList.java b/api/src/main/java/org/json/CookieList.java deleted file mode 100644 index 30bbb3670..000000000 --- a/api/src/main/java/org/json/CookieList.java +++ /dev/null @@ -1,90 +0,0 @@ -package org.json; - -/* -Copyright (c) 2002 JSON.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -The Software shall be used for Good, not Evil. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -import java.util.Iterator; - -/** - * Convert a web browser cookie list string to a JSONObject and back. - * @author JSON.org - * @version 2 - */ -public class CookieList { - - /** - * Convert a cookie list into a JSONObject. A cookie list is a sequence - * of name/value pairs. The names are separated from the values by '='. - * The pairs are separated by ';'. The names and the values - * will be unescaped, possibly converting '+' and '%' sequences. - * - * To add a cookie to a cooklist, - * cookielistJSONObject.put(cookieJSONObject.getString("name"), - * cookieJSONObject.getString("value")); - * @param string A cookie list string - * @return A JSONObject - * @throws JSONException - */ - public static JSONObject toJSONObject(String string) throws JSONException { - JSONObject o = 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(';'))); - x.next(); - } - return o; - } - - - /** - * Convert a JSONObject into a cookie list. A cookie list is a sequence - * 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 - * @return A cookie list string - * @throws JSONException - */ - public static String toString(JSONObject o) throws JSONException { - boolean b = false; - Iterator keys = o.keys(); - String s; - StringBuffer sb = new StringBuffer(); - while (keys.hasNext()) { - s = keys.next().toString(); - if (!o.isNull(s)) { - if (b) { - sb.append(';'); - } - sb.append(Cookie.escape(s)); - sb.append("="); - sb.append(Cookie.escape(o.getString(s))); - b = true; - } - } - return sb.toString(); - } -} diff --git a/api/src/main/java/org/json/HTTP.java b/api/src/main/java/org/json/HTTP.java deleted file mode 100644 index 131676267..000000000 --- a/api/src/main/java/org/json/HTTP.java +++ /dev/null @@ -1,163 +0,0 @@ -package org.json; - -/* -Copyright (c) 2002 JSON.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -The Software shall be used for Good, not Evil. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -import java.util.Iterator; - -/** - * Convert an HTTP header to a JSONObject and back. - * @author JSON.org - * @version 2 - */ -public class HTTP { - - /** Carriage return/line feed. */ - public static final String CRLF = "\r\n"; - - /** - * Convert an HTTP header string into a JSONObject. It can be a request - * header or a response header. A request header will contain - * {@code { - * Method: "POST" (for example), - * "Request-URI": "/" (for example), - * "HTTP-Version": "HTTP/1.1" (for example) - * }} - * A response header will contain - * {@code { - * "HTTP-Version": "HTTP/1.1" (for example), - * "Status-Code": "200" (for example), - * "Reason-Phrase": "OK" (for example) - * }} - * In addition, the other parameters in the header will be captured, using - * the HTTP field names as JSON names, so that {@code - * Date: Sun, 26 May 2002 18:06:04 GMT - * Cookie: Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s - * Cache-Control: no-cache} - * become - * {@code {... - * Date: "Sun, 26 May 2002 18:06:04 GMT", - * Cookie: "Q=q2=PPEAsg--; B=677gi6ouf29bn&b=2&f=s", - * "Cache-Control": "no-cache", - * ...}} - * It does no further checking or conversion. It does not parse dates. - * It does not do '%' transforms on URLs. - * @param string An HTTP header string. - * @return A JSONObject containing the elements and attributes - * of the XML string. - * @throws JSONException - */ - public static JSONObject toJSONObject(String string) throws JSONException { - JSONObject o = new JSONObject(); - HTTPTokener x = new HTTPTokener(string); - String t; - - t = x.nextToken(); - if (t.toUpperCase().startsWith("HTTP")) { - -// Response - - o.put("HTTP-Version", t); - o.put("Status-Code", x.nextToken()); - o.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()); - } - -// Fields - - while (x.more()) { - String name = x.nextTo(':'); - x.next(':'); - o.put(name, x.nextTo('\0')); - x.next(); - } - return o; - } - - - /** - * Convert a JSONObject into an HTTP header. A request header must contain - *
{ - * Method: "POST" (for example), - * "Request-URI": "/" (for example), - * "HTTP-Version": "HTTP/1.1" (for example) - * }- * A response header must contain - *
{ - * "HTTP-Version": "HTTP/1.1" (for example), - * "Status-Code": "200" (for example), - * "Reason-Phrase": "OK" (for example) - * }- * Any other members of the JSONObject will be output as HTTP fields. - * The result will end with two CRLF pairs. - * @param o 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; - StringBuffer sb = new StringBuffer(); - if (o.has("Status-Code") && o.has("Reason-Phrase")) { - sb.append(o.getString("HTTP-Version")); - sb.append(' '); - sb.append(o.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(' '); - sb.append('"'); - sb.append(o.getString("Request-URI")); - sb.append('"'); - sb.append(' '); - sb.append(o.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); - sb.append(": "); - sb.append(o.getString(s)); - sb.append(CRLF); - } - } - sb.append(CRLF); - return sb.toString(); - } -} diff --git a/api/src/main/java/org/json/HTTPTokener.java b/api/src/main/java/org/json/HTTPTokener.java deleted file mode 100644 index f2a6ba72b..000000000 --- a/api/src/main/java/org/json/HTTPTokener.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.json; - -/* -Copyright (c) 2002 JSON.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -The Software shall be used for Good, not Evil. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -/** - * The HTTPTokener extends the JSONTokener to provide additional methods - * for the parsing of HTTP headers. - * @author JSON.org - * @version 2 - */ -public class HTTPTokener extends JSONTokener { - - /** - * Construct an XMLTokener from a string. - * @param s A source string. - */ - public HTTPTokener(String s) { - super(s); - } - - - /** - * Get the next token or string. This is used in parsing HTTP headers. - * @throws JSONException - * @return A String. - */ - public String nextToken() throws JSONException { - char c; - char q; - StringBuffer sb = new StringBuffer(); - do { - c = next(); - } while (Character.isWhitespace(c)); - if (c == '"' || c == '\'') { - q = c; - for (;;) { - c = next(); - if (c < ' ') { - throw syntaxError("Unterminated string."); - } - if (c == q) { - return sb.toString(); - } - sb.append(c); - } - } - for (;;) { - if (c == 0 || Character.isWhitespace(c)) { - return sb.toString(); - } - sb.append(c); - c = next(); - } - } -} diff --git a/api/src/main/java/org/json/JSONArray.java b/api/src/main/java/org/json/JSONArray.java deleted file mode 100644 index b5b59eac5..000000000 --- a/api/src/main/java/org/json/JSONArray.java +++ /dev/null @@ -1,871 +0,0 @@ -package org.json; - -/* -Copyright (c) 2002 JSON.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -The Software shall be used for Good, not Evil. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -import java.io.IOException; -import java.io.Writer; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Map; - -/** - * A JSONArray is an ordered sequence of values. Its external text form is a - * string wrapped in square brackets with commas separating the values. The - * internal form is an object having {@code get} and {@code opt} - * methods for accessing the values by index, and {@code put} methods for - * adding or replacing values. The values can be any of these types: - * {@code Boolean}, {@code JSONArray}, {@code JSONObject}, - * {@code Number}, {@code String}, or the - * {@code JSONObject.NULL object}. - *
- * The constructor can convert a JSON text into a Java object. The - * {@code toString} method converts to JSON text. - *
- * A {@code get} method returns a value if one can be found, and throws an - * exception if one cannot be found. An {@code opt} method returns a - * default value instead of throwing an exception, and so is useful for - * obtaining optional values. - *
- * The generic {@code get()} and {@code opt()} methods return an - * object which you can cast or query for type. There are also typed - * {@code get} and {@code opt} methods that do type checking and type - * coersion for you. - *
- * The texts produced by the {@code toString} methods strictly conform to - * JSON syntax rules. The constructors are more forgiving in the texts they will - * accept: - *
- * Warning: This method assumes that the data structure is acyclical. - * - * @return a printable, displayable, transmittable - * representation of the array. - */ - public String toString() { - try { - return '[' + join(",") + ']'; - } catch (Exception e) { - return null; - } - } - - - /** - * Make a prettyprinted JSON text of this JSONArray. - * Warning: This method assumes that the data structure is acyclical. - * @param indentFactor The number of spaces to add to each level of - * indentation. - * @return a printable, displayable, transmittable - * representation of the object, beginning - * with {@code [} (left bracket) and ending - * with {@code ]} (right bracket). - * @throws JSONException - */ - public String toString(int indentFactor) throws JSONException { - return toString(indentFactor, 0); - } - - - /** - * Make a prettyprinted JSON text of this JSONArray. - * Warning: This method assumes that the data structure is acyclical. - * @param indentFactor The number of spaces to add to each level of - * indentation. - * @param indent The indention of the top level. - * @return a printable, displayable, transmittable - * representation of the array. - * @throws JSONException - */ - String toString(int indentFactor, int indent) throws JSONException { - int len = length(); - if (len == 0) { - return "[]"; - } - int i; - StringBuffer sb = new StringBuffer("["); - if (len == 1) { - sb.append(JSONObject.valueToString(this.myArrayList.get(0), - indentFactor, indent)); - } else { - int newindent = indent + indentFactor; - sb.append('\n'); - for (i = 0; i < len; i += 1) { - if (i > 0) { - sb.append(",\n"); - } - for (int j = 0; j < newindent; j += 1) { - sb.append(' '); - } - sb.append(JSONObject.valueToString(this.myArrayList.get(i), - indentFactor, newindent)); - } - sb.append('\n'); - for (i = 0; i < indent; i += 1) { - sb.append(' '); - } - } - sb.append(']'); - return sb.toString(); - } - - - /** - * Write the contents of the JSONArray as JSON text to a writer. - * For compactness, no whitespace is added. - *
- * Warning: This method assumes that the data structure is acyclical. - * - * @return The writer. - * @throws JSONException - */ - public Writer write(Writer writer) throws JSONException { - try { - boolean b = false; - int len = length(); - - writer.write('['); - - for (int i = 0; i < len; i += 1) { - if (b) { - writer.write(','); - } - Object v = this.myArrayList.get(i); - if (v instanceof JSONObject) { - ((JSONObject)v).write(writer); - } else if (v instanceof JSONArray) { - ((JSONArray)v).write(writer); - } else { - writer.write(JSONObject.valueToString(v)); - } - b = true; - } - writer.write(']'); - return writer; - } catch (IOException e) { - throw new JSONException(e); - } - } -} \ No newline at end of file diff --git a/api/src/main/java/org/json/JSONException.java b/api/src/main/java/org/json/JSONException.java deleted file mode 100644 index 535168f66..000000000 --- a/api/src/main/java/org/json/JSONException.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.json; - -/** - * The JSONException is thrown by the JSON.org classes then things are amiss. - * @author JSON.org - * @version 2 - */ -public class JSONException extends Exception { - private Throwable cause; - - /** - * Constructs a JSONException with an explanatory message. - * @param message Detail about the reason for the exception. - */ - public JSONException(String message) { - super(message); - } - - public JSONException(Throwable t) { - super(t.getMessage()); - this.cause = t; - } - - public Throwable getCause() { - return this.cause; - } -} diff --git a/api/src/main/java/org/json/JSONObject.java b/api/src/main/java/org/json/JSONObject.java deleted file mode 100644 index 89686da77..000000000 --- a/api/src/main/java/org/json/JSONObject.java +++ /dev/null @@ -1,1259 +0,0 @@ -package org.json; - -/* -Copyright (c) 2002 JSON.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -The Software shall be used for Good, not Evil. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -import java.util.Collection; -import java.lang.reflect.Field; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.io.IOException; -import java.io.Writer; - -/** - * A JSONObject is an unordered collection of name/value pairs. Its - * external form is a string wrapped in curly braces with colons between the - * names and values, and commas between the values and names. The internal form - * is an object having {@code get} and {@code opt} methods for - * accessing the values by name, and {@code put} methods for adding or - * replacing values by name. The values can be any of these types: - * {@code Boolean}, {@code JSONArray}, {@code JSONObject}, - * {@code Number}, {@code String}, or the {@code JSONObject.NULL} - * object. A JSONObject constructor can be used to convert an external form - * JSON text into an internal form whose values can be retrieved with the - * {@code get} and {@code opt} methods, or to convert values into a - * JSON text using the {@code put} and {@code toString} methods. - * A {@code get} method returns a value if one can be found, and throws an - * exception if one cannot be found. An {@code opt} method returns a - * default value instead of throwing an exception, and so is useful for - * obtaining optional values. - *
- * The generic {@code get()} and {@code opt()} methods return an - * object, which you can cast or query for type. There are also typed - * {@code get} and {@code opt} methods that do type checking and type - * coersion for you. - *
- * The {@code put} methods adds values to an object. For example,
- * myString = new JSONObject().put("JSON", "Hello, World!").toString();- * produces the string {@code {"JSON": "Hello, World"}}. - *
- * The texts produced by the {@code toString} methods strictly conform to - * the JSON sysntax rules. - * The constructors are more forgiving in the texts they will accept: - *
- * Warning: This method assumes that the data structure is acyclical. - * - * @return a printable, displayable, portable, transmittable - * representation of the object, beginning - * with {@code {} (left brace) and ending - * with {@code }} (right brace). - */ - public String toString() { - try { - Iterator keys = keys(); - StringBuffer sb = new StringBuffer("{"); - - while (keys.hasNext()) { - if (sb.length() > 1) { - sb.append(','); - } - Object o = keys.next(); - sb.append(quote(o.toString())); - sb.append(':'); - sb.append(valueToString(this.myHashMap.get(o))); - } - sb.append('}'); - return sb.toString(); - } catch (Exception e) { - return null; - } - } - - - /** - * Make a prettyprinted JSON text of this JSONObject. - *
- * Warning: This method assumes that the data structure is acyclical. - * @param indentFactor The number of spaces to add to each level of - * indentation. - * @return a printable, displayable, portable, transmittable - * representation of the object, beginning - * with {@code {} (left brace) and ending - * with {@code }} (right brace). - * @throws JSONException If the object contains an invalid number. - */ - public String toString(int indentFactor) throws JSONException { - return toString(indentFactor, 0); - } - - - /** - * Make a prettyprinted JSON text of this JSONObject. - *
- * Warning: This method assumes that the data structure is acyclical. - * @param indentFactor The number of spaces to add to each level of - * indentation. - * @param indent The indentation of the top level. - * @return a printable, displayable, transmittable - * representation of the object, beginning - * with {@code {} (left brace) and ending - * with {@code }} (right brace). - * @throws JSONException If the object contains an invalid number. - */ - String toString(int indentFactor, int indent) throws JSONException { - int i; - int n = length(); - if (n == 0) { - return "{}"; - } - Iterator keys = keys(); - StringBuffer sb = new StringBuffer("{"); - int newindent = indent + indentFactor; - Object o; - if (n == 1) { - o = keys.next(); - sb.append(quote(o.toString())); - sb.append(": "); - sb.append(valueToString(this.myHashMap.get(o), indentFactor, - indent)); - } else { - while (keys.hasNext()) { - o = keys.next(); - if (sb.length() > 1) { - sb.append(",\n"); - } else { - sb.append('\n'); - } - for (i = 0; i < newindent; i += 1) { - sb.append(' '); - } - sb.append(quote(o.toString())); - sb.append(": "); - sb.append(valueToString(this.myHashMap.get(o), indentFactor, - newindent)); - } - if (sb.length() > 1) { - sb.append('\n'); - for (i = 0; i < indent; i += 1) { - sb.append(' '); - } - } - } - sb.append('}'); - return sb.toString(); - } - - - /** - * Make a JSON text of an Object value. If the object has an - * value.toJSONString() method, then that method will be used to produce - * the JSON text. The method is required to produce a strictly - * conforming text. If the object does not contain a toJSONString - * method (which is the most common case), then a text will be - * produced by the rules. - *
- * Warning: This method assumes that the data structure is acyclical. - * @param value The value to be serialized. - * @return a printable, displayable, transmittable - * representation of the object, beginning - * with {@code {} (left brace) and ending - * with {@code }} (right brace). - * @throws JSONException If the value is or contains an invalid number. - */ - static String valueToString(Object value) throws JSONException { - if (value == null || value.equals(null)) { - return "null"; - } - if (value instanceof JSONString) { - Object o; - try { - o = ((JSONString)value).toJSONString(); - } catch (Exception e) { - throw new JSONException(e); - } - if (o instanceof String) { - return (String)o; - } - throw new JSONException("Bad value from toJSONString: " + o); - } - if (value instanceof Number) { - return numberToString((Number) value); - } - if (value instanceof Boolean || value instanceof JSONObject || - value instanceof JSONArray) { - return value.toString(); - } - return quote(value.toString()); - } - - - /** - * Make a prettyprinted JSON text of an object value. - *
- * Warning: This method assumes that the data structure is acyclical. - * @param value The value to be serialized. - * @param indentFactor The number of spaces to add to each level of - * indentation. - * @param indent The indentation of the top level. - * @return a printable, displayable, transmittable - * representation of the object, beginning - * with {@code {} (left brace) and ending - * with {@code }} (right brace). - * @throws JSONException If the object contains an invalid number. - */ - static String valueToString(Object value, int indentFactor, int indent) - throws JSONException { - if (value == null || value.equals(null)) { - return "null"; - } - try { - if (value instanceof JSONString) { - Object o = ((JSONString)value).toJSONString(); - if (o instanceof String) { - return (String)o; - } - } - } catch (Exception e) { - /* forget about it */ - } - if (value instanceof Number) { - return numberToString((Number) value); - } - if (value instanceof Boolean) { - return value.toString(); - } - if (value instanceof JSONObject) { - return ((JSONObject)value).toString(indentFactor, indent); - } - if (value instanceof JSONArray) { - return ((JSONArray)value).toString(indentFactor, indent); - } - return quote(value.toString()); - } - - - /** - * Write the contents of the JSONObject as JSON text to a writer. - * For compactness, no whitespace is added. - *
- * Warning: This method assumes that the data structure is acyclical. - * - * @return The writer. - * @throws JSONException - */ - public Writer write(Writer writer) throws JSONException { - try { - boolean b = false; - Iterator keys = keys(); - writer.write('{'); - - while (keys.hasNext()) { - if (b) { - writer.write(','); - } - Object k = keys.next(); - writer.write(quote(k.toString())); - writer.write(':'); - Object v = this.myHashMap.get(k); - if (v instanceof JSONObject) { - ((JSONObject)v).write(writer); - } else if (v instanceof JSONArray) { - ((JSONArray)v).write(writer); - } else { - writer.write(valueToString(v)); - } - b = true; - } - writer.write('}'); - return writer; - } catch (IOException e) { - throw new JSONException(e); - } - } -} \ No newline at end of file diff --git a/api/src/main/java/org/json/JSONString.java b/api/src/main/java/org/json/JSONString.java deleted file mode 100644 index 2822961ae..000000000 --- a/api/src/main/java/org/json/JSONString.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.json; -/** - * The {@code JSONString} interface allows a {@code toJSONString()} - * method so that a class can change the behavior of - * {@code JSONObject.toString()}, {@code JSONArray.toString()}, - * and {@code JSONWriter.value(}Object{@code )}. The - * {@code toJSONString} method will be used instead of the default behavior - * of using the Object's {@code toString()} method and quoting the result. - */ -public interface JSONString { - /** - * The {@code toJSONString} method allows a class to produce its own JSON - * serialization. - * - * @return A strictly syntactically correct JSON text. - */ - public String toJSONString(); -} diff --git a/api/src/main/java/org/json/JSONStringer.java b/api/src/main/java/org/json/JSONStringer.java deleted file mode 100644 index dcdab0fdf..000000000 --- a/api/src/main/java/org/json/JSONStringer.java +++ /dev/null @@ -1,78 +0,0 @@ -package org.json; - -/* -Copyright (c) 2006 JSON.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -The Software shall be used for Good, not Evil. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -import java.io.StringWriter; - -/** - * JSONStringer provides a quick and convenient way of producing JSON text. - * The texts produced strictly conform to JSON syntax rules. No whitespace is - * added, so the results are ready for transmission or storage. Each instance of - * JSONStringer can produce one JSON text. - *
- * A JSONStringer instance provides a {@code value} method for appending - * values to the - * text, and a {@code key} - * method for adding keys before values in objects. There are {@code array} - * and {@code endArray} methods that make and bound array values, and - * {@code object} and {@code endObject} methods which make and bound - * object values. All of these methods return the JSONWriter instance, - * permitting cascade style. For example,
- * myString = new JSONStringer() - * .object() - * .key("JSON") - * .value("Hello, World!") - * .endObject() - * .toString();which produces the string
- * {"JSON":"Hello, World!"}- *
- * The first method called must be {@code array} or {@code object}. - * There are no methods for adding commas or colons. JSONStringer adds them for - * you. Objects and arrays can be nested up to 20 levels deep. - *
- * This can sometimes be easier than using a JSONObject to build a string. - * @author JSON.org - * @version 2 - */ -public class JSONStringer extends JSONWriter { - /** - * Make a fresh JSONStringer. It can be used to build one JSON text. - */ - public JSONStringer() { - super(new StringWriter()); - } - - /** - * Return the JSON text. This method is used to obtain the product of the - * JSONStringer instance. It will return {@code null} if there was a - * problem in the construction of the JSON text (such as the calls to - * {@code array} were not properly balanced with calls to - * {@code endArray}). - * @return The JSON text. - */ - public String toString() { - return this.mode == 'd' ? this.writer.toString() : null; - } -} diff --git a/api/src/main/java/org/json/JSONTokener.java b/api/src/main/java/org/json/JSONTokener.java deleted file mode 100644 index 66a0af34d..000000000 --- a/api/src/main/java/org/json/JSONTokener.java +++ /dev/null @@ -1,460 +0,0 @@ -package org.json; - -/* -Copyright (c) 2002 JSON.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -The Software shall be used for Good, not Evil. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -/** - * A JSONTokener takes a source string and extracts characters and tokens from - * it. It is used by the JSONObject and JSONArray constructors to parse - * JSON source strings. - * @author JSON.org - * @version 2 - */ -public class JSONTokener { - - /** - * The index of the next character. - */ - private int myIndex; - - - /** - * The source string being tokenized. - */ - private String mySource; - - - /** - * Construct a JSONTokener from a string. - * - * @param s A source string. - */ - public JSONTokener(String s) { - this.myIndex = 0; - this.mySource = s; - } - - - /** - * Back up one character. This provides a sort of lookahead capability, - * so that you can test for a digit or letter before attempting to parse - * the next number or identifier. - */ - public void back() { - if (this.myIndex > 0) { - this.myIndex -= 1; - } - } - - - - /** - * Get the hex value of a character (base16). - * @param c A character between '0' and '9' or between 'A' and 'F' or - * between 'a' and 'f'. - * @return An int between 0 and 15, or -1 if c was not a hex digit. - */ - public static int dehexchar(char c) { - if (c >= '0' && c <= '9') { - return c - '0'; - } - if (c >= 'A' && c <= 'F') { - return c - ('A' - 10); - } - if (c >= 'a' && c <= 'f') { - return c - ('a' - 10); - } - return -1; - } - - - /** - * Determine if the source string still contains characters that next() - * can consume. - * @return true if not yet at the end of the source. - */ - public boolean more() { - return this.myIndex < this.mySource.length(); - } - - - /** - * Get the next character in the source string. - * - * @return The next character, or 0 if past the end of the source string. - */ - public char next() { - if (more()) { - char c = this.mySource.charAt(this.myIndex); - this.myIndex += 1; - return c; - } - return 0; - } - - - /** - * Consume the next character, and check that it matches a specified - * character. - * @param c The character to match. - * @return The character. - * @throws JSONException if the character does not match. - */ - public char next(char c) throws JSONException { - char n = next(); - if (n != c) { - throw syntaxError("Expected '" + c + "' and instead saw '" + - n + "'."); - } - return n; - } - - - /** - * Get the next n characters. - * - * @param n The number of characters to take. - * @return A string of n characters. - * @throws JSONException - * Substring bounds error if there are not - * n characters remaining in the source string. - */ - public String next(int n) throws JSONException { - int i = this.myIndex; - int j = i + n; - if (j >= this.mySource.length()) { - throw syntaxError("Substring bounds error"); - } - this.myIndex += n; - return this.mySource.substring(i, j); - } - - - /** - * Get the next char in the string, skipping whitespace - * and comments (slashslash, slashstar, and hash). - * @throws JSONException - * @return A character, or 0 if there are no more characters. - */ - public char nextClean() throws JSONException { - for (;;) { - char c = next(); - if (c == '/') { - switch (next()) { - case '/': - do { - c = next(); - } while (c != '\n' && c != '\r' && c != 0); - break; - case '*': - for (;;) { - c = next(); - if (c == 0) { - throw syntaxError("Unclosed comment."); - } - if (c == '*') { - if (next() == '/') { - break; - } - back(); - } - } - break; - default: - back(); - return '/'; - } - } else if (c == '#') { - do { - c = next(); - } while (c != '\n' && c != '\r' && c != 0); - } else if (c == 0 || c > ' ') { - return c; - } - } - } - - - /** - * Return the characters up to the next close quote character. - * Backslash processing is done. The formal JSON format does not - * allow strings in single quotes, but an implementation is allowed to - * accept them. - * @param quote The quoting character, either - * {@code "} (double quote) or - * {@code '} (single quote). - * @return A String. - * @throws JSONException Unterminated string. - */ - public String nextString(char quote) throws JSONException { - char c; - StringBuffer sb = new StringBuffer(); - for (;;) { - c = next(); - switch (c) { - case 0: - case '\n': - case '\r': - throw syntaxError("Unterminated string"); - case '\\': - c = next(); - switch (c) { - case 'b': - sb.append('\b'); - break; - case 't': - sb.append('\t'); - break; - case 'n': - sb.append('\n'); - break; - case 'f': - sb.append('\f'); - break; - case 'r': - sb.append('\r'); - break; - case 'u': - sb.append((char)Integer.parseInt(next(4), 16)); - break; - case 'x' : - sb.append((char) Integer.parseInt(next(2), 16)); - break; - default: - sb.append(c); - } - break; - default: - if (c == quote) { - return sb.toString(); - } - sb.append(c); - } - } - } - - - /** - * Get the text up but not including the specified character or the - * end of line, whichever comes first. - * @param d A delimiter character. - * @return A string. - */ - public String nextTo(char d) { - StringBuffer sb = new StringBuffer(); - for (;;) { - char c = next(); - if (c == d || c == 0 || c == '\n' || c == '\r') { - if (c != 0) { - back(); - } - return sb.toString().trim(); - } - sb.append(c); - } - } - - - /** - * Get the text up but not including one of the specified delimeter - * characters or the end of line, whichever comes first. - * @param delimiters A set of delimiter characters. - * @return A string, trimmed. - */ - public String nextTo(String delimiters) { - char c; - StringBuffer sb = new StringBuffer(); - for (;;) { - c = next(); - if (delimiters.indexOf(c) >= 0 || c == 0 || - c == '\n' || c == '\r') { - if (c != 0) { - back(); - } - return sb.toString().trim(); - } - sb.append(c); - } - } - - - /** - * Get the next value. The value can be a Boolean, Double, Integer, - * JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object. - * @throws JSONException If syntax error. - * - * @return An object. - */ - public Object nextValue() throws JSONException { - char c = nextClean(); - String s; - - switch (c) { - case '"': - case '\'': - return nextString(c); - case '{': - back(); - return new JSONObject(this); - case '[': - back(); - return new JSONArray(this); - } - - /* - * Handle unquoted text. This could be the values true, false, or - * null, or it can be a number. An implementation (such as this one) - * is allowed to also accept non-standard forms. - * - * Accumulate characters until we reach the end of the text or a - * formatting character. - */ - - StringBuffer sb = new StringBuffer(); - char b = c; - while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) { - sb.append(c); - c = next(); - } - back(); - - /* - * If it is true, false, or null, return the proper value. - */ - - s = sb.toString().trim(); - if (s.equals("")) { - throw syntaxError("Missing value."); - } - if (s.equalsIgnoreCase("true")) { - return Boolean.TRUE; - } - if (s.equalsIgnoreCase("false")) { - return Boolean.FALSE; - } - if (s.equalsIgnoreCase("null")) { - return JSONObject.NULL; - } - - /* - * If it might be a number, try converting it. We support the 0- and 0x- - * conventions. If a number cannot be produced, then the value will just - * be a string. Note that the 0-, 0x-, plus, and implied string - * conventions are non-standard. A JSON parser is free to accept - * non-JSON forms as long as it accepts all correct JSON forms. - */ - - if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') { - if (b == '0') { - if (s.length() > 2 && - (s.charAt(1) == 'x' || s.charAt(1) == 'X')) { - try { - return new Integer(Integer.parseInt(s.substring(2), - 16)); - } catch (Exception e) { - /* Ignore the error */ - } - } else { - try { - return new Integer(Integer.parseInt(s, 8)); - } catch (Exception e) { - /* Ignore the error */ - } - } - } - try { - return new Integer(s); - } catch (Exception e) { - try { - return new Long(s); - } catch (Exception f) { - try { - return new Double(s); - } catch (Exception g) { - return s; - } - } - } - } - return s; - } - - - /** - * Skip characters until the next character is the requested character. - * If the requested character is not found, no characters are skipped. - * @param to A character to skip to. - * @return The requested character, or zero if the requested character - * is not found. - */ - public char skipTo(char to) { - char c; - int index = this.myIndex; - do { - c = next(); - if (c == 0) { - this.myIndex = index; - return c; - } - } while (c != to); - back(); - return c; - } - - - /** - * Skip characters until past the requested string. - * If it is not found, we are left at the end of the source. - * @param to A string to skip past. - */ - public void skipPast(String to) { - this.myIndex = this.mySource.indexOf(to, this.myIndex); - if (this.myIndex < 0) { - this.myIndex = this.mySource.length(); - } else { - this.myIndex += to.length(); - } - } - - - /** - * Make a JSONException to signal a syntax error. - * - * @param message The error message. - * @return A JSONException object, suitable for throwing - */ - public JSONException syntaxError(String message) { - return new JSONException(message + toString()); - } - - - /** - * Make a printable string of this JSONTokener. - * - * @return " at character [this.myIndex] of [this.mySource]" - */ - public String toString() { - return " at character " + this.myIndex + " of " + this.mySource; - } -} \ No newline at end of file diff --git a/api/src/main/java/org/json/JSONWriter.java b/api/src/main/java/org/json/JSONWriter.java deleted file mode 100644 index b95959ec1..000000000 --- a/api/src/main/java/org/json/JSONWriter.java +++ /dev/null @@ -1,318 +0,0 @@ -package org.json; - -import java.io.IOException; -import java.io.Writer; - -/* -Copyright (c) 2006 JSON.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -The Software shall be used for Good, not Evil. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -*/ - -/** - * JSONWriter provides a quick and convenient way of producing JSON text. - * The texts produced strictly conform to JSON syntax rules. No whitespace is - * added, so the results are ready for transmission or storage. Each instance of - * JSONWriter can produce one JSON text. - *
- * A JSONWriter instance provides a {@code value} method for appending - * values to the - * text, and a {@code key} - * method for adding keys before values in objects. There are {@code array} - * and {@code endArray} methods that make and bound array values, and - * {@code object} and {@code endObject} methods which make and bound - * object values. All of these methods return the JSONWriter instance, - * permitting a cascade style. For example,
- * new JSONWriter(myWriter) - * .object() - * .key("JSON") - * .value("Hello, World!") - * .endObject();which writes
- * {"JSON":"Hello, World!"}- *
- * The first method called must be {@code array} or {@code object}. - * There are no methods for adding commas or colons. JSONWriter adds them for - * you. Objects and arrays can be nested up to 20 levels deep. - *
- * This can sometimes be easier than using a JSONObject to build a string.
- * @author JSON.org
- * @version 2
- */
-public class JSONWriter {
- private static final int maxdepth = 20;
-
- /**
- * The comma flag determines if a comma should be output before the next
- * value.
- */
- private boolean comma;
-
- /**
- * The current mode. Values:
- * 'a' (array),
- * 'd' (done),
- * 'i' (initial),
- * 'k' (key),
- * 'o' (object).
- */
- protected char mode;
-
- /**
- * The object/array stack.
- */
- private char stack[];
-
- /**
- * The stack top index. A value of 0 indicates that the stack is empty.
- */
- private int top;
-
- /**
- * The writer that will receive the output.
- */
- protected Writer writer;
-
- /**
- * Make a fresh JSONWriter. It can be used to build one JSON text.
- */
- public JSONWriter(Writer w) {
- this.comma = false;
- this.mode = 'i';
- this.stack = new char[maxdepth];
- this.top = 0;
- this.writer = w;
- }
-
- /**
- * Append a value.
- * @param s A string value.
- * @return this
- * @throws JSONException If the value is out of sequence.
- */
- private JSONWriter append(String s) throws JSONException {
- if (s == null) {
- throw new JSONException("Null pointer");
- }
- if (this.mode == 'o' || this.mode == 'a') {
- try {
- if (this.comma && this.mode == 'a') {
- this.writer.write(',');
- }
- this.writer.write(s);
- } catch (IOException e) {
- throw new JSONException(e);
- }
- if (this.mode == 'o') {
- this.mode = 'k';
- }
- this.comma = true;
- return this;
- }
- throw new JSONException("Value out of sequence.");
- }
-
- /**
- * Begin appending a new array. All values until the balancing
- * {@code endArray} will be appended to this array. The
- * {@code endArray} method must be called to mark the array's end.
- * @return this
- * @throws JSONException If the nesting is too deep, or if the object is
- * started in the wrong place (for example as a key or after the end of the
- * outermost array or object).
- */
- public JSONWriter array() throws JSONException {
- if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {
- this.push('a');
- this.append("[");
- this.comma = false;
- return this;
- }
- throw new JSONException("Misplaced array.");
- }
-
- /**
- * End something.
- * @param m 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 == 'o' ? "Misplaced endObject." :
- "Misplaced endArray.");
- }
- this.pop(m);
- try {
- this.writer.write(c);
- } catch (IOException e) {
- throw new JSONException(e);
- }
- this.comma = true;
- return this;
- }
-
- /**
- * End an array. This method most be called to balance calls to
- * {@code array}.
- * @return this
- * @throws JSONException If incorrectly nested.
- */
- public JSONWriter endArray() throws JSONException {
- return this.end('a', ']');
- }
-
- /**
- * End an object. This method most be called to balance calls to
- * {@code object}.
- * @return this
- * @throws JSONException If incorrectly nested.
- */
- public JSONWriter endObject() throws JSONException {
- return this.end('k', '}');
- }
-
- /**
- * 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.
- * @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) {
- throw new JSONException("Null key.");
- }
- if (this.mode == 'k') {
- try {
- if (this.comma) {
- this.writer.write(',');
- }
- this.writer.write(JSONObject.quote(s));
- this.writer.write(':');
- this.comma = false;
- this.mode = 'o';
- return this;
- } catch (IOException e) {
- throw new JSONException(e);
- }
- }
- throw new JSONException("Misplaced key.");
- }
-
-
- /**
- * Begin appending a new object. All keys and values until the balancing
- * {@code endObject} will be appended to this object. The
- * {@code endObject} method must be called to mark the object's end.
- * @return this
- * @throws JSONException If the nesting is too deep, or if the object is
- * started in the wrong place (for example as a key or after the end of the
- * outermost array or object).
- */
- public JSONWriter object() throws JSONException {
- if (this.mode == 'i') {
- this.mode = 'o';
- }
- if (this.mode == 'o' || this.mode == 'a') {
- this.append("{");
- this.push('k');
- this.comma = false;
- return this;
- }
- throw new JSONException("Misplaced object.");
-
- }
-
-
- /**
- * Pop an array or object scope.
- * @param c The scope to close.
- * @throws JSONException If nesting is wrong.
- */
- private void pop(char c) throws JSONException {
- if (this.top <= 0 || this.stack[this.top - 1] != c) {
- throw new JSONException("Nesting error.");
- }
- this.top -= 1;
- this.mode = this.top == 0 ? 'd' : this.stack[this.top - 1];
- }
-
- /**
- * Push an array or object scope.
- * @param c The scope to open.
- * @throws JSONException If nesting is too deep.
- */
- private void push(char c) throws JSONException {
- if (this.top >= maxdepth) {
- throw new JSONException("Nesting too deep.");
- }
- this.stack[this.top] = c;
- this.mode = c;
- this.top += 1;
- }
-
-
- /**
- * Append either the value {@code true} or the value
- * {@code false}.
- * @param b A boolean.
- * @return this
- * @throws JSONException
- */
- public JSONWriter value(boolean b) throws JSONException {
- return this.append(b ? "true" : "false");
- }
-
- /**
- * Append a double value.
- * @param d A double.
- * @return this
- * @throws JSONException If the number is not finite.
- */
- public JSONWriter value(double d) throws JSONException {
- return this.value(new Double(d));
- }
-
- /**
- * Append a long value.
- * @param l A long.
- * @return this
- * @throws JSONException
- */
- public JSONWriter value(long l) throws JSONException {
- return this.append(Long.toString(l));
- }
-
-
- /**
- * 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.
- * @return this
- * @throws JSONException If the value is out of sequence.
- */
- public JSONWriter value(Object o) throws JSONException {
- return this.append(JSONObject.valueToString(o));
- }
-}
diff --git a/api/src/main/java/org/json/Test.java b/api/src/main/java/org/json/Test.java
deleted file mode 100644
index a68e8146c..000000000
--- a/api/src/main/java/org/json/Test.java
+++ /dev/null
@@ -1,413 +0,0 @@
-package org.json;
-
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.Map;
-import java.io.StringWriter;
-
-/**
- * Test class. This file is not formally a member of the org.json library.
- * It is just a casual test tool.
- */
-public class Test {
-
- /**
- * Entry point.
- * @param args Command line argumments
- */
- public static void main(String args[]) {
- Iterator it;
- JSONArray a;
- JSONObject j;
- JSONStringer jj;
- String s;
-
-// Obj is a typical class that implements JSONString.
-
- class Obj implements JSONString {
- public String aString;
- public double aNumber;
- public boolean aBoolean;
-
- public Obj(String string, double n, boolean b) {
- this.aString = string;
- this.aNumber = n;
- this.aBoolean = b;
- }
-
- public String toJSONString() {
- return "{" + JSONObject.quote(this.aString) + ":" +
- JSONObject.doubleToString(this.aNumber) + "}";
- }
- public String toString() {
- return this.aString + " " + this.aNumber + " " + this.aBoolean;
- }
- }
-
-
- try {
- String sa[] = {"aString", "aNumber", "aBoolean"};
- Obj obj = new Obj("A string, a number, and a boolean", 42, true);
-
- j = XML.toJSONObject("");
- System.out.println(j.toString());
-
- jj = new JSONStringer();
- s = jj.object()
- .key("foo")
- .value("bar")
- .key("baz")
- .array()
- .object()
- .key("quux")
- .value("Thanks, Josh!")
- .endObject()
- .endArray()
- .endObject()
- .toString();
- System.out.println(s);
-
- System.out.println(new JSONStringer()
- .object()
- .key("a")
- .array()
- .array()
- .array()
- .value("b")
- .endArray()
- .endArray()
- .endArray()
- .endObject()
- .toString());
-
- jj = new JSONStringer();
- jj.array();
- jj.value(1);
- jj.array();
- jj.value(null);
- jj.array();
- jj.object();
- jj.key("empty-array").array().endArray();
- jj.key("answer").value(42);
- jj.key("null").value(null);
- jj.key("false").value(false);
- jj.key("true").value(true);
- jj.key("big").value(123456789e+88);
- jj.key("small").value(123456789e-88);
- jj.key("empty-object").object().endObject();
- jj.key("long");
- jj.value(9223372036854775807L);
- jj.endObject();
- jj.value("two");
- jj.endArray();
- jj.value(true);
- jj.endArray();
- jj.value(98.6);
- jj.value(-100.0);
- jj.object();
- jj.endObject();
- jj.object();
- jj.key("one");
- jj.value(1.00);
- jj.endObject();
- jj.value(obj);
- jj.endArray();
- System.out.println(jj.toString());
-
- System.out.println(new JSONArray(jj.toString()).toString(4));
-
- j = new JSONObject(obj, sa);
- j.put("test", obj);
- j.put("comment", "This object contains a test object that implements JSONString");
- System.out.println(j.toString(4));
-
- j = new JSONObject("{slashes: '///', closetag: '', backslash:'\\\\', ei: {quotes: '\"\\''},eo: {a: '\"quoted\"', b:\"don't\"}, quotes: [\"'\", '\"']}");
- System.out.println(j.toString(2));
- System.out.println(XML.toString(j));
- System.out.println("");
-
- j = new JSONObject(
- "/*comment*/{foo: [true, false,9876543210, 0.0, 1.00000001, 1.000000000001, 1.00000000000000001," +
- " .00000000000000001, 2.00, 0.1, 2e100, -32,[],{}, \"string\"], " +
- " to : null, op : 'Good'," +
- "ten:10} postfix comment");
- j.put("String", "98.6");
- j.put("JSONObject", new JSONObject());
- j.put("JSONArray", new JSONArray());
- j.put("int", 57);
- j.put("double", 123456789012345678901234567890.);
- j.put("true", true);
- j.put("false", false);
- j.put("null", JSONObject.NULL);
- j.put("bool", "true");
- j.put("zero", -0.0);
- a = j.getJSONArray("foo");
- a.put(666);
- a.put(2001.99);
- a.put("so \"fine\".");
- a.put("so Repository Address
");
- System.out.println(j.toString());
- System.out.println(XML.toString(j));
- System.out.println("");
-
- j = XML.toJSONObject("'+search+'
- * & (ampersand) is replaced by &
- * < (less than) is replaced by <
- * > (greater than) is replaced by >
- * " (double quote) is replaced by "
- *
- * @param string The string to be escaped.
- * @return The escaped string.
- */
- public static String escape(String string) {
- StringBuffer sb = new StringBuffer();
- for (int i = 0, len = string.length(); i < len; i++) {
- char c = string.charAt(i);
- switch (c) {
- case '&':
- sb.append("&");
- break;
- case '<':
- sb.append("<");
- break;
- case '>':
- sb.append(">");
- break;
- case '"':
- sb.append(""");
- break;
- default:
- sb.append(c);
- }
- }
- return sb.toString();
- }
-
- /**
- * Scan the content following the named tag, attaching it to the context.
- * @param x The XMLTokener containing the source string.
- * @param context The JSONObject that will include the new material.
- * @param name The tag name.
- * @return true if the close tag is processed.
- * @throws JSONException
- */
- private static boolean parse(XMLTokener x, JSONObject context,
- String name) throws JSONException {
- char c;
- int i;
- String n;
- JSONObject o = null;
- String s;
- Object t;
-
-// Test for and skip past these forms:
-//
-//
-//
-// ... ?>
-// Report errors for these forms:
-// <>
-// <=
-// <<
-
- t = x.nextToken();
-
-// ");
- return false;
- }
- x.back();
- } else if (c == '[') {
- t = x.nextToken();
- if (t.equals("CDATA")) {
- if (x.next() == '[') {
- s = x.nextCDATA();
- if (s.length() > 0) {
- context.accumulate("content", s);
- }
- return false;
- }
- }
- throw x.syntaxError("Expected 'CDATA['");
- }
- i = 1;
- do {
- t = x.nextMeta();
- if (t == null) {
- throw x.syntaxError("Missing '>' after ' 0);
- return false;
- } else if (t == QUEST) {
-
-//
-
- x.skipPast("?>");
- return false;
- } else if (t == SLASH) {
-
-// Close tag
-
- if (name == null || !x.nextToken().equals(name)) {
- throw x.syntaxError("Mismatched close tag");
- }
- if (x.nextToken() != GT) {
- throw x.syntaxError("Misshaped close tag");
- }
- return true;
-
- } else if (t instanceof Character) {
- throw x.syntaxError("Misshaped tag");
-
-// Open tag <
-
- } else {
- n = (String)t;
- t = null;
- o = new JSONObject();
- for (;;) {
- if (t == null) {
- t = x.nextToken();
- }
-
-// attribute = value
-
- if (t instanceof String) {
- s = (String)t;
- t = x.nextToken();
- if (t == EQ) {
- t = x.nextToken();
- if (!(t instanceof String)) {
- throw x.syntaxError("Missing value");
- }
- o.accumulate(s, t);
- t = null;
- } else {
- o.accumulate(s, "");
- }
-
-// Empty tag <.../>
-
- } else if (t == SLASH) {
- if (x.nextToken() != GT) {
- throw x.syntaxError("Misshaped tag");
- }
- context.accumulate(n, o);
- return false;
-
-// Content, between <...> and
-
- } else if (t == GT) {
- for (;;) {
- t = x.nextContent();
- if (t == null) {
- if (name != null) {
- throw x.syntaxError("Unclosed tag " + name);
- }
- return false;
- } else if (t instanceof String) {
- s = (String)t;
- if (s.length() > 0) {
- o.accumulate("content", s);
- }
-
-// 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 {
- context.accumulate(n, o);
- }
- return false;
- }
- }
- }
- } else {
- throw x.syntaxError("Misshaped tag");
- }
- }
- }
- }
-
-
- /**
- * Convert a well-formed (but not necessarily valid) XML string into a
- * JSONObject. Some information may be lost in this transformation
- * because JSON is a data format and XML is a document format. XML uses
- * elements, attributes, and content text, while JSON uses unordered
- * collections of name/value pairs and arrays of values. JSON does not
- * does not like to distinguish between elements and attributes.
- * Sequences of similar elements are represented as JSONArrays. Content
- * text may be placed in a "content" member. Comments, prologs, DTDs, and
- * {@code <[ [ ]]>} are ignored.
- * @param string The source string.
- * @return A JSONObject containing the structured data from the XML string.
- * @throws JSONException
- */
- public static JSONObject toJSONObject(String string) throws JSONException {
- JSONObject o = new JSONObject();
- XMLTokener x = new XMLTokener(string);
- while (x.more()) {
- x.skipPast("<");
- parse(x, o, null);
- }
- return o;
- }
-
-
- /**
- * Convert a JSONObject into a well-formed, element-normal XML string.
- * @param o A JSONObject.
- * @return A string.
- * @throws JSONException
- */
- public static String toString(Object o) throws JSONException {
- return toString(o, null);
- }
-
-
- /**
- * Convert a JSONObject into a well-formed, element-normal XML string.
- * @param o A JSONObject.
- * @param tagName The optional name of the enclosing tag.
- * @return A string.
- * @throws JSONException
- */
- public static String toString(Object o, String tagName)
- throws JSONException {
- StringBuffer b = new StringBuffer();
- int i;
- JSONArray ja;
- JSONObject jo;
- String k;
- Iterator keys;
- int len;
- String s;
- Object v;
- if (o instanceof JSONObject) {
-
-// Emit