Package Torello.Java.JSON
Class ReadBoxedJSON
- java.lang.Object
-
- Torello.Java.JSON.ReadBoxedJSON
-
public class ReadBoxedJSON 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
Builds on the J2EE Standard Release JSON Parsing Tools by providing additional help with converting JSON Data into Java Boxed-Primitive Types
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
Note that this class simply doesn't support any methods for reading ajava.lang.Character
. This is because their are just entirely too many possibilities, combinations & options for the appropriate "Default Behavior" when attempting to read a single character of data from any random JSON Data-Type.
Remember that the JSON Specification does not posses any "Single-JSON-Character" Data-Type. Creating some kind of Flag-Controlled Reader for a'char'
that can capably decide what to do would be so overtly verbose, for something so simple, that it isn't worth the effort...- See Also:
Json
,JsonObject
,JsonArray
Hi-Lited Source-Code:- View Here: Torello/Java/JSON/ReadBoxedJSON.java
- Open New Browser-Tab: Torello/Java/JSON/ReadBoxedJSON.java
File Size: 109,276 Bytes Line Count: 2,099 '\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
- 42 Method(s), 42 declared static
- 0 Field(s)
- Utilizes the Java-Standard
-
-
Nested Class Summary
Nested Classes Modifier and Type Class static class
ReadBoxedJSON.XL
-
Method Summary
Integer: Retrieve a JsonNumber
from aJsonArray
, and transform it to a Java Boxed-IntegerModifier and Type Method static Integer
getInteger(JsonArray ja, int index)
static Integer
getInteger(JsonArray ja, int index, int FLAGS, int defaultValue)
Integer: Retrieve a JsonNumber
from aJsonObject
, and transform it to a Java Boxed-IntegerModifier and Type Method static Integer
getInteger(JsonObject jo, String propertyName, boolean isOptional)
static Integer
getInteger(JsonObject jo, String propertyName, int FLAGS, int defaultValue)
Integer: Retrieve a JsonString
from aJsonArray
orJsonObject
, and parse it to a Boxed-IntegerModifier and Type Method static Integer
parseInteger(JsonArray ja, int index, int FLAGS, int defaultValue, Function<String,Integer> optionalParser)
static Integer
parseInteger(JsonObject jo, String propertyName, int FLAGS, int defaultValue, Function<String,Integer> optionalParser)
Long: Retrieve a JsonNumber
from aJsonArray
, and transform it to a Java Boxed-LongModifier and Type Method static Long
getLong(JsonArray ja, int index)
static Long
getLong(JsonArray ja, int index, int FLAGS, long defaultValue)
Long: Retrieve a JsonNumber
from aJsonObject
, and transform it to a Java Boxed-LongModifier and Type Method static Long
getLong(JsonObject jo, String propertyName, boolean isOptional)
static Long
getLong(JsonObject jo, String propertyName, int FLAGS, long defaultValue)
Long: Retrieve a JsonString
from aJsonArray
orJsonObject
, and parse it to a Boxed-LongModifier and Type Method static Long
parseLong(JsonArray ja, int index, int FLAGS, long defaultValue, Function<String,Long> optionalParser)
static Long
parseLong(JsonObject jo, String propertyName, int FLAGS, long defaultValue, Function<String,Long> optionalParser)
Short: Retrieve a JsonNumber
from aJsonArray
, and transform it to a Java Boxed-ShortModifier and Type Method static Short
getShort(JsonArray ja, int index)
static Short
getShort(JsonArray ja, int index, int FLAGS, short defaultValue)
Short: Retrieve a JsonNumber
from aJsonObject
, and transform it to a Java Boxed-ShortModifier and Type Method static Short
getShort(JsonObject jo, String propertyName, boolean isOptional)
static Short
getShort(JsonObject jo, String propertyName, int FLAGS, short defaultValue)
Short: Retrieve a JsonString
from aJsonArray
orJsonObject
, and parse it to a Boxed-ShortModifier and Type Method static Short
parseShort(JsonArray ja, int index, int FLAGS, short defaultValue, Function<String,Short> optionalParser)
static Short
parseShort(JsonObject jo, String propertyName, int FLAGS, short defaultValue, Function<String,Short> optionalParser)
Byte: Retrieve a JsonNumber
from aJsonArray
, and transform it to a Java Boxed-ByteModifier and Type Method static Byte
getByte(JsonArray ja, int index)
static Byte
getByte(JsonArray ja, int index, int FLAGS, byte defaultValue)
Byte: Retrieve a JsonNumber
from aJsonObject
, and transform it to a Java Boxed-ByteModifier and Type Method static Byte
getByte(JsonObject jo, String propertyName, boolean isOptional)
static Byte
getByte(JsonObject jo, String propertyName, int FLAGS, byte defaultValue)
Byte: Retrieve a JsonString
from aJsonArray
orJsonObject
, and parse it to a Boxed-ByteModifier and Type Method static Byte
parseByte(JsonArray ja, int index, int FLAGS, byte defaultValue, Function<String,Byte> optionalParser)
static Byte
parseByte(JsonObject jo, String propertyName, int FLAGS, byte defaultValue, Function<String,Byte> optionalParser)
Double: Retrieve a JsonNumber
from aJsonArray
, and transform it to a Java Boxed-DoubleModifier and Type Method static Double
getDouble(JsonArray ja, int index)
static Double
getDouble(JsonArray ja, int index, int FLAGS, double defaultValue)
Double: Retrieve a JsonNumber
from aJsonObject
, and transform it to a Java Boxed-DoubleModifier and Type Method static Double
getDouble(JsonObject jo, String propertyName, boolean isOptional)
static Double
getDouble(JsonObject jo, String propertyName, int FLAGS, double defaultValue)
Double: Retrieve a JsonString
from aJsonArray
orJsonObject
, and parse it to a Boxed-DoubleModifier and Type Method static Double
parseDouble(JsonArray ja, int index, int FLAGS, double defaultValue, Function<String,Double> optionalParser)
static Double
parseDouble(JsonObject jo, String propertyName, int FLAGS, double defaultValue, Function<String,Double> optionalParser)
Float: Retrieve a JsonNumber
from aJsonArray
, and transform it to a Java Boxed-FloatModifier and Type Method static Float
getFloat(JsonArray ja, int index)
static Float
getFloat(JsonArray ja, int index, int FLAGS, float defaultValue)
Float: Retrieve a JsonNumber
from aJsonObject
, and transform it to a Java Boxed-FloatModifier and Type Method static Float
getFloat(JsonObject jo, String propertyName, boolean isOptional)
static Float
getFloat(JsonObject jo, String propertyName, int FLAGS, float defaultValue)
Float: Retrieve a JsonString
from aJsonArray
orJsonObject
, and parse it to a Boxed-FloatModifier and Type Method static Float
parseFloat(JsonArray ja, int index, int FLAGS, float defaultValue, Function<String,Float> optionalParser)
static Float
parseFloat(JsonObject jo, String propertyName, int FLAGS, float defaultValue, Function<String,Float> parser)
Boolean: Retrieve JsonValue.TRUE
orFALSE
, from aJsonArray
, and transform to Boxed-BooleanModifier and Type Method static Boolean
getBoolean(JsonArray ja, int index)
static Boolean
getBoolean(JsonArray ja, int index, int FLAGS, boolean defaultValue)
Boolean: Retrieve JsonValue.TRUE
orFALSE
, from aJsonObject
, and transform to Boxed-BooleanModifier and Type Method static Boolean
getBoolean(JsonObject jo, String propertyName, boolean isOptional)
static Boolean
getBoolean(JsonObject jo, String propertyName, int FLAGS, boolean defaultValue)
Boolean: Retrieve a JsonString
from aJsonArray
orJsonObject
, and parse to a Boxed-BooleanModifier and Type Method static Boolean
parseBoolean(JsonArray ja, int index, int FLAGS, boolean defaultValue, Function<String,Boolean> optionalParser)
static Boolean
parseBoolean(JsonObject jo, String propertyName, int FLAGS, boolean defaultValue, Function<String,Boolean> parser)
-
-
-
Method Detail
-
getInteger
public static java.lang.Integer getInteger(JsonArray ja, int index)
Retrieve aJsonArray
element, and transform it to ajava.lang.Integer
. If Json-Null is found - then Java-Null is returned.
Boxed Java Null's:
This method facilitates translating a Json-Null into a Java-Null since a Java Boxed-Type number is returned from this method, rather than a Java Primitive- Parameters:
ja
- Any instance ofJsonArray
index
- A valid index into array parameter'ja'
. In Json (and unlike Java), array element types are not actually checked for consistency - meaning an array may hold many different types! This array-index must contain aJsonNumber
- Returns:
- An instance of Java-Type
java.lang.Integer
, if and only if the array element located at'index'
uses the appropriate Json-Type -JsonNumber
. If that array-index contains some other type, or thatJsonNumber
cannot be properly transformed to ajava.lang.Integer
, then an exception will throw.
If the specified array-element inside'ja'
has been set to Json-Null, then Java-Null is returned. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of'ja'
JsonTypeArrException
- If the array-element (specified by'index'
) does not actually contain aJsonNumber
(but rather, some other non-null Json-Type), then this exception throws.JsonArithmeticArrException
- Java's implementation ofintValueExact()
throws anArithmeticException
when that method cannot correctly transform / convert the element to a validjava.lang.Integer
. The thrown-exception is then wrapped into this exception for the purpose of providing a much improved error-message, and convenience fields.- See Also:
RJInternal.GET(JsonArray, int, Function, Class)
,JsonNumber.intValueExact()
- Code:
- Exact Method Body:
return GET(ja, index, JsonNumber::intValueExact, Integer.class);
-
getInteger
public static java.lang.Integer getInteger(JsonArray ja, int index, int FLAGS, int defaultValue)
Retrieve aJsonArray
element, and transform it to ajava.lang.Integer
. Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value (using the Flags). Additionally, if anArithmeticException
should throw, that exception-case can also be handled by using the default type-rounding mechanism java provides via methodintValue()
(without the'Exact'
word appended to the method-name). See the Java Documentation forBigDecimal.intValue()
for an explanation about the behavior of flagRETURN_JAPPROX_ON_AEX
and rounding ajava.lang.Integer
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException int val = ReadBoxedJSON.getInteger(jsonArray, 1, RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns a boxed '-1' if the array index is null, // or if the array index contains some non-integer number. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Integer => 'int' // (auto-unboxes the returned reference to a int-primitive) int val = ReadBoxedJSON.getInteger (jsonArray, 1, RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_AEX, -1);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing the element to retrieve.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.- Returns:
- If the
JsonArray
index contained a number, and that number was successfully transformed to ajava.lang.Integer
, then it is returned.
If there were errors attempting to transform the array-element, but the user-provided flags have specified that thedefaultValue
be returned, then that is returned. Null is returned if the flags have specified a null-return on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then, instead, the relevant exception will throw. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of input array'ja'
and none of these Exception-Flags are set:RETURN_NULL_ON_IOB
(Abbrev:RN_IOB
)RETURN_DEFVAL_ON_IOB
(Abbrev:RD_IOB
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticArrException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonNumber
into ajava.lang.Integer
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonNullArrException
- If theJsonArray
element located at'index'
contains Json-Null, rather than aJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeArrException
- If theJsonArray
element located at'index'
contains a some other Json-Type rather thanJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.GET(JsonArray, int, int, Number, Class, Function, Function)
,JsonNumber.intValueExact()
,JsonNumber.intValue()
- Code:
- Exact Method Body:
return GET( ja, index, FLAGS, defaultValue, Integer.class, JsonNumber::intValueExact, JsonNumber::intValue );
-
parseInteger
public static java.lang.Integer parseInteger (JsonArray ja, int index, int FLAGS, int defaultValue, java.util.function.Function<java.lang.String,java.lang.Integer> optionalParser)
Retrieve aJsonArray
element containing aJsonString
, and transform it to ajava.lang.Integer
, with either a user-provided parser, or the standard java integer parser Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value, using the'FLAGS'
parameter.
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException int val = ReadBoxedJSON.parseInteger(jsonArray, 1, RETURN_NULL_ON_NULL, -1, null); // The following invocation gracefully returns a boxed '-1' if the array index contains // an 'unparseable' String (or is null) // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Integer => 'int' // (auto-unboxes the returned reference to a int-primitive) int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_SPEX; int val = ReadBoxedJSON.parseInteger(jsonArray, 1, FLAGS, -1, null);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing theJsonString
element to retrieve.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.optionalParser
- Any function-pointer to a function that will transform / convert ajava.lang.String
into ajava.lang.Integer
.
NOTE: This parameter may be null, and if it is, the standard Java invocation ofInteger.parseInt(String.trim())
is used.- Returns:
- If the
JsonArray
index contained aJsonString
, and that string was successfully parsed to ajava.lang.Integer
, then it is returned.
If there were errors attempting to transform theString
array-element, and the user-provided flags specified thatdefaultValue
be returned, then that is returned. Null is returned if the flags specified it on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then the relevant exception will throw, instead. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of input array'ja'
and none of these Exception-Flags are set:RETURN_NULL_ON_IOB
(Abbrev:RN_IOB
)RETURN_DEFVAL_ON_IOB
(Abbrev:RD_IOB
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticArrException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonString
into ajava.lang.Integer
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonStrParseArrException
- If theJsonArray
element located at'index'
throws an exception while attempting to parse thejava.lang.String
into ajava.lang.Integer
, and none of these Exception-Flags are set: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
)
JsonNullArrException
- If theJsonArray
element located at'index'
contains Json-Null, rather than aJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeArrException
- If theJsonArray
element located at'index'
contains a some other Json-Type rather thanJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.PARSE(JsonObject, String, int, Number, Class, Function, Function, Function)
- Code:
- Exact Method Body:
return PARSE( ja, index, FLAGS, defaultValue, Integer.class, optionalParser, BigDecimal::intValueExact, BigDecimal::intValue );
-
getInteger
public static java.lang.Integer getInteger(JsonObject jo, java.lang.String propertyName, boolean isOptional)
Extract aJsonObject
property, and transform it to ajava.lang.Integer
. If Json-Null is found - then Java-Null is returned.
Boxed Java Null's:
This method facilitates translating a Json-Null into a Java-Null since a Java Boxed-Type number is returned from this method, rather than a Java Primitive- Parameters:
jo
- Any instance ofJsonObject
.propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Integer
.isOptional
- WhenTRUE
is passed, if'propertyName'
is not actually listed in'jo'
this method shall return Java-Null gracefully. WhenFALSE
is passed, if'jo'
does not have the specified property, aJsonPropMissingException
will throw.
If'jo'
actually has a property named'propertyName'
, then the value passed to this parameter is fully irrelevant.- Returns:
- An instance of Java-Type
java.lang.Integer
, if and only if the'propertyName'
is available inside'jo'
, and that property uses the appropriate Json-Type -JsonNumber
If the specified property has been set to 'null' (Json-Null) in the passedJsonObject
, then Java-Null is returned. Java-Null is also returned if'propertyName'
is not listed / unavailable in'jo'
andTRUE
has been passed to'isOptional'
.
Other cases shall generate an exception throw. - Throws:
JsonPropMissingException
- This exception shall throw if the specified property is missing from the'JsonObject'
(parameter'jo'
). This exception throw can be avoided if'TRUE'
is passed to parameter'isOptional'
.JsonArithmeticObjException
- Java's implementation ofintValueExact()
throws anArithmeticException
when that method cannot correctly transform / convert the element to a validjava.lang.Integer
. The thrown-exception is then wrapped into this exception for the purpose of providing a much improved error-message, and convenience fields.JsonTypeObjException
- If the property (specified by'propertyName'
) is extracted, but that property does not actually contain aJsonNumber
(but rather, some other non-null Json-Type), then this exception throws.- See Also:
RJInternal.GET(JsonObject, String, boolean, Function, Class)
,JsonNumber.intValueExact()
- Code:
- Exact Method Body:
return GET(jo, propertyName, isOptional, JsonNumber::intValueExact, Integer.class);
-
getInteger
public static java.lang.Integer getInteger(JsonObject jo, java.lang.String propertyName, int FLAGS, int defaultValue)
Retrieve aJsonObject
property, and transform it to ajava.lang.Integer
. Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value (using the Flags). Additionally, if anArithmeticException
should throw, that exception-case can also be handled by using the default type-rounding mechanism java provides via methodintValue()
(without the'Exact'
word appended to the method-name). See the Java Documentation forBigDecimal.intValue()
for an explanation about the behavior of flagRETURN_JAPPROX_ON_AEX
and rounding ajava.lang.Integer
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException int val = ReadBoxedJSON.getInteger(jsonObj, "myProp", RETURN_NULL_ON_NULL -1); // The following invocation gracefully returns a boxed '-1' if the property contains a // null, or if the property holds some non-integer number. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Integer => 'int' // (auto-unboxes the returned reference to a int-primitive) int val = ReadBoxedJSON.getInteger (jsonObj, "myProp", RETURN_DEFVAL_ON_NULL & RETURN_DEFVAL_ON_AEX, -1);
- Parameters:
jo
- Any instance ofJsonObject
propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Integer
.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.- Returns:
- If the
JsonObject
property contained a number, and that number was successfully transformed to ajava.lang.Integer
, then it is returned.
If there were errors attempting to transform the object-property, and the user-provided flags have specified that thedefaultValue
be returned, then that is returned. Null is returned if the flags have specified a null-return on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then, instead, the relevant exception will throw. - Throws:
JsonPropMissingException
- If'jo'
does not have a property with the name'propertyName'
and none of these Exception-Flags are set:RETURN_NULL_ON_MISSING
(Abbrev:RN_M
)RETURN_DEFVAL_ON_MISSING
(Abbrev:RD_M
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticObjException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonNumber
into ajava.lang.Integer
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonNullObjException
- If theJsonObject
property specified by'propertyName'
contains Json-Null, rather than aJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeObjException
- If theJsonObject
property specified by'propertyName'
contains a some other Json-Type rather thanJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.GET(JsonObject, String, int, Number, Class, Function, Function)
,JsonNumber.intValueExact()
,JsonNumber.intValue()
- Code:
- Exact Method Body:
return GET( jo, propertyName, FLAGS, defaultValue, Integer.class, JsonNumber::intValueExact, JsonNumber::intValue );
-
parseInteger
public static java.lang.Integer parseInteger (JsonObject jo, java.lang.String propertyName, int FLAGS, int defaultValue, java.util.function.Function<java.lang.String,java.lang.Integer> optionalParser)
Retrieve aJsonObject
property containing aJsonString
, and transform it to ajava.lang.Integer
, with either a user-provided parser, or the standard java integer parser Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value, using the'FLAGS'
parameter.
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException int val = ReadBoxedJSON.parseInteger(jsonObj, "myProp", RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns a boxed '-1' if the property contains an // 'unparseable' String, or if it is null. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Integer => 'int' // (auto-unboxes the returned reference to a int-primitive) int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_SPEX int val = ReadBoxedJSON.parseInteger(jsonObj, "myProp", FLAGS , -1);
- Parameters:
jo
- Any instance ofJsonObject
propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Integer
.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.optionalParser
- Any function-pointer to a function that will transform / convert ajava.lang.String
into ajava.lang.Integer
.
NOTE: This parameter may be null, and if it is, the standard Java invocation ofInteger.parseInt(String.trim())
is used.- Returns:
- If the
JsonObject
property contained aJsonString
, and that string was successfully parsed to ajava.lang.Integer
, then it is returned.
If there were errors attempting to transform theString
object-property, and the user-provided flags specified thatdefaultValue
be returned, then that is returned. Null is returned if the flags specified it on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then the relevant exception will throw, instead. - Throws:
JsonPropMissingException
- If'jo'
does not have a property with the name'propertyName'
and none of these Exception-Flags are set:RETURN_NULL_ON_MISSING
(Abbrev:RN_M
)RETURN_DEFVAL_ON_MISSING
(Abbrev:RD_M
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticObjException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonString
into ajava.lang.Integer
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonStrParseObjException
- If theJsonObject
property specified by'propertyName'
throws an exception while attempting to parse thejava.lang.String
into ajava.lang.Integer
, and none of these Exception-Flags are set: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
)
JsonNullObjException
- If theJsonObject
property specified by'propertyName'
contains Json-Null, rather than aJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeObjException
- If theJsonObject
property specified by'propertyName'
contains a some other Json-Type rather thanJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.PARSE(JsonObject, String, int, Number, Class, Function, Function, Function)
- Code:
- Exact Method Body:
return PARSE( jo, propertyName, FLAGS, defaultValue, Integer.class, optionalParser, BigDecimal::intValueExact, BigDecimal::intValue );
-
getLong
public static java.lang.Long getLong(JsonArray ja, int index)
Retrieve aJsonArray
element, and transform it to ajava.lang.Long
. If Json-Null is found - then Java-Null is returned.
Boxed Java Null's:
This method facilitates translating a Json-Null into a Java-Null since a Java Boxed-Type number is returned from this method, rather than a Java Primitive- Parameters:
ja
- Any instance ofJsonArray
index
- A valid index into array parameter'ja'
. In Json (and unlike Java), array element types are not actually checked for consistency - meaning an array may hold many different types! This array-index must contain aJsonNumber
- Returns:
- An instance of Java-Type
java.lang.Long
, if and only if the array element located at'index'
uses the appropriate Json-Type -JsonNumber
. If that array-index contains some other type, or thatJsonNumber
cannot be properly transformed to ajava.lang.Long
, then an exception will throw.
If the specified array-element inside'ja'
has been set to Json-Null, then Java-Null is returned. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of'ja'
JsonTypeArrException
- If the array-element (specified by'index'
) does not actually contain aJsonNumber
(but rather, some other non-null Json-Type), then this exception throws.JsonArithmeticArrException
- Java's implementation oflongValueExact()
throws anArithmeticException
when that method cannot correctly transform / convert the element to a validjava.lang.Long
. The thrown-exception is then wrapped into this exception for the purpose of providing a much improved error-message, and convenience fields.- See Also:
RJInternal.GET(JsonArray, int, Function, Class)
,JsonNumber.longValueExact()
- Code:
- Exact Method Body:
return GET(ja, index, JsonNumber::longValueExact, Long.class);
-
getLong
public static java.lang.Long getLong(JsonArray ja, int index, int FLAGS, long defaultValue)
Retrieve aJsonArray
element, and transform it to ajava.lang.Long
. Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value (using the Flags). Additionally, if anArithmeticException
should throw, that exception-case can also be handled by using the default type-rounding mechanism java provides via methodlongValue()
(without the'Exact'
word appended to the method-name). See the Java Documentation forBigDecimal.longValue()
for an explanation about the behavior of flagRETURN_JAPPROX_ON_AEX
and rounding ajava.lang.Long
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException long val = ReadBoxedJSON.getLong(jsonArray, 1, RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns a boxed '-1' if the array index is null, // or if the array index contains some non-integer number. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Long => 'long' // (auto-unboxes the returned reference to a long-primitive) long val = ReadBoxedJSON.getLong (jsonArray, 1, RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_AEX, -1);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing the element to retrieve.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.- Returns:
- If the
JsonArray
index contained a number, and that number was successfully transformed to ajava.lang.Long
, then it is returned.
If there were errors attempting to transform the array-element, but the user-provided flags have specified that thedefaultValue
be returned, then that is returned. Null is returned if the flags have specified a null-return on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then, instead, the relevant exception will throw. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of input array'ja'
and none of these Exception-Flags are set:RETURN_NULL_ON_IOB
(Abbrev:RN_IOB
)RETURN_DEFVAL_ON_IOB
(Abbrev:RD_IOB
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticArrException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonNumber
into ajava.lang.Long
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonNullArrException
- If theJsonArray
element located at'index'
contains Json-Null, rather than aJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeArrException
- If theJsonArray
element located at'index'
contains a some other Json-Type rather thanJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.GET(JsonArray, int, int, Number, Class, Function, Function)
,JsonNumber.longValueExact()
,JsonNumber.longValue()
- Code:
- Exact Method Body:
return GET( ja, index, FLAGS, defaultValue, Long.class, JsonNumber::longValueExact, JsonNumber::longValue );
-
parseLong
public static java.lang.Long parseLong (JsonArray ja, int index, int FLAGS, long defaultValue, java.util.function.Function<java.lang.String,java.lang.Long> optionalParser)
Retrieve aJsonArray
element containing aJsonString
, and transform it to ajava.lang.Long
, with either a user-provided parser, or the standard java long-integer parser Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value, using the'FLAGS'
parameter.
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException long val = ReadBoxedJSON.parseLong(jsonArray, 1, RETURN_NULL_ON_NULL, -1, null); // The following invocation gracefully returns a boxed '-1' if the array index contains // an 'unparseable' String (or is null) // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Long => 'long' // (auto-unboxes the returned reference to a long-primitive) int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_SPEX; long val = ReadBoxedJSON.parseLong(jsonArray, 1, FLAGS, -1, null);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing theJsonString
element to retrieve.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.optionalParser
- Any function-pointer to a function that will transform / convert ajava.lang.String
into ajava.lang.Long
.
NOTE: This parameter may be null, and if it is, the standard Java invocation ofLong.parseLong(String.trim())
is used.- Returns:
- If the
JsonArray
index contained aJsonString
, and that string was successfully parsed to ajava.lang.Long
, then it is returned.
If there were errors attempting to transform theString
array-element, and the user-provided flags specified thatdefaultValue
be returned, then that is returned. Null is returned if the flags specified it on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then the relevant exception will throw, instead. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of input array'ja'
and none of these Exception-Flags are set:RETURN_NULL_ON_IOB
(Abbrev:RN_IOB
)RETURN_DEFVAL_ON_IOB
(Abbrev:RD_IOB
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticArrException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonString
into ajava.lang.Long
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonStrParseArrException
- If theJsonArray
element located at'index'
throws an exception while attempting to parse thejava.lang.String
into ajava.lang.Long
, and none of these Exception-Flags are set: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
)
JsonNullArrException
- If theJsonArray
element located at'index'
contains Json-Null, rather than aJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeArrException
- If theJsonArray
element located at'index'
contains a some other Json-Type rather thanJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.PARSE(JsonObject, String, int, Number, Class, Function, Function, Function)
- Code:
- Exact Method Body:
return PARSE( ja, index, FLAGS, defaultValue, Long.class, optionalParser, BigDecimal::longValueExact, BigDecimal::longValue );
-
getLong
public static java.lang.Long getLong(JsonObject jo, java.lang.String propertyName, boolean isOptional)
Extract aJsonObject
property, and transform it to ajava.lang.Long
. If Json-Null is found - then Java-Null is returned.
Boxed Java Null's:
This method facilitates translating a Json-Null into a Java-Null since a Java Boxed-Type number is returned from this method, rather than a Java Primitive- Parameters:
jo
- Any instance ofJsonObject
.propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Long
.isOptional
- WhenTRUE
is passed, if'propertyName'
is not actually listed in'jo'
this method shall return Java-Null gracefully. WhenFALSE
is passed, if'jo'
does not have the specified property, aJsonPropMissingException
will throw.
If'jo'
actually has a property named'propertyName'
, then the value passed to this parameter is fully irrelevant.- Returns:
- An instance of Java-Type
java.lang.Long
, if and only if the'propertyName'
is available inside'jo'
, and that property uses the appropriate Json-Type -JsonNumber
If the specified property has been set to 'null' (Json-Null) in the passedJsonObject
, then Java-Null is returned. Java-Null is also returned if'propertyName'
is not listed / unavailable in'jo'
andTRUE
has been passed to'isOptional'
.
Other cases shall generate an exception throw. - Throws:
JsonPropMissingException
- This exception shall throw if the specified property is missing from the'JsonObject'
(parameter'jo'
). This exception throw can be avoided if'TRUE'
is passed to parameter'isOptional'
.JsonArithmeticObjException
- Java's implementation oflongValueExact()
throws anArithmeticException
when that method cannot correctly transform / convert the element to a validjava.lang.Long
. The thrown-exception is then wrapped into this exception for the purpose of providing a much improved error-message, and convenience fields.JsonTypeObjException
- If the property (specified by'propertyName'
) is extracted, but that property does not actually contain aJsonNumber
(but rather, some other non-null Json-Type), then this exception throws.- See Also:
RJInternal.GET(JsonObject, String, boolean, Function, Class)
,JsonNumber.longValueExact()
- Code:
- Exact Method Body:
return GET(jo, propertyName, isOptional, JsonNumber::longValueExact, Long.class);
-
getLong
public static java.lang.Long getLong(JsonObject jo, java.lang.String propertyName, int FLAGS, long defaultValue)
Retrieve aJsonObject
property, and transform it to ajava.lang.Long
. Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value (using the Flags). Additionally, if anArithmeticException
should throw, that exception-case can also be handled by using the default type-rounding mechanism java provides via methodintValue()
(without the'Exact'
word appended to the method-name). See the Java Documentation forBigDecimal.intValue()
for an explanation about the behavior of flagRETURN_JAPPROX_ON_AEX
and rounding ajava.lang.Long
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException int val = ReadBoxedJSON.getLong(jsonObj, "myProp", RETURN_NULL_ON_NULL -1); // The following invocation gracefully returns a boxed '-1' if the property contains a // null, or if the property holds some non-integer number. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Long => 'int' // (auto-unboxes the returned reference to a int-primitive) int val = ReadBoxedJSON.getLong (jsonObj, "myProp", RETURN_DEFVAL_ON_NULL & RETURN_DEFVAL_ON_AEX, -1);
- Parameters:
jo
- Any instance ofJsonObject
propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Long
.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.- Returns:
- If the
JsonObject
property contained a number, and that number was successfully transformed to ajava.lang.Long
, then it is returned.
If there were errors attempting to transform the object-property, and the user-provided flags have specified that thedefaultValue
be returned, then that is returned. Null is returned if the flags have specified a null-return on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then, instead, the relevant exception will throw. - Throws:
JsonPropMissingException
- If'jo'
does not have a property with the name'propertyName'
and none of these Exception-Flags are set:RETURN_NULL_ON_MISSING
(Abbrev:RN_M
)RETURN_DEFVAL_ON_MISSING
(Abbrev:RD_M
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticObjException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonNumber
into ajava.lang.Long
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonNullObjException
- If theJsonObject
property specified by'propertyName'
contains Json-Null, rather than aJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeObjException
- If theJsonObject
property specified by'propertyName'
contains a some other Json-Type rather thanJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.GET(JsonObject, String, int, Number, Class, Function, Function)
,JsonNumber.longValueExact()
,JsonNumber.longValue()
- Code:
- Exact Method Body:
return GET( jo, propertyName, FLAGS, defaultValue, Long.class, JsonNumber::longValueExact, JsonNumber::longValue );
-
parseLong
public static java.lang.Long parseLong (JsonObject jo, java.lang.String propertyName, int FLAGS, long defaultValue, java.util.function.Function<java.lang.String,java.lang.Long> optionalParser)
Retrieve aJsonObject
property containing aJsonString
, and transform it to ajava.lang.Long
, with either a user-provided parser, or the standard java long-integer parser Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value, using the'FLAGS'
parameter.
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException long val = ReadBoxedJSON.parseLong(jsonObj, "myProp", RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns a boxed '-1' if the property contains an // 'unparseable' String, or if it is null. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Long => 'long' // (auto-unboxes the returned reference to a long-primitive) int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_SPEX long val = ReadBoxedJSON.parseLong(jsonObj, "myProp", FLAGS , -1);
- Parameters:
jo
- Any instance ofJsonObject
propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Long
.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.optionalParser
- Any function-pointer to a function that will transform / convert ajava.lang.String
into ajava.lang.Long
.
NOTE: This parameter may be null, and if it is, the standard Java invocation ofLong.parseLong(String.trim())
is used.- Returns:
- If the
JsonObject
property contained aJsonString
, and that string was successfully parsed to ajava.lang.Long
, then it is returned.
If there were errors attempting to transform theString
object-property, and the user-provided flags specified thatdefaultValue
be returned, then that is returned. Null is returned if the flags specified it on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then the relevant exception will throw, instead. - Throws:
JsonPropMissingException
- If'jo'
does not have a property with the name'propertyName'
and none of these Exception-Flags are set:RETURN_NULL_ON_MISSING
(Abbrev:RN_M
)RETURN_DEFVAL_ON_MISSING
(Abbrev:RD_M
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticObjException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonString
into ajava.lang.Long
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonStrParseObjException
- If theJsonObject
property specified by'propertyName'
throws an exception while attempting to parse thejava.lang.String
into ajava.lang.Long
, and none of these Exception-Flags are set: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
)
JsonNullObjException
- If theJsonObject
property specified by'propertyName'
contains Json-Null, rather than aJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeObjException
- If theJsonObject
property specified by'propertyName'
contains a some other Json-Type rather thanJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.PARSE(JsonObject, String, int, Number, Class, Function, Function, Function)
- Code:
- Exact Method Body:
return PARSE( jo, propertyName, FLAGS, defaultValue, Long.class, optionalParser, BigDecimal::longValueExact, BigDecimal::longValue );
-
getShort
public static java.lang.Short getShort(JsonArray ja, int index)
Retrieve aJsonArray
element, and transform it to ajava.lang.Short
. If Json-Null is found - then Java-Null is returned.
Boxed Java Null's:
This method facilitates translating a Json-Null into a Java-Null since a Java Boxed-Type number is returned from this method, rather than a Java Primitive- Parameters:
ja
- Any instance ofJsonArray
index
- A valid index into array parameter'ja'
. In Json (and unlike Java), array element types are not actually checked for consistency - meaning an array may hold many different types! This array-index must contain aJsonNumber
- Returns:
- An instance of Java-Type
java.lang.Short
, if and only if the array element located at'index'
uses the appropriate Json-Type -JsonNumber
. If that array-index contains some other type, or thatJsonNumber
cannot be properly transformed to ajava.lang.Short
, then an exception will throw.
If the specified array-element inside'ja'
has been set to Json-Null, then Java-Null is returned. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of'ja'
JsonTypeArrException
- If the array-element (specified by'index'
) does not actually contain aJsonNumber
(but rather, some other non-null Json-Type), then this exception throws.JsonArithmeticArrException
- Java's implementation ofshortValueExact()
throws anArithmeticException
when that method cannot correctly transform / convert the element to a validjava.lang.Short
. The thrown-exception is then wrapped into this exception for the purpose of providing a much improved error-message, and convenience fields.- See Also:
RJInternal.GET(JsonArray, int, Function, Class)
,JsonNumber.bigDecimalValue()
- Code:
- Exact Method Body:
return GET(ja, index, jn -> jn.bigDecimalValue().shortValueExact(), Short.class);
-
getShort
public static java.lang.Short getShort(JsonArray ja, int index, int FLAGS, short defaultValue)
Retrieve aJsonArray
element, and transform it to ajava.lang.Short
. Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value (using the Flags). Additionally, if anArithmeticException
should throw, that exception-case can also be handled by using the default type-rounding mechanism java provides via methodshortValue()
(without the'Exact'
word appended to the method-name). See the Java Documentation forBigDecimal.shortValue()
for an explanation about the behavior of flagRETURN_JAPPROX_ON_AEX
and rounding ajava.lang.Short
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException short val = ReadBoxedJSON.getShort(jsonArray, 1, RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns a boxed '-1' if the array index is null, // or if the array index contains some non-integer number. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Short => 'short' // (auto-unboxes the returned reference to a short-primitive) short val = ReadBoxedJSON.getShort (jsonArray, 1, RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_AEX, -1);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing the element to retrieve.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.- Returns:
- If the
JsonArray
index contained a number, and that number was successfully transformed to ajava.lang.Short
, then it is returned.
If there were errors attempting to transform the array-element, but the user-provided flags have specified that thedefaultValue
be returned, then that is returned. Null is returned if the flags have specified a null-return on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then, instead, the relevant exception will throw. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of input array'ja'
and none of these Exception-Flags are set:RETURN_NULL_ON_IOB
(Abbrev:RN_IOB
)RETURN_DEFVAL_ON_IOB
(Abbrev:RD_IOB
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticArrException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonNumber
into ajava.lang.Short
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonNullArrException
- If theJsonArray
element located at'index'
contains Json-Null, rather than aJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeArrException
- If theJsonArray
element located at'index'
contains a some other Json-Type rather thanJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.GET(JsonArray, int, int, Number, Class, Function, Function)
,JsonNumber.bigDecimalValue()
- Code:
- Exact Method Body:
return GET( ja, index, FLAGS, defaultValue, Short.class, jn -> jn.bigDecimalValue().shortValueExact(), jn -> jn.bigDecimalValue().shortValue() );
-
parseShort
public static java.lang.Short parseShort (JsonArray ja, int index, int FLAGS, short defaultValue, java.util.function.Function<java.lang.String,java.lang.Short> optionalParser)
Retrieve aJsonArray
element containing aJsonString
, and transform it to ajava.lang.Short
, with either a user-provided parser, or the standard java short-integer parser Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value, using the'FLAGS'
parameter.
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException short val = ReadBoxedJSON.parseShort(jsonArray, 1, RETURN_NULL_ON_NULL, -1, null); // The following invocation gracefully returns a boxed '-1' if the array index contains // an 'unparseable' String (or is null) // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Short => 'short' // (auto-unboxes the returned reference to a short-primitive) int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_SPEX; short val = ReadBoxedJSON.parseShort(jsonArray, 1, FLAGS, -1, null);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing theJsonString
element to retrieve.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.optionalParser
- Any function-pointer to a function that will transform / convert ajava.lang.String
into ajava.lang.Short
.
NOTE: This parameter may be null, and if it is, the standard Java invocation ofShort.parseShort(String.trim())
is used.- Returns:
- If the
JsonArray
index contained aJsonString
, and that string was successfully parsed to ajava.lang.Short
, then it is returned.
If there were errors attempting to transform theString
array-element, and the user-provided flags specified thatdefaultValue
be returned, then that is returned. Null is returned if the flags specified it on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then the relevant exception will throw, instead. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of input array'ja'
and none of these Exception-Flags are set:RETURN_NULL_ON_IOB
(Abbrev:RN_IOB
)RETURN_DEFVAL_ON_IOB
(Abbrev:RD_IOB
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticArrException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonString
into ajava.lang.Short
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonStrParseArrException
- If theJsonArray
element located at'index'
throws an exception while attempting to parse thejava.lang.String
into ajava.lang.Short
, and none of these Exception-Flags are set: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
)
JsonNullArrException
- If theJsonArray
element located at'index'
contains Json-Null, rather than aJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeArrException
- If theJsonArray
element located at'index'
contains a some other Json-Type rather thanJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.PARSE(JsonObject, String, int, Number, Class, Function, Function, Function)
- Code:
- Exact Method Body:
return PARSE( ja, index, FLAGS, defaultValue, Short.class, optionalParser, BigDecimal::shortValueExact, BigDecimal::shortValue );
-
getShort
public static java.lang.Short getShort(JsonObject jo, java.lang.String propertyName, boolean isOptional)
Extract aJsonObject
property, and transform it to ajava.lang.Short
. If Json-Null is found - then Java-Null is returned.
Boxed Java Null's:
This method facilitates translating a Json-Null into a Java-Null since a Java Boxed-Type number is returned from this method, rather than a Java Primitive- Parameters:
jo
- Any instance ofJsonObject
.propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Short
.isOptional
- WhenTRUE
is passed, if'propertyName'
is not actually listed in'jo'
this method shall return Java-Null gracefully. WhenFALSE
is passed, if'jo'
does not have the specified property, aJsonPropMissingException
will throw.
If'jo'
actually has a property named'propertyName'
, then the value passed to this parameter is fully irrelevant.- Returns:
- An instance of Java-Type
java.lang.Short
, if and only if the'propertyName'
is available inside'jo'
, and that property uses the appropriate Json-Type -JsonNumber
If the specified property has been set to 'null' (Json-Null) in the passedJsonObject
, then Java-Null is returned. Java-Null is also returned if'propertyName'
is not listed / unavailable in'jo'
andTRUE
has been passed to'isOptional'
.
Other cases shall generate an exception throw. - Throws:
JsonPropMissingException
- This exception shall throw if the specified property is missing from the'JsonObject'
(parameter'jo'
). This exception throw can be avoided if'TRUE'
is passed to parameter'isOptional'
.JsonArithmeticObjException
- Java's implementation ofshortValueExact()
throws anArithmeticException
when that method cannot correctly transform / convert the element to a validjava.lang.Short
. The thrown-exception is then wrapped into this exception for the purpose of providing a much improved error-message, and convenience fields.JsonTypeObjException
- If the property (specified by'propertyName'
) is extracted, but that property does not actually contain aJsonNumber
(but rather, some other non-null Json-Type), then this exception throws.- See Also:
RJInternal.GET(JsonObject, String, boolean, Function, Class)
,JsonNumber.bigDecimalValue()
- Code:
- Exact Method Body:
return GET( jo, propertyName, isOptional, jn -> jn.bigDecimalValue().shortValueExact(), Short.class );
-
getShort
public static java.lang.Short getShort(JsonObject jo, java.lang.String propertyName, int FLAGS, short defaultValue)
Retrieve aJsonObject
property, and transform it to ajava.lang.Short
. Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value (using the Flags). Additionally, if anArithmeticException
should throw, that exception-case can also be handled by using the default type-rounding mechanism java provides via methodintValue()
(without the'Exact'
word appended to the method-name). See the Java Documentation forBigDecimal.intValue()
for an explanation about the behavior of flagRETURN_JAPPROX_ON_AEX
and rounding ajava.lang.Short
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException int val = ReadBoxedJSON.getShort(jsonObj, "myProp", RETURN_NULL_ON_NULL -1); // The following invocation gracefully returns a boxed '-1' if the property contains a // null, or if the property holds some non-integer number. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Short => 'int' // (auto-unboxes the returned reference to a int-primitive) int val = ReadBoxedJSON.getShort (jsonObj, "myProp", RETURN_DEFVAL_ON_NULL & RETURN_DEFVAL_ON_AEX, -1);
- Parameters:
jo
- Any instance ofJsonObject
propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Short
.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.- Returns:
- If the
JsonObject
property contained a number, and that number was successfully transformed to ajava.lang.Short
, then it is returned.
If there were errors attempting to transform the object-property, and the user-provided flags have specified that thedefaultValue
be returned, then that is returned. Null is returned if the flags have specified a null-return on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then, instead, the relevant exception will throw. - Throws:
JsonPropMissingException
- If'jo'
does not have a property with the name'propertyName'
and none of these Exception-Flags are set:RETURN_NULL_ON_MISSING
(Abbrev:RN_M
)RETURN_DEFVAL_ON_MISSING
(Abbrev:RD_M
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticObjException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonNumber
into ajava.lang.Short
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonNullObjException
- If theJsonObject
property specified by'propertyName'
contains Json-Null, rather than aJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeObjException
- If theJsonObject
property specified by'propertyName'
contains a some other Json-Type rather thanJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.GET(JsonObject, String, int, Number, Class, Function, Function)
,JsonNumber.bigDecimalValue()
- Code:
- Exact Method Body:
return GET( jo, propertyName, FLAGS, defaultValue, Short.class, jn -> jn.bigDecimalValue().shortValueExact(), jn -> jn.bigDecimalValue().shortValue() );
-
parseShort
public static java.lang.Short parseShort (JsonObject jo, java.lang.String propertyName, int FLAGS, short defaultValue, java.util.function.Function<java.lang.String,java.lang.Short> optionalParser)
Retrieve aJsonObject
property containing aJsonString
, and transform it to ajava.lang.Short
, with either a user-provided parser, or the standard java short-integer parser Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value, using the'FLAGS'
parameter.
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException short val = ReadBoxedJSON.parseShort(jsonObj, "myProp", RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns a boxed '-1' if the property contains an // 'unparseable' String, or if it is null. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Short => 'short' // (auto-unboxes the returned reference to a short-primitive) int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_SPEX short val = ReadBoxedJSON.parseShort(jsonObj, "myProp", FLAGS , -1);
- Parameters:
jo
- Any instance ofJsonObject
propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Short
.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.optionalParser
- Any function-pointer to a function that will transform / convert ajava.lang.String
into ajava.lang.Short
.
NOTE: This parameter may be null, and if it is, the standard Java invocation ofShort.parseShort(String.trim())
is used.- Returns:
- If the
JsonObject
property contained aJsonString
, and that string was successfully parsed to ajava.lang.Short
, then it is returned.
If there were errors attempting to transform theString
object-property, and the user-provided flags specified thatdefaultValue
be returned, then that is returned. Null is returned if the flags specified it on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then the relevant exception will throw, instead. - Throws:
JsonPropMissingException
- If'jo'
does not have a property with the name'propertyName'
and none of these Exception-Flags are set:RETURN_NULL_ON_MISSING
(Abbrev:RN_M
)RETURN_DEFVAL_ON_MISSING
(Abbrev:RD_M
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticObjException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonString
into ajava.lang.Short
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonStrParseObjException
- If theJsonObject
property specified by'propertyName'
throws an exception while attempting to parse thejava.lang.String
into ajava.lang.Short
, and none of these Exception-Flags are set: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
)
JsonNullObjException
- If theJsonObject
property specified by'propertyName'
contains Json-Null, rather than aJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeObjException
- If theJsonObject
property specified by'propertyName'
contains a some other Json-Type rather thanJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.PARSE(JsonObject, String, int, Number, Class, Function, Function, Function)
- Code:
- Exact Method Body:
return PARSE( jo, propertyName, FLAGS, defaultValue, Short.class, optionalParser, BigDecimal::shortValueExact, BigDecimal::shortValue );
-
getByte
public static java.lang.Byte getByte(JsonArray ja, int index)
Retrieve aJsonArray
element, and transform it to ajava.lang.Byte
. If Json-Null is found - then Java-Null is returned.
Boxed Java Null's:
This method facilitates translating a Json-Null into a Java-Null since a Java Boxed-Type number is returned from this method, rather than a Java Primitive- Parameters:
ja
- Any instance ofJsonArray
index
- A valid index into array parameter'ja'
. In Json (and unlike Java), array element types are not actually checked for consistency - meaning an array may hold many different types! This array-index must contain aJsonNumber
- Returns:
- An instance of Java-Type
java.lang.Byte
, if and only if the array element located at'index'
uses the appropriate Json-Type -JsonNumber
. If that array-index contains some other type, or thatJsonNumber
cannot be properly transformed to ajava.lang.Byte
, then an exception will throw.
If the specified array-element inside'ja'
has been set to Json-Null, then Java-Null is returned. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of'ja'
JsonTypeArrException
- If the array-element (specified by'index'
) does not actually contain aJsonNumber
(but rather, some other non-null Json-Type), then this exception throws.JsonArithmeticArrException
- Java's implementation ofbyteValueExact()
throws anArithmeticException
when that method cannot correctly transform / convert the element to a validjava.lang.Byte
. The thrown-exception is then wrapped into this exception for the purpose of providing a much improved error-message, and convenience fields.- See Also:
RJInternal.GET(JsonArray, int, Function, Class)
,JsonNumber.bigDecimalValue()
- Code:
- Exact Method Body:
return GET(ja, index, jn -> jn.bigDecimalValue().byteValueExact(), Byte.class);
-
getByte
public static java.lang.Byte getByte(JsonArray ja, int index, int FLAGS, byte defaultValue)
Retrieve aJsonArray
element, and transform it to ajava.lang.Byte
. Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value (using the Flags). Additionally, if anArithmeticException
should throw, that exception-case can also be handled by using the default type-rounding mechanism java provides via methodbyteValue()
(without the'Exact'
word appended to the method-name). See the Java Documentation forBigDecimal.byteValue()
for an explanation about the behavior of flagRETURN_JAPPROX_ON_AEX
and rounding ajava.lang.Byte
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException byte val = ReadBoxedJSON.getByte(jsonArray, 1, RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns a boxed '-1' if the array index is null, // or if the array index contains some non-integer number. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Byte => 'byte' // (auto-unboxes the returned reference to a byte-primitive) byte val = ReadBoxedJSON.getByte (jsonArray, 1, RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_AEX, -1);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing the element to retrieve.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.- Returns:
- If the
JsonArray
index contained a number, and that number was successfully transformed to ajava.lang.Byte
, then it is returned.
If there were errors attempting to transform the array-element, but the user-provided flags have specified that thedefaultValue
be returned, then that is returned. Null is returned if the flags have specified a null-return on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then, instead, the relevant exception will throw. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of input array'ja'
and none of these Exception-Flags are set:RETURN_NULL_ON_IOB
(Abbrev:RN_IOB
)RETURN_DEFVAL_ON_IOB
(Abbrev:RD_IOB
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticArrException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonNumber
into ajava.lang.Byte
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonNullArrException
- If theJsonArray
element located at'index'
contains Json-Null, rather than aJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeArrException
- If theJsonArray
element located at'index'
contains a some other Json-Type rather thanJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.GET(JsonArray, int, int, Number, Class, Function, Function)
,JsonNumber.bigDecimalValue()
- Code:
- Exact Method Body:
return GET( ja, index, FLAGS, defaultValue, Byte.class, jn -> jn.bigDecimalValue().byteValueExact(), jn -> jn.bigDecimalValue().byteValue() );
-
parseByte
public static java.lang.Byte parseByte (JsonArray ja, int index, int FLAGS, byte defaultValue, java.util.function.Function<java.lang.String,java.lang.Byte> optionalParser)
Retrieve aJsonArray
element containing aJsonString
, and transform it to ajava.lang.Byte
, with either a user-provided parser, or the standard java byte parser Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value, using the'FLAGS'
parameter.
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException byte val = ReadBoxedJSON.parseByte(jsonArray, 1, RETURN_NULL_ON_NULL, -1, null); // The following invocation gracefully returns a boxed '-1' if the array index contains // an 'unparseable' String (or is null) // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Byte => 'byte' // (auto-unboxes the returned reference to a byte-primitive) int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_SPEX; byte val = ReadBoxedJSON.parseByte(jsonArray, 1, FLAGS, -1, null);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing theJsonString
element to retrieve.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.optionalParser
- Any function-pointer to a function that will transform / convert ajava.lang.String
into ajava.lang.Byte
.
NOTE: This parameter may be null, and if it is, the standard Java invocation ofByte.parseByte(String.trim())
is used.- Returns:
- If the
JsonArray
index contained aJsonString
, and that string was successfully parsed to ajava.lang.Byte
, then it is returned.
If there were errors attempting to transform theString
array-element, and the user-provided flags specified thatdefaultValue
be returned, then that is returned. Null is returned if the flags specified it on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then the relevant exception will throw, instead. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of input array'ja'
and none of these Exception-Flags are set:RETURN_NULL_ON_IOB
(Abbrev:RN_IOB
)RETURN_DEFVAL_ON_IOB
(Abbrev:RD_IOB
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticArrException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonString
into ajava.lang.Byte
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonStrParseArrException
- If theJsonArray
element located at'index'
throws an exception while attempting to parse thejava.lang.String
into ajava.lang.Byte
, and none of these Exception-Flags are set: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
)
JsonNullArrException
- If theJsonArray
element located at'index'
contains Json-Null, rather than aJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeArrException
- If theJsonArray
element located at'index'
contains a some other Json-Type rather thanJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.PARSE(JsonObject, String, int, Number, Class, Function, Function, Function)
- Code:
- Exact Method Body:
return PARSE( ja, index, FLAGS, defaultValue, Byte.class, optionalParser, BigDecimal::byteValueExact, BigDecimal::byteValue );
-
getByte
public static java.lang.Byte getByte(JsonObject jo, java.lang.String propertyName, boolean isOptional)
Extract aJsonObject
property, and transform it to ajava.lang.Byte
. If Json-Null is found - then Java-Null is returned.
Boxed Java Null's:
This method facilitates translating a Json-Null into a Java-Null since a Java Boxed-Type number is returned from this method, rather than a Java Primitive- Parameters:
jo
- Any instance ofJsonObject
.propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Byte
.isOptional
- WhenTRUE
is passed, if'propertyName'
is not actually listed in'jo'
this method shall return Java-Null gracefully. WhenFALSE
is passed, if'jo'
does not have the specified property, aJsonPropMissingException
will throw.
If'jo'
actually has a property named'propertyName'
, then the value passed to this parameter is fully irrelevant.- Returns:
- An instance of Java-Type
java.lang.Byte
, if and only if the'propertyName'
is available inside'jo'
, and that property uses the appropriate Json-Type -JsonNumber
If the specified property has been set to 'null' (Json-Null) in the passedJsonObject
, then Java-Null is returned. Java-Null is also returned if'propertyName'
is not listed / unavailable in'jo'
andTRUE
has been passed to'isOptional'
.
Other cases shall generate an exception throw. - Throws:
JsonPropMissingException
- This exception shall throw if the specified property is missing from the'JsonObject'
(parameter'jo'
). This exception throw can be avoided if'TRUE'
is passed to parameter'isOptional'
.JsonArithmeticObjException
- Java's implementation ofbyteValueExact()
throws anArithmeticException
when that method cannot correctly transform / convert the element to a validjava.lang.Byte
. The thrown-exception is then wrapped into this exception for the purpose of providing a much improved error-message, and convenience fields.JsonTypeObjException
- If the property (specified by'propertyName'
) is extracted, but that property does not actually contain aJsonNumber
(but rather, some other non-null Json-Type), then this exception throws.- See Also:
RJInternal.GET(JsonObject, String, boolean, Function, Class)
,JsonNumber.bigDecimalValue()
- Code:
- Exact Method Body:
return GET( jo, propertyName, isOptional, jn -> jn.bigDecimalValue().byteValueExact(), Byte.class );
-
getByte
public static java.lang.Byte getByte(JsonObject jo, java.lang.String propertyName, int FLAGS, byte defaultValue)
Retrieve aJsonObject
property, and transform it to ajava.lang.Byte
. Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value (using the Flags). Additionally, if anArithmeticException
should throw, that exception-case can also be handled by using the default type-rounding mechanism java provides via methodintValue()
(without the'Exact'
word appended to the method-name). See the Java Documentation forBigDecimal.intValue()
for an explanation about the behavior of flagRETURN_JAPPROX_ON_AEX
and rounding ajava.lang.Byte
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException int val = ReadBoxedJSON.getByte(jsonObj, "myProp", RETURN_NULL_ON_NULL -1); // The following invocation gracefully returns a boxed '-1' if the property contains a // null, or if the property holds some non-integer number. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Byte => 'int' // (auto-unboxes the returned reference to a int-primitive) int val = ReadBoxedJSON.getByte (jsonObj, "myProp", RETURN_DEFVAL_ON_NULL & RETURN_DEFVAL_ON_AEX, -1);
- Parameters:
jo
- Any instance ofJsonObject
propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Byte
.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.- Returns:
- If the
JsonObject
property contained a number, and that number was successfully transformed to ajava.lang.Byte
, then it is returned.
If there were errors attempting to transform the object-property, and the user-provided flags have specified that thedefaultValue
be returned, then that is returned. Null is returned if the flags have specified a null-return on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then, instead, the relevant exception will throw. - Throws:
JsonPropMissingException
- If'jo'
does not have a property with the name'propertyName'
and none of these Exception-Flags are set:RETURN_NULL_ON_MISSING
(Abbrev:RN_M
)RETURN_DEFVAL_ON_MISSING
(Abbrev:RD_M
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticObjException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonNumber
into ajava.lang.Byte
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonNullObjException
- If theJsonObject
property specified by'propertyName'
contains Json-Null, rather than aJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeObjException
- If theJsonObject
property specified by'propertyName'
contains a some other Json-Type rather thanJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.GET(JsonObject, String, int, Number, Class, Function, Function)
,JsonNumber.bigDecimalValue()
- Code:
- Exact Method Body:
return GET( jo, propertyName, FLAGS, defaultValue, Byte.class, jn -> jn.bigDecimalValue().byteValueExact(), jn -> jn.bigDecimalValue().byteValue() );
-
parseByte
public static java.lang.Byte parseByte (JsonObject jo, java.lang.String propertyName, int FLAGS, byte defaultValue, java.util.function.Function<java.lang.String,java.lang.Byte> optionalParser)
Retrieve aJsonObject
element containing aJsonString
, and transform it to ajava.lang.Byte
, with either a user-provided parser, or the standard java byte parser Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value, using the'FLAGS'
parameter.
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException byte val = ReadBoxedJSON.parseByte(jsonObj, "myProp", RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns a boxed '-1' if the property contains an // 'unparseable' String, or if it is null. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Byte => 'byte' // (auto-unboxes the returned reference to a byte-primitive) int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_SPEX byte val = ReadBoxedJSON.parseByte(jsonObj, "myProp", FLAGS , -1);
- Parameters:
jo
- Any instance ofJsonObject
propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Byte
.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.optionalParser
- Any function-pointer to a function that will transform / convert ajava.lang.String
into ajava.lang.Byte
.
NOTE: This parameter may be null, and if it is, the standard Java invocation ofByte.parseByte(String.trim())
is used.- Returns:
- If the
JsonObject
property contained aJsonString
, and that string was successfully parsed to ajava.lang.Byte
, then it is returned.
If there were errors attempting to transform theString
object-property, and the user-provided flags specified thatdefaultValue
be returned, then that is returned. Null is returned if the flags specified it on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then the relevant exception will throw, instead. - Throws:
JsonPropMissingException
- If'jo'
does not have a property with the name'propertyName'
and none of these Exception-Flags are set:RETURN_NULL_ON_MISSING
(Abbrev:RN_M
)RETURN_DEFVAL_ON_MISSING
(Abbrev:RD_M
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticObjException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonString
into ajava.lang.Byte
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonStrParseObjException
- If theJsonObject
property specified by'propertyName'
throws an exception while attempting to parse thejava.lang.String
into ajava.lang.Byte
, and none of these Exception-Flags are set: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
)
JsonNullObjException
- If theJsonObject
property specified by'propertyName'
contains Json-Null, rather than aJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeObjException
- If theJsonObject
property specified by'propertyName'
contains a some other Json-Type rather thanJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.PARSE(JsonObject, String, int, Number, Class, Function, Function, Function)
- Code:
- Exact Method Body:
return PARSE( jo, propertyName, FLAGS, defaultValue, Byte.class, optionalParser, BigDecimal::byteValueExact, BigDecimal::byteValue );
-
getDouble
public static java.lang.Double getDouble(JsonArray ja, int index)
Retrieve aJsonArray
element, and transform it to ajava.lang.Double
. If Json-Null is found - then Java-Null is returned.
Boxed Java Null's:
This method facilitates translating a Json-Null into a Java-Null since a Java Boxed-Type number is returned from this method, rather than a Java Primitive
NOTE: If the precision of the providedJsonNumber
contains too much information, the extra digits will simply be lost; no exceptions will throw; and the method shall return a rounded answer - gracefully.
If the magnitude of the number is too large or too small, anArithmeticException
will throw, as explained below.- Parameters:
ja
- Any instance ofJsonArray
index
- A valid index into array parameter'ja'
. In Json (and unlike Java), array element types are not actually checked for consistency - meaning an array may hold many different types! This array-index must contain aJsonNumber
- Returns:
- An instance of Java-Type
java.lang.Double
, if and only if the array element located at'index'
uses the appropriate Json-Type -JsonNumber
. If that array-index contains some other type, or thatJsonNumber
cannot be properly transformed to ajava.lang.Double
, then an exception will throw.
If the specified array-element inside'ja'
has been set to Json-Null, then Java-Null is returned. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of'ja'
JsonTypeArrException
- If the array-element (specified by'index'
) does not actually contain aJsonNumber
(but rather, some other non-null Json-Type), then this exception throws.JsonArithmeticArrException
- Java's implementation ofBigDecimal.doubleValue()
will return an infinity constant (Double.NEGATIVE_INFINITY
orDouble.POSITIVE_INFINITY
) when the magnitude of the returned value is too large to fit inside the Java Typejava.lang.Double
.
This method checks for the infinity constants, and then proceeds to throw this exception when they are detected.- See Also:
RJInternal.GET(JsonArray, int, Function, Class)
,RJInternal.DOUBLE_WITH_CHECK(JsonNumber)
- Code:
- Exact Method Body:
return GET(ja, index, RJInternal::DOUBLE_WITH_CHECK, Double.class);
-
getDouble
public static java.lang.Double getDouble(JsonArray ja, int index, int FLAGS, double defaultValue)
Retrieve aJsonArray
element, and transform it to ajava.lang.Double
. Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value (using the Flags). Additionally, if anArithmeticException
should throw, that exception-case can also be handled by using the default type-rounding mechanism java provides via methoddoubleValue()
(without the'Exact'
word appended to the method-name). See the Java Documentation forBigDecimal.doubleValue()
for an explanation about the behavior of flagRETURN_JAPPROX_ON_AEX
and rounding ajava.lang.Double
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException double val = ReadBoxedJSON.getDouble(jsonArray, 1, RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns a boxed '-1' if the array index is null, // or if the array index contains some non-integer number. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Double => 'double' // (auto-unboxes the returned reference to a double-primitive) double val = ReadBoxedJSON.getDouble (jsonArray, 1, RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_AEX, -1);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing the element to retrieve.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.- Returns:
- If the
JsonArray
index contained a number, and that number was successfully transformed to ajava.lang.Double
, then it is returned.
If there were errors attempting to transform the array-element, but the user-provided flags have specified that thedefaultValue
be returned, then that is returned. Null is returned if the flags have specified a null-return on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then, instead, the relevant exception will throw. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of input array'ja'
and none of these Exception-Flags are set:RETURN_NULL_ON_IOB
(Abbrev:RN_IOB
)RETURN_DEFVAL_ON_IOB
(Abbrev:RD_IOB
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticArrException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonNumber
into ajava.lang.Double
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonNullArrException
- If theJsonArray
element located at'index'
contains Json-Null, rather than aJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeArrException
- If theJsonArray
element located at'index'
contains a some other Json-Type rather thanJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.GET(JsonArray, int, int, Number, Class, Function, Function)
,RJInternal.DOUBLE_WITH_CHECK(JsonNumber)
,JsonNumber.bigDecimalValue()
- Code:
- Exact Method Body:
return GET( ja, index, FLAGS, defaultValue, Double.class, RJInternal::DOUBLE_WITH_CHECK, jn -> jn.bigDecimalValue().doubleValue() );
-
parseDouble
public static java.lang.Double parseDouble (JsonArray ja, int index, int FLAGS, double defaultValue, java.util.function.Function<java.lang.String,java.lang.Double> optionalParser)
Retrieve aJsonArray
element containing aJsonString
, and transform it to ajava.lang.Double
, with either a user-provided parser, or the standard java double parser Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value, using the'FLAGS'
parameter.
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException double val = ReadBoxedJSON.parseDouble(jsonArray, 1, RETURN_NULL_ON_NULL, -1, null); // The following invocation gracefully returns a boxed '-1' if the array index contains // an 'unparseable' String (or is null) // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Double => 'double' // (auto-unboxes the returned reference to a double-primitive) int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_SPEX; double val = ReadBoxedJSON.parseDouble(jsonArray, 1, FLAGS, -1, null);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing theJsonString
element to retrieve.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.optionalParser
- Any function-pointer to a function that will transform / convert ajava.lang.String
into ajava.lang.Double
.
NOTE: This parameter may be null, and if it is, the standard Java invocation ofDouble.parseDouble(String.trim())
is used.- Returns:
- If the
JsonArray
index contained aJsonString
, and that string was successfully parsed to ajava.lang.Double
, then it is returned.
If there were errors attempting to transform theString
array-element, and the user-provided flags specified thatdefaultValue
be returned, then that is returned. Null is returned if the flags specified it on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then the relevant exception will throw, instead. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of input array'ja'
and none of these Exception-Flags are set:RETURN_NULL_ON_IOB
(Abbrev:RN_IOB
)RETURN_DEFVAL_ON_IOB
(Abbrev:RD_IOB
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticArrException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonString
into ajava.lang.Double
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticArrException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonString
into ajava.lang.Double
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonStrParseArrException
- If theJsonArray
element located at'index'
throws an exception while attempting to parse thejava.lang.String
into ajava.lang.Double
, and none of these Exception-Flags are set: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
)
JsonNullArrException
- If theJsonArray
element located at'index'
contains Json-Null, rather than aJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeArrException
- If theJsonArray
element located at'index'
contains a some other Json-Type rather thanJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.PARSE(JsonObject, String, int, Number, Class, Function, Function, Function)
- Code:
- Exact Method Body:
return PARSE( ja, index, FLAGS, defaultValue, Double.class, optionalParser, RJInternal::DOUBLE_WITH_CHECK, BigDecimal::doubleValue );
-
getDouble
public static java.lang.Double getDouble(JsonObject jo, java.lang.String propertyName, boolean isOptional)
Extract aJsonObject
property, and transform it to ajava.lang.Double
. If Json-Null is found - then Java-Null is returned.
Boxed Java Null's:
This method facilitates translating a Json-Null into a Java-Null since a Java Boxed-Type number is returned from this method, rather than a Java Primitive
NOTE: If the precision of the providedJsonNumber
contains too much information, the extra digits will simply be lost; no exceptions will throw; and the method shall return a rounded answer - gracefully.
If the magnitude of the number is too large or too small, anArithmeticException
will throw, as explained below.- Parameters:
jo
- Any instance ofJsonObject
.propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Double
.isOptional
- WhenTRUE
is passed, if'propertyName'
is not actually listed in'jo'
this method shall return Java-Null gracefully. WhenFALSE
is passed, if'jo'
does not have the specified property, aJsonPropMissingException
will throw.
If'jo'
actually has a property named'propertyName'
, then the value passed to this parameter is fully irrelevant.- Returns:
- An instance of Java-Type
java.lang.Double
, if and only if the'propertyName'
is available inside'jo'
, and that property uses the appropriate Json-Type -JsonNumber
If the specified property has been set to 'null' (Json-Null) in the passedJsonObject
, then Java-Null is returned. Java-Null is also returned if'propertyName'
is not listed / unavailable in'jo'
andTRUE
has been passed to'isOptional'
.
Other cases shall generate an exception throw. - Throws:
JsonPropMissingException
- This exception shall throw if the specified property is missing from the'JsonObject'
(parameter'jo'
). This exception throw can be avoided if'TRUE'
is passed to parameter'isOptional'
.JsonArithmeticObjException
- Java's implementation ofBigDecimal.doubleValue()
will return an infinity constant (Double.NEGATIVE_INFINITY
orDouble.POSITIVE_INFINITY
) when the magnitude of the returned value is too large to fit inside the Java Typejava.lang.Double
.
This method checks for the infinity constants, and then proceeds to throw this exception when they are detected.JsonTypeObjException
- If the property (specified by'propertyName'
) is extracted, but that property does not actually contain aJsonNumber
(but rather, some other non-null Json-Type), then this exception throws.- See Also:
RJInternal.GET(JsonObject, String, boolean, Function, Class)
,RJInternal.DOUBLE_WITH_CHECK(JsonNumber)
- Code:
- Exact Method Body:
return GET(jo, propertyName, isOptional, RJInternal::DOUBLE_WITH_CHECK, Double.class);
-
getDouble
public static java.lang.Double getDouble(JsonObject jo, java.lang.String propertyName, int FLAGS, double defaultValue)
Retrieve aJsonObject
property, and transform it to ajava.lang.Double
. Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value (using the Flags). Additionally, if anArithmeticException
should throw, that exception-case can also be handled by using the default type-rounding mechanism java provides via methoddoubleValue()
(without the'Exact'
word appended to the method-name). See the Java Documentation forBigDecimal.doubleValue()
for an explanation about the behavior of flagRETURN_JAPPROX_ON_AEX
and rounding ajava.lang.Double
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException double val = ReadBoxedJSON.getDouble(jsonObj, "myProp", RETURN_NULL_ON_NULL -1); // The following invocation gracefully returns a boxed '-1' if the property contains a // null, or if the property holds some non-integer number. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Double => 'double' // (auto-unboxes the returned reference to a double-primitive) double val = ReadBoxedJSON.getDouble (jsonObj, "myProp", RETURN_DEFVAL_ON_NULL & RETURN_DEFVAL_ON_AEX, -1);
- Parameters:
jo
- Any instance ofJsonObject
propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Double
.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.- Returns:
- If the
JsonObject
property contained a number, and that number was successfully transformed to ajava.lang.Double
, then it is returned.
If there were errors attempting to transform the object-property, and the user-provided flags have specified that thedefaultValue
be returned, then that is returned. Null is returned if the flags have specified a null-return on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then, instead, the relevant exception will throw. - Throws:
JsonPropMissingException
- If'jo'
does not have a property with the name'propertyName'
and none of these Exception-Flags are set:RETURN_NULL_ON_MISSING
(Abbrev:RN_M
)RETURN_DEFVAL_ON_MISSING
(Abbrev:RD_M
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticObjException
- Java's implementation ofdoubleValue()
throws anArithmeticException
when that method cannot correctly transform / convert the element to a validjava.lang.Double
. The thrown-exception is then wrapped into this exception for the purpose of providing a much improved error-message, and convenience fields.JsonNullObjException
- If theJsonObject
property specified by'propertyName'
contains Json-Null, rather than aJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeObjException
- If theJsonObject
property specified by'propertyName'
contains a some other Json-Type rather thanJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.GET(JsonObject, String, int, Number, Class, Function, Function)
,RJInternal.DOUBLE_WITH_CHECK(JsonNumber)
,JsonNumber.bigDecimalValue()
- Code:
- Exact Method Body:
return GET( jo, propertyName, FLAGS, defaultValue, Double.class, RJInternal::DOUBLE_WITH_CHECK, jn -> jn.bigDecimalValue().doubleValue() );
-
parseDouble
public static java.lang.Double parseDouble (JsonObject jo, java.lang.String propertyName, int FLAGS, double defaultValue, java.util.function.Function<java.lang.String,java.lang.Double> optionalParser)
Retrieve aJsonObject
property containing aJsonString
, and transform it to ajava.lang.Double
, with either a user-provided parser, or the standard java double parser Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value, using the'FLAGS'
parameter.
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException double val = ReadBoxedJSON.parseDouble(jsonObj, "myProp", RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns a boxed '-1' if the property contains an // 'unparseable' String, or if it is null. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Double => 'double' // (auto-unboxes the returned reference to a double-primitive) int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_SPEX double val = ReadBoxedJSON.parseDouble(jsonObj, "myProp", FLAGS , -1);
- Parameters:
jo
- Any instance ofJsonObject
propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Double
.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.optionalParser
- Any function-pointer to a function that will transform / convert ajava.lang.String
into ajava.lang.Double
.
NOTE: This parameter may be null, and if it is, the standard Java invocation ofDouble.parseDouble(String.trim())
is used.- Returns:
- If the
JsonObject
property contained aJsonString
, and that string was successfully parsed to ajava.lang.Double
, then it is returned.
If there were errors attempting to transform theString
object-property, and the user-provided flags specified thatdefaultValue
be returned, then that is returned. Null is returned if the flags specified it on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then the relevant exception will throw, instead. - Throws:
JsonPropMissingException
- If'jo'
does not have a property with the name'propertyName'
and none of these Exception-Flags are set:RETURN_NULL_ON_MISSING
(Abbrev:RN_M
)RETURN_DEFVAL_ON_MISSING
(Abbrev:RD_M
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticObjException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonString
into ajava.lang.Double
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonStrParseObjException
- If theJsonObject
property specified by'propertyName'
throws an exception while attempting to parse thejava.lang.String
into ajava.lang.Double
, and none of these Exception-Flags are set: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
)
JsonNullObjException
- If theJsonObject
property specified by'propertyName'
contains Json-Null, rather than aJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeObjException
- If theJsonObject
property specified by'propertyName'
contains a some other Json-Type rather thanJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.PARSE(JsonObject, String, int, Number, Class, Function, Function, Function)
- Code:
- Exact Method Body:
return PARSE( jo, propertyName, FLAGS, defaultValue, Double.class, optionalParser, RJInternal::DOUBLE_WITH_CHECK, BigDecimal::doubleValue );
-
getFloat
public static java.lang.Float getFloat(JsonArray ja, int index)
Retrieve aJsonArray
element, and transform it to ajava.lang.Float
. If Json-Null is found - then Java-Null is returned.
Boxed Java Null's:
This method facilitates translating a Json-Null into a Java-Null since a Java Boxed-Type number is returned from this method, rather than a Java Primitive
NOTE: If the precision of the providedJsonNumber
contains too much information, the extra digits will simply be lost; no exceptions will throw; and the method shall return a rounded answer - gracefully.
If the magnitude of the number is too large or too small, anArithmeticException
will throw, as explained below.- Parameters:
ja
- Any instance ofJsonArray
index
- A valid index into array parameter'ja'
. In Json (and unlike Java), array element types are not actually checked for consistency - meaning an array may hold many different types! This array-index must contain aJsonNumber
- Returns:
- An instance of Java-Type
java.lang.Float
, if and only if the array element located at'index'
uses the appropriate Json-Type -JsonNumber
. If that array-index contains some other type, or thatJsonNumber
cannot be properly transformed to ajava.lang.Float
, then an exception will throw.
If the specified array-element inside'ja'
has been set to Json-Null, then Java-Null is returned. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of'ja'
JsonTypeArrException
- If the array-element (specified by'index'
) does not actually contain aJsonNumber
(but rather, some other non-null Json-Type), then this exception throws.JsonArithmeticArrException
- Java's implementation ofBigDecimal.floatValue()
will return an infinity constant (Float.NEGATIVE_INFINITY
orFloat.POSITIVE_INFINITY
) when the magnitude of the returned value is too large to fit inside the Java Typejava.lang.Float
.
This method checks for the infinity constants, and then proceeds to throw this exception when they are detected.- See Also:
RJInternal.GET(JsonArray, int, Function, Class)
,RJInternal.FLOAT_WITH_CHECK(JsonNumber)
- Code:
- Exact Method Body:
return GET(ja, index, RJInternal::FLOAT_WITH_CHECK, Float.class);
-
getFloat
public static java.lang.Float getFloat(JsonArray ja, int index, int FLAGS, float defaultValue)
Retrieve aJsonArray
element, and transform it to ajava.lang.Float
. Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value (using the Flags). Additionally, if anArithmeticException
should throw, that exception-case can also be handled by using the default type-rounding mechanism java provides via methodfloatValue()
(without the'Exact'
word appended to the method-name). See the Java Documentation forBigDecimal.floatValue()
for an explanation about the behavior of flagRETURN_JAPPROX_ON_AEX
and rounding ajava.lang.Float
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException float val = ReadBoxedJSON.getFloat(jsonArray, 1, RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns a boxed '-1' if the array index is null, // or if the array index contains some non-integer number. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Float => 'float' // (auto-unboxes the returned reference to a float-primitive) float val = ReadBoxedJSON.getFloat (jsonArray, 1, RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_AEX, -1);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing the element to retrieve.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.- Returns:
- If the
JsonArray
index contained a number, and that number was successfully transformed to ajava.lang.Float
, then it is returned.
If there were errors attempting to transform the array-element, but the user-provided flags have specified that thedefaultValue
be returned, then that is returned. Null is returned if the flags have specified a null-return on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then, instead, the relevant exception will throw. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of input array'ja'
and none of these Exception-Flags are set:RETURN_NULL_ON_IOB
(Abbrev:RN_IOB
)RETURN_DEFVAL_ON_IOB
(Abbrev:RD_IOB
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticArrException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonNumber
into ajava.lang.Float
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonNullArrException
- If theJsonArray
element located at'index'
contains Json-Null, rather than aJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeArrException
- If theJsonArray
element located at'index'
contains a some other Json-Type rather thanJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.GET(JsonArray, int, int, Number, Class, Function, Function)
,RJInternal.FLOAT_WITH_CHECK(JsonNumber)
,JsonNumber.bigDecimalValue()
- Code:
- Exact Method Body:
return GET( ja, index, FLAGS, defaultValue, Float.class, RJInternal::FLOAT_WITH_CHECK, jn -> jn.bigDecimalValue().floatValue() );
-
parseFloat
public static java.lang.Float parseFloat (JsonArray ja, int index, int FLAGS, float defaultValue, java.util.function.Function<java.lang.String,java.lang.Float> optionalParser)
Retrieve aJsonArray
element containing aJsonString
, and transform it to ajava.lang.Float
, with either a user-provided parser, or the standard java float parser Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value, using the'FLAGS'
parameter.
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException float val = ReadBoxedJSON.parseFloat(jsonArray, 1, RETURN_NULL_ON_NULL, -1, null); // The following invocation gracefully returns a boxed '-1' if the array index contains // an 'unparseable' String (or is null) // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Float => 'float' // (auto-unboxes the returned reference to a float-primitive) int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_SPEX; float val = ReadBoxedJSON.parseFloat(jsonArray, 1, FLAGS, -1, null);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing theJsonString
element to retrieve.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.optionalParser
- Any function-pointer to a function that will transform / convert ajava.lang.String
into ajava.lang.Float
.
NOTE: This parameter may be null, and if it is, the standard Java invocation ofFloat.parseFloat(String.trim())
is used.- Returns:
- If the
JsonArray
index contained aJsonString
, and that string was successfully parsed to ajava.lang.Float
, then it is returned.
If there were errors attempting to transform theString
array-element, and the user-provided flags specified thatdefaultValue
be returned, then that is returned. Null is returned if the flags specified it on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then the relevant exception will throw, instead. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of input array'ja'
and none of these Exception-Flags are set:RETURN_NULL_ON_IOB
(Abbrev:RN_IOB
)RETURN_DEFVAL_ON_IOB
(Abbrev:RD_IOB
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticArrException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonString
into ajava.lang.Float
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonStrParseArrException
- If theJsonArray
element located at'index'
throws an exception while attempting to parse thejava.lang.String
into ajava.lang.Float
, and none of these Exception-Flags are set: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
)
JsonNullArrException
- If theJsonArray
element located at'index'
contains Json-Null, rather than aJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeArrException
- If theJsonArray
element located at'index'
contains a some other Json-Type rather thanJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.PARSE(JsonObject, String, int, Number, Class, Function, Function, Function)
- Code:
- Exact Method Body:
return PARSE( ja, index, FLAGS, defaultValue, Float.class, optionalParser, RJInternal::FLOAT_WITH_CHECK, BigDecimal::floatValue );
-
getFloat
public static java.lang.Float getFloat(JsonObject jo, java.lang.String propertyName, boolean isOptional)
Extract aJsonObject
property, and transform it to ajava.lang.Float
. If Json-Null is found - then Java-Null is returned.
Boxed Java Null's:
This method facilitates translating a Json-Null into a Java-Null since a Java Boxed-Type number is returned from this method, rather than a Java Primitive
NOTE: If the precision of the providedJsonNumber
contains too much information, the extra digits will simply be lost; no exceptions will throw; and the method shall return a rounded answer - gracefully.
If the magnitude of the number is too large or too small, anArithmeticException
will throw, as explained below.- Parameters:
jo
- Any instance ofJsonObject
.propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Float
.isOptional
- WhenTRUE
is passed, if'propertyName'
is not actually listed in'jo'
this method shall return Java-Null gracefully. WhenFALSE
is passed, if'jo'
does not have the specified property, aJsonPropMissingException
will throw.
If'jo'
actually has a property named'propertyName'
, then the value passed to this parameter is fully irrelevant.- Returns:
- An instance of Java-Type
java.lang.Float
, if and only if the'propertyName'
is available inside'jo'
, and that property uses the appropriate Json-Type -JsonNumber
If the specified property has been set to 'null' (Json-Null) in the passedJsonObject
, then Java-Null is returned. Java-Null is also returned if'propertyName'
is not listed / unavailable in'jo'
andTRUE
has been passed to'isOptional'
.
Other cases shall generate an exception throw. - Throws:
JsonPropMissingException
- This exception shall throw if the specified property is missing from the'JsonObject'
(parameter'jo'
). This exception throw can be avoided if'TRUE'
is passed to parameter'isOptional'
.JsonArithmeticObjException
- Java's implementation ofBigDecimal.floatValue()
will return an infinity constant (Float.NEGATIVE_INFINITY
orFloat.POSITIVE_INFINITY
) when the magnitude of the returned value is too large to fit inside the Java Typejava.lang.Float
.
This method checks for the infinity constants, and then proceeds to throw this exception when they are detected.JsonTypeObjException
- If the property (specified by'propertyName'
) is extracted, but that property does not actually contain aJsonNumber
(but rather, some other non-null Json-Type), then this exception throws.- See Also:
RJInternal.GET(JsonObject, String, boolean, Function, Class)
,RJInternal.FLOAT_WITH_CHECK(JsonNumber)
- Code:
- Exact Method Body:
return GET(jo, propertyName, isOptional, RJInternal::FLOAT_WITH_CHECK, Float.class);
-
getFloat
public static java.lang.Float getFloat(JsonObject jo, java.lang.String propertyName, int FLAGS, float defaultValue)
Retrieve aJsonObject
property, and transform it to ajava.lang.Float
. Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value (using the Flags). Additionally, if anArithmeticException
should throw, that exception-case can also be handled by using the default type-rounding mechanism java provides via methodintValue()
(without the'Exact'
word appended to the method-name). See the Java Documentation forBigDecimal.intValue()
for an explanation about the behavior of flagRETURN_JAPPROX_ON_AEX
and rounding ajava.lang.Float
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException int val = ReadBoxedJSON.getFloat(jsonObj, "myProp", RETURN_NULL_ON_NULL -1); // The following invocation gracefully returns a boxed '-1' if the property contains a // null, or if the property holds some non-integer number. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Float => 'int' // (auto-unboxes the returned reference to a int-primitive) int val = ReadBoxedJSON.getFloat (jsonObj, "myProp", RETURN_DEFVAL_ON_NULL & RETURN_DEFVAL_ON_AEX, -1);
- Parameters:
jo
- Any instance ofJsonObject
propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Float
.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.- Returns:
- If the
JsonObject
property contained a number, and that number was successfully transformed to ajava.lang.Float
, then it is returned.
If there were errors attempting to transform the object-property, and the user-provided flags have specified that thedefaultValue
be returned, then that is returned. Null is returned if the flags have specified a null-return on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then, instead, the relevant exception will throw. - Throws:
JsonPropMissingException
- If'jo'
does not have a property with the name'propertyName'
and none of these Exception-Flags are set:RETURN_NULL_ON_MISSING
(Abbrev:RN_M
)RETURN_DEFVAL_ON_MISSING
(Abbrev:RD_M
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticObjException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonNumber
into ajava.lang.Float
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonNullObjException
- If theJsonObject
property specified by'propertyName'
contains Json-Null, rather than aJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeObjException
- If theJsonObject
property specified by'propertyName'
contains a some other Json-Type rather thanJsonNumber
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.GET(JsonObject, String, int, Number, Class, Function, Function)
,RJInternal.FLOAT_WITH_CHECK(JsonNumber)
,JsonNumber.bigDecimalValue()
- Code:
- Exact Method Body:
return GET( jo, propertyName, FLAGS, defaultValue, Float.class, RJInternal::FLOAT_WITH_CHECK, jn -> jn.bigDecimalValue().floatValue() );
-
parseFloat
public static java.lang.Float parseFloat (JsonObject jo, java.lang.String propertyName, int FLAGS, float defaultValue, java.util.function.Function<java.lang.String,java.lang.Float> parser)
Retrieve aJsonObject
property containing aJsonString
, and transform it to ajava.lang.Float
, with either a user-provided parser, or the standard java float parser Please see the class documentation forJFlag
for details about the error-handling flags provided by this method. Note that using the (lesser-known) Java-Syntax below, the flag-constants may be referenced without typing their full names at each use:
// Note the use of the 'static' modifier here import static Torello.Java.Additional.JFlag.*;
This method allows for many type-transformaton errors to be handled by converting the returned answer into either Java-Null, or a user-provided default-value, using the'FLAGS'
parameter.
Auto Un-Boxing:
Java's compile-time type-magic allows a user to assign this method's Boxed-Type return-value to its matching Primitive Type (It is called automaticboxing
andunboxing
).
Just make sure to remember that if this method does return null because of erroneous JSON, and because the germaine Exception-Flags aren't set, your code would throw aNullPointerException
! Compile Time Type Inferencing allows for:
Example:
// If null is actually returned, this would throw NullPointerException float val = ReadBoxedJSON.parseFloat(jsonObj, "myProp", RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns a boxed '-1' if the property contains an // 'unparseable' String, or if it is null. // // Note that Java's Type Inferencing auto-converts the boxed java.lang.Float => 'float' // (auto-unboxes the returned reference to a float-primitive) int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_SPEX float val = ReadBoxedJSON.parseFloat(jsonObj, "myProp", FLAGS , -1);
- Parameters:
jo
- Any instance ofJsonObject
propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Float
.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.parser
- Any function-pointer to a function that will transform / convert ajava.lang.String
into ajava.lang.Float
.
NOTE: This parameter may be null, and if it is, the standard Java invocation ofFloat.parseFloat(String.trim())
is used.- Returns:
- If the
JsonObject
property contained aJsonString
, and that string was successfully parsed to ajava.lang.Float
, then it is returned.
If there were errors attempting to transform theString
object-property, and the user-provided flags specified thatdefaultValue
be returned, then that is returned. Null is returned if the flags specified it on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then the relevant exception will throw, instead. - Throws:
JsonPropMissingException
- If'jo'
does not have a property with the name'propertyName'
and none of these Exception-Flags are set:RETURN_NULL_ON_MISSING
(Abbrev:RN_M
)RETURN_DEFVAL_ON_MISSING
(Abbrev:RD_M
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonArithmeticObjException
- If the number converstion function throws anArithmeticException
while attempting to convert theJsonString
into ajava.lang.Float
, and none of these Exception-Flags are set:RETURN_NULL_ON_AEX
(Abbrev:RN_AEX
)RETURN_DEFVAL_ON_AEX
(Abbrev:RN_AEX
)RETURN_JAPPROX_ON_AEX
(Abbrev:RJA_AEX
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonStrParseObjException
- If theJsonObject
property specified by'propertyName'
throws an exception while attempting to parse thejava.lang.String
into ajava.lang.Float
, and none of these Exception-Flags are set: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
)
JsonNullObjException
- If theJsonObject
property specified by'propertyName'
contains Json-Null, rather than aJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeObjException
- If theJsonObject
property specified by'propertyName'
contains a some other Json-Type rather thanJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
RJInternal.PARSE(JsonObject, String, int, Number, Class, Function, Function, Function)
- Code:
- Exact Method Body:
return PARSE( jo, propertyName, FLAGS, defaultValue, Float.class, parser, RJInternal::FLOAT_WITH_CHECK, BigDecimal::floatValue );
-
getBoolean
public static java.lang.Boolean getBoolean(JsonArray ja, int index)
Retrieve aJsonArray
element, and transform it to ajava.lang.Boolean
. If Json-Null is found - then Java-Null is returned.
Boxed Java Null's:
This method facilitates translating a Json-Null into a Java-Null since a Java Boxed-Type number is returned from this method, rather than a Java Primitive- Parameters:
ja
- Any instance ofJsonArray
index
- A valid index into array parameter'ja'
. In Json (and unlike Java), array element types are not actually checked for consistency - meaning an array may hold many different types! This array-index must contain aJSON-BOOLEAN
- Returns:
- An instance of Java-Type
java.lang.Boolean
, if and only if the array element located at'index'
uses the appropriate Json-Type -JSON-BOOLEAN
. If that array-index contains some other type, or thatJSON-BOOLEAN
cannot be properly transformed to ajava.lang.Boolean
, then an exception will throw.
If the specified array-element inside'ja'
has been set to Json-Null, then Java-Null is returned. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of'ja'
JsonTypeArrException
- If the array-element (specified by'index'
) does not actually contain aJSON-BOOLEAN
(but rather, some other non-null Json-Type), then this exception throws.- See Also:
JsonValue.getValueType()
,JsonValue.ValueType.TRUE
,JsonValue.ValueType.FALSE
,JsonValue.TRUE
,JsonValue.FALSE
- Code:
- Exact Method Body:
// This will throw an IndexOutOfBoundsException if the index is out of bounds. JsonValue jv = ja.get(index); switch (jv.getValueType()) { // This method 'getBoolean' allows for null-returns. If Json-Null, return Java-Null. case NULL: return null; // AGAIN: "Type-Mapping" or "Type-Translating" really isn't rocket-science case TRUE: return true; case FALSE: return false; // The JsonValue at the specified array-index does not contain one of the two // Json-Boolean Types. Throw an exception. default: throw new JsonTypeArrException(ja, index, TRUE, jv, Boolean.class); }
-
getBoolean
public static java.lang.Boolean getBoolean(JsonArray ja, int index, int FLAGS, boolean defaultValue)
Retrieve aJsonArray
element, and transform it to ajava.lang.Boolean
. If Json-Null is found - then Java-Null is returned.
Boxed Java Null's:
This method facilitates translating a Json-Null into a Java-Null since a Java Boxed-Type number is returned from this method, rather than a Java Primitive- Parameters:
ja
- Any instance ofJsonArray
index
- A valid index into array parameter'ja'
. In Json (and unlike Java), array element types are not actually checked for consistency - meaning an array may hold many different types! This array-index must contain aJSON-BOOLEAN
FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.- Returns:
- If the
JsonArray
index contained a number, and that number was successfully transformed to ajava.lang.Boolean
, then it is returned.
If there were errors attempting to transform the array-element, but the user-provided flags have specified that thedefaultValue
be returned, then that is returned. Null is returned if the flags have specified a null-return on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then, instead, the relevant exception will throw. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of input array'ja'
and none of these Exception-Flags are set:RETURN_NULL_ON_IOB
(Abbrev:RN_IOB
)RETURN_DEFVAL_ON_IOB
(Abbrev:RD_IOB
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonNullArrException
- If theJsonObject
property specified by'propertyName'
contains Json-Null, rather than aJSON-BOOLEAN
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeArrException
- If theJsonObject
property specified by'propertyName'
contains a some other Json-Type rather thanJSON-BOOLEAN
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
JsonValue.getValueType()
,JsonValue.ValueType.TRUE
,JsonValue.ValueType.FALSE
,JsonValue.TRUE
,JsonValue.FALSE
- Code:
- Exact Method Body:
// When TRUE, the index provided turned out to be outside of the bounds of the array. The // IndexOutOfBounds "handler" (the method called here) will check the FLAGS, and: // // 1) return the defaultValue (if Requested by 'FLAGS' for IOOBEX) // 2) return null (if Requested by 'FLAGS' for IOOBEX) // 3) throw IndexOutOfBoundsException if (index >= ja.size()) return IOOBEX(ja, index, defaultValue, FLAGS); JsonValue jv = ja.get(index); // Throw an IndexOutOfBoundsException switch (jv.getValueType()) { // When a 'NULL' (Json-Null) JsonValue is present, the JsonNullArrException 'handler' // will do one of the following: // // 1) return the defaultValue (if Requested by 'FLAGS' for JNAEX) // 2) return null (if Requested by 'FLAGS' for JNAEX) // 3) throw JsonNullArrException case NULL: return JNAEX(ja, index, defaultValue, FLAGS, TRUE, Boolean.class); // AGAIN: "Type-Mapping" or "Type-Translating" really isn't rocket-science case TRUE: return true; case FALSE: return false; // The JsonValue at the specified array-index does not contain one of the two // Json-Boolean Types. The "JsonTypeArrException Handler" will do one of these: // // 1) return the defaultValue (if Requested by 'FLAGS' for JTAEX) // 2) return null (if Requested by 'FLAGS' for JTAEX) // 3) throw JsonTypeArrException default: return JTAEX(ja, index, defaultValue, FLAGS, TRUE, jv, Boolean.class); }
-
parseBoolean
public static java.lang.Boolean parseBoolean (JsonArray ja, int index, int FLAGS, boolean defaultValue, java.util.function.Function<java.lang.String,java.lang.Boolean> optionalParser)
Retrieve aJsonArray
element containing aJsonString
, and transform it to ajava.lang.Boolean
, with either a user-provided parser, or the standard java boolean parser- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing theJsonString
element to retrieve.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.optionalParser
- Any function-pointer to a function that will transform / convert ajava.lang.String
into ajava.lang.Boolean
.
NOTE: This parameter may be null, and if it is, the standard Java invocation ofBoolean.parseBoolean(String.trim())
is used.- Returns:
- If the
JsonArray
index contained aJsonString
, and that string was successfully parsed to ajava.lang.Boolean
, then it is returned.
If there were errors attempting to transform theString
array-element, and the user-provided flags specified thatdefaultValue
be returned, then that is returned. Null is returned if the flags specified it on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then the relevant exception will throw, instead. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of input array'ja'
and none of these Exception-Flags are set:RETURN_NULL_ON_IOB
(Abbrev:RN_IOB
)RETURN_DEFVAL_ON_IOB
(Abbrev:RD_IOB
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonStrParseArrException
- If theJsonArray
element located at'index'
throws an exception while attempting to parse thejava.lang.String
into ajava.lang.Boolean
, and none of these Exception-Flags are set: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
)
JsonNullArrException
- If theJsonArray
element located at'index'
contains Json-Null, rather than aJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeArrException
- If theJsonArray
element located at'index'
contains a some other Json-Type rather thanJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- Code:
- Exact Method Body:
// When TRUE, the index provided turned out to be outside of the bounds of the array. The // IndexOutOfBounds "handler" (the method called here) will check the FLAGS, and: // // 1) return the defaultValue (if Requested by 'FLAGS' for IOOBEX) // 2) return null (if Requested by 'FLAGS' for IOOBEX) // 3) throw IndexOutOfBoundsException if (index >= ja.size()) return IOOBEX(ja, index, defaultValue, FLAGS); JsonValue jv = ja.get(index); switch (jv.getValueType()) { // When a 'NULL' (Json-Null) JsonValue is present, the JsonNullArrException 'handler' // will do one of the following: // // 1) return the defaultValue (if Requested by 'FLAGS' for JNAEX) // 2) return null (if Requested by 'FLAGS' for JNAEX) // 3) throw JsonNullArrException case NULL: return JNAEX(ja, index, defaultValue, FLAGS, STRING, Boolean.class); case STRING: String s = ((JsonString) jv).getString(); // NOTE: This isn't actually an "Exception Case", and if the user hasn't made // a request, the empty-string is passed to whatever parser is configured if (s.length() == 0) { if ((FLAGS & RETURN_NULL_ON_0LEN_STR) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_0LEN_STR) != 0) return defaultValue; if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; } try { return (optionalParser != null) ? optionalParser.apply(s) : Boolean.parseBoolean(s.trim()); } // HANDLER STRIKES AGAIN! - but this time for "JsonStrParseArrException" // RETURNS: null, or defaultValue, (otherwise throws JsonStrParseArrException) catch (Exception e) { return JSPAEX(e, ja, index, defaultValue, FLAGS, jv, Boolean.class); } // The JsonValue at the specified array-index does not contain an JsonString. // The "JsonTypeArrException Handler" will do one of these: // // 1) return the defaultValue (if Requested by 'FLAGS' for JTAEX) // 2) return null (if Requested by 'FLAGS' for JTAEX) // 3) throw JsonTypeArrException default: return JTAEX(ja, index, defaultValue, FLAGS, STRING, jv, Boolean.class); }
-
getBoolean
public static java.lang.Boolean getBoolean(JsonObject jo, java.lang.String propertyName, boolean isOptional)
Extract aJsonObject
property, and transform it to ajava.lang.Boolean
. If Json-Null is found - then Java-Null is returned.
Boxed Java Null's:
This method facilitates translating a Json-Null into a Java-Null since a Java Boxed-Type number is returned from this method, rather than a Java Primitive- Parameters:
jo
- Any instance ofJsonObject
.propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Boolean
.isOptional
- WhenTRUE
is passed, if'propertyName'
is not actually listed in'jo'
this method shall return Java-Null gracefully. WhenFALSE
is passed, if'jo'
does not have the specified property, aJsonPropMissingException
will throw.
If'jo'
actually has a property named'propertyName'
, then the value passed to this parameter is fully irrelevant.- Returns:
- An instance of Java-Type
java.lang.Boolean
, if and only if the'propertyName'
is available inside'jo'
, and that property uses the appropriate Json-Type -JSON-BOOLEAN
If the specified property has been set to 'null' (Json-Null) in the passedJsonObject
, then Java-Null is returned. Java-Null is also returned if'propertyName'
is not listed / unavailable in'jo'
andTRUE
has been passed to'isOptional'
.
Other cases shall generate an exception throw. - Throws:
JsonPropMissingException
- This exception shall throw if the specified property is missing from the'JsonObject'
(parameter'jo'
). This exception throw can be avoided if'TRUE'
is passed to parameter'isOptional'
.JsonTypeObjException
- If the property (specified by'propertyName'
) is extracted, but that property does not actually contain aJSON-BOOLEAN
(but rather, some other non-null Json-Type), then this exception throws.- See Also:
JsonValue.getValueType()
,JsonValue.ValueType.TRUE
,JsonValue.ValueType.FALSE
,JsonValue.TRUE
,JsonValue.FALSE
- Code:
- Exact Method Body:
if (! jo.containsKey(propertyName)) { if (isOptional) return null; throw new JsonPropMissingException(jo, propertyName, TRUE, Boolean.class); } JsonValue jv = jo.get(propertyName); switch (jv.getValueType()) { // This method 'getBoolean' allows for null-returns. If Json-Null, return Java-Null. case NULL: return null; // AGAIN: "Type-Mapping" or "Type-Translating" really isn't rocket-science case TRUE: return true; case FALSE: return false; // The JsonValue at the specified array-index does not contain one of the two // Json-Boolean Types. Throw an exception. default: throw new JsonTypeObjException (jo, propertyName, TRUE, jv, Boolean.class); }
-
getBoolean
public static java.lang.Boolean getBoolean(JsonObject jo, java.lang.String propertyName, int FLAGS, boolean defaultValue)
Retrieve aJsonObject
property, and transform it to ajava.lang.Boolean
. If Json-Null is found - then Java-Null is returned.
Boxed Java Null's:
This method facilitates translating a Json-Null into a Java-Null since a Java Boxed-Type number is returned from this method, rather than a Java Primitive- Parameters:
jo
- Any instance ofJsonObject
propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Boolean
.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.- Returns:
- If the
JsonObject
property contained a number, and that number was successfully transformed to ajava.lang.Boolean
, then it is returned.
If there were errors attempting to transform the object-property, and the user-provided flags have specified that thedefaultValue
be returned, then that is returned. Null is returned if the flags have specified a null-return on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then, instead, the relevant exception will throw. - Throws:
JsonPropMissingException
- If'jo'
does not have a property with the name'propertyName'
and none of these Exception-Flags are set:RETURN_NULL_ON_MISSING
(Abbrev:RN_M
)RETURN_DEFVAL_ON_MISSING
(Abbrev:RD_M
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonNullObjException
- If theJsonObject
property specified by'propertyName'
contains Json-Null, rather than aJSON-BOOLEAN
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeObjException
- If theJsonObject
property specified by'propertyName'
contains a some other Json-Type rather thanJSON-BOOLEAN
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- See Also:
JsonValue.getValueType()
,JsonValue.ValueType.TRUE
,JsonValue.ValueType.FALSE
,JsonValue.TRUE
,JsonValue.FALSE
- Code:
- Exact Method Body:
JsonValue jv = jo.get(propertyName); // When TRUE, the user-specified 'property' (named by 'propertyName') isn't actually one // of the listed properties inside the JsonObject. The JsonPropMissingException "handler" // (the method called here) will check the FLAGS, and: // // 1) return the defaultValue (if Requested by 'FLAGS' for JPMEX) // 2) return null (if Requested by 'FLAGS' for JPMEX) // 3) throw JsonPropMissingException if (jv == null) return JPMEX(jo, propertyName, defaultValue, FLAGS, TRUE, Boolean.class); switch (jv.getValueType()) { // When a 'NULL' (Json-Null) JsonValue is present, the JsonNullObjException 'handler' // will do one of the following: // // 1) return the defaultValue (if Requested by 'FLAGS' for JNOEX) // 2) return null (if Requested by 'FLAGS' for JNOEX) // 3) throw JsonNullArrException case NULL: return JNOEX(jo, propertyName, defaultValue, FLAGS, TRUE, Boolean.class); // AGAIN: "Type-Mapping" or "Type-Translating" really isn't rocket-science case TRUE: return true; case FALSE: return false; // The property contains a JsonValue that is not one of the two // Json-Boolean Types. Throw an exception. // The JsonValue of 'propertyName' does not contain an JsonString. // The "JsonTypeObjException Handler" will do one of these: // // 1) return the defaultValue (if Requested by 'FLAGS' for JTOEX) // 2) return null (if Requested by 'FLAGS' for JTOEX) // 3) throw JsonTypeObjException default: return JTOEX(jo, propertyName, defaultValue, FLAGS, TRUE, jv, Boolean.class); }
-
parseBoolean
public static java.lang.Boolean parseBoolean (JsonObject jo, java.lang.String propertyName, int FLAGS, boolean defaultValue, java.util.function.Function<java.lang.String,java.lang.Boolean> parser)
Retrieve aJsonObject
property containing aJsonString
, and transform it to ajava.lang.Boolean
, with either a user-provided parser, or the standard java boolean parser- Parameters:
jo
- Any instance ofJsonObject
propertyName
- The name of the JSON property that should be contained within'jo'
. This property will be retrieved and subsequently transformed / converted to Java-Typejava.lang.Float
.FLAGS
- Return-value / exception-throw constants defined inJFlag
defaultValue
- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'
parameter! Specifically, if'FLAGS'
were passed'0'
, then this parameter is fully-irrelevant and ignored. The exception flags are listed in inner-classJFlags
AGAIN: If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.parser
- Any function-pointer to a function that will transform / convert ajava.lang.String
into ajava.lang.Float
.
NOTE: This parameter may be null, and if it is, the standard Java invocation ofFloat.parseFloat(String.trim())
is used.- Returns:
- If the
JsonObject
property contained aJsonString
, and that string was successfully parsed to ajava.lang.Float
, then it is returned.
If there were errors attempting to transform theString
object-property, and the user-provided flags specified thatdefaultValue
be returned, then that is returned. Null is returned if the flags specified it on error.
Ifflags
has been passed a'0'
, and an error-case has arisen, then the relevant exception will throw, instead. - Throws:
java.lang.IndexOutOfBoundsException
- If'index'
is out of the bounds of input array'ja'
and none of these Exception-Flags are set:RETURN_NULL_ON_IOB
(Abbrev:RN_IOB
)RETURN_DEFVAL_ON_IOB
(Abbrev:RD_IOB
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonStrParseObjException
- If theJsonObject
property specified by'propertyName'
throws an exception while attempting to parse thejava.lang.String
into ajava.lang.Float
, and none of these Exception-Flags are set: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
)
JsonNullObjException
- If theJsonObject
property specified by'propertyName'
contains Json-Null, rather than aJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_NULL
(Abbrev:RN_N
)RETURN_DEFVAL_ON_NULL
(Abbrev:RD_N
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
JsonTypeObjException
- If theJsonObject
property specified by'propertyName'
contains a some other Json-Type rather thanJsonString
, and none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE
(Abbrev:RN_WT
)RETURN_DEFVAL_ON_WRONG_JSONTYPE
(Abbrev:RD_WT
)RETURN_NULL_ON_ANY_ALL
(Abbrev:RN_AA
)RETURN_DEFVAL_ON_ANY_ALL
(Abbrev:RD_AA
)
- Code:
- Exact Method Body:
JsonValue jv = jo.get(propertyName); // When TRUE, the user-specified 'property' (named by 'propertyName') isn't actually one // of the listed properties inside the JsonObject. The JsonPropMissingException "handler" // (the method called here) will check the FLAGS, and: // // 1) return the defaultValue (if Requested by 'FLAGS' for JPMEX) // 2) return null (if Requested by 'FLAGS' for JPMEX) // 3) throw JsonPropMissingException if (jv == null) return JPMEX(jo, propertyName, defaultValue, FLAGS, STRING, Boolean.class); switch (jv.getValueType()) { // When a 'NULL' (Json-Null) JsonValue is present, the JsonNullObjException 'handler' // will do one of the following: // // 1) return the defaultValue (if Requested by 'FLAGS' for JNOEX) // 2) return null (if Requested by 'FLAGS' for JNOEX) // 3) throw JsonNullArrException case NULL: return JNOEX(jo, propertyName, defaultValue, FLAGS, STRING, Boolean.class); case STRING: String s = ((JsonString) jv).getString(); // NOTE: This isn't actually an "Exception Case", and if the user hasn't made // a request, the empty-string is passed to whatever parser is configured if (s.length() == 0) { if ((FLAGS & RETURN_NULL_ON_0LEN_STR) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_0LEN_STR) != 0) return defaultValue; if ((FLAGS & RETURN_NULL_ON_ANY_ALL) != 0) return null; if ((FLAGS & RETURN_DEFVAL_ON_ANY_ALL) != 0) return defaultValue; } try { return (parser != null) ? parser.apply(s) : Boolean.parseBoolean(s.trim()); } // HANDLER STRIKES AGAIN! - but this time for "JsonStrParseObjException" // RETURNS: null, or defaultValue, (otherwise throws JsonStrParseObjException) catch (Exception e) { return JSPOEX(e, jo, propertyName, defaultValue, FLAGS, jv, Boolean.class); } // The JsonValue of 'propertyName' does not contain an JsonString. // The "JsonTypeObjException Handler" will do one of these: // // 1) return the defaultValue (if Requested by 'FLAGS' for JTOEX) // 2) return null (if Requested by 'FLAGS' for JTOEX) // 3) throw JsonTypeObjException default: return JTOEX(jo, propertyName, defaultValue, FLAGS, STRING, jv, Boolean.class); }
-
-