propane 3.11.0-java → 4.0.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.mvn/extensions.xml +1 -1
- data/CHANGELOG.md +2 -0
- data/README.md +5 -5
- data/Rakefile +1 -1
- data/lib/propane/app.rb +2 -2
- data/lib/propane/version.rb +1 -1
- data/lib/propane-4.0.0.jar +0 -0
- data/library/slider/slider.rb +1 -1
- data/pom.rb +8 -8
- data/pom.xml +8 -8
- data/propane.gemspec +3 -3
- data/src/main/java/monkstone/ColorUtil.java +1 -1
- data/src/main/java/monkstone/MathToolModule.java +1 -1
- data/src/main/java/monkstone/PropaneLibrary.java +1 -1
- data/src/main/java/monkstone/fastmath/DegLutTables.java +10 -11
- data/src/main/java/monkstone/fastmath/Deglut.java +1 -1
- data/src/main/java/monkstone/filechooser/Chooser.java +1 -1
- data/src/main/java/monkstone/noise/LICENSE +121 -0
- data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +1 -1
- data/src/main/java/monkstone/slider/CustomVerticalSlider.java +1 -1
- data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +1 -1
- data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +1 -1
- data/src/main/java/monkstone/slider/SliderBar.java +1 -1
- data/src/main/java/monkstone/slider/SliderGroup.java +1 -1
- data/src/main/java/monkstone/slider/WheelHandler.java +1 -1
- data/src/main/java/monkstone/vecmath/package-info.java +1 -1
- data/src/main/java/monkstone/vecmath/vec2/Vec2.java +92 -68
- data/src/main/java/monkstone/vecmath/vec3/Vec3.java +1 -1
- data/src/main/java/monkstone/videoevent/CaptureEvent.java +1 -1
- data/src/main/java/monkstone/videoevent/MovieEvent.java +1 -1
- data/src/main/java/monkstone/videoevent/package-info.java +1 -1
- data/src/main/java/processing/awt/PGraphicsJava2D.java +0 -1
- data/src/main/java/processing/awt/PImageAWT.java +2 -4
- data/src/main/java/processing/core/PApplet.java +4 -4
- data/src/main/java/processing/core/PImage.java +3025 -3047
- data/src/main/java/processing/core/PMatrix.java +5 -2
- data/src/main/java/processing/data/DoubleDict.java +72 -43
- data/src/main/java/processing/data/DoubleList.java +6 -2
- data/src/main/java/processing/data/FloatDict.java +744 -756
- data/src/main/java/processing/data/FloatList.java +68 -26
- data/src/main/java/processing/data/IntDict.java +72 -45
- data/src/main/java/processing/data/IntList.java +63 -26
- data/src/main/java/processing/data/JSONArray.java +892 -931
- data/src/main/java/processing/data/JSONObject.java +1169 -1262
- data/src/main/java/processing/data/JSONTokener.java +30 -49
- data/src/main/java/processing/data/LongDict.java +699 -712
- data/src/main/java/processing/data/LongList.java +676 -700
- data/src/main/java/processing/data/Sort.java +1 -0
- data/src/main/java/processing/data/Table.java +4040 -3661
- data/src/main/java/processing/data/TableRow.java +16 -0
- data/src/main/java/processing/data/XML.java +1041 -956
- data/src/main/java/processing/event/TouchEvent.java +1 -1
- data/src/main/java/processing/opengl/FontTexture.java +2 -2
- data/src/main/java/processing/opengl/PGraphicsOpenGL.java +15 -18
- data/src/main/java/processing/opengl/PJOGL.java +2 -2
- data/src/main/java/processing/opengl/PShapeOpenGL.java +23 -24
- data/test/vecmath_spec_test.rb +14 -3
- data/vendors/Rakefile +1 -1
- metadata +9 -8
- data/lib/propane-3.11.0.jar +0 -0
@@ -33,7 +33,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
33
33
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
34
34
|
SOFTWARE.
|
35
35
|
*/
|
36
|
-
|
37
36
|
import java.io.File;
|
38
37
|
import java.io.IOException;
|
39
38
|
import java.io.PrintWriter;
|
@@ -42,6 +41,7 @@ import java.io.StringWriter;
|
|
42
41
|
import java.io.Writer;
|
43
42
|
import java.lang.reflect.Array;
|
44
43
|
import java.util.ArrayList;
|
44
|
+
import java.util.Arrays;
|
45
45
|
|
46
46
|
import processing.core.PApplet;
|
47
47
|
|
@@ -99,116 +99,111 @@ import processing.core.PApplet;
|
|
99
99
|
*/
|
100
100
|
public class JSONArray {
|
101
101
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
this.myArrayList = new ArrayList<>();
|
113
|
-
}
|
114
|
-
|
115
|
-
|
116
|
-
/**
|
117
|
-
* @nowebref
|
118
|
-
*/
|
119
|
-
public JSONArray(Reader reader) {
|
120
|
-
this(new JSONTokener(reader));
|
121
|
-
}
|
122
|
-
|
123
|
-
|
124
|
-
/**
|
125
|
-
* Construct a JSONArray from a JSONTokener.
|
126
|
-
*
|
127
|
-
* @param x A JSONTokener
|
128
|
-
* @throws RuntimeException If there is a syntax error.
|
129
|
-
* @nowebref
|
130
|
-
*/
|
131
|
-
protected JSONArray(JSONTokener x) {
|
132
|
-
this();
|
133
|
-
if (x.nextClean() != '[') {
|
134
|
-
throw new RuntimeException("A JSONArray text must start with '['");
|
102
|
+
/**
|
103
|
+
* The arrayList where the JSONArray's properties are kept.
|
104
|
+
*/
|
105
|
+
private final ArrayList<Object> myArrayList;
|
106
|
+
|
107
|
+
/**
|
108
|
+
* Construct an empty JSONArray.
|
109
|
+
*/
|
110
|
+
public JSONArray() {
|
111
|
+
this.myArrayList = new ArrayList<>();
|
135
112
|
}
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
113
|
+
|
114
|
+
/**
|
115
|
+
* @param reader
|
116
|
+
* @nowebref
|
117
|
+
*/
|
118
|
+
public JSONArray(Reader reader) {
|
119
|
+
this(new JSONTokener(reader));
|
120
|
+
}
|
121
|
+
|
122
|
+
/**
|
123
|
+
* Construct a JSONArray from a JSONTokener.
|
124
|
+
*
|
125
|
+
* @param x A JSONTokener
|
126
|
+
* @throws RuntimeException If there is a syntax error.
|
127
|
+
* @nowebref
|
128
|
+
*/
|
129
|
+
protected JSONArray(JSONTokener x) {
|
130
|
+
this();
|
131
|
+
if (x.nextClean() != '[') {
|
132
|
+
throw new RuntimeException("A JSONArray text must start with '['");
|
145
133
|
}
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
134
|
+
if (x.nextClean() != ']') {
|
135
|
+
x.back();
|
136
|
+
for (;;) {
|
137
|
+
if (x.nextClean() == ',') {
|
138
|
+
x.back();
|
139
|
+
myArrayList.add(JSONObject.NULL);
|
140
|
+
} else {
|
141
|
+
x.back();
|
142
|
+
myArrayList.add(x.nextValue());
|
143
|
+
}
|
144
|
+
switch (x.nextClean()) {
|
145
|
+
case ';', ',' -> {
|
146
|
+
if (x.nextClean() == ']') {
|
147
|
+
return;
|
148
|
+
}
|
149
|
+
x.back();
|
150
|
+
}
|
151
|
+
case ']' -> {
|
152
|
+
return;
|
153
|
+
}
|
154
|
+
default ->
|
155
|
+
throw new RuntimeException("Expected a ',' or ']'");
|
156
|
+
}
|
157
|
+
}
|
158
158
|
}
|
159
|
-
}
|
160
159
|
}
|
161
|
-
}
|
162
160
|
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
161
|
+
/**
|
162
|
+
* @param list
|
163
|
+
* @nowebref
|
164
|
+
*/
|
165
|
+
public JSONArray(IntList list) {
|
166
|
+
myArrayList = new ArrayList<>();
|
167
|
+
for (int item : list.values()) {
|
168
|
+
myArrayList.add(item);
|
169
|
+
}
|
171
170
|
}
|
172
|
-
}
|
173
171
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
172
|
+
/**
|
173
|
+
* @param list
|
174
|
+
* @nowebref
|
175
|
+
*/
|
176
|
+
public JSONArray(FloatList list) {
|
177
|
+
myArrayList = new ArrayList<>();
|
178
|
+
for (float item : list.values()) {
|
179
|
+
myArrayList.add(item);
|
180
|
+
}
|
182
181
|
}
|
183
|
-
}
|
184
182
|
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
myArrayList.add(item);
|
193
|
-
}
|
194
|
-
}
|
195
|
-
|
196
|
-
|
197
|
-
/**
|
198
|
-
* Construct a JSONArray from a source JSON text.
|
199
|
-
* @param source A string that begins with
|
200
|
-
* <code>[</code> <small>(left bracket)</small>
|
201
|
-
* and ends with <code>]</code> <small>(right bracket)</small>.
|
202
|
-
* @return {@code null} if there is a syntax error.
|
203
|
-
*/
|
204
|
-
static public JSONArray parse(String source) {
|
205
|
-
try {
|
206
|
-
return new JSONArray(new JSONTokener(source));
|
207
|
-
} catch (Exception e) {
|
208
|
-
return null;
|
183
|
+
/**
|
184
|
+
* @param list
|
185
|
+
* @nowebref
|
186
|
+
*/
|
187
|
+
public JSONArray(StringList list) {
|
188
|
+
myArrayList = new ArrayList<>();
|
189
|
+
myArrayList.addAll(Arrays.asList(list.values()));
|
209
190
|
}
|
210
|
-
}
|
211
191
|
|
192
|
+
/**
|
193
|
+
* Construct a JSONArray from a source JSON text.
|
194
|
+
*
|
195
|
+
* @param source A string that begins with <code>[</code> <small>(left
|
196
|
+
* bracket)</small>
|
197
|
+
* and ends with <code>]</code> <small>(right bracket)</small>.
|
198
|
+
* @return {@code null} if there is a syntax error.
|
199
|
+
*/
|
200
|
+
static public JSONArray parse(String source) {
|
201
|
+
try {
|
202
|
+
return new JSONArray(new JSONTokener(source));
|
203
|
+
} catch (Exception e) {
|
204
|
+
return null;
|
205
|
+
}
|
206
|
+
}
|
212
207
|
|
213
208
|
// /**
|
214
209
|
// * Construct a JSONArray from a Collection.
|
@@ -223,411 +218,409 @@ public class JSONArray {
|
|
223
218
|
// }
|
224
219
|
// }
|
225
220
|
// }
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
}
|
244
|
-
|
245
|
-
|
246
|
-
/**
|
247
|
-
* Get the optional object value associated with an index.
|
248
|
-
* @param index must be between 0 and length() - 1
|
249
|
-
* @return An object value, or null if there is no
|
250
|
-
* object at that index.
|
251
|
-
*/
|
252
|
-
private Object opt(int index) {
|
253
|
-
if (index < 0 || index >= this.size()) {
|
254
|
-
return null;
|
255
|
-
}
|
256
|
-
return myArrayList.get(index);
|
257
|
-
}
|
258
|
-
|
259
|
-
|
260
|
-
/**
|
261
|
-
* Get the object value associated with an index.
|
262
|
-
* @param index must be between 0 and length() - 1
|
263
|
-
* @return An object value.
|
264
|
-
* @throws RuntimeException If there is no value for the index.
|
265
|
-
*/
|
266
|
-
public Object get(int index) {
|
267
|
-
Object object = opt(index);
|
268
|
-
if (object == null) {
|
269
|
-
throw new RuntimeException("JSONArray[" + index + "] not found.");
|
270
|
-
}
|
271
|
-
return object;
|
272
|
-
}
|
273
|
-
|
274
|
-
|
275
|
-
/**
|
276
|
-
* Get the string associated with an index.
|
277
|
-
*
|
278
|
-
* @webref jsonarray:method
|
279
|
-
* @brief Gets the String value associated with an index
|
280
|
-
* @param index must be between 0 and length() - 1
|
281
|
-
* @return A string value.
|
282
|
-
* @throws RuntimeException If there is no string value for the index.
|
283
|
-
* @see JSONArray#getInt(int)
|
284
|
-
* @see JSONArray#getFloat(int)
|
285
|
-
* @see JSONArray#getBoolean(int)
|
286
|
-
*/
|
287
|
-
public String getString(int index) {
|
288
|
-
Object object = this.get(index);
|
289
|
-
if (object instanceof String) {
|
290
|
-
return (String)object;
|
291
|
-
}
|
292
|
-
throw new RuntimeException("JSONArray[" + index + "] not a string.");
|
293
|
-
}
|
294
|
-
|
295
|
-
|
296
|
-
/**
|
297
|
-
* Get the optional string associated with an index.
|
298
|
-
* The defaultValue is returned if the key is not found.
|
299
|
-
*
|
300
|
-
* @param index The index must be between 0 and length() - 1.
|
301
|
-
* @param defaultValue The default value.
|
302
|
-
* @return A String value.
|
303
|
-
*/
|
304
|
-
public String getString(int index, String defaultValue) {
|
305
|
-
Object object = this.opt(index);
|
306
|
-
return JSONObject.NULL.equals(object) ? defaultValue : object.toString();
|
307
|
-
}
|
308
|
-
|
309
|
-
|
310
|
-
/**
|
311
|
-
* Get the int value associated with an index.
|
312
|
-
*
|
313
|
-
* @webref jsonarray:method
|
314
|
-
* @brief Gets the int value associated with an index
|
315
|
-
* @param index must be between 0 and length() - 1
|
316
|
-
* @return The value.
|
317
|
-
* @throws RuntimeException If the key is not found or if the value is not a number.
|
318
|
-
* @see JSONArray#getFloat(int)
|
319
|
-
* @see JSONArray#getString(int)
|
320
|
-
* @see JSONArray#getBoolean(int)
|
321
|
-
*/
|
322
|
-
public int getInt(int index) {
|
323
|
-
Object object = this.get(index);
|
324
|
-
try {
|
325
|
-
return object instanceof Number
|
326
|
-
? ((Number)object).intValue()
|
327
|
-
: Integer.parseInt((String)object);
|
328
|
-
} catch (Exception e) {
|
329
|
-
throw new RuntimeException("JSONArray[" + index + "] is not a number.");
|
330
|
-
}
|
331
|
-
}
|
332
|
-
|
333
|
-
|
334
|
-
/**
|
335
|
-
* Get the optional int value associated with an index.
|
336
|
-
* The defaultValue is returned if there is no value for the index,
|
337
|
-
* or if the value is not a number and cannot be converted to a number.
|
338
|
-
* @param index The index must be between 0 and length() - 1.
|
339
|
-
* @param defaultValue The default value.
|
340
|
-
* @return The value.
|
341
|
-
*/
|
342
|
-
public int getInt(int index, int defaultValue) {
|
343
|
-
try {
|
344
|
-
return getInt(index);
|
345
|
-
} catch (Exception e) {
|
346
|
-
return defaultValue;
|
221
|
+
// TODO not decided whether we keep this one, but used heavily by JSONObject
|
222
|
+
/**
|
223
|
+
* Construct a JSONArray from an array
|
224
|
+
*
|
225
|
+
* @param array
|
226
|
+
* @throws RuntimeException If not an array.
|
227
|
+
*/
|
228
|
+
protected JSONArray(Object array) {
|
229
|
+
this();
|
230
|
+
if (array.getClass().isArray()) {
|
231
|
+
int length = Array.getLength(array);
|
232
|
+
for (int i = 0; i < length; i += 1) {
|
233
|
+
this.append(JSONObject.wrap(Array.get(array, i)));
|
234
|
+
}
|
235
|
+
} else {
|
236
|
+
throw new RuntimeException("JSONArray initial value should be a string or collection or array.");
|
237
|
+
}
|
347
238
|
}
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
Object object = this.get(index);
|
361
|
-
try {
|
362
|
-
return object instanceof Number
|
363
|
-
? ((Number)object).longValue()
|
364
|
-
: Long.parseLong((String)object);
|
365
|
-
} catch (Exception e) {
|
366
|
-
throw new RuntimeException("JSONArray[" + index + "] is not a number.");
|
239
|
+
|
240
|
+
/**
|
241
|
+
* Get the optional object value associated with an index.
|
242
|
+
*
|
243
|
+
* @param index must be between 0 and length() - 1
|
244
|
+
* @return An object value, or null if there is no object at that index.
|
245
|
+
*/
|
246
|
+
private Object opt(int index) {
|
247
|
+
if (index < 0 || index >= this.size()) {
|
248
|
+
return null;
|
249
|
+
}
|
250
|
+
return myArrayList.get(index);
|
367
251
|
}
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
} catch (Exception e) {
|
383
|
-
return defaultValue;
|
252
|
+
|
253
|
+
/**
|
254
|
+
* Get the object value associated with an index.
|
255
|
+
*
|
256
|
+
* @param index must be between 0 and length() - 1
|
257
|
+
* @return An object value.
|
258
|
+
* @throws RuntimeException If there is no value for the index.
|
259
|
+
*/
|
260
|
+
public Object get(int index) {
|
261
|
+
Object object = opt(index);
|
262
|
+
if (object == null) {
|
263
|
+
throw new RuntimeException("JSONArray[" + index + "] not found.");
|
264
|
+
}
|
265
|
+
return object;
|
384
266
|
}
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
public float getFloat(int index, float defaultValue) {
|
405
|
-
try {
|
406
|
-
return getFloat(index);
|
407
|
-
} catch (Exception e) {
|
408
|
-
return defaultValue;
|
267
|
+
|
268
|
+
/**
|
269
|
+
* Get the string associated with an index.
|
270
|
+
*
|
271
|
+
* @webref jsonarray:method
|
272
|
+
* @brief Gets the String value associated with an index
|
273
|
+
* @param index must be between 0 and length() - 1
|
274
|
+
* @return A string value.
|
275
|
+
* @throws RuntimeException If there is no string value for the index.
|
276
|
+
* @see JSONArray#getInt(int)
|
277
|
+
* @see JSONArray#getFloat(int)
|
278
|
+
* @see JSONArray#getBoolean(int)
|
279
|
+
*/
|
280
|
+
public String getString(int index) {
|
281
|
+
Object object = this.get(index);
|
282
|
+
if (object instanceof String string) {
|
283
|
+
return string;
|
284
|
+
}
|
285
|
+
throw new RuntimeException("JSONArray[" + index + "] not a string.");
|
409
286
|
}
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
Object object = this.get(index);
|
423
|
-
try {
|
424
|
-
return object instanceof Number
|
425
|
-
? ((Number)object).doubleValue()
|
426
|
-
: Double.parseDouble((String)object);
|
427
|
-
} catch (Exception e) {
|
428
|
-
throw new RuntimeException("JSONArray[" + index + "] is not a number.");
|
287
|
+
|
288
|
+
/**
|
289
|
+
* Get the optional string associated with an index. The defaultValue is
|
290
|
+
* returned if the key is not found.
|
291
|
+
*
|
292
|
+
* @param index The index must be between 0 and length() - 1.
|
293
|
+
* @param defaultValue The default value.
|
294
|
+
* @return A String value.
|
295
|
+
*/
|
296
|
+
public String getString(int index, String defaultValue) {
|
297
|
+
Object object = this.opt(index);
|
298
|
+
return JSONObject.NULL.equals(object) ? defaultValue : object.toString();
|
429
299
|
}
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
300
|
+
|
301
|
+
/**
|
302
|
+
* Get the int value associated with an index.
|
303
|
+
*
|
304
|
+
* @webref jsonarray:method
|
305
|
+
* @brief Gets the int value associated with an index
|
306
|
+
* @param index must be between 0 and length() - 1
|
307
|
+
* @return The value.
|
308
|
+
* @throws RuntimeException If the key is not found or if the value is not a
|
309
|
+
* number.
|
310
|
+
* @see JSONArray#getFloat(int)
|
311
|
+
* @see JSONArray#getString(int)
|
312
|
+
* @see JSONArray#getBoolean(int)
|
313
|
+
*/
|
314
|
+
public int getInt(int index) {
|
315
|
+
Object object = this.get(index);
|
316
|
+
try {
|
317
|
+
return object instanceof Number
|
318
|
+
? ((Number) object).intValue()
|
319
|
+
: Integer.parseInt((String) object);
|
320
|
+
} catch (NumberFormatException e) {
|
321
|
+
throw new RuntimeException("JSONArray[" + index + "] is not a number.");
|
322
|
+
}
|
447
323
|
}
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
*/
|
465
|
-
public boolean getBoolean(int index) {
|
466
|
-
Object object = this.get(index);
|
467
|
-
if (object.equals(Boolean.FALSE) ||
|
468
|
-
(object instanceof String &&
|
469
|
-
((String)object).equalsIgnoreCase("false"))) {
|
470
|
-
return false;
|
471
|
-
} else if (object.equals(Boolean.TRUE) ||
|
472
|
-
(object instanceof String &&
|
473
|
-
((String)object).equalsIgnoreCase("true"))) {
|
474
|
-
return true;
|
324
|
+
|
325
|
+
/**
|
326
|
+
* Get the optional int value associated with an index. The defaultValue is
|
327
|
+
* returned if there is no value for the index, or if the value is not a
|
328
|
+
* number and cannot be converted to a number.
|
329
|
+
*
|
330
|
+
* @param index The index must be between 0 and length() - 1.
|
331
|
+
* @param defaultValue The default value.
|
332
|
+
* @return The value.
|
333
|
+
*/
|
334
|
+
public int getInt(int index, int defaultValue) {
|
335
|
+
try {
|
336
|
+
return getInt(index);
|
337
|
+
} catch (Exception e) {
|
338
|
+
return defaultValue;
|
339
|
+
}
|
475
340
|
}
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
341
|
+
|
342
|
+
/**
|
343
|
+
* Get the long value associated with an index.
|
344
|
+
*
|
345
|
+
* @param index The index must be between 0 and length() - 1
|
346
|
+
* @return The value.
|
347
|
+
* @throws RuntimeException If the key is not found or if the value cannot
|
348
|
+
* be converted to a number.
|
349
|
+
*/
|
350
|
+
public long getLong(int index) {
|
351
|
+
Object object = this.get(index);
|
352
|
+
try {
|
353
|
+
return object instanceof Number
|
354
|
+
? ((Number) object).longValue()
|
355
|
+
: Long.parseLong((String) object);
|
356
|
+
} catch (NumberFormatException e) {
|
357
|
+
throw new RuntimeException("JSONArray[" + index + "] is not a number.");
|
358
|
+
}
|
494
359
|
}
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
public JSONArray getJSONArray(int index) {
|
512
|
-
Object object = this.get(index);
|
513
|
-
if (object instanceof JSONArray) {
|
514
|
-
return (JSONArray)object;
|
360
|
+
|
361
|
+
/**
|
362
|
+
* Get the optional long value associated with an index. The defaultValue is
|
363
|
+
* returned if there is no value for the index, or if the value is not a
|
364
|
+
* number and cannot be converted to a number.
|
365
|
+
*
|
366
|
+
* @param index The index must be between 0 and length() - 1.
|
367
|
+
* @param defaultValue The default value.
|
368
|
+
* @return The value.
|
369
|
+
*/
|
370
|
+
public long getLong(int index, long defaultValue) {
|
371
|
+
try {
|
372
|
+
return this.getLong(index);
|
373
|
+
} catch (Exception e) {
|
374
|
+
return defaultValue;
|
375
|
+
}
|
515
376
|
}
|
516
|
-
throw new RuntimeException("JSONArray[" + index + "] is not a JSONArray.");
|
517
|
-
}
|
518
377
|
|
378
|
+
/**
|
379
|
+
* Get a value from an index as a float.JSON uses 'double' values
|
380
|
+
* internally, so this is simply getDouble() cast to a float.
|
381
|
+
*
|
382
|
+
* @return
|
383
|
+
* @webref jsonarray:method
|
384
|
+
* @brief Gets the float value associated with an index
|
385
|
+
* @param index must be between 0 and length() - 1
|
386
|
+
* @see JSONArray#getInt(int)
|
387
|
+
* @see JSONArray#getString(int)
|
388
|
+
* @see JSONArray#getBoolean(int)
|
389
|
+
*/
|
390
|
+
public float getFloat(int index) {
|
391
|
+
return (float) getDouble(index);
|
392
|
+
}
|
519
393
|
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
394
|
+
public float getFloat(int index, float defaultValue) {
|
395
|
+
try {
|
396
|
+
return getFloat(index);
|
397
|
+
} catch (Exception e) {
|
398
|
+
return defaultValue;
|
399
|
+
}
|
525
400
|
}
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
if (object instanceof JSONObject) {
|
545
|
-
return (JSONObject)object;
|
401
|
+
|
402
|
+
/**
|
403
|
+
* Get the double value associated with an index.
|
404
|
+
*
|
405
|
+
* @param index must be between 0 and length() - 1
|
406
|
+
* @return The value.
|
407
|
+
* @throws RuntimeException If the key is not found or if the value cannot
|
408
|
+
* be converted to a number.
|
409
|
+
*/
|
410
|
+
public double getDouble(int index) {
|
411
|
+
Object object = this.get(index);
|
412
|
+
try {
|
413
|
+
return object instanceof Number
|
414
|
+
? ((Number) object).doubleValue()
|
415
|
+
: Double.parseDouble((String) object);
|
416
|
+
} catch (NumberFormatException e) {
|
417
|
+
throw new RuntimeException("JSONArray[" + index + "] is not a number.");
|
418
|
+
}
|
546
419
|
}
|
547
|
-
throw new RuntimeException("JSONArray[" + index + "] is not a JSONObject.");
|
548
|
-
}
|
549
420
|
|
421
|
+
/**
|
422
|
+
* Get the optional double value associated with an index. The defaultValue
|
423
|
+
* is returned if there is no value for the index, or if the value is not a
|
424
|
+
* number and cannot be converted to a number.
|
425
|
+
*
|
426
|
+
* @param index subscript
|
427
|
+
* @param defaultValue The default value.
|
428
|
+
* @return The value.
|
429
|
+
*/
|
430
|
+
public double getDouble(int index, double defaultValue) {
|
431
|
+
try {
|
432
|
+
return this.getDouble(index);
|
433
|
+
} catch (Exception e) {
|
434
|
+
return defaultValue;
|
435
|
+
}
|
436
|
+
}
|
550
437
|
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
438
|
+
/**
|
439
|
+
* Get the boolean value associated with an index. The string values "true"
|
440
|
+
* and "false" are converted to boolean.
|
441
|
+
*
|
442
|
+
* @webref jsonarray:method
|
443
|
+
* @brief Gets the boolean value associated with an index
|
444
|
+
* @param index must be between 0 and length() - 1
|
445
|
+
* @return The truth.
|
446
|
+
* @throws RuntimeException If there is no value for the index or if the
|
447
|
+
* value is not convertible to boolean.
|
448
|
+
* @see JSONArray#getInt(int)
|
449
|
+
* @see JSONArray#getFloat(int)
|
450
|
+
* @see JSONArray#getString(int)
|
451
|
+
*/
|
452
|
+
public boolean getBoolean(int index) {
|
453
|
+
Object object = this.get(index);
|
454
|
+
if (object.equals(Boolean.FALSE)
|
455
|
+
|| (object instanceof String
|
456
|
+
&& ((String) object).equalsIgnoreCase("false"))) {
|
457
|
+
return false;
|
458
|
+
} else if (object.equals(Boolean.TRUE)
|
459
|
+
|| (object instanceof String
|
460
|
+
&& ((String) object).equalsIgnoreCase("true"))) {
|
461
|
+
return true;
|
462
|
+
}
|
463
|
+
throw new RuntimeException("JSONArray[" + index + "] is not a boolean.");
|
556
464
|
}
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
465
|
+
|
466
|
+
/**
|
467
|
+
* Get the optional boolean value associated with an index. It returns the
|
468
|
+
* defaultValue if there is no value at that index or if it is not a Boolean
|
469
|
+
* or the String "true" or "false" (case insensitive).
|
470
|
+
*
|
471
|
+
* @param index The index must be between 0 and length() - 1.
|
472
|
+
* @param defaultValue A boolean default.
|
473
|
+
* @return The truth.
|
474
|
+
*/
|
475
|
+
public boolean getBoolean(int index, boolean defaultValue) {
|
476
|
+
try {
|
477
|
+
return getBoolean(index);
|
478
|
+
} catch (Exception e) {
|
479
|
+
return defaultValue;
|
480
|
+
}
|
571
481
|
}
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
482
|
+
|
483
|
+
/**
|
484
|
+
* Get the JSONArray associated with an index.
|
485
|
+
*
|
486
|
+
* @webref jsonobject:method
|
487
|
+
* @brief Gets the JSONArray associated with an index value
|
488
|
+
* @param index must be between 0 and length() - 1
|
489
|
+
* @return A JSONArray value.
|
490
|
+
* @throws RuntimeException If there is no value for the index. or if the
|
491
|
+
* value is not a JSONArray
|
492
|
+
* @see JSONArray#getJSONObject(int)
|
493
|
+
* @see JSONArray#setJSONObject(int, JSONObject)
|
494
|
+
* @see JSONArray#setJSONArray(int, JSONArray)
|
495
|
+
*/
|
496
|
+
public JSONArray getJSONArray(int index) {
|
497
|
+
Object object = this.get(index);
|
498
|
+
if (object instanceof JSONArray jSONArray) {
|
499
|
+
return jSONArray;
|
500
|
+
}
|
501
|
+
throw new RuntimeException("JSONArray[" + index + "] is not a JSONArray.");
|
587
502
|
}
|
588
|
-
return outgoing;
|
589
|
-
}
|
590
503
|
|
504
|
+
public JSONArray getJSONArray(int index, JSONArray defaultValue) {
|
505
|
+
try {
|
506
|
+
return getJSONArray(index);
|
507
|
+
} catch (Exception e) {
|
508
|
+
return defaultValue;
|
509
|
+
}
|
510
|
+
}
|
591
511
|
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
512
|
+
/**
|
513
|
+
* Get the JSONObject associated with an index.
|
514
|
+
*
|
515
|
+
* @webref jsonobject:method
|
516
|
+
* @brief Gets the JSONObject associated with an index value
|
517
|
+
* @param index the index value of the object to get
|
518
|
+
* @return A JSONObject value.
|
519
|
+
* @throws RuntimeException If there is no value for the index or if the
|
520
|
+
* value is not a JSONObject
|
521
|
+
* @see JSONArray#getJSONArray(int)
|
522
|
+
* @see JSONArray#setJSONObject(int, JSONObject)
|
523
|
+
* @see JSONArray#setJSONArray(int, JSONArray)
|
524
|
+
*/
|
525
|
+
public JSONObject getJSONObject(int index) {
|
526
|
+
Object object = this.get(index);
|
527
|
+
if (object instanceof JSONObject jSONObject) {
|
528
|
+
return jSONObject;
|
529
|
+
}
|
530
|
+
throw new RuntimeException("JSONArray[" + index + "] is not a JSONObject.");
|
597
531
|
}
|
598
|
-
return outgoing;
|
599
|
-
}
|
600
532
|
|
533
|
+
public JSONObject getJSONObject(int index, JSONObject defaultValue) {
|
534
|
+
try {
|
535
|
+
return getJSONObject(index);
|
536
|
+
} catch (Exception e) {
|
537
|
+
return defaultValue;
|
538
|
+
}
|
539
|
+
}
|
601
540
|
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
541
|
+
/**
|
542
|
+
* Get this entire array as a String array.
|
543
|
+
*
|
544
|
+
* @return
|
545
|
+
* @webref jsonarray:method
|
546
|
+
* @brief Gets the entire array as an array of Strings
|
547
|
+
* @see JSONArray#getIntArray()
|
548
|
+
*/
|
549
|
+
public String[] getStringArray() {
|
550
|
+
String[] outgoing = new String[size()];
|
551
|
+
for (int i = 0; i < size(); i++) {
|
552
|
+
outgoing[i] = getString(i);
|
553
|
+
}
|
554
|
+
return outgoing;
|
607
555
|
}
|
608
|
-
return outgoing;
|
609
|
-
}
|
610
556
|
|
557
|
+
/**
|
558
|
+
* Get this entire array as an int array.Everything must be an int.
|
559
|
+
*
|
560
|
+
* @return
|
561
|
+
* @webref jsonarray:method
|
562
|
+
* @brief Gets the entire array as array of ints
|
563
|
+
* @see JSONArray#getStringArray()
|
564
|
+
*/
|
565
|
+
public int[] getIntArray() {
|
566
|
+
int[] outgoing = new int[size()];
|
567
|
+
for (int i = 0; i < size(); i++) {
|
568
|
+
outgoing[i] = getInt(i);
|
569
|
+
}
|
570
|
+
return outgoing;
|
571
|
+
}
|
611
572
|
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
573
|
+
/**
|
574
|
+
* * Get this entire array as a long array.Everything must be an long.
|
575
|
+
*
|
576
|
+
* @return
|
577
|
+
*/
|
578
|
+
public long[] getLongArray() {
|
579
|
+
long[] outgoing = new long[size()];
|
580
|
+
for (int i = 0; i < size(); i++) {
|
581
|
+
outgoing[i] = getLong(i);
|
582
|
+
}
|
583
|
+
return outgoing;
|
617
584
|
}
|
618
|
-
return outgoing;
|
619
|
-
}
|
620
585
|
|
586
|
+
/**
|
587
|
+
* * Get this entire array as a float array.Everything must be an float.
|
588
|
+
*
|
589
|
+
* @return
|
590
|
+
*/
|
591
|
+
public float[] getFloatArray() {
|
592
|
+
float[] outgoing = new float[size()];
|
593
|
+
for (int i = 0; i < size(); i++) {
|
594
|
+
outgoing[i] = getFloat(i);
|
595
|
+
}
|
596
|
+
return outgoing;
|
597
|
+
}
|
621
598
|
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
599
|
+
/**
|
600
|
+
* * Get this entire array as a double array.Everything must be an double.
|
601
|
+
*
|
602
|
+
* @return
|
603
|
+
*/
|
604
|
+
public double[] getDoubleArray() {
|
605
|
+
double[] outgoing = new double[size()];
|
606
|
+
for (int i = 0; i < size(); i++) {
|
607
|
+
outgoing[i] = getDouble(i);
|
608
|
+
}
|
609
|
+
return outgoing;
|
627
610
|
}
|
628
|
-
return outgoing;
|
629
|
-
}
|
630
611
|
|
612
|
+
/**
|
613
|
+
* * Get this entire array as a boolean array.Everything must be a boolean.
|
614
|
+
*
|
615
|
+
* @return
|
616
|
+
*/
|
617
|
+
public boolean[] getBooleanArray() {
|
618
|
+
boolean[] outgoing = new boolean[size()];
|
619
|
+
for (int i = 0; i < size(); i++) {
|
620
|
+
outgoing[i] = getBoolean(i);
|
621
|
+
}
|
622
|
+
return outgoing;
|
623
|
+
}
|
631
624
|
|
632
625
|
// /**
|
633
626
|
// * Get the optional boolean value associated with an index.
|
@@ -692,89 +685,81 @@ public class JSONArray {
|
|
692
685
|
// public String optString(int index) {
|
693
686
|
// return this.optString(index, "");
|
694
687
|
// }
|
688
|
+
/**
|
689
|
+
* Append an String value. This increases the array's length by one.
|
690
|
+
*
|
691
|
+
* @webref jsonarray:method
|
692
|
+
* @brief Appends a value, increasing the array's length by one
|
693
|
+
* @param value a String value
|
694
|
+
* @return this.
|
695
|
+
* @see JSONArray#size()
|
696
|
+
* @see JSONArray#remove(int)
|
697
|
+
*/
|
698
|
+
public JSONArray append(String value) {
|
699
|
+
this.append((Object) value);
|
700
|
+
return this;
|
701
|
+
}
|
695
702
|
|
703
|
+
/**
|
704
|
+
* Append an int value. This increases the array's length by one.
|
705
|
+
*
|
706
|
+
* @param value an int value
|
707
|
+
* @return this.
|
708
|
+
*/
|
709
|
+
public JSONArray append(int value) {
|
710
|
+
this.append(Integer.valueOf(value));
|
711
|
+
return this;
|
712
|
+
}
|
696
713
|
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
this.append((Object)value);
|
709
|
-
return this;
|
710
|
-
}
|
711
|
-
|
712
|
-
|
713
|
-
/**
|
714
|
-
* Append an int value. This increases the array's length by one.
|
715
|
-
*
|
716
|
-
* @param value an int value
|
717
|
-
* @return this.
|
718
|
-
*/
|
719
|
-
public JSONArray append(int value) {
|
720
|
-
this.append(Integer.valueOf(value));
|
721
|
-
return this;
|
722
|
-
}
|
723
|
-
|
724
|
-
|
725
|
-
/**
|
726
|
-
* Append an long value. This increases the array's length by one.
|
727
|
-
*
|
728
|
-
* @nowebref
|
729
|
-
* @param value A long value.
|
730
|
-
* @return this.
|
731
|
-
*/
|
732
|
-
public JSONArray append(long value) {
|
733
|
-
this.append(Long.valueOf(value));
|
734
|
-
return this;
|
735
|
-
}
|
736
|
-
|
737
|
-
|
738
|
-
/**
|
739
|
-
* Append a float value. This increases the array's length by one.
|
740
|
-
* This will store the value as a double, since there are no floats in JSON.
|
741
|
-
*
|
742
|
-
* @param value a float value
|
743
|
-
* @throws RuntimeException if the value is not finite.
|
744
|
-
* @return this.
|
745
|
-
*/
|
746
|
-
public JSONArray append(float value) {
|
747
|
-
return append((double) value);
|
748
|
-
}
|
749
|
-
|
750
|
-
|
751
|
-
/**
|
752
|
-
* Append a double value. This increases the array's length by one.
|
753
|
-
*
|
754
|
-
* @nowebref
|
755
|
-
* @param value A double value.
|
756
|
-
* @throws RuntimeException if the value is not finite.
|
757
|
-
* @return this.
|
758
|
-
*/
|
759
|
-
public JSONArray append(double value) {
|
760
|
-
Double d = value;
|
761
|
-
JSONObject.testValidity(d);
|
762
|
-
this.append(d);
|
763
|
-
return this;
|
764
|
-
}
|
765
|
-
|
766
|
-
|
767
|
-
/**
|
768
|
-
* Append a boolean value. This increases the array's length by one.
|
769
|
-
*
|
770
|
-
* @param value a boolean value
|
771
|
-
* @return this.
|
772
|
-
*/
|
773
|
-
public JSONArray append(boolean value) {
|
774
|
-
this.append(value ? Boolean.TRUE : Boolean.FALSE);
|
775
|
-
return this;
|
776
|
-
}
|
714
|
+
/**
|
715
|
+
* Append an long value. This increases the array's length by one.
|
716
|
+
*
|
717
|
+
* @nowebref
|
718
|
+
* @param value A long value.
|
719
|
+
* @return this.
|
720
|
+
*/
|
721
|
+
public JSONArray append(long value) {
|
722
|
+
this.append(Long.valueOf(value));
|
723
|
+
return this;
|
724
|
+
}
|
777
725
|
|
726
|
+
/**
|
727
|
+
* Append a float value. This increases the array's length by one. This will
|
728
|
+
* store the value as a double, since there are no floats in JSON.
|
729
|
+
*
|
730
|
+
* @param value a float value
|
731
|
+
* @throws RuntimeException if the value is not finite.
|
732
|
+
* @return this.
|
733
|
+
*/
|
734
|
+
public JSONArray append(float value) {
|
735
|
+
return append((double) value);
|
736
|
+
}
|
737
|
+
|
738
|
+
/**
|
739
|
+
* Append a double value. This increases the array's length by one.
|
740
|
+
*
|
741
|
+
* @nowebref
|
742
|
+
* @param value A double value.
|
743
|
+
* @throws RuntimeException if the value is not finite.
|
744
|
+
* @return this.
|
745
|
+
*/
|
746
|
+
public JSONArray append(double value) {
|
747
|
+
Double d = value;
|
748
|
+
JSONObject.testValidity(d);
|
749
|
+
this.append(d);
|
750
|
+
return this;
|
751
|
+
}
|
752
|
+
|
753
|
+
/**
|
754
|
+
* Append a boolean value. This increases the array's length by one.
|
755
|
+
*
|
756
|
+
* @param value a boolean value
|
757
|
+
* @return this.
|
758
|
+
*/
|
759
|
+
public JSONArray append(boolean value) {
|
760
|
+
this.append(value ? Boolean.TRUE : Boolean.FALSE);
|
761
|
+
return this;
|
762
|
+
}
|
778
763
|
|
779
764
|
// /**
|
780
765
|
// * Put a value in the JSONArray, where the value will be a
|
@@ -786,8 +771,6 @@ public class JSONArray {
|
|
786
771
|
// this.append(new JSONArray(value));
|
787
772
|
// return this;
|
788
773
|
// }
|
789
|
-
|
790
|
-
|
791
774
|
// /**
|
792
775
|
// * Put a value in the JSONArray, where the value will be a
|
793
776
|
// * JSONObject which is produced from a Map.
|
@@ -798,38 +781,36 @@ public class JSONArray {
|
|
798
781
|
// this.append(new JSONObject(value));
|
799
782
|
// return this;
|
800
783
|
// }
|
784
|
+
/**
|
785
|
+
* @param value a JSONArray value
|
786
|
+
* @return
|
787
|
+
*/
|
788
|
+
public JSONArray append(JSONArray value) {
|
789
|
+
myArrayList.add(value);
|
790
|
+
return this;
|
791
|
+
}
|
801
792
|
|
793
|
+
/**
|
794
|
+
* @param value a JSONObject value
|
795
|
+
* @return
|
796
|
+
*/
|
797
|
+
public JSONArray append(JSONObject value) {
|
798
|
+
myArrayList.add(value);
|
799
|
+
return this;
|
800
|
+
}
|
802
801
|
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
|
809
|
-
|
810
|
-
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
public JSONArray append(JSONObject value) {
|
816
|
-
myArrayList.add(value);
|
817
|
-
return this;
|
818
|
-
}
|
819
|
-
|
820
|
-
|
821
|
-
/**
|
822
|
-
* Append an object value. This increases the array's length by one.
|
823
|
-
* @param value An object value. The value should be a
|
824
|
-
* Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
|
825
|
-
* JSONObject.NULL object.
|
826
|
-
* @return this.
|
827
|
-
*/
|
828
|
-
protected JSONArray append(Object value) {
|
829
|
-
myArrayList.add(value);
|
830
|
-
return this;
|
831
|
-
}
|
832
|
-
|
802
|
+
/**
|
803
|
+
* Append an object value. This increases the array's length by one.
|
804
|
+
*
|
805
|
+
* @param value An object value. The value should be a Boolean, Double,
|
806
|
+
* Integer, JSONArray, JSONObject, Long, or String, or the JSONObject.NULL
|
807
|
+
* object.
|
808
|
+
* @return this.
|
809
|
+
*/
|
810
|
+
protected JSONArray append(Object value) {
|
811
|
+
myArrayList.add(value);
|
812
|
+
return this;
|
813
|
+
}
|
833
814
|
|
834
815
|
// /**
|
835
816
|
// * Put a value in the JSONArray, where the value will be a
|
@@ -844,120 +825,114 @@ public class JSONArray {
|
|
844
825
|
// this.set(index, new JSONArray(value));
|
845
826
|
// return this;
|
846
827
|
// }
|
828
|
+
/**
|
829
|
+
* Put or replace a String value. If the index is greater than the length of
|
830
|
+
* the JSONArray, then null elements will be added as necessary to pad it
|
831
|
+
* out.
|
832
|
+
*
|
833
|
+
* @webref jsonarray:method
|
834
|
+
* @brief Put a String value in the JSONArray
|
835
|
+
* @param index an index value
|
836
|
+
* @param value the value to assign
|
837
|
+
* @return this.
|
838
|
+
* @throws RuntimeException If the index is negative.
|
839
|
+
* @see JSONArray#setInt(int, int)
|
840
|
+
* @see JSONArray#setFloat(int, float)
|
841
|
+
* @see JSONArray#setBoolean(int, boolean)
|
842
|
+
*/
|
843
|
+
public JSONArray setString(int index, String value) {
|
844
|
+
this.set(index, value);
|
845
|
+
return this;
|
846
|
+
}
|
847
847
|
|
848
|
+
/**
|
849
|
+
* Put or replace an int value. If the index is greater than the length of
|
850
|
+
* the JSONArray, then null elements will be added as necessary to pad it
|
851
|
+
* out.
|
852
|
+
*
|
853
|
+
* @webref jsonarray:method
|
854
|
+
* @brief Put an int value in the JSONArray
|
855
|
+
* @param index an index value
|
856
|
+
* @param value the value to assign
|
857
|
+
* @return this.
|
858
|
+
* @throws RuntimeException If the index is negative.
|
859
|
+
* @see JSONArray#setFloat(int, float)
|
860
|
+
* @see JSONArray#setString(int, String)
|
861
|
+
* @see JSONArray#setBoolean(int, boolean)
|
862
|
+
*/
|
863
|
+
public JSONArray setInt(int index, int value) {
|
864
|
+
this.set(index, value);
|
865
|
+
return this;
|
866
|
+
}
|
848
867
|
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
882
|
-
|
883
|
-
* @see JSONArray#setBoolean(int, boolean)
|
884
|
-
*/
|
885
|
-
public JSONArray setInt(int index, int value) {
|
886
|
-
this.set(index, Integer.valueOf(value));
|
887
|
-
return this;
|
888
|
-
}
|
889
|
-
|
890
|
-
|
891
|
-
/**
|
892
|
-
* Put or replace a long value. If the index is greater than the length of
|
893
|
-
* the JSONArray, then null elements will be added as necessary to pad
|
894
|
-
* it out.
|
895
|
-
* @param index The subscript.
|
896
|
-
* @param value A long value.
|
897
|
-
* @return this.
|
898
|
-
* @throws RuntimeException If the index is negative.
|
899
|
-
*/
|
900
|
-
public JSONArray setLong(int index, long value) {
|
901
|
-
return set(index, Long.valueOf(value));
|
902
|
-
}
|
903
|
-
|
904
|
-
|
905
|
-
/**
|
906
|
-
* Put or replace a float value. If the index is greater than the length
|
907
|
-
* of the JSONArray, then null elements will be added as necessary to pad
|
908
|
-
* it out. There are no 'double' values in JSON, so this is passed to
|
909
|
-
* setDouble(value).
|
910
|
-
*
|
911
|
-
* @webref jsonarray:method
|
912
|
-
* @brief Put a float value in the JSONArray
|
913
|
-
* @param index an index value
|
914
|
-
* @param value the value to assign
|
915
|
-
* @return this.
|
916
|
-
* @throws RuntimeException If the index is negative or if the value is
|
917
|
-
* not finite.
|
918
|
-
* @see JSONArray#setInt(int, int)
|
919
|
-
* @see JSONArray#setString(int, String)
|
920
|
-
* @see JSONArray#setBoolean(int, boolean)
|
921
|
-
*/
|
922
|
-
public JSONArray setFloat(int index, float value) {
|
923
|
-
return setDouble(index, value);
|
924
|
-
}
|
925
|
-
|
926
|
-
|
927
|
-
/**
|
928
|
-
* Put or replace a double value. If the index is greater than the length of
|
929
|
-
* the JSONArray, then null elements will be added as necessary to pad
|
930
|
-
* it out.
|
931
|
-
* @param index The subscript.
|
932
|
-
* @param value A double value.
|
933
|
-
* @return this.
|
934
|
-
* @throws RuntimeException If the index is negative or if the value is
|
935
|
-
* not finite.
|
936
|
-
*/
|
937
|
-
public JSONArray setDouble(int index, double value) {
|
938
|
-
return set(index, Double.valueOf(value));
|
939
|
-
}
|
940
|
-
|
941
|
-
|
942
|
-
/**
|
943
|
-
* Put or replace a boolean value in the JSONArray. If the index is greater
|
944
|
-
* than the length of the JSONArray, then null elements will be added as
|
945
|
-
* necessary to pad it out.
|
946
|
-
*
|
947
|
-
* @webref jsonarray:method
|
948
|
-
* @brief Put a boolean value in the JSONArray
|
949
|
-
* @param index an index value
|
950
|
-
* @param value the value to assign
|
951
|
-
* @return this.
|
952
|
-
* @throws RuntimeException If the index is negative.
|
953
|
-
* @see JSONArray#setInt(int, int)
|
954
|
-
* @see JSONArray#setFloat(int, float)
|
955
|
-
* @see JSONArray#setString(int, String)
|
956
|
-
*/
|
957
|
-
public JSONArray setBoolean(int index, boolean value) {
|
958
|
-
return set(index, value ? Boolean.TRUE : Boolean.FALSE);
|
959
|
-
}
|
868
|
+
/**
|
869
|
+
* Put or replace a long value. If the index is greater than the length of
|
870
|
+
* the JSONArray, then null elements will be added as necessary to pad it
|
871
|
+
* out.
|
872
|
+
*
|
873
|
+
* @param index The subscript.
|
874
|
+
* @param value A long value.
|
875
|
+
* @return this.
|
876
|
+
* @throws RuntimeException If the index is negative.
|
877
|
+
*/
|
878
|
+
public JSONArray setLong(int index, long value) {
|
879
|
+
return set(index, value);
|
880
|
+
}
|
881
|
+
|
882
|
+
/**
|
883
|
+
* Put or replace a float value. If the index is greater than the length of
|
884
|
+
* the JSONArray, then null elements will be added as necessary to pad it
|
885
|
+
* out. There are no 'double' values in JSON, so this is passed to
|
886
|
+
* setDouble(value).
|
887
|
+
*
|
888
|
+
* @webref jsonarray:method
|
889
|
+
* @brief Put a float value in the JSONArray
|
890
|
+
* @param index an index value
|
891
|
+
* @param value the value to assign
|
892
|
+
* @return this.
|
893
|
+
* @throws RuntimeException If the index is negative or if the value is not
|
894
|
+
* finite.
|
895
|
+
* @see JSONArray#setInt(int, int)
|
896
|
+
* @see JSONArray#setString(int, String)
|
897
|
+
* @see JSONArray#setBoolean(int, boolean)
|
898
|
+
*/
|
899
|
+
public JSONArray setFloat(int index, float value) {
|
900
|
+
return setDouble(index, value);
|
901
|
+
}
|
960
902
|
|
903
|
+
/**
|
904
|
+
* Put or replace a double value. If the index is greater than the length of
|
905
|
+
* the JSONArray, then null elements will be added as necessary to pad it
|
906
|
+
* out.
|
907
|
+
*
|
908
|
+
* @param index The subscript.
|
909
|
+
* @param value A double value.
|
910
|
+
* @return this.
|
911
|
+
* @throws RuntimeException If the index is negative or if the value is not
|
912
|
+
* finite.
|
913
|
+
*/
|
914
|
+
public JSONArray setDouble(int index, double value) {
|
915
|
+
return set(index, value);
|
916
|
+
}
|
917
|
+
|
918
|
+
/**
|
919
|
+
* Put or replace a boolean value in the JSONArray. If the index is greater
|
920
|
+
* than the length of the JSONArray, then null elements will be added as
|
921
|
+
* necessary to pad it out.
|
922
|
+
*
|
923
|
+
* @webref jsonarray:method
|
924
|
+
* @brief Put a boolean value in the JSONArray
|
925
|
+
* @param index an index value
|
926
|
+
* @param value the value to assign
|
927
|
+
* @return this.
|
928
|
+
* @throws RuntimeException If the index is negative.
|
929
|
+
* @see JSONArray#setInt(int, int)
|
930
|
+
* @see JSONArray#setFloat(int, float)
|
931
|
+
* @see JSONArray#setString(int, String)
|
932
|
+
*/
|
933
|
+
public JSONArray setBoolean(int index, boolean value) {
|
934
|
+
return set(index, value ? Boolean.TRUE : Boolean.FALSE);
|
935
|
+
}
|
961
936
|
|
962
937
|
// /**
|
963
938
|
// * Put a value in the JSONArray, where the value will be a
|
@@ -972,106 +947,103 @@ public class JSONArray {
|
|
972
947
|
// this.set(index, new JSONObject(value));
|
973
948
|
// return this;
|
974
949
|
// }
|
950
|
+
/**
|
951
|
+
* @return @webref jsonarray:method
|
952
|
+
* @brief Sets the JSONArray value associated with an index value
|
953
|
+
* @param index the index value to target
|
954
|
+
* @param value the value to assign
|
955
|
+
* @see JSONArray#setJSONObject(int, JSONObject)
|
956
|
+
* @see JSONArray#getJSONObject(int)
|
957
|
+
* @see JSONArray#getJSONArray(int)
|
958
|
+
*/
|
959
|
+
public JSONArray setJSONArray(int index, JSONArray value) {
|
960
|
+
set(index, value);
|
961
|
+
return this;
|
962
|
+
}
|
975
963
|
|
976
|
-
|
977
|
-
|
978
|
-
|
979
|
-
|
980
|
-
|
981
|
-
|
982
|
-
|
983
|
-
|
984
|
-
|
985
|
-
|
986
|
-
|
987
|
-
|
988
|
-
}
|
989
|
-
|
990
|
-
/**
|
991
|
-
* @webref jsonarray:method
|
992
|
-
* @brief Sets the JSONObject value associated with an index value
|
993
|
-
* @param index the index value to target
|
994
|
-
* @param value the value to assign
|
995
|
-
* @see JSONArray#setJSONArray(int, JSONArray)
|
996
|
-
* @see JSONArray#getJSONObject(int)
|
997
|
-
* @see JSONArray#getJSONArray(int)
|
998
|
-
*/
|
999
|
-
public JSONArray setJSONObject(int index, JSONObject value) {
|
1000
|
-
set(index, value);
|
1001
|
-
return this;
|
1002
|
-
}
|
1003
|
-
|
1004
|
-
|
1005
|
-
/**
|
1006
|
-
* Put or replace an object value in the JSONArray. If the index is greater
|
1007
|
-
* than the length of the JSONArray, then null elements will be added as
|
1008
|
-
* necessary to pad it out.
|
1009
|
-
* @param index The subscript.
|
1010
|
-
* @param value The value to put into the array. The value should be a
|
1011
|
-
* Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
|
1012
|
-
* JSONObject.NULL object.
|
1013
|
-
* @return this.
|
1014
|
-
* @throws RuntimeException If the index is negative or if the the value is
|
1015
|
-
* an invalid number.
|
1016
|
-
*/
|
1017
|
-
private JSONArray set(int index, Object value) {
|
1018
|
-
JSONObject.testValidity(value);
|
1019
|
-
if (index < 0) {
|
1020
|
-
throw new RuntimeException("JSONArray[" + index + "] not found.");
|
964
|
+
/**
|
965
|
+
* @return @webref jsonarray:method
|
966
|
+
* @brief Sets the JSONObject value associated with an index value
|
967
|
+
* @param index the index value to target
|
968
|
+
* @param value the value to assign
|
969
|
+
* @see JSONArray#setJSONArray(int, JSONArray)
|
970
|
+
* @see JSONArray#getJSONObject(int)
|
971
|
+
* @see JSONArray#getJSONArray(int)
|
972
|
+
*/
|
973
|
+
public JSONArray setJSONObject(int index, JSONObject value) {
|
974
|
+
set(index, value);
|
975
|
+
return this;
|
1021
976
|
}
|
1022
|
-
|
1023
|
-
|
1024
|
-
|
1025
|
-
|
1026
|
-
|
1027
|
-
|
1028
|
-
|
977
|
+
|
978
|
+
/**
|
979
|
+
* Put or replace an object value in the JSONArray. If the index is greater
|
980
|
+
* than the length of the JSONArray, then null elements will be added as
|
981
|
+
* necessary to pad it out.
|
982
|
+
*
|
983
|
+
* @param index The subscript.
|
984
|
+
* @param value The value to put into the array. The value should be a
|
985
|
+
* Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
|
986
|
+
* JSONObject.NULL object.
|
987
|
+
* @return this.
|
988
|
+
* @throws RuntimeException If the index is negative or if the the value is
|
989
|
+
* an invalid number.
|
990
|
+
*/
|
991
|
+
private JSONArray set(int index, Object value) {
|
992
|
+
JSONObject.testValidity(value);
|
993
|
+
if (index < 0) {
|
994
|
+
throw new RuntimeException("JSONArray[" + index + "] not found.");
|
995
|
+
}
|
996
|
+
if (index < this.size()) {
|
997
|
+
this.myArrayList.set(index, value);
|
998
|
+
} else {
|
999
|
+
while (index != this.size()) {
|
1000
|
+
this.append(JSONObject.NULL);
|
1001
|
+
}
|
1002
|
+
this.append(value);
|
1003
|
+
}
|
1004
|
+
return this;
|
1029
1005
|
}
|
1030
|
-
return this;
|
1031
|
-
}
|
1032
|
-
|
1033
|
-
|
1034
|
-
/**
|
1035
|
-
* Get the number of elements in the JSONArray, included nulls.
|
1036
|
-
*
|
1037
|
-
* @webref jsonarray:method
|
1038
|
-
* @brief Gets the number of elements in the JSONArray
|
1039
|
-
* @return The length (or size).
|
1040
|
-
* @see JSONArray#append(String)
|
1041
|
-
* @see JSONArray#remove(int)
|
1042
|
-
*/
|
1043
|
-
public int size() {
|
1044
|
-
return myArrayList.size();
|
1045
|
-
}
|
1046
|
-
|
1047
|
-
|
1048
|
-
/**
|
1049
|
-
* Determine if the value is null.
|
1050
|
-
* @webref
|
1051
|
-
* @param index must be between 0 and length() - 1
|
1052
|
-
* @return true if the value at the index is null, or if there is no value.
|
1053
|
-
*/
|
1054
|
-
public boolean isNull(int index) {
|
1055
|
-
return JSONObject.NULL.equals(this.opt(index));
|
1056
|
-
}
|
1057
|
-
|
1058
|
-
|
1059
|
-
/**
|
1060
|
-
* Remove an index and close the hole.
|
1061
|
-
*
|
1062
|
-
* @webref jsonarray:method
|
1063
|
-
* @brief Removes an element
|
1064
|
-
* @param index the index value of the element to be removed
|
1065
|
-
* @return The value that was associated with the index, or null if there was no value.
|
1066
|
-
* @see JSONArray#size()
|
1067
|
-
* @see JSONArray#append(String)
|
1068
|
-
*/
|
1069
|
-
public Object remove(int index) {
|
1070
|
-
Object o = this.opt(index);
|
1071
|
-
this.myArrayList.remove(index);
|
1072
|
-
return o;
|
1073
|
-
}
|
1074
1006
|
|
1007
|
+
/**
|
1008
|
+
* Get the number of elements in the JSONArray, included nulls.
|
1009
|
+
*
|
1010
|
+
* @webref jsonarray:method
|
1011
|
+
* @brief Gets the number of elements in the JSONArray
|
1012
|
+
* @return The length (or size).
|
1013
|
+
* @see JSONArray#append(String)
|
1014
|
+
* @see JSONArray#remove(int)
|
1015
|
+
*/
|
1016
|
+
public int size() {
|
1017
|
+
return myArrayList.size();
|
1018
|
+
}
|
1019
|
+
|
1020
|
+
/**
|
1021
|
+
* Determine if the value is null.
|
1022
|
+
*
|
1023
|
+
* @webref
|
1024
|
+
* @param index must be between 0 and length() - 1
|
1025
|
+
* @return true if the value at the index is null, or if there is no value.
|
1026
|
+
*/
|
1027
|
+
public boolean isNull(int index) {
|
1028
|
+
return JSONObject.NULL.equals(this.opt(index));
|
1029
|
+
}
|
1030
|
+
|
1031
|
+
/**
|
1032
|
+
* Remove an index and close the hole.
|
1033
|
+
*
|
1034
|
+
* @webref jsonarray:method
|
1035
|
+
* @brief Removes an element
|
1036
|
+
* @param index the index value of the element to be removed
|
1037
|
+
* @return The value that was associated with the index, or null if there
|
1038
|
+
* was no value.
|
1039
|
+
* @see JSONArray#size()
|
1040
|
+
* @see JSONArray#append(String)
|
1041
|
+
*/
|
1042
|
+
public Object remove(int index) {
|
1043
|
+
Object o = this.opt(index);
|
1044
|
+
this.myArrayList.remove(index);
|
1045
|
+
return o;
|
1046
|
+
}
|
1075
1047
|
|
1076
1048
|
// /**
|
1077
1049
|
// * Produce a JSONObject by combining a JSONArray of names with the values
|
@@ -1092,81 +1064,73 @@ public class JSONArray {
|
|
1092
1064
|
// }
|
1093
1065
|
// return jo;
|
1094
1066
|
// }
|
1095
|
-
|
1096
|
-
|
1097
1067
|
// protected boolean save(OutputStream output) {
|
1098
1068
|
// return write(PApplet.createWriter(output), null);
|
1099
1069
|
// }
|
1070
|
+
public boolean save(File file, String options) {
|
1071
|
+
boolean success;
|
1072
|
+
try (PrintWriter writer = PApplet.createWriter(file)) {
|
1073
|
+
success = write(writer, options);
|
1074
|
+
}
|
1075
|
+
return success;
|
1076
|
+
}
|
1100
1077
|
|
1078
|
+
public boolean write(PrintWriter output) {
|
1079
|
+
return write(output, null);
|
1080
|
+
}
|
1101
1081
|
|
1102
|
-
|
1103
|
-
|
1104
|
-
|
1105
|
-
|
1106
|
-
|
1107
|
-
|
1108
|
-
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1112
|
-
|
1113
|
-
|
1114
|
-
|
1115
|
-
|
1116
|
-
|
1117
|
-
|
1118
|
-
String[] opts = PApplet.split(options, ',');
|
1119
|
-
for (String opt : opts) {
|
1120
|
-
if (opt.equals("compact")) {
|
1121
|
-
indentFactor = -1;
|
1122
|
-
} else if (opt.startsWith("indent=")) {
|
1123
|
-
indentFactor = PApplet.parseInt(opt.substring(7), -2);
|
1124
|
-
if (indentFactor == -2) {
|
1125
|
-
throw new IllegalArgumentException("Could not read a number from " + opt);
|
1126
|
-
}
|
1127
|
-
} else {
|
1128
|
-
System.err.println("Ignoring " + opt);
|
1082
|
+
public boolean write(PrintWriter output, String options) {
|
1083
|
+
int indentFactor = 2;
|
1084
|
+
if (options != null) {
|
1085
|
+
String[] opts = PApplet.split(options, ',');
|
1086
|
+
for (String opt : opts) {
|
1087
|
+
if (opt.equals("compact")) {
|
1088
|
+
indentFactor = -1;
|
1089
|
+
} else if (opt.startsWith("indent=")) {
|
1090
|
+
indentFactor = PApplet.parseInt(opt.substring(7), -2);
|
1091
|
+
if (indentFactor == -2) {
|
1092
|
+
throw new IllegalArgumentException("Could not read a number from " + opt);
|
1093
|
+
}
|
1094
|
+
} else {
|
1095
|
+
System.err.println("Ignoring " + opt);
|
1096
|
+
}
|
1097
|
+
}
|
1129
1098
|
}
|
1130
|
-
|
1099
|
+
output.print(format(indentFactor));
|
1100
|
+
output.flush();
|
1101
|
+
return true;
|
1131
1102
|
}
|
1132
|
-
|
1133
|
-
|
1134
|
-
|
1135
|
-
|
1136
|
-
|
1137
|
-
|
1138
|
-
|
1139
|
-
|
1140
|
-
|
1141
|
-
|
1142
|
-
|
1143
|
-
|
1144
|
-
|
1145
|
-
try {
|
1146
|
-
return format(2);
|
1147
|
-
} catch (Exception e) {
|
1148
|
-
return null;
|
1149
|
-
}
|
1150
|
-
}
|
1151
|
-
|
1152
|
-
|
1153
|
-
/**
|
1154
|
-
* Make a pretty-printed JSON text of this JSONArray.
|
1155
|
-
* Warning: This method assumes that the data structure is acyclical.
|
1156
|
-
* @param indentFactor The number of spaces to add to each level of
|
1157
|
-
* indentation. Use -1 to specify no indentation and no newlines.
|
1158
|
-
* @return a printable, displayable, transmittable
|
1159
|
-
* representation of the object, beginning
|
1160
|
-
* with <code>[</code> <small>(left bracket)</small> and ending
|
1161
|
-
* with <code>]</code> <small>(right bracket)</small>.
|
1162
|
-
*/
|
1163
|
-
public String format(int indentFactor) {
|
1164
|
-
StringWriter sw = new StringWriter();
|
1165
|
-
synchronized (sw.getBuffer()) {
|
1166
|
-
return this.writeInternal(sw, indentFactor, 0).toString();
|
1103
|
+
|
1104
|
+
/**
|
1105
|
+
* Return the JSON data formatted with two spaces for indents. Chosen to do
|
1106
|
+
* this since it's the most common case (e.g. with println()). Same as
|
1107
|
+
* format(2). Use the format() function for more options.
|
1108
|
+
*/
|
1109
|
+
@Override
|
1110
|
+
public String toString() {
|
1111
|
+
try {
|
1112
|
+
return format(2);
|
1113
|
+
} catch (Exception e) {
|
1114
|
+
return null;
|
1115
|
+
}
|
1167
1116
|
}
|
1168
|
-
}
|
1169
1117
|
|
1118
|
+
/**
|
1119
|
+
* Make a pretty-printed JSON text of this JSONArray. Warning: This method
|
1120
|
+
* assumes that the data structure is acyclical.
|
1121
|
+
*
|
1122
|
+
* @param indentFactor The number of spaces to add to each level of
|
1123
|
+
* indentation. Use -1 to specify no indentation and no newlines.
|
1124
|
+
* @return a printable, displayable, transmittable representation of the
|
1125
|
+
* object, beginning with <code>[</code> <small>(left bracket)</small>
|
1126
|
+
* and ending with <code>]</code> <small>(right bracket)</small>.
|
1127
|
+
*/
|
1128
|
+
public String format(int indentFactor) {
|
1129
|
+
StringWriter sw = new StringWriter();
|
1130
|
+
synchronized (sw.getBuffer()) {
|
1131
|
+
return this.writeInternal(sw, indentFactor, 0).toString();
|
1132
|
+
}
|
1133
|
+
}
|
1170
1134
|
|
1171
1135
|
// /**
|
1172
1136
|
// * Write the contents of the JSONArray as JSON text to a writer. For
|
@@ -1179,82 +1143,79 @@ public class JSONArray {
|
|
1179
1143
|
// protected Writer write(Writer writer) {
|
1180
1144
|
// return this.write(writer, -1, 0);
|
1181
1145
|
// }
|
1182
|
-
|
1183
|
-
|
1184
|
-
|
1185
|
-
|
1186
|
-
|
1187
|
-
|
1188
|
-
|
1189
|
-
|
1190
|
-
|
1191
|
-
|
1192
|
-
|
1193
|
-
|
1194
|
-
|
1195
|
-
|
1196
|
-
|
1197
|
-
|
1198
|
-
|
1199
|
-
|
1200
|
-
|
1201
|
-
|
1202
|
-
|
1203
|
-
|
1204
|
-
|
1205
|
-
|
1206
|
-
if (length == 1) {
|
1207
|
-
JSONObject.writeValue(writer, this.myArrayList.get(0),
|
1208
|
-
indentFactor, indent);
|
1146
|
+
/**
|
1147
|
+
* Write the contents of the JSONArray as JSON text to a writer.
|
1148
|
+
* <p>
|
1149
|
+
* Warning: This method assumes that the data structure is acyclic.
|
1150
|
+
*
|
1151
|
+
* @param writer
|
1152
|
+
* @param indentFactor The number of spaces to add to each level of
|
1153
|
+
* indentation. Use -1 to specify no indentation and no newlines.
|
1154
|
+
* @param indent The indention of the top level.
|
1155
|
+
* @return The writer.
|
1156
|
+
* @throws RuntimeException
|
1157
|
+
*/
|
1158
|
+
protected Writer writeInternal(Writer writer, int indentFactor, int indent) {
|
1159
|
+
try {
|
1160
|
+
boolean commanate = false;
|
1161
|
+
int length = this.size();
|
1162
|
+
writer.write('[');
|
1163
|
+
|
1164
|
+
// Use -1 to signify 'no indent'
|
1165
|
+
int thisFactor = (indentFactor == -1) ? 0 : indentFactor;
|
1166
|
+
|
1167
|
+
if (length == 1) {
|
1168
|
+
JSONObject.writeValue(writer, this.myArrayList.get(0),
|
1169
|
+
indentFactor, indent);
|
1209
1170
|
// thisFactor, indent);
|
1210
|
-
|
1211
|
-
|
1212
|
-
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1171
|
+
} else if (length != 0) {
|
1172
|
+
final int newIndent = indent + thisFactor;
|
1173
|
+
|
1174
|
+
for (int i = 0; i < length; i += 1) {
|
1175
|
+
if (commanate) {
|
1176
|
+
writer.write(',');
|
1177
|
+
}
|
1178
|
+
if (indentFactor != -1) {
|
1179
|
+
writer.write('\n');
|
1180
|
+
}
|
1181
|
+
JSONObject.indent(writer, newIndent);
|
1221
1182
|
// JSONObject.writeValue(writer, this.myArrayList.get(i),
|
1222
1183
|
// thisFactor, newIndent);
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1184
|
+
JSONObject.writeValue(writer, this.myArrayList.get(i),
|
1185
|
+
indentFactor, newIndent);
|
1186
|
+
commanate = true;
|
1187
|
+
}
|
1188
|
+
if (indentFactor != -1) {
|
1189
|
+
writer.write('\n');
|
1190
|
+
}
|
1191
|
+
JSONObject.indent(writer, indent);
|
1192
|
+
}
|
1193
|
+
writer.write(']');
|
1194
|
+
return writer;
|
1195
|
+
} catch (IOException e) {
|
1196
|
+
throw new RuntimeException(e);
|
1226
1197
|
}
|
1227
|
-
if (indentFactor != -1) {
|
1228
|
-
writer.write('\n');
|
1229
|
-
}
|
1230
|
-
JSONObject.indent(writer, indent);
|
1231
|
-
}
|
1232
|
-
writer.write(']');
|
1233
|
-
return writer;
|
1234
|
-
} catch (IOException e) {
|
1235
|
-
throw new RuntimeException(e);
|
1236
1198
|
}
|
1237
|
-
|
1238
|
-
|
1239
|
-
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1249
|
-
|
1250
|
-
|
1251
|
-
|
1252
|
-
|
1253
|
-
|
1254
|
-
|
1255
|
-
|
1256
|
-
|
1199
|
+
|
1200
|
+
/**
|
1201
|
+
* Make a string from the contents of this JSONArray. The
|
1202
|
+
* <code>separator</code> string is inserted between each element. Warning:
|
1203
|
+
* This method assumes that the data structure is acyclic.
|
1204
|
+
*
|
1205
|
+
* @param separator A string that will be inserted between the elements.
|
1206
|
+
* @return a string.
|
1207
|
+
* @throws RuntimeException If the array contains an invalid number.
|
1208
|
+
*/
|
1209
|
+
public String join(String separator) {
|
1210
|
+
int len = this.size();
|
1211
|
+
StringBuilder sb = new StringBuilder();
|
1212
|
+
|
1213
|
+
for (int i = 0; i < len; i += 1) {
|
1214
|
+
if (i > 0) {
|
1215
|
+
sb.append(separator);
|
1216
|
+
}
|
1217
|
+
sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
|
1218
|
+
}
|
1219
|
+
return sb.toString();
|
1257
1220
|
}
|
1258
|
-
return sb.toString();
|
1259
|
-
}
|
1260
1221
|
}
|