mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 19:35:31 -04:00
Generalize the logic to disallow nested objects and arrays as keys in objects.
Fixes #771.
This commit is contained in:
parent
01727fd0ed
commit
661114c50d
@ -208,22 +208,14 @@ public class JSONObject {
|
|||||||
throw x.syntaxError("A JSONObject text must begin with '{'");
|
throw x.syntaxError("A JSONObject text must begin with '{'");
|
||||||
}
|
}
|
||||||
for (;;) {
|
for (;;) {
|
||||||
char prev = x.getPrevious();
|
|
||||||
c = x.nextClean();
|
c = x.nextClean();
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case 0:
|
case 0:
|
||||||
throw x.syntaxError("A JSONObject text must end with '}'");
|
throw x.syntaxError("A JSONObject text must end with '}'");
|
||||||
case '}':
|
case '}':
|
||||||
return;
|
return;
|
||||||
case '{':
|
|
||||||
case '[':
|
|
||||||
if(prev=='{') {
|
|
||||||
throw x.syntaxError("A JSON Object can not directly nest another JSON Object or JSON Array.");
|
|
||||||
}
|
|
||||||
// fall through
|
|
||||||
default:
|
default:
|
||||||
x.back();
|
key = x.nextSimpleValue(c).toString();
|
||||||
key = x.nextValue().toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// The key is followed by ':'.
|
// The key is followed by ':'.
|
||||||
|
@ -402,12 +402,7 @@ public class JSONTokener {
|
|||||||
*/
|
*/
|
||||||
public Object nextValue() throws JSONException {
|
public Object nextValue() throws JSONException {
|
||||||
char c = this.nextClean();
|
char c = this.nextClean();
|
||||||
String string;
|
|
||||||
|
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '"':
|
|
||||||
case '\'':
|
|
||||||
return this.nextString(c);
|
|
||||||
case '{':
|
case '{':
|
||||||
this.back();
|
this.back();
|
||||||
try {
|
try {
|
||||||
@ -423,6 +418,21 @@ public class JSONTokener {
|
|||||||
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
throw new JSONException("JSON Array or Object depth too large to process.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nextSimpleValue(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
Object nextSimpleValue(char c) {
|
||||||
|
String string;
|
||||||
|
|
||||||
|
switch (c) {
|
||||||
|
case '"':
|
||||||
|
case '\'':
|
||||||
|
return this.nextString(c);
|
||||||
|
case '{':
|
||||||
|
throw syntaxError("Nested object not expected here.");
|
||||||
|
case '[':
|
||||||
|
throw syntaxError("Nested array not expected here.");
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Handle unquoted text. This could be the values true, false, or
|
* Handle unquoted text. This could be the values true, false, or
|
||||||
|
@ -2224,6 +2224,24 @@ public class JSONObjectTest {
|
|||||||
"Expected a ',' or '}' at 15 [character 16 line 1]",
|
"Expected a ',' or '}' at 15 [character 16 line 1]",
|
||||||
e.getMessage());
|
e.getMessage());
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
// key is a nested map
|
||||||
|
String str = "{{\"foo\": \"bar\"}: \"baz\"}";
|
||||||
|
assertNull("Expected an exception",new JSONObject(str));
|
||||||
|
} catch (JSONException e) {
|
||||||
|
assertEquals("Expecting an exception message",
|
||||||
|
"Nested object not expected here. at 2 [character 3 line 1]",
|
||||||
|
e.getMessage());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// key is a nested array containing a map
|
||||||
|
String str = "{\"a\": 1, [{\"foo\": \"bar\"}]: \"baz\"}";
|
||||||
|
assertNull("Expected an exception",new JSONObject(str));
|
||||||
|
} catch (JSONException e) {
|
||||||
|
assertEquals("Expecting an exception message",
|
||||||
|
"Nested array not expected here. at 10 [character 11 line 1]",
|
||||||
|
e.getMessage());
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
// \0 after ,
|
// \0 after ,
|
||||||
String str = "{\"myKey\":true, \0\"myOtherKey\":false}";
|
String str = "{\"myKey\":true, \0\"myOtherKey\":false}";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user