/** * Strictly validate the JSON text * @param text The text to check. * @throws JSONException If the text is not valid. */ public static void validate(final String text) throws JSONException { JSONTokener x = new JSONTokener(text); validate(x); // make sure nothing more is present after last array or object char c = x.nextClean(); if ( c != 0 ) { throw x.syntaxError("Unexpected '" + c + "' at end of file."); } }
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()); switch (x.nextClean()) { case ';': case ',': if (x.nextClean() == ']') { return; x.back(); break; case ']': return; default: throw x.syntaxError("Expected a ',' or ']'");
/** * 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; }
/** * 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; }
/** * 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; }
char c = nextClean(); String s; case '"': case '\'': return nextString(c); case '{': back(); return new JSONObject(this); case '[': back(); return new JSONArray(this); while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) { sb.append(c); c = next(); back(); throw syntaxError("Missing value.");
/** * 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(','); } }
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 (next() == '/') { break; back(); back(); return '/'; c = next(); } while (c != '\n' && c != '\r' && c != 0); } else if (c == 0 || c > ' ') {
/** * Construct a JSONObject from a string. * This is the most commonly used JSONObject constructor. * @param string A string beginning * with <code>{</code> <small>(left brace)</small> and ending * with <code>}</code> <small>(right brace)</small>. * @exception JSONException If there is a syntax error in the source string. */ public JSONObject(String string) throws JSONException { this(new JSONTokener(string)); }
/** * 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); }
/** * Convert <code>%</code><i>hh</i> sequences to single characters, and * convert plus to space. * @param s A string that may contain * <code>+</code> <small>(plus)</small> and * <code>%</code><i>hh</i> 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(); } }
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."); v = unescape(x.nextTo(';')); x.next();
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': break; case 'u': sb.append((char)Integer.parseInt(next(4), 16)); break; case 'x' : sb.append((char) Integer.parseInt(next(2), 16)); break; default:
/** * 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); } }
/** * Construct a JSONArray from a source sJSON text. * @param string A string that begins with * <code>[</code> <small>(left bracket)</small> * and ends with <code>]</code> <small>(right bracket)</small>. * @throws JSONException If there is a syntax error. */ public JSONArray(String string) throws JSONException { this(new JSONTokener(string)); }
if (x.nextClean() != '{') { throw x.syntaxError("A JSONObject text must begin with '{'"); c = x.nextClean(); switch (c) { case 0: throw x.syntaxError("A JSONObject text must end with '}'"); case '}': return; default: x.back(); key = x.nextValue().toString(); c = x.nextClean(); if (c == '=') { if (x.next() != '>') { x.back(); throw x.syntaxError("Expected a ':' after a key"); put(key, x.nextValue()); switch (x.nextClean()) { case ';': case ',': if (x.nextClean() == '}') { return; x.back();
/** * 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 + ")."); } } } }
/** * 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); } }
/** * 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)); }