Package Torello.Java.Additional
Class ReadJSON
- java.lang.Object
-
- Torello.Java.Additional.ReadJSON
-
public class ReadJSON 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:- Provides for All-Manner of Exception-Handling & Error-Decision Configurations using the
(Simple)
JFlag
Class. - Provides Fine-Grained JSON Exception-Classes, all of which have Pre-written & Consistent Error-Messages,
- Handles the Transfer & Conversion of All Json-Type's into Java-Type's in just One Line of Code
- Is a Core Handler-Class for the (Experimental) Google-Chrome
Headless Browser
Package - Utilizes the Java-Standard
javax.json.*
Package-Library
Utilities that build on the J2EE Standard Release JSON Parsing Tool providing additional help with converting JSON Data into Java Data 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
The main goal of the methods in this class is simply to reduce the amount of code that needs to be written by a programmer when writing JSON De-Serializing Methods. For instance, when writing a Java Wrapper around a REST API (like the 'Facebook', 'YouTube', 'IMDB' or 'Twitter' API's), being able to write a constructor for Java Object that essentially needs just one line of code to assign each of the fields in the Java Object makes writing REST APIPOJO's
a lot faster.
If the JSON that your code is parsing is guaranteed to be free from nulls, missing properties, and incorrect types - then the vast majority of the methods in this class will not provide much additional benefit to your program. If your JSON data is definitively well-known, and well-understood, using the more basic features that are already availabe in the J2EE Json Parser should be enough! The features available in this class are here mainly to simplify handling all possibilities for error and unexpected-input that can happen when transmiting data with JSON.
NOTE: There are JSON Packages available (for instance the 'Jackson' Suite of Tools) that have hundreds of classes for creating Java-Objects out of JSON Objects. However, their level of complexity (and ubiquitous use of annotations) can be so over-powering, that they sometimes seem unusable.
Every one of the Java-Objects in the Headless-Browser / Chrome Remote Debugging A.P.I. have objects whose constructor's all use this'ReadJSON'
class to extract the Object Fields from the JSON Properties in just a few lines (Actually, the number of lines of code in each of the constructors in that package is exactly equal to the number of fields in each of the classes there.) For an example of the use of this class, please refer to any of the constructors or the commands inside the browser package.
Example:
public class Automobile { public final String make; public final String model public final short year; public final Integer miles; // Boxed-Type "Integer" (capital 'I') ALLOWS NULL! public final Boolean compact; // Boxed-Type "Boolean" (capital 'B') ALLOWS NULL! public Automobile(JsonObject jo) { // The "Make" of the care is guaranteed. // THROWS: If Property is not available, null, or not a JsonString this.make = ReadJSON.getString(jo, "Manufacturer"); // The "Model" of the car, isn't guaranteed. // THROWS: Only if the property isn't a JsonString // RETURNS: 'null' if it is missing, or set to Json-Null this.model = ReadJSON.getString(jo, "Model", true, false); // The "Year" Property is also guaranteed. // THROWS: If missing, null, wrong-type (on all error cases) // RETURNS: The "Year" jo property-value this.year = ReadJSON.getShort(jo, "Year"); // This is an optional "Miles" Property. // THROWS: No exception is thrown if this property is missing or null // If the property is the wrong-type (not a number), throws JsonTypeObjException // RETURNS: miles property-value, or the default-Value of '0' (if missing or null) this.miles = ReadJSON.getINTEGER(jo, "miles", RD_N | RD_M, 0); // Optional "compact" boolean property. // THROWS: NEVER - even if missing, null, or not a Json-Boolean. // RETURNS: 'isCompact' property-value *or* auto-assigns null on error this.compact = ReadJSON.getBOOLEAN(jo, "isCompact", RETURN_NULL_ON_ANY_ALL, false); } public static void main(String[] argv) throws Exception { // Read and load JSON into a String, then to a 'JsonObject' String json = FileRW.loadFileToString("some-file.json"); StringReader sr = new StringReader(json); JsonObject jo = Json.createReader(sr).readObject(); // Build an "Automobile" Object instance Automobile auto = new Automobile(jo); } }
- See Also:
Json
,JsonObject
,JsonArray
Hi-Lited Source-Code:- View Here: Torello/Java/Additional/ReadJSON.java
- Open New Browser-Tab: Torello/Java/Additional/ReadJSON.java
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
- 64 Method(s), 64 declared static
- 0 Field(s)
- Provides for All-Manner of Exception-Handling & Error-Decision Configurations using the
(Simple)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class static class
ReadJSON.XL
-
Method Summary
Integer: Retrieve a JsonNumber
from aJsonArray
, and transform it to a Java IntegerReturns Modifier and Type Method Primitive static int
getInt(JsonArray ja, int index)
Boxed-Primitive static Integer
getINTEGER(JsonArray ja, int index)
Boxed-Primitive static Integer
getINTEGER(JsonArray ja, int index, int FLAGS, int defaultValue)
Integer: Retrieve a JsonNumber
from aJsonObject
, and transform it to a Java IntegerReturns Modifier and Type Method Primitive static int
getInt(JsonObject jo, String propertyName)
Boxed-Primitive static Integer
getINTEGER(JsonObject jo, String propertyName, boolean isOptional)
Boxed-Primitive static Integer
getINTEGER(JsonObject jo, String propertyName, int FLAGS, int defaultValue)
Long: Retrieve a JsonNumber
from aJsonArray
, and transform it to a Java LongReturns Modifier and Type Method Primitive static long
getLong(JsonArray ja, int index)
Boxed-Primitive static Long
getLONG(JsonArray ja, int index)
Boxed-Primitive static Long
getLONG(JsonArray ja, int index, int FLAGS, long defaultValue)
Long: Retrieve a JsonNumber
from aJsonObject
, and transform it to a Java LongReturns Modifier and Type Method Primitive static long
getLong(JsonObject jo, String propertyName)
Boxed-Primitive static Long
getLONG(JsonObject jo, String propertyName, boolean isOptional)
Boxed-Primitive static Long
getLONG(JsonObject jo, String propertyName, int FLAGS, long defaultValue)
Short: Retrieve a JsonNumber
from aJsonArray
, and transform it to a Java ShortReturns Modifier and Type Method Primitive static short
getShort(JsonArray ja, int index)
Boxed-Primitive static Short
getSHORT(JsonArray ja, int index)
Boxed-Primitive static Short
getSHORT(JsonArray ja, int index, int FLAGS, short defaultValue)
Short: Retrieve a JsonNumber
from aJsonObject
, and transform it to a Java ShortReturns Modifier and Type Method Primitive static short
getShort(JsonObject jo, String propertyName)
Boxed-Primitive static Short
getSHORT(JsonObject jo, String propertyName, boolean isOptional)
Boxed-Primitive static Short
getSHORT(JsonObject jo, String propertyName, int FLAGS, short defaultValue)
Byte: Retrieve a JsonNumber
from aJsonArray
, and transform it to a Java ByteReturns Modifier and Type Method Primitive static byte
getByte(JsonArray ja, int index)
Boxed-Primitive static Byte
getBYTE(JsonArray ja, int index)
Boxed-Primitive static Byte
getBYTE(JsonArray ja, int index, int FLAGS, byte defaultValue)
Byte: Retrieve a JsonNumber
from aJsonObject
, and transform it to a Java ByteReturns Modifier and Type Method Primitive static byte
getByte(JsonObject jo, String propertyName)
Boxed-Primitive static Byte
getBYTE(JsonObject jo, String propertyName, boolean isOptional)
Boxed-Primitive static Byte
getBYTE(JsonObject jo, String propertyName, int FLAGS, byte defaultValue)
Double: Retrieve a JsonNumber
from aJsonArray
, and transform it to a Java DoubleReturns Modifier and Type Method Primitive static double
getDouble(JsonArray ja, int index)
Boxed-Primitive static Double
getDOUBLE(JsonArray ja, int index)
Boxed-Primitive static Double
getDOUBLE(JsonArray ja, int index, int FLAGS, double defaultValue)
Double: Retrieve a JsonNumber
from aJsonObject
, and transform it to a Java DoubleReturns Modifier and Type Method Primitive static double
getDouble(JsonObject jo, String propertyName)
Boxed-Primitive static Double
getDOUBLE(JsonObject jo, String propertyName, boolean isOptional)
Boxed-Primitive static Double
getDOUBLE(JsonObject jo, String propertyName, int FLAGS, double defaultValue)
Float: Retrieve a JsonNumber
from aJsonArray
, and transform it to a Java FloatReturns Modifier and Type Method Primitive static float
getFloat(JsonArray ja, int index)
Boxed-Primitive static Float
getFLOAT(JsonArray ja, int index)
Boxed-Primitive static Float
getFLOAT(JsonArray ja, int index, int FLAGS, float defaultValue)
Float: Retrieve a JsonNumber
from aJsonObject
, and transform it to a Java FloatReturns Modifier and Type Method Primitive static float
getFloat(JsonObject jo, String propertyName)
Boxed-Primitive static Float
getFLOAT(JsonObject jo, String propertyName, boolean isOptional)
Boxed-Primitive static Float
getFLOAT(JsonObject jo, String propertyName, int FLAGS, float defaultValue)
GUESS: Retrieve a JsonNumber
from aJsonArray
, and transform to best-fit NumberReturns Modifier and Type Method Boxed-Primitive static Number
getNUMBER(JsonArray ja, int index, boolean throwOnNull)
Boxed-Primitive static Number
getNUMBER(JsonArray ja, int index, int FLAGS, Number defaultValue)
GUESS: Retrieve a JsonNumber
from aJsonObject
, and transform to best-fit NumberReturns Modifier and Type Method Boxed-Primitive static Number
getNUMBER(JsonObject jo, String propertyName, boolean isOptional, boolean throwOnNull)
Boxed-Primitive static Number
getNUMBER(JsonObject jo, String propertyName, int FLAGS, Number defaultValue)
PARSE: Retrieve a JsonString
from aJsonArray
, and parse it to a Java-NumberReturns Modifier and Type Method Boxed-Primitive static Byte
parseBYTE(JsonArray ja, int index, int FLAGS, byte defaultValue, Function<String,Byte> optionalParser)
Boxed-Primitive static Double
parseDOUBLE(JsonArray ja, int index, int FLAGS, double defaultValue, Function<String,Double> optionalParser)
Boxed-Primitive static Float
parseFLOAT(JsonArray ja, int index, int FLAGS, float defaultValue, Function<String,Float> optionalParser)
Boxed-Primitive static Integer
parseINTEGER(JsonArray ja, int index, int FLAGS, int defaultValue, Function<String,Integer> optionalParser)
Boxed-Primitive static Long
parseLONG(JsonArray ja, int index, int FLAGS, long defaultValue, Function<String,Long> optionalParser)
Boxed-Primitive static Number
parseNUMBER(JsonArray ja, int index, int FLAGS, Number defaultValue, Function<String,Number> optionalParser)
Boxed-Primitive static Short
parseSHORT(JsonArray ja, int index, int FLAGS, short defaultValue, Function<String,Short> optionalParser)
PARSE: Retrieve a JsonString
from aJsonObject
, and parse it to a Java-NumberReturns Modifier and Type Method Boxed-Primitive static Byte
parseBYTE(JsonObject jo, String propertyName, int FLAGS, byte defaultValue, Function<String,Byte> optionalParser)
Boxed-Primitive static Double
parseDOUBLE(JsonObject jo, String propertyName, int FLAGS, double defaultValue, Function<String,Double> optionalParser)
Boxed-Primitive static Float
parseFLOAT(JsonObject jo, String propertyName, int FLAGS, float defaultValue, Function<String,Float> parser)
Boxed-Primitive static Integer
parseINTEGER(JsonObject jo, String propertyName, int FLAGS, int defaultValue, Function<String,Integer> optionalParser)
Boxed-Primitive static Long
parseLONG(JsonObject jo, String propertyName, int FLAGS, long defaultValue, Function<String,Long> optionalParser)
Boxed-Primitive static Number
parseNUMBER(JsonObject jo, String propertyName, int FLAGS, Number defaultValue, Function<String,Number> optionalParser)
Boxed-Primitive static Short
parseSHORT(JsonObject jo, String propertyName, int FLAGS, short defaultValue, Function<String,Short> optionalParser)
Boolean: Retrieve JsonValue.TRUE
orFALSE
, from aJsonArray
, and transform it to a Java BooleanReturns Modifier and Type Method Primitive static boolean
getBoolean(JsonArray ja, int index)
Boxed-Primitive static Boolean
getBOOLEAN(JsonArray ja, int index)
Boxed-Primitive static Boolean
getBOOLEAN(JsonArray ja, int index, int FLAGS, boolean defaultValue)
Boolean: Retrieve JsonValue.TRUE
orFALSE
, from aJsonObject
, and transform it to a Java BooleanReturns Modifier and Type Method Primitive static boolean
getBoolean(JsonObject jo, String propertyName)
Boxed-Primitive static Boolean
getBOOLEAN(JsonObject jo, String propertyName, boolean isOptional)
Boxed-Primitive static Boolean
getBOOLEAN(JsonObject jo, String propertyName, int FLAGS, boolean defaultValue)
PARSE: Retrieve a JsonString
and parse it to a Java-BooleanReturns Modifier and Type Method Boxed-Primitive static Boolean
parseBOOLEAN(JsonArray ja, int index, int FLAGS, boolean defaultValue, Function<String,Boolean> optionalParser)
Boxed-Primitive static Boolean
parseBOOLEAN(JsonObject jo, String propertyName, int FLAGS, boolean defaultValue, Function<String,Boolean> parser)
String: Retrieve a JsonString
, and transform it to a Java StringReturns Modifier and Type Method String static String
getString(JsonArray ja, int index, boolean throwOnNull)
String static String
getString(JsonObject jo, String propertyName, boolean isOptional, boolean throwOnNull)
-
-
-
Method Detail
-
getInt
public static int getInt(JsonArray ja, int index)
Retrieve aJsonArray
element, and transform it to an'int'
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
int
, if and only if the array element located at'index'
uses the appropriate Json-Type -JsonNumber
, and that element may be properly mapped to a Javaint
. - 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 validint
. The thrown-exception is then wrapped into this exception for the purpose of providing a much improved error-message, and convenience fields.JsonNullPrimitiveArrException
- If'index'
specifies an element inside'ja'
that has been assigned Json-Null, then this exception will throw. (Unlike Json, in Java a Primitiveint
will not accept 'null' as a value).- See Also:
RJInternal.GET(JsonArray, int, Class, Function)
,JsonNumber.intValueExact()
- Code:
- Exact Method Body:
return GET(ja, index, int.class, JsonNumber::intValueExact);
-
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.
NOTE: 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
NOTE: 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 = ReadJSON.getINTEGER(jsonArray, 1, RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns '-1' if the array index is null, // or if the array index contains some non-integer number. // Note that Type Inferencing auto-converts the returned java.lang.Integer => 'int' primitive int val = ReadJSON.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
- The return-value / exception-throw flag 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.
NOTE: 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 = ReadJSON.parseINTEGER(jsonArray, 1, RETURN_NULL_ON_NULL, -1, null); // The following invocation gracefully returns '-1' if the array index contains an // 'unparseable' String (or is null) // Note that Java's Type Inferencing auto-converts the java.lang.Integer => 'int' primitive int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_PARSEX; int val = ReadJSON.parseINTEGER(jsonArray, 1, FLAGS, -1, null);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing theJsonString
element to retrieve.FLAGS
- The return-value / exception-throw flag 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 );
-
getInt
public static int getInt(JsonObject jo, java.lang.String propertyName)
Extract aJsonObject
property, and transform it to an'int'
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-Typeint
.- Returns:
- An instance of Java-Type
int
, if and only if the'propertyName'
is available inside'jo'
, and that property uses the appropriate Json-Type -JsonNumber
. Furthermore, thatJsonNumber
must be assignable to Java Primitive Typeint
, or this method will throw an exception. - Throws:
JsonPropMissingException
- This exception throws if theJsonObject
(parameter'jo'
) does not have any properties with the name'propertyName'
.JsonArithmeticObjException
- Java's implementation ofintValueExact()
throws anArithmeticException
when that method cannot correctly transform / convert the element to a validint
. 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.JsonNullPrimitiveObjException
- If'propertyName'
specifies a property inside'jo'
that has been assigned Json-Null, then this exception will throw. (Unlike Json, in Java a Primitiveint
will not accept 'null' as a value).- See Also:
RJInternal.GET(JsonObject, String, Class, Function)
,JsonNumber.intValueExact()
- Code:
- Exact Method Body:
return GET(jo, propertyName, int.class, JsonNumber::intValueExact);
-
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.
NOTE: 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
NOTE: 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 = ReadJSON.getINTEGER(jsonObj, "myProp", RETURN_NULL_ON_NULL -1); // The following invocation gracefully returns '-1' if the property contains a null, // or if the property holds some non-integer number. // Note that Type Inferencing auto-converts the returned java.lang.Integer => 'int' primitive int val = ReadJSON.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
- The return-value / exception-throw flag 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.
NOTE: 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 = ReadJSON.parseINTEGER(jsonObj, "myProp", RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns '-1' if the property contains an // 'unparseable' String, or if it is null. // Note that Java's Type Inferencing auto-converts the java.lang.Integer => 'int' primitive int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_PARSEX int val = ReadJSON.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
- The return-value / exception-throw flag 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 long getLong(JsonArray ja, int index)
Retrieve aJsonArray
element, and transform it to a'long'
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
long
, if and only if the array element located at'index'
uses the appropriate Json-Type -JsonNumber
, and that element may be properly mapped to a Javalong
. - 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 validlong
. The thrown-exception is then wrapped into this exception for the purpose of providing a much improved error-message, and convenience fields.JsonNullPrimitiveArrException
- If'index'
specifies an element inside'ja'
that has been assigned Json-Null, then this exception will throw. (Unlike Json, in Java a Primitivelong
will not accept 'null' as a value).- See Also:
RJInternal.GET(JsonArray, int, Class, Function)
,JsonNumber.longValueExact()
- Code:
- Exact Method Body:
return GET(ja, index, long.class, JsonNumber::longValueExact);
-
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.
NOTE: 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
NOTE: 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 = ReadJSON.getLONG(jsonArray, 1, RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns '-1' if the array index is null, // or if the array index contains some non-integer number. // Note that Type Inferencing auto-converts the returned java.lang.Long => 'long' primitive long val = ReadJSON.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
- The return-value / exception-throw flag 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.
NOTE: 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 = ReadJSON.parseLONG(jsonArray, 1, RETURN_NULL_ON_NULL, -1, null); // The following invocation gracefully returns '-1' if the array index contains an // 'unparseable' String (or is null) // Note that Java's Type Inferencing auto-converts the java.lang.Long => 'long' primitive int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_PARSEX; long val = ReadJSON.parseLONG(jsonArray, 1, FLAGS, -1, null);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing theJsonString
element to retrieve.FLAGS
- The return-value / exception-throw flag 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 long getLong(JsonObject jo, java.lang.String propertyName)
Extract aJsonObject
property, and transform it to a'long'
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-Typelong
.- Returns:
- An instance of Java-Type
long
, if and only if the'propertyName'
is available inside'jo'
, and that property uses the appropriate Json-Type -JsonNumber
. Furthermore, thatJsonNumber
must be assignable to Java Primitive Typelong
, or this method will throw an exception. - Throws:
JsonPropMissingException
- This exception throws if theJsonObject
(parameter'jo'
) does not have any properties with the name'propertyName'
.JsonArithmeticObjException
- Java's implementation oflongValueExact()
throws anArithmeticException
when that method cannot correctly transform / convert the element to a validlong
. 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.JsonNullPrimitiveObjException
- If'propertyName'
specifies a property inside'jo'
that has been assigned Json-Null, then this exception will throw. (Unlike Json, in Java a Primitivelong
will not accept 'null' as a value).- See Also:
RJInternal.GET(JsonObject, String, Class, Function)
,JsonNumber.longValueExact()
- Code:
- Exact Method Body:
return GET(jo, propertyName, long.class, JsonNumber::longValueExact);
-
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.
NOTE: 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
NOTE: 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 = ReadJSON.getLONG(jsonObj, "myProp", RETURN_NULL_ON_NULL -1); // The following invocation gracefully returns '-1' if the property contains a null, // or if the property holds some non-integer number. // Note that Type Inferencing auto-converts the returned java.lang.Long => 'int' primitive int val = ReadJSON.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
- The return-value / exception-throw flag 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.
NOTE: 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 = ReadJSON.parseLONG(jsonObj, "myProp", RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns '-1' if the property contains an // 'unparseable' String, or if it is null. // Note that Java's Type Inferencing auto-converts the java.lang.Long => 'long' primitive int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_PARSEX long val = ReadJSON.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
- The return-value / exception-throw flag 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 short getShort(JsonArray ja, int index)
Retrieve aJsonArray
element, and transform it to a'short'
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
short
, if and only if the array element located at'index'
uses the appropriate Json-Type -JsonNumber
, and that element may be properly mapped to a Javashort
. - 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 validshort
. The thrown-exception is then wrapped into this exception for the purpose of providing a much improved error-message, and convenience fields.JsonNullPrimitiveArrException
- If'index'
specifies an element inside'ja'
that has been assigned Json-Null, then this exception will throw. (Unlike Json, in Java a Primitiveshort
will not accept 'null' as a value).- See Also:
RJInternal.GET(JsonArray, int, Class, Function)
,JsonNumber.bigDecimalValue()
- Code:
- Exact Method Body:
return GET(ja, index, short.class, jn -> jn.bigDecimalValue().shortValueExact());
-
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.
NOTE: 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
NOTE: 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 = ReadJSON.getSHORT(jsonArray, 1, RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns '-1' if the array index is null, // or if the array index contains some non-integer number. // Note that Type Inferencing auto-converts the returned java.lang.Short => 'short' primitive short val = ReadJSON.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
- The return-value / exception-throw flag 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.
NOTE: 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 = ReadJSON.parseSHORT(jsonArray, 1, RETURN_NULL_ON_NULL, -1, null); // The following invocation gracefully returns '-1' if the array index contains an // 'unparseable' String (or is null) // Note that Java's Type Inferencing auto-converts the java.lang.Short => 'short' primitive int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_PARSEX; short val = ReadJSON.parseSHORT(jsonArray, 1, FLAGS, -1, null);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing theJsonString
element to retrieve.FLAGS
- The return-value / exception-throw flag 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 short getShort(JsonObject jo, java.lang.String propertyName)
Extract aJsonObject
property, and transform it to a'short'
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-Typeshort
.- Returns:
- An instance of Java-Type
short
, if and only if the'propertyName'
is available inside'jo'
, and that property uses the appropriate Json-Type -JsonNumber
. Furthermore, thatJsonNumber
must be assignable to Java Primitive Typeshort
, or this method will throw an exception. - Throws:
JsonPropMissingException
- This exception throws if theJsonObject
(parameter'jo'
) does not have any properties with the name'propertyName'
.JsonArithmeticObjException
- Java's implementation ofshortValueExact()
throws anArithmeticException
when that method cannot correctly transform / convert the element to a validshort
. 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.JsonNullPrimitiveObjException
- If'propertyName'
specifies a property inside'jo'
that has been assigned Json-Null, then this exception will throw. (Unlike Json, in Java a Primitiveshort
will not accept 'null' as a value).- See Also:
RJInternal.GET(JsonObject, String, Class, Function)
,JsonNumber.bigDecimalValue()
- Code:
- Exact Method Body:
return GET(jo, propertyName, short.class, jn -> jn.bigDecimalValue().shortValueExact());
-
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.
NOTE: 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
NOTE: 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 = ReadJSON.getSHORT(jsonObj, "myProp", RETURN_NULL_ON_NULL -1); // The following invocation gracefully returns '-1' if the property contains a null, // or if the property holds some non-integer number. // Note that Type Inferencing auto-converts the returned java.lang.Short => 'int' primitive int val = ReadJSON.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
- The return-value / exception-throw flag 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.
NOTE: 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 = ReadJSON.parseSHORT(jsonObj, "myProp", RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns '-1' if the property contains an // 'unparseable' String, or if it is null. // Note that Java's Type Inferencing auto-converts the java.lang.Short => 'short' primitive int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_PARSEX short val = ReadJSON.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
- The return-value / exception-throw flag 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 byte getByte(JsonArray ja, int index)
Retrieve aJsonArray
element, and transform it to a'byte'
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
byte
, if and only if the array element located at'index'
uses the appropriate Json-Type -JsonNumber
, and that element may be properly mapped to a Javabyte
. - 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 validbyte
. The thrown-exception is then wrapped into this exception for the purpose of providing a much improved error-message, and convenience fields.JsonNullPrimitiveArrException
- If'index'
specifies an element inside'ja'
that has been assigned Json-Null, then this exception will throw. (Unlike Json, in Java a Primitivebyte
will not accept 'null' as a value).- See Also:
RJInternal.GET(JsonArray, int, Class, Function)
,JsonNumber.bigDecimalValue()
- Code:
- Exact Method Body:
return GET(ja, index, byte.class, jn -> jn.bigDecimalValue().byteValueExact());
-
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.
NOTE: 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
NOTE: 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 = ReadJSON.getBYTE(jsonArray, 1, RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns '-1' if the array index is null, // or if the array index contains some non-integer number. // Note that Type Inferencing auto-converts the returned java.lang.Byte => 'byte' primitive byte val = ReadJSON.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
- The return-value / exception-throw flag 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.
NOTE: 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 = ReadJSON.parseBYTE(jsonArray, 1, RETURN_NULL_ON_NULL, -1, null); // The following invocation gracefully returns '-1' if the array index contains an // 'unparseable' String (or is null) // Note that Java's Type Inferencing auto-converts the java.lang.Byte => 'byte' primitive int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_PARSEX; byte val = ReadJSON.parseBYTE(jsonArray, 1, FLAGS, -1, null);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing theJsonString
element to retrieve.FLAGS
- The return-value / exception-throw flag 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 byte getByte(JsonObject jo, java.lang.String propertyName)
Extract aJsonObject
property, and transform it to a'byte'
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-Typebyte
.- Returns:
- An instance of Java-Type
byte
, if and only if the'propertyName'
is available inside'jo'
, and that property uses the appropriate Json-Type -JsonNumber
. Furthermore, thatJsonNumber
must be assignable to Java Primitive Typebyte
, or this method will throw an exception. - Throws:
JsonPropMissingException
- This exception throws if theJsonObject
(parameter'jo'
) does not have any properties with the name'propertyName'
.JsonArithmeticObjException
- Java's implementation ofbyteValueExact()
throws anArithmeticException
when that method cannot correctly transform / convert the element to a validbyte
. 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.JsonNullPrimitiveObjException
- If'propertyName'
specifies a property inside'jo'
that has been assigned Json-Null, then this exception will throw. (Unlike Json, in Java a Primitivebyte
will not accept 'null' as a value).- See Also:
RJInternal.GET(JsonObject, String, Class, Function)
,JsonNumber.bigDecimalValue()
- Code:
- Exact Method Body:
return GET(jo, propertyName, byte.class, jn -> jn.bigDecimalValue().byteValueExact());
-
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.
NOTE: 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
NOTE: 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 = ReadJSON.getBYTE(jsonObj, "myProp", RETURN_NULL_ON_NULL -1); // The following invocation gracefully returns '-1' if the property contains a null, // or if the property holds some non-integer number. // Note that Type Inferencing auto-converts the returned java.lang.Byte => 'int' primitive int val = ReadJSON.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
- The return-value / exception-throw flag 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.
NOTE: 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 = ReadJSON.parseBYTE(jsonObj, "myProp", RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns '-1' if the property contains an // 'unparseable' String, or if it is null. // Note that Java's Type Inferencing auto-converts the java.lang.Byte => 'byte' primitive int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_PARSEX byte val = ReadJSON.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
- The return-value / exception-throw flag 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 double getDouble(JsonArray ja, int index)
Retrieve aJsonArray
element, and transform it to a'double'
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
double
, if and only if the array element located at'index'
uses the appropriate Json-Type -JsonNumber
, and that element may be properly mapped to a Javadouble
. - 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 Typedouble
.
This method checks for the infinity constants, and then proceeds to throw this exception when they are detected.JsonNullPrimitiveArrException
- If'index'
specifies an element inside'ja'
that has been assigned Json-Null, then this exception will throw. (Unlike Json, in Java a Primitivedouble
will not accept 'null' as a value).- See Also:
RJInternal.GET(JsonArray, int, Class, Function)
,RJInternal.DOUBLE_WITH_CHECK(JsonNumber)
- Code:
- Exact Method Body:
return GET(ja, index, double.class, RJInternal::DOUBLE_WITH_CHECK);
-
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.
NOTE: 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
NOTE: 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 = ReadJSON.getDOUBLE(jsonArray, 1, RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns '-1' if the array index is null, // or if the array index contains some non-integer number. // Note that Type Inferencing auto-converts the returned java.lang.Double => 'double' primitive double val = ReadJSON.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
- The return-value / exception-throw flag 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.
NOTE: 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 = ReadJSON.parseDOUBLE(jsonArray, 1, RETURN_NULL_ON_NULL, -1, null); // The following invocation gracefully returns '-1' if the array index contains an // 'unparseable' String (or is null) // Note that Java's Type Inferencing auto-converts the java.lang.Double => 'double' primitive int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_PARSEX; double val = ReadJSON.parseDOUBLE(jsonArray, 1, FLAGS, -1, null);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing theJsonString
element to retrieve.FLAGS
- The return-value / exception-throw flag 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 double getDouble(JsonObject jo, java.lang.String propertyName)
Extract aJsonObject
property, and transform it to a'double'
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-Typedouble
.- Returns:
- An instance of Java-Type
double
, if and only if the'propertyName'
is available inside'jo'
, and that property uses the appropriate Json-Type -JsonNumber
. Furthermore, thatJsonNumber
must be assignable to Java Primitive Typedouble
, or this method will throw an exception. - Throws:
JsonPropMissingException
- This exception throws if theJsonObject
(parameter'jo'
) does not have any properties with the name'propertyName'
.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 Typedouble
.
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.JsonNullPrimitiveObjException
- If'propertyName'
specifies a property inside'jo'
that has been assigned Json-Null, then this exception will throw. (Unlike Json, in Java a Primitivedouble
will not accept 'null' as a value).- See Also:
RJInternal.GET(JsonObject, String, Class, Function)
,RJInternal.DOUBLE_WITH_CHECK(JsonNumber)
- Code:
- Exact Method Body:
return GET(jo, propertyName, double.class, RJInternal::DOUBLE_WITH_CHECK);
-
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.
NOTE: 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
NOTE: 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 = ReadJSON.getDOUBLE(jsonObj, "myProp", RETURN_NULL_ON_NULL -1); // The following invocation gracefully returns '-1' if the property contains a null, // or if the property holds some non-integer number. // Note that Type Inferencing auto-converts the returned java.lang.Double => 'double' primitive double val = ReadJSON.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
- The return-value / exception-throw flag 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.
NOTE: 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 = ReadJSON.parseDOUBLE(jsonObj, "myProp", RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns '-1' if the property contains an // 'unparseable' String, or if it is null. // Note that Java's Type Inferencing auto-converts the java.lang.Double => 'double' primitive int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_PARSEX double val = ReadJSON.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
- The return-value / exception-throw flag 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 float getFloat(JsonArray ja, int index)
Retrieve aJsonArray
element, and transform it to a'float'
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
float
, if and only if the array element located at'index'
uses the appropriate Json-Type -JsonNumber
, and that element may be properly mapped to a Javafloat
. - 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 Typefloat
.
This method checks for the infinity constants, and then proceeds to throw this exception when they are detected.JsonNullPrimitiveArrException
- If'index'
specifies an element inside'ja'
that has been assigned Json-Null, then this exception will throw. (Unlike Json, in Java a Primitivefloat
will not accept 'null' as a value).- See Also:
RJInternal.GET(JsonArray, int, Class, Function)
,RJInternal.FLOAT_WITH_CHECK(JsonNumber)
- Code:
- Exact Method Body:
return GET(ja, index, float.class, RJInternal::FLOAT_WITH_CHECK);
-
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.
NOTE: 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
NOTE: 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 = ReadJSON.getFLOAT(jsonArray, 1, RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns '-1' if the array index is null, // or if the array index contains some non-integer number. // Note that Type Inferencing auto-converts the returned java.lang.Float => 'float' primitive float val = ReadJSON.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
- The return-value / exception-throw flag 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.
NOTE: 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 = ReadJSON.parseFLOAT(jsonArray, 1, RETURN_NULL_ON_NULL, -1, null); // The following invocation gracefully returns '-1' if the array index contains an // 'unparseable' String (or is null) // Note that Java's Type Inferencing auto-converts the java.lang.Float => 'float' primitive int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_PARSEX; float val = ReadJSON.parseFLOAT(jsonArray, 1, FLAGS, -1, null);
- Parameters:
ja
- Any instance ofJsonArray
index
- The array index containing theJsonString
element to retrieve.FLAGS
- The return-value / exception-throw flag 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 float getFloat(JsonObject jo, java.lang.String propertyName)
Extract aJsonObject
property, and transform it to a'float'
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-Typefloat
.- Returns:
- An instance of Java-Type
float
, if and only if the'propertyName'
is available inside'jo'
, and that property uses the appropriate Json-Type -JsonNumber
. Furthermore, thatJsonNumber
must be assignable to Java Primitive Typefloat
, or this method will throw an exception. - Throws:
JsonPropMissingException
- This exception throws if theJsonObject
(parameter'jo'
) does not have any properties with the name'propertyName'
.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 Typefloat
.
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.JsonNullPrimitiveObjException
- If'propertyName'
specifies a property inside'jo'
that has been assigned Json-Null, then this exception will throw. (Unlike Json, in Java a Primitivefloat
will not accept 'null' as a value).- See Also:
RJInternal.GET(JsonObject, String, Class, Function)
,RJInternal.FLOAT_WITH_CHECK(JsonNumber)
- Code:
- Exact Method Body:
return GET(jo, propertyName, float.class, RJInternal::FLOAT_WITH_CHECK);
-
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.
NOTE: 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
NOTE: 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 = ReadJSON.getFLOAT(jsonObj, "myProp", RETURN_NULL_ON_NULL -1); // The following invocation gracefully returns '-1' if the property contains a null, // or if the property holds some non-integer number. // Note that Type Inferencing auto-converts the returned java.lang.Float => 'int' primitive int val = ReadJSON.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
- The return-value / exception-throw flag 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.
NOTE: 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 = ReadJSON.parseFLOAT(jsonObj, "myProp", RETURN_NULL_ON_NULL, -1); // The following invocation gracefully returns '-1' if the property contains an // 'unparseable' String, or if it is null. // Note that Java's Type Inferencing auto-converts the java.lang.Float => 'float' primitive int FLAGS = RETURN_DEFVAL_ON_NULL | RETURN_DEFVAL_ON_PARSEX float val = ReadJSON.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
- The return-value / exception-throw flag 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 );
-
getNUMBER
public static java.lang.Number getNUMBER(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); }
-
getNUMBER
public static java.lang.Number getNUMBER(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
- The return-value / exception-throw flag 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);
-
parseNUMBER
public static java.lang.Number parseNUMBER (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
- The return-value / exception-throw flag 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 */ );
-
getNUMBER
public static java.lang.Number getNUMBER(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); }
-
getNUMBER
public static java.lang.Number getNUMBER(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
- The return-value / exception-throw flag 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);
-
parseNUMBER
public static java.lang.Number parseNUMBER (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
- The return-value / exception-throw flag 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 */ );
-
getBoolean
public static boolean getBoolean(JsonArray ja, int index)
Retrieve aJsonArray
element, and transform it to a'boolean'
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
boolean
, if and only if the array element located at'index'
uses the appropriate Json-Type -JSON-BOOLEAN
, and that element may be properly mapped to a Javaboolean
. - 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.JsonNullPrimitiveArrException
- If'index'
specifies an element inside'ja'
that has been assigned Json-Null, then this exception will throw. (Unlike Json, in Java a Primitiveboolean
will not accept 'null' as a value).- See Also:
getBOOLEAN(JsonArray, int)
- Code:
- Exact Method Body:
// This method (defined below) allows for null-returns, but "getBoolean" does not. // "getBOOLEAN" will only return null if the actual array location 'index' actually has // the word 'null' listed. Boolean ret = getBOOLEAN(ja, index); // In that case, throw the Null-Primitive Exception if (ret == null) throw new JsonNullPrimitiveArrException (ja, index, TRUE, boolean.class); return ret;
-
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.
NOTE: 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.
NOTE: 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
- The return-value / exception-throw flag 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
- The return-value / exception-throw flag 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 boolean getBoolean(JsonObject jo, java.lang.String propertyName)
Extract aJsonObject
property, and transform it to a'boolean'
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-Typeboolean
.- Returns:
- An instance of Java-Type
boolean
, if and only if the'propertyName'
is available inside'jo'
, and that property uses the appropriate Json-Type -JSON-BOOLEAN
. Furthermore, thatJSON-BOOLEAN
must be assignable to Java Primitive Typeboolean
, or this method will throw an exception. - Throws:
JsonPropMissingException
- This exception throws if theJsonObject
(parameter'jo'
) does not have any properties with the name'propertyName'
.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.JsonNullPrimitiveObjException
- If'propertyName'
specifies a property inside'jo'
that has been assigned Json-Null, then this exception will throw. (Unlike Json, in Java a Primitiveboolean
will not accept 'null' as a value).- See Also:
getBOOLEAN(JsonObject, String, boolean)
- Code:
- Exact Method Body:
// Since 'false' is passed, a null return-value will mean that the property was actually // set to null in the JsonObject itself. (It *IS NOT* missing, it is present, but rather // declared null) Boolean ret = getBOOLEAN(jo, propertyName, false); if (ret == null) throw new JsonNullPrimitiveObjException (jo, propertyName, TRUE, boolean.class); // NOTE: Java's Type-Stuff can automatically convert Boolean -> boolean else return ret;
-
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.
NOTE: 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.
NOTE: 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
- The return-value / exception-throw flag 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
- The return-value / exception-throw flag 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); }
-
getString
public static java.lang.String getString(JsonArray ja, int index, boolean throwOnNull)
Retrieve aJsonArray
element, and transform it to ajava.lang.String
.- 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 aJsonString
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.String
, if the'ja'
array element located at'index'
uses the appropriate Json-Type -JsonString
.
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 aJsonString
(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()
,JsonValue.ValueType.STRING
- 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, STRING, String.class); else return null; case STRING: return ((JsonString) jv).getString(); // The JsonValue at the specified array-index does not contain a JsonString. default: throw new JsonTypeArrException(ja, index, STRING, jv, String.class); }
-
getString
public static java.lang.String getString(JsonObject jo, java.lang.String propertyName, boolean isOptional, boolean throwOnNull)
Extract aJsonObject
property, and transform it to ajava.lang.String
.- 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.String
.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.String'
, if the'propertyName'
is available inside'jo'
, and that property uses the appropriate Json-Type -JsonString
.
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 aJsonString
(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()
,JsonValue.ValueType.STRING
- Code:
- Exact Method Body:
if (! jo.containsKey(propertyName)) { if (isOptional) return null; throw new JsonPropMissingException(jo, propertyName, STRING, String.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, STRING, String.class); else return null; case STRING: return ((JsonString) jv).getString(); // The JsonObject propertydoes not contain a JsonString. default: throw new JsonTypeObjException(jo, propertyName, STRING, jv, String.class); }
-
-