mirror of
https://github.com/fankes/JSON-java-compat.git
synced 2025-09-09 20:14:28 +08:00
Merge pull request #772 from eamonnmcmanus/complexkey
Disallow nested objects and arrays as keys in objects.
This commit is contained in:
@@ -205,13 +205,17 @@ public class JSONObjectTest {
|
||||
*/
|
||||
@Test
|
||||
public void unquotedText() {
|
||||
String str = "{key1:value1, key2:42}";
|
||||
String str = "{key1:value1, key2:42, 1.2 : 3.4, -7e5 : something!}";
|
||||
JSONObject jsonObject = new JSONObject(str);
|
||||
String textStr = jsonObject.toString();
|
||||
assertTrue("expected key1", textStr.contains("\"key1\""));
|
||||
assertTrue("expected value1", textStr.contains("\"value1\""));
|
||||
assertTrue("expected key2", textStr.contains("\"key2\""));
|
||||
assertTrue("expected 42", textStr.contains("42"));
|
||||
assertTrue("expected 1.2", textStr.contains("\"1.2\""));
|
||||
assertTrue("expected 3.4", textStr.contains("3.4"));
|
||||
assertTrue("expected -7E+5", textStr.contains("\"-7E+5\""));
|
||||
assertTrue("expected something!", textStr.contains("\"something!\""));
|
||||
Util.checkJSONObjectMaps(jsonObject);
|
||||
}
|
||||
|
||||
@@ -2224,6 +2228,42 @@ 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",
|
||||
"Missing value at 1 [character 2 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",
|
||||
"Missing value at 9 [character 10 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
try {
|
||||
// key contains }
|
||||
String str = "{foo}: 2}";
|
||||
assertNull("Expected an exception",new JSONObject(str));
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Expected a ':' after a key at 5 [character 6 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
try {
|
||||
// key contains ]
|
||||
String str = "{foo]: 2}";
|
||||
assertNull("Expected an exception",new JSONObject(str));
|
||||
} catch (JSONException e) {
|
||||
assertEquals("Expecting an exception message",
|
||||
"Expected a ':' after a key at 5 [character 6 line 1]",
|
||||
e.getMessage());
|
||||
}
|
||||
try {
|
||||
// \0 after ,
|
||||
String str = "{\"myKey\":true, \0\"myOtherKey\":false}";
|
||||
|
Reference in New Issue
Block a user