1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Merge pull request #360 from migueltt/tokener-fix

Creating a JSONObject from a string that contains a duplicate key (any level) throws a JSONException that includes location
This commit is contained in:
Sean Leary 2017-08-17 21:30:01 -05:00 committed by GitHub
commit 4cb1ae802a

View file

@ -231,7 +231,21 @@ public class JSONObject {
if (c != ':') { if (c != ':') {
throw x.syntaxError("Expected a ':' after a key"); 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 ','. // Pairs are separated by ','.