From dcbbccc76cd2a822ac0069f4f65b18accd8a9306 Mon Sep 17 00:00:00 2001 From: rikkarth Date: Fri, 15 Mar 2024 00:19:25 +0000 Subject: [PATCH 01/17] feat(#871-strictMode): strictMode configuration add to JSONParserConfiguration docs(#871-strictMode): add javadoc --- .../org/json/JSONParserConfiguration.java | 56 ++++++++++++++++--- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index 190daeb..4aaf025 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -4,11 +4,19 @@ package org.json; * Configuration object for the JSON parser. The configuration is immutable. */ public class JSONParserConfiguration extends ParserConfiguration { + /** * Used to indicate whether to overwrite duplicate key or not. */ private boolean overwriteDuplicateKey; + /** + * This flag, when set to true, instructs the parser to 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. + */ + private boolean strictMode; + /** * Configuration with the default values. */ @@ -26,10 +34,9 @@ public class JSONParserConfiguration extends ParserConfiguration { } /** - * Defines the maximum nesting depth that the parser will descend before throwing an exception - * when parsing a map into JSONObject or parsing a {@link java.util.Collection} instance into - * JSONArray. The default max nesting depth is 512, which means the parser will throw a JsonException - * if the maximum depth is reached. + * Defines the maximum nesting depth that the parser will descend before throwing an exception when parsing a map + * into JSONObject or parsing a {@link java.util.Collection} instance into JSONArray. The default max nesting depth + * is 512, which means the parser will throw a JsonException if the maximum depth is reached. * * @param maxNestingDepth the maximum nesting depth allowed to the JSON parser * @return The existing configuration will not be modified. A new configuration is returned. @@ -44,9 +51,8 @@ public class JSONParserConfiguration extends ParserConfiguration { } /** - * Controls the parser's behavior when meeting duplicate keys. - * If set to false, the parser will throw a JSONException when meeting a duplicate key. - * Or the duplicate key's value will be overwritten. + * Controls the parser's behavior when meeting duplicate keys. If set to false, the parser will throw a + * JSONException when meeting a duplicate key. Or the duplicate key's value will be overwritten. * * @param overwriteDuplicateKey defines should the parser overwrite duplicate keys. * @return The existing configuration will not be modified. A new configuration is returned. @@ -58,13 +64,45 @@ public class JSONParserConfiguration extends ParserConfiguration { return clone; } + /** - * The parser's behavior when meeting duplicate keys, controls whether the parser should - * overwrite duplicate keys or not. + * 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. * * @return The overwriteDuplicateKey configuration value. */ 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; + } } From 63e8314debb80ab2a887b22523cff081d0a7c7e2 Mon Sep 17 00:00:00 2001 From: rikkarth Date: Fri, 15 Mar 2024 00:45:32 +0000 Subject: [PATCH 02/17] feat(#871-strictMode): strictMode JSONArray initial implementation test(#871-strictMode): initial test implementation --- src/main/java/org/json/JSONArray.java | 1211 +++++++---------- .../junit/JSONParserConfigurationTest.java | 29 +- 2 files changed, 518 insertions(+), 722 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index f86075e..a108a55 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -18,11 +18,10 @@ import java.util.Map; /** - * A JSONArray is an ordered sequence of values. Its external text form is a - * string wrapped in square brackets with commas separating the values. The - * internal form is an object having get and opt - * methods for accessing the values by index, and put methods for - * adding or replacing values. The values can be any of these types: + * A JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with + * commas separating the values. The internal form is an object having get and opt methods for + * accessing the values by index, and put methods for adding or replacing values. The values can be any of + * these types: * Boolean, JSONArray, JSONObject, * Number, String, or the * JSONObject.NULL object. @@ -30,19 +29,17 @@ import java.util.Map; * The constructor can convert a JSON text into a Java object. The * toString method converts to JSON text. *

- * A get method returns a value if one can be found, and throws an - * exception if one cannot be found. An opt method returns a - * default value instead of throwing an exception, and so is useful for - * obtaining optional values. + * A get method returns a value if one can be found, and throws an exception if one cannot be found. An + * opt method returns a default value instead of throwing an exception, and so is useful for obtaining + * optional values. *

- * The generic get() and opt() methods return an - * object which you can cast or query for type. There are also typed + * The generic get() and opt() methods return an object which you can cast or query for type. + * There are also typed * get and opt methods that do type checking and type * coercion for you. *

- * The texts produced by the toString methods strictly conform to - * JSON syntax rules. The constructors are more forgiving in the texts they will - * accept: + * The texts produced by the toString methods strictly conform to JSON syntax rules. The constructors are + * more forgiving in the texts they will accept: *