Interface JsonIO<V,A extends V,O extends V,AB,OB>

Type Parameters:
V - the type for a JSON value
A - the type for a JSON array
O - the type of a JSON object
AB - the type used to build a JSON array
OB - the type used to build a JSON object

public interface JsonIO<V,A extends V,O extends V,AB,OB>
Abstraction layer around a library for reading and writing JSON. (E.g. Jakarta JSON-P or Jackson).
  • Field Details

  • Method Details

    • newInstance

      static <V, A extends V, O extends V, AB, OB> JsonIO<V,A,O,AB,OB> newInstance(OpenApiConfig config)
    • parseValue

      default Object parseValue(String value)
    • isArray

      boolean isArray(V value)
      Check whether a JSON value is an array
      Parameters:
      value - the JSON value to check
      Returns:
      true if value is a JSON array, otherwise false
    • asArray

      A asArray(V value)
      Cast a JSON value to a JSON array
      Parameters:
      value - the JSON value, which must represent a JSON array
      Returns:
      the JSON array
      Throws:
      ClassCastException - if value is not a JSON array
    • entries

      List<V> entries(A array)
      Get the list of JSON values contained by a JSON array
      Parameters:
      array - the JSON array
      Returns:
      the list of JSON values in the array
    • isObject

      boolean isObject(V value)
      Check whether a JSON value is a JSON object
      Parameters:
      value - the JSON value to check
      Returns:
      true if value is a JSON object, otherwise false
    • asObject

      O asObject(V value)
      Cast a JSON value to a JSON object
      Parameters:
      value - the JSON value, which must represent a JSON object
      Returns:
      the JSON object
      Throws:
      ClassCastException - if value is not a JSON object
    • hasKey

      boolean hasKey(O object, String key)
      Check whether a JSON object contains a particular key
      Parameters:
      object - the JSON object to check
      key - the key to look for
      Returns:
      true if object contains key key, otherwise false
    • properties

      Set<Map.Entry<String,V>> properties(O object)
      Get all the properties (keys and values) of a JSON object.

      This is a similar operation to Map.entrySet().

      Parameters:
      object - the JSON object
      Returns:
      the set of key-value pairs
    • isString

      boolean isString(V value)
      Check whether a JSON value is a JSON string.
      Parameters:
      value - the JSON value to check
      Returns:
      true if value is a string, otherwise false
    • asString

      String asString(V value)
      Convert a JSON string, number or boolean to a String.
      Parameters:
      value - the JSON value, must be a string
      Returns:
      the String value, or null if the JSON value does not represent a string, number or boolean
    • isBoolean

      boolean isBoolean(V value)
      Check whether a JSON value is a JSON boolean.
      Parameters:
      value - the JSON value to check
      Returns:
      true if value is a boolean, otherwise false
    • asBoolean

      Boolean asBoolean(V value)
      Convert a JSON boolean to a Boolean.
      Parameters:
      value - the JSON value, must be a boolean
      Returns:
      the Boolean value, or null if the JSON value does not represent a boolean
    • isNull

      boolean isNull(V value)
      Check whether a JSON value is a JSON null.
      Parameters:
      value - the JSON value to check
      Returns:
      true if value is null, otherwise false
    • getString

      default String getString(V object, String key)
      Get a property from a JSON object as a string. The same conversions are performed as asString(Object).
      Parameters:
      object - the JSON object
      key - the property key
      Returns:
      the property value, or null if object is not a JSON object, the property is not present, or the property value is not a string, number or boolean
    • getInt

      default Integer getInt(V object, String key)
      Get an integer property from a JSON object
      Parameters:
      object - the JSON object
      key - the property key
      Returns:
      the property value, or null if object is not a JSON object, the property is not present, or the property value is not an integer
    • getBoolean

      default Boolean getBoolean(V object, String key)
      Get an boolean property from a JSON object
      Parameters:
      object - the JSON object
      key - the property key
      Returns:
      the property value, or null if object is not a JSON object, the property is not present, or the property value is not a boolean
    • getBigDecimal

      default BigDecimal getBigDecimal(V object, String key)
      Get an number property from a JSON object
      Parameters:
      object - the JSON object
      key - the property key
      Returns:
      the property value, or null if object is not a JSON object, the property is not present, or the property value is not a number
    • getJsonInt

      Integer getJsonInt(O object, String key)
      Get an integer property from a JSON object
      Parameters:
      object - the JSON object
      key - the property key
      Returns:
      the property value, or null if the property is not present or is not an integer
    • getJsonString

      String getJsonString(O object, String key)
      Get a property from a JSON object as a string. The same conversions are performed as asString(Object).
      Parameters:
      object - the JSON object
      key - the property key
      Returns:
      the property value, or null if the property is not present or is not a string, number or boolean
    • getJsonBoolean

      Boolean getJsonBoolean(O object, String key)
      Get an boolean property from a JSON object
      Parameters:
      object - the JSON object
      key - the property key
      Returns:
      the property value, or null if the property is not present or is not a boolean
    • getJsonBigDecimal

      BigDecimal getJsonBigDecimal(O object, String key)
      Get an number property from a JSON object
      Parameters:
      object - the JSON object
      key - the property key
      Returns:
      the property value, or null if the property is not present or is not a number
    • getValue

      V getValue(O object, String key)
      Get a property from a JSON object
      Parameters:
      object - the JSON object
      key - the property key
      Returns:
      the property value, or null if the property is not present
    • getArray

      Optional<A> getArray(O object, String key)
      Get an JSON array property from a JSON object
      Parameters:
      object - the JSON object
      key - the property key
      Returns:
      an Optional containing the property value, or an empty Optional if the property is not present, or the property is not an array
    • getArray

      default <T> Optional<List<T>> getArray(O object, String key, Function<V,T> valueMapper)
      Get an JSON array property from a JSON object and convert it into a list of Java objects
      Type Parameters:
      T - the type that each value in the JSON array should be converted into
      Parameters:
      object - the JSON object
      key - the property key
      valueMapper - a function to convert a value within the JSON array into a Java object
      Returns:
      an Optional containing the list of Java objects, or an empty Optional if the property is not present, or the property is not an array
    • getObject

      Optional<O> getObject(O object, String key)
      Get an JSON object property from a JSON object
      Parameters:
      object - the JSON object
      key - the property key
      Returns:
      an Optional containing the property value, or an empty Optional if the property is not present, or the property is not an object
    • toJson

      default Optional<V> toJson(Object object)
      Convert a Java object to JSON. See toJson(Object, Object, PropertyMapper) for the list of supported types
      Parameters:
      object - the JSON object
      Returns:
      an Optional containing the JSON value, or an empty Optional if object is not one of the supported types
    • toJson

      default Optional<V> toJson(Object object, JsonIO.PropertyMapper<V,OB> handler)
    • fromJson

      Object fromJson(V object)
      Convert a JSON value into a Java object.
      Parameters:
      object - the JSON value
      Returns:
      the Java object, which may be null if object is null or represents a JSON null value
    • fromJson

      <T> T fromJson(V object, Class<T> desiredType)
      Convert a basic JSON value into a Java object of the desired type. This method cannot be used to convert JSON arrays or objects.

      The supported values for desiredType are:

      • String
      • Integer
      • BigInteger
      • Long
      • BigDecimal
      • Boolean
      Type Parameters:
      T - the desired Java type
      Parameters:
      object - the JSON object
      desiredType - the desired Java type
      Returns:
      the JSON object, or null if object cannot be converted to the desired type
    • toJson

      V toJson(Object object, V defaultValue, JsonIO.PropertyMapper<V,OB> propertyMapper)
      Convert a Java object to JSON.

      The following types are supported:

      • V (the JSON value type)
      • String
      • BigDecimal
      • BigInteger
      • Boolean
      • Double
      • Float
      • Short
      • Integer
      • Long
      • Character
      • Enum
      • List where each item is a supported type
      • Map where each key is a String and each value is a supported type
      Parameters:
      object - the JSON object
      defaultValue - the default value to return if value cannot be converted to JSON
      propertyMapper - mapper object to alter the default mapping of the object and its properties to JSON
      Returns:
      the JSON value, or defaultValue if value cannot be converted to JSON
    • fromString

      default V fromString(String value, Format format)
      Read a JSON or YAML document from a String
      Parameters:
      value - the JSON or YAML document
      format - the format
      Returns:
      the root JSON value from the document
    • fromStream

      default V fromStream(InputStream stream, Format format)
      Read a JSON or YAML document from an InputStream. The stream is read using the default charset.
      Parameters:
      stream - the input stream to read the JSON or YAML document from
      format - the format
      Returns:
      the root JSON value from the document
    • fromReader

      default V fromReader(Reader reader) throws IOException
      Read a YAML document from a Reader.
      Parameters:
      reader - the reader to read the YAML document from
      Returns:
      the root JSON value from the document
      Throws:
      IOException
    • fromReader

      V fromReader(Reader reader, Format format) throws IOException
      Read a JSON or YAML document from a Reader.
      Parameters:
      reader - the reader to read the JSON or YAML document from
      format - the format
      Returns:
      the root JSON value from the document
      Throws:
      IOException
    • toString

      String toString(V object, Format format)
      Serialize a JSON value to a JSON or YAML string
      Parameters:
      object - the JSON value
      format - the desired format
      Returns:
      the serialization of object
    • createArray

      AB createArray()
      Create a JSON array builder which can be used to build a JSON array. Example:
      
       AB builder = jsonIO().createArray();
       jsonIO().add(builder, value1);
       jsonIO().add(builder, value2);
       A array = jsonIO().buildArray(builder);
       
      Returns:
      the JSON array builder
    • add

      void add(AB array, V value)
      Add a JSON value to a JSON array builder.
      Parameters:
      array - the array builder
      value - the value to add
      See Also:
    • buildArray

      A buildArray(AB array)
      Convert a JSON array builder into a JSON array.
      Parameters:
      array - the JSON array builder
      Returns:
      the JSON array
      See Also:
    • createObject

      OB createObject()
      Create a JSON object builder which can be used to build a JSON object. Example:
      
       OB builder = jsonIO().createObject();
       jsonIO().set(builder, "key1", value1);
       jsonIO().set(builder, "key2", value2);
       O object = jsonIO().buildObject(builder);
       
      Returns:
      the JSON object builder
    • set

      void set(OB object, String key, V value)
      Set a property on a JSON object builder
      Parameters:
      object - the JSON object builder
      key - the property key
      value - the property value
      See Also:
    • setAll

      void setAll(OB object, O valueSource)
      Copy all properties from a JSON object to a JSON object builder.
      Parameters:
      object - the JSON object builder
      valueSource - the JSON object
      See Also:
    • buildObject

      O buildObject(OB object)
      Convert a JSON object builder into a JSON object.
      Parameters:
      object - the JSON object builder
      Returns:
      the JSON object
      See Also:
    • nullValue

      V nullValue()
      Returns the JSON value representing null.