Skip to content

Commit de984a5

Browse files
committed
Refactoring of code so it is optional to parse numbers as either BigDecimal or BigInteger when required
1 parent 18ba9eb commit de984a5

4 files changed

Lines changed: 114 additions & 62 deletions

File tree

HTTPTokener.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package org.json;
22

33
/*
4-
Copyright (c) 2002 JSON.org
4+
Original work Copyright (c) 2002 JSON.org
5+
Modified work Copyright (c) 2019 Isaias Arellano - [email protected]
56
67
Permission is hereby granted, free of charge, to any person obtaining a copy
78
of this software and associated documentation files (the "Software"), to deal
@@ -37,7 +38,23 @@ public class HTTPTokener extends JSONTokener {
3738
* @param string A source string.
3839
*/
3940
public HTTPTokener(String string) {
40-
super(string);
41+
this(string, false, 14);
42+
}
43+
44+
/**
45+
* Construct an HTTPTokener from a string.
46+
* @param string A source string.
47+
*/
48+
public HTTPTokener(String string, boolean bigNumberEnabled) {
49+
this(string, bigNumberEnabled, 14);
50+
}
51+
52+
/**
53+
* Construct an HTTPTokener from a string.
54+
* @param string A source string.
55+
*/
56+
public HTTPTokener(String string, boolean bigNumberEnabled, int bigNumberLength) {
57+
super(string, bigNumberEnabled, bigNumberLength);
4158
}
4259

4360

JSONArray.java

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package org.json;
22

33
/*
4-
Copyright (c) 2002 JSON.org
4+
Original work Copyright (c) 2002 JSON.org
5+
Modified work Copyright (c) 2019 Isaias Arellano - [email protected]
56
67
Permission is hereby granted, free of charge, to any person obtaining a copy
78
of this software and associated documentation files (the "Software"), to deal
@@ -88,13 +89,22 @@ public class JSONArray implements Iterable<Object> {
8889
*/
8990
private final ArrayList<Object> myArrayList;
9091

92+
private int bigNumberLength = 14;
93+
9194
/**
9295
* Construct an empty JSONArray.
9396
*/
9497
public JSONArray() {
9598
this.myArrayList = new ArrayList<Object>();
9699
}
97100

101+
102+
public JSONArray(int bigNumberLength) {
103+
this();
104+
this.bigNumberLength = bigNumberLength;
105+
106+
}
107+
98108
/**
99109
* Construct a JSONArray from a JSONTokener.
100110
*
@@ -103,8 +113,8 @@ public JSONArray() {
103113
* @throws JSONException
104114
* If there is a syntax error.
105115
*/
106-
public JSONArray(JSONTokener x) throws JSONException {
107-
this();
116+
public JSONArray(JSONTokener x, int bigNumberLength) throws JSONException {
117+
this(bigNumberLength);
108118
if (x.nextClean() != '[') {
109119
throw x.syntaxError("A JSONArray text must start with '['");
110120
}
@@ -148,6 +158,32 @@ public JSONArray(JSONTokener x) throws JSONException {
148158
}
149159
}
150160

161+
/**
162+
* Construct a JSONArray from a JSONTokener.
163+
*
164+
* @param x
165+
* A JSONTokener
166+
* @throws JSONException
167+
* If there is a syntax error.
168+
*/
169+
public JSONArray(JSONTokener x) throws JSONException {
170+
this(x, 14);
171+
}
172+
173+
/**
174+
* Construct a JSONArray from a source JSON text.
175+
*
176+
* @param source
177+
* A string that begins with <code>[</code>&nbsp;<small>(left
178+
* bracket)</small> and ends with <code>]</code>
179+
* &nbsp;<small>(right bracket)</small>.
180+
* @throws JSONException
181+
* If there is a syntax error.
182+
*/
183+
public JSONArray(String source, boolean bigNumberEnabled, int bigNumberLength) throws JSONException {
184+
this(new JSONTokener(source, bigNumberEnabled, bigNumberLength), bigNumberLength);
185+
}
186+
151187
/**
152188
* Construct a JSONArray from a source JSON text.
153189
*
@@ -159,7 +195,7 @@ public JSONArray(JSONTokener x) throws JSONException {
159195
* If there is a syntax error.
160196
*/
161197
public JSONArray(String source) throws JSONException {
162-
this(new JSONTokener(source));
198+
this(new JSONTokener(source), 14);
163199
}
164200

165201
/**
@@ -296,7 +332,7 @@ public Number getNumber(int index) throws JSONException {
296332
if (object instanceof Number) {
297333
return (Number)object;
298334
}
299-
return JSONObject.stringToNumber(object.toString());
335+
return JSONObject.stringToNumber(object.toString(), bigNumberLength);
300336
} catch (Exception e) {
301337
throw new JSONException("JSONArray[" + index + "] is not a number.", e);
302338
}
@@ -826,7 +862,7 @@ public Number optNumber(int index, Number defaultValue) {
826862

827863
if (val instanceof String) {
828864
try {
829-
return JSONObject.stringToNumber((String) val);
865+
return JSONObject.stringToNumber((String) val, bigNumberLength);
830866
} catch (Exception e) {
831867
return defaultValue;
832868
}

JSONObject.java

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,6 @@ public String toString() {
172172
*/
173173
public static final Object NULL = new Null();
174174

175-
/**
176-
* Indicates if BigNumber is to be used
177-
*/
178-
private boolean bigNumberEnabled = false;
179-
180175
/**
181176
* Parses number token as BigNumber is token length exceeds this value
182177
*/
@@ -220,17 +215,14 @@ public JSONObject(JSONObject jo, String[] names) {
220215
*
221216
* @param x
222217
* A JSONTokener object containing the source string.
223-
* @param bigNumberEnabled
224-
* If numbers should be attempted to be parsed as BigNumbers
225218
* @param bigNumberLength
226219
* Token length when it should be considered as BigNumber
227220
* @throws JSONException
228221
* If there is a syntax error in the source string or a
229222
* duplicated key.
230223
*/
231-
public JSONObject(JSONTokener x, boolean bigNumberEnabled, int bigNumberLength) throws JSONException {
224+
public JSONObject(JSONTokener x, int bigNumberLength) throws JSONException {
232225
this();
233-
this.bigNumberEnabled = bigNumberEnabled;
234226
this.bigNumberLength = bigNumberLength;
235227
char c;
236228
String key;
@@ -293,31 +285,16 @@ public JSONObject(JSONTokener x, boolean bigNumberEnabled, int bigNumberLength)
293285
/**
294286
* Construct a JSONObject from a JSONTokener.
295287
*
288+
* Using this constructor does not enable BigNumber support.
289+
*
296290
* @param x
297291
* A JSONTokener object containing the source string.
298-
* @param bigNumberEnabled
299-
* If numbers should be attempted to be parsed as BigNumbers.
300-
* If the, the default legnth of token to be considered as BigNumber is 14
301292
* @throws JSONException
302293
* If there is a syntax error in the source string or a
303294
* duplicated key.
304295
*/
305-
public JSONObject(JSONTokener x, boolean bigNumberEnabled) throws JSONException {
306-
this(x, bigNumberEnabled, 14);
307-
}
308-
/**
309-
* Construct a JSONObject from a JSONTokener.
310-
*
311-
* Using this constructor does not enable BigNumber support.
312-
*
313-
* @param x
314-
* A JSONTokener object containing the source string.
315-
* @throws JSONException
316-
* If there is a syntax error in the source string or a
317-
* duplicated key.
318-
*/
319296
public JSONObject(JSONTokener x) throws JSONException {
320-
this(x, false, 14);
297+
this(x, 14);
321298
}
322299

323300
/**
@@ -454,7 +431,7 @@ public JSONObject(Object object, String names[]) {
454431
* duplicated key.
455432
*/
456433
public JSONObject(String source, boolean bigNumberEnabled, int bigNumberLength) throws JSONException {
457-
this(new JSONTokener(source), bigNumberEnabled, bigNumberLength);
434+
this(new JSONTokener(source, bigNumberEnabled, bigNumberLength), bigNumberLength);
458435
}
459436

460437
/**
@@ -472,7 +449,7 @@ public JSONObject(String source, boolean bigNumberEnabled, int bigNumberLength)
472449
* duplicated key.
473450
*/
474451
public JSONObject(String source, boolean bigNumberEnabled) throws JSONException {
475-
this(new JSONTokener(source), bigNumberEnabled, 14);
452+
this(new JSONTokener(source, bigNumberEnabled, 14), 14);
476453
}
477454
/**
478455
* Construct a JSONObject from a source JSON text string. This is the most
@@ -490,7 +467,7 @@ public JSONObject(String source, boolean bigNumberEnabled) throws JSONException
490467
* duplicated key.
491468
*/
492469
public JSONObject(String source) throws JSONException {
493-
this(new JSONTokener(source), false, 14);
470+
this(new JSONTokener(source, false, 14), 14);
494471
}
495472

496473
/**
@@ -573,12 +550,12 @@ public JSONObject accumulate(String key, Object value) throws JSONException {
573550
Object object = this.opt(key);
574551
if (object == null) {
575552
this.put(key,
576-
value instanceof JSONArray ? new JSONArray().put(value)
553+
value instanceof JSONArray ? new JSONArray(this.bigNumberLength).put(value)
577554
: value);
578555
} else if (object instanceof JSONArray) {
579556
((JSONArray) object).put(value);
580557
} else {
581-
this.put(key, new JSONArray().put(object).put(value));
558+
this.put(key, new JSONArray(this.bigNumberLength).put(object).put(value));
582559
}
583560
return this;
584561
}
@@ -604,7 +581,7 @@ public JSONObject append(String key, Object value) throws JSONException {
604581
testValidity(value);
605582
Object object = this.opt(key);
606583
if (object == null) {
607-
this.put(key, new JSONArray().put(value));
584+
this.put(key, new JSONArray(this.bigNumberLength).put(value));
608585
} else if (object instanceof JSONArray) {
609586
this.put(key, ((JSONArray) object).put(value));
610587
} else {
@@ -801,7 +778,7 @@ public Number getNumber(String key) throws JSONException {
801778
if (object instanceof Number) {
802779
return (Number)object;
803780
}
804-
return stringToNumber(object.toString());
781+
return stringToNumber(object.toString(), bigNumberLength);
805782
} catch (Exception e) {
806783
throw new JSONException("JSONObject[" + quote(key)
807784
+ "] is not a number.", e);
@@ -1499,7 +1476,7 @@ public Number optNumber(String key, Number defaultValue) {
14991476
}
15001477

15011478
try {
1502-
return stringToNumber(val.toString());
1479+
return stringToNumber(val.toString(), bigNumberLength);
15031480
} catch (Exception e) {
15041481
return defaultValue;
15051482
}
@@ -1682,9 +1659,6 @@ private static <A extends Annotation> A getAnnotation(final Method m, final Clas
16821659
* implementations and interfaces has the annotation. Returns the depth of the
16831660
* annotation in the hierarchy.
16841661
*
1685-
* @param <A>
1686-
* type of the annotation
1687-
*
16881662
* @param m
16891663
* method to check
16901664
* @param annotationClass
@@ -2165,7 +2139,7 @@ protected static boolean isDecimalNotation(final String val) {
21652139
* @throws NumberFormatException thrown if the value is not a valid number. A public
21662140
* caller should catch this and wrap it in a {@link JSONException} if applicable.
21672141
*/
2168-
protected static Number stringToNumber(final String val) throws NumberFormatException {
2142+
protected static Number stringToNumber(final String val, int bigNumberLength) throws NumberFormatException {
21692143
char initial = val.charAt(0);
21702144
if ((initial >= '0' && initial <= '9') || initial == '-') {
21712145
// decimal representation
@@ -2231,7 +2205,7 @@ protected static Number stringToNumber(final String val) throws NumberFormatExce
22312205
*/
22322206
// Changes to this method must be copied to the corresponding method in
22332207
// the XML class to keep full support for Android
2234-
public static Object stringToValue(String string) {
2208+
public static Object stringToValue(String string, boolean bigNumberEnabled, int bigNumberLength) {
22352209
if ("".equals(string)) {
22362210
return string;
22372211
}
@@ -2256,7 +2230,7 @@ public static Object stringToValue(String string) {
22562230
if ((initial >= '0' && initial <= '9') || initial == '-') {
22572231
try {
22582232
if (bigNumberEnabled) {
2259-
return stringToNumber(string);
2233+
return stringToNumber(string, bigNumberLength);
22602234
} else {
22612235
if (isDecimalNotation(string)) {
22622236
Double d = Double.valueOf(string);
@@ -2318,7 +2292,7 @@ public JSONArray toJSONArray(JSONArray names) throws JSONException {
23182292
if (names == null || names.isEmpty()) {
23192293
return null;
23202294
}
2321-
JSONArray ja = new JSONArray();
2295+
JSONArray ja = new JSONArray(this.bigNumberLength);
23222296
for (int i = 0; i < names.length(); i += 1) {
23232297
ja.put(this.opt(names.getString(i)));
23242298
}

0 commit comments

Comments
 (0)