Package Torello.JSON

Class RJArrIntoPrimArray


  • public class RJArrIntoPrimArray
    extends java.lang.Object
    RJArrIntoPrimArray 🠞
    • RJArr - Read JsonArray
      This class is used for reading data directly from an already parsed JsonArray instance.

    • Into - Data that is extracted, is sent to a specific User-Provided Destination.

    • PrimArray - Data is sent to a Primitive Java-Array.
    Utilities for parsing Json Array's and sending the parsed values into a Java-Array

    This class builds on the J2EE Standard 'Glass-Fish' JSON Processor


    There are several JSON Parsers available, and even more implementations for serializing and de-serializing data to/from JSON. The Glass Fish Tool is included in the J2EE, and is available on GitHub. That is the one used by the Java HTML JAR Library (See: javax.json.* )

    Primary Classes Used: JsonArray and JsonObject


    This comment-note is intentionally repeated, verbatim, at the top of all Json Reader Classes in this package.


    JSON to Java Binding:
    JSON-Binding is the art of converting data that has been stored, saved or transmitted using Java-Script Object Notation into a Java Primitive or Object-Type. JSON can arrive into Java-Program Memory from almost any source. If you are wondering why such a massive amount of "work" is necessary just to convert a Json Integer into Java Integer, the value added is the extraordinary amount of attention paid to user configuration, error checking, & exception messaging. Methods here don't require more than 1 or 2 lines of code, and guarantee that a thorough type checking is performed.

    Unlike Java-Types which are checked by the Java-Compiler each-and-every time a programmer compiles his project, any guarantee that JSON-Type Data is pristine, uncorrupted, and in any kind of pre-agreed format may or may not be assured. The methods here are able to handle changes that might be made to an API (possibly from great distances away, and without the Software-Manager's consent). If an error could occur, configuration flags can be used to determine default error-recovery behaviors. If an exception does throw, the exception messages printed will contain multiple lines of detailed information.

    • Utilizes the Java-Standard javax.json.* Package-Library, & its Glass-Fish Implementation
    • Handles the Transfer & Conversion of All Json-Type's into Java-Type's with just One Line of Code
    • Provides all manner of User-Configurable Exception-Handling & Error-Decision Management via Class JFlag
    • Provides a Fine-Grained Suite of Exception-Classes, all with Consistent & Meaningful Error-Messages. Json Files can occasionally grow extremely large, and error messaging details make debugging easier
    • Primary Helper-Classes for the (Experimental) Google-Chrome Browser Automation Package

    The goal of Torello.JSON is to provide small, static helper methods in the same spirit as java.util.Objects and java.util.Arrays: simple calls that keep JSON handling out of your application logic. Instead of repeating loops, type checks, null checks, and exception wiring at every call site, these methods centralize that work behind a consistent set of straight-forward “read value” operations.


    Generate Java Primitive, Single-Dimension, Arrays using JsonArray's as Input

    The seven methods provided by this class are, also, all declared 'static'. They produce simple, Single-Dimension, Java Primitive-Array's. This makes for a total of seven "Generator Methods" provided in this class' simple A.P.I. There is one method for each of the Primitive-Types included with the JDK, except for type char
    It might be of some interest to know that the three types for which Oracle has written "Primitive-Stream" types (IntStream, LongStream and DoubleStream) are methods which, in this class, are nothing more than single line convenience Wrapper-Invocations around calls to the class RJArrIntoPrimStream, followed by a call to the Primitive-Stream Helper-Method which is included in all implementations of the Stream-API - which is 'toArray(...)'.

    These three are simple, single-line calls: intArr(...), longArr(...) and doubleArr(...).

    The remaining four Primitive-Types require methods which actually build a Java Boxed-Stream, and then convert the Boxed-Stream into a primitive array. The methods which do this include those for generating: short[], byte[], float[] and boolean[].





    Arrays from Primitive Stream's
    Java has three Primitive-Stream types: IntStream, LongStream and DoubleStream. These are also easily converted into array's via the following:
    StreamArray
    IntStreamint[] arr = IntStream.toArray()
    LongStreamlong[] arr = LongStream.toArray()
    DoubleStreamdouble[] arr = DoubleStream.toArray()


    It is for this reason that these three array types are not included as methods in this class. If you need an int[]-Array, please use:




    Method Parameters

    Parameter Explanation
    ja This may be any parsed JsonArray whose contents are consistent with the Expected Output-Array Base-Type returned from whichever method is being invoked.
    defaultValue When an appropriate JFlag-Value is included in the mask, this Default-Value will be passed to the User-Provided Consumer in whichever Error-Circumstance has occured (as per the Flag-Value Name).

    This Default-Value may be requested through JFlag-Values such as: RD_AEX, RD_IOB and RD_M (among others). The 'RD' in these Flag's Names is an abbreviation for "Return Default-Value".

    The Complete List of Flags which request that this Default-Value Parameter be employed under various Error-Circumstances may be viewed in the following link. The name of each of these flags attempts identify the type of Error-Situation to which it may be applied.

    Default-Value Flags
    FLAGS A Bit-Wise Flag-Mask that provides a means for configuring the Array-Processing Logic to properly handle several types of Error-Cases & potentatial, unexpected, array contents.

    A Flag-Mask of '0' requests that any Error-Situations which occur, should they crop up while processing the array, be handled by standard means - meaning by throwing one of the germaine Exception-Class.
    optionalUserParser This parameter can only serve a purpose when it is used in conjunction with a JFlag-Vaule which explicity requests that Java String-Values be handled by a parser (rather than causing an exception to throw).

    The two flags which may be employed to signal this Handler-Behavior are: RETURN_PARSE_ON_STR and RP_S (the latter being merely the abbreviated variant of the former).

    If neither of these flags have been AND'ed into the Flag-Mask parameter ("FLAGS"), then any function which is passed to this parameter ("optionalUserParser") is wholly ignored - simply because using a String-Parser is not a "Default Behavior of this class' Array-Processing Logic.
    See Also:
    Json, JsonArray



    Stateless Class:
    This class neither contains any program-state, nor can it be instantiated. The @StaticFunctional Annotation may also be called 'The Spaghetti Report'. Static-Functional classes are, essentially, C-Styled Files, without any constructors or non-static member fields. It is a concept very similar to the Java-Bean's @Stateless Annotation.

    • 1 Constructor(s), 1 declared private, zero-argument constructor
    • 7 Method(s), 7 declared static
    • 0 Field(s)


    • Method Summary

       
      Read the Contents of a Json-Array into a Java Primitive-Array
      Modifier and Type Method
      static boolean[] booleanArr​(JsonArray ja, boolean defaultValue, int FLAGS, Predicate<String> optionalUserParser)
      static byte[] byteArr​(JsonArray ja, byte defaultValue, int FLAGS, ToByteFunction<String> optionalUserParser)
      static double[] doubleArr​(JsonArray ja, double defaultValue, int FLAGS, ToDoubleFunction<String> optionalUserParser)
      static float[] floatArr​(JsonArray ja, float defaultValue, int FLAGS, ToFloatFunction<String> optionalUserParser)
      static int[] intArr​(JsonArray ja, int defaultValue, int FLAGS, ToIntFunction<String> optionalUserParser)
      static long[] longArr​(JsonArray ja, long defaultValue, int FLAGS, ToLongFunction<String> optionalUserParser)
      static short[] shortArr​(JsonArray ja, short defaultValue, int FLAGS, ToShortFunction<String> optionalUserParser)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • shortArr

        🡇         External-Java:    🗕  🗗  🗖
        public static short[] shortArr​
                    (JsonArray ja,
                     short defaultValue,
                     int FLAGS,
                     ToShortFunction<java.lang.String> optionalUserParser)
        
        JsonArray of Short
        Invokes: Method RJArrIntoBoxedStream.shortArr(JsonArray, short, int, Function)
        And: Method GENERATE_1DARRAY.boxedStreamToPrimitiveArray(Stream, JsonArray, Class, ObjIntConsumer, Object)
           
        Converts: A JsonArray into a Stream<Short> and then to a short[]
        Removes: Any JFlag masks which might insert null-entries in the return-stream
        See: Class JFlag for information about parameter 'FLAGS'
        See: Flag JFlag.NOT_ALLOWED_RET_NULL_MASKS for a list of all disallowed & filtered flags.
        Code:
        Exact Method Body:
         final short[] retArr = new short[ja.size()];
        
         return GENERATE_1DARRAY.boxedStreamToPrimitiveArray(
             RJArrIntoBoxedStream.shortArr(
                 ja,
                 defaultValue,
                 FLAGS & NOT_ALLOWED_RET_NULL_MASKS,
                 (optionalUserParser == null) ? null : optionalUserParser::applyAsShort
             ),
             ja,
             short.class,
             (Short s, int i) -> retArr[i] = s.shortValue(),
             retArr
         );
        
      • byteArr

        🡅  🡇         External-Java:    🗕  🗗  🗖
        public static byte[] byteArr​
                    (JsonArray ja,
                     byte defaultValue,
                     int FLAGS,
                     ToByteFunction<java.lang.String> optionalUserParser)
        
        JsonArray of Bytes
        Invokes: Method RJArrIntoBoxedStream.byteArr(JsonArray, byte, int, Function)
        And: Method GENERATE_1DARRAY.boxedStreamToPrimitiveArray(Stream, JsonArray, Class, ObjIntConsumer, Object)
           
        Converts: A JsonArray into a Stream<Byte> and then to a byte[]
        Removes: Any JFlag masks which might insert null-entries in the return-stream
        See: Class JFlag for information about parameter 'FLAGS'
        See: Flag JFlag.NOT_ALLOWED_RET_NULL_MASKS for a list of all disallowed & filtered flags.
        Code:
        Exact Method Body:
         final byte[] retArr = new byte[ja.size()];
        
         return GENERATE_1DARRAY.boxedStreamToPrimitiveArray(
             RJArrIntoBoxedStream.byteArr(
                 ja,
                 defaultValue,
                 FLAGS & NOT_ALLOWED_RET_NULL_MASKS,
                 (optionalUserParser == null) ? null : optionalUserParser::applyAsByte
             ),
             ja,
             byte.class,
             (Byte s, int i) -> retArr[i] = s.byteValue(),
             retArr
         );
        
      • floatArr

        🡅  🡇         External-Java:    🗕  🗗  🗖
        public static float[] floatArr​
                    (JsonArray ja,
                     float defaultValue,
                     int FLAGS,
                     ToFloatFunction<java.lang.String> optionalUserParser)
        
        JsonArray of Floats
        Invokes: Method RJArrIntoBoxedStream.floatArr(JsonArray, float, int, Function)
        And: Method GENERATE_1DARRAY.boxedStreamToPrimitiveArray(Stream, JsonArray, Class, ObjIntConsumer, Object)
           
        Converts: A JsonArray into a Stream<Float> and then to a float[]
        Removes: Any JFlag masks which might insert null-entries in the return-stream
        See: Class JFlag for information about parameter 'FLAGS'
        See: Flag JFlag.NOT_ALLOWED_RET_NULL_MASKS for a list of all disallowed & filtered flags.
        Code:
        Exact Method Body:
         final float[] retArr = new float[ja.size()];
        
         return GENERATE_1DARRAY.boxedStreamToPrimitiveArray(
             RJArrIntoBoxedStream.floatArr(
                 ja,
                 defaultValue,
                 FLAGS & NOT_ALLOWED_RET_NULL_MASKS,
                 (optionalUserParser == null) ? null : optionalUserParser::applyAsFloat
             ),
             ja,
             float.class,
             (Float s, int i) -> retArr[i] = s.floatValue(),
             retArr
         );
        
      • booleanArr

        🡅  🡇         External-Java:    🗕  🗗  🗖
        public static boolean[] booleanArr​
                    (JsonArray ja,
                     boolean defaultValue,
                     int FLAGS,
                     java.util.function.Predicate<java.lang.String> optionalUserParser)
        
        JsonArray of Booleans
        Invokes: Method RJArrIntoBoxedStream.booleanArr(JsonArray, boolean, int, Function)
        And: Method GENERATE_1DARRAY.boxedStreamToPrimitiveArray(Stream, JsonArray, Class, ObjIntConsumer, Object)
           
        Converts: A JsonArray into a Stream<Boolean> and then to a boolean[]
        Removes: Any JFlag masks which might insert null-entries in the return-stream
        See: Class JFlag for information about parameter 'FLAGS'
        See: Flag JFlag.NOT_ALLOWED_RET_NULL_MASKS for a list of all disallowed & filtered flags.
        Code:
        Exact Method Body:
         final boolean[] retArr = new boolean[ja.size()];
        
         return GENERATE_1DARRAY.boxedStreamToPrimitiveArray(
             RJArrIntoBoxedStream.booleanArr(
                 ja,
                 defaultValue,
                 FLAGS & NOT_ALLOWED_RET_NULL_MASKS,
                 (optionalUserParser == null) ? null : optionalUserParser::test
             ),
             ja,
             boolean.class,
             (Boolean s, int i) -> retArr[i] = s.booleanValue(),
             retArr
         );