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 | package Torello.JSON;
import Torello.JavaDoc.Annotations.IntoHTMLTable;
import static Torello.JavaDoc.Annotations.IntoHTMLTable.Background.GreenDither;
import static Torello.JavaDoc.Annotations.IntoHTMLTable.Background.BlueDither;
import java.util.function.Predicate;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.function.ToDoubleFunction;
import Torello.Java.Function.ToByteFunction;
import Torello.Java.Function.ToShortFunction;
import Torello.Java.Function.ToFloatFunction;
import javax.json.JsonObject;
import javax.json.JsonArray;
import javax.json.JsonString;
import javax.json.JsonValue;
import static javax.json.JsonValue.ValueType.STRING;
import static javax.json.JsonValue.ValueType.NULL;
/**
* Builds on the J2EE Standard Release JSON Parsing Tools by providing additional
* help with converting JSON Data into <B STYLE='color: red'>Java Primitive-Types</B>
*
* <EMBED CLASS='external-html' DATA-FILE-ID=ALL_CLASSES_NOTE>
* <EMBED CLASS='external-html' DATA-FILE-ID=PARSE_PRIM_JSON>
* <EMBED CLASS='external-html' DATA-FILE-ID=PARSE_PRIM_PTABLE>
* <EMBED CLASS='external-html' DATA-CH=char DATA-FILE-ID=JAVA_LANG_CHAR>
*
* @see JsonObject
* @see JsonArray
*/
@Torello.JavaDoc.Annotations.StaticFunctional
public class ParsePrimJSON
{
// This is a static class. Has no program state.
private ParsePrimJSON() { }
// ********************************************************************************************
// ********************************************************************************************
// int
// ********************************************************************************************
// ********************************************************************************************
/** <EMBED CLASS='external-html' DATA-JTYPE=int DATA-FILE-ID=PARSE_PRIM_JA> */
@IntoHTMLTable(
title="Extract a JsonString from a JsonArray, and Parse into a Java 'int' Primitive",
background=BlueDither
)
public static int parseInt(
final JsonArray ja,
final int index,
final ToIntFunction<String> optionalUserParser
)
{
final JsonString js = GET(ja, index, int.class);
try
{
return (optionalUserParser != null)
? optionalUserParser.applyAsInt(js.getString())
: Integer.parseInt(js.getString().trim());
}
catch (Exception e)
{ throw new JsonStrParseArrException(e, ja, index, js, int.class); }
}
/** <EMBED CLASS='external-html' DATA-JTYPE=long DATA-FILE-ID=PARSE_PRIM_JA> */
@IntoHTMLTable(
title="Extract a JsonString from a JsonArray, and Parse into a Java 'long' Primitive",
background=GreenDither
)
public static long parseLong(
final JsonArray ja,
final int index,
final ToLongFunction<String> optionalUserParser
)
{
final JsonString js = GET(ja, index, long.class);
try
{
return (optionalUserParser != null)
? optionalUserParser.applyAsLong(js.getString())
: Long.parseLong(js.getString().trim());
}
catch (Exception e)
{ throw new JsonStrParseArrException(e, ja, index, js, long.class); }
}
/** <EMBED CLASS='external-html' DATA-JTYPE=short DATA-FILE-ID=PARSE_PRIM_JA> */
@IntoHTMLTable(
title="Extract a JsonString from a JsonArray, and Parse into a Java 'short' Primitive",
background=BlueDither
)
public static short parseShort(
final JsonArray ja,
final int index,
final ToShortFunction<String> optionalUserParser
)
{
final JsonString js = GET(ja, index, short.class);
try
{
return (optionalUserParser != null)
? optionalUserParser.applyAsShort(js.getString())
: Short.parseShort(js.getString().trim());
}
catch (Exception e)
{ throw new JsonStrParseArrException(e, ja, index, js, short.class); }
}
/** <EMBED CLASS='external-html' DATA-JTYPE=byte DATA-FILE-ID=PARSE_PRIM_JA> */
@IntoHTMLTable(
title="Extract a JsonString from a JsonArray, and Parse into a Java 'byte' Primitive",
background=GreenDither
)
public static byte parseByte(
final JsonArray ja,
final int index,
final ToByteFunction<String> optionalUserParser
)
{
final JsonString js = GET(ja, index, byte.class);
try
{
return (optionalUserParser != null)
? optionalUserParser.applyAsByte(js.getString())
: Byte.parseByte(js.getString().trim());
}
catch (Exception e)
{ throw new JsonStrParseArrException(e, ja, index, js, byte.class); }
}
/** <EMBED CLASS='external-html' DATA-JTYPE=double DATA-FILE-ID=PARSE_PRIM_JA> */
@IntoHTMLTable(
title="Extract a JsonString from a JsonArray, and Parse into a Java 'double' Primitive",
background=BlueDither
)
public static double parseDouble(
final JsonArray ja,
final int index,
final ToDoubleFunction<String> optionalUserParser
)
{
final JsonString js = GET(ja, index, double.class);
try
{
return (optionalUserParser != null)
? optionalUserParser.applyAsDouble(js.getString())
: Double.parseDouble(js.getString().trim());
}
catch (Exception e)
{ throw new JsonStrParseArrException(e, ja, index, js, double.class); }
}
/** <EMBED CLASS='external-html' DATA-JTYPE=float DATA-FILE-ID=PARSE_PRIM_JA> */
@IntoHTMLTable(
title="Extract a JsonString from a JsonArray, and Parse into a Java 'float' Primitive",
background=GreenDither
)
public static float parseFloat(
final JsonArray ja,
final int index,
final ToFloatFunction<String> optionalUserParser
)
{
final JsonString js = GET(ja, index, float.class);
try
{
return (optionalUserParser != null)
? optionalUserParser.applyAsFloat(js.getString())
: Float.parseFloat(js.getString().trim());
}
catch (Exception e)
{ throw new JsonStrParseArrException(e, ja, index, js, float.class); }
}
/** <EMBED CLASS='external-html' DATA-JTYPE=boolean DATA-FILE-ID=PARSE_PRIM_JA> */
@IntoHTMLTable(
title="Extract a JsonString from a JsonArray, and Parse into a Java 'boolean' Primitive",
background=BlueDither
)
public static boolean parseBoolean(
final JsonArray ja,
final int index,
final Predicate<String> optionalUserParser
)
{
final JsonString js = GET(ja, index, boolean.class);
try
{
return (optionalUserParser != null)
? optionalUserParser.test(js.getString())
: Boolean.parseBoolean(js.getString().trim());
}
catch (Exception e)
{ throw new JsonStrParseArrException(e, ja, index, js, boolean.class); }
}
// ********************************************************************************************
// ********************************************************************************************
// The Internal Methods
// ********************************************************************************************
// ********************************************************************************************
/**
* This is an internal helper method for retrieving an element from a {@link JsonArray},
* and converting it to one of the standard <B STYLE='color: red;'>Java Types</B>.
*
* @param ja Any instance of {@link JsonArray}
* @param index Any index into the array which holds a {@link JsonString}
* @param primitiveClass Expected Primitive Class, to be returned by caller method
*
* @return The extracted {@link JsonString}.
*
* @throws JsonNullPrimitiveArrException If the array element at {@code 'index'} contains
* <B STYLE='color: red'>Json-Null</B>
* @throws JsonTypeArrException If the array element at {@code 'index'} doesn't contain
* {@link JsonString}
* @throws IndexOutOfBoundsException If {@code 'index'} is out of the bounds of {@code 'ja'}
*
* @see #parseInt(JsonArray, int, ToIntFunction)
* @see #parseLong(JsonArray, int, ToLongFunction)
* @see #parseShort(JsonArray, int, ToShortFunction)
* @see #parseByte(JsonArray, int, ToByteFunction)
* @see #parseDouble(JsonArray, int, ToDoubleFunction)
* @see #parseFloat(JsonArray, int, ToFloatFunction)
*/
protected static <T> JsonString GET(
final JsonArray ja,
final int index,
final Class<T> primitiveClass
)
{
// This will throw an IndexOutOfBoundsException if the index is out of bounds.
final JsonValue jv = ja.get(index);
switch (jv.getValueType())
{
// This particular Array-Location (the one specified by 'index') contained an actual
// 'null' in its place. Primitives cannot be assinged null, but fortunately there is
// a JsonException descendant class that handles this exact situation.
case NULL: throw new JsonNullPrimitiveArrException
(ja, index, STRING, primitiveClass);
case STRING: return (JsonString) jv;
// The JsonValue at the specified array-index does not contain a JsonString.
default: throw new JsonTypeArrException
(ja, index, STRING, jv, primitiveClass);
}
}
// ********************************************************************************************
// ********************************************************************************************
// int
// ********************************************************************************************
// ********************************************************************************************
/** <EMBED CLASS='external-html' DATA-JTYPE=int DATA-FILE-ID=PARSE_PRIM_JO> */
@IntoHTMLTable(
title="Extract a JsonString from a JsonObject, and Parse into a Java 'int' Primitive",
background=BlueDither
)
public static int parseInt(
final JsonObject jo,
final String propertyName,
final ToIntFunction<String> optionalUserParser
)
{
final JsonString js = GET(jo, propertyName, int.class);
try
{
return (optionalUserParser != null)
? optionalUserParser.applyAsInt(js.getString())
: Integer.parseInt(js.getString().trim());
}
catch (Exception e)
{ throw new JsonStrParseObjException(e, jo, propertyName, js, int.class); }
}
/** <EMBED CLASS='external-html' DATA-JTYPE=long DATA-FILE-ID=PARSE_PRIM_JO> */
@IntoHTMLTable(
title="Extract a JsonString from a JsonObject, and Parse into a Java 'long' Primitive",
background=GreenDither
)
public static long parseLong(
final JsonObject jo,
final String propertyName,
final ToLongFunction<String> optionalUserParser
)
{
final JsonString js = GET(jo, propertyName, long.class);
try
{
return (optionalUserParser != null)
? optionalUserParser.applyAsLong(js.getString())
: Long.parseLong(js.getString().trim());
}
catch (Exception e)
{ throw new JsonStrParseObjException(e, jo, propertyName, js, long.class); }
}
/** <EMBED CLASS='external-html' DATA-JTYPE=short DATA-FILE-ID=PARSE_PRIM_JO> */
@IntoHTMLTable(
title="Extract a JsonString from a JsonObject, and Parse into a Java 'short' Primitive",
background=BlueDither
)
public static short parseShort(
final JsonObject jo,
final String propertyName,
final ToShortFunction<String> optionalUserParser
)
{
final JsonString js = GET(jo, propertyName, short.class);
try
{
return (optionalUserParser != null)
? optionalUserParser.applyAsShort(js.getString())
: Short.parseShort(js.getString().trim());
}
catch (Exception e)
{ throw new JsonStrParseObjException(e, jo, propertyName, js, short.class); }
}
/** <EMBED CLASS='external-html' DATA-JTYPE=byte DATA-FILE-ID=PARSE_PRIM_JO> */
@IntoHTMLTable(
title="Extract a JsonString from a JsonObject, and Parse into a Java 'byte' Primitive",
background=GreenDither
)
public static byte parseByte(
final JsonObject jo,
final String propertyName,
final ToByteFunction<String> optionalUserParser
)
{
final JsonString js = GET(jo, propertyName, byte.class);
try
{
return (optionalUserParser != null)
? optionalUserParser.applyAsByte(js.getString())
: Byte.parseByte(js.getString().trim());
}
catch (Exception e)
{ throw new JsonStrParseObjException(e, jo, propertyName, js, byte.class); }
}
/** <EMBED CLASS='external-html' DATA-JTYPE=double DATA-FILE-ID=PARSE_PRIM_JO> */
@IntoHTMLTable(
title="Extract a JsonString from a JsonObject, and Parse into a Java 'double' Primitive",
background=BlueDither
)
public static double parseDouble(
final JsonObject jo,
final String propertyName,
final ToDoubleFunction<String> optionalUserParser
)
{
final JsonString js = GET(jo, propertyName, double.class);
try
{
return (optionalUserParser != null)
? optionalUserParser.applyAsDouble(js.getString())
: Double.parseDouble(js.getString().trim());
}
catch (Exception e)
{ throw new JsonStrParseObjException(e, jo, propertyName, js, double.class); }
}
/** <EMBED CLASS='external-html' DATA-JTYPE=float DATA-FILE-ID=PARSE_PRIM_JO> */
@IntoHTMLTable(
title="Extract a JsonString from a JsonObject, and Parse into a Java 'float' Primitive",
background=GreenDither
)
public static float parseFloat(
final JsonObject jo,
final String propertyName,
final ToFloatFunction<String> optionalUserParser
)
{
final JsonString js = GET(jo, propertyName, float.class);
try
{
return (optionalUserParser != null)
? optionalUserParser.applyAsFloat(js.getString())
: Float.parseFloat(js.getString().trim());
}
catch (Exception e)
{ throw new JsonStrParseObjException(e, jo, propertyName, js, float.class); }
}
/** <EMBED CLASS='external-html' DATA-JTYPE=boolean DATA-FILE-ID=PARSE_PRIM_JO> */
@IntoHTMLTable(
title="Extract a JsonString from a JsonObject, and Parse into a Java 'boolean' Primitive",
background=BlueDither
)
public static boolean parseBoolean(
final JsonObject jo,
final String propertyName,
final Predicate<String> optionalUserParser
)
{
final JsonString js = GET(jo, propertyName, boolean.class);
try
{
return (optionalUserParser != null)
? optionalUserParser.test(js.getString())
: Boolean.parseBoolean(js.getString().trim());
}
catch (Exception e)
{ throw new JsonStrParseObjException(e, jo, propertyName, js, boolean.class); }
}
// ********************************************************************************************
// ********************************************************************************************
// The Internal Methods
// ********************************************************************************************
// ********************************************************************************************
/**
* This is an internal helper method for retrieving a property from a {@link JsonObject},
* and converting it to one of the standard <B STYLE='color: red;'>Java Types</B>.
*
* @param jo Any instance of {@link JsonObject}
* @param propertyName Any property name contained by {@code 'jo'}, having a {@link JsonString}
* @param primitiveClass Expected Primitive Class, to be returned by caller method
*
* @return The extracted {@link JsonString}.
*
* @throws JsonNullPrimitiveObjException If the specified property contains
* <B STYLE='color: red'>Json-Null</B>
* @throws JsonTypeObjException If the specified property doesn't contain
* {@link JsonString}
* @throws JsonPropMissingException If the property is missing.
*
* @see #parseInt(JsonObject, String, ToIntFunction)
* @see #parseLong(JsonObject, String, ToLongFunction)
* @see #parseShort(JsonObject, String, ToShortFunction)
* @see #parseByte(JsonObject, String, ToByteFunction)
* @see #parseDouble(JsonObject, String, ToDoubleFunction)
* @see #parseFloat(JsonObject, String, ToFloatFunction)
*/
protected static <T> JsonString GET(
final JsonObject jo,
final String propertyName,
final Class<T> primitiveClass
)
{
// Here, a 'get' request was made for a property that isn't actually listed among the
// properties in the provided JsonObject. Since this internal 'GET' is used by methods
// that are trying to return a Java Primitive (like 'int' or 'float'), then an exception
// has to be thrown. The option of returning 'null' isn't possible here!
if (! jo.containsKey(propertyName)) throw new JsonPropMissingException
(jo, propertyName, STRING, primitiveClass);
final JsonValue jv = jo.get(propertyName);
switch (jv.getValueType())
{
// A Json-"null" was listed as the "value" assigned to this property
// Primitive's cannot encode "null." Fortunately, there is a specialized Java-HTML
// JsonException sub-class that handles exactly this type of situation.
//
// "someJsonObjectPropert": null,
case NULL: throw new JsonNullPrimitiveObjException
(jo, propertyName, STRING, primitiveClass);
case STRING: return (JsonString) jv;
// The JsonObject property does not contain a JsonString.
default: throw new JsonTypeObjException
(jo, propertyName, STRING, jv, primitiveClass);
}
}
}
|