Package Torello.JSON
Class ReadNumberJSON
- java.lang.Object
-
- Torello.JSON.ReadNumberJSON
-
public class ReadNumberJSON extends java.lang.Object
Builds on the J2EE Standard Release JSON Parsing Tools by providing additional help with converting JSON Data into the Best-Fitjava.lang.Number
This class builds on the J2EE Standard 'Glass-Fish' JSON Processor
There are several JSON Parsers available, and even more implementations for serializing and de-serializing data to/from JSON. The Glass Fish Tool is included in the J2EE, and is available on GitHub. That is the one used by the Java HTML JAR Library (See:javax.json.*)
Primary Classes Used:JsonArrayandJsonObject
This comment-note is intentionally repeated, verbatim, at the top of all Json Reader Classes in this package.
JSON to Java Binding:
JSON-Binding is the art of converting data that has been stored, saved or transmitted usingJava-Script Object Notationinto a Java Primitive or Object-Type.JSONcan arrive into Java-Program Memory from almost any source. If you are wondering why such a massive amount of "work" is necessary just to convert a Json Integer into Java Integer, the value added is the extraordinary amount of attention paid to user configuration, error checking, & exception messaging. Methods here don't require more than 1 or 2 lines of code, and guarantee that a thorough type checking is performed.
Unlike Java-Types which are checked by the Java-Compiler each-and-every time a programmer compiles his project, any guarantee that JSON-Type Data is pristine, uncorrupted, and in any kind of pre-agreed format may or may not be assured. The methods here are able to handle changes that might be made to an API (possibly from great distances away, and without the Software-Manager's consent). If an error could occur, configuration flags can be used to determine default error-recovery behaviors. If an exception does throw, the exception messages printed will contain multiple lines of detailed information.- Utilizes the Java-Standard
javax.json.*Package-Library, & its Glass-Fish Implementation - Handles the Transfer & Conversion of All Json-Type's into Java-Type's with just One Line of Code
- Provides all manner of User-Configurable Exception-Handling &
Error-Decision Management via Class
JFlag - Provides a Fine-Grained Suite of Exception-Classes, all with Consistent & Meaningful Error-Messages. Json Files can occasionally grow extremely large, and error messaging details make debugging easier
- Primary Helper-Classes for the (Experimental) Google-Chrome
Browser AutomationPackage
The goal ofTorello.JSONis to provide small, static helper methods in the same spirit asjava.util.Objectsandjava.util.Arrays: simple calls that keep JSON handling out of your application logic. Instead of repeating loops, type checks, null checks, and exception wiring at every call site, these methods centralize that work behind a consistent set of straight-forward “read value” operations.
Reads a Json NUMBER or numeric STRING and returns the best-fit java.lang.Number
This class reads a single Json value and returns a "best fit" instance ofjava.lang.Number. JSON data may represent numbers either as true JSON numbers (JsonNumber) or as numeric strings (JsonString), and this class supports both forms.
When the source is a true JSON number, the value is converted to the smallest standard Java numeric type that can represent it exactly. Integral values are returned as eitherInteger,Long, orBigIntegerdepending on magnitude. Non-integral values are returned as eitherFloat,Double, orBigDecimaldepending on whether the value round-trips without precision loss.
When the source is a JSON string, the text is parsed usingBigDecimal(by default) and then the same "best fit" conversion is applied. This means that values such as3and"3"can be handled uniformly, without forcing callers to duplicate type checks and parsing logic across large JSON traversals.
The primary goal is correctness and clarity. If a value is missing, is null, is not a NUMBER or STRING in the required context, or cannot be converted as requested, this package throws highly-informative exceptions so that errors are detected immediately while binding complex or evolving JSON structures.- See Also:
Json,JsonObject,JsonArray
Hi-Lited Source-Code:- View Here: Torello/JSON/ReadNumberJSON.java
- Open New Browser-Tab: Torello/JSON/ReadNumberJSON.java
File Size: 21,028 Bytes Line Count: 478 '\n' Characters Found
Stateless Class:This class neither contains any program-state, nor can it be instantiated. The@StaticFunctionalAnnotation may also be called 'The Spaghetti Report'.Static-Functionalclasses are, essentially, C-Styled Files, without any constructors or non-static member fields. It is a concept very similar to the Java-Bean's@StatelessAnnotation.
- 1 Constructor(s), 1 declared private, zero-argument constructor
- 11 Method(s), 11 declared static
- 0 Field(s)
- Utilizes the Java-Standard
-
-
Method Summary
Best-Fit: Guess appropriate Java Number-Type out of a JsonArrayModifier and Type Method static Numberget(JsonArray ja, int index, boolean throwOnNull)static Numberget(JsonArray ja, int index, int FLAGS, Number defaultValue)static Numberparse(JsonArray ja, int index, int FLAGS, Number defaultValue, Function<String,Number> optionalParser)Best-Fit: Guess appropriate Java Number-Type out of a JsonObjectModifier and Type Method static Numberget(JsonObject jo, String propertyName, boolean isOptional, boolean throwOnNull)static Numberget(JsonObject jo, String propertyName, int FLAGS, Number defaultValue)static Numberparse(JsonObject jo, String propertyName, int FLAGS, Number defaultValue, Function<String,Number> optionalParser)Handle either a JsonNumberor aJsonStringand Guess Number-TypeModifier and Type Method static Numberget(JsonArray ja, int i, int FLAGS, Number defaultValue, Function<String,Number> optionalParser)static Numberget(JsonObject jo, String propertyName, int FLAGS, Number defaultValue, Function<String,Number> optionalParser)Internal Use Only: (Protected Methods) Convert To Number Modifier and Type Method protected static NumberconvertToNumber(String s)protected static NumberconvertToNumber(BigDecimal bd)protected static NumberconvertToNumber(JsonNumber jn)
-
-
-
Method Detail
-
get
public static java.lang.Number get(JsonArray ja, int index, boolean throwOnNull)
Retrieve aJsonArrayelement, and transform it to ajava.lang.Number. There are several possible Boxed-Type subclasses ofjava.lang.Numberwhich 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.BigIntegerorBigDecimalis 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.Floatjava.lang.Doublejava.math.BigDecimal
- Parameters:
ja- Any instance ofJsonArrayindex- A valid index into'ja'throwOnNull- Asks that an exception throw if Json-Null is encountered- 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 aJsonNullArrExceptionthrows. - 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 theJsonArrayelement specified by'index'contains a Json-Null, andTRUEhas been passed to'throwOnNull'- See Also:
JsonValue.getValueType(),convertToNumber(JsonNumber)- Code:
- Exact Method Body:
// This will throw an IndexOutOfBoundsException if the index is out of bounds. JsonValue jv = ja.get(index); switch (jv.getValueType()) { case NULL: // This is simple-stuff (not rocket-science). "Type Mapping" Code has to worry // about what the meaning of "null" should be. if (throwOnNull) throw new JsonNullArrException(ja, index, NUMBER, Number.class); else return null; case NUMBER: return convertToNumber((JsonNumber) jv); // The JsonValue at the specified array-index does not contain a JsonString. default: throw new JsonTypeArrException(ja, index, NUMBER, jv, Number.class); }
-
get
public static java.lang.Number get(JsonArray ja, int index, int FLAGS, java.lang.Number defaultValue)
Retrieve aJsonArrayelement, and transform it to ajava.lang.Number. There are several possible Boxed-Type subclasses ofjava.lang.Numberwhich 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.BigIntegerorBigDecimalis 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.Floatjava.lang.Doublejava.math.BigDecimal
- Parameters:
ja- Any instance ofJsonArrayindex- The array index containing the element to retrieve.FLAGS- Return-value / exception-throw constants defined inJFlagdefaultValue- 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-classJFlagsIf 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:
- Best fit boxed number, or null.
- 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 theJsonArrayelement 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 theJsonArrayelement 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:
ReadBoxedJSON.GET(JsonArray, int, int, Number, Class, Function, Function),convertToNumber(JsonNumber)- Code:
- Exact Method Body:
return ReadBoxedJSON.GET (ja, index, FLAGS, defaultValue, Number.class, ReadNumberJSON::convertToNumber, null);
-
parse
public static java.lang.Number parse (JsonArray ja, int index, int FLAGS, java.lang.Number defaultValue, java.util.function.Function<java.lang.String,java.lang.Number> optionalParser)
Retrieve aJsonArrayelement 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.BigDecimalconstructor which accepts aString. What is done with the parsedBigDecimalinstance is explained below.
There are several possible Boxed-Type subclasses ofjava.lang.Numberwhich 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.BigIntegerorBigDecimalis 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.Floatjava.lang.Doublejava.math.BigDecimal
- Parameters:
ja- Any instance ofJsonArrayindex- The array index containing theJsonStringelement to retrieve.FLAGS- Return-value / exception-throw constants defined inJFlagdefaultValue- 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-classJFlagsIf 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.Stringinto the specified type.This parameter may be null, and if it is, the standard Java parser is used.- Returns:
- If the
JsonArrayindex contained aJsonString, and that string was successfully parsed to ajava.lang.Number, then it is returned.
If there were errors attempting to transform theStringarray-element, and the user-provided flags specified thatdefaultValuebe returned, then that is returned. Null is returned if the flags specified it on error.
Ifflagshas 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 theJsonArrayelement located at'index'throws an exception while attempting to parse thejava.lang.Stringinto 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 theJsonArrayelement 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 theJsonArrayelement 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:
ParseBoxedJSON.PARSE(JsonObject, String, int, Number, Class, Function, Function, Function),convertToNumber(String)- Code:
- Exact Method Body:
return ParseBoxedJSON.PARSE( ja, index, FLAGS, defaultValue, Number.class, optionalParser, ReadNumberJSON::convertToNumber, null /* Not Needed, convertToNumber won't throw */ );
-
get
public static java.lang.Number get(JsonObject jo, java.lang.String propertyName, boolean isOptional, boolean throwOnNull)
Extract aJsonObjectproperty, and transform it to ajava.lang.Number. There are several possible Boxed-Type subclasses ofjava.lang.Numberwhich 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.BigIntegerorBigDecimalis 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.Floatjava.lang.Doublejava.math.BigDecimal
- Parameters:
jo- Any instance ofJsonObject.propertyName- Any property name contained by'jo'isOptional- WhenTRUEis passed, if'propertyName'is not actually listed in'jo'this method shall return Java-Null gracefully. WhenFALSEis passed, if'jo'does not have the specified property, aJsonPropMissingExceptionwill throw.
If'jo'actually has a property named'propertyName', then the value passed to this parameter is fully irrelevant.throwOnNull- WhenTRUEis passed to this parameter, if theJsonArrayelement 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'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 aJsonNullObjExceptionthrows.
If the specified property is simply missing, Java-Null is returned, unless'isOptional'has been passedFALSE- in which case aJsonPropMissingExceptionshall 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 theJsonObjectproperty specified by'propertyName'contains a Json-Null, andTRUEhas been passed to'throwOnNull'- See Also:
JsonValue.getValueType(),convertToNumber(JsonNumber)- Code:
- Exact Method Body:
if (! jo.containsKey(propertyName)) { if (isOptional) return null; else throw new JsonPropMissingException(jo, propertyName, NUMBER, Number.class); } JsonValue jv = jo.get(propertyName); switch (jv.getValueType()) { case NULL: // This is simple-stuff (not rocket-science). "Type Mapping" Code has to worry // about what the meaning of "null" should be. if (throwOnNull) throw new JsonNullObjException (jo, propertyName, NUMBER, Number.class); else return null; case NUMBER: return convertToNumber((JsonNumber) jv); // The JsonObject propertydoes not contain a JsonNumber. default: throw new JsonTypeObjException (jo, propertyName, NUMBER, jv, Number.class); }
-
get
public static java.lang.Number get(JsonObject jo, java.lang.String propertyName, int FLAGS, java.lang.Number defaultValue)
Retrieve aJsonObjectproperty, and transform it to ajava.lang.Number. There are several possible Boxed-Type subclasses ofjava.lang.Numberwhich 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.BigIntegerorBigDecimalis 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.Floatjava.lang.Doublejava.math.BigDecimal
- Parameters:
jo- Any instance ofJsonObjectpropertyName- Any property name contained by'jo'FLAGS- Return-value / exception-throw constants defined inJFlagdefaultValue- 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-classJFlagsIf 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:
- Best fit boxed number, or null.
- 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 theJsonObjectproperty 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 theJsonObjectproperty 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:
ReadBoxedJSON.GET(JsonObject, String, int, Number, Class, Function, Function),convertToNumber(JsonNumber)- Code:
- Exact Method Body:
return ReadBoxedJSON.GET( jo, propertyName, FLAGS, defaultValue, Number.class, ReadNumberJSON::convertToNumber, null );
-
parse
public static java.lang.Number parse (JsonObject jo, java.lang.String propertyName, int FLAGS, java.lang.Number defaultValue, java.util.function.Function<java.lang.String,java.lang.Number> optionalParser)
Retrieve aJsonObjectproperty 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.BigDecimalconstructor which accepts aString. What is done with the parsedBigDecimalinstance is explained below.
There are several possible Boxed-Type subclasses ofjava.lang.Numberwhich 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.BigIntegerorBigDecimalis 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.Floatjava.lang.Doublejava.math.BigDecimal
- Parameters:
jo- Any instance ofJsonObjectpropertyName- Any property name contained by'jo'FLAGS- Return-value / exception-throw constants defined inJFlagdefaultValue- 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-classJFlagsIf 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.Stringinto the specified type.This parameter may be null, and if it is, the standard Java parser is used.- Returns:
- If the
JsonObjectproperty contained aJsonString, and that string was successfully parsed to ajava.lang.Number, then it is returned.
If there were errors attempting to transform theStringobject-property, and the user-provided flags specified thatdefaultValuebe returned, then that is returned. Null is returned if the flags specified it on error.
Ifflagshas 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 theJsonObjectproperty specified by'propertyName'throws an exception while attempting to parse thejava.lang.Stringinto 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 theJsonObjectproperty 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 theJsonObjectproperty 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:
ParseBoxedJSON.PARSE(JsonObject, String, int, Number, Class, Function, Function, Function),convertToNumber(String)- Code:
- Exact Method Body:
return ParseBoxedJSON.PARSE( jo, propertyName, FLAGS, defaultValue, Number.class, optionalParser, ReadNumberJSON::convertToNumber, null /* Not Needed, convertToNumber won't throw */ );
-
get
public static java.lang.Number get (JsonArray ja, int i, int FLAGS, java.lang.Number defaultValue, java.util.function.Function<java.lang.String,java.lang.Number> optionalParser)
Checks: Json Type of the specifiedJsonArrayindex contents, and dispatches based on type.
Flags: Accepts allError-Handler Flags; Suppresses exceptions when requested by the flag mask
Returns: A Java Boxed Number- Parameters:
ja- AnyJsonArrayi- Any index into theJsonArrayFLAGS- Return-value / exception-throw constants defined inJFlagdefaultValue- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'parameter! Specifically, if'flags'were passed'0', then this parameter is fully-irrelevant and ignored. The exception flags are listed in classJFlags
If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.optionalParser- A function wich can map ajava.lang.Stringinto a JavaNumber. This'optionalParser'is only used if the specified element contains aJsonStringrather than aJsonNumber. If aJsonNumberis found, or some other unrelated type, then this parameter is wholly ignored.
null may be passed to parameter'optionalParser'. If null is received, and if it turns out that aStringtoNumberparser-function is required to generate the result for this method, thennew BigDecimal(String.trim())will be used as the default-parser, instead.- Returns:
- If the
JsonArrayindex contained a number, and that number was successfully transformed to aNumber, then it is returned.
If the array-index was aString, and thatStringwas transformed into aNumberusing either'parser'- or the default parser, then thatNumberis returned.
In all exception or error cases, parameter'FLAGS'may indicate that either the default-value be returned, or that null be returned. If the exception-handling flags were not sufficiently set, and an error-case has occured, one of the exceptions listed below is thrown. - Throws:
java.lang.IndexOutOfBoundsException- If'index'is out of the bounds of input array'ja'and none of these Exception-Flags are set:RETURN_NULL_ON_IOB(Abbrev:RN_IOB)RETURN_DEFVAL_ON_IOB(Abbrev:RD_IOB)RETURN_NULL_ON_ANY_ALL(Abbrev:RN_AA)RETURN_DEFVAL_ON_ANY_ALL(Abbrev:RD_AA)
JsonStrParseArrException- If theJsonArrayelement located at'index'contains aJsonString, this method will attempt to parse thatStringinto a JavaNumber, using either the user-provided'parser'(if provided), or otherwise, the default-parser (new BigDecimal(String.trim())). This exception will throw if parsing theStringgenerates an exception throw AND none of these Exception-Flags are set:RETURN_NULL_ON_SPEX(Abbrev:RN_SPEX)RETURN_DEFVAL_ON_SPEX(Abbrev:RD_SPEX)RETURN_NULL_ON_ANY_ALL(Abbrev:RN_AA)RETURN_DEFVAL_ON_ANY_ALL(Abbrev:RD_AA)
JsonNullArrException- If theJsonArrayelement located at'index'contains Json-Null, rather than aJsonNumberor 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 theJsonArrayelement located at'index'neither contains aJsonNumber, nor aJsonString, AND none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE(Abbrev:RN_WT)RETURN_DEFVAL_ON_WRONG_JSONTYPE(Abbrev:RD_WT)RETURN_NULL_ON_ANY_ALL(Abbrev:RN_AA)RETURN_DEFVAL_ON_ANY_ALL(Abbrev:RD_AA)
- See Also:
get(JsonArray, int, int, Number),parse(JsonArray, int, int, Number, Function)- Code:
- Exact Method Body:
JsonValue.ValueType t; return ((i >= ja.size()) || ((t=ja.get(i).getValueType()) == NUMBER) || (t != STRING)) ? get(ja, i, FLAGS, defaultValue) : parse(ja, i, FLAGS, defaultValue, optionalParser);
-
get
public static java.lang.Number get (JsonObject jo, java.lang.String propertyName, int FLAGS, java.lang.Number defaultValue, java.util.function.Function<java.lang.String,java.lang.Number> optionalParser)
Checks: Json Type of the specifiedJsonObjectproperty, and dispatches based on type.
Flags: Accepts allError-Handler Flags; Suppresses exceptions when requested by the flag mask
Returns: A Java Boxed Number- Parameters:
jo- AnyJsonObjectpropertyName- Any of the properties defined in theJsonObjectFLAGS- Return-value / exception-throw constants defined inJFlagdefaultValue- This is the 'Default Value' returned by this method, if there are any exception or problems converting or extracting the specified number.
The default value is only returned by this method if the appropriate JSON Exception Flag has been passed to the'FLAGS'parameter! Specifically, if'flags'were passed'0', then this parameter is fully-irrelevant and ignored. The exception flags are listed in classJFlags
If an exception-case is encountered, but the exception-flag for that exception has not been set, then this default-value won't be returned, and that exception will throw instead.optionalParser- A function wich can map ajava.lang.Stringinto a JavaNumber. This'optionalParser'is only used if the specified element contains aJsonStringrather than aJsonNumber. If aJsonNumberis found, or some other unrelated type, then this parameter is wholly ignored.
null may be passed to parameter'optionalParser'. If null is received, and if it turns out that aStringtoNumberparser-function is required to generate the result for this method, thennew BigDecimal(String.trim())will be used as the default-parser, instead.- Returns:
- If the
JsonObjectproperty contained a number, and that number was successfully transformed to aNumber, then it is returned.
If the object-property was aString, and thatStringwas transformed into aNumberusing either'parser'- or the default parser, then thatNumberis returned.
In all exception or error cases, parameter'FLAGS'may indicate that either the default-value be returned, or that null be returned. If the exception-handling flags were not sufficiently set, and an error-case has occured, one of the exceptions listed below is thrown. - Throws:
JsonPropMissingException- If'jo'does not have a property with the name'propertyName'and none of these Exception-Flags are set:RETURN_NULL_ON_MISSING(Abbrev:RN_M)RETURN_DEFVAL_ON_MISSING(Abbrev:RD_M)RETURN_NULL_ON_ANY_ALL(Abbrev:RN_AA)RETURN_DEFVAL_ON_ANY_ALL(Abbrev:RD_AA)
JsonStrParseObjException- If theJsonObjectproperty specified by'propertyName'contains aJsonString, this method will attempt to parse thatStringinto a JavaNumber, using either the user-provided'parser'(if provided), or otherwise, the default-parser (new BigDecimal(String.trim())). This exception will throw if parsing theStringgenerates an exception throw AND none of these Exception-Flags are set:RETURN_NULL_ON_SPEX(Abbrev:RN_SPEX)RETURN_DEFVAL_ON_SPEX(Abbrev:RD_SPEX)RETURN_NULL_ON_ANY_ALL(Abbrev:RN_AA)RETURN_DEFVAL_ON_ANY_ALL(Abbrev:RD_AA)
JsonNullObjException- If theJsonObjectproperty specified by'propertyName'contains Json-Null, rather than aJsonNumberor 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 theJsonObjectproperty specified by'propertyName'neither contains aJsonNumber, nor aJsonString, AND none of these Exception-Flags are set:RETURN_NULL_ON_WRONG_JSONTYPE(Abbrev:RN_WT)RETURN_DEFVAL_ON_WRONG_JSONTYPE(Abbrev:RD_WT)RETURN_NULL_ON_ANY_ALL(Abbrev:RN_AA)RETURN_DEFVAL_ON_ANY_ALL(Abbrev:RD_AA)
- See Also:
get(JsonObject, String, int, Number),parse(JsonObject, String, int, Number, Function)- Code:
- Exact Method Body:
JsonValue.ValueType t; return ( (! jo.containsKey(propertyName)) || ((t = jo.get(propertyName).getValueType()) == NUMBER) || (t != STRING) ) ? get(jo, propertyName, FLAGS, defaultValue) : parse(jo, propertyName, FLAGS, defaultValue, optionalParser);
-
convertToNumber
protected static java.lang.Number convertToNumber(JsonNumber jn)
Converts anyJsonNumberinto one of the inheriting subclasses of Java classNumber- Parameters:
jn- AnyJsonNumber- Returns:
- The most appropriate intance of
java.lang.Number - See Also:
get(JsonObject, String, int, Number),JsonNumber.isIntegral(),JsonNumber.bigIntegerValue(),JsonNumber.bigDecimalValue()- Code:
- Exact Method Body:
if (jn.isIntegral()) { BigInteger bi = jn.bigIntegerValue(); int l = bi.bitLength(); if (l <= 32) return Integer.valueOf(bi.intValue()); if (l <= 64) return Long.valueOf(bi.longValue()); return bi; } else { BigDecimal bd = jn.bigDecimalValue(); // This probably isn't the most efficient thing I've ever written, but I do not // have the energy to stare at java.math.BigDecimal at the moment. The JavaDoc for // this JSON => Java-Type Conversion is quite intricate. I will figure this out at // at later date. float f = bd.floatValue(); if ( (! Float.isInfinite(f)) && (BigDecimal.valueOf(f).compareTo(bd) == 0) ) return (Float) f; double d = bd.doubleValue(); if ( (! Double.isInfinite(d)) && (BigDecimal.valueOf(d).compareTo(bd) == 0) ) return (Double) d; return bd; }
-
convertToNumber
protected static java.lang.Number convertToNumber(java.lang.String s)
Converts anyjava.lang.Stringinto one of the inheriting subclasses of Java classNumber- Parameters:
s- AnyString- Returns:
- The most appropriate instance of
java.lang.Number - Throws:
java.lang.NumberFormatException- If the inputStringisn't properly formatted as a number.- See Also:
parse(JsonObject, String, int, Number, Function),parse(JsonArray, int, int, Number, Function)- Code:
- Exact Method Body:
return convertToNumber(new BigDecimal(s.trim()));
-
convertToNumber
protected static java.lang.Number convertToNumber(java.math.BigDecimal bd)
Converts anyjava.math.BigDecimalinto one of the inheriting subclasses ofNumber.- Parameters:
bd- AnyBigDecimal- Returns:
- The most appropriate instance of
java.lang.Number - Code:
- Exact Method Body:
if (bd.scale() == 0) { BigInteger bi = bd.toBigInteger(); int l = bi.bitLength(); if (l <= 32) return Integer.valueOf(bi.intValue()); if (l <= 64) return Long.valueOf(bi.longValue()); return bi; } else { // This probably isn't the most efficient thing I've ever written, but I do not // have the energy to stare at java.math.BigDecimal at the moment. The JavaDoc for // this JSON => Java-Type Conversion is quite intricate. I will figure this out at // at later date. float f = bd.floatValue(); if ( (! Float.isInfinite(f)) && (BigDecimal.valueOf(f).compareTo(bd) == 0) ) return (Float) f; double d = bd.doubleValue(); if ( (! Double.isInfinite(d)) && (BigDecimal.valueOf(d).compareTo(bd) == 0) ) return (Double) d; return bd; }
-
-