Package Torello.JSON
Class RJArrIntoConsumer
- java.lang.Object
-
- Torello.JSON.RJArrIntoConsumer
-
public class RJArrIntoConsumer extends java.lang.Object
RJArrIntoConsumer 🠞RJArr- ReadJsonArray
This class is used for reading data directly from an already parsedJsonArrayinstance.
Into- Data that is extracted, is sent to a specific User-Provided Destination.
Consumer- Data is sent to a Java-Consumer.
Utilities for parsing Json Array's and sending the parsed values into a Java Consumer Functional-Interface.
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:JsonArrayandJsonObject
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 usingJava-Script Object Notationinto a Java Primitive or Object-Type.JSONcan 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 AutomationPackage
The goal ofTorello.JSONis to provide small, static helper methods in the same spirit asjava.util.Objectsandjava.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.
Retrieve String, Object & Json-Types from a JsonArray; Send to a Consumer
Each of the methods in this class, all declared'static', accept a standardJsonArrayas input, and will attempt to convert each of the elements of that array into the following output types. The provided table below explains which types are expected to be provided to the methods in this class, and what types will be sent to the User-ProvidedConsumer'safter parsing and extracting the array elements.Array Element Type Returned / Emitted Type JsonObjectInstance of User-Specified, Parameterized-Type <T>JsonStringInstance of java.lang.StringJsonObject-- same / no change -- JsonArray-- same / no change --
Each of the emitted types for these methods may be sent to one of two "kinds" of Java Consumer's. The JDK Standard Consumer, whose full package name isjava.util.function.Consumeraccepts the post-processed output from the arrays that are passed to these methodss. This consumer impements a Function-Interface with one method that accepts the type in the method signature.
The second "kind" of Consumer which is available is provided by the Java-HTML'.jar'Library. It is known, simply, as an:IntIntTConsumer. Both of these User-Provided Consumers will accept post-processed array-entries, one by one, until the end of the array has been reached.Consumer Kind / Type Explanation of Use java.util.function.ConsumerAs elements are retrieved, extracted and processed from the User-Provided JsonArray, the are sent to the single-input parameter Lambda-Method that user has provided.IntIntTConsumerAs elements are retrieved, they are passed to this Java-HTML Consumer, along with two primitive integers.
The first'int'is the array index of theJsonArrayin which the item was located.
The second'int'is the "success count" for how many elements have been extracted from the array. Often these integers are identical, but in cases where the user has requested to skip on failure, the integers may differ.
POJO De-Serialization:
The methods that appear to de-serialize a JSON-Object into a standard Java-Object do not automatically generate a Java-Type based solely on the properties present within aJsonObject. Automatic type-creation (as exists in libraries such as 'Lombok') is not a feature offered by this package. These methods can reduce quite a bit of the work needed to convert Json into Java by offering methods which extactJsonValue'susing just a single line of code.
The methods in this class which return or emit a user-provided Parameterized-Type<T>require the user specify a means for constructing instances of'T'. This class expects one of the two cases:- User-Provided Type
<T>has a single argument constructor which accepts aJsonObjectas a parameter. In this case, standard Java Reflection is used to retrieve and extract this constructor.
- A Java Functional-Interface / Lambda be provided that accepts a
JsonObjectand returns an instance of User-Provided Type<T>. Building or generating an instance of<T>is acheived, simply, by invoking this function with theJsonObjectwhich was extracted from the array.
Method Parameters
Parameter Explanation JsonArray jaThis may be any parsed JsonArraywhose contents are consistent with the Input-Parameters to the User-Provided Consumer.<T> defaultValueThe only requirement is this value must be either an instance of 'T', or an instance of a child / inheriting type of'T'.
'null' may also be provided as a default value.String defaultValueAny Java String, or null, will sufficeJsonObject defautValue
JsonArray defautValueFor either of these, any instance may be provided. Null is also allowed. Frequently it might be convenient to use the singleton, empty, instances provided by the Glass-Fish Implementation:
JsonValue.EMPTY_JSON_OBJECT
JsonValue.EMPTY_JSON_ARRAYint FLAGSA 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-Classes.Class<T> consumerClassThis may be any Java Class. It is required due to the limitations presented by Generic Erasure; this information is simply not available without providing a reference to this Class Object. java.util.function.Function
<JsonObject, T> objBuilderWhen this parameter is present, it will be used to generate an instance of User-Specified Type 'T'by passing theJsonObject'sencountered within the input array, to this user provided function.Lambda Signature:
public <T> T apply(JsonObject jo)IntIntTFunc <JsonObject, T> objBuilderSimilar to the above parameter, this object builder produces instances of User-Specified, Parameterized-Type 'T', but uses a@FunctionalInterfacewhich accepts three parameters, intead of just the singleJsonObjectLambda Signature:
public <T> T apply(int jaIndex, int acceptorCount, JsonObject jo)
The two "extra parameters" show above, are both primitive integers. The first one simply provides theJsonArrayindex where the object was found. The second'int'contains the current iteration's "success count." These two numbers are usually identical; only cases where the user has specified aJFlagthat requests the processor "Skip Entries on Failure" can These two integers take different values.java.util.function.Consumer
<?> cFor the methods in this class, once an object has been extracted & retrieved from the input JsonArray, it is then passed to this User-Provided Consumer. This is the how this class' name, "Into Consumer," was chosen.Lambda Signature:
public void accept(<?> obj))IntIntTConsumer <?> cAlmost identical to the one previous, this parameter acts as a User-Provided Consumer too. However, in addition to accepting the extracted 'T', String, JsonArrayorJsonObject, instances of this consumer must implement a method which also accepts both the array index and the success count (often these two numbers are the same).
Lambda Signature:
public void accept(int jaIndex, int acceptorCount, <?> obj)
The primary rationale for including an "extra" means for extracting data from an array is just to facilitate processing which may require information about where an object was located when it was extracted.
Hi-Lited Source-Code:This File's Source Code:
- View Here: Torello/JSON/RJArrIntoConsumer.java
- Open New Browser-Tab: Torello/JSON/RJArrIntoConsumer.java
File Size: 14,862 Bytes Line Count: 368 '\n' Characters Found
Internal Use Only: Settings-Record Helper:
- View Here: BASIC_TYPES.java
- Open New Browser-Tab: BASIC_TYPES.java
File Size: 28,899 Bytes Line Count: 621 '\n' Characters Found
Internal Use Only: Settings-Record Helper:
- View Here: EXTENDED_TYPES.java
- Open New Browser-Tab: EXTENDED_TYPES.java
File Size: 4,068 Bytes Line Count: 112 '\n' Characters Found
Internal Use Only: Settings-Record Helper:
- View Here: SETTINGS_REC_BUILDER.java
- Open New Browser-Tab: SETTINGS_REC_BUILDER.java
File Size: 9,477 Bytes Line Count: 218 '\n' Characters Found
Stateless Class:This class neither contains any program-state, nor can it be instantiated. The@StaticFunctionalAnnotation may also be called 'The Spaghetti Report'.Static-Functionalclasses are, essentially, C-Styled Files, without any constructors or non-static member fields. It is a concept very similar to the Java-Bean's@StatelessAnnotation.
- 1 Constructor(s), 1 declared private, zero-argument constructor
- 12 Method(s), 12 declared static
- 0 Field(s)
-
-
Method Summary
Read a JsonArraycontainingJsonObject's; send contents to a User-Provided Consumer<T>Modifier and Type Method static <T> voidobjArr(JsonArray ja, T defaultValue, int FLAGS, Class<T> consumerClass, Consumer<T> c)static <T> voidobjArr(JsonArray ja, T defaultValue, int FLAGS, Function<JsonObject,T> objBuilder, Class<T> consumerClass, Consumer<T> c)static <T> voidobjArr(JsonArray ja, T defaultValue, int FLAGS, IntIntTFunc<JsonObject,T> objBuilder, Class<T> consumerClass, Consumer<T> c)Read a JsonArraycontainingJsonObject's; send contents to a User-Provided IntIntTConsumer<T> (with Indices & Count)Modifier and Type Method static <T> voidobjArr2(JsonArray ja, T defaultValue, int FLAGS, Class<T> consumerClass, IntIntTConsumer<T> c)static <T> voidobjArr2(JsonArray ja, T defaultValue, int FLAGS, Function<JsonObject,T> objBuilder, Class<T> consumerClass, IntIntTConsumer<T> c)static <T> voidobjArr2(JsonArray ja, T defaultValue, int FLAGS, IntIntTFunc<JsonObject,T> objBuilder, Class<T> consumerClass, IntIntTConsumer<T> c)Read a JsonArraycontaining String's; send contents to a User-Provided String-Based ConsumerModifier and Type Method static voidstrArr(JsonArray ja, String defaultValue, int FLAGS, Consumer<String> c)static voidstrArr2(JsonArray ja, String defaultValue, int FLAGS, IntIntTConsumer<String> c)Read a JsonArraycontainingJsonObject's; send contents to a User-Provided JsonObject-Based ConsumerModifier and Type Method static voidjoArr(JsonArray ja, JsonObject defaultValue, int FLAGS, Consumer<JsonObject> c)static voidjoArr2(JsonArray ja, JsonObject defaultValue, int FLAGS, IntIntTConsumer<JsonObject> c)Read a JsonArraycontainingJsonArray's; send contents to a User-Provided JsonArray-Based ConsumerModifier and Type Method static voidjaArr(JsonArray ja, JsonArray defaultValue, int FLAGS, Consumer<JsonArray> c)static voidjaArr2(JsonArray ja, JsonArray defaultValue, int FLAGS, IntIntTConsumer<JsonArray> c)
-
-
-
Method Detail
-
objArr
public static <T> void objArr(JsonArray ja, T defaultValue, int FLAGS, java.lang.Class<T> consumerClass, java.util.function.Consumer<T> c)
Read a JsonArray of JsonObject's into a Consumer<T>, Class 'T' must have a JsonObject-ConstructorInvokes: Method ProcessJsonArray.objToJava(JsonArray, SettingsRec(T, Void)Passes: A pre-configured instance of SettingsRec<T, Void>to theProcessJsonArrayinvocation.Expects: Parameterized Type <T> to be a Java Class / Type that has a single-argument constructor which accepts a JsonObjectas a parameter.Sends: Generated Object is sent to the User-Provided Java Consumer<T>Parameter'c'- Code:
- Exact Method Body:
ProcessJsonArray.objToJava( ja, new SETTINGS_REC_BUILDER<T, Void> (defaultValue, FLAGS, null, EXTENDED_TYPES.FROM_CTOR(consumerClass)) .oneArgConsumer(c) );
-
objArr
public static <T> void objArr (JsonArray ja, T defaultValue, int FLAGS, java.util.function.Function<JsonObject,T> objBuilder, java.lang.Class<T> consumerClass, java.util.function.Consumer<T> c)
Read a JsonArray of JsonObject's into a Consumer<T>, Parameter 'objBuilder' must generate instances of 'T'Invokes: Method ProcessJsonArray.objToJava(JsonArray, SettingsRec(T, Void)Passes: A pre-configured instance of SettingsRec<T, Void>to theProcessJsonArrayinvocation.Expects: A Java Function<JsonObject, T>that will generate an instance of the expected output / emitted type<T>Type Note: Parameterized Type <T> may be any Java Object Class, not just a type which has a JsonObjectConstructor.Sends: Generated Object is sent to the User-Provided Java Consumer<T>Parameter'c'- Code:
- Exact Method Body:
ProcessJsonArray.objToJava( ja, new SETTINGS_REC_BUILDER<T, Void> (defaultValue, FLAGS, null, EXTENDED_TYPES.ONE_ARG_FUNC(objBuilder, consumerClass)) .oneArgConsumer(c) );
-
objArr
public static <T> void objArr(JsonArray ja, T defaultValue, int FLAGS, IntIntTFunc<JsonObject,T> objBuilder, java.lang.Class<T> consumerClass, java.util.function.Consumer<T> c)
Read a JsonArray of JsonObject's into a Consumer<T>, Parameter 'objBuilder' must generate instances of 'T'Invokes: Method ProcessJsonArray.objToJava(JsonArray, SettingsRec(T, Void)Passes: A pre-configured instance of SettingsRec<T, Void>to theProcessJsonArrayinvocation.Expects: An IntIntTFunc<JsonObject, T>that will generate an instance of the expected output / emitted type<T>Type Note: Parameterized Type <T> may be any Java Object Class, not just a type which has a JsonObjectConstructor.Sends: Generated Object is sent to the User-Provided Java Consumer<T>Parameter'c'- Code:
- Exact Method Body:
ProcessJsonArray.objToJava( ja, new SETTINGS_REC_BUILDER<T, Void> (defaultValue, FLAGS, null, EXTENDED_TYPES.THREE_ARG_FUNC(objBuilder, consumerClass)) .oneArgConsumer(c) );
-
objArr2
public static <T> void objArr2(JsonArray ja, T defaultValue, int FLAGS, java.lang.Class<T> consumerClass, IntIntTConsumer<T> c)
Read a JsonArray of JsonObject's into an IntIntTConsumer<T>, Class 'T' must have a JsonObject-ConstructorInvokes: Method ProcessJsonArray.objToJava(JsonArray, SettingsRec(T, Void)Passes: A pre-configured instance of SettingsRec<T, Void>to theProcessJsonArrayinvocation.Expects: Parameterized Type <T> to be a Java Class / Type that has a single-argument constructor which accepts a JsonObjectas a parameter.Sends: Generated Object is sent to the User-Provided Java-HTML IntIntTConsumer<T>Parameter'c'Consumer-Signature: (Functional Interface) void accept (int jsonArrIndex, int acceptCount, T obj)- Code:
- Exact Method Body:
ProcessJsonArray.objToJava( ja, new SETTINGS_REC_BUILDER<T, Void> (defaultValue, FLAGS, null, EXTENDED_TYPES.FROM_CTOR(consumerClass)) .threeArgConsumer(c) );
-
objArr2
public static <T> void objArr2 (JsonArray ja, T defaultValue, int FLAGS, java.util.function.Function<JsonObject,T> objBuilder, java.lang.Class<T> consumerClass, IntIntTConsumer<T> c)
Read a JsonArray of JsonObject's into an IntIntTConsumer<T>, Parameter 'objBuilder' must generate instances of 'T'Invokes: Method ProcessJsonArray.objToJava(JsonArray, SettingsRec(T, Void)Passes: A pre-configured instance of SettingsRec<T, Void>to theProcessJsonArrayinvocation.Expects: A Java Function<JsonObject, T>that will generate an instance of the expected output / emitted type<T>Type Note: Parameterized Type <T> may be any Java Object Class, not just a type which has a JsonObjectConstructor.Sends: Generated Object is sent to the User-Provided Java-HTML IntIntTConsumer<T>Parameter'c'Consumer-Signature: (Functional Interface) void accept (int jsonArrIndex, int acceptCount, T obj)- Code:
- Exact Method Body:
ProcessJsonArray.objToJava( ja, new SETTINGS_REC_BUILDER<T, Void> (defaultValue, FLAGS, null, EXTENDED_TYPES.ONE_ARG_FUNC(objBuilder, consumerClass)) .threeArgConsumer(c) );
-
objArr2
public static <T> void objArr2(JsonArray ja, T defaultValue, int FLAGS, IntIntTFunc<JsonObject,T> objBuilder, java.lang.Class<T> consumerClass, IntIntTConsumer<T> c)
Read a JsonArray of JsonObject's into an IntIntTConsumer<T>, Parameter 'objBuilder' must generate instances of 'T'Invokes: Method ProcessJsonArray.objToJava(JsonArray, SettingsRec(T, Void)Passes: A pre-configured instance of SettingsRec<T, Void>to theProcessJsonArrayinvocation.Expects: An IntIntTFunc<JsonObject, T>that will generate an instance of the expected output / emitted type<T>Type Note: Parameterized Type <T> may be any Java Object Class, not just a type which has a JsonObjectConstructor.Sends: Generated Object is sent to the User-Provided Java-HTML IntIntTConsumer<T>Parameter'c'Consumer-Signature: (Functional Interface) void accept (int jsonArrIndex, int acceptCount, T obj)- Code:
- Exact Method Body:
ProcessJsonArray.objToJava( ja, new SETTINGS_REC_BUILDER<T, Void> (defaultValue, FLAGS, null, EXTENDED_TYPES .THREE_ARG_FUNC(objBuilder, consumerClass)) .threeArgConsumer(c) );
-
strArr
public static void strArr(JsonArray ja, java.lang.String defaultValue, int FLAGS, java.util.function.Consumer<java.lang.String> c)
Reads a JsonArray of String's into a Java Consumer<String>Invokes: Method ProcessJsonArray.strToJava(JsonArray, SettingsRec(T, Void)Passes: A pre-configured instance of SettingsRec<String, Void>to theProcessJsonArrayinvocation.Sends: Retrieved / extracted Stringis sent to the User-Provided JavaConsumer<String>Parameter'c'- Code:
- Exact Method Body:
ProcessJsonArray.strToJava( ja, new SETTINGS_REC_BUILDER<String, Void> (defaultValue, FLAGS, null, BASIC_TYPES.STRING_REC()) .oneArgConsumer(c) );
-
strArr2
public static void strArr2(JsonArray ja, java.lang.String defaultValue, int FLAGS, IntIntTConsumer<java.lang.String> c)
Reads a JsonArray of String's into an IntIntTConsumer<String>Invokes: Method ProcessJsonArray.strToJava(JsonArray, SettingsRec(T, Void)Passes: A pre-configured instance of SettingsRec<String, Void>to theProcessJsonArrayinvocation.Sends: Retrieved / extracted Stringis sent to the User-Provided Java-HTMLIntIntTConsumer<String>Parameter'c'Consumer-Signature: (Functional Interface) void accept (int jsonArrIndex, int acceptCount, String str)- Code:
- Exact Method Body:
ProcessJsonArray.strToJava( ja, new SETTINGS_REC_BUILDER<String, Void> (defaultValue, FLAGS, null, BASIC_TYPES.STRING_REC()) .threeArgConsumer(c) );
-
joArr
public static void joArr(JsonArray ja, JsonObject defaultValue, int FLAGS, java.util.function.Consumer<JsonObject> c)
Reads a JsonArray of JsonObject's into a Java Consumer<JsonObject>Invokes: Method ProcessJsonArray.joToJava(JsonArray, SettingsRec(JsonArray, Void)Passes: A pre-configured instance of SettingsRec<JsonObject, Void>to theProcessJsonArrayinvocation.Sends: Retrieved / extracted JsonObjectis sent to the User-Provided JavaConsumer<JsonObject>Parameter'c'- Code:
- Exact Method Body:
ProcessJsonArray.joToJava( ja, new SETTINGS_REC_BUILDER<JsonObject, Void> (defaultValue, FLAGS, null, BASIC_TYPES.JSON_OBJECT_REC()) .oneArgConsumer(c) );
-
joArr2
public static void joArr2(JsonArray ja, JsonObject defaultValue, int FLAGS, IntIntTConsumer<JsonObject> c)
Reads a JsonArray of JsonObject's into an IntIntTConsumer<JsonObject>Invokes: Method ProcessJsonArray.joToJava(JsonArray, SettingsRec(JsonArray, Void)Passes: A pre-configured instance of SettingsRec<JsonObject, Void>to theProcessJsonObjectinvocation.Sends: Retrieved / extracted JsonObjectis sent to the User-Provided Java-HTMLIntIntTConsumer<JsonObject>Parameter'c'Consumer-Signature: (Functional Interface) void accept (int jsonArrIndex, int acceptCount, JsonObject jo)- Code:
- Exact Method Body:
ProcessJsonArray.joToJava( ja, new SETTINGS_REC_BUILDER<JsonObject, Void> (defaultValue, FLAGS, null, BASIC_TYPES.JSON_OBJECT_REC()) .threeArgConsumer(c) );
-
jaArr
public static void jaArr(JsonArray ja, JsonArray defaultValue, int FLAGS, java.util.function.Consumer<JsonArray> c)
Reads a JsonArray of JsonObject's into a Java Consumer<JsonArray>Invokes: Method ProcessJsonArray.jaToJava(JsonArray, SettingsRec(JsonArray, Void)Passes: A pre-configured instance of SettingsRec<JsonArray, Void>to theProcessJsonArrayinvocation.Sends: Retrieved / extracted JsonArrayis sent to the User-Provided JavaConsumer<JsonArray>Parameter'c'- Code:
- Exact Method Body:
ProcessJsonArray.jaToJava( ja, new SETTINGS_REC_BUILDER<JsonArray, Void> (defaultValue, FLAGS, null, BASIC_TYPES.JSON_ARRAY_REC()) .oneArgConsumer(c) );
-
jaArr2
public static void jaArr2(JsonArray ja, JsonArray defaultValue, int FLAGS, IntIntTConsumer<JsonArray> c)
Reads a JsonArray of JsonArray's into an IntIntTConsumer<JsonArray>Invokes: Method ProcessJsonArray.jaToJava(JsonArray, SettingsRec(JsonArray, Void)Passes: A pre-configured instance of SettingsRec<JsonArray, Void>to theProcessJsonArrayinvocation.Sends: Retrieved / extracted JsonArrayis sent to the User-Provided Java-HTMLIntIntTConsumer<JsonArray>Parameter'c'Consumer-Signature: (Functional Interface) void accept (int jsonArrIndex, int acceptCount, JsonArray ja)- Code:
- Exact Method Body:
ProcessJsonArray.jaToJava( ja, new SETTINGS_REC_BUILDER<JsonArray, Void> (defaultValue, FLAGS, null, BASIC_TYPES.JSON_ARRAY_REC()) .threeArgConsumer(c) );
-
-