Generalize the logic to disallow nested objects and arrays as keys in objects.

Fixes #771.
This commit is contained in:
Éamonn McManus
2023-09-20 10:50:48 -07:00
parent 01727fd0ed
commit 661114c50d
3 changed files with 37 additions and 17 deletions

View File

@@ -2224,6 +2224,24 @@ public class JSONObjectTest {
"Expected a ',' or '}' at 15 [character 16 line 1]",
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 {
// \0 after ,
String str = "{\"myKey\":true, \0\"myOtherKey\":false}";