if (nextClean() != '{') throw syntaxError("A JSONObject text must begin with '{'"); c= nextClean(); switch (c) throw syntaxError("A JSONObject text must end with '}'"); case '}': return jsonObject; default: back(); key= nextValue().toString(); c= nextClean(); if (c == '=') if (next() != '>') back(); throw syntaxError("Expected a ':' after a key"); putOnce(jsonObject, key, nextValue()); switch (nextClean()) if (nextClean() == '}')
/** * 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; }
/** * Determine if the source string still contains characters that next() * can consume. * * @return true if not yet at the end of the source. * @throws JSONException thrown if underlying IOException is thrown. */ public boolean more() throws JSONException { char nextChar= next(); if (nextChar == 0) { return false; } back(); return true; }
char c= nextClean(); String s; return nextString(c); case '{': back(); return parseObject(); case '[': case '(': back(); return parseArray(); c= next(); back(); throw syntaxError("Missing value"); return stringToValue(s);
char c= nextClean(); char q; if (c == '[') throw syntaxError("A JSONArray text must start with '['"); if (nextClean() == ']') back(); for (;;) if (nextClean() == ',') back(); list.add(null); back(); list.add(nextValue()); c= nextClean(); switch (c) if (nextClean() == ']') back(); break; case ']': throw syntaxError("Expected a '" + q + "'");
/** * Same as {@link #deserialize(String)}, but uses an instance of * java.io.Reader as json input. * * @param input the stream where the json input is coming from. * @return an Java instance deserialized from the java.io.Reader's input. */ public T deserialize(Reader input) { ObjectBinder binder= createObjectBinder(); return (T) binder.bind(new JSONTokener(input).nextValue()); }
/** * Get the next char in the string, skipping whitespace. * * @throws JSONException if the syntax of the JSON stream is not correct. * @return A character, or 0 if there are no more characters. */ public char nextClean() throws JSONException { for (;;) { char c= next(); if (c == 0 || c > ' ') { return c; } } }
/** * Same as {@link #deserialize(java.io.Reader, Class)}, but uses an instance of * java.io.Reader as json input. * * @param input the stream where the json input is coming from. * @param root a Class used to create the initial object. * @return an Java instance deserialized from the java.io.Reader's input. */ public T deserialize(Reader input, Class root) { ObjectBinder binder= createObjectBinder(); return (T) binder.bind(new JSONTokener(input).nextValue(), root); }
/** * 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. * @throws JSONException if the underlying JSON stream syntax is incorrect */ public String nextTo(char d) throws JSONException { StringBuilder sb= new StringBuilder(); for (;;) { char c= next(); if (c == d || c == 0 || c == '\n' || c == '\r') { if (c != 0) { back(); } return sb.toString().trim(); } sb.append(c); } }
for (;;) c= next(); switch (c) case '\n': case '\r': throw syntaxError("Unterminated string"); case '\\': c= next(); switch (c) break; case 'u': sb.append((char) Integer.parseInt(next(4), 16)); break; case 'x': sb.append((char) Integer.parseInt(next(2), 16)); break; default:
/** * Deserialize the given json formatted input into a Java object. * * @param input a json formatted string. * @return an Java instance deserialized from the json input. */ public T deserialize(String input) { ObjectBinder binder= createObjectBinder(); return (T) binder.bind(new JSONTokener(input).nextValue()); }
/** * Get the text up but not including one of the specified delimiter * 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) throws JSONException { char c; StringBuilder sb= new StringBuilder(); 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); } }
/** * Deserialize the given json input, and use the given Class as * the type of the initial object to deserialize into. This object * must implement a no-arg constructor. * * @param input a json formatted string. * @param root a Class used to create the initial object. * @return the object created from the given json input. */ public T deserialize(String input, Class root) { ObjectBinder binder= createObjectBinder(); return (T) binder.bind(new JSONTokener(input).nextValue(), root); }
/** * Deserialize the given json input, and use the given ObjectFactory to * create the initial object to deserialize into. * * @param input a json formatted string. * @param factory an ObjectFactory used to create the initial object. * @return the object created from the given json input. */ public T deserialize(String input, ObjectFactory factory) { use((String) null, factory); ObjectBinder binder= createObjectBinder(); return (T) binder.bind(new JSONTokener(input).nextValue()); }
/** * Same as {@link #deserialize(String, ObjectFactory)}, but uses an instance of * java.io.Reader as json input. * * @param input the stream where the json input is coming from. * @param factory an ObjectFactory used to create the initial object. * @return an Java instance deserialized from the java.io.Reader's input. */ public T deserialize(Reader input, ObjectFactory factory) { use((String) null, factory); ObjectBinder binder= createObjectBinder(); return (T) binder.bind(new JSONTokener(input).nextValue()); }