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