@@ -75,19 +75,31 @@ public JSONArray() {
75
75
}
76
76
77
77
/**
78
- * Construct a JSONArray from a JSONTokener.
78
+ * Constructs a JSONArray from a JSONTokener.
79
+ * <p>
80
+ * This constructor reads the JSONTokener to parse a JSON array. It uses the default JSONParserConfiguration.
79
81
*
80
- * @param x
81
- * A JSONTokener
82
- * @throws JSONException
83
- * If there is a syntax error.
82
+ * @param x A JSONTokener
83
+ * @throws JSONException If there is a syntax error.
84
84
*/
85
85
public JSONArray (JSONTokener x ) throws JSONException {
86
+ this (x , new JSONParserConfiguration ());
87
+ }
88
+
89
+ /**
90
+ * Constructs a JSONArray from a JSONTokener and a JSONParserConfiguration.
91
+ * JSONParserConfiguration contains strictMode turned off (false) by default.
92
+ *
93
+ * @param x A JSONTokener instance from which the JSONArray is constructed.
94
+ * @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
95
+ * @throws JSONException If a syntax error occurs during the construction of the JSONArray.
96
+ */
97
+ public JSONArray (JSONTokener x , JSONParserConfiguration jsonParserConfiguration ) throws JSONException {
86
98
this ();
87
99
if (x .nextClean () != '[' ) {
88
100
throw x .syntaxError ("A JSONArray text must start with '['" );
89
101
}
90
-
102
+
91
103
char nextChar = x .nextClean ();
92
104
if (nextChar == 0 ) {
93
105
// array is unclosed. No ']' found, instead EOF
@@ -101,27 +113,34 @@ public JSONArray(JSONTokener x) throws JSONException {
101
113
this .myArrayList .add (JSONObject .NULL );
102
114
} else {
103
115
x .back ();
104
- this .myArrayList .add (x .nextValue ());
116
+ this .myArrayList .add (x .nextValue (jsonParserConfiguration ));
105
117
}
106
118
switch (x .nextClean ()) {
107
- case 0 :
108
- // array is unclosed. No ']' found, instead EOF
109
- throw x .syntaxError ("Expected a ',' or ']'" );
110
- case ',' :
111
- nextChar = x .nextClean ();
112
- if (nextChar == 0 ) {
119
+ case 0 :
113
120
// array is unclosed. No ']' found, instead EOF
114
121
throw x .syntaxError ("Expected a ',' or ']'" );
115
- }
116
- if (nextChar == ']' ) {
122
+ case ',' :
123
+ nextChar = x .nextClean ();
124
+ if (nextChar == 0 ) {
125
+ // array is unclosed. No ']' found, instead EOF
126
+ throw x .syntaxError ("Expected a ',' or ']'" );
127
+ }
128
+ if (nextChar == ']' ) {
129
+ return ;
130
+ }
131
+ x .back ();
132
+ break ;
133
+ case ']' :
134
+ if (jsonParserConfiguration .isStrictMode ()) {
135
+ nextChar = x .nextClean ();
136
+ if (nextChar != 0 ) {
137
+ throw x .syntaxError ("invalid character found after end of array: " + nextChar );
138
+ }
139
+ }
140
+
117
141
return ;
118
- }
119
- x .back ();
120
- break ;
121
- case ']' :
122
- return ;
123
- default :
124
- throw x .syntaxError ("Expected a ',' or ']'" );
142
+ default :
143
+ throw x .syntaxError ("Expected a ',' or ']'" );
125
144
}
126
145
}
127
146
}
@@ -138,7 +157,19 @@ public JSONArray(JSONTokener x) throws JSONException {
138
157
* If there is a syntax error.
139
158
*/
140
159
public JSONArray (String source ) throws JSONException {
141
- this (new JSONTokener (source ));
160
+ this (new JSONTokener (source ), new JSONParserConfiguration ());
161
+ }
162
+
163
+ /**
164
+ * Constructs a JSONArray from a source JSON text and a JSONParserConfiguration.
165
+ *
166
+ * @param source A string that begins with <code>[</code> <small>(left bracket)</small> and
167
+ * ends with <code>]</code> <small>(right bracket)</small>.
168
+ * @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
169
+ * @throws JSONException If there is a syntax error.
170
+ */
171
+ public JSONArray (String source , JSONParserConfiguration jsonParserConfiguration ) throws JSONException {
172
+ this (new JSONTokener (source ), jsonParserConfiguration );
142
173
}
143
174
144
175
/**
@@ -367,7 +398,7 @@ public Number getNumber(int index) throws JSONException {
367
398
368
399
/**
369
400
* Get the enum value associated with an index.
370
- *
401
+ *
371
402
* @param <E>
372
403
* Enum Type
373
404
* @param clazz
@@ -555,7 +586,7 @@ public String join(String separator) throws JSONException {
555
586
if (len == 0 ) {
556
587
return "" ;
557
588
}
558
-
589
+
559
590
StringBuilder sb = new StringBuilder (
560
591
JSONObject .valueToString (this .myArrayList .get (0 )));
561
592
@@ -869,7 +900,7 @@ public Integer optIntegerObject(int index, Integer defaultValue) {
869
900
870
901
/**
871
902
* Get the enum value associated with a key.
872
- *
903
+ *
873
904
* @param <E>
874
905
* Enum Type
875
906
* @param clazz
@@ -884,7 +915,7 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, int index) {
884
915
885
916
/**
886
917
* Get the enum value associated with a key.
887
- *
918
+ *
888
919
* @param <E>
889
920
* Enum Type
890
921
* @param clazz
@@ -917,8 +948,8 @@ public <E extends Enum<E>> E optEnum(Class<E> clazz, int index, E defaultValue)
917
948
}
918
949
919
950
/**
920
- * Get the optional BigInteger value associated with an index. The
921
- * defaultValue is returned if there is no value for the index, or if the
951
+ * Get the optional BigInteger value associated with an index. The
952
+ * defaultValue is returned if there is no value for the index, or if the
922
953
* value is not a number and cannot be converted to a number.
923
954
*
924
955
* @param index
@@ -933,8 +964,8 @@ public BigInteger optBigInteger(int index, BigInteger defaultValue) {
933
964
}
934
965
935
966
/**
936
- * Get the optional BigDecimal value associated with an index. The
937
- * defaultValue is returned if there is no value for the index, or if the
967
+ * Get the optional BigDecimal value associated with an index. The
968
+ * defaultValue is returned if there is no value for the index, or if the
938
969
* value is not a number and cannot be converted to a number. If the value
939
970
* is float or double, the {@link BigDecimal#BigDecimal(double)}
940
971
* constructor will be used. See notes on the constructor for conversion
@@ -1103,7 +1134,7 @@ public Number optNumber(int index, Number defaultValue) {
1103
1134
if (val instanceof Number ){
1104
1135
return (Number ) val ;
1105
1136
}
1106
-
1137
+
1107
1138
if (val instanceof String ) {
1108
1139
try {
1109
1140
return JSONObject .stringToNumber ((String ) val );
@@ -1180,7 +1211,7 @@ public JSONArray put(Collection<?> value) {
1180
1211
public JSONArray put (double value ) throws JSONException {
1181
1212
return this .put (Double .valueOf (value ));
1182
1213
}
1183
-
1214
+
1184
1215
/**
1185
1216
* Append a float value. This increases the array's length by one.
1186
1217
*
@@ -1435,19 +1466,19 @@ public JSONArray put(int index, Object value) throws JSONException {
1435
1466
*
1436
1467
* @param collection
1437
1468
* A Collection.
1438
- * @return this.
1469
+ * @return this.
1439
1470
*/
1440
1471
public JSONArray putAll (Collection <?> collection ) {
1441
1472
this .addAll (collection , false );
1442
1473
return this ;
1443
1474
}
1444
-
1475
+
1445
1476
/**
1446
1477
* Put an Iterable's elements in to the JSONArray.
1447
1478
*
1448
1479
* @param iter
1449
1480
* An Iterable.
1450
- * @return this.
1481
+ * @return this.
1451
1482
*/
1452
1483
public JSONArray putAll (Iterable <?> iter ) {
1453
1484
this .addAll (iter , false );
@@ -1459,7 +1490,7 @@ public JSONArray putAll(Iterable<?> iter) {
1459
1490
*
1460
1491
* @param array
1461
1492
* A JSONArray.
1462
- * @return this.
1493
+ * @return this.
1463
1494
*/
1464
1495
public JSONArray putAll (JSONArray array ) {
1465
1496
// directly copy the elements from the source array to this one
@@ -1474,7 +1505,7 @@ public JSONArray putAll(JSONArray array) {
1474
1505
* @param array
1475
1506
* Array. If the parameter passed is null, or not an array or Iterable, an
1476
1507
* exception will be thrown.
1477
- * @return this.
1508
+ * @return this.
1478
1509
*
1479
1510
* @throws JSONException
1480
1511
* If not an array, JSONArray, Iterable or if an value is non-finite number.
@@ -1485,17 +1516,17 @@ public JSONArray putAll(Object array) throws JSONException {
1485
1516
this .addAll (array , false );
1486
1517
return this ;
1487
1518
}
1488
-
1519
+
1489
1520
/**
1490
- * Creates a JSONPointer using an initialization string and tries to
1521
+ * Creates a JSONPointer using an initialization string and tries to
1491
1522
* match it to an item within this JSONArray. For example, given a
1492
1523
* JSONArray initialized with this document:
1493
1524
* <pre>
1494
1525
* [
1495
1526
* {"b":"c"}
1496
1527
* ]
1497
1528
* </pre>
1498
- * and this JSONPointer string:
1529
+ * and this JSONPointer string:
1499
1530
* <pre>
1500
1531
* "/0/b"
1501
1532
* </pre>
@@ -1508,17 +1539,17 @@ public JSONArray putAll(Object array) throws JSONException {
1508
1539
public Object query (String jsonPointer ) {
1509
1540
return query (new JSONPointer (jsonPointer ));
1510
1541
}
1511
-
1542
+
1512
1543
/**
1513
- * Uses a user initialized JSONPointer and tries to
1544
+ * Uses a user initialized JSONPointer and tries to
1514
1545
* match it to an item within this JSONArray. For example, given a
1515
1546
* JSONArray initialized with this document:
1516
1547
* <pre>
1517
1548
* [
1518
1549
* {"b":"c"}
1519
1550
* ]
1520
1551
* </pre>
1521
- * and this JSONPointer:
1552
+ * and this JSONPointer:
1522
1553
* <pre>
1523
1554
* "/0/b"
1524
1555
* </pre>
@@ -1531,23 +1562,23 @@ public Object query(String jsonPointer) {
1531
1562
public Object query (JSONPointer jsonPointer ) {
1532
1563
return jsonPointer .queryFrom (this );
1533
1564
}
1534
-
1565
+
1535
1566
/**
1536
1567
* Queries and returns a value from this object using {@code jsonPointer}, or
1537
1568
* returns null if the query fails due to a missing key.
1538
- *
1569
+ *
1539
1570
* @param jsonPointer the string representation of the JSON pointer
1540
1571
* @return the queried value or {@code null}
1541
1572
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
1542
1573
*/
1543
1574
public Object optQuery (String jsonPointer ) {
1544
1575
return optQuery (new JSONPointer (jsonPointer ));
1545
1576
}
1546
-
1577
+
1547
1578
/**
1548
1579
* Queries and returns a value from this object using {@code jsonPointer}, or
1549
1580
* returns null if the query fails due to a missing key.
1550
- *
1581
+ *
1551
1582
* @param jsonPointer The JSON pointer
1552
1583
* @return the queried value or {@code null}
1553
1584
* @throws IllegalArgumentException if {@code jsonPointer} has invalid syntax
@@ -1667,11 +1698,11 @@ public String toString() {
1667
1698
1668
1699
/**
1669
1700
* Make a pretty-printed JSON text of this JSONArray.
1670
- *
1701
+ *
1671
1702
* <p>If <pre> {@code indentFactor > 0}</pre> and the {@link JSONArray} has only
1672
1703
* one element, then the array will be output on a single line:
1673
1704
* <pre>{@code [1]}</pre>
1674
- *
1705
+ *
1675
1706
* <p>If an array has 2 or more elements, then it will be output across
1676
1707
* multiple lines: <pre>{@code
1677
1708
* [
@@ -1683,7 +1714,7 @@ public String toString() {
1683
1714
* <p><b>
1684
1715
* Warning: This method assumes that the data structure is acyclical.
1685
1716
* </b>
1686
- *
1717
+ *
1687
1718
* @param indentFactor
1688
1719
* The number of spaces to add to each level of indentation.
1689
1720
* @return a printable, displayable, transmittable representation of the
@@ -1717,11 +1748,11 @@ public Writer write(Writer writer) throws JSONException {
1717
1748
1718
1749
/**
1719
1750
* Write the contents of the JSONArray as JSON text to a writer.
1720
- *
1751
+ *
1721
1752
* <p>If <pre>{@code indentFactor > 0}</pre> and the {@link JSONArray} has only
1722
1753
* one element, then the array will be output on a single line:
1723
1754
* <pre>{@code [1]}</pre>
1724
- *
1755
+ *
1725
1756
* <p>If an array has 2 or more elements, then it will be output across
1726
1757
* multiple lines: <pre>{@code
1727
1758
* [
@@ -1947,7 +1978,7 @@ private void addAll(Object array, boolean wrap, int recursionDepth, JSONParserCo
1947
1978
"JSONArray initial value should be a string or collection or array." );
1948
1979
}
1949
1980
}
1950
-
1981
+
1951
1982
/**
1952
1983
* Create a new JSONException in a common format for incorrect conversions.
1953
1984
* @param idx index of the item
0 commit comments