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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583 | package Torello.JSON;
import Torello.JavaDoc.Annotations.IntoHTMLTable;
import static Torello.JavaDoc.Annotations.IntoHTMLTable.Background.BlueDither;
import static Torello.JavaDoc.Annotations.IntoHTMLTable.Background.GreenDither;
import static Torello.JSON.JFlag.*;
import javax.json.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Objects;
import java.util.function.Function;
import static javax.json.JsonValue.ValueType.*;
/**
* Builds on the J2EE Standard Release JSON Parsing Tools by providing additional
* help with converting JSON Data into Java Object-Data.
*
* <EMBED CLASS='external-html' DATA-FILE-ID=ALL_CLASSES_NOTE>
* <EMBED CLASS='external-html' DATA-FILE-ID=READ_JSON>
* <EMBED CLASS='external-html' DATA-FILE-ID=JO_DESERIALIZE>
*
* @see Json
* @see JsonObject
* @see JsonArray
*/
@Torello.JavaDoc.Annotations.StaticFunctional
public class ReadJSON
{
// This is a static class. Has no program state.
private ReadJSON() { }
// ********************************************************************************************
// ********************************************************************************************
// Object, Class<T>
// ********************************************************************************************
// ********************************************************************************************
/**
* Retrieve a {@link JsonArray} element, and transform it to a Java {@code Object} (POJO).
* <EMBED CLASS='external-HTML' DATA-FILE-ID=READ_POJO_NOTE>
* <EMBED CLASS=defs DATA-JTYPE=JsonObject DATA-TYPE='Type Parameter T'>
*
* @param <T> <EMBED CLASS='external-html' DATA-FILE-ID=READ_TYPE_PARAM_T>
* @param ja Any instance of {@link JsonArray}
* @param index A valid index into {@code 'ja'}
* @param c <EMBED CLASS='external-html' DATA-FILE-ID=READ_C>
* @param throwOnNull <EMBED CLASS='external-html' DATA-FILE-ID=COMMON_THROW_ON_JA>
* @return <EMBED CLASS='external-html' DATA-FILE-ID=READJSON_RET_TON_JA>
*
* @throws IndexOutOfBoundsException If {@code 'index'} is out of bounds of {@code 'ja'}
* @throws NullPointerException If either {@code 'c'} or {@code 'ja'} are passed null
* @throws InvalidClassException <EMBED CLASS='external-html' DATA-FILE-ID=INV_CLASS_EX>
* @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JTAEX>
* @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JNAEX>
* @throws JsonBuildPOJOArrException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JBPAEX>
*/
public static <T> T getObject(
final JsonArray ja,
final int index,
final Class<T> c,
final boolean throwOnNull
)
{
final JsonValue jv = NPE_CHECK_JA(ja).get(index);
final Constructor<T> ctor = InvalidClassException.check(c);
switch (jv.getValueType())
{
case NULL:
// This is simple-stuff (not rocket-science). "Type Mapping" Code has to worry
// about what the meaning of "null" should be.
if (throwOnNull) throw new JsonNullArrException(ja, index, OBJECT, c);
else return null;
case OBJECT:
try
{ return ctor.newInstance((JsonObject) jv); }
catch (Exception e)
{ throw new JsonBuildPOJOArrException(e, ja, index, (JsonObject) jv, c); }
// The JsonValue at the specified array-index does not contain a JsonObject.
default: throw new JsonTypeArrException(ja, index, OBJECT, jv, c);
}
}
/**
* Extract a {@link JsonObject} property, and transform it to a Java {@code Object} (POJO).
* <EMBED CLASS='external-HTML' DATA-FILE-ID=READ_POJO_NOTE>
* <EMBED CLASS=defs DATA-TYPE='Type Parameter T' DATA-JTYPE=JsonObject>
*
* @param <T> <EMBED CLASS='external-html' DATA-FILE-ID=READ_TYPE_PARAM_T>
* @param jo Any instance of {@link JsonObject}.
* @param propertyName Any property name contained by {@code 'jo'}
* @param isOptional <EMBED CLASS='external-html' DATA-FILE-ID=COMMON_IS_OPTIONAL>
* @param throwOnNull <EMBED CLASS='external-html' DATA-FILE-ID=COMMON_THROW_ON_JO>
* @return <EMBED CLASS='external-html' DATA-FILE-ID=READJSON_RET_TON_JO>
*
* @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JPMEX>
* @throws NullPointerException If {@code 'jo', 'propertyName'} or {@code 'c'} are null
* @throws InvalidClassException <EMBED CLASS='external-html' DATA-FILE-ID=INV_CLASS_EX>
* @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JTOEX>
* @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JNOEX>
* @throws JsonBuildPOJOObjException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JBPOEX>
*/
public static <T> T getObject(
final JsonObject jo,
final String propertyName,
final Class<T> c,
final boolean isOptional,
final boolean throwOnNull
)
{
final JsonValue jv = NPE_CHECK_JO(jo, propertyName);
final Constructor<T> ctor = InvalidClassException.check(c);
if (jv == null)
{
if (isOptional) return null;
throw new JsonPropMissingException(jo, propertyName, OBJECT, c);
}
switch (jv.getValueType())
{
case NULL:
// This is simple-stuff (not rocket-science). "Type Mapping" Code has to
// worry about what the meaning of "null" should be.
if (throwOnNull) throw new JsonNullObjException(jo, propertyName, OBJECT, c);
else return null;
case OBJECT:
try
{ return ctor.newInstance(jo); }
catch (Exception e)
{
throw new JsonBuildPOJOObjException
(e, jo, propertyName, (JsonObject) jv, c);
}
// The JsonObject propertydoes not contain a JsonObject.
default: throw new JsonTypeObjException(jo, propertyName, OBJECT, jv, c);
}
}
// ********************************************************************************************
// ********************************************************************************************
// Object, Function<JsonObject, T>
// ********************************************************************************************
// ********************************************************************************************
/** <EMBED CLASS='external-html' DATA-FILE-ID=READJSON_GETOBJ_JA2> */
@IntoHTMLTable(
title=
"Reads one JsonObject from a JsonArray, and uses a Generator Function to build " +
"a POJO",
background=BlueDither
)
public static <T> T getObject(
final JsonArray ja,
final int index,
final Function<JsonObject, ? extends T> builderFunction,
final Class<T> c,
final boolean throwOnNull
)
{
final JsonValue jv = NPE_CHECK_JA(ja).get(index);
switch (jv.getValueType())
{
case NULL:
if (throwOnNull) throw new JsonNullArrException(ja, index, OBJECT, c);
else return null;
case OBJECT:
try
{ return builderFunction.apply((JsonObject) jv); }
catch (Exception e)
{ throw new JsonBuildPOJOArrException(e, ja, index, (JsonObject) jv, c); }
default: throw new JsonTypeArrException(ja, index, OBJECT, jv, c);
}
}
/** <EMBED CLASS='external-html' DATA-FILE-ID=READJSON_GETOBJ_JO2> */
@IntoHTMLTable(
title=
"Reads one JsonObject from a JsonArray, and uses a Generator Function to build " +
"a POJO",
background=GreenDither
)
public static <T> T getObject(
final JsonObject jo,
final String propertyName,
final Function<JsonObject, ? extends T> builderFunction,
final Class<T> c,
final boolean isOptional,
final boolean throwOnNull
)
{
final JsonValue jv = NPE_CHECK_JO(jo, propertyName);
if (jv == null)
{
if (isOptional) return null;
throw new JsonPropMissingException(jo, propertyName, OBJECT, c);
}
switch (jv.getValueType())
{
case NULL:
if (throwOnNull) throw new JsonNullObjException(jo, propertyName, OBJECT, c);
else return null;
case OBJECT:
try
{ return builderFunction.apply((JsonObject) jv); }
catch (Exception e)
{
throw new JsonBuildPOJOObjException
(e, jo, propertyName, (JsonObject) jv, c);
}
default: throw new JsonTypeObjException(jo, propertyName, OBJECT, jv, c);
}
}
/**
* This class contains a lot of the reason / impetus for writing {@code 'ReadJSON'}. This
* does converts a {@link JsonObject} into a Java-Object. The actual binding must be
* implemented by the programmer - because the class-type that is passed to this method
* (parameter {@code 'c'}) must have a constructor accepting this {@code JsonObject}.
*
* <EMBED CLASS='external-html' DATA-FILE-ID=READ_POJO_NOTE>
*
* @param <T> <EMBED CLASS='external-html' DATA-FILE-ID=READ_TYPE_PARAM_T>
* @param jo This may be any {@link JsonObject} which can be bound to {@code 'c'}.
* @param c <EMBED CLASS='external-html' DATA-FILE-ID=READ_C>
* @return An instance of the class {@code 'T'}, which is specified by {@code 'c'}
*
* @throws JsonException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JEX_REFL>
* @throws NullPointerException If {@code 'jo'} or {@code 'c'} are passed null
*
* @throws InvalidClassException Throws if a valid, single-argument, {@code 'JsonObject'}
* constructor is not present or is not accessible within {@code 'c'}
*/
public static <T> T getObject(JsonObject jo, Class<T> c)
{
Objects.requireNonNull(jo, "You have passed null to Class Parameter 'jo'");
Objects.requireNonNull(c, "You have passed null to Class Parameter 'c'");
final Constructor<T> ctor = InvalidClassException.check(c);
try
{ return ctor.newInstance(jo); }
catch (Exception e)
{
// *MANY* possible Exception's may be thrown, and *ALL* are checked exceptions
throw new JsonException(
"Unable to instantiate class: [" + c.getName() + "] using provided " +
"Java-Object\n" +
"See Exception.getCause() for details.",
e
);
}
}
// ********************************************************************************************
// ********************************************************************************************
// String
// ********************************************************************************************
// ********************************************************************************************
/**
* Retrieve a {@link JsonArray} element, and transform it to a {@code java.lang.String}.
* <EMBED CLASS=defs DATA-JTYPE=JsonString DATA-TYPE='java.lang.String'>
*
* @param ja Any instance of {@link JsonArray}
* @param index A valid index into {@code 'ja'}
* @param throwOnNull <EMBED CLASS='external-html' DATA-FILE-ID=COMMON_THROW_ON_JA>
* @return <EMBED CLASS='external-html' DATA-FILE-ID=READJSON_RET_TON_JA>
*
* @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'}
* @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JTAEX>
* @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JNAEX>
* @throws NullPointerException If {@code 'ja'} is passed null.
*
* @see JsonValue#getValueType()
* @see JsonValue.ValueType#STRING
*/
public static String getString(JsonArray ja, int index, boolean throwOnNull)
{
// This will also throw an IndexOutOfBoundsException if the index is out of bounds.
final JsonValue jv = NPE_CHECK_JA(ja).get(index);
switch (jv.getValueType())
{
case NULL:
// This is simple-stuff (not rocket-science). "Type Mapping" Code has to worry
// about what the meaning of "null" should be.
if (throwOnNull) throw new JsonNullArrException(ja, index, STRING, String.class);
else return null;
case STRING: return ((JsonString) jv).getString();
// The JsonValue at the specified array-index does not contain a JsonString.
default: throw new JsonTypeArrException(ja, index, STRING, jv, String.class);
}
}
/**
* Extract a {@link JsonObject} property, and transform it to a {@code java.lang.String}.
* <EMBED CLASS=defs DATA-TYPE='java.lang.String' DATA-JTYPE=JsonString>
*
* @param jo Any instance of {@link JsonObject}.
* @param propertyName Any property name contained by {@code 'jo'}
* @param isOptional <EMBED CLASS='external-html' DATA-FILE-ID=COMMON_IS_OPTIONAL>
* @param throwOnNull <EMBED CLASS='external-html' DATA-FILE-ID=COMMON_THROW_ON_JO>
* @return <EMBED CLASS='external-html' DATA-FILE-ID=READJSON_RET_TON_JO>
*
* @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JPMEX>
* @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JTOEX>
* @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JNOEX>
* @throws NullPointerException if either {@code 'jo'} or {@code 'propertyName'} are null
*
* @see JsonValue#getValueType()
* @see JsonValue.ValueType#STRING
*/
public static String getString
(JsonObject jo, String propertyName, boolean isOptional, boolean throwOnNull)
{
final JsonValue jv = NPE_CHECK_JO(jo, propertyName);
if (jv == null)
{
if (isOptional) return null;
throw new JsonPropMissingException(jo, propertyName, STRING, String.class);
}
switch (jv.getValueType())
{
case NULL:
// This is simple-stuff (not rocket-science). "Type Mapping" Code has to worry
// about what the meaning of "null" should be.
if (throwOnNull) throw new JsonNullObjException
(jo, propertyName, STRING, String.class);
else return null;
case STRING: return ((JsonString) jv).getString();
// The JsonObject propertydoes not contain a JsonString.
default: throw new JsonTypeObjException(jo, propertyName, STRING, jv, String.class);
}
}
// ********************************************************************************************
// ********************************************************************************************
// JsonObject
// ********************************************************************************************
// ********************************************************************************************
/**
* Extract an instance of {@link JsonObject} from an instance of {@link JsonArray}.
*
* <EMBED CLASS=defs DATA-JTYPE=JsonObject DATA-TYPE=JsonObject>
* @param ja Any instance of {@link JsonArray}
* @param index A valid index into {@code 'ja'}
* @param throwOnNull <EMBED CLASS='external-html' DATA-FILE-ID=COMMON_THROW_ON_JA>
*
* @return The requested {@link JsonObject}, or null (only if null's have been permitted).
* @throws IndexOutOfBoundsException If {@code 'index'} is out of bounds of {@code 'ja'}
* @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JTAEX>
* @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JNAEX>
* @throws NullPointerException If {@code 'ja'} is passed null
*
* @see JsonValue#getValueType()
* @see JsonValue.ValueType#OBJECT
*/
public static JsonObject getJsonObject(JsonArray ja, int index, boolean throwOnNull)
{
// This will also throw an IndexOutOfBoundsException if the index is out of bounds.
final JsonValue jv = NPE_CHECK_JA(ja).get(index);
switch (jv.getValueType())
{
case NULL:
if (throwOnNull)
throw new JsonNullArrException(ja, index, OBJECT, JsonObject.class);
else
return null;
case OBJECT: return (JsonObject) jv;
// The JsonValue at the specified array-index does not contain a JsonObject.
default: throw new JsonTypeArrException(ja, index, OBJECT, jv, JsonObject.class);
}
}
/**
* Extract an instance of {@link JsonObject} from an instance of {@link JsonObject}.
*
* <EMBED CLASS=defs DATA-JTYPE=JsonObject DATA-TYPE=JsonObject>
* @param jo Any instance of {@link JsonObject}.
* @param propertyName Name of the JSON property that should be contained within {@code 'jo'}
* @param isOptional <EMBED CLASS='external-html' DATA-FILE-ID=COMMON_IS_OPTIONAL>
* @param throwOnNull <EMBED CLASS='external-html' DATA-FILE-ID=COMMON_THROW_ON_JO>
*
* @return The requested {@link JsonObject}, or null (only if null's have been permitted).
* @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JPMEX>
* @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JTOEX>
* @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JNOEX>
* @throws NullPointerException if either {@code 'jo'} or {@code 'propertyName'} are null
*
* @see JsonValue#getValueType()
* @see JsonValue.ValueType#OBJECT
*/
public static JsonObject getJsonObject
(JsonObject jo, String propertyName, boolean isOptional, boolean throwOnNull)
{
final JsonValue jv = NPE_CHECK_JO(jo, propertyName);
if (jv == null)
{
if (isOptional) return null;
throw new JsonPropMissingException(jo, propertyName, OBJECT, JsonObject.class);
}
switch (jv.getValueType())
{
case NULL:
if (throwOnNull)
throw new JsonNullObjException(jo, propertyName, OBJECT, JsonObject.class);
else
return null;
case OBJECT: return (JsonObject) jv;
// The JsonValue at the specified property does not contain a JsonObject.
default:
throw new JsonTypeObjException(jo, propertyName, OBJECT, jv, JsonObject.class);
}
}
// ********************************************************************************************
// ********************************************************************************************
// JsonArray
// ********************************************************************************************
// ********************************************************************************************
/**
* Extract an instance of {@link JsonArray} from an instance of {@link JsonArray}.
*
* <EMBED CLASS=defs DATA-JTYPE=JsonArray DATA-TYPE=JsonArray>
* @param ja Any instance of {@link JsonArray}
* @param index A valid index into {@code 'ja'}
* @param throwOnNull <EMBED CLASS='external-html' DATA-FILE-ID=COMMON_THROW_ON_JA>
*
* @return The requested {@link JsonArray}, or null (only if null's have been permitted).
* @throws IndexOutOfBoundsException If {@code 'index'} is out of bounds of {@code 'ja'}
* @throws JsonTypeArrException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JTAEX>
* @throws JsonNullArrException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JNAEX>
* @throws NullPointerException if {@code 'ja'} is passed null
*
* @see JsonValue#getValueType()
* @see JsonValue.ValueType#ARRAY
*/
public static JsonArray getJsonArray(JsonArray ja, int index, boolean throwOnNull)
{
// This will also throw an IndexOutOfBoundsException if the index is out of bounds.
final JsonValue jv = NPE_CHECK_JA(ja).get(index);
switch (jv.getValueType())
{
case NULL:
if (throwOnNull)
throw new JsonNullArrException(ja, index, ARRAY, JsonArray.class);
else
return null;
case ARRAY: return (JsonArray) jv;
// The JsonValue at the specified property does not contain a JsonArray.
default: throw new JsonTypeArrException(ja, index, ARRAY, jv, JsonArray.class);
}
}
/**
* Extract an instance of {@link JsonArray} from an instance of {@link JsonObject}.
*
* <EMBED CLASS=defs DATA-JTYPE=JsonArray DATA-TYPE=JsonArray>
* @param jo Any instance of {@link JsonObject}.
* @param propertyName Name of the JSON property that should be contained within {@code 'jo'}
* @param isOptional <EMBED CLASS='external-html' DATA-FILE-ID=COMMON_IS_OPTIONAL>
* @param throwOnNull <EMBED CLASS='external-html' DATA-FILE-ID=COMMON_THROW_ON_JO>
*
* @return The requested {@link JsonArray}, or null (only if null's have been permitted).
* @throws JsonPropMissingException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JPMEX>
* @throws JsonTypeObjException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JTOEX>
* @throws JsonNullObjException <EMBED CLASS='external-html' DATA-FILE-ID=READ_JNOEX>
* @throws NullPointerException if either {@code 'jo'} or {@code 'propertyName'} are null
*
* @see JsonValue#getValueType()
* @see JsonValue.ValueType#ARRAY
*/
public static JsonArray getJsonArray
(JsonObject jo, String propertyName, boolean isOptional, boolean throwOnNull)
{
final JsonValue jv = NPE_CHECK_JO(jo, propertyName);
if (jv == null)
{
if (isOptional) return null;
throw new JsonPropMissingException(jo, propertyName, ARRAY, JsonArray.class);
}
switch (jv.getValueType())
{
case NULL:
if (throwOnNull)
throw new JsonNullObjException(jo, propertyName, ARRAY, JsonArray.class);
else
return null;
case ARRAY: return (JsonArray) jv;
// The JsonValue at the specified property does not contain a JsonArray.
default:
throw new JsonTypeObjException(jo, propertyName, ARRAY, jv, JsonArray.class);
}
}
// ********************************************************************************************
// ********************************************************************************************
// Internal Methods
// ********************************************************************************************
// ********************************************************************************************
private static JsonArray NPE_CHECK_JA(JsonArray ja)
{
Objects.requireNonNull(ja, "You have passed null to JsonArray Parameter 'ja'");
return ja;
}
private static JsonValue NPE_CHECK_JO(JsonObject jo, String property)
{
Objects.requireNonNull(jo, "You have passed null to JsonObject Parameter 'jo'");
Objects.requireNonNull(property, "You have passed null to String Parameter 'property'");
return jo.get(property);
}
private static JsonObject NPE_CHECK_JO(JsonObject jo)
{
Objects.requireNonNull(jo, "You have passed null to JsonObject Parameter 'jo'");
return jo;
}
}
|