From 1f308db7c48048e15c9953ebd6499c84dc135f3b Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 14 Dec 2024 10:01:27 -0600 Subject: [PATCH] restore-jsonparserconfiguration: Restore methods to be used for strict mode --- src/main/java/org/json/JSONArray.java | 61 +++++++++++++------ .../org/json/JSONParserConfiguration.java | 36 +++++++++++ 2 files changed, 80 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 87bd99b..fe56b50 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -83,11 +83,22 @@ public class JSONArray implements Iterable { * If there is a syntax error. */ public JSONArray(JSONTokener x) throws JSONException { + this(x, new JSONParserConfiguration()); + } + + /** + * Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration. + * + * @param x A JSONTokener instance from which the JSONArray is constructed. + * @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser. + * @throws JSONException If a syntax error occurs during the construction of the JSONArray. + */ + public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration) throws JSONException { this(); if (x.nextClean() != '[') { throw x.syntaxError("A JSONArray text must start with '['"); } - + char nextChar = x.nextClean(); if (nextChar == 0) { // array is unclosed. No ']' found, instead EOF @@ -104,27 +115,28 @@ public class JSONArray implements Iterable { this.myArrayList.add(x.nextValue()); } switch (x.nextClean()) { - case 0: - // array is unclosed. No ']' found, instead EOF - throw x.syntaxError("Expected a ',' or ']'"); - case ',': - nextChar = x.nextClean(); - if (nextChar == 0) { + case 0: // array is unclosed. No ']' found, instead EOF throw x.syntaxError("Expected a ',' or ']'"); - } - if (nextChar == ']') { + case ',': + nextChar = x.nextClean(); + if (nextChar == 0) { + // array is unclosed. No ']' found, instead EOF + throw x.syntaxError("Expected a ',' or ']'"); + } + if (nextChar == ']') { + return; + } + x.back(); + break; + case ']': return; - } - x.back(); - break; - case ']': - return; - default: - throw x.syntaxError("Expected a ',' or ']'"); + default: + throw x.syntaxError("Expected a ',' or ']'"); } } } + } /** @@ -138,7 +150,22 @@ public class JSONArray implements Iterable { * If there is a syntax error. */ public JSONArray(String source) throws JSONException { - this(new JSONTokener(source)); + this(source, new JSONParserConfiguration()); + } + + /** + * Construct a JSONArray from a source JSON text. + * + * @param source + * A string that begins with [ (left + * bracket) and ends with ] + *  (right bracket). + * @param jsonParserConfiguration the parser config object + * @throws JSONException + * If there is a syntax error. + */ + public JSONArray(String source, JSONParserConfiguration jsonParserConfiguration) throws JSONException { + this(new JSONTokener(source), jsonParserConfiguration); } /** diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index 190daeb..46996cb 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -17,6 +17,12 @@ public class JSONParserConfiguration extends ParserConfiguration { this.overwriteDuplicateKey = false; } + /** + * This flag, when set to true, instructs the parser to enforce strict mode when parsing JSON text. + * Garbage chars at the end of the doc, unquoted string, and single-quoted strings are all disallowed. + */ + private boolean strictMode; + @Override protected JSONParserConfiguration clone() { JSONParserConfiguration clone = new JSONParserConfiguration(); @@ -58,6 +64,23 @@ public class JSONParserConfiguration extends ParserConfiguration { return clone; } + /** + * Sets the strict mode configuration for the JSON parser. + *

+ * When strict mode is enabled, the parser will throw a JSONException if it encounters an invalid character + * immediately following the final ']' character in the input. This is useful for ensuring strict adherence to the + * JSON syntax, as any characters after the final closing bracket of a JSON array are considered invalid. + * + * @param mode a boolean value indicating whether strict mode should be enabled or not + * @return a new JSONParserConfiguration instance with the updated strict mode setting + */ + public JSONParserConfiguration withStrictMode(final boolean mode) { + JSONParserConfiguration clone = this.clone(); + clone.strictMode = mode; + + return clone; + } + /** * The parser's behavior when meeting duplicate keys, controls whether the parser should * overwrite duplicate keys or not. @@ -67,4 +90,17 @@ public class JSONParserConfiguration extends ParserConfiguration { public boolean isOverwriteDuplicateKey() { return this.overwriteDuplicateKey; } + + /** + * Retrieves the current strict mode setting of the JSON parser. + *

+ * Strict mode, when enabled, instructs the parser to throw a JSONException if it encounters an invalid character + * immediately following the final ']' character in the input. This ensures strict adherence to the JSON syntax, as + * any characters after the final closing bracket of a JSON array are considered invalid. + * + * @return the current strict mode setting. True if strict mode is enabled, false otherwise. + */ + public boolean isStrictMode() { + return this.strictMode; + } }