Package Torello.JSON
Class ReadJSON
- java.lang.Object
-
- Torello.JSON.ReadJSON
-
public class ReadJSON extends java.lang.Object
Builds on the J2EE Standard Release JSON Parsing Tools by providing additional help with converting JSON Data into Java Object-Data.
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.
ReadJSON: Readers for Non-Primitive JSON Values
ReadJSONis a static helper class dedicated to reading the non-primitive JSON value types handled by the Java-HTML JSON package. Unlike the boxed-primitive and primitive readers, this class focuses on higher-level JSON structures and user-defined object construction.
This class exists specifically to handle the other JSON-mappable types:String,JsonArray,JsonObject, and user-provided object types (<T>).
All numeric values, booleans, and primitive conversions are intentionally excluded from this class and are handled by the boxed-primitive and primitive reader classes elsewhere in the package.
For user-defined types (<T>), the standard methods in this class expect the target type to expose a constructor that accepts a singleJsonObject. This allows JSON-backed domain objects to be built in a direct, explicit, and predictable manner.
This design keeps object construction visible and local, rather than hidden behind reflection, annotations, or automatic binding rules.
Design Intent:
ReadJSONis intentionally narrow in scope. It does not perform automatic binding, reflection-based mapping, or annotation processing. It reads exactly one JSON value, enforces the expected structure, and either returns the requested type or fails immediately with a descriptive error.
Together with the primitive and boxed-primitive readers, this class completes the full set of JSON value handling needed by the Java-HTML JSON package — while keeping each concern isolated, explicit, and easy to reason about.
POJO De-Serialization:
The methods that appear to de-serialize a JSON-Object into a standard Java-Object do not automatically generate a Java-Type based solely on the properties present within aJsonObject. Automatic type-creation (as exists in libraries such as 'Lombok') is not a feature offered by this package. These methods can reduce quite a bit of the work needed to convert Json into Java by offering methods which extactJsonValue'susing just a single line of code.
The methods in this class which return or emit a user-provided Parameterized-Type<T>require the user specify a means for constructing instances of'T'. This class expects one of the two cases:- User-Provided Type
<T>has a single argument constructor which accepts aJsonObjectas a parameter. In this case, standard Java Reflection is used to retrieve and extract this constructor.
- A Java Functional-Interface / Lambda be provided that accepts a
JsonObjectand returns an instance of User-Provided Type<T>. Building or generating an instance of<T>is acheived, simply, by invoking this function with theJsonObjectwhich was extracted from the array.
- See Also:
Json,JsonObject,JsonArray
Hi-Lited Source-Code:- View Here: Torello/JSON/ReadJSON.java
- Open New Browser-Tab: Torello/JSON/ReadJSON.java
File Size: 25,340 Bytes Line Count: 583 '\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
Object <T>: Retrieve a JsonObjectand transform it to a POJO, using the POJO's ConstructorModifier and Type Method static <T> TgetObject(JsonArray ja, int index, Class<T> c, boolean throwOnNull)static <T> TgetObject(JsonObject jo, String propertyName, Class<T> c, boolean isOptional, boolean throwOnNull)Object <T>: Retrieve a JsonObjectand transform it to a POJO, with a User-Provided Builder-FunctionModifier and Type Method static <T> TgetObject(JsonArray ja, int index, Function<JsonObject,? extends T> builderFunction, Class<T> c, boolean throwOnNull)static <T> TgetObject(JsonObject jo, String propertyName, Function<JsonObject,? extends T> builderFunction, Class<T> c, boolean isOptional, boolean throwOnNull)Object <T>: Transform a JsonObjectto a POJO, using the POJO's ConstructorModifier and Type Method static <T> TgetObject(JsonObject jo, Class<T> c)String: Retrieve a JsonStringand Transform it to a Java StringModifier and Type Method static StringgetString(JsonArray ja, int index, boolean throwOnNull)static StringgetString(JsonObject jo, String propertyName, boolean isOptional, boolean throwOnNull)JsonObject: Retrieve a JsonObjectModifier and Type Method static JsonObjectgetJsonObject(JsonArray ja, int index, boolean throwOnNull)static JsonObjectgetJsonObject(JsonObject jo, String propertyName, boolean isOptional, boolean throwOnNull)JsonArray: Retrieve a JsonArrayModifier and Type Method static JsonArraygetJsonArray(JsonArray ja, int index, boolean throwOnNull)static JsonArraygetJsonArray(JsonObject jo, String propertyName, boolean isOptional, boolean throwOnNull)
-
-
-
Method Detail
-
getObject
public static <T> T getObject(JsonArray ja, int index, java.lang.Class<T> c, boolean throwOnNull)
Retrieve aJsonArrayelement, and transform it to a JavaObject(POJO).- Type Parameters:
T- This is the Java-Type to which theJsonObjectis going to be bound / transformed.
It is important to note that this method is not as smart as you might think. The Java-Type instance that is being created / instantiated must contain a consructor that accepts aJsonObjectas a parameter in order to do the actual bindng. specifically, whatever class is passed to type parameter'T', that class must have a single-parameter constructor whose parameter-type isJsonObject- Parameters:
ja- Any instance ofJsonArrayindex- A valid index into'ja'c- This is the class that will bind to theJsonObjectparameter'jo'.The class'c'must have a constructor that accepts aJsonObject. If this class doesn't have such a constructor, then aJsonBindingExceptionthrows.throwOnNull- WhenTRUEis passed to this parameter, if theJsonObjectproperty 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
Type Parameter T, if the'ja'array element located at'index'uses the appropriate Json-Type -JsonObject.
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 bounds of'ja'java.lang.NullPointerException- If either'c'or'ja'are passed nullInvalidClassException- Throws if a User-ProvidedclassJsonTypeArrException- If the array-element (specified by'index') does not actually contain aJsonObject(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'JsonBuildPOJOArrException- Throws if theJsonObjectproperty specified by'propertyName'cannot be instantiated into a Java POJO.
If this method is using a constructor, then failing to retrieve or execute the constructor will cause this exception to be thrown. If this method is executing a User Provided Build-Function, and the function throws, then that exception will be wrapped into this one's'cause'and re-thrown.- Code:
- Exact Method Body:
final JsonValue jv = NPE_CHECK_JA(ja).get(index); final Constructor<T> ctor = InvalidClassException.check(c); 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, OBJECT, c); else return null; case OBJECT: try { return ctor.newInstance((JsonObject) jv); } catch (Exception e) { throw new JsonBuildPOJOArrException(e, ja, index, (JsonObject) jv, c); } // The JsonValue at the specified array-index does not contain a JsonObject. default: throw new JsonTypeArrException(ja, index, OBJECT, jv, c); }
-
getObject
public static <T> T getObject(JsonObject jo, java.lang.String propertyName, java.lang.Class<T> c, boolean isOptional, boolean throwOnNull)
Extract aJsonObjectproperty, and transform it to a JavaObject(POJO).- Type Parameters:
T- This is the Java-Type to which theJsonObjectis going to be bound / transformed.
It is important to note that this method is not as smart as you might think. The Java-Type instance that is being created / instantiated must contain a consructor that accepts aJsonObjectas a parameter in order to do the actual bindng. specifically, whatever class is passed to type parameter'T', that class must have a single-parameter constructor whose parameter-type isJsonObject- 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
Type Parameter T', if the'propertyName'is available inside'jo', and that property uses the appropriate Json-Type -JsonObject.
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'.java.lang.NullPointerException- If'jo', 'propertyName'or'c'are nullInvalidClassException- Throws if a User-ProvidedclassJsonTypeObjException- If the property (specified by'propertyName') is extracted, but that property does not actually contain aJsonObject(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'JsonBuildPOJOObjException- Throws if theJsonObjectlocated at'index'cannot be instantiated into a Java POJO.
If this method is using a constructor, then failing to retrieve or execute the constructor will cause this exception to be thrown. If this method is executing a User Provided Build-Function, and the function throws, then that exception will be wrapped into this one's'cause'and re-thrown.- Code:
- Exact Method Body:
final JsonValue jv = NPE_CHECK_JO(jo, propertyName); final Constructor<T> ctor = InvalidClassException.check(c); if (jv == null) { if (isOptional) return null; throw new JsonPropMissingException(jo, propertyName, OBJECT, c); } 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, OBJECT, c); else return null; case OBJECT: try { return ctor.newInstance(jo); } catch (Exception e) { throw new JsonBuildPOJOObjException (e, jo, propertyName, (JsonObject) jv, c); } // The JsonObject propertydoes not contain a JsonObject. default: throw new JsonTypeObjException(jo, propertyName, OBJECT, jv, c); }
-
getObject
public static <T> T getObject (JsonArray ja, int index, java.util.function.Function<JsonObject,? extends T> builderFunction, java.lang.Class<T> c, boolean throwOnNull)
Reads one JsonObject from a JsonArray, and uses a Generator Function to build a POJOIdentical To: Method getObject()Primary Difference: Accepts a User-Provided "Builder" or "Generator" Function, rather a using a classconstructor.See that method's documentation for throws and parameters explanation - Code:
- Exact Method Body:
final JsonValue jv = NPE_CHECK_JA(ja).get(index); switch (jv.getValueType()) { case NULL: if (throwOnNull) throw new JsonNullArrException(ja, index, OBJECT, c); else return null; case OBJECT: try { return builderFunction.apply((JsonObject) jv); } catch (Exception e) { throw new JsonBuildPOJOArrException(e, ja, index, (JsonObject) jv, c); } default: throw new JsonTypeArrException(ja, index, OBJECT, jv, c); }
-
getObject
public static <T> T getObject (JsonObject jo, java.lang.String propertyName, java.util.function.Function<JsonObject,? extends T> builderFunction, java.lang.Class<T> c, boolean isOptional, boolean throwOnNull)
Reads one JsonObject from a JsonArray, and uses a Generator Function to build a POJOIdentical To: Method getObject()Primary Difference: Accepts a User-Provided "Builder" or "Generator" Function, rather a using a classconstructor.See that method's documentation for throws and parameters explanation - Code:
- Exact Method Body:
final JsonValue jv = NPE_CHECK_JO(jo, propertyName); if (jv == null) { if (isOptional) return null; throw new JsonPropMissingException(jo, propertyName, OBJECT, c); } switch (jv.getValueType()) { case NULL: if (throwOnNull) throw new JsonNullObjException(jo, propertyName, OBJECT, c); else return null; case OBJECT: try { return builderFunction.apply((JsonObject) jv); } catch (Exception e) { throw new JsonBuildPOJOObjException (e, jo, propertyName, (JsonObject) jv, c); } default: throw new JsonTypeObjException(jo, propertyName, OBJECT, jv, c); }
-
getObject
public static <T> T getObject(JsonObject jo, java.lang.Class<T> c)
This class contains a lot of the reason / impetus for writing'ReadJSON'. This does converts aJsonObjectinto a Java-Object. The actual binding must be implemented by the programmer - because the class-type that is passed to this method (parameter'c') must have a constructor accepting thisJsonObject.
No Auto-Mapping:
This method makes no effort to figure out whichJsonObjectproperties should be bound to the POJOObjectfields! Note, however, that all of the other one-line type conversion methods in this class is exactly why writing such a constuctor for your Java-Type's should be much easier.
That really is the point of this JSON Package.- Type Parameters:
T- This is the Java-Type to which theJsonObjectis going to be bound / transformed.
It is important to note that this method is not as smart as you might think. The Java-Type instance that is being created / instantiated must contain a consructor that accepts aJsonObjectas a parameter in order to do the actual bindng. specifically, whatever class is passed to type parameter'T', that class must have a single-parameter constructor whose parameter-type isJsonObject- Parameters:
jo- This may be anyJsonObjectwhich can be bound to'c'.c- This is the class that will bind to theJsonObjectparameter'jo'.The class'c'must have a constructor that accepts aJsonObject. If this class doesn't have such a constructor, then aJsonBindingExceptionthrows.- Returns:
- An instance of the class
'T', which is specified by'c' - Throws:
JsonException- If any of the Java reflection-operations throw exceptions, those exceptions are wrapped into aJsonExceptioninstance, and then re-thrown with the original exception assigned to theThrowable.cause. ClassJsonExceptionis an unchecked / Runtime exception, while many of the cause exceptions are not. The possible cause exceptions are listed below.
Description copied from:java.lang.Constructor.newInstance(Object... args), JDK 1.8Exception JDK Explanation IllegalAccessExceptionif this Constructor object is enforcing Java language access control and the underlying constructor is inaccessible. IllegalArgumentExceptionif the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion; if this constructor pertains to an enum type. InstantiationExceptionif the class that declares the underlying constructor represents an abstract class. InvocationTargetExceptionif the underlying constructor throws an exception. ExceptionInInitializerErrorif the initialization provoked by this method fails. java.lang.NullPointerException- If'jo'or'c'are passed nullInvalidClassException- Throws if a valid, single-argument,'JsonObject'constructor is not present or is not accessible within'c'- Code:
- Exact Method Body:
Objects.requireNonNull(jo, "You have passed null to Class Parameter 'jo'"); Objects.requireNonNull(c, "You have passed null to Class Parameter 'c'"); final Constructor<T> ctor = InvalidClassException.check(c); try { return ctor.newInstance(jo); } catch (Exception e) { // *MANY* possible Exception's may be thrown, and *ALL* are checked exceptions throw new JsonException( "Unable to instantiate class: [" + c.getName() + "] using provided " + "Java-Object\n" + "See Exception.getCause() for details.", e ); }
-
getString
public static java.lang.String getString(JsonArray ja, int index, boolean throwOnNull)
Retrieve aJsonArrayelement, and transform it to ajava.lang.String.- Parameters:
ja- Any instance ofJsonArrayindex- A valid index into'ja'throwOnNull- WhenTRUEis passed to this parameter, if theJsonObjectproperty 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'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 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 aJsonString(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'java.lang.NullPointerException- If'ja'is passed null.- See Also:
JsonValue.getValueType(),JsonValue.ValueType.STRING- Code:
- Exact Method Body:
// This will also throw an IndexOutOfBoundsException if the index is out of bounds. final JsonValue jv = NPE_CHECK_JA(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 aJsonObjectproperty, and transform it to ajava.lang.String.- 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.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 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 aJsonString(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'java.lang.NullPointerException- if either'jo'or'propertyName'are null- See Also:
JsonValue.getValueType(),JsonValue.ValueType.STRING- Code:
- Exact Method Body:
final JsonValue jv = NPE_CHECK_JO(jo, propertyName); if (jv == null) { if (isOptional) return null; throw new JsonPropMissingException(jo, propertyName, STRING, String.class); } 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); }
-
getJsonObject
public static JsonObject getJsonObject(JsonArray ja, int index, boolean throwOnNull)
Extract an instance ofJsonObjectfrom an instance ofJsonArray.- Parameters:
ja- Any instance ofJsonArrayindex- A valid index into'ja'throwOnNull- WhenTRUEis passed to this parameter, if theJsonObjectproperty 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:
- The requested
JsonObject, or null (only if null's have been permitted). - Throws:
java.lang.IndexOutOfBoundsException- If'index'is out of bounds of'ja'JsonTypeArrException- If the array-element (specified by'index') does not actually contain aJsonObject(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'java.lang.NullPointerException- If'ja'is passed null- See Also:
JsonValue.getValueType(),JsonValue.ValueType.OBJECT- Code:
- Exact Method Body:
// This will also throw an IndexOutOfBoundsException if the index is out of bounds. final JsonValue jv = NPE_CHECK_JA(ja).get(index); switch (jv.getValueType()) { case NULL: if (throwOnNull) throw new JsonNullArrException(ja, index, OBJECT, JsonObject.class); else return null; case OBJECT: return (JsonObject) jv; // The JsonValue at the specified array-index does not contain a JsonObject. default: throw new JsonTypeArrException(ja, index, OBJECT, jv, JsonObject.class); }
-
getJsonObject
public static JsonObject getJsonObject(JsonObject jo, java.lang.String propertyName, boolean isOptional, boolean throwOnNull)
Extract an instance ofJsonObjectfrom an instance ofJsonObject.- Parameters:
jo- Any instance ofJsonObject.propertyName- Name of the JSON property that should be contained within'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:
- The requested
JsonObject, or null (only if null's have been permitted). - 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 aJsonObject(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'java.lang.NullPointerException- if either'jo'or'propertyName'are null- See Also:
JsonValue.getValueType(),JsonValue.ValueType.OBJECT- Code:
- Exact Method Body:
final JsonValue jv = NPE_CHECK_JO(jo, propertyName); if (jv == null) { if (isOptional) return null; throw new JsonPropMissingException(jo, propertyName, OBJECT, JsonObject.class); } switch (jv.getValueType()) { case NULL: if (throwOnNull) throw new JsonNullObjException(jo, propertyName, OBJECT, JsonObject.class); else return null; case OBJECT: return (JsonObject) jv; // The JsonValue at the specified property does not contain a JsonObject. default: throw new JsonTypeObjException(jo, propertyName, OBJECT, jv, JsonObject.class); }
-
getJsonArray
public static JsonArray getJsonArray(JsonArray ja, int index, boolean throwOnNull)
- Parameters:
ja- Any instance ofJsonArrayindex- A valid index into'ja'throwOnNull- WhenTRUEis passed to this parameter, if theJsonObjectproperty 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:
- The requested
JsonArray, or null (only if null's have been permitted). - Throws:
java.lang.IndexOutOfBoundsException- If'index'is out of bounds of'ja'JsonTypeArrException- If the array-element (specified by'index') does not actually contain aJsonArray(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'java.lang.NullPointerException- if'ja'is passed null- See Also:
JsonValue.getValueType(),JsonValue.ValueType.ARRAY- Code:
- Exact Method Body:
// This will also throw an IndexOutOfBoundsException if the index is out of bounds. final JsonValue jv = NPE_CHECK_JA(ja).get(index); switch (jv.getValueType()) { case NULL: if (throwOnNull) throw new JsonNullArrException(ja, index, ARRAY, JsonArray.class); else return null; case ARRAY: return (JsonArray) jv; // The JsonValue at the specified property does not contain a JsonArray. default: throw new JsonTypeArrException(ja, index, ARRAY, jv, JsonArray.class); }
-
getJsonArray
public static JsonArray getJsonArray(JsonObject jo, java.lang.String propertyName, boolean isOptional, boolean throwOnNull)
Extract an instance ofJsonArrayfrom an instance ofJsonObject.- Parameters:
jo- Any instance ofJsonObject.propertyName- Name of the JSON property that should be contained within'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:
- The requested
JsonArray, or null (only if null's have been permitted). - 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 aJsonArray(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'java.lang.NullPointerException- if either'jo'or'propertyName'are null- See Also:
JsonValue.getValueType(),JsonValue.ValueType.ARRAY- Code:
- Exact Method Body:
final JsonValue jv = NPE_CHECK_JO(jo, propertyName); if (jv == null) { if (isOptional) return null; throw new JsonPropMissingException(jo, propertyName, ARRAY, JsonArray.class); } switch (jv.getValueType()) { case NULL: if (throwOnNull) throw new JsonNullObjException(jo, propertyName, ARRAY, JsonArray.class); else return null; case ARRAY: return (JsonArray) jv; // The JsonValue at the specified property does not contain a JsonArray. default: throw new JsonTypeObjException(jo, propertyName, ARRAY, jv, JsonArray.class); }
-
-