001package Torello.JSON;
002
003import static javax.json.JsonValue.ValueType.*;
004
005import Torello.JavaDoc.Annotations.IntoHTMLTable;
006import static Torello.JavaDoc.Annotations.IntoHTMLTable.Background.BlueDither;
007import static Torello.JavaDoc.Annotations.IntoHTMLTable.Background.GreenDither;
008
009import java.util.function.Predicate;
010import java.util.function.ToIntFunction;
011import java.util.function.ToLongFunction;
012import java.util.function.ToDoubleFunction;
013
014import Torello.Java.Function.ToByteFunction;
015import Torello.Java.Function.ToShortFunction;
016import Torello.Java.Function.ToFloatFunction;
017
018import javax.json.JsonArray;
019import javax.json.JsonObject;
020import javax.json.JsonValue;
021import javax.json.JsonString;
022import javax.json.JsonNumber;
023
024import java.util.function.Function;
025
026
027/**
028 * Utilities for parsing &amp; converting <B><I>{@link JsonString}'s</I>
029 * <SPAN STYLE='color: red;'>or</SPAN> <I>{@link JsonNumber}'s</I></B> into Java
030 * Boxed-Primitive Types.
031 * 
032 * <EMBED CLASS='external-html' DATA-FILE-ID=ALL_CLASSES_NOTE>
033 * <EMBED CLASS='external-html' DATA-FILE-ID=RORP_PRIM_JSON>
034 * <EMBED CLASS='external-html' DATA-FILE-ID=RORP_PRIM_PTABLE>
035 * <EMBED CLASS='external-html' DATA-CH='java.lang.Character' DATA-FILE-ID=JAVA_LANG_CHAR>
036 * 
037 * @see JsonObject
038 * @see JsonArray
039 */
040@Torello.JavaDoc.Annotations.StaticFunctional
041public class RorPPrimJSON
042{
043    private RorPPrimJSON() { }
044
045
046    // ****************************************************************************************
047    // ****************************************************************************************
048    // JsonArray index location to Boxed Primitive, ACCEPTS USER FLAGS
049    // ****************************************************************************************
050    // ****************************************************************************************
051
052
053    /**
054     * <EMBED CLASS='external-html' DATA-TYPE=int DATA-FILE-ID=RORP_PRIM_JA>
055     * @see ReadPrimJSON#getInt(JsonArray, int)
056     * @see ParsePrimJSON#parseInt(JsonArray, int, ToIntFunction)
057     */
058    @IntoHTMLTable(
059        title=  "Read either a JsonNumber or a JsonString from a JsonArray, "+
060                "and Translate to a Java 'int' Primitive",
061        background=BlueDither
062    )
063    public static int getInteger(
064            final JsonArray             ja,
065            final int                   i,
066            final ToIntFunction<String> optionalUserParser
067        )
068    {
069        return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING))
070            ? ReadPrimJSON.getInt(ja, i)
071            : ParsePrimJSON.parseInt(ja, i, optionalUserParser);
072    }
073
074    /**
075     * <EMBED CLASS='external-html' DATA-TYPE=long DATA-FILE-ID=RORP_PRIM_JA>
076     * @see ReadPrimJSON#getLong(JsonArray, int)
077     * @see ParsePrimJSON#parseLong(JsonArray, int, ToLongFunction)
078     */
079    @IntoHTMLTable(
080        title=  "Read either a JsonNumber or a JsonString from a JsonArray, "+
081                "and Translate to a Java 'long' Primitive",
082        background=GreenDither
083    )
084    public static long getLong(
085            final JsonArray                 ja,
086            final int                       i,
087            final ToLongFunction<String>    optionalUserParser
088        )
089    {
090        return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING))
091            ? ReadPrimJSON.getLong(ja, i)
092            : ParsePrimJSON.parseLong(ja, i, optionalUserParser);
093    }
094
095    /**
096     * <EMBED CLASS='external-html' DATA-TYPE=short DATA-FILE-ID=RORP_PRIM_JA>
097     * @see ReadPrimJSON#getShort(JsonArray, int)
098     * @see ParsePrimJSON#parseShort(JsonArray, int, ToShortFunction)
099     */
100    @IntoHTMLTable(
101        title=  "Read either a JsonNumber or a JsonString from a JsonArray, "+
102                "and Translate to a Java 'short' Primitive",
103        background=BlueDither
104    )
105    public static short getShort(
106            final JsonArray                 ja,
107            final int                       i,
108            final ToShortFunction<String>   optionalUserParser
109        )
110    {
111        return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING))
112            ? ReadPrimJSON.getShort(ja, i)
113            : ParsePrimJSON.parseShort(ja, i, optionalUserParser);
114    }
115
116    /**
117     * <EMBED CLASS='external-html' DATA-TYPE=byte DATA-FILE-ID=RORP_PRIM_JA>
118     * @see ReadPrimJSON#getByte(JsonArray, int)
119     * @see ParsePrimJSON#parseByte(JsonArray, int, ToByteFunction)
120     */
121    @IntoHTMLTable(
122        title=  "Read either a JsonNumber or a JsonString from a JsonArray, "+
123                "and Translate to a Java 'byte' Primitive",
124        background=GreenDither
125    )
126    public static byte getByte(
127            final JsonArray                 ja,
128            final int                       i,
129            final ToByteFunction<String>    optionalUserParser
130        )
131    {
132        return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING))
133            ? ReadPrimJSON.getByte(ja, i)
134            : ParsePrimJSON.parseByte(ja, i, optionalUserParser);
135    }
136
137    /**
138     * <EMBED CLASS='external-html' DATA-TYPE=double DATA-FILE-ID=RORP_PRIM_JA>
139     * @see ReadPrimJSON#getDouble(JsonArray, int)
140     * @see ParsePrimJSON#parseDouble(JsonArray, int, ToDoubleFunction)
141     */
142    @IntoHTMLTable(
143        title=  "Read either a JsonNumber or a JsonString from a JsonArray, "+
144                "and Translate to a Java 'double' Primitive",
145        background=BlueDither
146    )
147    public static double getDouble(
148            final JsonArray                 ja,
149            final int                       i,
150            final ToDoubleFunction<String>  optionalUserParser
151        )
152    {
153        return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING))
154            ? ReadPrimJSON.getDouble(ja, i)
155            : ParsePrimJSON.parseDouble(ja, i, optionalUserParser);
156    }
157
158    /**
159     * <EMBED CLASS='external-html' DATA-TYPE=float DATA-FILE-ID=RORP_PRIM_JA>
160     * @see ReadPrimJSON#getFloat(JsonArray, int)
161     * @see ParsePrimJSON#parseFloat(JsonArray, int, ToFloatFunction)
162     */
163    @IntoHTMLTable(
164        title=  "Read either a JsonNumber or a JsonString from a JsonArray, "+
165                "and Translate to a Java 'float' Primitive",
166        background=GreenDither
167    )
168    public static float getFloat(
169            final JsonArray                 ja,
170            final int                       i,
171            final ToFloatFunction<String>   optionalUserParser
172        )
173    {
174        return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING))
175            ? ReadPrimJSON.getFloat(ja, i)
176            : ParsePrimJSON.parseFloat(ja, i, optionalUserParser);
177    }
178
179    /**
180     * <EMBED CLASS='external-html' DATA-TYPE=boolean DATA-FILE-ID=RORP_PRIM_JA>
181     * @see ReadPrimJSON#getBoolean(JsonArray, int)
182     * @see ParsePrimJSON#parseBoolean(JsonArray, int, Predicate)
183     */
184    @IntoHTMLTable(
185        title=  "Read either a Json TRUE, FALSE or a JsonString from a JsonArray, "+
186                "and Translate to a Java 'boolean' Primitive",
187        background=BlueDither
188    )
189    public static boolean getBoolean(
190            final JsonArray         ja,
191            final int               i,
192            final Predicate<String> optionalUserParser
193        )
194    {
195        return ((i >= ja.size()) ||  (ja.get(i).getValueType() != STRING))
196            ? ReadPrimJSON.getBoolean(ja, i)
197            : ParsePrimJSON.parseBoolean(ja, i, optionalUserParser);
198    }
199
200
201    // ****************************************************************************************
202    // ****************************************************************************************
203    // JsonObject Property to Boxed Primitive, ACCEPTS USER FLAGS
204    // ****************************************************************************************
205    // ****************************************************************************************
206
207
208    /**
209     * <EMBED CLASS='external-html' DATA-TYPE=int DATA-FILE-ID=RORP_PRIM_JO>
210     * @see ReadPrimJSON#getInt(JsonObject, String)
211     * @see ParsePrimJSON#parseInt(JsonObject, String, ToIntFunction)
212     */
213    @IntoHTMLTable(
214        title=  "Read either a JsonNumber or a JsonString from a JsonObject, "+
215                "and Translate to a Java 'int' Primitive",
216        background=GreenDither
217    )
218    public static int getInteger(
219            final JsonObject            jo,
220            final String                propertyName,
221            final ToIntFunction<String> optionalUserParser
222        )
223    {
224        final JsonValue jv = jo.get(propertyName);
225
226        return ((jv == null) || (jv.getValueType() != STRING))
227            ? ReadPrimJSON.getInt(jo, propertyName)
228            : ParsePrimJSON.parseInt(jo, propertyName, optionalUserParser);
229    }
230
231    /**
232     * <EMBED CLASS='external-html' DATA-TYPE=long DATA-FILE-ID=RORP_PRIM_JO>
233     * @see ReadPrimJSON#getLong(JsonObject, String)
234     * @see ParsePrimJSON#parseLong(JsonObject, String, ToLongFunction)
235     */
236    @IntoHTMLTable(
237        title=  "Read either a JsonNumber or a JsonString from a JsonObject, "+
238                "and Translate to a Java 'long' Primitive",
239        background=BlueDither
240    )
241    public static long getLong(
242            final JsonObject                jo,
243            final String                    propertyName,
244            final ToLongFunction<String>    optionalUserParser
245        )
246    {
247        final JsonValue jv = jo.get(propertyName);
248
249        return ((jv == null) || (jv.getValueType() != STRING))
250            ? ReadPrimJSON.getLong(jo, propertyName)
251            : ParsePrimJSON.parseLong(jo, propertyName, optionalUserParser);
252    }
253
254    /**
255     * <EMBED CLASS='external-html' DATA-TYPE=short DATA-FILE-ID=RORP_PRIM_JO>
256     * @see ReadPrimJSON#getShort(JsonObject, String)
257     * @see ParsePrimJSON#parseShort(JsonObject, String, ToShortFunction)
258     */
259    @IntoHTMLTable(
260        title=  "Read either a JsonNumber or a JsonString from a JsonObject, "+
261                "and Translate to a Java 'short' Primitive",
262        background=GreenDither
263    )
264    public static short getShort(
265            final JsonObject                jo,
266            final String                    propertyName,
267            final ToShortFunction<String>   optionalUserParser
268        )
269    {
270        final JsonValue jv = jo.get(propertyName);
271
272        return ((jv == null) || (jv.getValueType() != STRING))
273            ? ReadPrimJSON.getShort(jo, propertyName)
274            : ParsePrimJSON.parseShort(jo, propertyName, optionalUserParser);
275    }
276
277    /**
278     * <EMBED CLASS='external-html' DATA-TYPE=byte DATA-FILE-ID=RORP_PRIM_JO>
279     * @see ReadPrimJSON#getByte(JsonObject, String)
280     * @see ParsePrimJSON#parseByte(JsonObject, String, ToByteFunction)
281     */
282    @IntoHTMLTable(
283        title=  "Read either a JsonNumber or a JsonString from a JsonObject, "+
284                "and Translate to a Java 'byte' Primitive",
285        background=BlueDither
286    )
287    public static byte getByte(
288            final JsonObject                jo,
289            final String                    propertyName,
290            final ToByteFunction<String>    optionalUserParser
291        )
292    {
293        final JsonValue jv = jo.get(propertyName);
294
295        return ((jv == null) || (jv.getValueType() != STRING))
296            ? ReadPrimJSON.getByte(jo, propertyName)
297            : ParsePrimJSON.parseByte(jo, propertyName, optionalUserParser);
298    }
299
300    /**
301     * <EMBED CLASS='external-html' DATA-TYPE=double DATA-FILE-ID=RORP_PRIM_JO>
302     * @see ReadPrimJSON#getDouble(JsonObject, String)
303     * @see ParsePrimJSON#parseDouble(JsonObject, String, ToDoubleFunction)
304     */
305    @IntoHTMLTable(
306        title=  "Read either a JsonNumber or a JsonString from a JsonObject, "+
307                "and Translate to a Java 'double' Primitive",
308        background=GreenDither
309    )
310    public static double getDouble(
311            final JsonObject                jo,
312            final String                    propertyName,
313            final ToDoubleFunction<String>  optionalUserParser
314        )
315    {
316        final JsonValue jv = jo.get(propertyName);
317
318        return ((jv == null) || (jv.getValueType() != STRING))
319            ? ReadPrimJSON.getDouble(jo, propertyName)
320            : ParsePrimJSON.parseDouble(jo, propertyName, optionalUserParser);
321    }
322
323    /**
324     * <EMBED CLASS='external-html' DATA-TYPE=float DATA-FILE-ID=RORP_PRIM_JO>
325     * @see ReadPrimJSON#getFloat(JsonObject, String)
326     * @see ParsePrimJSON#parseFloat(JsonObject, String, ToFloatFunction)
327     */
328    @IntoHTMLTable(
329        title=  "Read either a JsonNumber or a JsonString from a JsonObject, "+
330                "and Translate to a Java 'float' Primitive",
331        background=BlueDither
332    )
333    public static float getFloat(
334            final JsonObject                jo,
335            final String                    propertyName,
336            final ToFloatFunction<String>   optionalUserParser
337        )
338    {
339        final JsonValue jv = jo.get(propertyName);
340
341        return ((jv == null) || (jv.getValueType() != STRING))
342            ? ReadPrimJSON.getFloat(jo, propertyName)
343            : ParsePrimJSON.parseFloat(jo, propertyName, optionalUserParser);
344    }
345
346    /**
347     * <EMBED CLASS='external-html' DATA-TYPE=boolean DATA-FILE-ID=RORP_PRIM_JO>
348     * @see ReadPrimJSON#getBoolean(JsonObject, String)
349     * @see ParsePrimJSON#parseBoolean(JsonObject, String, Predicate)
350     */
351    @IntoHTMLTable(
352        title=  "Read either a Json TRUE, FALSE or a JsonString from a JsonObject, " +
353                "and Translate to a Java 'boolean' Primitive",
354        background=GreenDither
355    )
356    public static boolean getBoolean(
357            final JsonObject        jo,
358            final String            propertyName,
359            final Predicate<String> optionalUserParser
360        )
361    {
362        final JsonValue jv = jo.get(propertyName);
363
364        return ((jv == null) || (jv.getValueType() != STRING))
365            ? ReadPrimJSON.getBoolean(jo, propertyName)
366            : ParsePrimJSON.parseBoolean(jo, propertyName, optionalUserParser);
367    }
368
369}