propane 3.10.0-java → 3.11.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. checksums.yaml +4 -4
  2. data/.mvn/wrapper/maven-wrapper.properties +1 -1
  3. data/CHANGELOG.md +2 -0
  4. data/README.md +7 -7
  5. data/lib/propane/app.rb +2 -5
  6. data/lib/propane/helper_methods.rb +6 -6
  7. data/lib/propane/version.rb +1 -1
  8. data/lib/{propane-3.10.0.jar → propane-3.11.0.jar} +0 -0
  9. data/pom.rb +6 -6
  10. data/pom.xml +6 -6
  11. data/propane.gemspec +3 -3
  12. data/src/main/java/monkstone/noise/OpenSimplex2F.java +838 -737
  13. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +8 -13
  14. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +14 -28
  15. data/src/main/java/processing/awt/PImageAWT.java +6 -4
  16. data/src/main/java/processing/core/PApplet.java +71 -59
  17. data/src/main/java/processing/core/PImage.java +14 -14
  18. data/src/main/java/processing/opengl/PGraphicsOpenGL.java +13 -13
  19. data/src/main/java/processing/opengl/PShader.java +1 -6
  20. data/src/main/java/processing/opengl/PSurfaceJOGL.java +6 -6
  21. data/{lib/java/processing/opengl → src/main/resources}/cursors/arrow.png +0 -0
  22. data/{lib/java/processing/opengl → src/main/resources}/cursors/cross.png +0 -0
  23. data/{lib/java/processing/opengl → src/main/resources}/cursors/hand.png +0 -0
  24. data/{lib/java/processing/opengl → src/main/resources}/cursors/license.txt +0 -0
  25. data/{lib/java/processing/opengl → src/main/resources}/cursors/move.png +0 -0
  26. data/{lib/java/processing/opengl → src/main/resources}/cursors/text.png +0 -0
  27. data/{lib/java/processing/opengl → src/main/resources}/cursors/wait.png +0 -0
  28. data/{lib/java/processing/opengl → src/main/resources}/shaders/ColorFrag.glsl +0 -0
  29. data/{lib/java/processing/opengl → src/main/resources}/shaders/ColorVert.glsl +0 -0
  30. data/{lib/java/processing/opengl → src/main/resources}/shaders/LightFrag.glsl +0 -0
  31. data/{lib/java/processing/opengl → src/main/resources}/shaders/LightVert.glsl +0 -0
  32. data/{lib/java/processing/opengl → src/main/resources}/shaders/LineFrag.glsl +0 -0
  33. data/{lib/java/processing/opengl → src/main/resources}/shaders/LineVert.glsl +0 -0
  34. data/{lib/java/processing/opengl → src/main/resources}/shaders/MaskFrag.glsl +0 -0
  35. data/{lib/java/processing/opengl → src/main/resources}/shaders/PointFrag.glsl +0 -0
  36. data/{lib/java/processing/opengl → src/main/resources}/shaders/PointVert.glsl +0 -0
  37. data/{lib/java/processing/opengl → src/main/resources}/shaders/TexFrag.glsl +0 -0
  38. data/{lib/java/processing/opengl → src/main/resources}/shaders/TexLightFrag.glsl +0 -0
  39. data/{lib/java/processing/opengl → src/main/resources}/shaders/TexLightVert.glsl +0 -0
  40. data/{lib/java/processing/opengl → src/main/resources}/shaders/TexVert.glsl +0 -0
  41. data/test/test_helper.rb +1 -0
  42. data/vendors/Rakefile +1 -1
  43. metadata +29 -155
  44. data/lib/java/japplemenubar/JAppleMenuBar.java +0 -88
  45. data/lib/java/japplemenubar/libjAppleMenuBar.jnilib +0 -0
  46. data/lib/java/monkstone/ColorUtil.java +0 -127
  47. data/lib/java/monkstone/MathToolModule.java +0 -287
  48. data/lib/java/monkstone/PropaneLibrary.java +0 -46
  49. data/lib/java/monkstone/core/LibraryProxy.java +0 -136
  50. data/lib/java/monkstone/fastmath/DegLutTables.java +0 -111
  51. data/lib/java/monkstone/fastmath/Deglut.java +0 -71
  52. data/lib/java/monkstone/fastmath/package-info.java +0 -6
  53. data/lib/java/monkstone/filechooser/Chooser.java +0 -39
  54. data/lib/java/monkstone/noise/FastTerrain.java +0 -874
  55. data/lib/java/monkstone/noise/Noise.java +0 -90
  56. data/lib/java/monkstone/noise/NoiseGenerator.java +0 -75
  57. data/lib/java/monkstone/noise/NoiseMode.java +0 -28
  58. data/lib/java/monkstone/noise/OpenSimplex2F.java +0 -881
  59. data/lib/java/monkstone/noise/OpenSimplex2S.java +0 -1106
  60. data/lib/java/monkstone/noise/SmoothTerrain.java +0 -1099
  61. data/lib/java/monkstone/slider/CustomHorizontalSlider.java +0 -164
  62. data/lib/java/monkstone/slider/CustomVerticalSlider.java +0 -178
  63. data/lib/java/monkstone/slider/SimpleHorizontalSlider.java +0 -145
  64. data/lib/java/monkstone/slider/SimpleSlider.java +0 -166
  65. data/lib/java/monkstone/slider/SimpleVerticalSlider.java +0 -157
  66. data/lib/java/monkstone/slider/Slider.java +0 -61
  67. data/lib/java/monkstone/slider/SliderBar.java +0 -245
  68. data/lib/java/monkstone/slider/SliderGroup.java +0 -56
  69. data/lib/java/monkstone/slider/WheelHandler.java +0 -35
  70. data/lib/java/monkstone/vecmath/GfxRender.java +0 -86
  71. data/lib/java/monkstone/vecmath/JRender.java +0 -56
  72. data/lib/java/monkstone/vecmath/ShapeRender.java +0 -87
  73. data/lib/java/monkstone/vecmath/package-info.java +0 -20
  74. data/lib/java/monkstone/vecmath/vec2/Vec2.java +0 -802
  75. data/lib/java/monkstone/vecmath/vec2/package-info.java +0 -6
  76. data/lib/java/monkstone/vecmath/vec3/Vec3.java +0 -727
  77. data/lib/java/monkstone/vecmath/vec3/package-info.java +0 -6
  78. data/lib/java/monkstone/videoevent/CaptureEvent.java +0 -27
  79. data/lib/java/monkstone/videoevent/MovieEvent.java +0 -32
  80. data/lib/java/monkstone/videoevent/package-info.java +0 -20
  81. data/lib/java/processing/awt/PGraphicsJava2D.java +0 -3040
  82. data/lib/java/processing/awt/PImageAWT.java +0 -377
  83. data/lib/java/processing/awt/PShapeJava2D.java +0 -387
  84. data/lib/java/processing/awt/PSurfaceAWT.java +0 -1581
  85. data/lib/java/processing/awt/ShimAWT.java +0 -581
  86. data/lib/java/processing/core/PApplet.java +0 -15156
  87. data/lib/java/processing/core/PConstants.java +0 -523
  88. data/lib/java/processing/core/PFont.java +0 -1126
  89. data/lib/java/processing/core/PGraphics.java +0 -8600
  90. data/lib/java/processing/core/PImage.java +0 -3377
  91. data/lib/java/processing/core/PMatrix.java +0 -208
  92. data/lib/java/processing/core/PMatrix2D.java +0 -562
  93. data/lib/java/processing/core/PMatrix3D.java +0 -890
  94. data/lib/java/processing/core/PShape.java +0 -3561
  95. data/lib/java/processing/core/PShapeOBJ.java +0 -483
  96. data/lib/java/processing/core/PShapeSVG.java +0 -2016
  97. data/lib/java/processing/core/PStyle.java +0 -63
  98. data/lib/java/processing/core/PSurface.java +0 -198
  99. data/lib/java/processing/core/PSurfaceNone.java +0 -431
  100. data/lib/java/processing/core/PVector.java +0 -1066
  101. data/lib/java/processing/core/ThinkDifferent.java +0 -115
  102. data/lib/java/processing/data/DoubleDict.java +0 -850
  103. data/lib/java/processing/data/DoubleList.java +0 -928
  104. data/lib/java/processing/data/FloatDict.java +0 -847
  105. data/lib/java/processing/data/FloatList.java +0 -936
  106. data/lib/java/processing/data/IntDict.java +0 -807
  107. data/lib/java/processing/data/IntList.java +0 -936
  108. data/lib/java/processing/data/JSONArray.java +0 -1260
  109. data/lib/java/processing/data/JSONObject.java +0 -2282
  110. data/lib/java/processing/data/JSONTokener.java +0 -435
  111. data/lib/java/processing/data/LongDict.java +0 -802
  112. data/lib/java/processing/data/LongList.java +0 -937
  113. data/lib/java/processing/data/Sort.java +0 -46
  114. data/lib/java/processing/data/StringDict.java +0 -613
  115. data/lib/java/processing/data/StringList.java +0 -800
  116. data/lib/java/processing/data/Table.java +0 -4936
  117. data/lib/java/processing/data/TableRow.java +0 -198
  118. data/lib/java/processing/data/XML.java +0 -1156
  119. data/lib/java/processing/dxf/RawDXF.java +0 -404
  120. data/lib/java/processing/event/Event.java +0 -125
  121. data/lib/java/processing/event/KeyEvent.java +0 -70
  122. data/lib/java/processing/event/MouseEvent.java +0 -114
  123. data/lib/java/processing/event/TouchEvent.java +0 -57
  124. data/lib/java/processing/javafx/PGraphicsFX2D.java +0 -32
  125. data/lib/java/processing/javafx/PSurfaceFX.java +0 -173
  126. data/lib/java/processing/net/Client.java +0 -744
  127. data/lib/java/processing/net/Server.java +0 -388
  128. data/lib/java/processing/opengl/FontTexture.java +0 -378
  129. data/lib/java/processing/opengl/FrameBuffer.java +0 -513
  130. data/lib/java/processing/opengl/LinePath.java +0 -627
  131. data/lib/java/processing/opengl/LineStroker.java +0 -681
  132. data/lib/java/processing/opengl/PGL.java +0 -3483
  133. data/lib/java/processing/opengl/PGraphics2D.java +0 -615
  134. data/lib/java/processing/opengl/PGraphics3D.java +0 -281
  135. data/lib/java/processing/opengl/PGraphicsOpenGL.java +0 -13753
  136. data/lib/java/processing/opengl/PJOGL.java +0 -2008
  137. data/lib/java/processing/opengl/PShader.java +0 -1484
  138. data/lib/java/processing/opengl/PShapeOpenGL.java +0 -5269
  139. data/lib/java/processing/opengl/PSurfaceJOGL.java +0 -1385
  140. data/lib/java/processing/opengl/Texture.java +0 -1696
  141. data/lib/java/processing/opengl/VertexBuffer.java +0 -88
  142. data/lib/java/processing/pdf/PGraphicsPDF.java +0 -581
  143. data/lib/java/processing/svg/PGraphicsSVG.java +0 -378
  144. data/src/main/java/processing/opengl/cursors/arrow.png +0 -0
  145. data/src/main/java/processing/opengl/cursors/cross.png +0 -0
  146. data/src/main/java/processing/opengl/cursors/hand.png +0 -0
  147. data/src/main/java/processing/opengl/cursors/license.txt +0 -27
  148. data/src/main/java/processing/opengl/cursors/move.png +0 -0
  149. data/src/main/java/processing/opengl/cursors/text.png +0 -0
  150. data/src/main/java/processing/opengl/cursors/wait.png +0 -0
  151. data/src/main/java/processing/opengl/shaders/ColorFrag.glsl +0 -32
  152. data/src/main/java/processing/opengl/shaders/ColorVert.glsl +0 -34
  153. data/src/main/java/processing/opengl/shaders/LightFrag.glsl +0 -33
  154. data/src/main/java/processing/opengl/shaders/LightVert.glsl +0 -151
  155. data/src/main/java/processing/opengl/shaders/LineFrag.glsl +0 -32
  156. data/src/main/java/processing/opengl/shaders/LineVert.glsl +0 -100
  157. data/src/main/java/processing/opengl/shaders/MaskFrag.glsl +0 -40
  158. data/src/main/java/processing/opengl/shaders/PointFrag.glsl +0 -32
  159. data/src/main/java/processing/opengl/shaders/PointVert.glsl +0 -56
  160. data/src/main/java/processing/opengl/shaders/TexFrag.glsl +0 -37
  161. data/src/main/java/processing/opengl/shaders/TexLightFrag.glsl +0 -37
  162. data/src/main/java/processing/opengl/shaders/TexLightVert.glsl +0 -157
  163. data/src/main/java/processing/opengl/shaders/TexVert.glsl +0 -38
@@ -1,2282 +0,0 @@
1
- package processing.data;
2
-
3
- // This code has been modified heavily to more closely match the rest of the
4
- // Processing API. In the spirit of the rest of the project, where we try to
5
- // keep the API as simple as possible, we have erred on the side of being
6
- // conservative in choosing which functions to include, since we haven't yet
7
- // decided what's truly necessary. Power users looking for a full-featured
8
- // version can use the original version from json.org, or one of the many
9
- // other APIs that are available. As with all Processing API, if there's a
10
- // function that should be added, please let use know, and have others vote:
11
- // http://code.google.com/p/processing/issues/list
12
-
13
- /*
14
- Copyright (c) 2002 JSON.org
15
-
16
- Permission is hereby granted, free of charge, to any person obtaining a copy
17
- of this software and associated documentation files (the "Software"), to deal
18
- in the Software without restriction, including without limitation the rights
19
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20
- copies of the Software, and to permit persons to whom the Software is
21
- furnished to do so, subject to the following conditions:
22
-
23
- The above copyright notice and this permission notice shall be included in all
24
- copies or substantial portions of the Software.
25
-
26
- The Software shall be used for Good, not Evil.
27
-
28
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34
- SOFTWARE.
35
- */
36
-
37
- import java.io.File;
38
- import java.io.IOException;
39
- import java.io.PrintWriter;
40
- import java.io.Reader;
41
- import java.io.StringWriter;
42
- import java.io.Writer;
43
- import java.lang.reflect.Method;
44
- import java.lang.reflect.Modifier;
45
- import java.util.Collection;
46
- import java.util.HashMap;
47
- import java.util.Iterator;
48
- import java.util.Map;
49
- import java.util.Set;
50
-
51
- import processing.core.PApplet;
52
-
53
- /**
54
- * A JSONObject is an unordered collection of name/value pairs. Its external
55
- * form is a string wrapped in curly braces with colons between the names and
56
- * values, and commas between the values and names. The internal form is an
57
- * object having <code>get</code> and <code>opt</code> methods for accessing the
58
- * values by name, and <code>put</code> methods for adding or replacing values
59
- * by name. The values can be any of these types: <code>Boolean</code>,
60
- * <code>JSONArray</code>, <code>JSONObject</code>, <code>Number</code>,
61
- * <code>String</code>, or the <code>JSONObject.NULL</code> object. A JSONObject
62
- * constructor can be used to convert an external form JSON text into an
63
- * internal form whose values can be retrieved with the <code>get</code> and
64
- * <code>opt</code> methods, or to convert values into a JSON text using the
65
- * <code>put</code> and <code>toString</code> methods. A <code>get</code> method
66
- * returns a value if one can be found, and throws an exception if one cannot be
67
- * found. An <code>opt</code> method returns a default value instead of throwing
68
- * an exception, and so is useful for obtaining optional values.
69
- * <p>
70
- * The generic <code>get()</code> and <code>opt()</code> methods return an
71
- * object, which you can cast or query for type. There are also typed
72
- * <code>get</code> and <code>opt</code> methods that do type checking and type
73
- * coercion for you. The opt methods differ from the get methods in that they do
74
- * not throw. Instead, they return a specified value, such as null.
75
- * <p>
76
- * The <code>put</code> methods add or replace values in an object. For example,
77
- *
78
- * <pre>
79
- * myString = new JSONObject().put(&quot;JSON&quot;, &quot;Hello, World!&quot;).toString();
80
- * </pre>
81
- *
82
- * produces the string <code>{"JSON": "Hello, World"}</code>.
83
- * <p>
84
- * The texts produced by the <code>toString</code> methods strictly conform to
85
- * the JSON syntax rules. The constructors are more forgiving in the texts they
86
- * will accept:
87
- * <ul>
88
- * <li>An extra <code>,</code>&nbsp;<small>(comma)</small> may appear just
89
- * before the closing brace.</li>
90
- * <li>Strings may be quoted with <code>'</code>&nbsp;<small>(single
91
- * quote)</small>.</li>
92
- * <li>Strings do not need to be quoted at all if they do not begin with a quote
93
- * or single quote, and if they do not contain leading or trailing spaces, and
94
- * if they do not contain any of these characters:
95
- * <code>{ } [ ] / \ : , = ; #</code> and if they do not look like numbers and
96
- * if they are not the reserved words <code>true</code>, <code>false</code>, or
97
- * <code>null</code>.</li>
98
- * <li>Keys can be followed by <code>=</code> or {@code =>} as well as by
99
- * <code>:</code>.</li>
100
- * <li>Values can be followed by <code>;</code> <small>(semicolon)</small> as
101
- * well as by <code>,</code> <small>(comma)</small>.</li>
102
- * </ul>
103
- *
104
- * @author JSON.org
105
- * @version 2012-12-01
106
- * @webref data:composite
107
- * @see JSONArray
108
- * @see PApplet#loadJSONObject(String)
109
- * @see PApplet#loadJSONArray(String)
110
- * @see PApplet#saveJSONObject(JSONObject, String)
111
- * @see PApplet#saveJSONArray(JSONArray, String)
112
- */
113
- public class JSONObject {
114
- /**
115
- * The maximum number of keys in the key pool.
116
- */
117
- private static final int keyPoolSize = 100;
118
-
119
- /**
120
- * Key pooling is like string interning, but without permanently tying up
121
- * memory. To help conserve memory, storage of duplicated key strings in
122
- * JSONObjects will be avoided by using a key pool to manage unique key
123
- * string objects. This is used by JSONObject.put(string, object).
124
- */
125
- private static HashMap<String, Object> keyPool =
126
- new HashMap<>(keyPoolSize);
127
-
128
-
129
- // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
130
-
131
-
132
- /**
133
- * JSONObject.NULL is equivalent to the value that JavaScript calls null,
134
- * whilst Java's null is equivalent to the value that JavaScript calls
135
- * undefined.
136
- */
137
- private static final class Null {
138
- /**
139
- * There is only intended to be a single instance of the NULL object,
140
- * so the clone method returns itself.
141
- * @return NULL.
142
- */
143
- @Override
144
- protected final Object clone() {
145
- return this;
146
- }
147
-
148
- /**
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.
153
- */
154
- @Override
155
- public boolean equals(Object object) {
156
- return object == null || object == this;
157
- }
158
-
159
- /**
160
- * Get the "null" string value.
161
- * @return The string "null".
162
- */
163
- @Override
164
- public String toString() {
165
- return "null";
166
- }
167
-
168
- @Override
169
- public int hashCode() {
170
- // TODO Auto-generated method stub
171
- return super.hashCode();
172
- }
173
- }
174
-
175
-
176
- // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
177
-
178
-
179
- /**
180
- * The map where the JSONObject's properties are kept.
181
- */
182
- // private final Map map;
183
- private final HashMap<String, Object> map;
184
-
185
-
186
- /**
187
- * It is sometimes more convenient and less ambiguous to have a
188
- * <code>NULL</code> object than to use Java's <code>null</code> value.
189
- * <code>JSONObject.NULL.equals(null)</code> returns <code>true</code>.
190
- * <code>JSONObject.NULL.toString()</code> returns <code>"null"</code>.
191
- */
192
- public static final Object NULL = new Null();
193
-
194
-
195
- /**
196
- * Construct an empty JSONObject.
197
- * @nowebref
198
- */
199
- public JSONObject() {
200
- this.map = new HashMap<>();
201
- }
202
-
203
-
204
- // /**
205
- // * Construct a JSONObject from a subset of another JSONObject.
206
- // * An array of strings is used to identify the keys that should be copied.
207
- // * Missing keys are ignored.
208
- // * @param jo A JSONObject.
209
- // * @param names An array of strings.
210
- // */
211
- // public JSONObject(JSONObject jo, String[] names) {
212
- // this();
213
- // for (int i = 0; i < names.length; i += 1) {
214
- // try {
215
- // this.putOnce(names[i], jo.opt(names[i]));
216
- // } catch (Exception ignore) {
217
- // }
218
- // }
219
- // }
220
-
221
-
222
- /**
223
- * @nowebref
224
- */
225
- public JSONObject(Reader reader) {
226
- this(new JSONTokener(reader));
227
- }
228
-
229
-
230
- /**
231
- * Construct a JSONObject from a JSONTokener.
232
- * @param x A JSONTokener object containing the source string.
233
- * @throws RuntimeException If there is a syntax error in the source string
234
- * or a duplicated key.
235
- */
236
- protected JSONObject(JSONTokener x) {
237
- this();
238
- char c;
239
- String key;
240
-
241
- if (x.nextClean() != '{') {
242
- throw new RuntimeException("A JSONObject text must begin with '{'");
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
- }
255
-
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();
262
- }
263
- } else if (c != ':') {
264
- throw new RuntimeException("Expected a ':' after a key");
265
- }
266
- this.putOnce(key, x.nextValue());
267
-
268
- // Pairs are separated by ','. We will also tolerate ';'.
269
-
270
- switch (x.nextClean()) {
271
- case ';':
272
- case ',':
273
- if (x.nextClean() == '}') {
274
- return;
275
- }
276
- x.back();
277
- break;
278
- case '}':
279
- return;
280
- default:
281
- throw new RuntimeException("Expected a ',' or '}'");
282
- }
283
- }
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));
302
- }
303
- }
304
- }
305
- }
306
-
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));
315
- }
316
- }
317
-
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));
326
- }
327
- }
328
-
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
367
- // /**
368
- // * Construct a JSONObject from an Object, using reflection to find the
369
- // * public members. The resulting JSONObject's keys will be the strings
370
- // * from the names array, and the values will be the field values associated
371
- // * with those keys in the object. If a key is not found or not visible,
372
- // * then it will not be copied into the new JSONObject.
373
- // * @param object An object that has fields that should be used to make a
374
- // * JSONObject.
375
- // * @param names An array of strings, the names of the fields to be obtained
376
- // * from the object.
377
- // */
378
- // public JSONObject(Object object, String names[]) {
379
- // this();
380
- // Class c = object.getClass();
381
- // for (int i = 0; i < names.length; i += 1) {
382
- // String name = names[i];
383
- // try {
384
- // this.putOpt(name, c.getField(name).get(object));
385
- // } catch (Exception ignore) {
386
- // }
387
- // }
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
-
404
-
405
- // /**
406
- // * Construct a JSONObject from a ResourceBundle.
407
- // * @param baseName The ResourceBundle base name.
408
- // * @param locale The Locale to load the ResourceBundle for.
409
- // * @throws JSONException If any JSONExceptions are detected.
410
- // */
411
- // public JSON(String baseName, Locale locale) {
412
- // this();
413
- // ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale,
414
- // Thread.currentThread().getContextClassLoader());
415
- //
416
- // // Iterate through the keys in the bundle.
417
- //
418
- // Enumeration keys = bundle.getKeys();
419
- // while (keys.hasMoreElements()) {
420
- // Object key = keys.nextElement();
421
- // if (key instanceof String) {
422
- //
423
- // // Go through the path, ensuring that there is a nested JSONObject for each
424
- // // segment except the last. Add the value using the last segment's name into
425
- // // the deepest nested JSONObject.
426
- //
427
- // String[] path = ((String)key).split("\\.");
428
- // int last = path.length - 1;
429
- // JSON target = this;
430
- // for (int i = 0; i < last; i += 1) {
431
- // String segment = path[i];
432
- // JSON nextTarget = target.optJSONObject(segment);
433
- // if (nextTarget == null) {
434
- // nextTarget = new JSON();
435
- // target.put(segment, nextTarget);
436
- // }
437
- // target = nextTarget;
438
- // }
439
- // target.put(path[last], bundle.getString((String)key));
440
- // }
441
- // }
442
- // }
443
-
444
-
445
- // /**
446
- // * Accumulate values under a key. It is similar to the put method except
447
- // * that if there is already an object stored under the key then a
448
- // * JSONArray is stored under the key to hold all of the accumulated values.
449
- // * If there is already a JSONArray, then the new value is appended to it.
450
- // * In contrast, the put method replaces the previous value.
451
- // *
452
- // * If only one value is accumulated that is not a JSONArray, then the
453
- // * result will be the same as using put. But if multiple values are
454
- // * accumulated, then the result will be like append.
455
- // * @param key A key string.
456
- // * @param value An object to be accumulated under the key.
457
- // * @return this.
458
- // * @throws JSONException If the value is an invalid number
459
- // * or if the key is null.
460
- // */
461
- // public JSONObject accumulate(
462
- // String key,
463
- // Object value
464
- // ) throws JSONException {
465
- // testValidity(value);
466
- // Object object = this.opt(key);
467
- // if (object == null) {
468
- // this.put(key, value instanceof JSONArray
469
- // ? new JSONArray().put(value)
470
- // : value);
471
- // } else if (object instanceof JSONArray) {
472
- // ((JSONArray)object).put(value);
473
- // } else {
474
- // this.put(key, new JSONArray().put(object).put(value));
475
- // }
476
- // return this;
477
- // }
478
-
479
-
480
- // /**
481
- // * Append values to the array under a key. If the key does not exist in the
482
- // * JSONObject, then the key is put in the JSONObject with its value being a
483
- // * JSONArray containing the value parameter. If the key was already
484
- // * associated with a JSONArray, then the value parameter is appended to it.
485
- // * @param key A key string.
486
- // * @param value An object to be accumulated under the key.
487
- // * @return this.
488
- // * @throws JSONException If the key is null or if the current value
489
- // * associated with the key is not a JSONArray.
490
- // */
491
- // public JSONObject append(String key, Object value) throws JSONException {
492
- // testValidity(value);
493
- // Object object = this.opt(key);
494
- // if (object == null) {
495
- // this.put(key, new JSONArray().put(value));
496
- // } else if (object instanceof JSONArray) {
497
- // this.put(key, ((JSONArray)object).put(value));
498
- // } else {
499
- // throw new JSONException("JSONObject[" + key +
500
- // "] is not a JSONArray.");
501
- // }
502
- // return this;
503
- // }
504
-
505
-
506
- /**
507
- * Produce a string from a double. The string "null" will be returned if
508
- * the number is not finite.
509
- * @param d A double.
510
- * @return A String.
511
- */
512
- static protected String doubleToString(double d) {
513
- if (Double.isInfinite(d) || Double.isNaN(d)) {
514
- return "null";
515
- }
516
-
517
- // Shave off trailing zeros and decimal point, if possible.
518
-
519
- String string = Double.toString(d);
520
- if (string.indexOf('.') > 0 && string.indexOf('e') < 0 &&
521
- string.indexOf('E') < 0) {
522
- while (string.endsWith("0")) {
523
- string = string.substring(0, string.length() - 1);
524
- }
525
- if (string.endsWith(".")) {
526
- string = string.substring(0, string.length() - 1);
527
- }
528
- }
529
- return string;
530
- }
531
-
532
-
533
- /**
534
- * Get the value object associated with a key.
535
- *
536
- * @param key A key string.
537
- * @return The object associated with the key.
538
- * @throws RuntimeException if the key is not found.
539
- */
540
- public Object get(String key) {
541
- if (key == null) {
542
- throw new RuntimeException("JSONObject.get(null) called");
543
- }
544
- Object object = this.opt(key);
545
- if (object == null) {
546
- // Adding for rev 0257 in line with other p5 api
547
- return null;
548
- }
549
- if (object == null) {
550
- throw new RuntimeException("JSONObject[" + quote(key) + "] not found");
551
- }
552
- return object;
553
- }
554
-
555
-
556
- /**
557
- * Gets the String associated with a key
558
- *
559
- * @webref jsonobject:method
560
- * @brief Gets the string value associated with a key
561
- * @param key a key string
562
- * @return A string which is the value.
563
- * @throws RuntimeException if there is no string value for the key.
564
- * @see JSONObject#getInt(String)
565
- * @see JSONObject#getFloat(String)
566
- * @see JSONObject#getBoolean(String)
567
- */
568
- public String getString(String key) {
569
- Object object = this.get(key);
570
- if (object == null) {
571
- // Adding for rev 0257 in line with other p5 api
572
- return null;
573
- }
574
- if (object instanceof String) {
575
- return (String)object;
576
- }
577
- throw new RuntimeException("JSONObject[" + quote(key) + "] is not a string");
578
- }
579
-
580
-
581
- /**
582
- * Get an optional string associated with a key.
583
- * It returns the defaultValue if there is no such key.
584
- *
585
- * @param key A key string.
586
- * @param defaultValue The default.
587
- * @return A string which is the value.
588
- */
589
- public String getString(String key, String defaultValue) {
590
- Object object = this.opt(key);
591
- return NULL.equals(object) ? defaultValue : object.toString();
592
- }
593
-
594
-
595
- /**
596
- * Gets the int value associated with a key
597
- *
598
- * @webref jsonobject:method
599
- * @brief Gets the int value associated with a key
600
- * @param key A key string.
601
- * @return The integer value.
602
- * @throws RuntimeException if the key is not found or if the value cannot
603
- * be converted to an integer.
604
- * @see JSONObject#getFloat(String)
605
- * @see JSONObject#getString(String)
606
- * @see JSONObject#getBoolean(String)
607
- */
608
- public int getInt(String key) {
609
- Object object = this.get(key);
610
- if (object == null) {
611
- throw new RuntimeException("JSONObject[" + quote(key) + "] not found");
612
- }
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.");
618
- }
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;
637
- }
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);
657
- }
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;
676
- }
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;
698
- }
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;
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
-
832
-
833
- // /**
834
- // * Get an array of field names from a JSONObject.
835
- // *
836
- // * @return An array of field names, or null if there are no names.
837
- // */
838
- // public static String[] getNames(JSONObject jo) {
839
- // int length = jo.length();
840
- // if (length == 0) {
841
- // return null;
842
- // }
843
- // Iterator iterator = jo.keys();
844
- // String[] names = new String[length];
845
- // int i = 0;
846
- // while (iterator.hasNext()) {
847
- // names[i] = (String)iterator.next();
848
- // i += 1;
849
- // }
850
- // return names;
851
- // }
852
- //
853
- //
854
- // /**
855
- // * Get an array of field names from an Object.
856
- // *
857
- // * @return An array of field names, or null if there are no names.
858
- // */
859
- // public static String[] getNames(Object object) {
860
- // if (object == null) {
861
- // return null;
862
- // }
863
- // Class klass = object.getClass();
864
- // Field[] fields = klass.getFields();
865
- // int length = fields.length;
866
- // if (length == 0) {
867
- // return null;
868
- // }
869
- // String[] names = new String[length];
870
- // for (int i = 0; i < length; i += 1) {
871
- // names[i] = fields[i].getName();
872
- // }
873
- // return names;
874
- // }
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
-
886
-
887
- // /**
888
- // * Increment a property of a JSONObject. If there is no such property,
889
- // * create one with a value of 1. If there is such a property, and if
890
- // * it is an Integer, Long, Double, or Float, then add one to it.
891
- // * @param key A key string.
892
- // * @return this.
893
- // * @throws JSONException If there is already a property with this name
894
- // * that is not an Integer, Long, Double, or Float.
895
- // */
896
- // public JSON increment(String key) {
897
- // Object value = this.opt(key);
898
- // if (value == null) {
899
- // this.put(key, 1);
900
- // } else if (value instanceof Integer) {
901
- // this.put(key, ((Integer)value).intValue() + 1);
902
- // } else if (value instanceof Long) {
903
- // this.put(key, ((Long)value).longValue() + 1);
904
- // } else if (value instanceof Double) {
905
- // this.put(key, ((Double)value).doubleValue() + 1);
906
- // } else if (value instanceof Float) {
907
- // this.put(key, ((Float)value).floatValue() + 1);
908
- // } else {
909
- // throw new RuntimeException("Unable to increment [" + quote(key) + "].");
910
- // }
911
- // return this;
912
- // }
913
-
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() {
935
- // return this.keySet().iterator();
936
- return map.keySet().iterator();
937
- }
938
-
939
-
940
- /**
941
- * Get a set of keys of the JSONObject.
942
- *
943
- * @return A keySet.
944
- */
945
- public Set keys() {
946
- return this.map.keySet();
947
- }
948
-
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
-
959
-
960
- // /**
961
- // * Produce a JSONArray containing the names of the elements of this
962
- // * JSONObject.
963
- // * @return A JSONArray containing the key strings, or null if the JSONObject
964
- // * is empty.
965
- // */
966
- // public JSONArray names() {
967
- // JSONArray ja = new JSONArray();
968
- // Iterator keys = this.keys();
969
- // while (keys.hasNext()) {
970
- // ja.append(keys.next());
971
- // }
972
- // return ja.size() == 0 ? null : ja;
973
- // }
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");
985
- }
986
- testValidity(number);
987
-
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
- }
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
-
1013
-
1014
- // /**
1015
- // * Get an optional boolean associated with a key.
1016
- // * It returns false if there is no such key, or if the value is not
1017
- // * Boolean.TRUE or the String "true".
1018
- // *
1019
- // * @param key A key string.
1020
- // * @return The truth.
1021
- // */
1022
- // private boolean optBoolean(String key) {
1023
- // return this.optBoolean(key, false);
1024
- // }
1025
-
1026
-
1027
- // /**
1028
- // * Get an optional double associated with a key,
1029
- // * or NaN if there is no such key or if its value is not a number.
1030
- // * If the value is a string, an attempt will be made to evaluate it as
1031
- // * a number.
1032
- // *
1033
- // * @param key A string which is the key.
1034
- // * @return An object which is the value.
1035
- // */
1036
- // private double optDouble(String key) {
1037
- // return this.optDouble(key, Double.NaN);
1038
- // }
1039
-
1040
-
1041
- // /**
1042
- // * Get an optional int value associated with a key,
1043
- // * or zero if there is no such key or if the value is not a number.
1044
- // * If the value is a string, an attempt will be made to evaluate it as
1045
- // * a number.
1046
- // *
1047
- // * @param key A key string.
1048
- // * @return An object which is the value.
1049
- // */
1050
- // private int optInt(String key) {
1051
- // return this.optInt(key, 0);
1052
- // }
1053
-
1054
-
1055
- // /**
1056
- // * Get an optional JSONArray associated with a key.
1057
- // * It returns null if there is no such key, or if its value is not a
1058
- // * JSONArray.
1059
- // *
1060
- // * @param key A key string.
1061
- // * @return A JSONArray which is the value.
1062
- // */
1063
- // private JSONArray optJSONArray(String key) {
1064
- // Object o = this.opt(key);
1065
- // return o instanceof JSONArray ? (JSONArray)o : null;
1066
- // }
1067
-
1068
-
1069
- // /**
1070
- // * Get an optional JSONObject associated with a key.
1071
- // * It returns null if there is no such key, or if its value is not a
1072
- // * JSONObject.
1073
- // *
1074
- // * @param key A key string.
1075
- // * @return A JSONObject which is the value.
1076
- // */
1077
- // private JSONObject optJSONObject(String key) {
1078
- // Object object = this.opt(key);
1079
- // return object instanceof JSONObject ? (JSONObject)object : null;
1080
- // }
1081
-
1082
-
1083
- // /**
1084
- // * Get an optional long value associated with a key,
1085
- // * or zero if there is no such key or if the value is not a number.
1086
- // * If the value is a string, an attempt will be made to evaluate it as
1087
- // * a number.
1088
- // *
1089
- // * @param key A key string.
1090
- // * @return An object which is the value.
1091
- // */
1092
- // public long optLong(String key) {
1093
- // return this.optLong(key, 0);
1094
- // }
1095
-
1096
-
1097
- // /**
1098
- // * Get an optional string associated with a key.
1099
- // * It returns an empty string if there is no such key. If the value is not
1100
- // * a string and is not null, then it is converted to a string.
1101
- // *
1102
- // * @param key A key string.
1103
- // * @return A string which is the value.
1104
- // */
1105
- // public String optString(String key) {
1106
- // return this.optString(key, "");
1107
- // }
1108
-
1109
-
1110
- private void populateMap(Object bean) {
1111
- Class klass = bean.getClass();
1112
-
1113
- // If klass is a System class then set includeSuperClass to false.
1114
-
1115
- boolean includeSuperClass = klass.getClassLoader() != null;
1116
-
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
- }
1277
-
1278
-
1279
- // /**
1280
- // * Put a key/value pair in the JSONObject, where the value will be a
1281
- // * JSONArray which is produced from a Collection.
1282
- // * @param key A key string.
1283
- // * @param value A Collection value.
1284
- // * @return this.
1285
- // * @throws JSONException
1286
- // */
1287
- // public JSONObject put(String key, Collection value) {
1288
- // this.put(key, new JSONArray(value));
1289
- // return this;
1290
- // }
1291
-
1292
-
1293
- // /**
1294
- // * Put a key/value pair in the JSONObject, where the value will be a
1295
- // * JSONObject which is produced from a Map.
1296
- // * @param key A key string.
1297
- // * @param value A Map value.
1298
- // * @return this.
1299
- // * @throws JSONException
1300
- // */
1301
- // //public JSONObject put(String key, HashMap<String, Object> value) {
1302
- // public JSONObject put(String key, Map value) {
1303
- // this.put(key, new JSONObject(value));
1304
- // return this;
1305
- // }
1306
-
1307
-
1308
- /**
1309
- * Put a key/value pair in the JSONObject. If the value is null,
1310
- * then the key will be removed from the JSONObject if it is present.
1311
- * @param key A key string.
1312
- * @param value An object which is the value. It should be of one of these
1313
- * types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String,
1314
- * or the JSONObject.NULL object.
1315
- * @return this.
1316
- * @throws RuntimeException If the value is non-finite number
1317
- * or if the key is null.
1318
- */
1319
- public JSONObject put(String key, Object value) {
1320
- String pooled;
1321
- if (key == null) {
1322
- throw new RuntimeException("Null key.");
1323
- }
1324
- if (value != null) {
1325
- testValidity(value);
1326
- pooled = (String)keyPool.get(key);
1327
- if (pooled == null) {
1328
- if (keyPool.size() >= keyPoolSize) {
1329
- keyPool = new HashMap<>(keyPoolSize);
1330
- }
1331
- keyPool.put(key, key);
1332
- } else {
1333
- key = pooled;
1334
- }
1335
- this.map.put(key, value);
1336
- } else {
1337
- this.remove(key);
1338
- }
1339
- return this;
1340
- }
1341
-
1342
-
1343
- /**
1344
- * Put a key/value pair in the JSONObject, but only if the key and the
1345
- * value are both non-null, and only if there is not already a member
1346
- * with that name.
1347
- * @param key
1348
- * @param value
1349
- * @return {@code this}.
1350
- * @throws RuntimeException if the key is a duplicate, or if
1351
- * {@link #put(String,Object)} throws.
1352
- */
1353
- private JSONObject putOnce(String key, Object value) {
1354
- if (key != null && value != null) {
1355
- if (this.opt(key) != null) {
1356
- throw new RuntimeException("Duplicate key \"" + key + "\"");
1357
- }
1358
- this.put(key, value);
1359
- }
1360
- return this;
1361
- }
1362
-
1363
-
1364
- // /**
1365
- // * Put a key/value pair in the JSONObject, but only if the
1366
- // * key and the value are both non-null.
1367
- // * @param key A key string.
1368
- // * @param value An object which is the value. It should be of one of these
1369
- // * types: Boolean, Double, Integer, JSONArray, JSONObject, Long, String,
1370
- // * or the JSONObject.NULL object.
1371
- // * @return this.
1372
- // * @throws JSONException If the value is a non-finite number.
1373
- // */
1374
- // public JSONObject putOpt(String key, Object value) {
1375
- // if (key != null && value != null) {
1376
- // this.put(key, value);
1377
- // }
1378
- // return this;
1379
- // }
1380
-
1381
-
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
- }
1399
- }
1400
- }
1401
-
1402
- static public Writer quote(String string, Writer w) throws IOException {
1403
- if (string == null || string.length() == 0) {
1404
- w.write("\"\"");
1405
- return w;
1406
- }
1407
-
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('\\');
1427
- }
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);
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
- }
1493
-
1494
- /*
1495
- * If it might be a number, try converting it.
1496
- * If a number cannot be produced, then the value will just
1497
- * be a string. Note that the plus and implied string
1498
- * conventions are non-standard. A JSON parser may accept
1499
- * non-JSON forms as long as it accepts all correct JSON forms.
1500
- */
1501
-
1502
- char b = string.charAt(0);
1503
- if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') {
1504
- try {
1505
- if (string.indexOf('.') > -1 ||
1506
- string.indexOf('e') > -1 || string.indexOf('E') > -1) {
1507
- d = Double.valueOf(string);
1508
- if (!d.isInfinite() && !d.isNaN()) {
1509
- return d;
1510
- }
1511
- } else {
1512
- Long myLong = Long.valueOf(string);
1513
- if (myLong.longValue() == myLong.intValue()) {
1514
- return Integer.valueOf(myLong.intValue());
1515
- } else {
1516
- return myLong;
1517
- }
1518
- }
1519
- } catch (Exception ignore) {
1520
- }
1521
- }
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.");
1543
- }
1544
- }
1545
- }
1546
- }
1547
-
1548
-
1549
- // /**
1550
- // * Produce a JSONArray containing the values of the members of this
1551
- // * JSONObject.
1552
- // * @param names A JSONArray containing a list of key strings. This
1553
- // * determines the sequence of the values in the result.
1554
- // * @return A JSONArray of values.
1555
- // * @throws JSONException If any of the values are non-finite numbers.
1556
- // */
1557
- // public JSONArray toJSONArray(JSONArray names) {
1558
- // if (names == null || names.size() == 0) {
1559
- // return null;
1560
- // }
1561
- // JSONArray ja = new JSONArray();
1562
- // for (int i = 0; i < names.size(); i += 1) {
1563
- // ja.append(this.opt(names.getString(i)));
1564
- // }
1565
- // return ja;
1566
- // }
1567
-
1568
-
1569
- // protected boolean save(OutputStream output) {
1570
- // return save(PApplet.createWriter(output));
1571
- // }
1572
-
1573
-
1574
- public boolean save(File file, String options) {
1575
- PrintWriter writer = PApplet.createWriter(file);
1576
- boolean success = write(writer, options);
1577
- writer.close();
1578
- return success;
1579
- }
1580
-
1581
-
1582
- public boolean write(PrintWriter output) {
1583
- return write(output, null);
1584
- }
1585
-
1586
-
1587
- public boolean write(PrintWriter output, String options) {
1588
- int indentFactor = 2;
1589
- if (options != null) {
1590
- String[] opts = PApplet.split(options, ',');
1591
- for (String opt : opts) {
1592
- if (opt.equals("compact")) {
1593
- indentFactor = -1;
1594
- } else if (opt.startsWith("indent=")) {
1595
- indentFactor = PApplet.parseInt(opt.substring(7), -2);
1596
- if (indentFactor == -2) {
1597
- throw new IllegalArgumentException("Could not read a number from " + opt);
1598
- }
1599
- } else {
1600
- System.err.println("Ignoring " + opt);
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
- }
1669
- // if (value instanceof JSONString) {
1670
- // Object object;
1671
- // try {
1672
- // object = ((JSONString)value).toJSONString();
1673
- // } catch (Exception e) {
1674
- // throw new RuntimeException(e);
1675
- // }
1676
- // if (object instanceof String) {
1677
- // return (String)object;
1678
- // }
1679
- // throw new RuntimeException("Bad value from toJSONString: " + object);
1680
- // }
1681
- if (value instanceof Number) {
1682
- return numberToString((Number) value);
1683
- }
1684
- if (value instanceof Boolean || value instanceof JSONObject ||
1685
- value instanceof JSONArray) {
1686
- return value.toString();
1687
- }
1688
- if (value instanceof Map) {
1689
- return new JSONObject(value).toString();
1690
- }
1691
- if (value instanceof Collection) {
1692
- return new JSONArray(value).toString();
1693
- }
1694
- if (value.getClass().isArray()) {
1695
- return new JSONArray(value).toString();
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
- }
1726
-
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();
1746
- }
1747
- return new JSONObject(object);
1748
- } catch(Exception exception) {
1749
- return null;
1750
- }
1751
- }
1752
-
1753
-
1754
- // /**
1755
- // * Write the contents of the JSONObject as JSON text to a writer.
1756
- // * For compactness, no whitespace is added.
1757
- // * <p>
1758
- // * Warning: This method assumes that the data structure is acyclical.
1759
- // *
1760
- // * @return The writer.
1761
- // * @throws JSONException
1762
- // */
1763
- // protected Writer write(Writer writer) {
1764
- // return this.write(writer, 0, 0);
1765
- // }
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
- /*
1788
- } else if (value instanceof JSONString) {
1789
- Object o;
1790
- try {
1791
- o = ((JSONString) value).toJSONString();
1792
- } catch (Exception e) {
1793
- throw new RuntimeException(e);
1794
- }
1795
- writer.write(o != null ? o.toString() : quote(value.toString()));
1796
- */
1797
- } else {
1798
- quote(value.toString(), writer);
1799
- }
1800
- return writer;
1801
- }
1802
-
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) {
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;
1855
- }
1856
- if (indentFactor != -1) {
1857
- writer.write('\n');
1858
- }
1859
- indent(writer, indent);
1860
- }
1861
- writer.write('}');
1862
- return writer;
1863
- } catch (IOException exception) {
1864
- throw new RuntimeException(exception);
1865
- }
1866
- }
1867
-
1868
-
1869
- // // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1870
- //
1871
- //
1872
- // class JSONException extends RuntimeException {
1873
- //
1874
- // public JSONException(String message) {
1875
- // super(message);
1876
- // }
1877
- //
1878
- // public JSONException(Throwable throwable) {
1879
- // super(throwable);
1880
- // }
1881
- // }
1882
-
1883
-
1884
- // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1885
-
1886
-
1887
- // /**
1888
- // * Get the hex value of a character (base16).
1889
- // * @param c A character between '0' and '9' or between 'A' and 'F' or
1890
- // * between 'a' and 'f'.
1891
- // * @return An int between 0 and 15, or -1 if c was not a hex digit.
1892
- // */
1893
- // static protected int dehexchar(char c) {
1894
- // if (c >= '0' && c <= '9') {
1895
- // return c - '0';
1896
- // }
1897
- // if (c >= 'A' && c <= 'F') {
1898
- // return c - ('A' - 10);
1899
- // }
1900
- // if (c >= 'a' && c <= 'f') {
1901
- // return c - ('a' - 10);
1902
- // }
1903
- // return -1;
1904
- // }
1905
-
1906
-
1907
- // static class JSONTokener {
1908
- // private long character;
1909
- // private boolean eof;
1910
- // private long index;
1911
- // private long line;
1912
- // private char previous;
1913
- // private Reader reader;
1914
- // private boolean usePrevious;
1915
- //
1916
- //
1917
- // /**
1918
- // * Construct a JSONTokener from a Reader.
1919
- // *
1920
- // * @param reader A reader.
1921
- // */
1922
- // public JSONTokener(Reader reader) {
1923
- // this.reader = reader.markSupported()
1924
- // ? reader
1925
- // : new BufferedReader(reader);
1926
- // this.eof = false;
1927
- // this.usePrevious = false;
1928
- // this.previous = 0;
1929
- // this.index = 0;
1930
- // this.character = 1;
1931
- // this.line = 1;
1932
- // }
1933
- //
1934
- //
1935
- // /**
1936
- // * Construct a JSONTokener from an InputStream.
1937
- // */
1938
- // public JSONTokener(InputStream inputStream) {
1939
- // this(new InputStreamReader(inputStream));
1940
- // }
1941
- //
1942
- //
1943
- // /**
1944
- // * Construct a JSONTokener from a string.
1945
- // *
1946
- // * @param s A source string.
1947
- // */
1948
- // public JSONTokener(String s) {
1949
- // this(new StringReader(s));
1950
- // }
1951
- //
1952
- //
1953
- // /**
1954
- // * Back up one character. This provides a sort of lookahead capability,
1955
- // * so that you can test for a digit or letter before attempting to parse
1956
- // * the next number or identifier.
1957
- // */
1958
- // public void back() {
1959
- // if (this.usePrevious || this.index <= 0) {
1960
- // throw new RuntimeException("Stepping back two steps is not supported");
1961
- // }
1962
- // this.index -= 1;
1963
- // this.character -= 1;
1964
- // this.usePrevious = true;
1965
- // this.eof = false;
1966
- // }
1967
- //
1968
- //
1969
- // public boolean end() {
1970
- // return this.eof && !this.usePrevious;
1971
- // }
1972
- //
1973
- //
1974
- // /**
1975
- // * Determine if the source string still contains characters that next()
1976
- // * can consume.
1977
- // * @return true if not yet at the end of the source.
1978
- // */
1979
- // public boolean more() {
1980
- // this.next();
1981
- // if (this.end()) {
1982
- // return false;
1983
- // }
1984
- // this.back();
1985
- // return true;
1986
- // }
1987
- //
1988
- //
1989
- // /**
1990
- // * Get the next character in the source string.
1991
- // *
1992
- // * @return The next character, or 0 if past the end of the source string.
1993
- // */
1994
- // public char next() {
1995
- // int c;
1996
- // if (this.usePrevious) {
1997
- // this.usePrevious = false;
1998
- // c = this.previous;
1999
- // } else {
2000
- // try {
2001
- // c = this.reader.read();
2002
- // } catch (IOException exception) {
2003
- // throw new RuntimeException(exception);
2004
- // }
2005
- //
2006
- // if (c <= 0) { // End of stream
2007
- // this.eof = true;
2008
- // c = 0;
2009
- // }
2010
- // }
2011
- // this.index += 1;
2012
- // if (this.previous == '\r') {
2013
- // this.line += 1;
2014
- // this.character = c == '\n' ? 0 : 1;
2015
- // } else if (c == '\n') {
2016
- // this.line += 1;
2017
- // this.character = 0;
2018
- // } else {
2019
- // this.character += 1;
2020
- // }
2021
- // this.previous = (char) c;
2022
- // return this.previous;
2023
- // }
2024
- //
2025
- //
2026
- // /**
2027
- // * Consume the next character, and check that it matches a specified
2028
- // * character.
2029
- // * @param c The character to match.
2030
- // * @return The character.
2031
- // * @throws JSONException if the character does not match.
2032
- // */
2033
- // public char next(char c) {
2034
- // char n = this.next();
2035
- // if (n != c) {
2036
- // throw new RuntimeException("Expected '" + c + "' and instead saw '" + n + "'");
2037
- // }
2038
- // return n;
2039
- // }
2040
- //
2041
- //
2042
- // /**
2043
- // * Get the next n characters.
2044
- // *
2045
- // * @param n The number of characters to take.
2046
- // * @return A string of n characters.
2047
- // * @throws JSONException
2048
- // * Substring bounds error if there are not
2049
- // * n characters remaining in the source string.
2050
- // */
2051
- // public String next(int n) {
2052
- // if (n == 0) {
2053
- // return "";
2054
- // }
2055
- //
2056
- // char[] chars = new char[n];
2057
- // int pos = 0;
2058
- //
2059
- // while (pos < n) {
2060
- // chars[pos] = this.next();
2061
- // if (this.end()) {
2062
- // throw new RuntimeException("Substring bounds error");
2063
- // }
2064
- // pos += 1;
2065
- // }
2066
- // return new String(chars);
2067
- // }
2068
- //
2069
- //
2070
- // /**
2071
- // * Get the next char in the string, skipping whitespace.
2072
- // * @throws JSONException
2073
- // * @return A character, or 0 if there are no more characters.
2074
- // */
2075
- // public char nextClean() {
2076
- // for (;;) {
2077
- // char c = this.next();
2078
- // if (c == 0 || c > ' ') {
2079
- // return c;
2080
- // }
2081
- // }
2082
- // }
2083
- //
2084
- //
2085
- // /**
2086
- // * Return the characters up to the next close quote character.
2087
- // * Backslash processing is done. The formal JSON format does not
2088
- // * allow strings in single quotes, but an implementation is allowed to
2089
- // * accept them.
2090
- // * @param quote The quoting character, either
2091
- // * <code>"</code>&nbsp;<small>(double quote)</small> or
2092
- // * <code>'</code>&nbsp;<small>(single quote)</small>.
2093
- // * @return A String.
2094
- // * @throws JSONException Unterminated string.
2095
- // */
2096
- // public String nextString(char quote) {
2097
- // char c;
2098
- // StringBuffer sb = new StringBuffer();
2099
- // for (;;) {
2100
- // c = this.next();
2101
- // switch (c) {
2102
- // case 0:
2103
- // case '\n':
2104
- // case '\r':
2105
- // throw new RuntimeException("Unterminated string");
2106
- // case '\\':
2107
- // c = this.next();
2108
- // switch (c) {
2109
- // case 'b':
2110
- // sb.append('\b');
2111
- // break;
2112
- // case 't':
2113
- // sb.append('\t');
2114
- // break;
2115
- // case 'n':
2116
- // sb.append('\n');
2117
- // break;
2118
- // case 'f':
2119
- // sb.append('\f');
2120
- // break;
2121
- // case 'r':
2122
- // sb.append('\r');
2123
- // break;
2124
- // case 'u':
2125
- // sb.append((char)Integer.parseInt(this.next(4), 16));
2126
- // break;
2127
- // case '"':
2128
- // case '\'':
2129
- // case '\\':
2130
- // case '/':
2131
- // sb.append(c);
2132
- // break;
2133
- // default:
2134
- // throw new RuntimeException("Illegal escape.");
2135
- // }
2136
- // break;
2137
- // default:
2138
- // if (c == quote) {
2139
- // return sb.toString();
2140
- // }
2141
- // sb.append(c);
2142
- // }
2143
- // }
2144
- // }
2145
- //
2146
- //
2147
- // /**
2148
- // * Get the text up but not including the specified character or the
2149
- // * end of line, whichever comes first.
2150
- // * @param delimiter A delimiter character.
2151
- // * @return A string.
2152
- // */
2153
- // public String nextTo(char delimiter) {
2154
- // StringBuffer sb = new StringBuffer();
2155
- // for (;;) {
2156
- // char c = this.next();
2157
- // if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
2158
- // if (c != 0) {
2159
- // this.back();
2160
- // }
2161
- // return sb.toString().trim();
2162
- // }
2163
- // sb.append(c);
2164
- // }
2165
- // }
2166
- //
2167
- //
2168
- // /**
2169
- // * Get the text up but not including one of the specified delimiter
2170
- // * characters or the end of line, whichever comes first.
2171
- // * @param delimiters A set of delimiter characters.
2172
- // * @return A string, trimmed.
2173
- // */
2174
- // public String nextTo(String delimiters) {
2175
- // char c;
2176
- // StringBuffer sb = new StringBuffer();
2177
- // for (;;) {
2178
- // c = this.next();
2179
- // if (delimiters.indexOf(c) >= 0 || c == 0 ||
2180
- // c == '\n' || c == '\r') {
2181
- // if (c != 0) {
2182
- // this.back();
2183
- // }
2184
- // return sb.toString().trim();
2185
- // }
2186
- // sb.append(c);
2187
- // }
2188
- // }
2189
- //
2190
- //
2191
- // /**
2192
- // * Get the next value. The value can be a Boolean, Double, Integer,
2193
- // * JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object.
2194
- // * @throws JSONException If syntax error.
2195
- // *
2196
- // * @return An object.
2197
- // */
2198
- // public Object nextValue() {
2199
- // char c = this.nextClean();
2200
- // String string;
2201
- //
2202
- // switch (c) {
2203
- // case '"':
2204
- // case '\'':
2205
- // return this.nextString(c);
2206
- // case '{':
2207
- // this.back();
2208
- // return new JSONObject(this);
2209
- // case '[':
2210
- // this.back();
2211
- // return new JSONArray(this);
2212
- // }
2213
- //
2214
- // /*
2215
- // * Handle unquoted text. This could be the values true, false, or
2216
- // * null, or it can be a number. An implementation (such as this one)
2217
- // * is allowed to also accept non-standard forms.
2218
- // *
2219
- // * Accumulate characters until we reach the end of the text or a
2220
- // * formatting character.
2221
- // */
2222
- //
2223
- // StringBuffer sb = new StringBuffer();
2224
- // while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
2225
- // sb.append(c);
2226
- // c = this.next();
2227
- // }
2228
- // this.back();
2229
- //
2230
- // string = sb.toString().trim();
2231
- // if ("".equals(string)) {
2232
- // throw new RuntimeException("Missing value");
2233
- // }
2234
- // return JSONObject.stringToValue(string);
2235
- // }
2236
- //
2237
- //
2238
- // /**
2239
- // * Skip characters until the next character is the requested character.
2240
- // * If the requested character is not found, no characters are skipped.
2241
- // * @param to A character to skip to.
2242
- // * @return The requested character, or zero if the requested character
2243
- // * is not found.
2244
- // */
2245
- // public char skipTo(char to) {
2246
- // char c;
2247
- // try {
2248
- // long startIndex = this.index;
2249
- // long startCharacter = this.character;
2250
- // long startLine = this.line;
2251
- // this.reader.mark(1000000);
2252
- // do {
2253
- // c = this.next();
2254
- // if (c == 0) {
2255
- // this.reader.reset();
2256
- // this.index = startIndex;
2257
- // this.character = startCharacter;
2258
- // this.line = startLine;
2259
- // return c;
2260
- // }
2261
- // } while (c != to);
2262
- // } catch (IOException exc) {
2263
- // throw new RuntimeException(exc);
2264
- // }
2265
- //
2266
- // this.back();
2267
- // return c;
2268
- // }
2269
- //
2270
- //
2271
- // /**
2272
- // * Make a printable string of this JSONTokener.
2273
- // *
2274
- // * @return " at {index} [character {character} line {line}]"
2275
- // */
2276
- // @Override
2277
- // public String toString() {
2278
- // return " at " + this.index + " [character " + this.character + " line " +
2279
- // this.line + "]";
2280
- // }
2281
- // }
2282
- }