mirror of
https://github.com/fankes/JSON-java-compat.git
synced 2025-09-08 11:34:43 +08:00
Add test cases for invalid input
This commit is contained in:
@@ -225,12 +225,19 @@ public class JSONObject {
|
||||
throw x.syntaxError("A JSONObject text must begin with '{'");
|
||||
}
|
||||
for (;;) {
|
||||
char prev = x.getPrevious();
|
||||
c = x.nextClean();
|
||||
switch (c) {
|
||||
case 0:
|
||||
throw x.syntaxError("A JSONObject text must end with '}'");
|
||||
case '}':
|
||||
return;
|
||||
case '{':
|
||||
case '[':
|
||||
if(prev=='{') {
|
||||
throw x.syntaxError("A JSON Object can not directly nest another JSON Object or JSON Array.");
|
||||
}
|
||||
// fall through
|
||||
default:
|
||||
x.back();
|
||||
key = x.nextValue().toString();
|
||||
|
@@ -209,6 +209,12 @@ public class JSONTokener {
|
||||
this.previous = (char) c;
|
||||
return this.previous;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the last character read from the input or '\0' if nothing has been read yet.
|
||||
* @return the last character read from the input.
|
||||
*/
|
||||
protected char getPrevious() { return this.previous;}
|
||||
|
||||
/**
|
||||
* Increments the internal indexes according to the previous character
|
||||
@@ -428,10 +434,18 @@ public class JSONTokener {
|
||||
return this.nextString(c);
|
||||
case '{':
|
||||
this.back();
|
||||
return new JSONObject(this);
|
||||
try {
|
||||
return new JSONObject(this);
|
||||
} catch (StackOverflowError e) {
|
||||
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
||||
}
|
||||
case '[':
|
||||
this.back();
|
||||
return new JSONArray(this);
|
||||
try {
|
||||
return new JSONArray(this);
|
||||
} catch (StackOverflowError e) {
|
||||
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user