Package org.forgerock.json
Class JsonValueFunctions
java.lang.Object
org.forgerock.json.JsonValueFunctions
This class contains the utility functions to convert a
JsonValue to another type.-
Method Summary
Modifier and TypeMethodDescriptionstatic Function<JsonValue,byte[], JsonValueException> Returns abyte[]by converting the JsonValue usingJsonValue.asString()and thenBase64-decoding the result.static Function<JsonValue,byte[], JsonValueException> Returns abyte[]by converting the JsonValue usingJsonValue.asString()and thenBase64url-decoding the result.static Function<JsonValue,Charset, JsonValueException> charset()Returns the JSON string value as a character set used for byte encoding/decoding.static Function<JsonValue,JsonValue, JsonValueException> deepTransformBy(Function<JsonValue, ?, JsonValueException> function) Returns the JSON value as the result of a deep JsonValue object-traversal, applying the provided transformfunctionto each element.static Function<JsonValue,Duration, JsonValueException> duration()Deprecated, for removal: This API element is subject to removal in a future version.static <T extends Enum<T>>
Function<JsonValue,T, JsonValueException> enumConstant(Class<T> type) Returns the JSON string value as an enum constant of the specified enum type.static Function<JsonValue,Instant, JsonValueException> Returns aInstantby converting the JsonValue into a number and interpreting that as the number of seconds since the UTC epoch as perInstant.ofEpochSecond(long).static Function<JsonValue,File, JsonValueException> file()Returns the JSON string value as aFileobject.static Function<JsonValue,JsonValue, JsonValueException> identity()Deprecated.useJsonValue::copydirectly insteadstatic Function<JsonValue,Instant, JsonValueException> instant()integer()Returns anIntegerby converting the JsonValue usingJsonValue.asInteger(), or by parsing the JsonValue string usingInteger.parseInt(String).static Function<JsonValue,Duration, JsonValueException> Returns the JSON string value as aDuration.Returns the JSON value as aListcontaining objects whose type (and value) is specified by a transformation function.static Function<JsonValue,Number, JsonValueException> numberInInclusiveRange(Number lower, Number upper) Returns a function that returns a number in the inclusive range oflowerandupper(ornull) from aJsonValue.static Function<JsonValue,Optional<JsonValue>, JsonValueException> optional()Return anOptionalif the given JsonValue has a value, orOptional.empty()if the JsonValue contains a null value.static <T> Function<JsonValue,Optional<T>, JsonValueException> optionalOf(Function<JsonValue, T, JsonValueException> mappingFunction) Return anOptionalof a new type if the given JsonValue has a value, orOptional.empty()if the JsonValue contains a null value.static Function<JsonValue,Boolean, NeverThrowsException> Returns aBooleanby converting the JsonValue usingJsonValue.asLong(), or by parsing the JsonValue string usingBoolean.parseBoolean(String).static Function<JsonValue,Long, NumberFormatException> Returns aLongby converting the JsonValue usingJsonValue.asLong(), or by parsing the JsonValue string usingLong.parseLong(String).static Function<JsonValue,Pattern, JsonValueException> pattern()Returns the JSON string value as a regular expression pattern.pointer()Returns the JSON string value as a JSON pointer.static Function<JsonValue,Double, JsonValueException> static Function<JsonValue,Integer, JsonValueException> static <V> Function<JsonValue,Set<V>, JsonValueException> Returns the JSON value as aSetcontaining objects whose type (and value) is specified by the parametertype.Returns the JSON value as aSetcontaining objects whose type (and value) is specified by a transformation function.static Function<JsonValue,List<String>, JsonValueException> Return a list of string values from the given JsonValue.static Function<JsonValue,URI, JsonValueException> uri()Returns the JSON string value as a uniform resource identifier.static Function<JsonValue,URL, JsonValueException> url()Returns the JSON string value as a uniform resource locator.static Function<JsonValue,UUID, JsonValueException> uuid()Returns the JSON string value as a universally unique identifier (UUID).
-
Method Details
-
charset
Returns the JSON string value as a character set used for byte encoding/decoding. If the JSON value isnull, this function returnsnull.- Returns:
- the character set represented by the string value.
- Throws:
JsonValueException- if the JSON value is not a string or the character set specified is invalid.
-
javaDuration
- Returns:
- the duration represented by the string value.
- Throws:
JsonValueException- if the JSON value is not a string or the duration specified is invalid.
-
duration
@Deprecated(since="27.0.0", forRemoval=true) public static Function<JsonValue,Duration, duration()JsonValueException> Deprecated, for removal: This API element is subject to removal in a future version.usejavaDuration()instead- Returns:
- the duration represented by the string value.
- Throws:
JsonValueException- if the JSON value is not a string or the duration specified is invalid.
-
enumConstant
public static <T extends Enum<T>> Function<JsonValue,T, enumConstantJsonValueException> (Class<T> type) Returns the JSON string value as an enum constant of the specified enum type. The string value and enum constants are compared, ignoring case considerations. If the JSON value isnull, this method returnsnull.- Type Parameters:
T- the enum type subclass.- Parameters:
type- the enum type to match constants with the value.- Returns:
- the enum constant represented by the string value.
- Throws:
IllegalArgumentException- iftypedoes not represent an enum type, or if the JSON value does not match any of the enum's constants.NullPointerException- iftypeisnull.
-
file
Returns the JSON string value as aFileobject. If the JSON value isnull, this method returnsnull.- Returns:
- a file represented by the string value.
- Throws:
JsonValueException- if the JSON value is not a string.
-
instant
Returns aFunctionto transform aJsonValuerepresenting a UTC format datetime to anInstant. The function returns null if the JSON represents a null value and will raise if JSON date is not in UTC format.- Returns:
- a function that will create an
Instantrepresentation of a JSON UTC date
-
pattern
Returns the JSON string value as a regular expression pattern. If the JSON value isnull, this method returnsnull.- Returns:
- the compiled regular expression pattern.
- Throws:
JsonValueException- if the pattern is not a string or the value is not a valid regular expression pattern.
-
pointer
Returns the JSON string value as a JSON pointer. If the JSON value isnull, this method returnsnull.- Returns:
- the JSON pointer represented by the JSON value string.
- Throws:
JsonValueException- if the JSON value is not a string or valid JSON pointer.
-
uri
Returns the JSON string value as a uniform resource identifier. If the JSON value isnull, this method returnsnull.- Returns:
- the URI represented by the string value.
- Throws:
JsonValueException- if the given string violates URI syntax.
-
url
Returns the JSON string value as a uniform resource locator. If the JSON value isnull, this method returnsnull.- Returns:
- the URL represented by the string value.
- Throws:
JsonValueException- if the given string violates URL syntax.
-
uuid
Returns the JSON string value as a universally unique identifier (UUID). If the JSON value isnull, this method returnsnull.- Returns:
- the UUID represented by the JSON value string.
- Throws:
JsonValueException- if the JSON value is not a string or valid UUID.
-
listOf
public static <V,E extends Exception> Function<JsonValue,List<V>, listOfE> (Function<JsonValue, V, E> transformFunction) Returns the JSON value as aListcontaining objects whose type (and value) is specified by a transformation function. If the value isnull, this method returnsnull. It is up to the transformation function to transform/enforce source types of the elements in the Json source collection. If any of the elements of the list are not of the appropriate type, or the type-transformation cannot occur, the exception specified by the transformation function is thrown.- Type Parameters:
V- the type of elements in this listE- the type of exception thrown by the transformation function- Parameters:
transformFunction- aFunctionto transform an element of the JsonValue list to the desired type- Returns:
- the list value, or
nullif no value. - Throws:
NullPointerException- iftransformFunctionisnull.
-
setOf
public static <V,E extends Exception> Function<JsonValue,Set<V>, setOfE> (Function<JsonValue, V, E> transformFunction) Returns the JSON value as aSetcontaining objects whose type (and value) is specified by a transformation function. If the value isnull, this method returnsnull. It is up to the transformation function to transform/enforce source types of the elements in the Json source collection. If called on an object which wraps a List, this method will drop duplicates performing element comparisons using equals/hashCode. If any of the elements of the collection are not of the appropriate type, or the type-transformation cannot occur, the exception specified by the transformation function is thrown.- Type Parameters:
V- the type of elements in this setE- the type of exception thrown by the transformation function- Parameters:
transformFunction- aFunctionto transform an element of the JsonValue set to the desired type- Returns:
- the set value, or
nullif no value. - Throws:
NullPointerException- iftransformFunctionisnull.
-
setOf
Returns the JSON value as aSetcontaining objects whose type (and value) is specified by the parametertype. If the value isnull, this method returnsnull. If called on an object which wraps a List, this method will drop duplicates performing element comparisons using equals/hashCode. If any of the elements of the collection are not of the appropriate type, or the type-transformation cannot occur,JsonValueExceptionis thrown.- Type Parameters:
V- the type of elements in this set- Parameters:
type- aClassthat specifies the desired type of each element in the resultant JsonValue set- Returns:
- the set value, or
nullif no value. - Throws:
NullPointerException- iftypeisnull.JsonValueException- if the elements of the collection cannot be cast astype.
-
deepTransformBy
public static Function<JsonValue,JsonValue, deepTransformByJsonValueException> (Function<JsonValue, ?, JsonValueException> function) Returns the JSON value as the result of a deep JsonValue object-traversal, applying the provided transformfunctionto each element.- Parameters:
function- aFunctionthat applies the desired element transformation in the resultant JsonValue set- Returns:
- the transformed JsonValue
- Throws:
JsonValueException- if the elements of the JsonValue cannot be transformed byfunction.
-
identity
Deprecated.useJsonValue::copydirectly insteadReturns an identity function that will copy the inputJsonValue.- Returns:
- an identity function that will copy the input
JsonValue. - Throws:
JsonValueException- if an error occurred while copying the input.
-
integer
Returns anIntegerby converting the JsonValue usingJsonValue.asInteger(), or by parsing the JsonValue string usingInteger.parseInt(String).- Returns:
- an integer
- Throws:
NumberFormatException- if unable to parse the JsonValue as anInteger
-
parseLong
Returns aLongby converting the JsonValue usingJsonValue.asLong(), or by parsing the JsonValue string usingLong.parseLong(String).- Returns:
- a
Long - Throws:
NumberFormatException- if unable to parse the JsonValue as anLong
-
parseBoolean
Returns aBooleanby converting the JsonValue usingJsonValue.asLong(), or by parsing the JsonValue string usingBoolean.parseBoolean(String).- Returns:
- a
Boolean
-
base64urlBinary
Returns abyte[]by converting the JsonValue usingJsonValue.asString()and thenBase64url-decoding the result.- Returns:
- the base64url-decoded data, or
nullif the JsonValue is null.
-
base64Binary
Returns abyte[]by converting the JsonValue usingJsonValue.asString()and thenBase64-decoding the result.- Returns:
- the base64-decoded data, or
nullif the JsonValue is null.
-
epochSecondsInstant
Returns aInstantby converting the JsonValue into a number and interpreting that as the number of seconds since the UTC epoch as perInstant.ofEpochSecond(long).- Returns:
- the decoded
Instant, ornullif the JsonValue is null.
-
stringOrListOfStrings
Return a list of string values from the given JsonValue. If the JsonValue is a list of strings then they are returned, otherwise the JsonValue is assumed to be a string and returned as a list of one element. In either case, the returned list is immutable.- Returns:
- the list of strings from the given JsonValue, or
nullif the JsonValue is null.
-
optional
Return anOptionalif the given JsonValue has a value, orOptional.empty()if the JsonValue contains a null value.- Returns:
- an Optional of the JsonValue
-
optionalOf
public static <T> Function<JsonValue,Optional<T>, optionalOfJsonValueException> (Function<JsonValue, T, JsonValueException> mappingFunction) Return anOptionalof a new type if the given JsonValue has a value, orOptional.empty()if the JsonValue contains a null value.- Type Parameters:
T- the expected result type- Parameters:
mappingFunction- a mapping function to convert the object in the JsonValue to a new type- Returns:
- an Optional of the given
mappingFunctionvalue
-
positiveDouble
- Returns:
- a function that returns a positive
Double(ornull)
-
positiveInteger
- Returns:
- a function that returns a positive
Integer(ornull)
-
numberInInclusiveRange
public static Function<JsonValue,Number, numberInInclusiveRangeJsonValueException> (Number lower, Number upper) Returns a function that returns a number in the inclusive range oflowerandupper(ornull) from aJsonValue.- Parameters:
lower- the lower boundupper- the upper bound- Returns:
- A function that returns a number in the inclusive range of
lowerandupper(ornull)
-
javaDuration()instead