Skip to content

Commit 21f7a94

Browse files
committed
[FEATURE] Global JSONParserConfiguration support
1 parent 896ce0f commit 21f7a94

4 files changed

Lines changed: 51 additions & 12 deletions

File tree

‎src/main/java/org/json/JSONArray.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private static boolean checkForSyntaxError(JSONTokener x, JSONParserConfiguratio
184184
* If there is a syntax error.
185185
*/
186186
public JSONArray(String source) throws JSONException {
187-
this(source, new JSONParserConfiguration());
187+
this(source, JSONParserConfiguration.getInstance());
188188
}
189189

190190
/**
@@ -209,7 +209,7 @@ public JSONArray(String source, JSONParserConfiguration jsonParserConfiguration)
209209
* A Collection.
210210
*/
211211
public JSONArray(Collection<?> collection) {
212-
this(collection, 0, new JSONParserConfiguration());
212+
this(collection, 0, JSONParserConfiguration.getInstance());
213213
}
214214

215215
/**
@@ -1410,7 +1410,7 @@ public JSONArray put(int index, long value) throws JSONException {
14101410
* If a key in the map is <code>null</code>
14111411
*/
14121412
public JSONArray put(int index, Map<?, ?> value) throws JSONException {
1413-
this.put(index, new JSONObject(value, new JSONParserConfiguration()));
1413+
this.put(index, new JSONObject(value, JSONParserConfiguration.getInstance()));
14141414
return this;
14151415
}
14161416

@@ -1967,7 +1967,7 @@ private void addAll(Object array, boolean wrap) throws JSONException {
19671967
* Variable for tracking the count of nested object creations.
19681968
*/
19691969
private void addAll(Object array, boolean wrap, int recursionDepth) {
1970-
addAll(array, wrap, recursionDepth, new JSONParserConfiguration());
1970+
addAll(array, wrap, recursionDepth, JSONParserConfiguration.getInstance());
19711971
}
19721972
/**
19731973
* Add an array's elements to the JSONArray.

‎src/main/java/org/json/JSONObject.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ private static void checkKeyForStrictMode(JSONTokener jsonTokener, JSONParserCon
359359
* If a key in the map is <code>null</code>
360360
*/
361361
public JSONObject(Map<?, ?> m) {
362-
this(m, 0, new JSONParserConfiguration());
362+
this(m, 0, JSONParserConfiguration.getInstance());
363363
}
364364

365365
/**
@@ -460,7 +460,7 @@ private JSONObject(Map<?, ?> m, int recursionDepth, JSONParserConfiguration json
460460
*/
461461
public JSONObject(Object bean) {
462462
this();
463-
this.populateMap(bean, new JSONParserConfiguration());
463+
this.populateMap(bean, JSONParserConfiguration.getInstance());
464464
}
465465

466466
public JSONObject(Object bean, JSONParserConfiguration jsonParserConfiguration) {
@@ -470,7 +470,7 @@ public JSONObject(Object bean, JSONParserConfiguration jsonParserConfiguration)
470470

471471
private JSONObject(Object bean, Set<Object> objectsRecord) {
472472
this();
473-
this.populateMap(bean, objectsRecord, new JSONParserConfiguration());
473+
this.populateMap(bean, objectsRecord, JSONParserConfiguration.getInstance());
474474
}
475475

476476
/**
@@ -513,7 +513,7 @@ public JSONObject(Object object, String ... names) {
513513
* duplicated key.
514514
*/
515515
public JSONObject(String source) throws JSONException {
516-
this(source, new JSONParserConfiguration());
516+
this(source, JSONParserConfiguration.getInstance());
517517
}
518518

519519
/**
@@ -2960,7 +2960,7 @@ static Object wrap(Object object, int recursionDepth, JSONParserConfiguration js
29602960
}
29612961

29622962
private static Object wrap(Object object, Set<Object> objectsRecord) {
2963-
return wrap(object, objectsRecord, 0, new JSONParserConfiguration());
2963+
return wrap(object, objectsRecord, 0, JSONParserConfiguration.getInstance());
29642964
}
29652965

29662966
private static Object wrap(Object object, Set<Object> objectsRecord, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) {

‎src/main/java/org/json/JSONParserConfiguration.java‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,45 @@
44
* Configuration object for the JSON parser. The configuration is immutable.
55
*/
66
public class JSONParserConfiguration extends ParserConfiguration {
7+
8+
/**
9+
* system-wide default configuration.
10+
*/
11+
private static JSONParserConfiguration globalConfig = null;
12+
13+
/**
14+
* Sets the system-wide global JSONParserConfiguration.
15+
* This can only be called once during the application's lifecycle.
16+
*
17+
* @param configuration The configuration to set globally.
18+
* @throws IllegalStateException if the configuration is already set.
19+
* @throws IllegalArgumentException if the provided config is null.
20+
*/
21+
public static void setGlobalConfiguration(JSONParserConfiguration configuration){
22+
if (configuration == null) {
23+
throw new IllegalArgumentException("Global JSONParserConfiguration cannot be null.");
24+
}
25+
if(globalConfig == null){
26+
globalConfig = configuration;
27+
} else {
28+
throw new IllegalStateException("Global JSONParserConfiguration has already been set. It cannot be modified.");
29+
}
30+
}
31+
32+
/**
33+
* Retrieves the system-wide global JSONParserConfiguration.
34+
* If one hasn't been explicitly set, it returns a fresh instance with standard defaults.
35+
*
36+
* @return The active global JSONParserConfiguration.
37+
*/
38+
static JSONParserConfiguration getInstance() {
39+
JSONParserConfiguration config = globalConfig;
40+
if (config == null) {
41+
return new JSONParserConfiguration();
42+
}
43+
return config;
44+
}
45+
746
/**
847
* Used to indicate whether to overwrite duplicate key or not.
948
*/
@@ -73,7 +112,7 @@ public JSONParserConfiguration withOverwriteDuplicateKey(final boolean overwrite
73112

74113
return clone;
75114
}
76-
115+
77116
/**
78117
* Controls the parser's behavior when meeting Java null values while converting maps.
79118
* If set to true, the parser will put a JSONObject.NULL into the resulting JSONObject.

‎src/main/java/org/json/JSONTokener.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class JSONTokener {
4141
* @param reader the source.
4242
*/
4343
public JSONTokener(Reader reader) {
44-
this(reader, new JSONParserConfiguration());
44+
this(reader, JSONParserConfiguration.getInstance());
4545
}
4646

4747
/**
@@ -70,7 +70,7 @@ public JSONTokener(Reader reader, JSONParserConfiguration jsonParserConfiguratio
7070
* @param inputStream The source.
7171
*/
7272
public JSONTokener(InputStream inputStream) {
73-
this(inputStream, new JSONParserConfiguration());
73+
this(inputStream, JSONParserConfiguration.getInstance());
7474
}
7575

7676
/**

0 commit comments

Comments
 (0)