|
|
@ -27,36 +27,38 @@ SOFTWARE.
|
|
|
|
* it. It is used by the JSONObject and JSONArray constructors to parse
|
|
|
|
* it. It is used by the JSONObject and JSONArray constructors to parse
|
|
|
|
* JSON source strings.
|
|
|
|
* JSON source strings.
|
|
|
|
* @author JSON.org
|
|
|
|
* @author JSON.org
|
|
|
|
* @version 2010-02-02
|
|
|
|
* @version 2014-05-03
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
package net.yacy.cora.util;
|
|
|
|
package net.yacy.cora.util;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.Reader;
|
|
|
|
import java.io.Reader;
|
|
|
|
import java.io.StringReader;
|
|
|
|
import java.io.StringReader;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class JSONTokener {
|
|
|
|
public class JSONTokener {
|
|
|
|
|
|
|
|
|
|
|
|
private int character;
|
|
|
|
private long character;
|
|
|
|
private boolean eof;
|
|
|
|
private boolean eof;
|
|
|
|
private int index;
|
|
|
|
private long index;
|
|
|
|
private int line;
|
|
|
|
private long line;
|
|
|
|
private char previous;
|
|
|
|
private char previous;
|
|
|
|
private final Reader reader;
|
|
|
|
private Reader reader;
|
|
|
|
private boolean usePrevious;
|
|
|
|
private boolean usePrevious;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Construct a JSONTokener from a reader.
|
|
|
|
* Construct a JSONTokener from a Reader.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param reader A reader.
|
|
|
|
* @param reader A reader.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public JSONTokener(Reader reader) {
|
|
|
|
public JSONTokener(Reader reader) {
|
|
|
|
this.reader = reader.markSupported() ?
|
|
|
|
this.reader = reader.markSupported()
|
|
|
|
reader : new BufferedReader(reader);
|
|
|
|
? reader
|
|
|
|
|
|
|
|
: new BufferedReader(reader);
|
|
|
|
this.eof = false;
|
|
|
|
this.eof = false;
|
|
|
|
this.usePrevious = false;
|
|
|
|
this.usePrevious = false;
|
|
|
|
this.previous = 0;
|
|
|
|
this.previous = 0;
|
|
|
@ -66,6 +68,15 @@ public class JSONTokener {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Construct a JSONTokener from an InputStream.
|
|
|
|
|
|
|
|
* @param inputStream The source.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public JSONTokener(InputStream inputStream) throws JSONException {
|
|
|
|
|
|
|
|
this(new InputStreamReader(inputStream));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Construct a JSONTokener from a string.
|
|
|
|
* Construct a JSONTokener from a string.
|
|
|
|
*
|
|
|
|
*
|
|
|
@ -122,11 +133,11 @@ public class JSONTokener {
|
|
|
|
* @return true if not yet at the end of the source.
|
|
|
|
* @return true if not yet at the end of the source.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public boolean more() throws JSONException {
|
|
|
|
public boolean more() throws JSONException {
|
|
|
|
next();
|
|
|
|
this.next();
|
|
|
|
if (end()) {
|
|
|
|
if (this.end()) {
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
back();
|
|
|
|
this.back();
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -144,7 +155,7 @@ public class JSONTokener {
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
c = this.reader.read();
|
|
|
|
c = this.reader.read();
|
|
|
|
} catch (final IOException exception) {
|
|
|
|
} catch (IOException exception) {
|
|
|
|
throw new JSONException(exception);
|
|
|
|
throw new JSONException(exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -176,9 +187,9 @@ public class JSONTokener {
|
|
|
|
* @throws JSONException if the character does not match.
|
|
|
|
* @throws JSONException if the character does not match.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public char next(char c) throws JSONException {
|
|
|
|
public char next(char c) throws JSONException {
|
|
|
|
char n = next();
|
|
|
|
char n = this.next();
|
|
|
|
if (n != c) {
|
|
|
|
if (n != c) {
|
|
|
|
throw syntaxError("Expected '" + c + "' and instead saw '" +
|
|
|
|
throw this.syntaxError("Expected '" + c + "' and instead saw '" +
|
|
|
|
n + "'");
|
|
|
|
n + "'");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
return n;
|
|
|
@ -199,17 +210,17 @@ public class JSONTokener {
|
|
|
|
return "";
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char[] buffer = new char[n];
|
|
|
|
char[] chars = new char[n];
|
|
|
|
int pos = 0;
|
|
|
|
int pos = 0;
|
|
|
|
|
|
|
|
|
|
|
|
while (pos < n) {
|
|
|
|
while (pos < n) {
|
|
|
|
buffer[pos] = next();
|
|
|
|
chars[pos] = this.next();
|
|
|
|
if (end()) {
|
|
|
|
if (this.end()) {
|
|
|
|
throw syntaxError("Substring bounds error");
|
|
|
|
throw this.syntaxError("Substring bounds error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos += 1;
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new String(buffer);
|
|
|
|
return new String(chars);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -220,7 +231,7 @@ public class JSONTokener {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public char nextClean() throws JSONException {
|
|
|
|
public char nextClean() throws JSONException {
|
|
|
|
for (;;) {
|
|
|
|
for (;;) {
|
|
|
|
char c = next();
|
|
|
|
char c = this.next();
|
|
|
|
if (c == 0 || c > ' ') {
|
|
|
|
if (c == 0 || c > ' ') {
|
|
|
|
return c;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -243,14 +254,14 @@ public class JSONTokener {
|
|
|
|
char c;
|
|
|
|
char c;
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (;;) {
|
|
|
|
for (;;) {
|
|
|
|
c = next();
|
|
|
|
c = this.next();
|
|
|
|
switch (c) {
|
|
|
|
switch (c) {
|
|
|
|
case 0:
|
|
|
|
case 0:
|
|
|
|
case '\n':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
case '\r':
|
|
|
|
throw syntaxError("Unterminated string");
|
|
|
|
throw this.syntaxError("Unterminated string");
|
|
|
|
case '\\':
|
|
|
|
case '\\':
|
|
|
|
c = next();
|
|
|
|
c = this.next();
|
|
|
|
switch (c) {
|
|
|
|
switch (c) {
|
|
|
|
case 'b':
|
|
|
|
case 'b':
|
|
|
|
sb.append('\b');
|
|
|
|
sb.append('\b');
|
|
|
@ -268,7 +279,7 @@ public class JSONTokener {
|
|
|
|
sb.append('\r');
|
|
|
|
sb.append('\r');
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
case 'u':
|
|
|
|
sb.append((char)Integer.parseInt(next(4), 16));
|
|
|
|
sb.append((char)Integer.parseInt(this.next(4), 16));
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
case '"':
|
|
|
|
case '\'':
|
|
|
|
case '\'':
|
|
|
@ -277,7 +288,7 @@ public class JSONTokener {
|
|
|
|
sb.append(c);
|
|
|
|
sb.append(c);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
throw syntaxError("Illegal escape.");
|
|
|
|
throw this.syntaxError("Illegal escape.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
default:
|
|
|
@ -293,16 +304,16 @@ public class JSONTokener {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Get the text up but not including the specified character or the
|
|
|
|
* Get the text up but not including the specified character or the
|
|
|
|
* end of line, whichever comes first.
|
|
|
|
* end of line, whichever comes first.
|
|
|
|
* @param d A delimiter character.
|
|
|
|
* @param delimiter A delimiter character.
|
|
|
|
* @return A string.
|
|
|
|
* @return A string.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public String nextTo(char d) throws JSONException {
|
|
|
|
public String nextTo(char delimiter) throws JSONException {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (;;) {
|
|
|
|
for (;;) {
|
|
|
|
char c = next();
|
|
|
|
char c = this.next();
|
|
|
|
if (c == d || c == 0 || c == '\n' || c == '\r') {
|
|
|
|
if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
|
|
|
|
if (c != 0) {
|
|
|
|
if (c != 0) {
|
|
|
|
back();
|
|
|
|
this.back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sb.toString().trim();
|
|
|
|
return sb.toString().trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -321,11 +332,11 @@ public class JSONTokener {
|
|
|
|
char c;
|
|
|
|
char c;
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
for (;;) {
|
|
|
|
for (;;) {
|
|
|
|
c = next();
|
|
|
|
c = this.next();
|
|
|
|
if (delimiters.indexOf(c) >= 0 || c == 0 ||
|
|
|
|
if (delimiters.indexOf(c) >= 0 || c == 0 ||
|
|
|
|
c == '\n' || c == '\r') {
|
|
|
|
c == '\n' || c == '\r') {
|
|
|
|
if (c != 0) {
|
|
|
|
if (c != 0) {
|
|
|
|
back();
|
|
|
|
this.back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sb.toString().trim();
|
|
|
|
return sb.toString().trim();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -342,22 +353,19 @@ public class JSONTokener {
|
|
|
|
* @return An object.
|
|
|
|
* @return An object.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public Object nextValue() throws JSONException {
|
|
|
|
public Object nextValue() throws JSONException {
|
|
|
|
char c = nextClean();
|
|
|
|
char c = this.nextClean();
|
|
|
|
String s;
|
|
|
|
String string;
|
|
|
|
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
switch (c) {
|
|
|
|
case '"':
|
|
|
|
case '"':
|
|
|
|
case '\'':
|
|
|
|
case '\'':
|
|
|
|
return nextString(c);
|
|
|
|
return this.nextString(c);
|
|
|
|
case '{':
|
|
|
|
case '{':
|
|
|
|
back();
|
|
|
|
this.back();
|
|
|
|
return new JSONObject(this);
|
|
|
|
return new JSONObject(this);
|
|
|
|
case '[':
|
|
|
|
case '[':
|
|
|
|
case '(':
|
|
|
|
this.back();
|
|
|
|
back();
|
|
|
|
|
|
|
|
return new JSONArray(this);
|
|
|
|
return new JSONArray(this);
|
|
|
|
default:
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
/*
|
|
|
@ -372,15 +380,15 @@ public class JSONTokener {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
|
|
|
|
while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
|
|
|
|
sb.append(c);
|
|
|
|
sb.append(c);
|
|
|
|
c = next();
|
|
|
|
c = this.next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
back();
|
|
|
|
this.back();
|
|
|
|
|
|
|
|
|
|
|
|
s = sb.toString().trim();
|
|
|
|
string = sb.toString().trim();
|
|
|
|
if (s.equals("")) {
|
|
|
|
if ("".equals(string)) {
|
|
|
|
throw syntaxError("Missing value");
|
|
|
|
throw this.syntaxError("Missing value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return JSONObject.stringToValue(s);
|
|
|
|
return JSONObject.stringToValue(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -394,12 +402,12 @@ public class JSONTokener {
|
|
|
|
public char skipTo(char to) throws JSONException {
|
|
|
|
public char skipTo(char to) throws JSONException {
|
|
|
|
char c;
|
|
|
|
char c;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
int startIndex = this.index;
|
|
|
|
long startIndex = this.index;
|
|
|
|
int startCharacter = this.character;
|
|
|
|
long startCharacter = this.character;
|
|
|
|
int startLine = this.line;
|
|
|
|
long startLine = this.line;
|
|
|
|
this.reader.mark(Integer.MAX_VALUE);
|
|
|
|
this.reader.mark(1000000);
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
c = next();
|
|
|
|
c = this.next();
|
|
|
|
if (c == 0) {
|
|
|
|
if (c == 0) {
|
|
|
|
this.reader.reset();
|
|
|
|
this.reader.reset();
|
|
|
|
this.index = startIndex;
|
|
|
|
this.index = startIndex;
|
|
|
@ -408,11 +416,10 @@ public class JSONTokener {
|
|
|
|
return c;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (c != to);
|
|
|
|
} while (c != to);
|
|
|
|
} catch (final IOException exc) {
|
|
|
|
} catch (IOException exception) {
|
|
|
|
throw new JSONException(exc);
|
|
|
|
throw new JSONException(exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.back();
|
|
|
|
back();
|
|
|
|
|
|
|
|
return c;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -424,7 +431,7 @@ public class JSONTokener {
|
|
|
|
* @return A JSONException object, suitable for throwing
|
|
|
|
* @return A JSONException object, suitable for throwing
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public JSONException syntaxError(String message) {
|
|
|
|
public JSONException syntaxError(String message) {
|
|
|
|
return new JSONException(message + toString());
|
|
|
|
return new JSONException(message + this.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -435,6 +442,7 @@ public class JSONTokener {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
public String toString() {
|
|
|
|
return " at " + this.index + " [character " + this.character + " line " + this.line + "]";
|
|
|
|
return " at " + this.index + " [character " + this.character + " line " +
|
|
|
|
|
|
|
|
this.line + "]";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|