Skip to content

Commit

Permalink
Adding JSONTokener.back() just before throwing JSONException
Browse files Browse the repository at this point in the history
This forces JSONTokener.syntaxError(..) to point to the last character of the duplicate key.
  • Loading branch information
migueltt committed Aug 10, 2017
1 parent 7fed023 commit 7d83534
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,13 @@ public JSONObject(JSONTokener x) throws JSONException {
throw x.syntaxError("Expected a ':' after a key");
}

// Replace: this.putOnce(key, x.nextValue());
// Use syntaxError(..) to include error location

if (key != null) {
// Check if key exists
if (this.opt(key) != null) {
// back one token to point to the last key character
x.back();
throw x.syntaxError("Duplicate key \"" + key + "\"");
}
// Only add value if non-null
Expand Down

0 comments on commit 7d83534

Please sign in to comment.