propane 3.3.1-java → 3.6.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. checksums.yaml +4 -4
  2. data/.mvn/extensions.xml +1 -2
  3. data/.mvn/wrapper/MavenWrapperDownloader.java +117 -0
  4. data/.mvn/wrapper/maven-wrapper.properties +2 -3
  5. data/.travis.yml +9 -0
  6. data/CHANGELOG.md +17 -5
  7. data/Gemfile +2 -0
  8. data/README.md +17 -8
  9. data/Rakefile +16 -30
  10. data/bin/propane +3 -1
  11. data/lib/propane.rb +6 -4
  12. data/lib/propane/app.rb +20 -10
  13. data/lib/propane/creators/sketch_class.rb +7 -1
  14. data/lib/propane/creators/sketch_factory.rb +4 -2
  15. data/lib/propane/creators/sketch_writer.rb +1 -0
  16. data/lib/propane/helper_methods.rb +23 -24
  17. data/lib/propane/helpers/numeric.rb +2 -0
  18. data/lib/propane/helpers/version_error.rb +1 -0
  19. data/lib/propane/library.rb +5 -1
  20. data/lib/propane/library_loader.rb +2 -0
  21. data/lib/propane/native_folder.rb +21 -15
  22. data/lib/propane/native_loader.rb +3 -0
  23. data/lib/propane/runner.rb +14 -6
  24. data/lib/propane/version.rb +2 -1
  25. data/library/boids/boids.rb +21 -11
  26. data/library/color_group/color_group.rb +28 -0
  27. data/library/control_panel/control_panel.rb +8 -5
  28. data/library/dxf/dxf.rb +6 -0
  29. data/library/file_chooser/chooser.rb +10 -9
  30. data/library/file_chooser/file_chooser.rb +10 -9
  31. data/library/library_proxy/library_proxy.rb +2 -0
  32. data/library/net/net.rb +7 -0
  33. data/library/simplex_noise/simplex_noise.rb +2 -0
  34. data/library/slider/slider.rb +23 -22
  35. data/library/vector_utils/vector_utils.rb +4 -0
  36. data/library/video_event/video_event.rb +4 -1
  37. data/mvnw +127 -51
  38. data/mvnw.cmd +182 -145
  39. data/pom.rb +53 -50
  40. data/pom.xml +17 -8
  41. data/propane.gemspec +13 -11
  42. data/src/main/java/monkstone/ColorUtil.java +13 -1
  43. data/src/main/java/monkstone/MathToolModule.java +253 -203
  44. data/src/main/java/monkstone/PropaneLibrary.java +2 -2
  45. data/src/main/java/monkstone/fastmath/Deglut.java +1 -1
  46. data/src/main/java/monkstone/filechooser/Chooser.java +2 -1
  47. data/src/main/java/monkstone/noise/SimplexNoise.java +2 -2
  48. data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +1 -1
  49. data/src/main/java/monkstone/slider/CustomVerticalSlider.java +1 -1
  50. data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +1 -1
  51. data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +1 -1
  52. data/src/main/java/monkstone/slider/SliderBar.java +1 -1
  53. data/src/main/java/monkstone/slider/SliderGroup.java +1 -1
  54. data/src/main/java/monkstone/slider/WheelHandler.java +7 -6
  55. data/src/main/java/monkstone/vecmath/package-info.java +1 -1
  56. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +2 -2
  57. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +1 -1
  58. data/src/main/java/monkstone/videoevent/CaptureEvent.java +27 -0
  59. data/src/main/java/monkstone/videoevent/{VideoInterface.java → MovieEvent.java} +11 -27
  60. data/src/main/java/monkstone/videoevent/package-info.java +1 -1
  61. data/src/main/java/processing/awt/PGraphicsJava2D.java +2164 -1661
  62. data/src/main/java/processing/awt/PImageAWT.java +377 -0
  63. data/src/main/java/processing/awt/PShapeJava2D.java +280 -268
  64. data/src/main/java/processing/awt/PSurfaceAWT.java +942 -829
  65. data/src/main/java/processing/awt/ShimAWT.java +581 -0
  66. data/src/main/java/processing/core/PApplet.java +831 -824
  67. data/src/main/java/processing/core/PConstants.java +477 -447
  68. data/src/main/java/processing/core/PFont.java +914 -880
  69. data/src/main/java/processing/core/PGraphics.java +229 -213
  70. data/src/main/java/processing/core/PImage.java +620 -318
  71. data/src/main/java/processing/core/PMatrix.java +172 -159
  72. data/src/main/java/processing/core/PMatrix2D.java +478 -409
  73. data/src/main/java/processing/core/PMatrix3D.java +762 -735
  74. data/src/main/java/processing/core/PShape.java +2888 -2652
  75. data/src/main/java/processing/core/PShapeOBJ.java +436 -415
  76. data/src/main/java/processing/core/PShapeSVG.java +1702 -1479
  77. data/src/main/java/processing/core/PStyle.java +40 -37
  78. data/src/main/java/processing/core/PSurface.java +139 -97
  79. data/src/main/java/processing/core/PSurfaceNone.java +296 -208
  80. data/src/main/java/processing/core/PVector.java +997 -965
  81. data/src/main/java/processing/core/ThinkDifferent.java +12 -17
  82. data/src/main/java/processing/data/DoubleDict.java +756 -710
  83. data/src/main/java/processing/data/DoubleList.java +749 -696
  84. data/src/main/java/processing/data/FloatDict.java +748 -702
  85. data/src/main/java/processing/data/FloatList.java +751 -697
  86. data/src/main/java/processing/data/IntDict.java +720 -673
  87. data/src/main/java/processing/data/IntList.java +699 -633
  88. data/src/main/java/processing/data/JSONArray.java +931 -873
  89. data/src/main/java/processing/data/JSONObject.java +1262 -1165
  90. data/src/main/java/processing/data/JSONTokener.java +351 -341
  91. data/src/main/java/processing/data/LongDict.java +710 -663
  92. data/src/main/java/processing/data/LongList.java +701 -635
  93. data/src/main/java/processing/data/Sort.java +37 -41
  94. data/src/main/java/processing/data/StringDict.java +525 -486
  95. data/src/main/java/processing/data/StringList.java +626 -580
  96. data/src/main/java/processing/data/Table.java +3690 -3510
  97. data/src/main/java/processing/data/TableRow.java +182 -183
  98. data/src/main/java/processing/data/XML.java +957 -883
  99. data/src/main/java/processing/dxf/RawDXF.java +404 -0
  100. data/src/main/java/processing/event/Event.java +87 -66
  101. data/src/main/java/processing/event/KeyEvent.java +48 -41
  102. data/src/main/java/processing/event/MouseEvent.java +88 -113
  103. data/src/main/java/processing/event/TouchEvent.java +10 -6
  104. data/src/main/java/processing/javafx/PGraphicsFX2D.java +20 -345
  105. data/src/main/java/processing/javafx/PSurfaceFX.java +149 -121
  106. data/src/main/java/processing/net/Client.java +744 -0
  107. data/src/main/java/processing/net/Server.java +388 -0
  108. data/src/main/java/processing/opengl/FontTexture.java +289 -270
  109. data/src/main/java/processing/opengl/FrameBuffer.java +386 -364
  110. data/src/main/java/processing/opengl/LinePath.java +547 -500
  111. data/src/main/java/processing/opengl/LineStroker.java +588 -581
  112. data/src/main/java/processing/opengl/PGL.java +3047 -2914
  113. data/src/main/java/processing/opengl/PGraphics2D.java +408 -315
  114. data/src/main/java/processing/opengl/PGraphics3D.java +107 -72
  115. data/src/main/java/processing/opengl/PGraphicsOpenGL.java +12378 -12075
  116. data/src/main/java/processing/opengl/PJOGL.java +1753 -1670
  117. data/src/main/java/processing/opengl/PShader.java +1266 -1257
  118. data/src/main/java/processing/opengl/PShapeOpenGL.java +4678 -4580
  119. data/src/main/java/processing/opengl/PSurfaceJOGL.java +1114 -1027
  120. data/src/main/java/processing/opengl/Texture.java +1492 -1401
  121. data/src/main/java/processing/opengl/VertexBuffer.java +57 -55
  122. data/test/create_test.rb +21 -20
  123. data/test/deglut_spec_test.rb +4 -2
  124. data/test/helper_methods_test.rb +49 -20
  125. data/test/math_tool_test.rb +39 -32
  126. data/test/native_folder.rb +47 -0
  127. data/test/respond_to_test.rb +3 -2
  128. data/test/sketches/key_event.rb +2 -2
  129. data/test/sketches/library/my_library/my_library.rb +3 -0
  130. data/test/test_helper.rb +2 -0
  131. data/test/vecmath_spec_test.rb +35 -22
  132. data/vendors/Rakefile +33 -62
  133. metadata +54 -45
  134. data/src/main/java/processing/core/util/image/ImageLoadFacade.java +0 -161
  135. data/src/main/java/processing/core/util/image/ImageSaveFacade.java +0 -169
  136. data/src/main/java/processing/core/util/image/constants/TifConstants.java +0 -45
  137. data/src/main/java/processing/core/util/image/load/AwtImageLoadStrategy.java +0 -80
  138. data/src/main/java/processing/core/util/image/load/Base64StringImageLoadStrategy.java +0 -73
  139. data/src/main/java/processing/core/util/image/load/FallbackImageLoadStrategy.java +0 -70
  140. data/src/main/java/processing/core/util/image/load/ImageIoImageLoadStrategy.java +0 -132
  141. data/src/main/java/processing/core/util/image/load/ImageLoadStrategy.java +0 -48
  142. data/src/main/java/processing/core/util/image/load/ImageLoadUtil.java +0 -45
  143. data/src/main/java/processing/core/util/image/load/TgaImageLoadStrategy.java +0 -255
  144. data/src/main/java/processing/core/util/image/load/TiffImageLoadStrategy.java +0 -98
  145. data/src/main/java/processing/core/util/image/save/ImageSaveStrategy.java +0 -49
  146. data/src/main/java/processing/core/util/image/save/ImageSaveUtil.java +0 -48
  147. data/src/main/java/processing/core/util/image/save/ImageWriterImageSaveStrategy.java +0 -179
  148. data/src/main/java/processing/core/util/image/save/SaveImageException.java +0 -41
  149. data/src/main/java/processing/core/util/image/save/TgaImageSaveStrategy.java +0 -198
  150. data/src/main/java/processing/core/util/image/save/TiffImageSaveStrategy.java +0 -91
  151. data/src/main/java/processing/core/util/image/save/TiffNakedFilenameImageSaveStrategy.java +0 -57
  152. data/src/main/java/processing/core/util/io/InputFactory.java +0 -285
  153. data/src/main/java/processing/core/util/io/PathUtil.java +0 -109
  154. data/src/main/java/processing/opengl/shaders/LightVert-brcm.glsl +0 -154
  155. data/src/main/java/processing/opengl/shaders/LightVert-vc4.glsl +0 -154
  156. data/src/main/java/processing/opengl/shaders/TexLightVert-brcm.glsl +0 -160
  157. data/src/main/java/processing/opengl/shaders/TexLightVert-vc4.glsl +0 -160
@@ -33,6 +33,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33
33
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34
34
  SOFTWARE.
35
35
  */
36
+
36
37
  import java.io.File;
37
38
  import java.io.IOException;
38
39
  import java.io.PrintWriter;
@@ -110,92 +111,95 @@ import processing.core.PApplet;
110
111
  * @see PApplet#saveJSONArray(JSONArray, String)
111
112
  */
112
113
  public class JSONObject {
113
-
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 {
114
138
  /**
115
- * The maximum number of keys in the key pool.
139
+ * There is only intended to be a single instance of the NULL object,
140
+ * so the clone method returns itself.
141
+ * @return NULL.
116
142
  */
117
- private static final int keyPoolSize = 100;
143
+ @Override
144
+ protected final Object clone() {
145
+ return this;
146
+ }
118
147
 
119
148
  /**
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).
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.
124
153
  */
125
- private static HashMap<String, Object> keyPool
126
- = new HashMap<>(keyPoolSize);
154
+ @Override
155
+ public boolean equals(Object object) {
156
+ return object == null || object == this;
157
+ }
127
158
 
128
- // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
129
159
  /**
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.
160
+ * Get the "null" string value.
161
+ * @return The string "null".
133
162
  */
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
- }
163
+ @Override
164
+ public String toString() {
165
+ return "null";
166
+ }
146
167
 
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
- }
168
+ @Override
169
+ public int hashCode() {
170
+ // TODO Auto-generated method stub
171
+ return super.hashCode();
172
+ }
173
+ }
158
174
 
159
- /**
160
- * Get the "null" string value.
161
- *
162
- * @return The string "null".
163
- */
164
- @Override
165
- public String toString() {
166
- return "null";
167
- }
168
175
 
169
- @Override
170
- public int hashCode() {
171
- // TODO Auto-generated method stub
172
- return super.hashCode();
173
- }
174
- }
176
+ // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
175
177
 
176
- // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
177
- /**
178
- * The map where the JSONObject's properties are kept.
179
- */
178
+
179
+ /**
180
+ * The map where the JSONObject's properties are kept.
181
+ */
180
182
  // private final Map map;
181
- private final HashMap<String, Object> map;
183
+ private final HashMap<String, Object> map;
182
184
 
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();
190
185
 
191
- /**
192
- * Construct an empty JSONObject.
193
- *
194
- * @nowebref
195
- */
196
- public JSONObject() {
197
- this.map = new HashMap<>();
198
- }
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();
193
+
194
+
195
+ /**
196
+ * Construct an empty JSONObject.
197
+ * @nowebref
198
+ */
199
+ public JSONObject() {
200
+ this.map = new HashMap<>();
201
+ }
202
+
199
203
 
200
204
  // /**
201
205
  // * Construct a JSONObject from a subset of another JSONObject.
@@ -213,144 +217,153 @@ public class JSONObject {
213
217
  // }
214
218
  // }
215
219
  // }
216
- /**
217
- * @nowebref
218
- */
219
- public JSONObject(Reader reader) {
220
- this(new JSONTokener(reader));
221
- }
222
220
 
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;
234
221
 
235
- if (x.nextClean() != '{') {
236
- throw new RuntimeException("A JSONObject text must begin with '{'");
237
- }
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
- }
222
+ /**
223
+ * @nowebref
224
+ */
225
+ public JSONObject(Reader reader) {
226
+ this(new JSONTokener(reader));
227
+ }
249
228
 
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
- }
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 '{'");
276
243
  }
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
+ }
277
255
 
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
- }
256
+ // The key is followed by ':'. We will also tolerate '=' or '=>'.
257
+
258
+ c = x.nextClean();
259
+ if (c == '=') {
260
+ if (x.next() != '>') {
261
+ x.back();
295
262
  }
296
- }
263
+ } else if (c != ':') {
264
+ throw new RuntimeException("Expected a ':' after a key");
265
+ }
266
+ this.putOnce(key, x.nextValue());
297
267
 
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));
268
+ // Pairs are separated by ','. We will also tolerate ';'.
269
+
270
+ switch (x.nextClean()) {
271
+ case ';':
272
+ case ',':
273
+ if (x.nextClean() == '}') {
274
+ return;
305
275
  }
276
+ x.back();
277
+ break;
278
+ case '}':
279
+ return;
280
+ default:
281
+ throw new RuntimeException("Expected a ',' or '}'");
282
+ }
306
283
  }
307
-
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));
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));
315
302
  }
303
+ }
316
304
  }
305
+ }
317
306
 
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
- }
307
+
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));
326
315
  }
316
+ }
327
317
 
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);
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));
351
326
  }
327
+ }
352
328
 
353
- // holding off on this method until we decide on how to handle reflection
329
+
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));
337
+ }
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
354
367
  // /**
355
368
  // * Construct a JSONObject from an Object, using reflection to find the
356
369
  // * public members. The resulting JSONObject's keys will be the strings
@@ -373,19 +386,21 @@ public class JSONObject {
373
386
  // }
374
387
  // }
375
388
  // }
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
- }
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
+
389
404
 
390
405
  // /**
391
406
  // * Construct a JSONObject from a ResourceBundle.
@@ -425,6 +440,8 @@ public class JSONObject {
425
440
  // }
426
441
  // }
427
442
  // }
443
+
444
+
428
445
  // /**
429
446
  // * Accumulate values under a key. It is similar to the put method except
430
447
  // * that if there is already an object stored under the key then a
@@ -458,6 +475,8 @@ public class JSONObject {
458
475
  // }
459
476
  // return this;
460
477
  // }
478
+
479
+
461
480
  // /**
462
481
  // * Append values to the array under a key. If the key does not exist in the
463
482
  // * JSONObject, then the key is put in the JSONObject with its value being a
@@ -482,315 +501,334 @@ public class JSONObject {
482
501
  // }
483
502
  // return this;
484
503
  // }
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
- }
496
504
 
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;
509
- }
510
505
 
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;
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";
531
515
  }
532
516
 
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");
555
- }
517
+ // Shave off trailing zeros and decimal point, if possible.
556
518
 
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();
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
+ }
568
528
  }
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
- }
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");
594
543
  }
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
- }
544
+ Object object = this.opt(key);
545
+ if (object == null) {
546
+ // Adding for rev 0257 in line with other p5 api
547
+ return null;
611
548
  }
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
- }
549
+ if (object == null) {
550
+ throw new RuntimeException("JSONObject[" + quote(key) + "] not found");
630
551
  }
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
- }
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;
647
573
  }
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);
574
+ if (object instanceof String) {
575
+ return (String)object;
659
576
  }
660
-
661
- public float getFloat(String key, float defaultValue) {
662
- try {
663
- return getFloat(key);
664
- } catch (Exception e) {
665
- return defaultValue;
666
- }
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");
667
612
  }
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
- }
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.");
686
618
  }
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
- }
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;
703
637
  }
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.");
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);
730
657
  }
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
- }
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;
747
676
  }
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.");
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;
770
698
  }
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.");
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.");
717
+ }
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;
793
736
  }
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;
762
+ }
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;
781
+ }
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;
801
+ }
802
+ if (object instanceof JSONArray) {
803
+ return (JSONArray)object;
804
+ }
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;
825
+ }
826
+ if (object instanceof JSONObject) {
827
+ return (JSONObject)object;
828
+ }
829
+ throw new RuntimeException("JSONObject[" + quote(key) + "] is not a JSONObject.");
830
+ }
831
+
794
832
 
795
833
  // /**
796
834
  // * Get an array of field names from a JSONObject.
@@ -834,15 +872,17 @@ public class JSONObject {
834
872
  // }
835
873
  // return names;
836
874
  // }
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
- }
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
+
846
886
 
847
887
  // /**
848
888
  // * Increment a property of a JSONObject. If there is no such property,
@@ -870,46 +910,52 @@ public class JSONObject {
870
910
  // }
871
911
  // return this;
872
912
  // }
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
- }
885
913
 
886
- /**
887
- * Get an enumeration of the keys of the JSONObject.
888
- *
889
- * @return An iterator of the keys.
890
- */
891
- public Iterator keyIterator() {
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() {
892
935
  // return this.keySet().iterator();
893
- return map.keySet().iterator();
894
- }
936
+ return map.keySet().iterator();
937
+ }
895
938
 
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
- }
904
939
 
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
- }
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
+
949
+
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
+ }
958
+
913
959
 
914
960
  // /**
915
961
  // * Produce a JSONArray containing the names of the elements of this
@@ -925,42 +971,45 @@ public class JSONObject {
925
971
  // }
926
972
  // return ja.size() == 0 ? null : ja;
927
973
  // }
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;
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");
953
985
  }
986
+ testValidity(number);
954
987
 
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);
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
+ }
963
999
  }
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
+
964
1013
 
965
1014
  // /**
966
1015
  // * Get an optional boolean associated with a key.
@@ -973,6 +1022,8 @@ public class JSONObject {
973
1022
  // private boolean optBoolean(String key) {
974
1023
  // return this.optBoolean(key, false);
975
1024
  // }
1025
+
1026
+
976
1027
  // /**
977
1028
  // * Get an optional double associated with a key,
978
1029
  // * or NaN if there is no such key or if its value is not a number.
@@ -985,6 +1036,8 @@ public class JSONObject {
985
1036
  // private double optDouble(String key) {
986
1037
  // return this.optDouble(key, Double.NaN);
987
1038
  // }
1039
+
1040
+
988
1041
  // /**
989
1042
  // * Get an optional int value associated with a key,
990
1043
  // * or zero if there is no such key or if the value is not a number.
@@ -997,6 +1050,8 @@ public class JSONObject {
997
1050
  // private int optInt(String key) {
998
1051
  // return this.optInt(key, 0);
999
1052
  // }
1053
+
1054
+
1000
1055
  // /**
1001
1056
  // * Get an optional JSONArray associated with a key.
1002
1057
  // * It returns null if there is no such key, or if its value is not a
@@ -1009,6 +1064,8 @@ public class JSONObject {
1009
1064
  // Object o = this.opt(key);
1010
1065
  // return o instanceof JSONArray ? (JSONArray)o : null;
1011
1066
  // }
1067
+
1068
+
1012
1069
  // /**
1013
1070
  // * Get an optional JSONObject associated with a key.
1014
1071
  // * It returns null if there is no such key, or if its value is not a
@@ -1021,6 +1078,8 @@ public class JSONObject {
1021
1078
  // Object object = this.opt(key);
1022
1079
  // return object instanceof JSONObject ? (JSONObject)object : null;
1023
1080
  // }
1081
+
1082
+
1024
1083
  // /**
1025
1084
  // * Get an optional long value associated with a key,
1026
1085
  // * or zero if there is no such key or if the value is not a number.
@@ -1033,6 +1092,8 @@ public class JSONObject {
1033
1092
  // public long optLong(String key) {
1034
1093
  // return this.optLong(key, 0);
1035
1094
  // }
1095
+
1096
+
1036
1097
  // /**
1037
1098
  // * Get an optional string associated with a key.
1038
1099
  // * It returns an empty string if there is no such key. If the value is not
@@ -1044,169 +1105,176 @@ public class JSONObject {
1044
1105
  // public String optString(String key) {
1045
1106
  // return this.optString(key, "");
1046
1107
  // }
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
- }
1092
1108
 
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
- }
1105
1109
 
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
- }
1110
+ private void populateMap(Object bean) {
1111
+ Class klass = bean.getClass();
1123
1112
 
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
- }
1113
+ // If klass is a System class then set includeSuperClass to false.
1136
1114
 
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
- }
1152
-
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
- }
1115
+ boolean includeSuperClass = klass.getClassLoader() != null;
1166
1116
 
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
- }
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
+ }
1197
1277
 
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
- }
1210
1278
 
1211
1279
  // /**
1212
1280
  // * Put a key/value pair in the JSONObject, where the value will be a
@@ -1220,6 +1288,8 @@ public class JSONObject {
1220
1288
  // this.put(key, new JSONArray(value));
1221
1289
  // return this;
1222
1290
  // }
1291
+
1292
+
1223
1293
  // /**
1224
1294
  // * Put a key/value pair in the JSONObject, where the value will be a
1225
1295
  // * JSONObject which is produced from a Map.
@@ -1233,61 +1303,63 @@ public class JSONObject {
1233
1303
  // this.put(key, new JSONObject(value));
1234
1304
  // return this;
1235
1305
  // }
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.");
1252
- }
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;
1269
- }
1270
1306
 
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);
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);
1288
1330
  }
1289
- return this;
1331
+ keyPool.put(key, key);
1332
+ } else {
1333
+ key = pooled;
1334
+ }
1335
+ this.map.put(key, value);
1336
+ } else {
1337
+ this.remove(key);
1290
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);
1359
+ }
1360
+ return this;
1361
+ }
1362
+
1291
1363
 
1292
1364
  // /**
1293
1365
  // * Put a key/value pair in the JSONObject, but only if the
@@ -1305,170 +1377,174 @@ public class JSONObject {
1305
1377
  // }
1306
1378
  // return this;
1307
1379
  // }
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
- }
1327
1380
 
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
- }
1333
1381
 
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;
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
+ }
1385
1399
  }
1400
+ }
1386
1401
 
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);
1402
+ static public Writer quote(String string, Writer w) throws IOException {
1403
+ if (string == null || string.length() == 0) {
1404
+ w.write("\"\"");
1405
+ return w;
1396
1406
  }
1397
1407
 
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;
1409
- }
1410
- if (string.equalsIgnoreCase("true")) {
1411
- return Boolean.TRUE;
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('\\');
1412
1427
  }
1413
- if (string.equalsIgnoreCase("false")) {
1414
- return Boolean.FALSE;
1415
- }
1416
- if (string.equalsIgnoreCase("null")) {
1417
- return JSONObject.NULL;
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);
1418
1454
  }
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
+ }
1419
1493
 
1420
- /*
1494
+ /*
1421
1495
  * If it might be a number, try converting it.
1422
1496
  * If a number cannot be produced, then the value will just
1423
1497
  * be a string. Note that the plus and implied string
1424
1498
  * conventions are non-standard. A JSON parser may accept
1425
1499
  * non-JSON forms as long as it accepts all correct JSON forms.
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
- }
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
+ }
1446
1518
  }
1447
- return string;
1519
+ } catch (Exception ignore) {
1520
+ }
1448
1521
  }
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
- }
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.");
1470
1543
  }
1544
+ }
1471
1545
  }
1546
+ }
1547
+
1472
1548
 
1473
1549
  // /**
1474
1550
  // * Produce a JSONArray containing the values of the members of this
@@ -1488,101 +1564,108 @@ public class JSONObject {
1488
1564
  // }
1489
1565
  // return ja;
1490
1566
  // }
1567
+
1568
+
1491
1569
  // protected boolean save(OutputStream output) {
1492
1570
  // return save(PApplet.createWriter(output));
1493
1571
  // }
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
- }
1500
1572
 
1501
- public boolean write(PrintWriter output) {
1502
- return write(output, null);
1503
- }
1504
1573
 
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
- }
1521
- }
1522
- output.print(format(indentFactor));
1523
- output.flush();
1524
- return true;
1525
- }
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
+ }
1526
1580
 
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
- }
1539
- }
1540
1581
 
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
- }
1559
- }
1582
+ public boolean write(PrintWriter output) {
1583
+ return write(output, null);
1584
+ }
1560
1585
 
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";
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);
1585
1601
  }
1602
+ }
1603
+ }
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();
1641
+ }
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";
1668
+ }
1586
1669
  // if (value instanceof JSONString) {
1587
1670
  // Object object;
1588
1671
  // try {
@@ -1595,74 +1678,78 @@ public class JSONObject {
1595
1678
  // }
1596
1679
  // throw new RuntimeException("Bad value from toJSONString: " + object);
1597
1680
  // }
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());
1681
+ if (value instanceof Number) {
1682
+ return numberToString((Number) value);
1615
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();
1696
+ }
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
+ }
1616
1726
 
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;
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();
1664
1746
  }
1747
+ return new JSONObject(object);
1748
+ } catch(Exception exception) {
1749
+ return null;
1665
1750
  }
1751
+ }
1752
+
1666
1753
 
1667
1754
  // /**
1668
1755
  // * Write the contents of the JSONObject as JSON text to a writer.
@@ -1676,26 +1763,28 @@ public class JSONObject {
1676
1763
  // protected Writer write(Writer writer) {
1677
1764
  // return this.write(writer, 0, 0);
1678
1765
  // }
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
- /*
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
+ /*
1699
1788
  } else if (value instanceof JSONString) {
1700
1789
  Object o;
1701
1790
  try {
@@ -1704,76 +1793,78 @@ public class JSONObject {
1704
1793
  throw new RuntimeException(e);
1705
1794
  }
1706
1795
  writer.write(o != null ? o.toString() : quote(value.toString()));
1707
- */
1708
- } else {
1709
- quote(value.toString(), writer);
1710
- }
1711
- return writer;
1796
+ */
1797
+ } else {
1798
+ quote(value.toString(), writer);
1712
1799
  }
1800
+ return writer;
1801
+ }
1713
1802
 
1714
- static final void indent(Writer writer, int indent) throws IOException {
1715
- for (int i = 0; i < indent; i += 1) {
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) {
1716
1850
  writer.write(' ');
1851
+ }
1852
+ //writeValue(writer, this.map.get(key), actualFactor, newIndent);
1853
+ writeValue(writer, this.map.get(key), indentFactor, newIndent);
1854
+ commanate = true;
1717
1855
  }
1718
- }
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);
1856
+ if (indentFactor != -1) {
1857
+ writer.write('\n');
1775
1858
  }
1859
+ indent(writer, indent);
1860
+ }
1861
+ writer.write('}');
1862
+ return writer;
1863
+ } catch (IOException exception) {
1864
+ throw new RuntimeException(exception);
1776
1865
  }
1866
+ }
1867
+
1777
1868
 
1778
1869
  // // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1779
1870
  //
@@ -1788,7 +1879,11 @@ public class JSONObject {
1788
1879
  // super(throwable);
1789
1880
  // }
1790
1881
  // }
1791
- // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1882
+
1883
+
1884
+ // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1885
+
1886
+
1792
1887
  // /**
1793
1888
  // * Get the hex value of a character (base16).
1794
1889
  // * @param c A character between '0' and '9' or between 'A' and 'F' or
@@ -1807,6 +1902,8 @@ public class JSONObject {
1807
1902
  // }
1808
1903
  // return -1;
1809
1904
  // }
1905
+
1906
+
1810
1907
  // static class JSONTokener {
1811
1908
  // private long character;
1812
1909
  // private boolean eof;