From 7fed0230806031033eaeda8ed1a2851103b91fee Mon Sep 17 00:00:00 2001 From: Miguel Date: Wed, 9 Aug 2017 21:52:36 -0400 Subject: [PATCH 1/4] 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 fcc0c91a7..9d5fc8758 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -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()); + + // 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 ','. From 7d8353401ad0943a00423cb4f3e7d71a4b6a162f Mon Sep 17 00:00:00 2001 From: Miguel Date: Thu, 10 Aug 2017 19:05:57 -0400 Subject: [PATCH 2/4] Adding JSONTokener.back() just before throwing JSONException This forces JSONTokener.syntaxError(..) to point to the last character of the duplicate key. --- JSONObject.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/JSONObject.java b/JSONObject.java index 9d5fc8758..28f401e1e 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -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 From f177c972589463674c4e3883a494cf4c1f186721 Mon Sep 17 00:00:00 2001 From: Miguel Date: Thu, 10 Aug 2017 19:12:41 -0400 Subject: [PATCH 3/4] Replacing tabs with 4-spaces --- JSONObject.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/JSONObject.java b/JSONObject.java index 28f401e1e..a55ecf82d 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -236,15 +236,15 @@ public JSONObject(JSONTokener x) throws JSONException { if (key != null) { // Check if key exists - if (this.opt(key) != null) { - // back one token to point to the last key character - x.back(); + 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 Object value = x.nextValue(); if (value!=null) { - this.put(key, value); + this.put(key, value); } } From 2e0a8137bd911736ded8bda32c07203a0a97e9a6 Mon Sep 17 00:00:00 2001 From: Miguel Date: Mon, 14 Aug 2017 13:01:31 -0400 Subject: [PATCH 4/4] Removed JSONTokener.back() --- JSONObject.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/JSONObject.java b/JSONObject.java index a55ecf82d..1b7b0a10a 100644 --- a/JSONObject.java +++ b/JSONObject.java @@ -237,8 +237,7 @@ public JSONObject(JSONTokener x) throws JSONException { if (key != null) { // Check if key exists if (this.opt(key) != null) { - // back one token to point to the last key character - x.back(); + // key already exists throw x.syntaxError("Duplicate key \"" + key + "\""); } // Only add value if non-null