From e67abb3842abdebc17ccd9ac1eb7c7b83daa6106 Mon Sep 17 00:00:00 2001 From: rikkarth Date: Fri, 15 Mar 2024 22:28:31 +0000 Subject: [PATCH] feat(#871-strictMode): improved validation, strict mode for quotes implementation --- src/main/java/org/json/JSONArray.java | 9 +- src/main/java/org/json/JSONObject.java | 1587 +++++++---------- src/main/java/org/json/JSONTokener.java | 428 +++-- .../junit/JSONParserConfigurationTest.java | 19 +- 4 files changed, 956 insertions(+), 1087 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index a108a55..12b1c39 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -109,7 +109,11 @@ public class JSONArray implements Iterable { this.myArrayList.add(JSONObject.NULL); } else { x.back(); - this.myArrayList.add(x.nextValue()); + if (jsonParserConfiguration.isStrictMode()) { + this.myArrayList.add(x.nextValue(true)); + } else { + this.myArrayList.add(x.nextValue()); + } } switch (x.nextClean()) { case 0: @@ -1693,7 +1697,8 @@ public class JSONArray implements Iterable { * @throws JSONException If not an array or if an array value is non-finite number. * @throws NullPointerException Thrown if the array parameter is null. */ - private void addAll(Object array, boolean wrap, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) + private void addAll(Object array, boolean wrap, int recursionDepth, JSONParserConfiguration + jsonParserConfiguration) throws JSONException { if (array.getClass().isArray()) { int length = Array.getLength(array); diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 8c0e0fc..634226b 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -15,47 +15,51 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.math.BigDecimal; import java.math.BigInteger; -import java.util.*; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.IdentityHashMap; +import java.util.Iterator; +import java.util.Locale; +import java.util.Map; import java.util.Map.Entry; +import java.util.ResourceBundle; +import java.util.Set; import java.util.regex.Pattern; /** - * A JSONObject is an unordered collection of name/value pairs. Its external - * form is a string wrapped in curly braces with colons between the names and - * values, and commas between the values and names. The internal form is an - * object having get and opt methods for accessing - * the values by name, and put methods for adding or replacing - * values by name. The values can be any of these types: Boolean, + * A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces + * with colons between the names and values, and commas between the values and names. The internal form is an object + * having get and opt methods for accessing the values by name, and put methods + * for adding or replacing values by name. The values can be any of these types: Boolean, * JSONArray, JSONObject, Number, * String, or the JSONObject.NULL object. A - * JSONObject constructor can be used to convert an external form JSON text - * into an internal form whose values can be retrieved with the + * JSONObject constructor can be used to convert an external form JSON text into an internal form whose values can be + * retrieved with the * get and opt methods, or to convert values into a * JSON text using the put and toString methods. 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. + * 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 opt methods differ from the get methods in that they - * do not throw. Instead, they return a specified value, such as null. + * coercion for you. The opt methods differ from the get methods in that they do not throw. Instead, they return a + * specified value, such as null. *

- * The put methods add or replace values in an object. For - * example, + * The put methods add or replace values in an object. For example, * *

  * myString = new JSONObject()
  *         .put("JSON", "Hello, World!").toString();
  * 
- * + *

* produces the string {"JSON": "Hello, World"}. *

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