/** *@paraminJSONencodedstring.Nullisnotpermittedandwillyielda *tokenerthatthrows{@codeNullPointerExceptions}whenmethodsare *called.
*/ public JSONTokener(String in) { // consume an optional byte order mark (BOM) if it exists if (in != null && in.startsWith("\ufeff")) {
in = in.substring(1);
} this.in = in;
}
/** *Returnsthenextvaluefromtheinput. * *@returna{@linkJSONObject},{@linkJSONArray},String,Boolean, *Integer,Long,Doubleor{@linkJSONObject#NULL}. *@throwsJSONExceptioniftheinputismalformed.
*/ public Object nextValue() throws JSONException { int c = nextCleanInternal(); switch (c) { case -1: throw syntaxError("End of input");
case'{': return readObject();
case'[': return readArray();
case'\'': case'"': return nextString((char) c);
default:
pos--; return readLiteral();
}
}
privateint nextCleanInternal() throws JSONException { while (pos < in.length()) { int c = in.charAt(pos++); switch (c) { case'\t': case' ': case'\n': case'\r': continue;
/* try to parse as an integral type... */ if (literal.indexOf('.') == -1) { int base = 10;
String number = literal; if (number.startsWith("0x") || number.startsWith("0X")) {
number = number.substring(2);
base = 16;
} elseif (number.startsWith("0") && number.length() > 1) {
number = number.substring(1);
base = 8;
} try { long longValue = Long.parseLong(number, base); if (longValue <= Integer.MAX_VALUE && longValue >= Integer.MIN_VALUE) { return (int) longValue;
} else { return longValue;
}
} catch (NumberFormatException e) { /* *Thisonlyhappensforintegralnumbersgreaterthan *Long.MAX_VALUE,numbersinexponentialform(5e-10)and *unquotedstrings.Fallthroughtotryfloatingpoint.
*/
}
}
/* ...next try to parse as a floating point... */ try { returnDouble.valueOf(literal);
} catch (NumberFormatException ignored) {
}
/* ... finally give up. We have an unquoted string */ returnnew String(literal); // a new string avoids leaking memory
}
/** *Returnsthestringuptobutnotincludinganyofthegivencharactersor *anewlinecharacter.Thisdoesnotconsumetheexcludedcharacter.
*/ private String nextToInternal(String excluded) { int start = pos; for (; pos < in.length(); pos++) { char c = in.charAt(pos); if (c == '\r' || c == '\n' || excluded.indexOf(c) != -1) { return in.substring(start, pos);
}
} return in.substring(start);
}
/** *Readsasequenceofkey/valuepairsandthetrailingclosingbrace'}'of *anobject.Theopeningbrace'{'shouldhavealreadybeenread.
*/ private JSONObject readObject() throws JSONException {
JSONObject result = new JSONObject();
/* Peek to see if this is the empty object. */ int first = nextCleanInternal(); if (first == '}') { return result;
} elseif (first != -1) {
pos--;
}
while (true) {
Object name = nextValue(); if (!(name instanceof String)) { if (name == null) { throw syntaxError("Names cannot be null");
} else { throw syntaxError("Names must be strings, but " + name
+ " is of type " + name.getClass().getName());
}
}
/* *Expectthename/valueseparatortobeeitheracolon':',an *equalssign'=',oranarrow"=>".Thelasttwoarebogusbutwe *includethembecausethat'swhattheoriginalimplementationdid.
*/ int separator = nextCleanInternal(); if (separator != ':' && separator != '=') { throw syntaxError("Expected ':' after " + name);
} if (pos < in.length() && in.charAt(pos) == '>') {
pos++;
}
/** *Readsasequenceofvaluesandthetrailingclosingbrace']'ofan *array.Theopeningbrace'['shouldhavealreadybeenread.Notethat *"[]"yieldsanemptyarray,but"[,]"returnsatwo-elementarray *equivalentto"[null,null]".
*/ private JSONArray readArray() throws JSONException {
JSONArray result = new JSONArray();
/* to cover input that ends with ",]". */ boolean hasTrailingSeparator = false;
while (true) { switch (nextCleanInternal()) { case -1: throw syntaxError("Unterminated array"); case']': if (hasTrailingSeparator) {
result.put(null);
} return result; case',': case';': /* A separator without a value first means "null". */
result.put(null);
hasTrailingSeparator = true; continue; default:
pos--;
}
/** *Returnsthecurrentpositionandtheentireinputstring.
*/
@Override public String toString() { // consistent with the original implementation return" at character " + pos + " of " + in;
}
/** *Returnsthenextavailablecharacterifitequals{@codec}.Otherwisean *exceptionisthrown.
*/ publicchar next(char c) throws JSONException { char result = next(); if (result != c) { throw syntaxError("Expected " + c + " but was " + result);
} return result;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.