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