/** * Parses a JSON string and constructs a corresponding Java object by calling the methods of a {@link org.bson.BSONCallback * BSONCallback} during parsing. If the callback {@code c} is null, this method is equivalent to {@link JSON#parse(String) * parse(String)}. * * @param s the string to parse * @param c the BSONCallback to call during parsing * @return a Java object representing the JSON data * @throws JSONParseException if s is not valid JSON */ public static Object parse(final String s, final BSONCallback c) { if (s == null || (s.trim()).equals("")) { return null; } JSONParser p = new JSONParser(s, c); return p.parse(); }
get(); int start = this.pos; boolean isDouble = false; if (check('-') || check('+')) { pos++; case '.': isDouble = true; parseFraction(); break; case 'e': case 'E': isDouble = true; parseExponent(); break; default:
/** * Parses an object for the form <i>{}</i> and <i>{ members }</i>. * * @return DBObject the next object * @throws JSONParseException if invalid JSON is found */ protected Object parseObject(final String name) { if (name != null) { _callback.objectStart(name); } else { _callback.objectStart(); } read('{'); char current = get(); while (get() != '}') { String key = parseString(false); read(':'); Object value = parse(key); doCallback(key, value); if ((current = get()) == ',') { read(','); } else { break; } } read('}'); return _callback.objectDone(); }
/** * Advances the pointer through the exponent. */ public void parseExponent() { // get past E pos++; if (check('-') || check('+')) { pos++; } outer: while (pos < s.length()) { switch (s.charAt(pos)) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': pos++; break; default: break outer; } } }
/** * Parses the next array. * * @return the array * @throws JSONParseException if invalid JSON is found */ public Object parseArray() { return parseArray(null); }
/** * Parse an unknown type. * * @return Object the next item * @throws JSONParseException if invalid JSON is found */ public Object parse() { return parse(null); }
/** * Parses an object for the form <i>{}</i> and <i>{ members }</i>. * * @return DBObject the next object * @throws JSONParseException if invalid JSON is found */ public Object parseObject() { return parseObject(null); }
/** * Checks the current character, making sure that it is the expected character. * * @param ch the character expected * @throws JSONParseException if the current character does not match the given character */ public boolean check(final char ch) { return get() == ch; }
/** * Advances the pointed through <i>.digits</i>. */ public void parseFraction() { // get past . pos++; outer: while (pos < s.length()) { switch (s.charAt(pos)) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': pos++; break; case 'e': case 'E': parseExponent(); break; default: break outer; } } }
/** * Read the current character, making sure that it is the expected character. Advances the pointer to the next character. * * @param ch the character expected * @throws JSONParseException if the current character does not match the given character */ public void read(final char ch) { if (!check(ch)) { throw new JSONParseException(s, pos); } pos++; }
/** * Parses the next array. * * @return the array * @throws JSONParseException if invalid JSON is found */ public Object parseArray() { return parseArray(null); }
/** * Parse an unknown type. * * @return Object the next item * @throws JSONParseException if invalid JSON is found */ public Object parse() { return parse(null); }
/** * Parses an object for the form <i>{}</i> and <i>{ members }</i>. * * @return DBObject the next object * @throws JSONParseException if invalid JSON is found */ public Object parseObject() { return parseObject(null); }
/** * Checks the current character, making sure that it is the expected character. * * @param ch the character expected * @throws JSONParseException if the current character does not match the given character */ public boolean check(final char ch) { return get() == ch; }
/** * Advances the pointed through <i>.digits</i>. */ public void parseFraction() { // get past . pos++; outer: while (pos < s.length()) { switch (s.charAt(pos)) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': pos++; break; case 'e': case 'E': parseExponent(); break; default: break outer; } } }
/** * Parses the next array. * * @return the array * @throws JSONParseException if invalid JSON is found */ protected Object parseArray(final String name) { if (name != null) { _callback.arrayStart(name); } else { _callback.arrayStart(); } read('['); int i = 0; char current = get(); while (current != ']') { String elemName = String.valueOf(i++); Object elem = parse(elemName); doCallback(elemName, elem); if ((current = get()) == ',') { read(','); } else if (current == ']') { break; } else { throw new JSONParseException(s, pos); } } read(']'); return _callback.arrayDone(); }