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
package Torello.Java.JSON;

import javax.json.*;
import java.lang.reflect.*;
import java.math.*;

import java.util.function.Function;

import static javax.json.JsonValue.ValueType.*;
import static Torello.Java.JSON.JFlag.*;
import static Torello.Java.JSON.RJInternal.*;

/**
 * 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=GLASS_FISH_NOTE>
 * <EMBED CLASS='external-html' DATA-CH=char DATA-FILE-ID=JAVA_LANG_CHAR>
 * 
 * @see Json
 * @see JsonObject
 * @see JsonArray
 */
@Torello.JavaDoc.StaticFunctional
@Torello.JavaDoc.JDHeaderBackgroundImg(EmbedTagFileID="JSON_JDHBI")
@SuppressWarnings("cast")
public class ReadPrimJSON
{
    // This is a static class.  Has no program state.
    private ReadPrimJSON() { }


    // ********************************************************************************************
    // ********************************************************************************************
    // int
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Retrieve a {@link JsonArray} element, and transform it to an {@code 'int'} primitive
     * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE=int DATA-M=intValueExact>
     * 
     * @param ja    Any instance of {@link JsonArray}
     * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA>
     * @return      <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JA>
     * 
     * @throws IndexOutOfBoundsException     If {@code 'index'} is out of the bounds of {@code 'ja'}
     * @throws JsonTypeArrException          <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX>
     * @throws JsonArithmeticArrException    <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX>
     * @throws JsonNullPrimitiveArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPAEX>
     * 
     * @see RJInternal#GET(JsonArray, int, Class, Function)
     * @see JsonNumber#intValueExact()
     */
    public static int getInt(JsonArray ja, int index)
    { return GET(ja, index, int.class, JsonNumber::intValueExact); }

    /**
     * Extract a {@link JsonObject} property, and transform it to an {@code 'int'} primitive
     * <EMBED CLASS=defs DATA-TYPE=int DATA-JTYPE=JsonNumber DATA-M=intValueExact>
     * 
     * @param jo            Any instance of {@link JsonObject}
     * @param propertyName  <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO>
     * @return              <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JO>
     * 
     * @throws JsonPropMissingException      <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX_PR>
     * @throws JsonArithmeticObjException    <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX>
     * @throws JsonTypeObjException          <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX>
     * @throws JsonNullPrimitiveObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPOEX>
     * 
     * @see RJInternal#GET(JsonObject, String, Class, Function)
     * @see JsonNumber#intValueExact()
     */
    public static int getInt(JsonObject jo, String propertyName)
    { return GET(jo, propertyName, int.class, JsonNumber::intValueExact); }


    // ********************************************************************************************
    // ********************************************************************************************
    // long
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Retrieve a {@link JsonArray} element, and transform it to a {@code 'long'} primitive
     * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE=long DATA-M=longValueExact>
     * 
     * @param ja    Any instance of {@link JsonArray}
     * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA>
     * @return      <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JA>
     * 
     * @throws IndexOutOfBoundsException     If {@code 'index'} is out of the bounds of {@code 'ja'}
     * @throws JsonTypeArrException          <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX>
     * @throws JsonArithmeticArrException    <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX>
     * @throws JsonNullPrimitiveArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPAEX>
     * 
     * @see RJInternal#GET(JsonArray, int, Class, Function)
     * @see JsonNumber#longValueExact()
     */
    public static long getLong(JsonArray ja, int index)
    { return GET(ja, index, long.class, JsonNumber::longValueExact); }

    /**
     * Extract a {@link JsonObject} property, and transform it to a {@code 'long'} primitive
     * <EMBED CLASS=defs DATA-TYPE=long DATA-JTYPE=JsonNumber DATA-M=longValueExact>
     * 
     * @param jo            Any instance of {@link JsonObject}
     * @param propertyName  <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO>
     * @return              <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JO>
     * 
     * @throws JsonPropMissingException      <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX_PR>
     * @throws JsonArithmeticObjException    <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX>
     * @throws JsonTypeObjException          <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX>
     * @throws JsonNullPrimitiveObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPOEX>
     * 
     * @see RJInternal#GET(JsonObject, String, Class, Function)
     * @see JsonNumber#longValueExact()
     */
    public static long getLong(JsonObject jo, String propertyName)
    { return GET(jo, propertyName, long.class, JsonNumber::longValueExact); }


    // ********************************************************************************************
    // ********************************************************************************************
    // short
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Retrieve a {@link JsonArray} element, and transform it to a {@code 'short'} primitive
     * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE=short DATA-M=shortValueExact>
     * 
     * @param ja    Any instance of {@link JsonArray}
     * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA>
     * @return      <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JA>
     * 
     * @throws IndexOutOfBoundsException     If {@code 'index'} is out of the bounds of {@code 'ja'}
     * @throws JsonTypeArrException          <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX>
     * @throws JsonArithmeticArrException    <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX>
     * @throws JsonNullPrimitiveArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPAEX>
     * 
     * @see RJInternal#GET(JsonArray, int, Class, Function)
     * @see JsonNumber#bigDecimalValue()
     */
    public static short getShort(JsonArray ja, int index)
    { return GET(ja, index, short.class, jn -> jn.bigDecimalValue().shortValueExact()); }

    /**
     * Extract a {@link JsonObject} property, and transform it to a {@code 'short'} primitive
     * <EMBED CLASS=defs DATA-TYPE=short DATA-JTYPE=JsonNumber DATA-M=shortValueExact>
     * 
     * @param jo            Any instance of {@link JsonObject}
     * @param propertyName  <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO>
     * @return              <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JO>
     * 
     * @throws JsonPropMissingException      <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX_PR>
     * @throws JsonArithmeticObjException    <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX>
     * @throws JsonTypeObjException          <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX>
     * @throws JsonNullPrimitiveObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPOEX>
     * 
     * @see RJInternal#GET(JsonObject, String, Class, Function)
     * @see JsonNumber#bigDecimalValue()
     */
    public static short getShort(JsonObject jo, String propertyName)
    { return GET(jo, propertyName, short.class, jn -> jn.bigDecimalValue().shortValueExact()); }


    // ********************************************************************************************
    // ********************************************************************************************
    // byte
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Retrieve a {@link JsonArray} element, and transform it to a {@code 'byte'} primitive
     * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE=byte DATA-M=byteValueExact>
     * 
     * @param ja    Any instance of {@link JsonArray}
     * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA>
     * @return      <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JA>
     * 
     * @throws IndexOutOfBoundsException     If {@code 'index'} is out of the bounds of {@code 'ja'}
     * @throws JsonTypeArrException          <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX>
     * @throws JsonArithmeticArrException    <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX>
     * @throws JsonNullPrimitiveArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPAEX>
     * 
     * @see RJInternal#GET(JsonArray, int, Class, Function)
     * @see JsonNumber#bigDecimalValue()
     */
    public static byte getByte(JsonArray ja, int index)
    { return GET(ja, index, byte.class, jn -> jn.bigDecimalValue().byteValueExact()); }

    /**
     * Extract a {@link JsonObject} property, and transform it to a {@code 'byte'} primitive
     * <EMBED CLASS=defs DATA-TYPE=byte DATA-JTYPE=JsonNumber DATA-M=byteValueExact>
     * 
     * @param jo            Any instance of {@link JsonObject}
     * @param propertyName  <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO>
     * @return              <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JO>
     * 
     * @throws JsonPropMissingException      <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX_PR>
     * @throws JsonArithmeticObjException    <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX>
     * @throws JsonTypeObjException          <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX>
     * @throws JsonNullPrimitiveObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPOEX>
     * 
     * @see RJInternal#GET(JsonObject, String, Class, Function)
     * @see JsonNumber#bigDecimalValue()
     */
    public static byte getByte(JsonObject jo, String propertyName)
    { return GET(jo, propertyName, byte.class, jn -> jn.bigDecimalValue().byteValueExact()); }


    // ********************************************************************************************
    // ********************************************************************************************
    // double
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Retrieve a {@link JsonArray} element, and transform it to a {@code 'double'} primitive
     * 
     * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE=double DATA-M=doubleValue
     *  DATA-INFINITY=Double>
     * <EMBED CLASS='external-html' DATA-FILE-ID=JR_FLOAT>
     * 
     * @param ja    Any instance of {@link JsonArray}
     * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA>
     * @return      <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JA>
     * 
     * @throws IndexOutOfBoundsException     If {@code 'index'} is out of the bounds of {@code 'ja'}
     * @throws JsonTypeArrException          <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX>
     * @throws JsonArithmeticArrException    <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX_INF>
     * @throws JsonNullPrimitiveArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPAEX>
     * 
     * @see RJInternal#GET(JsonArray, int, Class, Function)
     * @see RJInternal#DOUBLE_WITH_CHECK(JsonNumber)
     */
    public static double getDouble(JsonArray ja, int index)
    { return GET(ja, index, double.class, RJInternal::DOUBLE_WITH_CHECK); }

    /**
     * Extract a {@link JsonObject} property, and transform it to a {@code 'double'} primitive
     * 
     * <EMBED CLASS=defs DATA-TYPE=double DATA-JTYPE=JsonNumber DATA-M=doubleValue
     *  DATA-INFINITY=Double>
     * <EMBED CLASS='external-html' DATA-FILE-ID=JR_FLOAT>
     * 
     * @param jo            Any instance of {@link JsonObject}
     * @param propertyName  <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO>
     * @return              <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JO>
     * 
     * @throws JsonPropMissingException      <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX_PR>
     * @throws JsonArithmeticObjException    <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX_INF>
     * @throws JsonTypeObjException          <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX>
     * @throws JsonNullPrimitiveObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPOEX>
     * 
     * @see RJInternal#GET(JsonObject, String, Class, Function)
     * @see RJInternal#DOUBLE_WITH_CHECK(JsonNumber)
     */
    public static double getDouble(JsonObject jo, String propertyName)
    { return GET(jo, propertyName, double.class, RJInternal::DOUBLE_WITH_CHECK); }


    // ********************************************************************************************
    // ********************************************************************************************
    // float
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Retrieve a {@link JsonArray} element, and transform it to a {@code 'float'} primitive
     * 
     * <EMBED CLASS=defs DATA-JTYPE=JsonNumber DATA-TYPE=float DATA-M=floatValue
     *  DATA-INFINITY=Float>
     * <EMBED CLASS='external-html' DATA-FILE-ID=JR_FLOAT>
     * 
     * @param ja    Any instance of {@link JsonArray}
     * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA>
     * @return      <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JA>
     * 
     * @throws IndexOutOfBoundsException     If {@code 'index'} is out of the bounds of {@code 'ja'}
     * @throws JsonTypeArrException          <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX>
     * @throws JsonArithmeticArrException    <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX_INF>
     * @throws JsonNullPrimitiveArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPAEX>
     * 
     * @see RJInternal#GET(JsonArray, int, Class, Function)
     * @see RJInternal#FLOAT_WITH_CHECK(JsonNumber)
     */
    public static float getFloat(JsonArray ja, int index)
    { return GET(ja, index, float.class, RJInternal::FLOAT_WITH_CHECK); }

    /**
     * Extract a {@link JsonObject} property, and transform it to a {@code 'float'} primitive
     * 
     * <EMBED CLASS=defs DATA-TYPE=float DATA-JTYPE=JsonNumber DATA-M=floatValue
     *  DATA-INFINITY=Float>
     * <EMBED CLASS='external-html' DATA-FILE-ID=JR_FLOAT>
     * 
     * @param jo            Any instance of {@link JsonObject}
     * @param propertyName  <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO>
     * @return              <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JO>
     * 
     * @throws JsonPropMissingException      <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX_PR>
     * @throws JsonArithmeticObjException    <EMBED CLASS='external-html' DATA-FILE-ID=JR_JAEX_INF>
     * @throws JsonTypeObjException          <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX>
     * @throws JsonNullPrimitiveObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPOEX>
     * 
     * @see RJInternal#GET(JsonObject, String, Class, Function)
     * @see RJInternal#FLOAT_WITH_CHECK(JsonNumber)
     */
    public static float getFloat(JsonObject jo, String propertyName)
    { return GET(jo, propertyName, float.class, RJInternal::FLOAT_WITH_CHECK); }


    // ********************************************************************************************
    // ********************************************************************************************
    // boolean
    // ********************************************************************************************
    // ********************************************************************************************


    /**
     * Retrieve a {@link JsonArray} element, and transform it to a {@code 'boolean'} primitive
     * <EMBED CLASS=defs DATA-JTYPE='JSON-BOOLEAN' DATA-TYPE=boolean>
     * 
     * @param ja    Any instance of {@link JsonArray}
     * @param index <EMBED CLASS='external-html' DATA-FILE-ID=JR_INDEX_JA>
     * @return      <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JA>
     * 
     * @throws IndexOutOfBoundsException     If {@code 'index'} is out of the bounds of {@code 'ja'}
     * @throws JsonTypeArrException          <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTAEX>
     * @throws JsonNullPrimitiveArrException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPAEX>
     * 
     * @see ReadBoxedJSON#getBoolean(JsonArray, int)
     */
    public static boolean getBoolean(JsonArray ja, int index)
    {
        // This method (defined below) allows for null-returns, but "getBoolean" does not.  
        // "getBOOLEAN" will only return null if the actual array location 'index' actually has
        // the word 'null' listed.

        Boolean ret = ReadBoxedJSON.getBoolean(ja, index);

        // In that case, throw the Null-Primitive Exception
        if (ret == null) throw new JsonNullPrimitiveArrException
                (ja, index, TRUE, boolean.class);

        return ret;
    }

    /**
     * Extract a {@link JsonObject} property, and transform it to a {@code 'boolean'} primitive
     * <EMBED CLASS=defs DATA-TYPE=boolean DATA-JTYPE='JSON-BOOLEAN'>
     * 
     * @param               jo Any instance of {@link JsonObject}
     * @param propertyName  <EMBED CLASS='external-html' DATA-FILE-ID=JR_PN_JO>
     * @return              <EMBED CLASS='external-html' DATA-FILE-ID=JR_PRRET_JO>
     * 
     * @throws JsonPropMissingException      <EMBED CLASS='external-html' DATA-FILE-ID=JR_JPMEX_PR>
     * @throws JsonTypeObjException          <EMBED CLASS='external-html' DATA-FILE-ID=JR_JTOEX>
     * @throws JsonNullPrimitiveObjException <EMBED CLASS='external-html' DATA-FILE-ID=JR_JNPOEX>
     * 
     * @see ReadBoxedJSON#getBoolean(JsonObject, String, boolean)
     */
    public static boolean getBoolean(JsonObject jo, String propertyName)
    {
        // Since 'false' is passed, a null return-value will mean that the property was actually
        // set to null in the JsonObject itself.  (It *IS NOT* missing, it is present, but rather
        // declared null)

        Boolean ret = ReadBoxedJSON.getBoolean(jo, propertyName, false);

        if (ret == null) throw new JsonNullPrimitiveObjException
            (jo, propertyName, TRUE, boolean.class);

        // NOTE: Java's Type-Stuff can automatically convert Boolean -> boolean
        else return ret;
    }
}