Skip to content

Commit

Permalink
Merge pull request stleary#360 from migueltt/tokener-fix
Browse files Browse the repository at this point in the history
Creating a JSONObject from a string that contains a duplicate key (any level) throws a JSONException that includes location
  • Loading branch information
stleary authored Aug 18, 2017
2 parents d9b8507 + 2e0a813 commit 4cb1ae8
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,21 @@ public JSONObject(JSONTokener x) throws JSONException {
if (c != ':') {
throw x.syntaxError("Expected a ':' after a key");
}
this.putOnce(key, x.nextValue());

// Use syntaxError(..) to include error location

if (key != null) {
// Check if key exists
if (this.opt(key) != null) {
// key already exists
throw x.syntaxError("Duplicate key \"" + key + "\"");
}
// Only add value if non-null
Object value = x.nextValue();
if (value!=null) {
this.put(key, value);
}
}

// Pairs are separated by ','.

Expand Down

0 comments on commit 4cb1ae8

Please sign in to comment.