Merge pull request #772 from eamonnmcmanus/complexkey

Disallow nested objects and arrays as keys in objects.
This commit is contained in:
Sean Leary
2023-10-01 11:04:40 -05:00
committed by GitHub
4 changed files with 71 additions and 19 deletions

View File

@@ -208,22 +208,14 @@ 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();
key = x.nextSimpleValue(c).toString();
}
// The key is followed by ':'.
@@ -1726,12 +1718,12 @@ public class JSONObject {
final Object result = method.invoke(bean);
if (result != null) {
// check cyclic dependency and throw error if needed
// the wrap and populateMap combination method is
// the wrap and populateMap combination method is
// itself DFS recursive
if (objectsRecord.contains(result)) {
throw recursivelyDefinedObjectException(key);
}
objectsRecord.add(result);
this.map.put(key, wrap(result, objectsRecord));
@@ -1740,7 +1732,7 @@ public class JSONObject {
// we don't use the result anywhere outside of wrap
// if it's a resource we should be sure to close it
// after calling toString
// after calling toString
if (result instanceof Closeable) {
try {
((Closeable) result).close();