Package Torello.JSON
Class RorPPrimJSON
- java.lang.Object
-
- Torello.JSON.RorPPrimJSON
-
public class RorPPrimJSON extends java.lang.Object
Utilities for parsing & convertingJsonString's orJsonNumber's into Java Boxed-Primitive Types.
This class builds on the J2EE Standard 'Glass-Fish' JSON Processor
There are several JSON Parsers available, and even more implementations for serializing and de-serializing data to/from JSON. The 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 single JSON value (either a number or a string) from a JsonArray or JsonObject and converts it into a corresponding Java primitive type.
This class exists to make reading numeric and boolean values from large, complex JSON structures both safer and easier. While JSON itself is loosely typed, Java is not, and bridging that gap reliably requires careful handling of value types, missing fields, and unexpected representations.
In practice, JSON sources may freely alternate between numeric values and their string representations (for example3versus"3"), may omit optional fields entirely, or may returnnullin cases where a value is not mandatory. This class provides explicit, well-organized methods for handling each of these cases, ensuring that conversions are performed deliberately and that mistakes are detected immediately.
Rather than attempting to infer or generate higher-level Java types, this class focuses on making low-level JSON access predictable and transparent. A significant amount of effort is dedicated to validating inputs and producing detailed error messages, so that when something goes wrong while traversing an unfamiliar or evolving JSON document, the cause of the failure is clear and actionable. The result is less repeated boilerplate, fewer silent failures, and greater confidence when consuming complex JSON data.
Method Parameters
Parameter Explanation jaThe source JsonArrayfrom which the value is retrieved.indexThe zero-based index within jaidentifying the target element.joThe source JsonObjectfrom which the property value is retrieved.propertyNameThe name of the property whose value is to be read and parsed. optionalUserParserAn optional user-supplied parser applied to the raw string value before conversion; may be nullto use default parsing behavior.
Single Character:
Note that this class simply doesn't support any methods for reading ajava.lang.Character. This is because their are just entirely too many possibilities, combinations & options for the appropriate "Default Behavior" when attempting to read a single character of data from any random JSON Data-Type.
Remember that the JSON Specification does not posses any "Single-JSON-Character" Data-Type. Creating some kind of Flag-Controlled Reader for a'char'that can capably decide what to do would be so overtly verbose, for something so simple, that it isn't worth the effort...- See Also:
JsonObject,JsonArray
Hi-Lited Source-Code:- View Here: Torello/JSON/RorPPrimJSON.java
- Open New Browser-Tab: Torello/JSON/RorPPrimJSON.java
File Size: 14,146 Bytes Line Count: 369 '\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
- 14 Method(s), 14 declared static
- 0 Field(s)
- Utilizes the Java-Standard
-
-
Method Summary
From JsonArray: Retrieve either a JsonNumberorJsonStringfrom aJsonArrayand Transform it to a Java PrimitiveModifier and Type Method static booleangetBoolean(JsonArray ja, int i, Predicate<String> optionalUserParser)static bytegetByte(JsonArray ja, int i, ToByteFunction<String> optionalUserParser)static doublegetDouble(JsonArray ja, int i, ToDoubleFunction<String> optionalUserParser)static floatgetFloat(JsonArray ja, int i, ToFloatFunction<String> optionalUserParser)static intgetInteger(JsonArray ja, int i, ToIntFunction<String> optionalUserParser)static longgetLong(JsonArray ja, int i, ToLongFunction<String> optionalUserParser)static shortgetShort(JsonArray ja, int i, ToShortFunction<String> optionalUserParser)From JsonObject: Retrieve either a JsonNumberorJsonStringfrom aJsonObjectand Transform it to a Java PrimitiveModifier and Type Method static booleangetBoolean(JsonObject jo, String propertyName, Predicate<String> optionalUserParser)static bytegetByte(JsonObject jo, String propertyName, ToByteFunction<String> optionalUserParser)static doublegetDouble(JsonObject jo, String propertyName, ToDoubleFunction<String> optionalUserParser)static floatgetFloat(JsonObject jo, String propertyName, ToFloatFunction<String> optionalUserParser)static intgetInteger(JsonObject jo, String propertyName, ToIntFunction<String> optionalUserParser)static longgetLong(JsonObject jo, String propertyName, ToLongFunction<String> optionalUserParser)static shortgetShort(JsonObject jo, String propertyName, ToShortFunction<String> optionalUserParser)
-
-
-
Method Detail
-
getInteger
public static int getInteger (JsonArray ja, int i, java.util.function.ToIntFunction<java.lang.String> optionalUserParser)
Read either a JsonNumber or a JsonString from a JsonArray, and Translate to a Java 'int' PrimitiveConverts: Either a JsonNumberor aJsonStringinto a Java'int'primitive.- See Also:
ReadPrimJSON.getInt(JsonArray, int),ParsePrimJSON.parseInt(JsonArray, int, ToIntFunction)- Code:
- Exact Method Body:
return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING)) ? ReadPrimJSON.getInt(ja, i) : ParsePrimJSON.parseInt(ja, i, optionalUserParser);
-
getLong
public static long getLong (JsonArray ja, int i, java.util.function.ToLongFunction<java.lang.String> optionalUserParser)
Read either a JsonNumber or a JsonString from a JsonArray, and Translate to a Java 'long' PrimitiveConverts: Either a JsonNumberor aJsonStringinto a Java'long'primitive.- See Also:
ReadPrimJSON.getLong(JsonArray, int),ParsePrimJSON.parseLong(JsonArray, int, ToLongFunction)- Code:
- Exact Method Body:
return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING)) ? ReadPrimJSON.getLong(ja, i) : ParsePrimJSON.parseLong(ja, i, optionalUserParser);
-
getShort
public static short getShort (JsonArray ja, int i, ToShortFunction<java.lang.String> optionalUserParser)
Read either a JsonNumber or a JsonString from a JsonArray, and Translate to a Java 'short' PrimitiveConverts: Either a JsonNumberor aJsonStringinto a Java'short'primitive.- See Also:
ReadPrimJSON.getShort(JsonArray, int),ParsePrimJSON.parseShort(JsonArray, int, ToShortFunction)- Code:
- Exact Method Body:
return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING)) ? ReadPrimJSON.getShort(ja, i) : ParsePrimJSON.parseShort(ja, i, optionalUserParser);
-
getByte
public static byte getByte (JsonArray ja, int i, ToByteFunction<java.lang.String> optionalUserParser)
Read either a JsonNumber or a JsonString from a JsonArray, and Translate to a Java 'byte' PrimitiveConverts: Either a JsonNumberor aJsonStringinto a Java'byte'primitive.- See Also:
ReadPrimJSON.getByte(JsonArray, int),ParsePrimJSON.parseByte(JsonArray, int, ToByteFunction)- Code:
- Exact Method Body:
return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING)) ? ReadPrimJSON.getByte(ja, i) : ParsePrimJSON.parseByte(ja, i, optionalUserParser);
-
getDouble
public static double getDouble (JsonArray ja, int i, java.util.function.ToDoubleFunction<java.lang.String> optionalUserParser)
Read either a JsonNumber or a JsonString from a JsonArray, and Translate to a Java 'double' PrimitiveConverts: Either a JsonNumberor aJsonStringinto a Java'double'primitive.- See Also:
ReadPrimJSON.getDouble(JsonArray, int),ParsePrimJSON.parseDouble(JsonArray, int, ToDoubleFunction)- Code:
- Exact Method Body:
return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING)) ? ReadPrimJSON.getDouble(ja, i) : ParsePrimJSON.parseDouble(ja, i, optionalUserParser);
-
getFloat
public static float getFloat (JsonArray ja, int i, ToFloatFunction<java.lang.String> optionalUserParser)
Read either a JsonNumber or a JsonString from a JsonArray, and Translate to a Java 'float' PrimitiveConverts: Either a JsonNumberor aJsonStringinto a Java'float'primitive.- See Also:
ReadPrimJSON.getFloat(JsonArray, int),ParsePrimJSON.parseFloat(JsonArray, int, ToFloatFunction)- Code:
- Exact Method Body:
return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING)) ? ReadPrimJSON.getFloat(ja, i) : ParsePrimJSON.parseFloat(ja, i, optionalUserParser);
-
getBoolean
public static boolean getBoolean (JsonArray ja, int i, java.util.function.Predicate<java.lang.String> optionalUserParser)
Read either a Json TRUE, FALSE or a JsonString from a JsonArray, and Translate to a Java 'boolean' PrimitiveConverts: Either a JsonNumberor aJsonStringinto a Java'boolean'primitive.- See Also:
ReadPrimJSON.getBoolean(JsonArray, int),ParsePrimJSON.parseBoolean(JsonArray, int, Predicate)- Code:
- Exact Method Body:
return ((i >= ja.size()) || (ja.get(i).getValueType() != STRING)) ? ReadPrimJSON.getBoolean(ja, i) : ParsePrimJSON.parseBoolean(ja, i, optionalUserParser);
-
getInteger
public static int getInteger (JsonObject jo, java.lang.String propertyName, java.util.function.ToIntFunction<java.lang.String> optionalUserParser)
Read either a JsonNumber or a JsonString from a JsonObject, and Translate to a Java 'int' PrimitiveConverts: Either a JsonNumberor aJsonStringinto a Java'int'primitive.- See Also:
ReadPrimJSON.getInt(JsonObject, String),ParsePrimJSON.parseInt(JsonObject, String, ToIntFunction)- Code:
- Exact Method Body:
final JsonValue jv = jo.get(propertyName); return ((jv == null) || (jv.getValueType() != STRING)) ? ReadPrimJSON.getInt(jo, propertyName) : ParsePrimJSON.parseInt(jo, propertyName, optionalUserParser);
-
getLong
public static long getLong (JsonObject jo, java.lang.String propertyName, java.util.function.ToLongFunction<java.lang.String> optionalUserParser)
Read either a JsonNumber or a JsonString from a JsonObject, and Translate to a Java 'long' PrimitiveConverts: Either a JsonNumberor aJsonStringinto a Java'long'primitive.- See Also:
ReadPrimJSON.getLong(JsonObject, String),ParsePrimJSON.parseLong(JsonObject, String, ToLongFunction)- Code:
- Exact Method Body:
final JsonValue jv = jo.get(propertyName); return ((jv == null) || (jv.getValueType() != STRING)) ? ReadPrimJSON.getLong(jo, propertyName) : ParsePrimJSON.parseLong(jo, propertyName, optionalUserParser);
-
getShort
public static short getShort (JsonObject jo, java.lang.String propertyName, ToShortFunction<java.lang.String> optionalUserParser)
Read either a JsonNumber or a JsonString from a JsonObject, and Translate to a Java 'short' PrimitiveConverts: Either a JsonNumberor aJsonStringinto a Java'short'primitive.- See Also:
ReadPrimJSON.getShort(JsonObject, String),ParsePrimJSON.parseShort(JsonObject, String, ToShortFunction)- Code:
- Exact Method Body:
final JsonValue jv = jo.get(propertyName); return ((jv == null) || (jv.getValueType() != STRING)) ? ReadPrimJSON.getShort(jo, propertyName) : ParsePrimJSON.parseShort(jo, propertyName, optionalUserParser);
-
getByte
public static byte getByte (JsonObject jo, java.lang.String propertyName, ToByteFunction<java.lang.String> optionalUserParser)
Read either a JsonNumber or a JsonString from a JsonObject, and Translate to a Java 'byte' PrimitiveConverts: Either a JsonNumberor aJsonStringinto a Java'byte'primitive.- See Also:
ReadPrimJSON.getByte(JsonObject, String),ParsePrimJSON.parseByte(JsonObject, String, ToByteFunction)- Code:
- Exact Method Body:
final JsonValue jv = jo.get(propertyName); return ((jv == null) || (jv.getValueType() != STRING)) ? ReadPrimJSON.getByte(jo, propertyName) : ParsePrimJSON.parseByte(jo, propertyName, optionalUserParser);
-
getDouble
public static double getDouble (JsonObject jo, java.lang.String propertyName, java.util.function.ToDoubleFunction<java.lang.String> optionalUserParser)
Read either a JsonNumber or a JsonString from a JsonObject, and Translate to a Java 'double' PrimitiveConverts: Either a JsonNumberor aJsonStringinto a Java'double'primitive.- See Also:
ReadPrimJSON.getDouble(JsonObject, String),ParsePrimJSON.parseDouble(JsonObject, String, ToDoubleFunction)- Code:
- Exact Method Body:
final JsonValue jv = jo.get(propertyName); return ((jv == null) || (jv.getValueType() != STRING)) ? ReadPrimJSON.getDouble(jo, propertyName) : ParsePrimJSON.parseDouble(jo, propertyName, optionalUserParser);
-
getFloat
public static float getFloat (JsonObject jo, java.lang.String propertyName, ToFloatFunction<java.lang.String> optionalUserParser)
Read either a JsonNumber or a JsonString from a JsonObject, and Translate to a Java 'float' PrimitiveConverts: Either a JsonNumberor aJsonStringinto a Java'float'primitive.- See Also:
ReadPrimJSON.getFloat(JsonObject, String),ParsePrimJSON.parseFloat(JsonObject, String, ToFloatFunction)- Code:
- Exact Method Body:
final JsonValue jv = jo.get(propertyName); return ((jv == null) || (jv.getValueType() != STRING)) ? ReadPrimJSON.getFloat(jo, propertyName) : ParsePrimJSON.parseFloat(jo, propertyName, optionalUserParser);
-
getBoolean
public static boolean getBoolean (JsonObject jo, java.lang.String propertyName, java.util.function.Predicate<java.lang.String> optionalUserParser)
Read either a Json TRUE, FALSE or a JsonString from a JsonObject, and Translate to a Java 'boolean' PrimitiveConverts: Either a JsonNumberor aJsonStringinto a Java'boolean'primitive.- See Also:
ReadPrimJSON.getBoolean(JsonObject, String),ParsePrimJSON.parseBoolean(JsonObject, String, Predicate)- Code:
- Exact Method Body:
final JsonValue jv = jo.get(propertyName); return ((jv == null) || (jv.getValueType() != STRING)) ? ReadPrimJSON.getBoolean(jo, propertyName) : ParsePrimJSON.parseBoolean(jo, propertyName, optionalUserParser);
-
-