Package Torello.Java.JSON
Class ReadNumberJSON
- java.lang.Object
-
- Torello.Java.JSON.ReadNumberJSON
-
public class ReadNumberJSON 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 the Best-Fitjava.lang.Number
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
- See Also:
Json
,JsonObject
,JsonArray
Hi-Lited Source-Code:- View Here: Torello/Java/JSON/ReadNumberJSON.java
- Open New Browser-Tab: Torello/Java/JSON/ReadNumberJSON.java
File Size: 16,049 Bytes Line Count: 332 '\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
- 8 Method(s), 8 declared static
- 0 Field(s)
- Utilizes the Java-Standard
-
-
Method Summary
Best-Fit: Guess appropriate Java Number-Type out of a JsonArray
Modifier and Type Method static Number
get(JsonArray ja, int index, boolean throwOnNull)
static Number
get(JsonArray ja, int index, int FLAGS, Number defaultValue)
static Number
parse(JsonArray ja, int index, int FLAGS, Number defaultValue, Function<String,Number> optionalParser)
Best-Fit: Guess appropriate Java Number-Type out of a JsonObject
Modifier and Type Method static Number
get(JsonObject jo, String propertyName, boolean isOptional, boolean throwOnNull)
static Number
get(JsonObject jo, String propertyName, int FLAGS, Number defaultValue)
static Number
parse(JsonObject jo, String propertyName, int FLAGS, Number defaultValue, Function<String,Number> optionalParser)
Handle either a JsonNumber
or aJsonString
and Guess Number-TypeModifier and Type Method static Number
get(JsonArray ja, int i, int FLAGS, Number defaultValue, Function<String,Number> optionalParser)
static Number
get(JsonObject jo, String propertyName, int FLAGS, Number defaultValue, Function<String,Number> optionalParser)
-
-
-
Method Detail
-
get
public static java.lang.Number get(JsonArray ja, int index, boolean throwOnNull)
Retrieve aJsonArray
element, and transform it to ajava.lang.Number
. There are several possible Boxed-Type subclasses ofjava.lang.Number
which may be returned from this method. This method makes an effort to return the most appropriate subclass based on the value of theJsonNumber
. Note that if the number is too large to fit into the standard boxed-types, an instance ofjava.lang.BigInteger
orBigDecimal
is returned, instead.
If a call toJsonNumber.isIntegral()
evaluates toTRUE
, then one of the following types listed below is returned. The methodBigInteger.bitLength()
is used to determine which of these is used.java.lang.Integer
(The number uses 32 bits or less)java.lang.Long
(The number uses 64 bits or less)java.math.BigInteger
If the call toisIntegral()
returnsFALSE
, then one of these types is returned:java.lang.Float
java.lang.Double
java.math.BigDecimal
- 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
throwOnNull
- WhenTRUE
is passed to this parameter, if theJsonArray
element located at'index'
contains Json-Null, then this method will throw aJsonNullArrException
. When this parameter isFALSE
, if the specified array element contains Json-Null, then Java-Null is returned.- Returns:
- An instance of Java-Type
java.lang.Number
, if the'ja'
array element located at'index'
uses the appropriate Json-Type -JsonNumber
.
If the specified array element contains Json-Null, and'throwOnNull'
has been passedFALSE
, then Java-Null is returned. If the element is null, and'throwOnNull'
isTRUE
, then aJsonNullArrException
throws. - 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.JsonNullArrException
- If theJsonArray
element specified by'index'
contains a Json-Null, andTRUE
has been passed to'throwOnNull'
- See Also:
JsonValue.getValueType()
,RJInternal.convertToNumber(JsonNumber)
- Code:
- Exact Method Body:
// This will throw an IndexOutOfBoundsException if the index is out of bounds. JsonValue jv = ja.get(index); switch (jv.getValueType()) { case NULL: // This is simple-stuff (not rocket-science). "Type Mapping" Code has to worry // about what the meaning of "null" should be. if (throwOnNull) throw new JsonNullArrException(ja, index, NUMBER, Number.class); else return null; case NUMBER: return convertToNumber((JsonNumber) jv); // The JsonValue at the specified array-index does not contain a JsonString. default: throw new JsonTypeArrException(ja, index, NUMBER, jv, Number.class); }
-
get
public static java.lang.Number get(JsonArray ja, int index, int FLAGS, java.lang.Number defaultValue)
Retrieve aJsonArray
element, and transform it to ajava.lang.Number
. There are several possible Boxed-Type subclasses ofjava.lang.Number
which may be returned from this method. This method makes an effort to return the most appropriate subclass based on the value of theJsonNumber
. Note that if the number is too large to fit into the standard boxed-types, an instance ofjava.lang.BigInteger
orBigDecimal
is returned, instead.
If a call toJsonNumber.isIntegral()
evaluates toTRUE
, then one of the following types listed below is returned. The methodBigInteger.bitLength()
is used to determine which of these is used.java.lang.Integer
(The number uses 32 bits or less)java.lang.Long
(The number uses 64 bits or less)java.math.BigInteger
If the call toisIntegral()
returnsFALSE
, then one of these types is returned:java.lang.Float
java.lang.Double
java.math.BigDecimal
- 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.Number
, 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 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.convertToNumber(JsonNumber)
- Code:
- Exact Method Body:
return GET(ja, index, FLAGS, defaultValue, Number.class, RJInternal::convertToNumber, null);
-
parse
public static java.lang.Number parse (JsonArray ja, int index, int FLAGS, java.lang.Number defaultValue, java.util.function.Function<java.lang.String,java.lang.Number> optionalParser)
Retrieve aJsonArray
element containing aJsonString
, and transform it to ajava.lang.Number
, with either a user-provided parser, or the standard java parser
If'parser'
is passed null, the default parser is used, which is just thejava.math.BigDecimal
constructor which accepts aString
. What is done with the parsedBigDecimal
instance is explained below.
There are several possible Boxed-Type subclasses ofjava.lang.Number
which may be returned from this method. This method makes an effort to return the most appropriate subclass based on the value of theJsonNumber
. Note that if the number is too large to fit into the standard boxed-types, an instance ofjava.lang.BigInteger
orBigDecimal
is returned, instead.
If a call toBigDecimal.scale()
returns'0'
then one of the following types listed below is returned. The methodBigInteger.bitLength()
is used to determine which of these is used.java.lang.Integer
(The number uses 32 bits or less)java.lang.Long
(The number uses 64 bits or less)java.math.BigInteger
Otherwise, one of these types is returned:java.lang.Float
java.lang.Double
java.math.BigDecimal
- 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.Number
.
NOTE: This parameter may be null, and if it is, the standard Java invocation ofnew BigDecimal(String.trim())
is used.- Returns:
- If the
JsonArray
index contained aJsonString
, and that string was successfully parsed to ajava.lang.Number
, 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.Number
, 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)
,RJInternal.convertToNumber(String)
- Code:
- Exact Method Body:
return PARSE( ja, index, FLAGS, defaultValue, Number.class, optionalParser, RJInternal::convertToNumber, null /* Not Needed, convertToNumber won't throw */ );
-
get
public static java.lang.Number get(JsonObject jo, java.lang.String propertyName, boolean isOptional, boolean throwOnNull)
Extract aJsonObject
property, and transform it to ajava.lang.Number
. There are several possible Boxed-Type subclasses ofjava.lang.Number
which may be returned from this method. This method makes an effort to return the most appropriate subclass based on the value of theJsonNumber
. Note that if the number is too large to fit into the standard boxed-types, an instance ofjava.lang.BigInteger
orBigDecimal
is returned, instead.
If a call toJsonNumber.isIntegral()
evaluates toTRUE
, then one of the following types listed below is returned. The methodBigInteger.bitLength()
is used to determine which of these is used.java.lang.Integer
(The number uses 32 bits or less)java.lang.Long
(The number uses 64 bits or less)java.math.BigInteger
If the call toisIntegral()
returnsFALSE
, then one of these types is returned:java.lang.Float
java.lang.Double
java.math.BigDecimal
- 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.Number
.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.throwOnNull
- WhenTRUE
is passed to this parameter, if theJsonObject
property named'propertyName'
evaluates to Json-Null, then this method will throw aJsonNullObjException
. When this parameter isFALSE
, if the specified property contains Json-Null, then Java-Null is returned.- Returns:
- An instance of Java-Type
java.lang.Number'
, if the'propertyName'
is available inside'jo'
, and that property uses the appropriate Json-Type -JsonNumber
.
If the specified property contains Json-Null, and'throwOnNull'
has been passedFALSE
, then Java-Null is returned. If the property is null, and'throwOnNull'
isTRUE
, then aJsonNullObjException
throws.
If the specified property is simply missing, Java-Null is returned, unless'isOptional'
has been passedFALSE
- in which case aJsonPropMissingException
shall be thrown. - 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 aJsonNumber
(but rather, some other non-null Json-Type), then this exception throws.JsonNullObjException
- If theJsonObject
property specified by'propertyName'
contains a Json-Null, andTRUE
has been passed to'throwOnNull'
- See Also:
JsonValue.getValueType()
,RJInternal.convertToNumber(JsonNumber)
- Code:
- Exact Method Body:
if (! jo.containsKey(propertyName)) { if (isOptional) return null; else throw new JsonPropMissingException(jo, propertyName, NUMBER, Number.class); } JsonValue jv = jo.get(propertyName); switch (jv.getValueType()) { case NULL: // This is simple-stuff (not rocket-science). "Type Mapping" Code has to worry // about what the meaning of "null" should be. if (throwOnNull) throw new JsonNullObjException (jo, propertyName, NUMBER, Number.class); else return null; case NUMBER: return convertToNumber((JsonNumber) jv); // The JsonObject propertydoes not contain a JsonNumber. default: throw new JsonTypeObjException (jo, propertyName, NUMBER, jv, Number.class); }
-
get
public static java.lang.Number get(JsonObject jo, java.lang.String propertyName, int FLAGS, java.lang.Number defaultValue)
Retrieve aJsonObject
property, and transform it to ajava.lang.Number
. There are several possible Boxed-Type subclasses ofjava.lang.Number
which may be returned from this method. This method makes an effort to return the most appropriate subclass based on the value of theJsonNumber
. Note that if the number is too large to fit into the standard boxed-types, an instance ofjava.lang.BigInteger
orBigDecimal
is returned, instead.
If a call toJsonNumber.isIntegral()
evaluates toTRUE
, then one of the following types listed below is returned. The methodBigInteger.bitLength()
is used to determine which of these is used.java.lang.Integer
(The number uses 32 bits or less)java.lang.Long
(The number uses 64 bits or less)java.math.BigInteger
If the call toisIntegral()
returnsFALSE
, then one of these types is returned:java.lang.Float
java.lang.Double
java.math.BigDecimal
- 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.Number
.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.Number
, 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 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.convertToNumber(JsonNumber)
- Code:
- Exact Method Body:
return GET (jo, propertyName, FLAGS, defaultValue, Number.class, RJInternal::convertToNumber, null);
-
parse
public static java.lang.Number parse (JsonObject jo, java.lang.String propertyName, int FLAGS, java.lang.Number defaultValue, java.util.function.Function<java.lang.String,java.lang.Number> optionalParser)
Retrieve aJsonObject
property containing aJsonString
, and transform it to ajava.lang.Number
, with either a user-provided parser, or the standard java parser
If'parser'
is passed null, the default parser is used, which is just thejava.math.BigDecimal
constructor which accepts aString
. What is done with the parsedBigDecimal
instance is explained below.
There are several possible Boxed-Type subclasses ofjava.lang.Number
which may be returned from this method. This method makes an effort to return the most appropriate subclass based on the value of theJsonNumber
. Note that if the number is too large to fit into the standard boxed-types, an instance ofjava.lang.BigInteger
orBigDecimal
is returned, instead.
If a call toBigDecimal.scale()
returns'0'
then one of the following types listed below is returned. The methodBigInteger.bitLength()
is used to determine which of these is used.java.lang.Integer
(The number uses 32 bits or less)java.lang.Long
(The number uses 64 bits or less)java.math.BigInteger
Otherwise, one of these types is returned:java.lang.Float
java.lang.Double
java.math.BigDecimal
- 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.Number
.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.Number
.
NOTE: This parameter may be null, and if it is, the standard Java invocation ofnew BigDecimal(String.trim())
is used.- Returns:
- If the
JsonObject
property contained aJsonString
, and that string was successfully parsed to ajava.lang.Number
, 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
)
JsonStrParseObjException
- If theJsonObject
property specified by'propertyName'
throws an exception while attempting to parse thejava.lang.String
into ajava.lang.Number
, 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)
,RJInternal.convertToNumber(String)
- Code:
- Exact Method Body:
return PARSE( jo, propertyName, FLAGS, defaultValue, Number.class, optionalParser, RJInternal::convertToNumber, null /* Not Needed, convertToNumber won't throw */ );
-
get
public static java.lang.Number get (JsonArray ja, int i, int FLAGS, java.lang.Number defaultValue, java.util.function.Function<java.lang.String,java.lang.Number> optionalParser)
Used to convert aJsonArray
element which can handle either aString
, or aNumber
, and transform that element into a JavaNumber
.
This method simply dispatches to one of theReadJSON
methods, depending upon whether a string or a number is found at the array index location.- Parameters:
ja
- AnyJsonArray
i
- Any index into theJsonArray
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 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
- A function wich can map ajava.lang.String
into a JavaNumber
. This'optionalParser'
is only used if the specified element contains aJsonString
rather than aJsonNumber
. If aJsonNumber
is found, or some other unrelated type, then this parameter is wholly ignored.
NOTE: null may be passed to parameter'optionalParser'
. If null is received, and if it turns out that aString
toNumber
parser-function is required to generate the result for this method, thennew BigDecimal(String.trim())
will be used as the default-parser, instead.- Returns:
- If the
JsonArray
index contained a number, and that number was successfully transformed to aNumber
, then it is returned.
If the array-index was aString
, and thatString
was transformed into aNumber
using either'parser'
- or the default parser, then thatNumber
is returned.
In all exception or error cases, parameter'FLAGS'
may indicate that either the default-value be returned, or that null be returned. If the exception-handling flags were not sufficiently set, and an error-case has occured, one of the exceptions listed below is thrown. - 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'
contains aJsonString
, this method will attempt to parse thatString
into a JavaNumber
, using either the user-provided'parser'
(if provided), or otherwise, the default-parser (new BigDecimal(String.trim())
). This exception will throw if parsing theString
generates an exception throw 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 aJsonNumber
or 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'
neither contains aJsonNumber
, nor aJsonString
, 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:
get(JsonArray, int, int, Number)
,parse(JsonArray, int, int, Number, Function)
- Code:
- Exact Method Body:
JsonValue.ValueType t; return ((i >= ja.size()) || ((t=ja.get(i).getValueType()) == NUMBER) || (t != STRING)) ? get(ja, i, FLAGS, defaultValue) : parse(ja, i, FLAGS, defaultValue, optionalParser);
-
get
public static java.lang.Number get (JsonObject jo, java.lang.String propertyName, int FLAGS, java.lang.Number defaultValue, java.util.function.Function<java.lang.String,java.lang.Number> optionalParser)
Used to convert aJsonObject
property which can handle either aString
, or aNumber
, and transform that element into a JavaNumber
.
This method simply dispatches to one of theReadJSON
methods, depending upon whether the specified object property contains a string or a number.- Parameters:
jo
- AnyJsonObject
propertyName
- Any of the properties defined in theJsonObject
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 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
- A function wich can map ajava.lang.String
into a JavaNumber
. This'optionalParser'
is only used if the specified element contains aJsonString
rather than aJsonNumber
. If aJsonNumber
is found, or some other unrelated type, then this parameter is wholly ignored.
NOTE: null may be passed to parameter'optionalParser'
. If null is received, and if it turns out that aString
toNumber
parser-function is required to generate the result for this method, thennew BigDecimal(String.trim())
will be used as the default-parser, instead.- Returns:
- If the
JsonObject
property contained a number, and that number was successfully transformed to aNumber
, then it is returned.
If the object-property was aString
, and thatString
was transformed into aNumber
using either'parser'
- or the default parser, then thatNumber
is returned.
In all exception or error cases, parameter'FLAGS'
may indicate that either the default-value be returned, or that null be returned. If the exception-handling flags were not sufficiently set, and an error-case has occured, one of the exceptions listed below is thrown. - 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
)
JsonStrParseObjException
- If theJsonObject
property specified by'propertyName'
contains aJsonString
, this method will attempt to parse thatString
into a JavaNumber
, using either the user-provided'parser'
(if provided), or otherwise, the default-parser (new BigDecimal(String.trim())
). This exception will throw if parsing theString
generates an exception throw 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 aJsonNumber
or 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'
neither contains aJsonNumber
, nor aJsonString
, 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:
get(JsonObject, String, int, Number)
,parse(JsonObject, String, int, Number, Function)
- Code:
- Exact Method Body:
JsonValue.ValueType t; return ( (! jo.containsKey(propertyName)) || ((t = jo.get(propertyName).getValueType()) == NUMBER) || (t != STRING) ) ? get(jo, propertyName, FLAGS, defaultValue) : parse(jo, propertyName, FLAGS, defaultValue, optionalParser);
-
-