Class JsonValueFunctions

java.lang.Object
org.forgerock.json.JsonValueFunctions

public final class JsonValueFunctions extends Object
This class contains the utility functions to convert a JsonValue to another type.
  • Method Details

    • charset

      public static Function<JsonValue,Charset,JsonValueException> charset()
      Returns the JSON string value as a character set used for byte encoding/decoding. If the JSON value is null, this function returns null.
      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.
    • duration

      public static Function<JsonValue,Duration,JsonValueException> duration()
      Returns the JSON string value as a Duration. If the JSON value is null, this method returns null.
      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,JsonValueException> enumConstant(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 is null, this method returns null.
      Type Parameters:
      T - the enum type sub-class.
      Parameters:
      type - the enum type to match constants with the value.
      Returns:
      the enum constant represented by the string value.
      Throws:
      IllegalArgumentException - if type does not represent an enum type. or if the JSON value does not match any of the enum's constants.
      NullPointerException - if type is null.
    • file

      public static Function<JsonValue,File,JsonValueException> file()
      Returns the JSON string value as a File object. If the JSON value is null, this method returns null.
      Returns:
      a file represented by the string value.
      Throws:
      JsonValueException - if the JSON value is not a string.
    • instant

      public static Function<JsonValue,Instant,JsonValueException> instant()
      Returns a Function to transform a JsonValue representing a UTC format datetime to an Instant. 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 Instant representation of a JSON UTC date
    • pattern

      public static Function<JsonValue,Pattern,JsonValueException> pattern()
      Returns the JSON string value as a regular expression pattern. If the JSON value is null, this method returns null.
      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 is null, this method returns null.
      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

      public static Function<JsonValue,URI,JsonValueException> uri()
      Returns the JSON string value as a uniform resource identifier. If the JSON value is null, this method returns null.
      Returns:
      the URI represented by the string value.
      Throws:
      JsonValueException - if the given string violates URI syntax.
    • url

      public static Function<JsonValue,URL,JsonValueException> url()
      Returns the JSON string value as a uniform resource locator. If the JSON value is null, this method returns null.
      Returns:
      the URL represented by the string value.
      Throws:
      JsonValueException - if the given string violates URL syntax.
    • uuid

      public static Function<JsonValue,UUID,JsonValueException> uuid()
      Returns the JSON string value as a universally unique identifier (UUID). If the JSON value is null, this method returns null.
      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>,E> listOf(Function<JsonValue,V,E> transformFunction)
      Returns the JSON value as a List containing objects whose type (and value) is specified by a transformation function. If the value is null, this method returns null. 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 list
      E - the type of exception thrown by the transformation function
      Parameters:
      transformFunction - a Function to transform an element of the JsonValue list to the desired type
      Returns:
      the list value, or null if no value.
      Throws:
      NullPointerException - if transformFunction is null.
    • setOf

      public static <V, E extends Exception> Function<JsonValue,Set<V>,E> setOf(Function<JsonValue,V,E> transformFunction)
      Returns the JSON value as a Set containing objects whose type (and value) is specified by a transformation function. If the value is null, this method returns null. 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 set
      E - the type of exception thrown by the transformation function
      Parameters:
      transformFunction - a Function to transform an element of the JsonValue set to the desired type
      Returns:
      the set value, or null if no value.
      Throws:
      NullPointerException - if transformFunction is null.
    • setOf

      public static <V> Function<JsonValue,Set<V>,JsonValueException> setOf(Class<V> type)
      Returns the JSON value as a Set containing objects whose type (and value) is specified by the parameter type. If the value is null, this method returns null. 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, JsonValueException is thrown.
      Type Parameters:
      V - the type of elements in this set
      Parameters:
      type - a Class that specifies the desired type of each element in the resultant JsonValue set
      Returns:
      the set value, or null if no value.
      Throws:
      NullPointerException - if type is null.
      JsonValueException - if the elements of the collection cannot be cast as type.
    • deepTransformBy

      public 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 transform function to each element.
      Parameters:
      function - a Function that 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 by function.
    • identity

      Deprecated.
      use JsonValue::copy directly instead
      Returns an identity function that will copy the input JsonValue.
      Returns:
      an identity function that will copy the input JsonValue.
      Throws:
      JsonValueException - if an error occurred while copying the input.
    • integer

      public static Function<JsonValue,Integer,NumberFormatException> integer()
      Returns an Integer by converting the JsonValue using JsonValue.asInteger(), or by parsing the JsonValue string using Integer.parseInt(String).
      Returns:
      an integer
      Throws:
      NumberFormatException - if unable to parse the JsonValue as an Integer
    • parseLong

      public static Function<JsonValue,Long,NumberFormatException> parseLong()
      Returns a Long by converting the JsonValue using JsonValue.asLong(), or by parsing the JsonValue string using Long.parseLong(String).
      Returns:
      a Long
      Throws:
      NumberFormatException - if unable to parse the JsonValue as an Long
    • parseBoolean

      public static Function<JsonValue,Boolean,NeverThrowsException> parseBoolean()
      Returns a Boolean by converting the JsonValue using JsonValue.asLong(), or by parsing the JsonValue string using Boolean.parseBoolean(String).
      Returns:
      a Boolean
    • base64urlBinary

      public static Function<JsonValue,byte[],JsonValueException> base64urlBinary()
      Returns a byte[] by converting the JsonValue using JsonValue.asString() and then Base64url-decoding the result.
      Returns:
      the base64url-decoded data, or null if the JsonValue is null.
    • base64Binary

      public static Function<JsonValue,byte[],JsonValueException> base64Binary()
      Returns a byte[] by converting the JsonValue using JsonValue.asString() and then Base64-decoding the result.
      Returns:
      the base64-decoded data, or null if the JsonValue is null.
    • epochSecondsInstant

      public static Function<JsonValue,Instant,JsonValueException> epochSecondsInstant()
      Returns a Instant by converting the JsonValue into a number and interpreting that as the number of seconds since the UTC epoch as per Instant.ofEpochSecond(long).
      Returns:
      the decoded Instant, or null if the JsonValue is null.
    • stringOrListOfStrings

      public static Function<JsonValue,List<String>,JsonValueException> 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 null if the JsonValue is null.
    • optional

      Return an Optional if the given JsonValue has a value, or Optional.empty() if the JsonValue contains a null value.
      Returns:
      an Optional of the JsonValue
    • optionalOf

      public static <T> Function<JsonValue,Optional<T>,JsonValueException> optionalOf(Function<JsonValue,T,JsonValueException> mappingFunction)
      Return an Optional of a new type if the given JsonValue has a value, or Optional.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 mappingFunction value