Package Torello.Java.JSON
Class ReadArrJSON
- java.lang.Object
-
- Torello.Java.JSON.ReadArrJSON
-
public class ReadArrJSON extends java.lang.Object
JSON Binding Helper-Class:
JSON-Binding is the art of converting data that has been stored, saved or transmitted usingJava-Script Object Notation
into a Primitive-Type or Object-Type of any Programming Language, for instance Java.JSON
often arrives into Java-Program Memory from an external Internet Connection, and may have traveled hundreds or even thousands of miles from a Host-Server.
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 is never completely assured.
Being able to handle changes that might be made to an API (possibly from great distances away, and without the Software-Manager's consent) is the type of feature that robust JSON-Code simply has to offer.
Binding-Helper Features:- 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
- Primary Helper-Classes for the (Experimental) Google-Chrome
Headless Browser
Package
Utilities for parsing Json Array's into Java Array's.
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 tool included in the J2EE is available on GitHub, and that is the one used by the Java HTML JAR Library. (See:javax.json.*
)
Primary Classes Used:JsonArray
andJsonObject
The methods in this class simplify converting aJsonArray
into one of the following types:- Simple Primitive-Array, such as:
int[], double[]
orboolean[]
- Primitive-Stream (Java provides precisely three:
IntStream, LongStream
andDoubleStream
) - Stream of Boxed-Primitives:
Stream<Integer>
,Stream<Boolean>
etc...
Below are some examples ofJsonArray's
. These arrays are perfectly legal in JSON, but present some problems to Java Programmers. A simple one line invocation follows in the example after this, for parsing these JSON-Arrays into Java-Arrays - without throwing exceptions:
Java Script Object Notation (JSON):
{ "intArr" : [1, 2, 3, 4, 5], "floatArr" : [1.1, 2.2, 3.3, 4.4, 5.5], "intArrSurprise": ["1", 2, "3", null, 5], "floatArrSurprise": ["1.1", 2.2, "3.3", true, 5.5] }
In the example code, below, the above JSON is parsed into Java-Arrays:
Example:
String json = Scrape.scrapePage(new URL("http://some.url.com/some/REST/endpoint")); StringReader sr = new StringReader(json); JsonObject jo = Json.createReader(sr).readObject(); // Simple Parse, without any problems int[] intArr = ReadArrJSON.intArrToStream (jo.getJsonArray("intArr"), -1, 0, null).toArray(); // Simple Parse, without any problems float[] floatArr = ReadArrJSON.floatArray (jo.getJsonArray("floatArr"), -1, 0 null); // Flag RETURN_PARSE_ON_STR ==> Attempt to Parse String's into Integer's // Flag RETURN_DEFVAL_ON_NULL ==> Insert a "4" if there are any nulls in the array int[] intSurprise = ReadArrJSON.intArrToStream( jo.getJsonArray("intArrSurprise"), 4, RETURN_PARSE_ON_STR | RETURN_DEFVAL_ON_NULL, null ) .toArray(); // Flag RETURN_PARSE_ON_STR ==> Attempt to Parse String's into Float's // Flag RETURN_DEFVAL_ON_WRONG_JSONTYPE ==> Insert a "4.4" if there are any problematic types float[] floatSurprise = ReadArrJSON.floatArray( jo.getJsonArray("floatArrSurprise"), 4.4, RETURN_PARSE_ON_STR | RETURN_DEFVAL_ON_WRONG_JSONTYPE, null );
There are also methods provided in this class for generating aStream<String>
or aStream<Object>
.
Boxed-Primitives & The Java Stream API
Anyjava.util.stream.Stream
may be easily converted into other types. The example below shows how to convert aStream<T>
into other types of containers, and it is hopefully obvious that'T'
should be substituted by any of the Boxed-Primitive Type's which are used in this class:Conversion-Target Stream-Method Invocation T[]
Stream.toArray(T[]::new);
List<T>
Stream.collect(Collectors.toList());
Vector<T>
Stream.collect(Collectors.toCollection(Vector::new));
TreeSet<T>
Stream.collect(Collectors.toCollection(TreeSet::new));
Iterator<T>
Stream.iterator();
Hi-Lited Source-Code:- View Here: Torello/Java/JSON/ReadArrJSON.java
- Open New Browser-Tab: Torello/Java/JSON/ReadArrJSON.java
File Size: 111,300 Bytes Line Count: 2,418 '\n' Characters Found
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
- 30 Method(s), 30 declared static
- 7 Field(s), 7 declared static, 7 declared final
- Utilizes the Java-Standard
-
-
Nested Class Summary
Nested Classes Modifier and Type Class static class
ReadArrJSON.DimN
-
Field Summary
Fields Modifier and Type Field static int
NOT_ALLOWED_RET_NULL_MASKS
-
Method Summary
Stream<String>
: Read aJsonArray
into a JavaStream<String>
Modifier and Type Method static Stream<String>
strArrayToStream(JsonArray ja, String defaultValue, int FLAGS)
Stream<Object>
: Read aJsonArray
into a JavaStream<Object>
Modifier and Type Method static <T> Stream<T>
objArrayToStream(JsonArray ja, T defaultValue, int FLAGS, Class<T> returnClass)
Primitive-Stream: Read a JsonArray
to a Primitive-Stream (easily converts to an array)Modifier and Type Method static DoubleStream
doubleArrayToStream(JsonArray ja, double defaultValue, int FLAGS, ToDoubleFunction<String> optionalUserParser)
static IntStream
intArrayToStream(JsonArray ja, int defaultValue, int FLAGS, ToIntFunction<String> optionalUserParser)
static LongStream
longArrayToStream(JsonArray ja, long defaultValue, int FLAGS, ToLongFunction<String> optionalUserParser)
Boxed-Primitive Stream: Read a JsonArray
into a Java Stream of Boxed-TypesModifier and Type Method static Stream<Boolean>
booleanArrayToBoxedStream(JsonArray ja, boolean defaultValue, int FLAGS, Function<String,Boolean> optionalUserParser)
static Stream<Byte>
byteArrayToBoxedStream(JsonArray ja, byte defaultValue, int FLAGS, Function<String,Byte> optionalUserParser)
static Stream<Double>
doubleArrayToBoxedStream(JsonArray ja, double defaultValue, int FLAGS, Function<String,Double> optionalUserParser)
static Stream<Float>
floatArrayToBoxedStream(JsonArray ja, float defaultValue, int FLAGS, Function<String,Float> optionalUserParser)
static Stream<Integer>
integerArrayToBoxedStream(JsonArray ja, int defaultValue, int FLAGS, Function<String,Integer> optionalUserParser)
static Stream<Long>
longArrayToBoxedStream(JsonArray ja, long defaultValue, int FLAGS, Function<String,Long> optionalUserParser)
static Stream<Number>
numberArrayToBoxedStream(JsonArray ja, Number defaultValue, int FLAGS, Function<String,Number> optionalUserParser)
static Stream<Short>
shortArrayToBoxedStream(JsonArray ja, short defaultValue, int FLAGS, Function<String,Short> optionalUserParser)
Primitive-Array: Read a JsonArray
into a Simple Java (Un-Boxed) Primitive-ArrayModifier and Type Method static boolean[]
booleanArray(JsonArray ja, boolean defaultValue, int FLAGS, Predicate<String> optionalUserParser)
static byte[]
byteArray(JsonArray ja, byte defaultValue, int FLAGS, ToByteFunction<String> optionalUserParser)
static float[]
floatArray(JsonArray ja, float defaultValue, int FLAGS, ToFloatFunction<String> optionalUserParser)
static short[]
shortArray(JsonArray ja, short defaultValue, int FLAGS, ToShortFunction<String> optionalUserParser)
Protected, Internal Methods Modifier and Type Method protected static <T,U>
UboxedStreamToPrimitiveArray(Stream<T> boxedStream, JsonArray ja, Class<T> returnClass, ObjIntConsumer<T> arrayBuilder, U retArr)
protected static <T,U>
UjsonArrToBoxedStream(JsonArray ja, Torello.Java.JSON.ReadArrJSON.RECORD<T,U> rec)
protected static <T,U>
UjsonArrToStream(JsonArray ja, Torello.Java.JSON.ReadArrJSON.RECORD<T,U> rec)
-
-
-
Field Detail
-
NOT_ALLOWED_RET_NULL_MASKS
public static final int NOT_ALLOWED_RET_NULL_MASKS
These are eliminated / removed from any call to a method which returns either an array of Java Primitives or a Java Primitive Stream- See Also:
intArrayToStream(JsonArray, int, int, ToIntFunction)
,longArrayToStream(JsonArray, long, int, ToLongFunction)
,doubleArrayToStream(JsonArray, double, int, ToDoubleFunction)
,byteArray(JsonArray, byte, int, ToByteFunction)
,floatArray(JsonArray, float, int, ToFloatFunction)
,shortArray(JsonArray, short, int, ToShortFunction)
,booleanArray(JsonArray, boolean, int, Predicate)
, Constant Field Values- Code:
- Exact Field Declaration Expression:
public static final int NOT_ALLOWED_RET_NULL_MASKS = ~(RETURN_NULL_ON_ANY_ALL | RETURN_NULL_ON_AEX | RETURN_NULL_ON_NULL | RETURN_NULL_ON_SPEX | RETURN_NULL_ON_STR | RETURN_NULL_ON_0LEN_STR | RETURN_NULL_ON_WRONG_JSONTYPE | INSERT_NULL_ON_NON_SUBARRAY_TYPE);
-
-
Method Detail
-
jsonArrToStream
protected static <T,U> U jsonArrToStream (JsonArray ja, Torello.Java.JSON.ReadArrJSON.RECORD<T,U> rec)
Builds a Primitive-Stream from aJsonArray
- Parameters:
ja
- TheJsonArray
rec
- The configurations- Throws:
JsonNullPrimitiveArrException
- If the are any Json-Null values contained by the input array, and none of these Exception-Flags are set:RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)SKIP_ON_NULL
(Abbrev:S_N
)
RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)SKIP_ON_ANY_ALL
(Abbrev:S_AA
)
JsonArithmeticArrException
- This exception will throw if the number-converstion function throws anArithmeticException
while attempting to convert the Json-Type into a Java-Type, and none of these Exception-Flags are set:RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RD_AEX
)SKIP_ON_AEX
(Abbrev:S_AEX
)
RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)SKIP_ON_ANY_ALL
(Abbrev:S_AA
)
JsonStrParseArrException
- This Exception Throws If:- Any of the
JsonArray
elements contain aJsonString
- The flag
RETURN_PARSE_ON_STR
(Abbrev:RP_S
) has been set! - A Parsing-Exception is thrown while attempting to Parse the
String
This exception will throw, If None of the Flags Listed Below are Set:RETURN_DEFVAL_ON_SPEX
(Abbrev:RD_SPEX
)SKIP_ON_SPEX
(Abbrev:S_SPEX
)
RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)SKIP_ON_ANY_ALL
(Abbrev:S_AA
)
If theString
is a zero-lengthString
, these flags will help:RETURN_DEFVAL_ON_0LEN_STR
(Abbrev:RD_ZLS
)SKIP_ON_0LEN_STR
(Abbrev:S_ZLS
)
- Any of the
JsonTypeArrException
- If theJsonArray
contains elements that are not the appropriate type, and none of these Exception-Flags are set:RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)SKIP_ON_WRONG_JSONTYPE
(Abbrev:S_WT
)
RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)SKIP_ON_ANY_ALL
(Abbrev:S_AA
)
If there are anyString's
in the array, then these flags will help:RETURN_PARSE_ON_STR
(Abbrev:RP_S
)RETURN_DEFVAL_ON_STR
(Abbrev:RD_S
)SKIP_ON_STR
(Abbrev:S_S
)
- Code:
- Exact Method Body:
rec.ja = ja; rec.c.run(); // Construct a new Stream-Builder int SIZE = ja.size(); JsonValue jv; for (int i=0; i < SIZE; i++) switch ((jv = ja.get(i)).getValueType()) { // javax.json.JsonValue.ValueType.NULL case NULL: if (rec.handlerNull != null) rec.handlerNull.run(); else throw new JsonNullPrimitiveArrException(ja, i, NUMBER, rec.CLASS); break; // javax.json.JsonValue.ValueType.NUMBER case NUMBER: JsonNumber jn = (JsonNumber) jv; // Guaranteed to work properly ==> This is the "definition of RJA_AEX" // // "numberConverter" is just Number.intValue, longValue, doubleValue // Number.intValue(), longValue(), doubleValue() **DO NOT** throw any // exceptions. They round (if necessary), or return MAX_INT, MAX_LONG, etc... // (also, only if necessary) if (rec.RJA_AEX || rec.jsonNumWillFit.test(jn)) rec.a.accept(rec.numberConverter.apply(jn.numberValue())); else if (rec.handlerAEX != null) rec.handlerAEX.run(); else throw new JsonArithmeticArrException (getAEX(jn, rec.numberConverterExThrow), ja, i, NUMBER, jv, rec.CLASS); break; // javax.json.JsonValue.ValueType.STRING case STRING: rec.handlerJsonString.accept(jv, i); break; // javax.json.JsonValue.ValueType.TRUE, FALSE, ARRAY, OBJECT default: if (rec.handlerWrongType != null) rec.handlerWrongType.run(); else throw new JsonTypeArrException(ja, i, NUMBER, jv, rec.CLASS); } // Run Stream.Builder.build(); return rec.b.get();
-
intArrayToStream
public static java.util.stream.IntStream intArrayToStream (JsonArray ja, int defaultValue, int FLAGS, java.util.function.ToIntFunction<java.lang.String> optionalUserParser)
Convenience Method
Invokes:jsonArrToStream(JsonArray, RECORD)
Passes: JavaIntStream
Configurtion-Record
Removes: AnyJFlag
masks which might insert null-entries in the return-stream
For information about theJFlag
parameter, click the method link above.
Producing a standard javaint[]
array from anIntStream
is a trivial-converstion:
Java Line of Code:
int[] arr = intArrayToStream(ja, -1, 0, null).toArray();
-
longArrayToStream
public static java.util.stream.LongStream longArrayToStream (JsonArray ja, long defaultValue, int FLAGS, java.util.function.ToLongFunction<java.lang.String> optionalUserParser)
Convenience Method
Invokes:jsonArrToStream(JsonArray, RECORD)
Passes: JavaLongStream
Configurtion-Record
Removes: AnyJFlag
masks which might insert null-entries in the return-stream
For information about theJFlag
parameter, click the method link above.
Producing a standard javalong[]
array from aLongStream
is a trivial-converstion:
Java Line of Code:
long[] arr = longArrayToStream(ja, -1, 0, null).toArray();
-
doubleArrayToStream
public static java.util.stream.DoubleStream doubleArrayToStream (JsonArray ja, double defaultValue, int FLAGS, java.util.function.ToDoubleFunction<java.lang.String> optionalUserParser)
Convenience Method
Invokes:jsonArrToStream(JsonArray, RECORD)
Passes: JavaDoubleStream
Configurtion-Record
Removes: AnyJFlag
masks which might insert null-entries in the return-stream
For information about theJFlag
parameter, click the method link above.
Producing a standard javadouble[]
array from aDoubleStream
is a trivial-converstion:
Java Line of Code:
double[] arr = doubleArrayToStream(ja, -1, 0, null).toArray();
-
jsonArrToBoxedStream
protected static <T,U> U jsonArrToBoxedStream (JsonArray ja, Torello.Java.JSON.ReadArrJSON.RECORD<T,U> rec)
Builds a Stream of Boxed-Type Numbers from aJsonArray
- Parameters:
rec
- The configurations- Throws:
JsonNullPrimitiveArrException
- If there are any Json-Null values contained by the input array, and none of the following Exception-Flags are set:SKIP_ON_NULL
(Abbrev:S_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)
RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)SKIP_ON_ANY_ALL
(Abbrev:S_AA
)
JsonArithmeticArrException
- This exception will throw if the number-converstion function throws anArithmeticException
while attempting to convert the Json-Type into a Java-Type, and none of the following Exception-Flags are set:RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RD_AEX
)SKIP_ON_AEX
(Abbrev:S_AEX
)
RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)SKIP_ON_ANY_ALL
(Abbrev:S_AA
)
JsonStrParseArrException
- This Exception Throws If:- Any of the
JsonArray
elements contain aJsonString
- The flag
RETURN_PARSE_ON_STR
(Abbrev:RP_S
) has been set! - A Parsing-Exception is thrown while attempting to Parse the
String
This exception will throw, If None of the Flags Listed Below are Set:SKIP_ON_SPEX
(Abbrev:S_SPEX
)RETURN_NULL_ON_SPEX
(Abbrev:RN_SPEX
)RETURN_DEFVAL_ON_SPEX
(Abbrev:RD_SPEX
)
RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
If theString
is a zero-lengthString
, these flags will help:RETURN_NULL_ON_0LEN_STR
(Abbrev:RN_ZLS
)RETURN_DEFVAL_ON_0LEN_STR
(Abbrev:RD_ZLS
)SKIP_ON_0LEN_STR
(Abbrev:S_ZLS
)
- Any of the
JsonTypeArrException
- If theJsonArray
contains elements that are not the appropriate type, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)SKIP_ON_WRONG_JSONTYPE
(Abbrev:S_WT
)
RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)SKIP_ON_ANY_ALL
(Abbrev:S_AA
)
If there are anyString's
in the array, then these flags will help:RETURN_PARSE_ON_STR
(Abbrev:RP_S
)RETURN_NULL_ON_STR
(Abbrev:RN_S
)RETURN_DEFVAL_ON_STR
(Abbrev:RD_S
)SKIP_ON_STR
(Abbrev:S_S
)
- Code:
- Exact Method Body:
rec.c.run(); // Construct a new Stream-Builder rec.ja = ja; int SIZE = ja.size(); JsonValue jv; for (int i=0; i < SIZE; i++) switch ((jv = ja.get(i)).getValueType()) { // javax.json.JsonValue.ValueType.NULL case NULL: rec.handlerNull.run(); break; // javax.json.JsonValue.ValueType.NUMBER case NUMBER: JsonNumber jn = (JsonNumber) jv; // Guaranteed to work properly ==> This is the "definition of RJA_AEX" // // Number.intValue(), longValue(), doubleValue() **DO NOT** throw any // exceptions. They round (if necessary), or return MAX_INT, MAX_LONG, etc... // (also, only if necessary) if (rec.RJA_AEX || rec.jsonNumWillFit.test(jn)) rec.a.accept(rec.numberConverter.apply(jn.numberValue())); else if (rec.handlerAEX != null) rec.handlerAEX.run(); else throw new JsonArithmeticArrException (getAEX(jn, rec.numberConverterExThrow), ja, i, NUMBER, jv, rec.CLASS); break; // javax.json.JsonValue.ValueType.STRING case STRING: rec.handlerJsonString.accept(jv, i); break; // OBJECT, ARRAY, TRUE, FALSE default: if (rec.handlerWrongType != null) rec.handlerWrongType.run(); else throw new JsonTypeArrException(ja, i, NUMBER, jv, rec.CLASS); } // Run Stream.Builder.build() return rec.b.get();
-
boxedStreamToPrimitiveArray
protected static <T,U> U boxedStreamToPrimitiveArray (java.util.stream.Stream<T> boxedStream, JsonArray ja, java.lang.Class<T> returnClass, java.util.function.ObjIntConsumer<T> arrayBuilder, U retArr)
Converts a Java Boxed Stream to a Primitive Array.- Type Parameters:
T
- The generic-type of thejava.util.stream.Stream
U
- The returned primitive-array type- Parameters:
boxedStream
- A Java Boxed StreamarrayBuilder
- A consumer which inserts a Boxed-Number into the Primitive-Arrayja
- TheJsonArray
that generated the Boxed-Stream. Needed for error-outputreturnClass
- Thejava.lang.Class
(generic-type parameter) for theStream
. In other words, theClass
for type-parameter<T>
. This is only needed if there is an exception, for proper & readable error-reporting.- Code:
- Exact Method Body:
// Since the array-index is used inside of a LAMBDA, this "EffectivelyFinal" counter // class becomes necessary. Counter counter = new Counter(-1); // Don't forget, Stream's are not guaranteed to be processed in order, unless an // explicit request is made using something like 'forEachOrdered' boxedStream.forEachOrdered((T boxedPrimitive) -> { int i = counter.addOne(); // "Final, or Effectively Final" // A Primitive-Array (like int[], long[], boolean[] etc...) may not have null if (boxedPrimitive == null) throw new JsonNullPrimitiveArrException(ja, i, NUMBER, returnClass); // The 'arrayBuilder' just assigns the value to the array[i] arrayBuilder.accept(boxedPrimitive, i); }); return retArr;
-
shortArray
public static short[] shortArray (JsonArray ja, short defaultValue, int FLAGS, ToShortFunction<java.lang.String> optionalUserParser)
Convenience Method
Invokes:shortArrayToBoxedStream(JsonArray, short, int, Function)
And:boxedStreamToPrimitiveArray(Stream, JsonArray, Class, ObjIntConsumer, Object)
Converts:JsonArray
toStream<Short>
and then toshort[]
Removes: AnyJFlag
masks which might insert null-entries in the return-array
For information about usingFLAGS
, see:jsonArrToBoxedStream(JsonArray, RECORD)
-
byteArray
public static byte[] byteArray (JsonArray ja, byte defaultValue, int FLAGS, ToByteFunction<java.lang.String> optionalUserParser)
Convenience Method
Invokes:byteArrayToBoxedStream(JsonArray, byte, int, Function)
And:boxedStreamToPrimitiveArray(Stream, JsonArray, Class, ObjIntConsumer, Object)
Converts:JsonArray
toStream<Byte>
and then tobyte[]
Removes: AnyJFlag
masks which might insert null-entries in the return-array
For information about usingFLAGS
, see:jsonArrToBoxedStream(JsonArray, RECORD)
-
floatArray
public static float[] floatArray (JsonArray ja, float defaultValue, int FLAGS, ToFloatFunction<java.lang.String> optionalUserParser)
Convenience Method
Invokes:floatArrayToBoxedStream(JsonArray, float, int, Function)
And:boxedStreamToPrimitiveArray(Stream, JsonArray, Class, ObjIntConsumer, Object)
Converts:JsonArray
toStream<Float>
and then tofloat[]
Removes: AnyJFlag
masks which might insert null-entries in the return-array
For information about usingFLAGS
, see:jsonArrToBoxedStream(JsonArray, RECORD)
-
booleanArray
public static boolean[] booleanArray (JsonArray ja, boolean defaultValue, int FLAGS, java.util.function.Predicate<java.lang.String> optionalUserParser)
Convenience Method
Invokes:booleanArrayToBoxedStream(JsonArray, boolean, int, Function)
And:boxedStreamToPrimitiveArray(Stream, JsonArray, Class, ObjIntConsumer, Object)
Converts:JsonArray
toStream<Boolean>
and then toboolean[]
Removes: AnyJFlag
masks which might insert null-entries in the return-array
For information about usingFLAGS
, see:jsonArrToBoxedStream(JsonArray, RECORD)
-
integerArrayToBoxedStream
public static java.util.stream.Stream<java.lang.Integer> integerArrayToBoxedStream (JsonArray ja, int defaultValue, int FLAGS, java.util.function.Function<java.lang.String,java.lang.Integer> optionalUserParser)
Convenience Method
Invokes:jsonArrToBoxedStream(JsonArray, RECORD)
Fills in Configuration Record for Integer-Retrieval
For more Information about theJFlag
Parameter, Click the Method-Link above.
Producing a Boxed-Integer Array from this Method is as Follows:
Java Line of Code:
Integer[] arr = integerArrayToBoxedStream(ja, -1, 0 null).toArray(Integer[]::new);
-
shortArrayToBoxedStream
public static java.util.stream.Stream<java.lang.Short> shortArrayToBoxedStream (JsonArray ja, short defaultValue, int FLAGS, java.util.function.Function<java.lang.String,java.lang.Short> optionalUserParser)
Convenience Method
Invokes:jsonArrToBoxedStream(JsonArray, RECORD)
Fills in Configuration Record for Short-Retrieval
For more Information about theJFlag
Parameter, Click the Method-Link above.
Producing a Boxed-Short Array from this Method is as Follows:
Java Line of Code:
Short[] arr = shortArrayToBoxedStream(ja, -1, 0 null).toArray(Short[]::new);
-
byteArrayToBoxedStream
public static java.util.stream.Stream<java.lang.Byte> byteArrayToBoxedStream (JsonArray ja, byte defaultValue, int FLAGS, java.util.function.Function<java.lang.String,java.lang.Byte> optionalUserParser)
Convenience Method
Invokes:jsonArrToBoxedStream(JsonArray, RECORD)
Fills in Configuration Record for Byte-Retrieval
For more Information about theJFlag
Parameter, Click the Method-Link above.
Producing a Boxed-Byte Array from this Method is as Follows:
Java Line of Code:
Byte[] arr = byteArrayToBoxedStream(ja, -1, 0 null).toArray(Byte[]::new);
-
longArrayToBoxedStream
public static java.util.stream.Stream<java.lang.Long> longArrayToBoxedStream (JsonArray ja, long defaultValue, int FLAGS, java.util.function.Function<java.lang.String,java.lang.Long> optionalUserParser)
Convenience Method
Invokes:jsonArrToBoxedStream(JsonArray, RECORD)
Fills in Configuration Record for Long-Retrieval
For more Information about theJFlag
Parameter, Click the Method-Link above.
Producing a Boxed-Long Array from this Method is as Follows:
Java Line of Code:
Long[] arr = longArrayToBoxedStream(ja, -1, 0 null).toArray(Long[]::new);
-
doubleArrayToBoxedStream
public static java.util.stream.Stream<java.lang.Double> doubleArrayToBoxedStream (JsonArray ja, double defaultValue, int FLAGS, java.util.function.Function<java.lang.String,java.lang.Double> optionalUserParser)
Convenience Method
Invokes:jsonArrToBoxedStream(JsonArray, RECORD)
Fills in Configuration Record for Double-Retrieval
For more Information about theJFlag
Parameter, Click the Method-Link above.
Producing a Boxed-Double Array from this Method is as Follows:
Java Line of Code:
Double[] arr = doubleArrayToBoxedStream(ja, -1, 0 null).toArray(Double[]::new);
-
floatArrayToBoxedStream
public static java.util.stream.Stream<java.lang.Float> floatArrayToBoxedStream (JsonArray ja, float defaultValue, int FLAGS, java.util.function.Function<java.lang.String,java.lang.Float> optionalUserParser)
Convenience Method
Invokes:jsonArrToBoxedStream(JsonArray, RECORD)
Fills in Configuration Record for Float-Retrieval
For more Information about theJFlag
Parameter, Click the Method-Link above.
Producing a Boxed-Float Array from this Method is as Follows:
Java Line of Code:
Float[] arr = floatArrayToBoxedStream(ja, -1, 0 null).toArray(Float[]::new);
-
numberArrayToBoxedStream
public static java.util.stream.Stream<java.lang.Number> numberArrayToBoxedStream (JsonArray ja, java.lang.Number defaultValue, int FLAGS, java.util.function.Function<java.lang.String,java.lang.Number> optionalUserParser)
Convenience Method
Invokes:jsonArrToBoxedStream(JsonArray, RECORD)
Fills in Configuration Record for Number-Retrieval
For more Information about theJFlag
Parameter, Click the Method-Link above.
Producing a Boxed-Number Array from this Method is as Follows:
Java Line of Code:
Number[] arr = numberArrayToBoxedStream(ja, -1, 0 null).toArray(Number[]::new);
-
strArrayToStream
public static java.util.stream.Stream<java.lang.String> strArrayToStream (JsonArray ja, java.lang.String defaultValue, int FLAGS)
Converts aJsonArray
into aStream<String>
. A JavaStream<String>
is easily converted to aString[]
-Array via a call to:
Java Line of Code:
String[] sArr = strArrayToStream(myJsonArray, null, 0).toArray(String[]::new);
- Parameters:
ja
- AnyJsonArray
, but preferrably one which contains instances ofJsonString
defaultValue
- When used in conjunction with'FLAGS'
, this default-value may be inserted into the output-array when error cases occur at particular array-index locations.FLAGS
- Optional flags. SeeJFlag
for details.- Returns:
- A Java
Stream<String>
. - Code:
- Exact Method Body:
return strArrayToStreamINTERNAL(ja, RECORD.getStringStreamRec(defaultValue, FLAGS));
-
booleanArrayToBoxedStream
public static java.util.stream.Stream<java.lang.Boolean> booleanArrayToBoxedStream (JsonArray ja, boolean defaultValue, int FLAGS, java.util.function.Function<java.lang.String,java.lang.Boolean> optionalUserParser)
Converts aJsonArray
into aStream<Boolean>
. A JavaStream<Boolean>
is easily converted to aBoolean[]
-Array via a call to the lines below. Note that in this example, the return array is an array ofBoolean
objects; it is not a primitiveboolean[]
array. (To parse into a primitive array, usebooleanArray(JsonArray, boolean, int, Predicate)
)
Java Line of Code:
Boolean[] bArr = booleanArrayToBoxedStream(myJsonArray, false, 0, null) .toArray(Boolean[]::new);
- Parameters:
ja
- AnyJsonArray
, but preferrably one which contains instances ofJsonValue.TRUE
orJsonValue.FALSE
defaultValue
- When used in conjunction with'FLAGS'
, this default-value may be inserted into the output-array when error cases occur at particular array-index locations.FLAGS
- Optional flags. SeeJFlag
for details.optionalUserParser
- If the appropriate flags are set, if aJsonString
is encountered while processing theJsonArray
, an attempt to parse theString
into aBoolean
will be made using thisString
-parser.- Returns:
- A Java
Stream<Boolean>
. - Code:
- Exact Method Body:
return booleanArrayToBoxedStreamINTERNAL( ja, RECORD.getBOOLEANStreamRec (defaultValue, FLAGS, optionalUserParser, false) );
-
objArrayToStream
public static <T> java.util.stream.Stream<T> objArrayToStream (JsonArray ja, T defaultValue, int FLAGS, java.lang.Class<T> returnClass)
Converts aJsonArray
into aStream<T>
. It is important to remember that the packageTorello.Java.JSON
does not attempt to generate Java-Type's or their Constructors. The 'Type' of theStream<T>
that is returned by this method (the value of'T'
) must be an Object-Class that contains a Constructor that accepts aJsonObject
as input.
This package does not perform the type of Auto-Type-Generation that is done by Lombok or Jackson.
Note that a JavaStream<T>
is easily converted to aT[]
-Array via a call to the line below.
Java Line of Code:
MyClass[] bArr = objArrayToStream(myJsonArray, null, 0, MyClass.class) .toArray(MyClass[]::new);
- Type Parameters:
T
- This is the 'type' of the array being built. If there were a class, for example, named'Automobile'
, the value passed to parameter'returnClass'
would simply beAutomobile.class
.- Parameters:
ja
- AnyJsonArray
, but preferrably one which contains instances of the class that has been specified by Type-Parameter'T'
defaultValue
- When used in conjunction with'FLAGS'
, this default-value may be inserted into the output-array when error-cases occur while interpreting the array contents.FLAGS
- Optional flags. SeeJFlag
for details.returnClass
- Thejava.lang.Class
of the returned / outputStream
-Type currently being constructed & filled.- Returns:
- A Java
Stream<T>
. - Code:
- Exact Method Body:
return objArrayToStreamINTERNAL (ja, RECORD.getObjectStreamRec(defaultValue, FLAGS, returnClass));
-
-