propane 3.9.0-java → 3.10.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/CHANGELOG.md +2 -2
  4. data/README.md +3 -3
  5. data/Rakefile +6 -6
  6. data/lib/java/japplemenubar/JAppleMenuBar.java +88 -0
  7. data/lib/java/japplemenubar/libjAppleMenuBar.jnilib +0 -0
  8. data/lib/java/monkstone/ColorUtil.java +127 -0
  9. data/lib/java/monkstone/MathToolModule.java +287 -0
  10. data/lib/java/monkstone/PropaneLibrary.java +46 -0
  11. data/lib/java/monkstone/core/LibraryProxy.java +136 -0
  12. data/lib/java/monkstone/fastmath/DegLutTables.java +111 -0
  13. data/lib/java/monkstone/fastmath/Deglut.java +71 -0
  14. data/lib/java/monkstone/fastmath/package-info.java +6 -0
  15. data/lib/java/monkstone/filechooser/Chooser.java +39 -0
  16. data/{src/main → lib}/java/monkstone/noise/FastTerrain.java +0 -0
  17. data/{src/main → lib}/java/monkstone/noise/Noise.java +0 -0
  18. data/{src/main → lib}/java/monkstone/noise/NoiseGenerator.java +0 -0
  19. data/{src/main → lib}/java/monkstone/noise/NoiseMode.java +0 -0
  20. data/lib/java/monkstone/noise/OpenSimplex2F.java +881 -0
  21. data/lib/java/monkstone/noise/OpenSimplex2S.java +1106 -0
  22. data/{src/main → lib}/java/monkstone/noise/SmoothTerrain.java +0 -0
  23. data/lib/java/monkstone/slider/CustomHorizontalSlider.java +164 -0
  24. data/lib/java/monkstone/slider/CustomVerticalSlider.java +178 -0
  25. data/lib/java/monkstone/slider/SimpleHorizontalSlider.java +145 -0
  26. data/lib/java/monkstone/slider/SimpleSlider.java +166 -0
  27. data/lib/java/monkstone/slider/SimpleVerticalSlider.java +157 -0
  28. data/lib/java/monkstone/slider/Slider.java +61 -0
  29. data/lib/java/monkstone/slider/SliderBar.java +245 -0
  30. data/lib/java/monkstone/slider/SliderGroup.java +56 -0
  31. data/lib/java/monkstone/slider/WheelHandler.java +35 -0
  32. data/lib/java/monkstone/vecmath/GfxRender.java +86 -0
  33. data/lib/java/monkstone/vecmath/JRender.java +56 -0
  34. data/lib/java/monkstone/vecmath/ShapeRender.java +87 -0
  35. data/lib/java/monkstone/vecmath/package-info.java +20 -0
  36. data/lib/java/monkstone/vecmath/vec2/Vec2.java +802 -0
  37. data/lib/java/monkstone/vecmath/vec2/package-info.java +6 -0
  38. data/lib/java/monkstone/vecmath/vec3/Vec3.java +727 -0
  39. data/lib/java/monkstone/vecmath/vec3/package-info.java +6 -0
  40. data/lib/java/monkstone/videoevent/CaptureEvent.java +27 -0
  41. data/lib/java/monkstone/videoevent/MovieEvent.java +32 -0
  42. data/lib/java/monkstone/videoevent/package-info.java +20 -0
  43. data/lib/java/processing/awt/PGraphicsJava2D.java +3040 -0
  44. data/lib/java/processing/awt/PImageAWT.java +377 -0
  45. data/lib/java/processing/awt/PShapeJava2D.java +387 -0
  46. data/lib/java/processing/awt/PSurfaceAWT.java +1581 -0
  47. data/lib/java/processing/awt/ShimAWT.java +581 -0
  48. data/lib/java/processing/core/PApplet.java +15156 -0
  49. data/lib/java/processing/core/PConstants.java +523 -0
  50. data/lib/java/processing/core/PFont.java +1126 -0
  51. data/lib/java/processing/core/PGraphics.java +8600 -0
  52. data/lib/java/processing/core/PImage.java +3377 -0
  53. data/lib/java/processing/core/PMatrix.java +208 -0
  54. data/lib/java/processing/core/PMatrix2D.java +562 -0
  55. data/lib/java/processing/core/PMatrix3D.java +890 -0
  56. data/lib/java/processing/core/PShape.java +3561 -0
  57. data/lib/java/processing/core/PShapeOBJ.java +483 -0
  58. data/lib/java/processing/core/PShapeSVG.java +2016 -0
  59. data/lib/java/processing/core/PStyle.java +63 -0
  60. data/lib/java/processing/core/PSurface.java +198 -0
  61. data/lib/java/processing/core/PSurfaceNone.java +431 -0
  62. data/lib/java/processing/core/PVector.java +1066 -0
  63. data/lib/java/processing/core/ThinkDifferent.java +115 -0
  64. data/lib/java/processing/data/DoubleDict.java +850 -0
  65. data/lib/java/processing/data/DoubleList.java +928 -0
  66. data/lib/java/processing/data/FloatDict.java +847 -0
  67. data/lib/java/processing/data/FloatList.java +936 -0
  68. data/lib/java/processing/data/IntDict.java +807 -0
  69. data/lib/java/processing/data/IntList.java +936 -0
  70. data/lib/java/processing/data/JSONArray.java +1260 -0
  71. data/lib/java/processing/data/JSONObject.java +2282 -0
  72. data/lib/java/processing/data/JSONTokener.java +435 -0
  73. data/lib/java/processing/data/LongDict.java +802 -0
  74. data/lib/java/processing/data/LongList.java +937 -0
  75. data/lib/java/processing/data/Sort.java +46 -0
  76. data/lib/java/processing/data/StringDict.java +613 -0
  77. data/lib/java/processing/data/StringList.java +800 -0
  78. data/lib/java/processing/data/Table.java +4936 -0
  79. data/lib/java/processing/data/TableRow.java +198 -0
  80. data/lib/java/processing/data/XML.java +1156 -0
  81. data/lib/java/processing/dxf/RawDXF.java +404 -0
  82. data/lib/java/processing/event/Event.java +125 -0
  83. data/lib/java/processing/event/KeyEvent.java +70 -0
  84. data/lib/java/processing/event/MouseEvent.java +114 -0
  85. data/lib/java/processing/event/TouchEvent.java +57 -0
  86. data/lib/java/processing/javafx/PGraphicsFX2D.java +32 -0
  87. data/lib/java/processing/javafx/PSurfaceFX.java +173 -0
  88. data/lib/java/processing/net/Client.java +744 -0
  89. data/lib/java/processing/net/Server.java +388 -0
  90. data/lib/java/processing/opengl/FontTexture.java +378 -0
  91. data/lib/java/processing/opengl/FrameBuffer.java +513 -0
  92. data/lib/java/processing/opengl/LinePath.java +627 -0
  93. data/lib/java/processing/opengl/LineStroker.java +681 -0
  94. data/lib/java/processing/opengl/PGL.java +3483 -0
  95. data/lib/java/processing/opengl/PGraphics2D.java +615 -0
  96. data/lib/java/processing/opengl/PGraphics3D.java +281 -0
  97. data/lib/java/processing/opengl/PGraphicsOpenGL.java +13753 -0
  98. data/lib/java/processing/opengl/PJOGL.java +2008 -0
  99. data/lib/java/processing/opengl/PShader.java +1484 -0
  100. data/lib/java/processing/opengl/PShapeOpenGL.java +5269 -0
  101. data/lib/java/processing/opengl/PSurfaceJOGL.java +1385 -0
  102. data/lib/java/processing/opengl/Texture.java +1696 -0
  103. data/lib/java/processing/opengl/VertexBuffer.java +88 -0
  104. data/lib/java/processing/opengl/cursors/arrow.png +0 -0
  105. data/lib/java/processing/opengl/cursors/cross.png +0 -0
  106. data/lib/java/processing/opengl/cursors/hand.png +0 -0
  107. data/lib/java/processing/opengl/cursors/license.txt +27 -0
  108. data/lib/java/processing/opengl/cursors/move.png +0 -0
  109. data/lib/java/processing/opengl/cursors/text.png +0 -0
  110. data/lib/java/processing/opengl/cursors/wait.png +0 -0
  111. data/lib/java/processing/opengl/shaders/ColorFrag.glsl +32 -0
  112. data/lib/java/processing/opengl/shaders/ColorVert.glsl +34 -0
  113. data/lib/java/processing/opengl/shaders/LightFrag.glsl +33 -0
  114. data/lib/java/processing/opengl/shaders/LightVert.glsl +151 -0
  115. data/lib/java/processing/opengl/shaders/LineFrag.glsl +32 -0
  116. data/lib/java/processing/opengl/shaders/LineVert.glsl +100 -0
  117. data/lib/java/processing/opengl/shaders/MaskFrag.glsl +40 -0
  118. data/lib/java/processing/opengl/shaders/PointFrag.glsl +32 -0
  119. data/lib/java/processing/opengl/shaders/PointVert.glsl +56 -0
  120. data/lib/java/processing/opengl/shaders/TexFrag.glsl +37 -0
  121. data/lib/java/processing/opengl/shaders/TexLightFrag.glsl +37 -0
  122. data/lib/java/processing/opengl/shaders/TexLightVert.glsl +157 -0
  123. data/lib/java/processing/opengl/shaders/TexVert.glsl +38 -0
  124. data/lib/java/processing/pdf/PGraphicsPDF.java +581 -0
  125. data/lib/java/processing/svg/PGraphicsSVG.java +378 -0
  126. data/lib/propane/app.rb +8 -13
  127. data/lib/propane/version.rb +1 -1
  128. data/mvnw +3 -3
  129. data/mvnw.cmd +2 -2
  130. data/pom.rb +7 -2
  131. data/pom.xml +14 -2
  132. data/propane.gemspec +2 -2
  133. data/src/main/java/monkstone/FastNoiseModuleJava.java +127 -0
  134. data/src/main/java/monkstone/MathToolModule.java +30 -30
  135. data/src/main/java/monkstone/PropaneLibrary.java +2 -0
  136. data/src/main/java/monkstone/SmoothNoiseModuleJava.java +127 -0
  137. data/src/main/java/monkstone/fastmath/DegLutTables.java +15 -15
  138. data/src/main/java/monkstone/filechooser/Chooser.java +1 -1
  139. data/src/main/java/monkstone/noise/OpenSimplex2F.java +752 -820
  140. data/src/main/java/monkstone/noise/OpenSimplex2S.java +1138 -1106
  141. data/src/main/java/monkstone/slider/WheelHandler.java +1 -1
  142. data/src/main/java/monkstone/vecmath/JRender.java +6 -6
  143. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +20 -19
  144. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +12 -12
  145. data/src/main/java/processing/awt/PGraphicsJava2D.java +11 -3
  146. data/src/main/java/processing/core/PApplet.java +89 -89
  147. data/src/main/java/processing/core/PConstants.java +155 -163
  148. data/src/main/java/processing/opengl/PJOGL.java +6 -5
  149. data/vendors/Rakefile +1 -1
  150. metadata +136 -19
@@ -0,0 +1,1260 @@
1
+ package processing.data;
2
+
3
+ // This code has been modified heavily to more closely match the rest of the
4
+ // Processing API. In the spirit of the rest of the project, where we try to
5
+ // keep the API as simple as possible, we have erred on the side of being
6
+ // conservative in choosing which functions to include, since we haven't yet
7
+ // decided what's truly necessary. Power users looking for a full-featured
8
+ // version can use the original version from json.org, or one of the many
9
+ // other APIs that are available. As with all Processing API, if there's a
10
+ // function that should be added, please let use know, and have others vote:
11
+ // http://code.google.com/p/processing/issues/list
12
+
13
+ /*
14
+ Copyright (c) 2002 JSON.org
15
+
16
+ Permission is hereby granted, free of charge, to any person obtaining a copy
17
+ of this software and associated documentation files (the "Software"), to deal
18
+ in the Software without restriction, including without limitation the rights
19
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20
+ copies of the Software, and to permit persons to whom the Software is
21
+ furnished to do so, subject to the following conditions:
22
+
23
+ The above copyright notice and this permission notice shall be included in all
24
+ copies or substantial portions of the Software.
25
+
26
+ The Software shall be used for Good, not Evil.
27
+
28
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34
+ SOFTWARE.
35
+ */
36
+
37
+ import java.io.File;
38
+ import java.io.IOException;
39
+ import java.io.PrintWriter;
40
+ import java.io.Reader;
41
+ import java.io.StringWriter;
42
+ import java.io.Writer;
43
+ import java.lang.reflect.Array;
44
+ import java.util.ArrayList;
45
+
46
+ import processing.core.PApplet;
47
+
48
+ /**
49
+ * A JSONArray is an ordered sequence of values. Its external text form is a
50
+ * string wrapped in square brackets with commas separating the values. The
51
+ * internal form is an object having <code>get</code> and <code>opt</code>
52
+ * methods for accessing the values by index, and <code>put</code> methods for
53
+ * adding or replacing values. The values can be any of these types:
54
+ * <code>Boolean</code>, <code>JSONArray</code>, <code>JSONObject</code>,
55
+ * <code>Number</code>, <code>String</code>, or the
56
+ * <code>JSONObject.NULL object</code>.
57
+ * <p>
58
+ * The constructor can convert a JSON text into a Java object. The
59
+ * <code>toString</code> method converts to JSON text.
60
+ * <p>
61
+ * A <code>get</code> method returns a value if one can be found, and throws an
62
+ * exception if one cannot be found. An <code>opt</code> method returns a
63
+ * default value instead of throwing an exception, and so is useful for
64
+ * obtaining optional values.
65
+ * <p>
66
+ * The generic <code>get()</code> and <code>opt()</code> methods return an
67
+ * object which you can cast or query for type. There are also typed
68
+ * <code>get</code> and <code>opt</code> methods that do type checking and type
69
+ * coercion for you.
70
+ * <p>
71
+ * The texts produced by the <code>toString</code> methods strictly conform to
72
+ * JSON syntax rules. The constructors are more forgiving in the texts they will
73
+ * accept:
74
+ * <ul>
75
+ * <li>An extra <code>,</code>&nbsp;<small>(comma)</small> may appear just
76
+ * before the closing bracket.</li>
77
+ * <li>The <code>null</code> value will be inserted when there is <code>,</code>
78
+ * &nbsp;<small>(comma)</small> elision.</li>
79
+ * <li>Strings may be quoted with <code>'</code>&nbsp;<small>(single
80
+ * quote)</small>.</li>
81
+ * <li>Strings do not need to be quoted at all if they do not begin with a quote
82
+ * or single quote, and if they do not contain leading or trailing spaces, and
83
+ * if they do not contain any of these characters:
84
+ * <code>{ } [ ] / \ : , = ; #</code> and if they do not look like numbers and
85
+ * if they are not the reserved words <code>true</code>, <code>false</code>, or
86
+ * <code>null</code>.</li>
87
+ * <li>Values can be separated by <code>;</code> <small>(semicolon)</small> as
88
+ * well as by <code>,</code> <small>(comma)</small>.</li>
89
+ * </ul>
90
+ *
91
+ * @author JSON.org
92
+ * @version 2012-11-13
93
+ * @webref data:composite
94
+ * @see JSONObject
95
+ * @see PApplet#loadJSONObject(String)
96
+ * @see PApplet#loadJSONArray(String)
97
+ * @see PApplet#saveJSONObject(JSONObject, String)
98
+ * @see PApplet#saveJSONArray(JSONArray, String)
99
+ */
100
+ public class JSONArray {
101
+
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 '['");
135
+ }
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());
145
+ }
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 ']'");
158
+ }
159
+ }
160
+ }
161
+ }
162
+
163
+
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));
171
+ }
172
+ }
173
+
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));
182
+ }
183
+ }
184
+
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>&nbsp;<small>(left bracket)</small>
201
+ * and ends with <code>]</code>&nbsp;<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;
209
+ }
210
+ }
211
+
212
+
213
+ // /**
214
+ // * Construct a JSONArray from a Collection.
215
+ // * @param collection A Collection.
216
+ // */
217
+ // public JSONArray(Collection collection) {
218
+ // myArrayList = new ArrayList<Object>();
219
+ // if (collection != null) {
220
+ // Iterator iter = collection.iterator();
221
+ // while (iter.hasNext()) {
222
+ // myArrayList.add(JSONObject.wrap(iter.next()));
223
+ // }
224
+ // }
225
+ // }
226
+
227
+
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.");
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;
347
+ }
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.");
367
+ }
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;
384
+ }
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;
409
+ }
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.");
429
+ }
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;
447
+ }
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;
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;
494
+ }
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;
515
+ }
516
+ throw new RuntimeException("JSONArray[" + index + "] is not a JSONArray.");
517
+ }
518
+
519
+
520
+ public JSONArray getJSONArray(int index, JSONArray defaultValue) {
521
+ try {
522
+ return getJSONArray(index);
523
+ } catch (Exception e) {
524
+ return defaultValue;
525
+ }
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;
546
+ }
547
+ throw new RuntimeException("JSONArray[" + index + "] is not a JSONObject.");
548
+ }
549
+
550
+
551
+ public JSONObject getJSONObject(int index, JSONObject defaultValue) {
552
+ try {
553
+ return getJSONObject(index);
554
+ } catch (Exception e) {
555
+ return defaultValue;
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
+ }
590
+
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);
597
+ }
598
+ return outgoing;
599
+ }
600
+
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);
607
+ }
608
+ return outgoing;
609
+ }
610
+
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);
617
+ }
618
+ return outgoing;
619
+ }
620
+
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);
627
+ }
628
+ return outgoing;
629
+ }
630
+
631
+
632
+ // /**
633
+ // * Get the optional boolean value associated with an index.
634
+ // * It returns false if there is no value at that index,
635
+ // * or if the value is not Boolean.TRUE or the String "true".
636
+ // *
637
+ // * @param index The index must be between 0 and length() - 1.
638
+ // * @return The truth.
639
+ // */
640
+ // public boolean optBoolean(int index) {
641
+ // return this.optBoolean(index, false);
642
+ // }
643
+ //
644
+ //
645
+ // /**
646
+ // * Get the optional double value associated with an index.
647
+ // * NaN is returned if there is no value for the index,
648
+ // * or if the value is not a number and cannot be converted to a number.
649
+ // *
650
+ // * @param index The index must be between 0 and length() - 1.
651
+ // * @return The value.
652
+ // */
653
+ // public double optDouble(int index) {
654
+ // return this.optDouble(index, Double.NaN);
655
+ // }
656
+ //
657
+ //
658
+ // /**
659
+ // * Get the optional int value associated with an index.
660
+ // * Zero is returned if there is no value for the index,
661
+ // * or if the value is not a number and cannot be converted to a number.
662
+ // *
663
+ // * @param index The index must be between 0 and length() - 1.
664
+ // * @return The value.
665
+ // */
666
+ // public int optInt(int index) {
667
+ // return this.optInt(index, 0);
668
+ // }
669
+ //
670
+ //
671
+ // /**
672
+ // * Get the optional long value associated with an index.
673
+ // * Zero is returned if there is no value for the index,
674
+ // * or if the value is not a number and cannot be converted to a number.
675
+ // *
676
+ // * @param index The index must be between 0 and length() - 1.
677
+ // * @return The value.
678
+ // */
679
+ // public long optLong(int index) {
680
+ // return this.optLong(index, 0);
681
+ // }
682
+ //
683
+ //
684
+ // /**
685
+ // * Get the optional string value associated with an index. It returns an
686
+ // * empty string if there is no value at that index. If the value
687
+ // * is not a string and is not null, then it is coverted to a string.
688
+ // *
689
+ // * @param index The index must be between 0 and length() - 1.
690
+ // * @return A String value.
691
+ // */
692
+ // public String optString(int index) {
693
+ // return this.optString(index, "");
694
+ // }
695
+
696
+
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
+ }
777
+
778
+
779
+ // /**
780
+ // * Put a value in the JSONArray, where the value will be a
781
+ // * JSONArray which is produced from a Collection.
782
+ // * @param value A Collection value.
783
+ // * @return this.
784
+ // */
785
+ // public JSONArray append(Collection value) {
786
+ // this.append(new JSONArray(value));
787
+ // return this;
788
+ // }
789
+
790
+
791
+ // /**
792
+ // * Put a value in the JSONArray, where the value will be a
793
+ // * JSONObject which is produced from a Map.
794
+ // * @param value A Map value.
795
+ // * @return this.
796
+ // */
797
+ // public JSONArray append(Map value) {
798
+ // this.append(new JSONObject(value));
799
+ // return this;
800
+ // }
801
+
802
+
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
+
833
+
834
+ // /**
835
+ // * Put a value in the JSONArray, where the value will be a
836
+ // * JSONArray which is produced from a Collection.
837
+ // * @param index The subscript.
838
+ // * @param value A Collection value.
839
+ // * @return this.
840
+ // * @throws RuntimeException If the index is negative or if the value is
841
+ // * not finite.
842
+ // */
843
+ // public JSONArray set(int index, Collection value) {
844
+ // this.set(index, new JSONArray(value));
845
+ // return this;
846
+ // }
847
+
848
+
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
+ }
960
+
961
+
962
+ // /**
963
+ // * Put a value in the JSONArray, where the value will be a
964
+ // * JSONObject that is produced from a Map.
965
+ // * @param index The subscript.
966
+ // * @param value The Map value.
967
+ // * @return this.
968
+ // * @throws RuntimeException If the index is negative or if the the value is
969
+ // * an invalid number.
970
+ // */
971
+ // public JSONArray set(int index, Map value) {
972
+ // this.set(index, new JSONObject(value));
973
+ // return this;
974
+ // }
975
+
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.");
1021
+ }
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);
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
+ }
1074
+
1075
+
1076
+ // /**
1077
+ // * Produce a JSONObject by combining a JSONArray of names with the values
1078
+ // * of this JSONArray.
1079
+ // * @param names A JSONArray containing a list of key strings. These will be
1080
+ // * paired with the values.
1081
+ // * @return A JSONObject, or null if there are no names or if this JSONArray
1082
+ // * has no values.
1083
+ // * @throws JSONException If any of the names are null.
1084
+ // */
1085
+ // public JSON toJSONObject(JSONArray names) {
1086
+ // if (names == null || names.length() == 0 || this.length() == 0) {
1087
+ // return null;
1088
+ // }
1089
+ // JSON jo = new JSON();
1090
+ // for (int i = 0; i < names.length(); i += 1) {
1091
+ // jo.put(names.getString(i), this.opt(i));
1092
+ // }
1093
+ // return jo;
1094
+ // }
1095
+
1096
+
1097
+ // protected boolean save(OutputStream output) {
1098
+ // return write(PApplet.createWriter(output), null);
1099
+ // }
1100
+
1101
+
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
+ }
1108
+
1109
+
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);
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;
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>&nbsp;<small>(left bracket)</small> and ending
1161
+ * with <code>]</code>&nbsp;<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
+
1170
+
1171
+ // /**
1172
+ // * Write the contents of the JSONArray as JSON text to a writer. For
1173
+ // * compactness, no whitespace is added.
1174
+ // * <p>
1175
+ // * Warning: This method assumes that the data structure is acyclic.
1176
+ // *
1177
+ // * @return The writer.
1178
+ // */
1179
+ // protected Writer write(Writer writer) {
1180
+ // return this.write(writer, -1, 0);
1181
+ // }
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);
1209
+ // thisFactor, indent);
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);
1221
+ // JSONObject.writeValue(writer, this.myArrayList.get(i),
1222
+ // thisFactor, newIndent);
1223
+ JSONObject.writeValue(writer, this.myArrayList.get(i),
1224
+ indentFactor, newIndent);
1225
+ commanate = true;
1226
+ }
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
+ }
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)));
1257
+ }
1258
+ return sb.toString();
1259
+ }
1260
+ }