Skip to content

Commit 4cb1ae8

Browse files
authored
Merge pull request stleary#360 from migueltt/tokener-fix
Creating a JSONObject from a string that contains a duplicate key (any level) throws a JSONException that includes location
2 parents d9b8507 + 2e0a813 commit 4cb1ae8

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

JSONObject.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,21 @@ public JSONObject(JSONTokener x) throws JSONException {
231231
if (c != ':') {
232232
throw x.syntaxError("Expected a ':' after a key");
233233
}
234-
this.putOnce(key, x.nextValue());
234+
235+
// Use syntaxError(..) to include error location
236+
237+
if (key != null) {
238+
// Check if key exists
239+
if (this.opt(key) != null) {
240+
// key already exists
241+
throw x.syntaxError("Duplicate key \"" + key + "\"");
242+
}
243+
// Only add value if non-null
244+
Object value = x.nextValue();
245+
if (value!=null) {
246+
this.put(key, value);
247+
}
248+
}
235249

236250
// Pairs are separated by ','.
237251

0 commit comments

Comments
 (0)