forked from stackimpact/stackimpact-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonValue.java
More file actions
executable file
·500 lines (461 loc) · 16.6 KB
/
JsonValue.java
File metadata and controls
executable file
·500 lines (461 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*******************************************************************************
* Copyright (c) 2013, 2015 EclipseSource.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
******************************************************************************/
package com.eclipsesource.json;
import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.io.StringWriter;
import java.io.Writer;
/**
* Represents a JSON value. This can be a JSON <strong>object</strong>, an <strong> array</strong>,
* a <strong>number</strong>, a <strong>string</strong>, or one of the literals
* <strong>true</strong>, <strong>false</strong>, and <strong>null</strong>.
* <p>
* The literals <strong>true</strong>, <strong>false</strong>, and <strong>null</strong> are
* represented by the constants {@link #TRUE}, {@link #FALSE}, and {@link #NULL}.
* </p>
* <p>
* JSON <strong>objects</strong> and <strong>arrays</strong> are represented by the subtypes
* {@link JsonObject} and {@link JsonArray}. Instances of these types can be created using the
* public constructors of these classes.
* </p>
* <p>
* Instances that represent JSON <strong>numbers</strong>, <strong>strings</strong> and
* <strong>boolean</strong> values can be created using the static factory methods
* {@link #valueOf(String)}, {@link #valueOf(long)}, {@link #valueOf(double)}, etc.
* </p>
* <p>
* In order to find out whether an instance of this class is of a certain type, the methods
* {@link #isObject()}, {@link #isArray()}, {@link #isString()}, {@link #isNumber()} etc. can be
* used.
* </p>
* <p>
* If the type of a JSON value is known, the methods {@link #asObject()}, {@link #asArray()},
* {@link #asString()}, {@link #asInt()}, etc. can be used to get this value directly in the
* appropriate target type.
* </p>
* <p>
* This class is <strong>not supposed to be extended</strong> by clients.
* </p>
*/
@SuppressWarnings("serial") // use default serial UID
public abstract class JsonValue implements Serializable {
/**
* Represents the JSON literal <code>true</code>.
* @deprecated Use <code>Json.TRUE</code> instead
*/
@Deprecated
public static final JsonValue TRUE = Json.TRUE;
/**
* Represents the JSON literal <code>false</code>.
* @deprecated Use <code>Json.FALSE</code> instead
*/
@Deprecated
public static final JsonValue FALSE = Json.FALSE;
/**
* Represents the JSON literal <code>null</code>.
* @deprecated Use <code>Json.NULL</code> instead
*/
@Deprecated
public static final JsonValue NULL = Json.NULL;
JsonValue() {
// prevent subclasses outside of this package
}
/**
* Reads a JSON value from the given reader.
* <p>
* Characters are read in chunks and buffered internally, therefore wrapping an existing reader in
* an additional <code>BufferedReader</code> does <strong>not</strong> improve reading
* performance.
* </p>
*
* @param reader
* the reader to read the JSON value from
* @return the JSON value that has been read
* @throws IOException
* if an I/O error occurs in the reader
* @throws ParseException
* if the input is not valid JSON
* @deprecated Use {@link Json#parse(Reader)} instead
*/
@Deprecated
public static JsonValue readFrom(Reader reader) throws IOException {
return new JsonParser(reader).parse();
}
/**
* Reads a JSON value from the given string.
*
* @param text
* the string that contains the JSON value
* @return the JSON value that has been read
* @throws ParseException
* if the input is not valid JSON
* @deprecated Use {@link Json#parse(String)} instead
*/
@Deprecated
public static JsonValue readFrom(String text) {
try {
return new JsonParser(text).parse();
} catch (IOException exception) {
// JsonParser does not throw IOException for String
throw new RuntimeException(exception);
}
}
/**
* Returns a JsonValue instance that represents the given <code>int</code> value.
*
* @param value
* the value to get a JSON representation for
* @return a JSON value that represents the given value
* @deprecated Use <code>Json.value()</code> instead
*/
@Deprecated
public static JsonValue valueOf(int value) {
return Json.value(value);
}
/**
* Returns a JsonValue instance that represents the given <code>long</code> value.
*
* @param value
* the value to get a JSON representation for
* @return a JSON value that represents the given value
* @deprecated Use <code>Json.value()</code> instead
*/
@Deprecated
public static JsonValue valueOf(long value) {
return Json.value(value);
}
/**
* Returns a JsonValue instance that represents the given <code>float</code> value.
*
* @param value
* the value to get a JSON representation for
* @return a JSON value that represents the given value
* @deprecated Use <code>Json.value()</code> instead
*/
@Deprecated
public static JsonValue valueOf(float value) {
return Json.value(value);
}
/**
* Returns a JsonValue instance that represents the given <code>double</code> value.
*
* @param value
* the value to get a JSON representation for
* @return a JSON value that represents the given value
* @deprecated Use <code>Json.value()</code> instead
*/
@Deprecated
public static JsonValue valueOf(double value) {
return Json.value(value);
}
/**
* Returns a JsonValue instance that represents the given string.
*
* @param string
* the string to get a JSON representation for
* @return a JSON value that represents the given string
* @deprecated Use <code>Json.value()</code> instead
*/
@Deprecated
public static JsonValue valueOf(String string) {
return Json.value(string);
}
/**
* Returns a JsonValue instance that represents the given <code>boolean</code> value.
*
* @param value
* the value to get a JSON representation for
* @return a JSON value that represents the given value
* @deprecated Use <code>Json.value()</code> instead
*/
@Deprecated
public static JsonValue valueOf(boolean value) {
return Json.value(value);
}
/**
* Detects whether this value represents a JSON object. If this is the case, this value is an
* instance of {@link JsonObject}.
*
* @return <code>true</code> if this value is an instance of JsonObject
*/
public boolean isObject() {
return false;
}
/**
* Detects whether this value represents a JSON array. If this is the case, this value is an
* instance of {@link JsonArray}.
*
* @return <code>true</code> if this value is an instance of JsonArray
*/
public boolean isArray() {
return false;
}
/**
* Detects whether this value represents a JSON number.
*
* @return <code>true</code> if this value represents a JSON number
*/
public boolean isNumber() {
return false;
}
/**
* Detects whether this value represents a JSON string.
*
* @return <code>true</code> if this value represents a JSON string
*/
public boolean isString() {
return false;
}
/**
* Detects whether this value represents a boolean value.
*
* @return <code>true</code> if this value represents either the JSON literal <code>true</code> or
* <code>false</code>
*/
public boolean isBoolean() {
return false;
}
/**
* Detects whether this value represents the JSON literal <code>true</code>.
*
* @return <code>true</code> if this value represents the JSON literal <code>true</code>
*/
public boolean isTrue() {
return false;
}
/**
* Detects whether this value represents the JSON literal <code>false</code>.
*
* @return <code>true</code> if this value represents the JSON literal <code>false</code>
*/
public boolean isFalse() {
return false;
}
/**
* Detects whether this value represents the JSON literal <code>null</code>.
*
* @return <code>true</code> if this value represents the JSON literal <code>null</code>
*/
public boolean isNull() {
return false;
}
/**
* Returns this JSON value as {@link JsonObject}, assuming that this value represents a JSON
* object. If this is not the case, an exception is thrown.
*
* @return a JSONObject for this value
* @throws UnsupportedOperationException
* if this value is not a JSON object
*/
public JsonObject asObject() {
throw new UnsupportedOperationException("Not an object: " + toString());
}
/**
* Returns this JSON value as {@link JsonArray}, assuming that this value represents a JSON array.
* If this is not the case, an exception is thrown.
*
* @return a JSONArray for this value
* @throws UnsupportedOperationException
* if this value is not a JSON array
*/
public JsonArray asArray() {
throw new UnsupportedOperationException("Not an array: " + toString());
}
/**
* Returns this JSON value as an <code>int</code> value, assuming that this value represents a
* JSON number that can be interpreted as Java <code>int</code>. If this is not the case, an
* exception is thrown.
* <p>
* To be interpreted as Java <code>int</code>, the JSON number must neither contain an exponent
* nor a fraction part. Moreover, the number must be in the <code>Integer</code> range.
* </p>
*
* @return this value as <code>int</code>
* @throws UnsupportedOperationException
* if this value is not a JSON number
* @throws NumberFormatException
* if this JSON number can not be interpreted as <code>int</code> value
*/
public int asInt() {
throw new UnsupportedOperationException("Not a number: " + toString());
}
/**
* Returns this JSON value as a <code>long</code> value, assuming that this value represents a
* JSON number that can be interpreted as Java <code>long</code>. If this is not the case, an
* exception is thrown.
* <p>
* To be interpreted as Java <code>long</code>, the JSON number must neither contain an exponent
* nor a fraction part. Moreover, the number must be in the <code>Long</code> range.
* </p>
*
* @return this value as <code>long</code>
* @throws UnsupportedOperationException
* if this value is not a JSON number
* @throws NumberFormatException
* if this JSON number can not be interpreted as <code>long</code> value
*/
public long asLong() {
throw new UnsupportedOperationException("Not a number: " + toString());
}
/**
* Returns this JSON value as a <code>float</code> value, assuming that this value represents a
* JSON number. If this is not the case, an exception is thrown.
* <p>
* If the JSON number is out of the <code>Float</code> range, {@link Float#POSITIVE_INFINITY} or
* {@link Float#NEGATIVE_INFINITY} is returned.
* </p>
*
* @return this value as <code>float</code>
* @throws UnsupportedOperationException
* if this value is not a JSON number
*/
public float asFloat() {
throw new UnsupportedOperationException("Not a number: " + toString());
}
/**
* Returns this JSON value as a <code>double</code> value, assuming that this value represents a
* JSON number. If this is not the case, an exception is thrown.
* <p>
* If the JSON number is out of the <code>Double</code> range, {@link Double#POSITIVE_INFINITY} or
* {@link Double#NEGATIVE_INFINITY} is returned.
* </p>
*
* @return this value as <code>double</code>
* @throws UnsupportedOperationException
* if this value is not a JSON number
*/
public double asDouble() {
throw new UnsupportedOperationException("Not a number: " + toString());
}
/**
* Returns this JSON value as String, assuming that this value represents a JSON string. If this
* is not the case, an exception is thrown.
*
* @return the string represented by this value
* @throws UnsupportedOperationException
* if this value is not a JSON string
*/
public String asString() {
throw new UnsupportedOperationException("Not a string: " + toString());
}
/**
* Returns this JSON value as a <code>boolean</code> value, assuming that this value is either
* <code>true</code> or <code>false</code>. If this is not the case, an exception is thrown.
*
* @return this value as <code>boolean</code>
* @throws UnsupportedOperationException
* if this value is neither <code>true</code> or <code>false</code>
*/
public boolean asBoolean() {
throw new UnsupportedOperationException("Not a boolean: " + toString());
}
/**
* Writes the JSON representation of this value to the given writer in its minimal form, without
* any additional whitespace.
* <p>
* Writing performance can be improved by using a {@link java.io.BufferedWriter BufferedWriter}.
* </p>
*
* @param writer
* the writer to write this value to
* @throws IOException
* if an I/O error occurs in the writer
*/
public void writeTo(Writer writer) throws IOException {
writeTo(writer, WriterConfig.MINIMAL);
}
/**
* Writes the JSON representation of this value to the given writer using the given formatting.
* <p>
* Writing performance can be improved by using a {@link java.io.BufferedWriter BufferedWriter}.
* </p>
*
* @param writer
* the writer to write this value to
* @param config
* a configuration that controls the formatting or <code>null</code> for the minimal form
* @throws IOException
* if an I/O error occurs in the writer
*/
public void writeTo(Writer writer, WriterConfig config) throws IOException {
if (writer == null) {
throw new NullPointerException("writer is null");
}
if (config == null) {
throw new NullPointerException("config is null");
}
WritingBuffer buffer = new WritingBuffer(writer, 128);
write(config.createWriter(buffer));
buffer.flush();
}
/**
* Returns the JSON string for this value in its minimal form, without any additional whitespace.
* The result is guaranteed to be a valid input for the method {@link #readFrom(String)} and to
* create a value that is <em>equal</em> to this object.
*
* @return a JSON string that represents this value
*/
@Override
public String toString() {
return toString(WriterConfig.MINIMAL);
}
/**
* Returns the JSON string for this value using the given formatting.
*
* @param config
* a configuration that controls the formatting or <code>null</code> for the minimal form
* @return a JSON string that represents this value
*/
public String toString(WriterConfig config) {
StringWriter writer = new StringWriter();
try {
writeTo(writer, config);
} catch (IOException exception) {
// StringWriter does not throw IOExceptions
throw new RuntimeException(exception);
}
return writer.toString();
}
/**
* Indicates whether some other object is "equal to" this one according to the contract specified
* in {@link Object#equals(Object)}.
* <p>
* Two JsonValues are considered equal if and only if they represent the same JSON text. As a
* consequence, two given JsonObjects may be different even though they contain the same set of
* names with the same values, but in a different order.
* </p>
*
* @param object
* the reference object with which to compare
* @return true if this object is the same as the object argument; false otherwise
*/
@Override
public boolean equals(Object object) {
return super.equals(object);
}
@Override
public int hashCode() {
return super.hashCode();
}
abstract void write(JsonWriter writer) throws IOException;
}