propane 3.1.0.pre-java → 3.2.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/.mvn/wrapper/maven-wrapper.properties +1 -0
  3. data/CHANGELOG.md +1 -5
  4. data/README.md +23 -12
  5. data/Rakefile +23 -12
  6. data/lib/propane/helpers/version_error.rb +6 -0
  7. data/lib/propane/runner.rb +12 -0
  8. data/lib/propane/version.rb +1 -1
  9. data/library/slider/slider.rb +1 -1
  10. data/mvnw +234 -0
  11. data/mvnw.cmd +145 -0
  12. data/pom.xml +28 -27
  13. data/propane.gemspec +2 -2
  14. data/src/main/java/japplemenubar/JAppleMenuBar.java +41 -47
  15. data/src/main/java/monkstone/ColorUtil.java +1 -1
  16. data/src/main/java/monkstone/MathToolModule.java +12 -11
  17. data/src/main/java/monkstone/PropaneLibrary.java +9 -10
  18. data/src/main/java/monkstone/core/LibraryProxy.java +124 -113
  19. data/src/main/java/monkstone/fastmath/Deglut.java +86 -89
  20. data/src/main/java/monkstone/filechooser/Chooser.java +7 -13
  21. data/src/main/java/monkstone/noise/SimplexNoise.java +0 -1
  22. data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +4 -4
  23. data/src/main/java/monkstone/slider/CustomVerticalSlider.java +1 -1
  24. data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +9 -9
  25. data/src/main/java/monkstone/slider/SimpleSlider.java +0 -9
  26. data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +11 -13
  27. data/src/main/java/monkstone/slider/Slider.java +1 -1
  28. data/src/main/java/monkstone/slider/SliderBar.java +1 -1
  29. data/src/main/java/monkstone/slider/SliderGroup.java +1 -1
  30. data/src/main/java/monkstone/slider/WheelHandler.java +8 -9
  31. data/src/main/java/monkstone/vecmath/AppRender.java +2 -2
  32. data/src/main/java/monkstone/vecmath/ShapeRender.java +2 -2
  33. data/src/main/java/monkstone/vecmath/package-info.java +2 -2
  34. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +2 -2
  35. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +1 -1
  36. data/src/main/java/monkstone/videoevent/VideoInterface.java +11 -5
  37. data/src/main/java/monkstone/videoevent/package-info.java +2 -2
  38. data/src/main/java/processing/awt/PGraphicsJava2D.java +1742 -2243
  39. data/src/main/java/processing/awt/PShapeJava2D.java +268 -270
  40. data/src/main/java/processing/awt/PSurfaceAWT.java +821 -920
  41. data/src/main/java/processing/core/DesktopHandler.java +94 -0
  42. data/src/main/java/processing/core/PApplet.java +14170 -14082
  43. data/src/main/java/processing/core/PConstants.java +447 -473
  44. data/src/main/java/processing/core/PFont.java +867 -873
  45. data/src/main/java/processing/core/PGraphics.java +7193 -7428
  46. data/src/main/java/processing/core/PImage.java +3051 -3117
  47. data/src/main/java/processing/core/PMatrix.java +159 -172
  48. data/src/main/java/processing/core/PMatrix2D.java +403 -444
  49. data/src/main/java/processing/core/PMatrix3D.java +735 -749
  50. data/src/main/java/processing/core/PShape.java +2651 -2793
  51. data/src/main/java/processing/core/PShapeOBJ.java +415 -422
  52. data/src/main/java/processing/core/PShapeSVG.java +1466 -1475
  53. data/src/main/java/processing/core/PStyle.java +37 -40
  54. data/src/main/java/processing/core/PSurface.java +98 -103
  55. data/src/main/java/processing/core/PSurfaceNone.java +208 -236
  56. data/src/main/java/processing/core/PVector.java +961 -990
  57. data/src/main/java/processing/data/DoubleDict.java +709 -753
  58. data/src/main/java/processing/data/DoubleList.java +695 -748
  59. data/src/main/java/processing/data/FloatDict.java +702 -746
  60. data/src/main/java/processing/data/FloatList.java +697 -751
  61. data/src/main/java/processing/data/IntDict.java +673 -718
  62. data/src/main/java/processing/data/IntList.java +633 -699
  63. data/src/main/java/processing/data/JSONArray.java +873 -931
  64. data/src/main/java/processing/data/JSONObject.java +1165 -1262
  65. data/src/main/java/processing/data/JSONTokener.java +341 -351
  66. data/src/main/java/processing/data/LongDict.java +662 -707
  67. data/src/main/java/processing/data/LongList.java +634 -700
  68. data/src/main/java/processing/data/Sort.java +41 -37
  69. data/src/main/java/processing/data/StringDict.java +486 -522
  70. data/src/main/java/processing/data/StringList.java +580 -624
  71. data/src/main/java/processing/data/Table.java +3508 -3686
  72. data/src/main/java/processing/data/TableRow.java +183 -182
  73. data/src/main/java/processing/data/XML.java +883 -957
  74. data/src/main/java/processing/event/Event.java +66 -87
  75. data/src/main/java/processing/event/KeyEvent.java +41 -48
  76. data/src/main/java/processing/event/MouseEvent.java +93 -103
  77. data/src/main/java/processing/event/TouchEvent.java +6 -10
  78. data/src/main/java/processing/javafx/PGraphicsFX2D.java +5 -69
  79. data/src/main/java/processing/javafx/PSurfaceFX.java +2 -7
  80. data/src/main/java/processing/opengl/FontTexture.java +270 -290
  81. data/src/main/java/processing/opengl/FrameBuffer.java +363 -375
  82. data/src/main/java/processing/opengl/LinePath.java +500 -543
  83. data/src/main/java/processing/opengl/LineStroker.java +582 -593
  84. data/src/main/java/processing/opengl/PGL.java +2881 -2904
  85. data/src/main/java/processing/opengl/PGraphics2D.java +315 -408
  86. data/src/main/java/processing/opengl/PGraphics3D.java +72 -107
  87. data/src/main/java/processing/opengl/PGraphicsOpenGL.java +12043 -12230
  88. data/src/main/java/processing/opengl/PJOGL.java +1681 -1745
  89. data/src/main/java/processing/opengl/PShader.java +1257 -1260
  90. data/src/main/java/processing/opengl/PShapeOpenGL.java +4599 -4662
  91. data/src/main/java/processing/opengl/PSurfaceJOGL.java +1030 -1047
  92. data/src/main/java/processing/opengl/Texture.java +1397 -1462
  93. data/src/main/java/processing/opengl/VertexBuffer.java +55 -57
  94. data/src/main/resources/icon/icon-1024.png +0 -0
  95. data/src/main/resources/icon/icon-128.png +0 -0
  96. data/src/main/resources/icon/icon-16.png +0 -0
  97. data/src/main/resources/icon/icon-256.png +0 -0
  98. data/src/main/resources/icon/icon-32.png +0 -0
  99. data/src/main/resources/icon/icon-48.png +0 -0
  100. data/src/main/resources/icon/icon-512.png +0 -0
  101. data/src/main/resources/icon/icon-64.png +0 -0
  102. data/vendors/Rakefile +1 -1
  103. metadata +12 -8
  104. data/src/main/java/processing/core/ThinkDifferent.java +0 -70
@@ -33,7 +33,6 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33
33
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34
34
  SOFTWARE.
35
35
  */
36
-
37
36
  import java.io.File;
38
37
  import java.io.IOException;
39
38
  import java.io.PrintWriter;
@@ -111,95 +110,92 @@ import processing.core.PApplet;
111
110
  * @see PApplet#saveJSONArray(JSONArray, String)
112
111
  */
113
112
  public class JSONObject {
114
- /**
115
- * The maximum number of keys in the key pool.
116
- */
117
- private static final int keyPoolSize = 100;
118
-
119
- /**
120
- * Key pooling is like string interning, but without permanently tying up
121
- * memory. To help conserve memory, storage of duplicated key strings in
122
- * JSONObjects will be avoided by using a key pool to manage unique key
123
- * string objects. This is used by JSONObject.put(string, object).
124
- */
125
- private static HashMap<String, Object> keyPool =
126
- new HashMap<>(keyPoolSize);
127
-
128
-
129
- // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
130
-
131
-
132
- /**
133
- * JSONObject.NULL is equivalent to the value that JavaScript calls null,
134
- * whilst Java's null is equivalent to the value that JavaScript calls
135
- * undefined.
136
- */
137
- private static final class Null {
113
+
138
114
  /**
139
- * There is only intended to be a single instance of the NULL object,
140
- * so the clone method returns itself.
141
- * @return NULL.
115
+ * The maximum number of keys in the key pool.
142
116
  */
143
- @Override
144
- protected final Object clone() {
145
- return this;
146
- }
117
+ private static final int keyPoolSize = 100;
147
118
 
148
119
  /**
149
- * A Null object is equal to the null value and to itself.
150
- * @param object An object to test for nullness.
151
- * @return true if the object parameter is the JSONObject.NULL object
152
- * or null.
120
+ * Key pooling is like string interning, but without permanently tying up
121
+ * memory. To help conserve memory, storage of duplicated key strings in
122
+ * JSONObjects will be avoided by using a key pool to manage unique key
123
+ * string objects. This is used by JSONObject.put(string, object).
153
124
  */
154
- @Override
155
- public boolean equals(Object object) {
156
- return object == null || object == this;
157
- }
125
+ private static HashMap<String, Object> keyPool
126
+ = new HashMap<>(keyPoolSize);
158
127
 
128
+ // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
159
129
  /**
160
- * Get the "null" string value.
161
- * @return The string "null".
130
+ * JSONObject.NULL is equivalent to the value that JavaScript calls null,
131
+ * whilst Java's null is equivalent to the value that JavaScript calls
132
+ * undefined.
162
133
  */
163
- @Override
164
- public String toString() {
165
- return "null";
166
- }
167
-
168
- @Override
169
- public int hashCode() {
170
- // TODO Auto-generated method stub
171
- return super.hashCode();
172
- }
173
- }
134
+ private static final class Null {
135
+
136
+ /**
137
+ * There is only intended to be a single instance of the NULL object, so
138
+ * the clone method returns itself.
139
+ *
140
+ * @return NULL.
141
+ */
142
+ @Override
143
+ protected final Object clone() {
144
+ return this;
145
+ }
174
146
 
147
+ /**
148
+ * A Null object is equal to the null value and to itself.
149
+ *
150
+ * @param object An object to test for nullness.
151
+ * @return true if the object parameter is the JSONObject.NULL object or
152
+ * null.
153
+ */
154
+ @Override
155
+ public boolean equals(Object object) {
156
+ return object == null || object == this;
157
+ }
175
158
 
176
- // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
159
+ /**
160
+ * Get the "null" string value.
161
+ *
162
+ * @return The string "null".
163
+ */
164
+ @Override
165
+ public String toString() {
166
+ return "null";
167
+ }
177
168
 
169
+ @Override
170
+ public int hashCode() {
171
+ // TODO Auto-generated method stub
172
+ return super.hashCode();
173
+ }
174
+ }
178
175
 
179
- /**
180
- * The map where the JSONObject's properties are kept.
181
- */
176
+ // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
177
+ /**
178
+ * The map where the JSONObject's properties are kept.
179
+ */
182
180
  // private final Map map;
183
- private final HashMap<String, Object> map;
184
-
185
-
186
- /**
187
- * It is sometimes more convenient and less ambiguous to have a
188
- * <code>NULL</code> object than to use Java's <code>null</code> value.
189
- * <code>JSONObject.NULL.equals(null)</code> returns <code>true</code>.
190
- * <code>JSONObject.NULL.toString()</code> returns <code>"null"</code>.
191
- */
192
- public static final Object NULL = new Null();
181
+ private final HashMap<String, Object> map;
193
182
 
183
+ /**
184
+ * It is sometimes more convenient and less ambiguous to have a
185
+ * <code>NULL</code> object than to use Java's <code>null</code> value.
186
+ * <code>JSONObject.NULL.equals(null)</code> returns <code>true</code>.
187
+ * <code>JSONObject.NULL.toString()</code> returns <code>"null"</code>.
188
+ */
189
+ public static final Object NULL = new Null();
194
190
 
195
- /**
196
- * Construct an empty JSONObject.
197
- * @nowebref
198
- */
199
- public JSONObject() {
200
- this.map = new HashMap<>();
201
- }
202
-
191
+ /**
192
+ * Construct an empty JSONObject.
193
+ *
194
+ * @nowebref
195
+ */
196
+ public JSONObject() {
197
+ this.map = new HashMap<>();
198
+ }
203
199
 
204
200
  // /**
205
201
  // * Construct a JSONObject from a subset of another JSONObject.
@@ -217,153 +213,144 @@ public class JSONObject {
217
213
  // }
218
214
  // }
219
215
  // }
220
-
221
-
222
- /**
223
- * @nowebref
224
- */
225
- public JSONObject(Reader reader) {
226
- this(new JSONTokener(reader));
227
- }
228
-
229
-
230
- /**
231
- * Construct a JSONObject from a JSONTokener.
232
- * @param x A JSONTokener object containing the source string.
233
- * @throws RuntimeException If there is a syntax error in the source string
234
- * or a duplicated key.
235
- */
236
- protected JSONObject(JSONTokener x) {
237
- this();
238
- char c;
239
- String key;
240
-
241
- if (x.nextClean() != '{') {
242
- throw new RuntimeException("A JSONObject text must begin with '{'");
216
+ /**
217
+ * @nowebref
218
+ */
219
+ public JSONObject(Reader reader) {
220
+ this(new JSONTokener(reader));
243
221
  }
244
- for (;;) {
245
- c = x.nextClean();
246
- switch (c) {
247
- case 0:
248
- throw new RuntimeException("A JSONObject text must end with '}'");
249
- case '}':
250
- return;
251
- default:
252
- x.back();
253
- key = x.nextValue().toString();
254
- }
255
222
 
256
- // The key is followed by ':'. We will also tolerate '=' or '=>'.
223
+ /**
224
+ * Construct a JSONObject from a JSONTokener.
225
+ *
226
+ * @param x A JSONTokener object containing the source string.
227
+ * @throws RuntimeException If there is a syntax error in the source string
228
+ * or a duplicated key.
229
+ */
230
+ protected JSONObject(JSONTokener x) {
231
+ this();
232
+ char c;
233
+ String key;
257
234
 
258
- c = x.nextClean();
259
- if (c == '=') {
260
- if (x.next() != '>') {
261
- x.back();
235
+ if (x.nextClean() != '{') {
236
+ throw new RuntimeException("A JSONObject text must begin with '{'");
262
237
  }
263
- } else if (c != ':') {
264
- throw new RuntimeException("Expected a ':' after a key");
265
- }
266
- this.putOnce(key, x.nextValue());
267
-
268
- // Pairs are separated by ','. We will also tolerate ';'.
238
+ for (;;) {
239
+ c = x.nextClean();
240
+ switch (c) {
241
+ case 0:
242
+ throw new RuntimeException("A JSONObject text must end with '}'");
243
+ case '}':
244
+ return;
245
+ default:
246
+ x.back();
247
+ key = x.nextValue().toString();
248
+ }
269
249
 
270
- switch (x.nextClean()) {
271
- case ';':
272
- case ',':
273
- if (x.nextClean() == '}') {
274
- return;
250
+ // The key is followed by ':'. We will also tolerate '=' or '=>'.
251
+ c = x.nextClean();
252
+ if (c == '=') {
253
+ if (x.next() != '>') {
254
+ x.back();
255
+ }
256
+ } else if (c != ':') {
257
+ throw new RuntimeException("Expected a ':' after a key");
258
+ }
259
+ this.putOnce(key, x.nextValue());
260
+
261
+ // Pairs are separated by ','. We will also tolerate ';'.
262
+ switch (x.nextClean()) {
263
+ case ';':
264
+ case ',':
265
+ if (x.nextClean() == '}') {
266
+ return;
267
+ }
268
+ x.back();
269
+ break;
270
+ case '}':
271
+ return;
272
+ default:
273
+ throw new RuntimeException("Expected a ',' or '}'");
274
+ }
275
275
  }
276
- x.back();
277
- break;
278
- case '}':
279
- return;
280
- default:
281
- throw new RuntimeException("Expected a ',' or '}'");
282
- }
283
276
  }
284
- }
285
-
286
-
287
- /**
288
- * Construct a JSONObject from a Map.
289
- *
290
- * @param map A map object that can be used to initialize the contents of
291
- * the JSONObject.
292
- */
293
- protected JSONObject(HashMap<String, Object> map) {
294
- this.map = new HashMap<>();
295
- if (map != null) {
296
- Iterator i = map.entrySet().iterator();
297
- while (i.hasNext()) {
298
- Map.Entry e = (Map.Entry) i.next();
299
- Object value = e.getValue();
300
- if (value != null) {
301
- map.put((String) e.getKey(), wrap(value));
277
+
278
+ /**
279
+ * Construct a JSONObject from a Map.
280
+ *
281
+ * @param map A map object that can be used to initialize the contents of
282
+ * the JSONObject.
283
+ */
284
+ protected JSONObject(HashMap<String, Object> map) {
285
+ this.map = new HashMap<>();
286
+ if (map != null) {
287
+ Iterator i = map.entrySet().iterator();
288
+ while (i.hasNext()) {
289
+ Map.Entry e = (Map.Entry) i.next();
290
+ Object value = e.getValue();
291
+ if (value != null) {
292
+ map.put((String) e.getKey(), wrap(value));
293
+ }
294
+ }
302
295
  }
303
- }
304
296
  }
305
- }
306
-
307
297
 
308
- /**
309
- * @nowebref
310
- */
311
- public JSONObject(IntDict dict) {
312
- map = new HashMap<>();
313
- for (int i = 0; i < dict.size(); i++) {
314
- setInt(dict.key(i), dict.value(i));
298
+ /**
299
+ * @nowebref
300
+ */
301
+ public JSONObject(IntDict dict) {
302
+ map = new HashMap<>();
303
+ for (int i = 0; i < dict.size(); i++) {
304
+ setInt(dict.key(i), dict.value(i));
305
+ }
315
306
  }
316
- }
317
307
 
318
-
319
- /**
320
- * @nowebref
321
- */
322
- public JSONObject(FloatDict dict) {
323
- map = new HashMap<>();
324
- for (int i = 0; i < dict.size(); i++) {
325
- setFloat(dict.key(i), dict.value(i));
308
+ /**
309
+ * @nowebref
310
+ */
311
+ public JSONObject(FloatDict dict) {
312
+ map = new HashMap<>();
313
+ for (int i = 0; i < dict.size(); i++) {
314
+ setFloat(dict.key(i), dict.value(i));
315
+ }
326
316
  }
327
- }
328
317
 
318
+ /**
319
+ * @nowebref
320
+ */
321
+ public JSONObject(StringDict dict) {
322
+ map = new HashMap<>();
323
+ for (int i = 0; i < dict.size(); i++) {
324
+ setString(dict.key(i), dict.value(i));
325
+ }
326
+ }
329
327
 
330
- /**
331
- * @nowebref
332
- */
333
- public JSONObject(StringDict dict) {
334
- map = new HashMap<>();
335
- for (int i = 0; i < dict.size(); i++) {
336
- setString(dict.key(i), dict.value(i));
328
+ /**
329
+ * Construct a JSONObject from an Object using bean getters. It reflects on
330
+ * all of the public methods of the object. For each of the methods with no
331
+ * parameters and a name starting with <code>"get"</code> or
332
+ * <code>"is"</code> followed by an uppercase letter, the method is invoked,
333
+ * and a key and the value returned from the getter method are put into the
334
+ * new JSONObject.
335
+ *
336
+ * The key is formed by removing the <code>"get"</code> or <code>"is"</code>
337
+ * prefix. If the second remaining character is not upper case, then the
338
+ * first character is converted to lower case.
339
+ *
340
+ * For example, if an object has a method named <code>"getName"</code>, and
341
+ * if the result of calling <code>object.getName()</code> is
342
+ * <code>"Larry Fine"</code>, then the JSONObject will contain
343
+ * <code>"name": "Larry Fine"</code>.
344
+ *
345
+ * @param bean An object that has getter methods that should be used to make
346
+ * a JSONObject.
347
+ */
348
+ protected JSONObject(Object bean) {
349
+ this();
350
+ this.populateMap(bean);
337
351
  }
338
- }
339
-
340
-
341
- /**
342
- * Construct a JSONObject from an Object using bean getters.
343
- * It reflects on all of the public methods of the object.
344
- * For each of the methods with no parameters and a name starting
345
- * with <code>"get"</code> or <code>"is"</code> followed by an uppercase letter,
346
- * the method is invoked, and a key and the value returned from the getter method
347
- * are put into the new JSONObject.
348
- *
349
- * The key is formed by removing the <code>"get"</code> or <code>"is"</code> prefix.
350
- * If the second remaining character is not upper case, then the first
351
- * character is converted to lower case.
352
- *
353
- * For example, if an object has a method named <code>"getName"</code>, and
354
- * if the result of calling <code>object.getName()</code> is <code>"Larry Fine"</code>,
355
- * then the JSONObject will contain <code>"name": "Larry Fine"</code>.
356
- *
357
- * @param bean An object that has getter methods that should be used
358
- * to make a JSONObject.
359
- */
360
- protected JSONObject(Object bean) {
361
- this();
362
- this.populateMap(bean);
363
- }
364
-
365
-
366
- // holding off on this method until we decide on how to handle reflection
352
+
353
+ // holding off on this method until we decide on how to handle reflection
367
354
  // /**
368
355
  // * Construct a JSONObject from an Object, using reflection to find the
369
356
  // * public members. The resulting JSONObject's keys will be the strings
@@ -386,21 +373,19 @@ public class JSONObject {
386
373
  // }
387
374
  // }
388
375
  // }
389
-
390
-
391
- /**
392
- * Construct a JSONObject from a source JSON text string.
393
- * This is the most commonly used JSONObject constructor.
394
- * @param source A string beginning
395
- * with <code>{</code>&nbsp;<small>(left brace)</small> and ending
396
- * with <code>}</code>&nbsp;<small>(right brace)</small>.
397
- * @exception RuntimeException If there is a syntax error in the source
398
- * string or a duplicated key.
399
- */
400
- static public JSONObject parse(String source) {
401
- return new JSONObject(new JSONTokener(source));
402
- }
403
-
376
+ /**
377
+ * Construct a JSONObject from a source JSON text string. This is the most
378
+ * commonly used JSONObject constructor.
379
+ *
380
+ * @param source A string beginning with <code>{</code>&nbsp;<small>(left
381
+ * brace)</small> and ending with <code>}</code>&nbsp;<small>(right
382
+ * brace)</small>.
383
+ * @exception RuntimeException If there is a syntax error in the source
384
+ * string or a duplicated key.
385
+ */
386
+ static public JSONObject parse(String source) {
387
+ return new JSONObject(new JSONTokener(source));
388
+ }
404
389
 
405
390
  // /**
406
391
  // * Construct a JSONObject from a ResourceBundle.
@@ -440,8 +425,6 @@ public class JSONObject {
440
425
  // }
441
426
  // }
442
427
  // }
443
-
444
-
445
428
  // /**
446
429
  // * Accumulate values under a key. It is similar to the put method except
447
430
  // * that if there is already an object stored under the key then a
@@ -475,8 +458,6 @@ public class JSONObject {
475
458
  // }
476
459
  // return this;
477
460
  // }
478
-
479
-
480
461
  // /**
481
462
  // * Append values to the array under a key. If the key does not exist in the
482
463
  // * JSONObject, then the key is put in the JSONObject with its value being a
@@ -501,334 +482,315 @@ public class JSONObject {
501
482
  // }
502
483
  // return this;
503
484
  // }
485
+ /**
486
+ * Produce a string from a double. The string "null" will be returned if the
487
+ * number is not finite.
488
+ *
489
+ * @param d A double.
490
+ * @return A String.
491
+ */
492
+ static protected String doubleToString(double d) {
493
+ if (Double.isInfinite(d) || Double.isNaN(d)) {
494
+ return "null";
495
+ }
504
496
 
505
-
506
- /**
507
- * Produce a string from a double. The string "null" will be returned if
508
- * the number is not finite.
509
- * @param d A double.
510
- * @return A String.
511
- */
512
- static protected String doubleToString(double d) {
513
- if (Double.isInfinite(d) || Double.isNaN(d)) {
514
- return "null";
497
+ // Shave off trailing zeros and decimal point, if possible.
498
+ String string = Double.toString(d);
499
+ if (string.indexOf('.') > 0 && string.indexOf('e') < 0
500
+ && string.indexOf('E') < 0) {
501
+ while (string.endsWith("0")) {
502
+ string = string.substring(0, string.length() - 1);
503
+ }
504
+ if (string.endsWith(".")) {
505
+ string = string.substring(0, string.length() - 1);
506
+ }
507
+ }
508
+ return string;
515
509
  }
516
510
 
517
- // Shave off trailing zeros and decimal point, if possible.
518
-
519
- String string = Double.toString(d);
520
- if (string.indexOf('.') > 0 && string.indexOf('e') < 0 &&
521
- string.indexOf('E') < 0) {
522
- while (string.endsWith("0")) {
523
- string = string.substring(0, string.length() - 1);
524
- }
525
- if (string.endsWith(".")) {
526
- string = string.substring(0, string.length() - 1);
527
- }
528
- }
529
- return string;
530
- }
531
-
532
-
533
- /**
534
- * Get the value object associated with a key.
535
- *
536
- * @param key A key string.
537
- * @return The object associated with the key.
538
- * @throws RuntimeException if the key is not found.
539
- */
540
- public Object get(String key) {
541
- if (key == null) {
542
- throw new RuntimeException("JSONObject.get(null) called");
543
- }
544
- Object object = this.opt(key);
545
- if (object == null) {
546
- // Adding for rev 0257 in line with other p5 api
547
- return null;
548
- }
549
- if (object == null) {
550
- throw new RuntimeException("JSONObject[" + quote(key) + "] not found");
551
- }
552
- return object;
553
- }
554
-
555
-
556
- /**
557
- * Gets the String associated with a key
558
- *
559
- * @webref jsonobject:method
560
- * @brief Gets the string value associated with a key
561
- * @param key a key string
562
- * @return A string which is the value.
563
- * @throws RuntimeException if there is no string value for the key.
564
- * @see JSONObject#getInt(String)
565
- * @see JSONObject#getFloat(String)
566
- * @see JSONObject#getBoolean(String)
567
- */
568
- public String getString(String key) {
569
- Object object = this.get(key);
570
- if (object == null) {
571
- // Adding for rev 0257 in line with other p5 api
572
- return null;
573
- }
574
- if (object instanceof String) {
575
- return (String)object;
576
- }
577
- throw new RuntimeException("JSONObject[" + quote(key) + "] is not a string");
578
- }
579
-
580
-
581
- /**
582
- * Get an optional string associated with a key.
583
- * It returns the defaultValue if there is no such key.
584
- *
585
- * @param key A key string.
586
- * @param defaultValue The default.
587
- * @return A string which is the value.
588
- */
589
- public String getString(String key, String defaultValue) {
590
- Object object = this.opt(key);
591
- return NULL.equals(object) ? defaultValue : object.toString();
592
- }
593
-
594
-
595
- /**
596
- * Gets the int value associated with a key
597
- *
598
- * @webref jsonobject:method
599
- * @brief Gets the int value associated with a key
600
- * @param key A key string.
601
- * @return The integer value.
602
- * @throws RuntimeException if the key is not found or if the value cannot
603
- * be converted to an integer.
604
- * @see JSONObject#getFloat(String)
605
- * @see JSONObject#getString(String)
606
- * @see JSONObject#getBoolean(String)
607
- */
608
- public int getInt(String key) {
609
- Object object = this.get(key);
610
- if (object == null) {
611
- throw new RuntimeException("JSONObject[" + quote(key) + "] not found");
511
+ /**
512
+ * Get the value object associated with a key.
513
+ *
514
+ * @param key A key string.
515
+ * @return The object associated with the key.
516
+ * @throws RuntimeException if the key is not found.
517
+ */
518
+ public Object get(String key) {
519
+ if (key == null) {
520
+ throw new RuntimeException("JSONObject.get(null) called");
521
+ }
522
+ Object object = this.opt(key);
523
+ if (object == null) {
524
+ // Adding for rev 0257 in line with other p5 api
525
+ return null;
526
+ }
527
+ if (object == null) {
528
+ throw new RuntimeException("JSONObject[" + quote(key) + "] not found");
529
+ }
530
+ return object;
612
531
  }
613
- try {
614
- return object instanceof Number ?
615
- ((Number)object).intValue() : Integer.parseInt((String)object);
616
- } catch (Exception e) {
617
- throw new RuntimeException("JSONObject[" + quote(key) + "] is not an int.");
532
+
533
+ /**
534
+ * Gets the String associated with a key
535
+ *
536
+ * @webref jsonobject:method
537
+ * @brief Gets the string value associated with a key
538
+ * @param key a key string
539
+ * @return A string which is the value.
540
+ * @throws RuntimeException if there is no string value for the key.
541
+ * @see JSONObject#getInt(String)
542
+ * @see JSONObject#getFloat(String)
543
+ * @see JSONObject#getBoolean(String)
544
+ */
545
+ public String getString(String key) {
546
+ Object object = this.get(key);
547
+ if (object == null) {
548
+ // Adding for rev 0257 in line with other p5 api
549
+ return null;
550
+ }
551
+ if (object instanceof String) {
552
+ return (String) object;
553
+ }
554
+ throw new RuntimeException("JSONObject[" + quote(key) + "] is not a string");
618
555
  }
619
- }
620
-
621
-
622
- /**
623
- * Get an optional int value associated with a key,
624
- * or the default if there is no such key or if the value is not a number.
625
- * If the value is a string, an attempt will be made to evaluate it as
626
- * a number.
627
- *
628
- * @param key A key string.
629
- * @param defaultValue The default.
630
- * @return An object which is the value.
631
- */
632
- public int getInt(String key, int defaultValue) {
633
- try {
634
- return this.getInt(key);
635
- } catch (Exception e) {
636
- return defaultValue;
556
+
557
+ /**
558
+ * Get an optional string associated with a key. It returns the defaultValue
559
+ * if there is no such key.
560
+ *
561
+ * @param key A key string.
562
+ * @param defaultValue The default.
563
+ * @return A string which is the value.
564
+ */
565
+ public String getString(String key, String defaultValue) {
566
+ Object object = this.opt(key);
567
+ return NULL.equals(object) ? defaultValue : object.toString();
637
568
  }
638
- }
639
-
640
-
641
- /**
642
- * Get the long value associated with a key.
643
- *
644
- * @param key A key string.
645
- * @return The long value.
646
- * @throws RuntimeException if the key is not found or if the value cannot
647
- * be converted to a long.
648
- */
649
- public long getLong(String key) {
650
- Object object = this.get(key);
651
- try {
652
- return object instanceof Number
653
- ? ((Number)object).longValue()
654
- : Long.parseLong((String)object);
655
- } catch (Exception e) {
656
- throw new RuntimeException("JSONObject[" + quote(key) + "] is not a long.", e);
569
+
570
+ /**
571
+ * Gets the int value associated with a key
572
+ *
573
+ * @webref jsonobject:method
574
+ * @brief Gets the int value associated with a key
575
+ * @param key A key string.
576
+ * @return The integer value.
577
+ * @throws RuntimeException if the key is not found or if the value cannot
578
+ * be converted to an integer.
579
+ * @see JSONObject#getFloat(String)
580
+ * @see JSONObject#getString(String)
581
+ * @see JSONObject#getBoolean(String)
582
+ */
583
+ public int getInt(String key) {
584
+ Object object = this.get(key);
585
+ if (object == null) {
586
+ throw new RuntimeException("JSONObject[" + quote(key) + "] not found");
587
+ }
588
+ try {
589
+ return object instanceof Number
590
+ ? ((Number) object).intValue() : Integer.parseInt((String) object);
591
+ } catch (Exception e) {
592
+ throw new RuntimeException("JSONObject[" + quote(key) + "] is not an int.");
593
+ }
657
594
  }
658
- }
659
-
660
-
661
- /**
662
- * Get an optional long value associated with a key,
663
- * or the default if there is no such key or if the value is not a number.
664
- * If the value is a string, an attempt will be made to evaluate it as
665
- * a number.
666
- *
667
- * @param key A key string.
668
- * @param defaultValue The default.
669
- * @return An object which is the value.
670
- */
671
- public long getLong(String key, long defaultValue) {
672
- try {
673
- return this.getLong(key);
674
- } catch (Exception e) {
675
- return defaultValue;
595
+
596
+ /**
597
+ * Get an optional int value associated with a key, or the default if there
598
+ * is no such key or if the value is not a number. If the value is a string,
599
+ * an attempt will be made to evaluate it as a number.
600
+ *
601
+ * @param key A key string.
602
+ * @param defaultValue The default.
603
+ * @return An object which is the value.
604
+ */
605
+ public int getInt(String key, int defaultValue) {
606
+ try {
607
+ return this.getInt(key);
608
+ } catch (Exception e) {
609
+ return defaultValue;
610
+ }
676
611
  }
677
- }
678
-
679
-
680
- /**
681
- * @webref jsonobject:method
682
- * @brief Gets the float value associated with a key
683
- * @param key a key string
684
- * @see JSONObject#getInt(String)
685
- * @see JSONObject#getString(String)
686
- * @see JSONObject#getBoolean(String)
687
- */
688
- public float getFloat(String key) {
689
- return (float) getDouble(key);
690
- }
691
-
692
-
693
- public float getFloat(String key, float defaultValue) {
694
- try {
695
- return getFloat(key);
696
- } catch (Exception e) {
697
- return defaultValue;
612
+
613
+ /**
614
+ * Get the long value associated with a key.
615
+ *
616
+ * @param key A key string.
617
+ * @return The long value.
618
+ * @throws RuntimeException if the key is not found or if the value cannot
619
+ * be converted to a long.
620
+ */
621
+ public long getLong(String key) {
622
+ Object object = this.get(key);
623
+ try {
624
+ return object instanceof Number
625
+ ? ((Number) object).longValue()
626
+ : Long.parseLong((String) object);
627
+ } catch (Exception e) {
628
+ throw new RuntimeException("JSONObject[" + quote(key) + "] is not a long.", e);
629
+ }
698
630
  }
699
- }
700
-
701
-
702
- /**
703
- * Get the double value associated with a key.
704
- * @param key A key string.
705
- * @return The numeric value.
706
- * @throws RuntimeException if the key is not found or
707
- * if the value is not a Number object and cannot be converted to a number.
708
- */
709
- public double getDouble(String key) {
710
- Object object = this.get(key);
711
- try {
712
- return object instanceof Number
713
- ? ((Number)object).doubleValue()
714
- : Double.parseDouble((String)object);
715
- } catch (Exception e) {
716
- throw new RuntimeException("JSONObject[" + quote(key) + "] is not a number.");
631
+
632
+ /**
633
+ * Get an optional long value associated with a key, or the default if there
634
+ * is no such key or if the value is not a number. If the value is a string,
635
+ * an attempt will be made to evaluate it as a number.
636
+ *
637
+ * @param key A key string.
638
+ * @param defaultValue The default.
639
+ * @return An object which is the value.
640
+ */
641
+ public long getLong(String key, long defaultValue) {
642
+ try {
643
+ return this.getLong(key);
644
+ } catch (Exception e) {
645
+ return defaultValue;
646
+ }
717
647
  }
718
- }
719
-
720
-
721
- /**
722
- * Get an optional double associated with a key, or the
723
- * defaultValue if there is no such key or if its value is not a number.
724
- * If the value is a string, an attempt will be made to evaluate it as
725
- * a number.
726
- *
727
- * @param key A key string.
728
- * @param defaultValue The default.
729
- * @return An object which is the value.
730
- */
731
- public double getDouble(String key, double defaultValue) {
732
- try {
733
- return this.getDouble(key);
734
- } catch (Exception e) {
735
- return defaultValue;
648
+
649
+ /**
650
+ * @webref jsonobject:method
651
+ * @brief Gets the float value associated with a key
652
+ * @param key a key string
653
+ * @see JSONObject#getInt(String)
654
+ * @see JSONObject#getString(String)
655
+ * @see JSONObject#getBoolean(String)
656
+ */
657
+ public float getFloat(String key) {
658
+ return (float) getDouble(key);
736
659
  }
737
- }
738
-
739
-
740
- /**
741
- * Get the boolean value associated with a key.
742
- *
743
- * @webref jsonobject:method
744
- * @brief Gets the boolean value associated with a key
745
- * @param key a key string
746
- * @return The truth.
747
- * @throws RuntimeException if the value is not a Boolean or the String "true" or "false".
748
- * @see JSONObject#getInt(String)
749
- * @see JSONObject#getFloat(String)
750
- * @see JSONObject#getString(String)
751
- */
752
- public boolean getBoolean(String key) {
753
- Object object = this.get(key);
754
- if (object.equals(Boolean.FALSE) ||
755
- (object instanceof String &&
756
- ((String)object).equalsIgnoreCase("false"))) {
757
- return false;
758
- } else if (object.equals(Boolean.TRUE) ||
759
- (object instanceof String &&
760
- ((String)object).equalsIgnoreCase("true"))) {
761
- return true;
660
+
661
+ public float getFloat(String key, float defaultValue) {
662
+ try {
663
+ return getFloat(key);
664
+ } catch (Exception e) {
665
+ return defaultValue;
666
+ }
762
667
  }
763
- throw new RuntimeException("JSONObject[" + quote(key) + "] is not a Boolean.");
764
- }
765
-
766
-
767
- /**
768
- * Get an optional boolean associated with a key.
769
- * It returns the defaultValue if there is no such key, or if it is not
770
- * a Boolean or the String "true" or "false" (case insensitive).
771
- *
772
- * @param key A key string.
773
- * @param defaultValue The default.
774
- * @return The truth.
775
- */
776
- public boolean getBoolean(String key, boolean defaultValue) {
777
- try {
778
- return this.getBoolean(key);
779
- } catch (Exception e) {
780
- return defaultValue;
668
+
669
+ /**
670
+ * Get the double value associated with a key.
671
+ *
672
+ * @param key A key string.
673
+ * @return The numeric value.
674
+ * @throws RuntimeException if the key is not found or if the value is not a
675
+ * Number object and cannot be converted to a number.
676
+ */
677
+ public double getDouble(String key) {
678
+ Object object = this.get(key);
679
+ try {
680
+ return object instanceof Number
681
+ ? ((Number) object).doubleValue()
682
+ : Double.parseDouble((String) object);
683
+ } catch (Exception e) {
684
+ throw new RuntimeException("JSONObject[" + quote(key) + "] is not a number.");
685
+ }
781
686
  }
782
- }
783
-
784
-
785
- /**
786
- * Get the JSONArray value associated with a key.
787
- *
788
- * @webref jsonobject:method
789
- * @brief Gets the JSONArray value associated with a key
790
- * @param key a key string
791
- * @return A JSONArray which is the value, or null if not present
792
- * @throws RuntimeException if the value is not a JSONArray.
793
- * @see JSONObject#getJSONObject(String)
794
- * @see JSONObject#setJSONObject(String, JSONObject)
795
- * @see JSONObject#setJSONArray(String, JSONArray)
796
- */
797
- public JSONArray getJSONArray(String key) {
798
- Object object = this.get(key);
799
- if (object == null) {
800
- return null;
687
+
688
+ /**
689
+ * Get an optional double associated with a key, or the defaultValue if
690
+ * there is no such key or if its value is not a number. If the value is a
691
+ * string, an attempt will be made to evaluate it as a number.
692
+ *
693
+ * @param key A key string.
694
+ * @param defaultValue The default.
695
+ * @return An object which is the value.
696
+ */
697
+ public double getDouble(String key, double defaultValue) {
698
+ try {
699
+ return this.getDouble(key);
700
+ } catch (Exception e) {
701
+ return defaultValue;
702
+ }
801
703
  }
802
- if (object instanceof JSONArray) {
803
- return (JSONArray)object;
704
+
705
+ /**
706
+ * Get the boolean value associated with a key.
707
+ *
708
+ * @webref jsonobject:method
709
+ * @brief Gets the boolean value associated with a key
710
+ * @param key a key string
711
+ * @return The truth.
712
+ * @throws RuntimeException if the value is not a Boolean or the String
713
+ * "true" or "false".
714
+ * @see JSONObject#getInt(String)
715
+ * @see JSONObject#getFloat(String)
716
+ * @see JSONObject#getString(String)
717
+ */
718
+ public boolean getBoolean(String key) {
719
+ Object object = this.get(key);
720
+ if (object.equals(Boolean.FALSE)
721
+ || (object instanceof String
722
+ && ((String) object).equalsIgnoreCase("false"))) {
723
+ return false;
724
+ } else if (object.equals(Boolean.TRUE)
725
+ || (object instanceof String
726
+ && ((String) object).equalsIgnoreCase("true"))) {
727
+ return true;
728
+ }
729
+ throw new RuntimeException("JSONObject[" + quote(key) + "] is not a Boolean.");
804
730
  }
805
- throw new RuntimeException("JSONObject[" + quote(key) + "] is not a JSONArray.");
806
- }
807
-
808
-
809
- /**
810
- * Get the JSONObject value associated with a key.
811
- *
812
- * @webref jsonobject:method
813
- * @brief Gets the JSONObject value associated with a key
814
- * @param key a key string
815
- * @return A JSONObject which is the value or null if not available.
816
- * @throws RuntimeException if the value is not a JSONObject.
817
- * @see JSONObject#getJSONArray(String)
818
- * @see JSONObject#setJSONObject(String, JSONObject)
819
- * @see JSONObject#setJSONArray(String, JSONArray)
820
- */
821
- public JSONObject getJSONObject(String key) {
822
- Object object = this.get(key);
823
- if (object == null) {
824
- return null;
731
+
732
+ /**
733
+ * Get an optional boolean associated with a key. It returns the
734
+ * defaultValue if there is no such key, or if it is not a Boolean or the
735
+ * String "true" or "false" (case insensitive).
736
+ *
737
+ * @param key A key string.
738
+ * @param defaultValue The default.
739
+ * @return The truth.
740
+ */
741
+ public boolean getBoolean(String key, boolean defaultValue) {
742
+ try {
743
+ return this.getBoolean(key);
744
+ } catch (Exception e) {
745
+ return defaultValue;
746
+ }
825
747
  }
826
- if (object instanceof JSONObject) {
827
- return (JSONObject)object;
748
+
749
+ /**
750
+ * Get the JSONArray value associated with a key.
751
+ *
752
+ * @webref jsonobject:method
753
+ * @brief Gets the JSONArray value associated with a key
754
+ * @param key a key string
755
+ * @return A JSONArray which is the value, or null if not present
756
+ * @throws RuntimeException if the value is not a JSONArray.
757
+ * @see JSONObject#getJSONObject(String)
758
+ * @see JSONObject#setJSONObject(String, JSONObject)
759
+ * @see JSONObject#setJSONArray(String, JSONArray)
760
+ */
761
+ public JSONArray getJSONArray(String key) {
762
+ Object object = this.get(key);
763
+ if (object == null) {
764
+ return null;
765
+ }
766
+ if (object instanceof JSONArray) {
767
+ return (JSONArray) object;
768
+ }
769
+ throw new RuntimeException("JSONObject[" + quote(key) + "] is not a JSONArray.");
828
770
  }
829
- throw new RuntimeException("JSONObject[" + quote(key) + "] is not a JSONObject.");
830
- }
831
771
 
772
+ /**
773
+ * Get the JSONObject value associated with a key.
774
+ *
775
+ * @webref jsonobject:method
776
+ * @brief Gets the JSONObject value associated with a key
777
+ * @param key a key string
778
+ * @return A JSONObject which is the value or null if not available.
779
+ * @throws RuntimeException if the value is not a JSONObject.
780
+ * @see JSONObject#getJSONArray(String)
781
+ * @see JSONObject#setJSONObject(String, JSONObject)
782
+ * @see JSONObject#setJSONArray(String, JSONArray)
783
+ */
784
+ public JSONObject getJSONObject(String key) {
785
+ Object object = this.get(key);
786
+ if (object == null) {
787
+ return null;
788
+ }
789
+ if (object instanceof JSONObject) {
790
+ return (JSONObject) object;
791
+ }
792
+ throw new RuntimeException("JSONObject[" + quote(key) + "] is not a JSONObject.");
793
+ }
832
794
 
833
795
  // /**
834
796
  // * Get an array of field names from a JSONObject.
@@ -872,17 +834,15 @@ public class JSONObject {
872
834
  // }
873
835
  // return names;
874
836
  // }
875
-
876
-
877
- /**
878
- * Determine if the JSONObject contains a specific key.
879
- * @param key A key string.
880
- * @return true if the key exists in the JSONObject.
881
- */
882
- public boolean hasKey(String key) {
883
- return map.containsKey(key);
884
- }
885
-
837
+ /**
838
+ * Determine if the JSONObject contains a specific key.
839
+ *
840
+ * @param key A key string.
841
+ * @return true if the key exists in the JSONObject.
842
+ */
843
+ public boolean hasKey(String key) {
844
+ return map.containsKey(key);
845
+ }
886
846
 
887
847
  // /**
888
848
  // * Increment a property of a JSONObject. If there is no such property,
@@ -910,52 +870,46 @@ public class JSONObject {
910
870
  // }
911
871
  // return this;
912
872
  // }
873
+ /**
874
+ * Determine if the value associated with the key is null or if there is no
875
+ * value.
876
+ *
877
+ * @webref
878
+ * @param key A key string.
879
+ * @return true if there is no value associated with the key or if the value
880
+ * is the JSONObject.NULL object.
881
+ */
882
+ public boolean isNull(String key) {
883
+ return JSONObject.NULL.equals(this.opt(key));
884
+ }
913
885
 
914
-
915
- /**
916
- * Determine if the value associated with the key is null or if there is
917
- * no value.
918
- *
919
- * @webref
920
- * @param key A key string.
921
- * @return true if there is no value associated with the key or if
922
- * the value is the JSONObject.NULL object.
923
- */
924
- public boolean isNull(String key) {
925
- return JSONObject.NULL.equals(this.opt(key));
926
- }
927
-
928
-
929
- /**
930
- * Get an enumeration of the keys of the JSONObject.
931
- *
932
- * @return An iterator of the keys.
933
- */
934
- public Iterator keyIterator() {
886
+ /**
887
+ * Get an enumeration of the keys of the JSONObject.
888
+ *
889
+ * @return An iterator of the keys.
890
+ */
891
+ public Iterator keyIterator() {
935
892
  // return this.keySet().iterator();
936
- return map.keySet().iterator();
937
- }
938
-
939
-
940
- /**
941
- * Get a set of keys of the JSONObject.
942
- *
943
- * @return A keySet.
944
- */
945
- public Set keys() {
946
- return this.map.keySet();
947
- }
948
-
893
+ return map.keySet().iterator();
894
+ }
949
895
 
950
- /**
951
- * Get the number of keys stored in the JSONObject.
952
- *
953
- * @return The number of keys in the JSONObject.
954
- */
955
- public int size() {
956
- return this.map.size();
957
- }
896
+ /**
897
+ * Get a set of keys of the JSONObject.
898
+ *
899
+ * @return A keySet.
900
+ */
901
+ public Set keys() {
902
+ return this.map.keySet();
903
+ }
958
904
 
905
+ /**
906
+ * Get the number of keys stored in the JSONObject.
907
+ *
908
+ * @return The number of keys in the JSONObject.
909
+ */
910
+ public int size() {
911
+ return this.map.size();
912
+ }
959
913
 
960
914
  // /**
961
915
  // * Produce a JSONArray containing the names of the elements of this
@@ -971,45 +925,42 @@ public class JSONObject {
971
925
  // }
972
926
  // return ja.size() == 0 ? null : ja;
973
927
  // }
974
-
975
-
976
- /**
977
- * Produce a string from a Number.
978
- * @param number A Number
979
- * @return A String.
980
- * @throws RuntimeException If number is null or a non-finite number.
981
- */
982
- private static String numberToString(Number number) {
983
- if (number == null) {
984
- throw new RuntimeException("Null pointer");
928
+ /**
929
+ * Produce a string from a Number.
930
+ *
931
+ * @param number A Number
932
+ * @return A String.
933
+ * @throws RuntimeException If number is null or a non-finite number.
934
+ */
935
+ private static String numberToString(Number number) {
936
+ if (number == null) {
937
+ throw new RuntimeException("Null pointer");
938
+ }
939
+ testValidity(number);
940
+
941
+ // Shave off trailing zeros and decimal point, if possible.
942
+ String string = number.toString();
943
+ if (string.indexOf('.') > 0 && string.indexOf('e') < 0
944
+ && string.indexOf('E') < 0) {
945
+ while (string.endsWith("0")) {
946
+ string = string.substring(0, string.length() - 1);
947
+ }
948
+ if (string.endsWith(".")) {
949
+ string = string.substring(0, string.length() - 1);
950
+ }
951
+ }
952
+ return string;
985
953
  }
986
- testValidity(number);
987
954
 
988
- // Shave off trailing zeros and decimal point, if possible.
989
-
990
- String string = number.toString();
991
- if (string.indexOf('.') > 0 && string.indexOf('e') < 0 &&
992
- string.indexOf('E') < 0) {
993
- while (string.endsWith("0")) {
994
- string = string.substring(0, string.length() - 1);
995
- }
996
- if (string.endsWith(".")) {
997
- string = string.substring(0, string.length() - 1);
998
- }
955
+ /**
956
+ * Get an optional value associated with a key.
957
+ *
958
+ * @param key A key string.
959
+ * @return An object which is the value, or null if there is no value.
960
+ */
961
+ private Object opt(String key) {
962
+ return key == null ? null : this.map.get(key);
999
963
  }
1000
- return string;
1001
- }
1002
-
1003
-
1004
- /**
1005
- * Get an optional value associated with a key.
1006
- * @param key A key string.
1007
- * @return An object which is the value, or null if there is no value.
1008
- */
1009
- private Object opt(String key) {
1010
- return key == null ? null : this.map.get(key);
1011
- }
1012
-
1013
964
 
1014
965
  // /**
1015
966
  // * Get an optional boolean associated with a key.
@@ -1022,8 +973,6 @@ public class JSONObject {
1022
973
  // private boolean optBoolean(String key) {
1023
974
  // return this.optBoolean(key, false);
1024
975
  // }
1025
-
1026
-
1027
976
  // /**
1028
977
  // * Get an optional double associated with a key,
1029
978
  // * or NaN if there is no such key or if its value is not a number.
@@ -1036,8 +985,6 @@ public class JSONObject {
1036
985
  // private double optDouble(String key) {
1037
986
  // return this.optDouble(key, Double.NaN);
1038
987
  // }
1039
-
1040
-
1041
988
  // /**
1042
989
  // * Get an optional int value associated with a key,
1043
990
  // * or zero if there is no such key or if the value is not a number.
@@ -1050,8 +997,6 @@ public class JSONObject {
1050
997
  // private int optInt(String key) {
1051
998
  // return this.optInt(key, 0);
1052
999
  // }
1053
-
1054
-
1055
1000
  // /**
1056
1001
  // * Get an optional JSONArray associated with a key.
1057
1002
  // * It returns null if there is no such key, or if its value is not a
@@ -1064,8 +1009,6 @@ public class JSONObject {
1064
1009
  // Object o = this.opt(key);
1065
1010
  // return o instanceof JSONArray ? (JSONArray)o : null;
1066
1011
  // }
1067
-
1068
-
1069
1012
  // /**
1070
1013
  // * Get an optional JSONObject associated with a key.
1071
1014
  // * It returns null if there is no such key, or if its value is not a
@@ -1078,8 +1021,6 @@ public class JSONObject {
1078
1021
  // Object object = this.opt(key);
1079
1022
  // return object instanceof JSONObject ? (JSONObject)object : null;
1080
1023
  // }
1081
-
1082
-
1083
1024
  // /**
1084
1025
  // * Get an optional long value associated with a key,
1085
1026
  // * or zero if there is no such key or if the value is not a number.
@@ -1092,8 +1033,6 @@ public class JSONObject {
1092
1033
  // public long optLong(String key) {
1093
1034
  // return this.optLong(key, 0);
1094
1035
  // }
1095
-
1096
-
1097
1036
  // /**
1098
1037
  // * Get an optional string associated with a key.
1099
1038
  // * It returns an empty string if there is no such key. If the value is not
@@ -1105,176 +1044,169 @@ public class JSONObject {
1105
1044
  // public String optString(String key) {
1106
1045
  // return this.optString(key, "");
1107
1046
  // }
1047
+ private void populateMap(Object bean) {
1048
+ Class klass = bean.getClass();
1049
+
1050
+ // If klass is a System class then set includeSuperClass to false.
1051
+ boolean includeSuperClass = klass.getClassLoader() != null;
1052
+
1053
+ Method[] methods = includeSuperClass
1054
+ ? klass.getMethods()
1055
+ : klass.getDeclaredMethods();
1056
+ for (int i = 0; i < methods.length; i += 1) {
1057
+ try {
1058
+ Method method = methods[i];
1059
+ if (Modifier.isPublic(method.getModifiers())) {
1060
+ String name = method.getName();
1061
+ String key = "";
1062
+ if (name.startsWith("get")) {
1063
+ if ("getClass".equals(name)
1064
+ || "getDeclaringClass".equals(name)) {
1065
+ key = "";
1066
+ } else {
1067
+ key = name.substring(3);
1068
+ }
1069
+ } else if (name.startsWith("is")) {
1070
+ key = name.substring(2);
1071
+ }
1072
+ if (key.length() > 0
1073
+ && Character.isUpperCase(key.charAt(0))
1074
+ && method.getParameterTypes().length == 0) {
1075
+ if (key.length() == 1) {
1076
+ key = key.toLowerCase();
1077
+ } else if (!Character.isUpperCase(key.charAt(1))) {
1078
+ key = key.substring(0, 1).toLowerCase()
1079
+ + key.substring(1);
1080
+ }
1081
+
1082
+ Object result = method.invoke(bean, (Object[]) null);
1083
+ if (result != null) {
1084
+ this.map.put(key, wrap(result));
1085
+ }
1086
+ }
1087
+ }
1088
+ } catch (Exception ignore) {
1089
+ }
1090
+ }
1091
+ }
1108
1092
 
1093
+ /**
1094
+ * @webref jsonobject:method
1095
+ * @brief Put a key/String pair in the JSONObject
1096
+ * @param key a key string
1097
+ * @param value the value to assign
1098
+ * @see JSONObject#setInt(String, int)
1099
+ * @see JSONObject#setFloat(String, float)
1100
+ * @see JSONObject#setBoolean(String, boolean)
1101
+ */
1102
+ public JSONObject setString(String key, String value) {
1103
+ return put(key, value);
1104
+ }
1109
1105
 
1110
- private void populateMap(Object bean) {
1111
- Class klass = bean.getClass();
1106
+ /**
1107
+ * Put a key/int pair in the JSONObject.
1108
+ *
1109
+ * @webref jsonobject:method
1110
+ * @brief Put a key/int pair in the JSONObject
1111
+ * @param key a key string
1112
+ * @param value the value to assign
1113
+ * @return this.
1114
+ * @throws RuntimeException If the key is null.
1115
+ * @see JSONObject#setFloat(String, float)
1116
+ * @see JSONObject#setString(String, String)
1117
+ * @see JSONObject#setBoolean(String, boolean)
1118
+ */
1119
+ public JSONObject setInt(String key, int value) {
1120
+ this.put(key, Integer.valueOf(value));
1121
+ return this;
1122
+ }
1112
1123
 
1113
- // If klass is a System class then set includeSuperClass to false.
1124
+ /**
1125
+ * Put a key/long pair in the JSONObject.
1126
+ *
1127
+ * @param key A key string.
1128
+ * @param value A long which is the value.
1129
+ * @return this.
1130
+ * @throws RuntimeException If the key is null.
1131
+ */
1132
+ public JSONObject setLong(String key, long value) {
1133
+ this.put(key, Long.valueOf(value));
1134
+ return this;
1135
+ }
1114
1136
 
1115
- boolean includeSuperClass = klass.getClassLoader() != null;
1137
+ /**
1138
+ * @webref jsonobject:method
1139
+ * @brief Put a key/float pair in the JSONObject
1140
+ * @param key a key string
1141
+ * @param value the value to assign
1142
+ * @throws RuntimeException If the key is null or if the number is NaN or
1143
+ * infinite.
1144
+ * @see JSONObject#setInt(String, int)
1145
+ * @see JSONObject#setString(String, String)
1146
+ * @see JSONObject#setBoolean(String, boolean)
1147
+ */
1148
+ public JSONObject setFloat(String key, float value) {
1149
+ this.put(key, Double.valueOf(value));
1150
+ return this;
1151
+ }
1116
1152
 
1117
- Method[] methods = includeSuperClass
1118
- ? klass.getMethods()
1119
- : klass.getDeclaredMethods();
1120
- for (int i = 0; i < methods.length; i += 1) {
1121
- try {
1122
- Method method = methods[i];
1123
- if (Modifier.isPublic(method.getModifiers())) {
1124
- String name = method.getName();
1125
- String key = "";
1126
- if (name.startsWith("get")) {
1127
- if ("getClass".equals(name) ||
1128
- "getDeclaringClass".equals(name)) {
1129
- key = "";
1130
- } else {
1131
- key = name.substring(3);
1132
- }
1133
- } else if (name.startsWith("is")) {
1134
- key = name.substring(2);
1135
- }
1136
- if (key.length() > 0 &&
1137
- Character.isUpperCase(key.charAt(0)) &&
1138
- method.getParameterTypes().length == 0) {
1139
- if (key.length() == 1) {
1140
- key = key.toLowerCase();
1141
- } else if (!Character.isUpperCase(key.charAt(1))) {
1142
- key = key.substring(0, 1).toLowerCase() +
1143
- key.substring(1);
1144
- }
1145
-
1146
- Object result = method.invoke(bean, (Object[])null);
1147
- if (result != null) {
1148
- this.map.put(key, wrap(result));
1149
- }
1150
- }
1151
- }
1152
- } catch (Exception ignore) {
1153
- }
1154
- }
1155
- }
1156
-
1157
-
1158
- /**
1159
- * @webref jsonobject:method
1160
- * @brief Put a key/String pair in the JSONObject
1161
- * @param key a key string
1162
- * @param value the value to assign
1163
- * @see JSONObject#setInt(String, int)
1164
- * @see JSONObject#setFloat(String, float)
1165
- * @see JSONObject#setBoolean(String, boolean)
1166
- */
1167
- public JSONObject setString(String key, String value) {
1168
- return put(key, value);
1169
- }
1170
-
1171
-
1172
- /**
1173
- * Put a key/int pair in the JSONObject.
1174
- *
1175
- * @webref jsonobject:method
1176
- * @brief Put a key/int pair in the JSONObject
1177
- * @param key a key string
1178
- * @param value the value to assign
1179
- * @return this.
1180
- * @throws RuntimeException If the key is null.
1181
- * @see JSONObject#setFloat(String, float)
1182
- * @see JSONObject#setString(String, String)
1183
- * @see JSONObject#setBoolean(String, boolean)
1184
- */
1185
- public JSONObject setInt(String key, int value) {
1186
- this.put(key, Integer.valueOf(value));
1187
- return this;
1188
- }
1189
-
1190
-
1191
- /**
1192
- * Put a key/long pair in the JSONObject.
1193
- *
1194
- * @param key A key string.
1195
- * @param value A long which is the value.
1196
- * @return this.
1197
- * @throws RuntimeException If the key is null.
1198
- */
1199
- public JSONObject setLong(String key, long value) {
1200
- this.put(key, Long.valueOf(value));
1201
- return this;
1202
- }
1203
-
1204
- /**
1205
- * @webref jsonobject:method
1206
- * @brief Put a key/float pair in the JSONObject
1207
- * @param key a key string
1208
- * @param value the value to assign
1209
- * @throws RuntimeException If the key is null or if the number is NaN or infinite.
1210
- * @see JSONObject#setInt(String, int)
1211
- * @see JSONObject#setString(String, String)
1212
- * @see JSONObject#setBoolean(String, boolean)
1213
- */
1214
- public JSONObject setFloat(String key, float value) {
1215
- this.put(key, Double.valueOf(value));
1216
- return this;
1217
- }
1218
-
1219
-
1220
- /**
1221
- * Put a key/double pair in the JSONObject.
1222
- *
1223
- * @param key A key string.
1224
- * @param value A double which is the value.
1225
- * @return this.
1226
- * @throws RuntimeException If the key is null or if the number is NaN or infinite.
1227
- */
1228
- public JSONObject setDouble(String key, double value) {
1229
- this.put(key, Double.valueOf(value));
1230
- return this;
1231
- }
1232
-
1233
-
1234
- /**
1235
- * Put a key/boolean pair in the JSONObject.
1236
- *
1237
- * @webref jsonobject:method
1238
- * @brief Put a key/boolean pair in the JSONObject
1239
- * @param key a key string
1240
- * @param value the value to assign
1241
- * @return this.
1242
- * @throws RuntimeException If the key is null.
1243
- * @see JSONObject#setInt(String, int)
1244
- * @see JSONObject#setFloat(String, float)
1245
- * @see JSONObject#setString(String, String)
1246
- */
1247
- public JSONObject setBoolean(String key, boolean value) {
1248
- this.put(key, value ? Boolean.TRUE : Boolean.FALSE);
1249
- return this;
1250
- }
1251
-
1252
- /**
1253
- * @webref jsonobject:method
1254
- * @brief Sets the JSONObject value associated with a key
1255
- * @param key a key string
1256
- * @param value value to assign
1257
- * @see JSONObject#setJSONArray(String, JSONArray)
1258
- * @see JSONObject#getJSONObject(String)
1259
- * @see JSONObject#getJSONArray(String)
1260
- */
1261
- public JSONObject setJSONObject(String key, JSONObject value) {
1262
- return put(key, value);
1263
- }
1264
-
1265
- /**
1266
- * @webref jsonobject:method
1267
- * @brief Sets the JSONArray value associated with a key
1268
- * @param key a key string
1269
- * @param value value to assign
1270
- * @see JSONObject#setJSONObject(String, JSONObject)
1271
- * @see JSONObject#getJSONObject(String)
1272
- * @see JSONObject#getJSONArray(String)
1273
- */
1274
- public JSONObject setJSONArray(String key, JSONArray value) {
1275
- return put(key, value);
1276
- }
1153
+ /**
1154
+ * Put a key/double pair in the JSONObject.
1155
+ *
1156
+ * @param key A key string.
1157
+ * @param value A double which is the value.
1158
+ * @return this.
1159
+ * @throws RuntimeException If the key is null or if the number is NaN or
1160
+ * infinite.
1161
+ */
1162
+ public JSONObject setDouble(String key, double value) {
1163
+ this.put(key, Double.valueOf(value));
1164
+ return this;
1165
+ }
1277
1166
 
1167
+ /**
1168
+ * Put a key/boolean pair in the JSONObject.
1169
+ *
1170
+ * @webref jsonobject:method
1171
+ * @brief Put a key/boolean pair in the JSONObject
1172
+ * @param key a key string
1173
+ * @param value the value to assign
1174
+ * @return this.
1175
+ * @throws RuntimeException If the key is null.
1176
+ * @see JSONObject#setInt(String, int)
1177
+ * @see JSONObject#setFloat(String, float)
1178
+ * @see JSONObject#setString(String, String)
1179
+ */
1180
+ public JSONObject setBoolean(String key, boolean value) {
1181
+ this.put(key, value ? Boolean.TRUE : Boolean.FALSE);
1182
+ return this;
1183
+ }
1184
+
1185
+ /**
1186
+ * @webref jsonobject:method
1187
+ * @brief Sets the JSONObject value associated with a key
1188
+ * @param key a key string
1189
+ * @param value value to assign
1190
+ * @see JSONObject#setJSONArray(String, JSONArray)
1191
+ * @see JSONObject#getJSONObject(String)
1192
+ * @see JSONObject#getJSONArray(String)
1193
+ */
1194
+ public JSONObject setJSONObject(String key, JSONObject value) {
1195
+ return put(key, value);
1196
+ }
1197
+
1198
+ /**
1199
+ * @webref jsonobject:method
1200
+ * @brief Sets the JSONArray value associated with a key
1201
+ * @param key a key string
1202
+ * @param value value to assign
1203
+ * @see JSONObject#setJSONObject(String, JSONObject)
1204
+ * @see JSONObject#getJSONObject(String)
1205
+ * @see JSONObject#getJSONArray(String)
1206
+ */
1207
+ public JSONObject setJSONArray(String key, JSONArray value) {
1208
+ return put(key, value);
1209
+ }
1278
1210
 
1279
1211
  // /**
1280
1212
  // * Put a key/value pair in the JSONObject, where the value will be a
@@ -1288,8 +1220,6 @@ public class JSONObject {
1288
1220
  // this.put(key, new JSONArray(value));
1289
1221
  // return this;
1290
1222
  // }
1291
-
1292
-
1293
1223
  // /**
1294
1224
  // * Put a key/value pair in the JSONObject, where the value will be a
1295
1225
  // * JSONObject which is produced from a Map.
@@ -1303,63 +1233,61 @@ public class JSONObject {
1303
1233
  // this.put(key, new JSONObject(value));
1304
1234
  // return this;
1305
1235
  // }
1306
-
1307
-
1308
- /**
1309
- * Put a key/value pair in the JSONObject. If the value is null,
1310
- * then the key will be removed from the JSONObject if it is present.
1311
- * @param key A key string.
1312
- * @param value An object which is the value. It should be of one of these
1313
- * types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String,
1314
- * or the JSONObject.NULL object.
1315
- * @return this.
1316
- * @throws RuntimeException If the value is non-finite number
1317
- * or if the key is null.
1318
- */
1319
- public JSONObject put(String key, Object value) {
1320
- String pooled;
1321
- if (key == null) {
1322
- throw new RuntimeException("Null key.");
1323
- }
1324
- if (value != null) {
1325
- testValidity(value);
1326
- pooled = (String)keyPool.get(key);
1327
- if (pooled == null) {
1328
- if (keyPool.size() >= keyPoolSize) {
1329
- keyPool = new HashMap<>(keyPoolSize);
1236
+ /**
1237
+ * Put a key/value pair in the JSONObject. If the value is null, then the
1238
+ * key will be removed from the JSONObject if it is present.
1239
+ *
1240
+ * @param key A key string.
1241
+ * @param value An object which is the value. It should be of one of these
1242
+ * types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or
1243
+ * the JSONObject.NULL object.
1244
+ * @return this.
1245
+ * @throws RuntimeException If the value is non-finite number or if the key
1246
+ * is null.
1247
+ */
1248
+ public JSONObject put(String key, Object value) {
1249
+ String pooled;
1250
+ if (key == null) {
1251
+ throw new RuntimeException("Null key.");
1330
1252
  }
1331
- keyPool.put(key, key);
1332
- } else {
1333
- key = pooled;
1334
- }
1335
- this.map.put(key, value);
1336
- } else {
1337
- this.remove(key);
1338
- }
1339
- return this;
1340
- }
1341
-
1342
-
1343
- /**
1344
- * Put a key/value pair in the JSONObject, but only if the key and the
1345
- * value are both non-null, and only if there is not already a member
1346
- * with that name.
1347
- * @param key
1348
- * @param value
1349
- * @return {@code this}.
1350
- * @throws RuntimeException if the key is a duplicate, or if
1351
- * {@link #put(String,Object)} throws.
1352
- */
1353
- private JSONObject putOnce(String key, Object value) {
1354
- if (key != null && value != null) {
1355
- if (this.opt(key) != null) {
1356
- throw new RuntimeException("Duplicate key \"" + key + "\"");
1357
- }
1358
- this.put(key, value);
1253
+ if (value != null) {
1254
+ testValidity(value);
1255
+ pooled = (String) keyPool.get(key);
1256
+ if (pooled == null) {
1257
+ if (keyPool.size() >= keyPoolSize) {
1258
+ keyPool = new HashMap<>(keyPoolSize);
1259
+ }
1260
+ keyPool.put(key, key);
1261
+ } else {
1262
+ key = pooled;
1263
+ }
1264
+ this.map.put(key, value);
1265
+ } else {
1266
+ this.remove(key);
1267
+ }
1268
+ return this;
1359
1269
  }
1360
- return this;
1361
- }
1362
1270
 
1271
+ /**
1272
+ * Put a key/value pair in the JSONObject, but only if the key and the value
1273
+ * are both non-null, and only if there is not already a member with that
1274
+ * name.
1275
+ *
1276
+ * @param key
1277
+ * @param value
1278
+ * @return {@code this}.
1279
+ * @throws RuntimeException if the key is a duplicate, or if
1280
+ * {@link #put(String,Object)} throws.
1281
+ */
1282
+ private JSONObject putOnce(String key, Object value) {
1283
+ if (key != null && value != null) {
1284
+ if (this.opt(key) != null) {
1285
+ throw new RuntimeException("Duplicate key \"" + key + "\"");
1286
+ }
1287
+ this.put(key, value);
1288
+ }
1289
+ return this;
1290
+ }
1363
1291
 
1364
1292
  // /**
1365
1293
  // * Put a key/value pair in the JSONObject, but only if the
@@ -1377,174 +1305,170 @@ public class JSONObject {
1377
1305
  // }
1378
1306
  // return this;
1379
1307
  // }
1308
+ /**
1309
+ * Produce a string in double quotes with backslash sequences in all the
1310
+ * right places. A backslash will be inserted within </, producing <\/,
1311
+ * allowing JSON text to be delivered in HTML. In JSON text, a string cannot
1312
+ * contain a control character or an unescaped quote or backslash. @param
1313
+ * string A String @return A String correctly formatted for insertion in a
1314
+ * JSON text.
1315
+ */
1316
+ static public String quote(String string) {
1317
+ StringWriter sw = new StringWriter();
1318
+ synchronized (sw.getBuffer()) {
1319
+ try {
1320
+ return quote(string, sw).toString();
1321
+ } catch (IOException ignored) {
1322
+ // will never happen - we are writing to a string writer
1323
+ return "";
1324
+ }
1325
+ }
1326
+ }
1380
1327
 
1328
+ static public Writer quote(String string, Writer w) throws IOException {
1329
+ if (string == null || string.length() == 0) {
1330
+ w.write("\"\"");
1331
+ return w;
1332
+ }
1381
1333
 
1382
- /**
1383
- * Produce a string in double quotes with backslash sequences in all the
1384
- * right places. A backslash will be inserted within </, producing <\/,
1385
- * allowing JSON text to be delivered in HTML. In JSON text, a string
1386
- * cannot contain a control character or an unescaped quote or backslash.
1387
- * @param string A String
1388
- * @return A String correctly formatted for insertion in a JSON text.
1389
- */
1390
- static public String quote(String string) {
1391
- StringWriter sw = new StringWriter();
1392
- synchronized (sw.getBuffer()) {
1393
- try {
1394
- return quote(string, sw).toString();
1395
- } catch (IOException ignored) {
1396
- // will never happen - we are writing to a string writer
1397
- return "";
1398
- }
1334
+ char b;
1335
+ char c = 0;
1336
+ String hhhh;
1337
+ int i;
1338
+ int len = string.length();
1339
+
1340
+ w.write('"');
1341
+ for (i = 0; i < len; i += 1) {
1342
+ b = c;
1343
+ c = string.charAt(i);
1344
+ switch (c) {
1345
+ case '\\':
1346
+ case '"':
1347
+ w.write('\\');
1348
+ w.write(c);
1349
+ break;
1350
+ case '/':
1351
+ if (b == '<') {
1352
+ w.write('\\');
1353
+ }
1354
+ w.write(c);
1355
+ break;
1356
+ case '\b':
1357
+ w.write("\\b");
1358
+ break;
1359
+ case '\t':
1360
+ w.write("\\t");
1361
+ break;
1362
+ case '\n':
1363
+ w.write("\\n");
1364
+ break;
1365
+ case '\f':
1366
+ w.write("\\f");
1367
+ break;
1368
+ case '\r':
1369
+ w.write("\\r");
1370
+ break;
1371
+ default:
1372
+ if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
1373
+ || (c >= '\u2000' && c < '\u2100')) {
1374
+ w.write("\\u");
1375
+ hhhh = Integer.toHexString(c);
1376
+ w.write("0000", 0, 4 - hhhh.length());
1377
+ w.write(hhhh);
1378
+ } else {
1379
+ w.write(c);
1380
+ }
1381
+ }
1382
+ }
1383
+ w.write('"');
1384
+ return w;
1399
1385
  }
1400
- }
1401
1386
 
1402
- static public Writer quote(String string, Writer w) throws IOException {
1403
- if (string == null || string.length() == 0) {
1404
- w.write("\"\"");
1405
- return w;
1387
+ /**
1388
+ * Remove a name and its value, if present.
1389
+ *
1390
+ * @param key The name to be removed.
1391
+ * @return The value that was associated with the name, or null if there was
1392
+ * no value.
1393
+ */
1394
+ public Object remove(String key) {
1395
+ return this.map.remove(key);
1406
1396
  }
1407
1397
 
1408
- char b;
1409
- char c = 0;
1410
- String hhhh;
1411
- int i;
1412
- int len = string.length();
1413
-
1414
- w.write('"');
1415
- for (i = 0; i < len; i += 1) {
1416
- b = c;
1417
- c = string.charAt(i);
1418
- switch (c) {
1419
- case '\\':
1420
- case '"':
1421
- w.write('\\');
1422
- w.write(c);
1423
- break;
1424
- case '/':
1425
- if (b == '<') {
1426
- w.write('\\');
1398
+ /**
1399
+ * Try to convert a string into a number, boolean, or null. If the string
1400
+ * can't be converted, return the string.
1401
+ *
1402
+ * @param string A String.
1403
+ * @return A simple JSON value.
1404
+ */
1405
+ static protected Object stringToValue(String string) {
1406
+ Double d;
1407
+ if (string.equals("")) {
1408
+ return string;
1427
1409
  }
1428
- w.write(c);
1429
- break;
1430
- case '\b':
1431
- w.write("\\b");
1432
- break;
1433
- case '\t':
1434
- w.write("\\t");
1435
- break;
1436
- case '\n':
1437
- w.write("\\n");
1438
- break;
1439
- case '\f':
1440
- w.write("\\f");
1441
- break;
1442
- case '\r':
1443
- w.write("\\r");
1444
- break;
1445
- default:
1446
- if (c < ' ' || (c >= '\u0080' && c < '\u00a0')
1447
- || (c >= '\u2000' && c < '\u2100')) {
1448
- w.write("\\u");
1449
- hhhh = Integer.toHexString(c);
1450
- w.write("0000", 0, 4 - hhhh.length());
1451
- w.write(hhhh);
1452
- } else {
1453
- w.write(c);
1410
+ if (string.equalsIgnoreCase("true")) {
1411
+ return Boolean.TRUE;
1412
+ }
1413
+ if (string.equalsIgnoreCase("false")) {
1414
+ return Boolean.FALSE;
1415
+ }
1416
+ if (string.equalsIgnoreCase("null")) {
1417
+ return JSONObject.NULL;
1454
1418
  }
1455
- }
1456
- }
1457
- w.write('"');
1458
- return w;
1459
- }
1460
-
1461
-
1462
- /**
1463
- * Remove a name and its value, if present.
1464
- * @param key The name to be removed.
1465
- * @return The value that was associated with the name,
1466
- * or null if there was no value.
1467
- */
1468
- public Object remove(String key) {
1469
- return this.map.remove(key);
1470
- }
1471
-
1472
-
1473
- /**
1474
- * Try to convert a string into a number, boolean, or null. If the string
1475
- * can't be converted, return the string.
1476
- * @param string A String.
1477
- * @return A simple JSON value.
1478
- */
1479
- static protected Object stringToValue(String string) {
1480
- Double d;
1481
- if (string.equals("")) {
1482
- return string;
1483
- }
1484
- if (string.equalsIgnoreCase("true")) {
1485
- return Boolean.TRUE;
1486
- }
1487
- if (string.equalsIgnoreCase("false")) {
1488
- return Boolean.FALSE;
1489
- }
1490
- if (string.equalsIgnoreCase("null")) {
1491
- return JSONObject.NULL;
1492
- }
1493
1419
 
1494
- /*
1420
+ /*
1495
1421
  * If it might be a number, try converting it.
1496
1422
  * If a number cannot be produced, then the value will just
1497
1423
  * be a string. Note that the plus and implied string
1498
1424
  * conventions are non-standard. A JSON parser may accept
1499
1425
  * non-JSON forms as long as it accepts all correct JSON forms.
1500
- */
1501
-
1502
- char b = string.charAt(0);
1503
- if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') {
1504
- try {
1505
- if (string.indexOf('.') > -1 ||
1506
- string.indexOf('e') > -1 || string.indexOf('E') > -1) {
1507
- d = Double.valueOf(string);
1508
- if (!d.isInfinite() && !d.isNaN()) {
1509
- return d;
1510
- }
1511
- } else {
1512
- Long myLong = Long.valueOf(string);
1513
- if (myLong.longValue() == myLong.intValue()) {
1514
- return Integer.valueOf(myLong.intValue());
1515
- } else {
1516
- return myLong;
1517
- }
1426
+ */
1427
+ char b = string.charAt(0);
1428
+ if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') {
1429
+ try {
1430
+ if (string.indexOf('.') > -1
1431
+ || string.indexOf('e') > -1 || string.indexOf('E') > -1) {
1432
+ d = Double.valueOf(string);
1433
+ if (!d.isInfinite() && !d.isNaN()) {
1434
+ return d;
1435
+ }
1436
+ } else {
1437
+ Long myLong = Long.valueOf(string);
1438
+ if (myLong.longValue() == myLong.intValue()) {
1439
+ return Integer.valueOf(myLong.intValue());
1440
+ } else {
1441
+ return myLong;
1442
+ }
1443
+ }
1444
+ } catch (Exception ignore) {
1445
+ }
1518
1446
  }
1519
- } catch (Exception ignore) {
1520
- }
1447
+ return string;
1521
1448
  }
1522
- return string;
1523
- }
1524
-
1525
-
1526
- /**
1527
- * Throw an exception if the object is a NaN or infinite number.
1528
- * @param o The object to test. If not Float or Double, accepted without
1529
- * exceptions.
1530
- * @throws RuntimeException If o is infinite or NaN.
1531
- */
1532
- static protected void testValidity(Object o) {
1533
- if (o != null) {
1534
- if (o instanceof Double) {
1535
- if (((Double)o).isInfinite() || ((Double)o).isNaN()) {
1536
- throw new RuntimeException(
1537
- "JSON does not allow non-finite numbers.");
1538
- }
1539
- } else if (o instanceof Float) {
1540
- if (((Float)o).isInfinite() || ((Float)o).isNaN()) {
1541
- throw new RuntimeException(
1542
- "JSON does not allow non-finite numbers.");
1449
+
1450
+ /**
1451
+ * Throw an exception if the object is a NaN or infinite number.
1452
+ *
1453
+ * @param o The object to test. If not Float or Double, accepted without
1454
+ * exceptions.
1455
+ * @throws RuntimeException If o is infinite or NaN.
1456
+ */
1457
+ static protected void testValidity(Object o) {
1458
+ if (o != null) {
1459
+ if (o instanceof Double) {
1460
+ if (((Double) o).isInfinite() || ((Double) o).isNaN()) {
1461
+ throw new RuntimeException(
1462
+ "JSON does not allow non-finite numbers.");
1463
+ }
1464
+ } else if (o instanceof Float) {
1465
+ if (((Float) o).isInfinite() || ((Float) o).isNaN()) {
1466
+ throw new RuntimeException(
1467
+ "JSON does not allow non-finite numbers.");
1468
+ }
1469
+ }
1543
1470
  }
1544
- }
1545
1471
  }
1546
- }
1547
-
1548
1472
 
1549
1473
  // /**
1550
1474
  // * Produce a JSONArray containing the values of the members of this
@@ -1564,108 +1488,101 @@ public class JSONObject {
1564
1488
  // }
1565
1489
  // return ja;
1566
1490
  // }
1567
-
1568
-
1569
1491
  // protected boolean save(OutputStream output) {
1570
1492
  // return save(PApplet.createWriter(output));
1571
1493
  // }
1494
+ public boolean save(File file, String options) {
1495
+ PrintWriter writer = PApplet.createWriter(file);
1496
+ boolean success = write(writer, options);
1497
+ writer.close();
1498
+ return success;
1499
+ }
1572
1500
 
1501
+ public boolean write(PrintWriter output) {
1502
+ return write(output, null);
1503
+ }
1573
1504
 
1574
- public boolean save(File file, String options) {
1575
- PrintWriter writer = PApplet.createWriter(file);
1576
- boolean success = write(writer, options);
1577
- writer.close();
1578
- return success;
1579
- }
1580
-
1581
-
1582
- public boolean write(PrintWriter output) {
1583
- return write(output, null);
1584
- }
1585
-
1586
-
1587
- public boolean write(PrintWriter output, String options) {
1588
- int indentFactor = 2;
1589
- if (options != null) {
1590
- String[] opts = PApplet.split(options, ',');
1591
- for (String opt : opts) {
1592
- if (opt.equals("compact")) {
1593
- indentFactor = -1;
1594
- } else if (opt.startsWith("indent=")) {
1595
- indentFactor = PApplet.parseInt(opt.substring(7), -2);
1596
- if (indentFactor == -2) {
1597
- throw new IllegalArgumentException("Could not read a number from " + opt);
1598
- }
1599
- } else {
1600
- System.err.println("Ignoring " + opt);
1505
+ public boolean write(PrintWriter output, String options) {
1506
+ int indentFactor = 2;
1507
+ if (options != null) {
1508
+ String[] opts = PApplet.split(options, ',');
1509
+ for (String opt : opts) {
1510
+ if (opt.equals("compact")) {
1511
+ indentFactor = -1;
1512
+ } else if (opt.startsWith("indent=")) {
1513
+ indentFactor = PApplet.parseInt(opt.substring(7), -2);
1514
+ if (indentFactor == -2) {
1515
+ throw new IllegalArgumentException("Could not read a number from " + opt);
1516
+ }
1517
+ } else {
1518
+ System.err.println("Ignoring " + opt);
1519
+ }
1520
+ }
1601
1521
  }
1602
- }
1522
+ output.print(format(indentFactor));
1523
+ output.flush();
1524
+ return true;
1603
1525
  }
1604
- output.print(format(indentFactor));
1605
- output.flush();
1606
- return true;
1607
- }
1608
-
1609
-
1610
- /**
1611
- * Return the JSON data formatted with two spaces for indents.
1612
- * Chosen to do this since it's the most common case (e.g. with println()).
1613
- * Same as format(2). Use the format() function for more options.
1614
- */
1615
- @Override
1616
- public String toString() {
1617
- try {
1618
- return format(2);
1619
- } catch (Exception e) {
1620
- return null;
1621
- }
1622
- }
1623
-
1624
-
1625
- /**
1626
- * Make a prettyprinted JSON text of this JSONObject.
1627
- * <p>
1628
- * Warning: This method assumes that the data structure is acyclical.
1629
- * @param indentFactor The number of spaces to add to each level of
1630
- * indentation.
1631
- * @return a printable, displayable, portable, transmittable
1632
- * representation of the object, beginning
1633
- * with <code>{</code>&nbsp;<small>(left brace)</small> and ending
1634
- * with <code>}</code>&nbsp;<small>(right brace)</small>.
1635
- * @throws RuntimeException If the object contains an invalid number.
1636
- */
1637
- public String format(int indentFactor) {
1638
- StringWriter w = new StringWriter();
1639
- synchronized (w.getBuffer()) {
1640
- return this.writeInternal(w, indentFactor, 0).toString();
1526
+
1527
+ /**
1528
+ * Return the JSON data formatted with two spaces for indents. Chosen to do
1529
+ * this since it's the most common case (e.g. with println()). Same as
1530
+ * format(2). Use the format() function for more options.
1531
+ */
1532
+ @Override
1533
+ public String toString() {
1534
+ try {
1535
+ return format(2);
1536
+ } catch (Exception e) {
1537
+ return null;
1538
+ }
1641
1539
  }
1642
- }
1643
-
1644
- /**
1645
- * Make a JSON text of an Object value. If the object has an
1646
- * value.toJSONString() method, then that method will be used to produce
1647
- * the JSON text. The method is required to produce a strictly
1648
- * conforming text. If the object does not contain a toJSONString
1649
- * method (which is the most common case), then a text will be
1650
- * produced by other means. If the value is an array or Collection,
1651
- * then a JSONArray will be made from it and its toJSONString method
1652
- * will be called. If the value is a MAP, then a JSONObject will be made
1653
- * from it and its toJSONString method will be called. Otherwise, the
1654
- * value's toString method will be called, and the result will be quoted.
1655
- *
1656
- * <p>
1657
- * Warning: This method assumes that the data structure is acyclical.
1658
- * @param value The value to be serialized.
1659
- * @return a printable, displayable, transmittable
1660
- * representation of the object, beginning
1661
- * with <code>{</code>&nbsp;<small>(left brace)</small> and ending
1662
- * with <code>}</code>&nbsp;<small>(right brace)</small>.
1663
- * @throws RuntimeException If the value is or contains an invalid number.
1664
- */
1665
- static protected String valueToString(Object value) {
1666
- if (value == null || value.equals(null)) {
1667
- return "null";
1540
+
1541
+ /**
1542
+ * Make a prettyprinted JSON text of this JSONObject.
1543
+ * <p>
1544
+ * Warning: This method assumes that the data structure is acyclical.
1545
+ *
1546
+ * @param indentFactor The number of spaces to add to each level of
1547
+ * indentation.
1548
+ * @return a printable, displayable, portable, transmittable representation
1549
+ * of the object, beginning with <code>{</code>&nbsp;<small>(left
1550
+ * brace)</small> and ending with <code>}</code>&nbsp;<small>(right
1551
+ * brace)</small>.
1552
+ * @throws RuntimeException If the object contains an invalid number.
1553
+ */
1554
+ public String format(int indentFactor) {
1555
+ StringWriter w = new StringWriter();
1556
+ synchronized (w.getBuffer()) {
1557
+ return this.writeInternal(w, indentFactor, 0).toString();
1558
+ }
1668
1559
  }
1560
+
1561
+ /**
1562
+ * Make a JSON text of an Object value. If the object has an
1563
+ * value.toJSONString() method, then that method will be used to produce the
1564
+ * JSON text. The method is required to produce a strictly conforming text.
1565
+ * If the object does not contain a toJSONString method (which is the most
1566
+ * common case), then a text will be produced by other means. If the value
1567
+ * is an array or Collection, then a JSONArray will be made from it and its
1568
+ * toJSONString method will be called. If the value is a MAP, then a
1569
+ * JSONObject will be made from it and its toJSONString method will be
1570
+ * called. Otherwise, the value's toString method will be called, and the
1571
+ * result will be quoted.
1572
+ *
1573
+ * <p>
1574
+ * Warning: This method assumes that the data structure is acyclical.
1575
+ *
1576
+ * @param value The value to be serialized.
1577
+ * @return a printable, displayable, transmittable representation of the
1578
+ * object, beginning with <code>{</code>&nbsp;<small>(left brace)</small>
1579
+ * and ending with <code>}</code>&nbsp;<small>(right brace)</small>.
1580
+ * @throws RuntimeException If the value is or contains an invalid number.
1581
+ */
1582
+ static protected String valueToString(Object value) {
1583
+ if (value == null || value.equals(null)) {
1584
+ return "null";
1585
+ }
1669
1586
  // if (value instanceof JSONString) {
1670
1587
  // Object object;
1671
1588
  // try {
@@ -1678,78 +1595,74 @@ public class JSONObject {
1678
1595
  // }
1679
1596
  // throw new RuntimeException("Bad value from toJSONString: " + object);
1680
1597
  // }
1681
- if (value instanceof Number) {
1682
- return numberToString((Number) value);
1683
- }
1684
- if (value instanceof Boolean || value instanceof JSONObject ||
1685
- value instanceof JSONArray) {
1686
- return value.toString();
1687
- }
1688
- if (value instanceof Map) {
1689
- return new JSONObject(value).toString();
1690
- }
1691
- if (value instanceof Collection) {
1692
- return new JSONArray(value).toString();
1693
- }
1694
- if (value.getClass().isArray()) {
1695
- return new JSONArray(value).toString();
1598
+ if (value instanceof Number) {
1599
+ return numberToString((Number) value);
1600
+ }
1601
+ if (value instanceof Boolean || value instanceof JSONObject
1602
+ || value instanceof JSONArray) {
1603
+ return value.toString();
1604
+ }
1605
+ if (value instanceof Map) {
1606
+ return new JSONObject(value).toString();
1607
+ }
1608
+ if (value instanceof Collection) {
1609
+ return new JSONArray(value).toString();
1610
+ }
1611
+ if (value.getClass().isArray()) {
1612
+ return new JSONArray(value).toString();
1613
+ }
1614
+ return quote(value.toString());
1696
1615
  }
1697
- return quote(value.toString());
1698
- }
1699
-
1700
- /**
1701
- * Wrap an object, if necessary. If the object is null, return the NULL
1702
- * object. If it is an array or collection, wrap it in a JSONArray. If
1703
- * it is a map, wrap it in a JSONObject. If it is a standard property
1704
- * (Double, String, et al) then it is already wrapped. Otherwise, if it
1705
- * comes from one of the java packages, turn it into a string. And if
1706
- * it doesn't, try to wrap it in a JSONObject. If the wrapping fails,
1707
- * then null is returned.
1708
- *
1709
- * @param object The object to wrap
1710
- * @return The wrapped value
1711
- */
1712
- static protected Object wrap(Object object) {
1713
- try {
1714
- if (object == null) {
1715
- return NULL;
1716
- }
1717
- if (object instanceof JSONObject || object instanceof JSONArray ||
1718
- NULL.equals(object) || /*object instanceof JSONString ||*/
1719
- object instanceof Byte || object instanceof Character ||
1720
- object instanceof Short || object instanceof Integer ||
1721
- object instanceof Long || object instanceof Boolean ||
1722
- object instanceof Float || object instanceof Double ||
1723
- object instanceof String) {
1724
- return object;
1725
- }
1726
1616
 
1727
- if (object instanceof Collection) {
1728
- return new JSONArray(object);
1729
- }
1730
- if (object.getClass().isArray()) {
1731
- return new JSONArray(object);
1732
- }
1733
- if (object instanceof Map) {
1734
- return new JSONObject(object);
1735
- }
1736
- Package objectPackage = object.getClass().getPackage();
1737
- String objectPackageName = objectPackage != null
1738
- ? objectPackage.getName()
1739
- : "";
1740
- if (
1741
- objectPackageName.startsWith("java.") ||
1742
- objectPackageName.startsWith("javax.") ||
1743
- object.getClass().getClassLoader() == null
1744
- ) {
1745
- return object.toString();
1617
+ /**
1618
+ * Wrap an object, if necessary. If the object is null, return the NULL
1619
+ * object. If it is an array or collection, wrap it in a JSONArray. If it is
1620
+ * a map, wrap it in a JSONObject. If it is a standard property (Double,
1621
+ * String, et al) then it is already wrapped. Otherwise, if it comes from
1622
+ * one of the java packages, turn it into a string. And if it doesn't, try
1623
+ * to wrap it in a JSONObject. If the wrapping fails, then null is returned.
1624
+ *
1625
+ * @param object The object to wrap
1626
+ * @return The wrapped value
1627
+ */
1628
+ static protected Object wrap(Object object) {
1629
+ try {
1630
+ if (object == null) {
1631
+ return NULL;
1632
+ }
1633
+ if (object instanceof JSONObject || object instanceof JSONArray
1634
+ || NULL.equals(object)
1635
+ || /*object instanceof JSONString ||*/ object instanceof Byte || object instanceof Character
1636
+ || object instanceof Short || object instanceof Integer
1637
+ || object instanceof Long || object instanceof Boolean
1638
+ || object instanceof Float || object instanceof Double
1639
+ || object instanceof String) {
1640
+ return object;
1641
+ }
1642
+
1643
+ if (object instanceof Collection) {
1644
+ return new JSONArray(object);
1645
+ }
1646
+ if (object.getClass().isArray()) {
1647
+ return new JSONArray(object);
1648
+ }
1649
+ if (object instanceof Map) {
1650
+ return new JSONObject(object);
1651
+ }
1652
+ Package objectPackage = object.getClass().getPackage();
1653
+ String objectPackageName = objectPackage != null
1654
+ ? objectPackage.getName()
1655
+ : "";
1656
+ if (objectPackageName.startsWith("java.")
1657
+ || objectPackageName.startsWith("javax.")
1658
+ || object.getClass().getClassLoader() == null) {
1659
+ return object.toString();
1660
+ }
1661
+ return new JSONObject(object);
1662
+ } catch (Exception exception) {
1663
+ return null;
1746
1664
  }
1747
- return new JSONObject(object);
1748
- } catch(Exception exception) {
1749
- return null;
1750
1665
  }
1751
- }
1752
-
1753
1666
 
1754
1667
  // /**
1755
1668
  // * Write the contents of the JSONObject as JSON text to a writer.
@@ -1763,28 +1676,26 @@ public class JSONObject {
1763
1676
  // protected Writer write(Writer writer) {
1764
1677
  // return this.write(writer, 0, 0);
1765
1678
  // }
1766
-
1767
-
1768
- static final Writer writeValue(Writer writer, Object value,
1769
- int indentFactor, int indent) throws IOException {
1770
- if (value == null || value.equals(null)) {
1771
- writer.write("null");
1772
- } else if (value instanceof JSONObject) {
1773
- ((JSONObject) value).writeInternal(writer, indentFactor, indent);
1774
- } else if (value instanceof JSONArray) {
1775
- ((JSONArray) value).writeInternal(writer, indentFactor, indent);
1776
- } else if (value instanceof Map) {
1777
- new JSONObject(value).writeInternal(writer, indentFactor, indent);
1778
- } else if (value instanceof Collection) {
1779
- new JSONArray(value).writeInternal(writer, indentFactor,
1780
- indent);
1781
- } else if (value.getClass().isArray()) {
1782
- new JSONArray(value).writeInternal(writer, indentFactor, indent);
1783
- } else if (value instanceof Number) {
1784
- writer.write(numberToString((Number) value));
1785
- } else if (value instanceof Boolean) {
1786
- writer.write(value.toString());
1787
- /*
1679
+ static final Writer writeValue(Writer writer, Object value,
1680
+ int indentFactor, int indent) throws IOException {
1681
+ if (value == null || value.equals(null)) {
1682
+ writer.write("null");
1683
+ } else if (value instanceof JSONObject) {
1684
+ ((JSONObject) value).writeInternal(writer, indentFactor, indent);
1685
+ } else if (value instanceof JSONArray) {
1686
+ ((JSONArray) value).writeInternal(writer, indentFactor, indent);
1687
+ } else if (value instanceof Map) {
1688
+ new JSONObject(value).writeInternal(writer, indentFactor, indent);
1689
+ } else if (value instanceof Collection) {
1690
+ new JSONArray(value).writeInternal(writer, indentFactor,
1691
+ indent);
1692
+ } else if (value.getClass().isArray()) {
1693
+ new JSONArray(value).writeInternal(writer, indentFactor, indent);
1694
+ } else if (value instanceof Number) {
1695
+ writer.write(numberToString((Number) value));
1696
+ } else if (value instanceof Boolean) {
1697
+ writer.write(value.toString());
1698
+ /*
1788
1699
  } else if (value instanceof JSONString) {
1789
1700
  Object o;
1790
1701
  try {
@@ -1793,78 +1704,76 @@ public class JSONObject {
1793
1704
  throw new RuntimeException(e);
1794
1705
  }
1795
1706
  writer.write(o != null ? o.toString() : quote(value.toString()));
1796
- */
1797
- } else {
1798
- quote(value.toString(), writer);
1707
+ */
1708
+ } else {
1709
+ quote(value.toString(), writer);
1710
+ }
1711
+ return writer;
1799
1712
  }
1800
- return writer;
1801
- }
1802
1713
 
1803
-
1804
- static final void indent(Writer writer, int indent) throws IOException {
1805
- for (int i = 0; i < indent; i += 1) {
1806
- writer.write(' ');
1807
- }
1808
- }
1809
-
1810
- /**
1811
- * Write the contents of the JSONObject as JSON text to a writer.
1812
- * <p>
1813
- * Warning: This method assumes that the data structure is acyclical.
1814
- *
1815
- * @return The writer.
1816
- * @throws RuntimeException
1817
- */
1818
- protected Writer writeInternal(Writer writer, int indentFactor, int indent) {
1819
- try {
1820
- boolean commanate = false;
1821
- final int length = this.size();
1822
- Iterator keys = this.keyIterator();
1823
- writer.write('{');
1824
-
1825
- int actualFactor = (indentFactor == -1) ? 0 : indentFactor;
1826
-
1827
- if (length == 1) {
1828
- Object key = keys.next();
1829
- writer.write(quote(key.toString()));
1830
- writer.write(':');
1831
- if (actualFactor > 0) {
1832
- writer.write(' ');
1833
- }
1834
- //writeValue(writer, this.map.get(key), actualFactor, indent);
1835
- writeValue(writer, this.map.get(key), indentFactor, indent);
1836
- } else if (length != 0) {
1837
- final int newIndent = indent + actualFactor;
1838
- while (keys.hasNext()) {
1839
- Object key = keys.next();
1840
- if (commanate) {
1841
- writer.write(',');
1842
- }
1843
- if (indentFactor != -1) {
1844
- writer.write('\n');
1845
- }
1846
- indent(writer, newIndent);
1847
- writer.write(quote(key.toString()));
1848
- writer.write(':');
1849
- if (actualFactor > 0) {
1714
+ static final void indent(Writer writer, int indent) throws IOException {
1715
+ for (int i = 0; i < indent; i += 1) {
1850
1716
  writer.write(' ');
1851
- }
1852
- //writeValue(writer, this.map.get(key), actualFactor, newIndent);
1853
- writeValue(writer, this.map.get(key), indentFactor, newIndent);
1854
- commanate = true;
1855
1717
  }
1856
- if (indentFactor != -1) {
1857
- writer.write('\n');
1858
- }
1859
- indent(writer, indent);
1860
- }
1861
- writer.write('}');
1862
- return writer;
1863
- } catch (IOException exception) {
1864
- throw new RuntimeException(exception);
1865
1718
  }
1866
- }
1867
1719
 
1720
+ /**
1721
+ * Write the contents of the JSONObject as JSON text to a writer.
1722
+ * <p>
1723
+ * Warning: This method assumes that the data structure is acyclical.
1724
+ *
1725
+ * @return The writer.
1726
+ * @throws RuntimeException
1727
+ */
1728
+ protected Writer writeInternal(Writer writer, int indentFactor, int indent) {
1729
+ try {
1730
+ boolean commanate = false;
1731
+ final int length = this.size();
1732
+ Iterator keys = this.keyIterator();
1733
+ writer.write('{');
1734
+
1735
+ int actualFactor = (indentFactor == -1) ? 0 : indentFactor;
1736
+
1737
+ if (length == 1) {
1738
+ Object key = keys.next();
1739
+ writer.write(quote(key.toString()));
1740
+ writer.write(':');
1741
+ if (actualFactor > 0) {
1742
+ writer.write(' ');
1743
+ }
1744
+ //writeValue(writer, this.map.get(key), actualFactor, indent);
1745
+ writeValue(writer, this.map.get(key), indentFactor, indent);
1746
+ } else if (length != 0) {
1747
+ final int newIndent = indent + actualFactor;
1748
+ while (keys.hasNext()) {
1749
+ Object key = keys.next();
1750
+ if (commanate) {
1751
+ writer.write(',');
1752
+ }
1753
+ if (indentFactor != -1) {
1754
+ writer.write('\n');
1755
+ }
1756
+ indent(writer, newIndent);
1757
+ writer.write(quote(key.toString()));
1758
+ writer.write(':');
1759
+ if (actualFactor > 0) {
1760
+ writer.write(' ');
1761
+ }
1762
+ //writeValue(writer, this.map.get(key), actualFactor, newIndent);
1763
+ writeValue(writer, this.map.get(key), indentFactor, newIndent);
1764
+ commanate = true;
1765
+ }
1766
+ if (indentFactor != -1) {
1767
+ writer.write('\n');
1768
+ }
1769
+ indent(writer, indent);
1770
+ }
1771
+ writer.write('}');
1772
+ return writer;
1773
+ } catch (IOException exception) {
1774
+ throw new RuntimeException(exception);
1775
+ }
1776
+ }
1868
1777
 
1869
1778
  // // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1870
1779
  //
@@ -1879,11 +1788,7 @@ public class JSONObject {
1879
1788
  // super(throwable);
1880
1789
  // }
1881
1790
  // }
1882
-
1883
-
1884
- // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1885
-
1886
-
1791
+ // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1887
1792
  // /**
1888
1793
  // * Get the hex value of a character (base16).
1889
1794
  // * @param c A character between '0' and '9' or between 'A' and 'F' or
@@ -1902,8 +1807,6 @@ public class JSONObject {
1902
1807
  // }
1903
1808
  // return -1;
1904
1809
  // }
1905
-
1906
-
1907
1810
  // static class JSONTokener {
1908
1811
  // private long character;
1909
1812
  // private boolean eof;