From 7fed0230806031033eaeda8ed1a2851103b91fee Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 9 Aug 2017 21:52:36 -0400 Subject: [PATCH] Update to include error location when creating JSONObject from string/text --- JSONObject.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/JSONObject.java b/JSONObject.java index fcc0c91..9d5fc87 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -231,7 +231,21 @@ public class JSONObject { if (c != ':') { throw x.syntaxError("Expected a ':' after a key"); } - this.putOnce(key, x.nextValue()); + + // Replace: this.putOnce(key, x.nextValue()); + // Use syntaxError(..) to include error location + + if (key != null) { + // Check if key exists + if (this.opt(key) != null) { + 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 ','.