Interface ClassConverter

All Superinterfaces:
Converter
All Known Implementing Classes:
AbstractClassConverter

public interface ClassConverter
extends Converter
Converts Java class instances to JSON
Author:
Jim Earley (xml.jim@gmail.com)
  • Method Details

    • convertToJSON

      <T> JSONNode convertToJSON​(T source) throws JSONConversionException
      Convert an object to a JSONNode
      Type Parameters:
      T - The source type
      Parameters:
      source - the object instance to convert
      Returns:
      a JSONNode. Use JSONNode.isArray() or JSONNode.isObject() to determine how to cast. The use JSONNode.asJSONArray() or JSONNode.asJSONObject() to cast and access the values of the JSONNode
      Throws:
      JSONConversionException - thrown if a conversion error occurs
    • convertToJSONObject

      <T> JSONObject convertToJSONObject​(T source) throws JSONConversionException
      Convert to a JSONObject
      Type Parameters:
      T - The source type
      Parameters:
      source - the object instance
      Returns:
      A JSONObject representing the object
      Throws:
      JSONConversionException - thrown if a conversion error occurs
    • convertToJSONObject

      JSONObject convertToJSONObject​(Map<String,​?> source) throws JSONConversionException
      Convert a Map to a JSONObject. Map keys must be Strings.
      Parameters:
      source - The Map to convert
      Returns:
      A JSONObject. The Map's keys become the JSONObject keys. Values are converted to JSONValue instances based on type
      Throws:
      JSONConversionException - thrown if a conversion error occurs
    • convertToJSONObject

      JSONObject convertToJSONObject​(Map<String,​?> source, ValueConverter<?> converter) throws JSONConversionException
      Convert a Map to a JSONObject. Map keys must be Strings. Map Entry values are passed through a ValueConverter prior to setting the value
      Parameters:
      source - The Map to convert
      converter - a ValueConverter to be applied to each entry on the map.
      Returns:
      A JSONObject. The Map's keys become the JSONObject keys. Values are converted to JSONValue instances using the specified ValueConverter
      Throws:
      JSONConversionException - thrown if a conversion error occurs
    • convertToJSONObject

      JSONObject convertToJSONObject​(Map<String,​?> source, Class<?> targetClass) throws JSONConversionException
      Convert a Map to a JSONObject. Map keys must be Strings. Map values are converted to instances of the targetClass
      Parameters:
      source - The Map to convert
      targetClass - The target class of each entry to be used for scanning and extracting values. The targetClass must be concrete and implement or inherit from the value's class instance.
      Returns:
      a JSONObject. The Map's keys become the JSONObject keys. Values are converted from instances of the targetClass
      Throws:
      JSONConversionException - thrown if a conversion error occurs
    • convertToJSONObject

      JSONObject convertToJSONObject​(Map<String,​?> source, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass) throws JSONConversionException
      Convert a Map to JSONObject. Optionally uses a ValueConverter or targetClass to set the values of each entry. Note: You can use either a ValueConverter OR targetClass, but not both. Otherwise, a JSONConversionException will be thrown
      Parameters:
      source - The map to convert
      valueConverter - The optional ValueConverter. If a ValueConverter is not being used, apply Optional.empty()
      targetClass - The optional target class. If a target class is not used, apply Optional.empty()
      Returns:
      a JSONObject The Map's keys become the JSONObject keys. Values are set based on the presence of a ValueConverter, target class, or neither.
      Throws:
      JSONConversionException - thrown if a conversion error occurs
    • convertToJSONArray

      JSONArray convertToJSONArray​(List<?> source) throws JSONConversionException
      Convert a list to a JSONArray
      Parameters:
      source - the List to convert
      Returns:
      A JSONArray
      Throws:
      JSONConversionException - thrown if a conversion error occurs
    • convertToJSONArray

      JSONArray convertToJSONArray​(List<?> source, Class<?> targetClass) throws JSONConversionException
      Convert a List to a JSONArray
      Parameters:
      source - the List
      targetClass - the targetClass to cast each item
      Returns:
      a JSONArray
      Throws:
      JSONConversionException - thrown if a conversion error occurs
    • convertToJSONArray

      JSONArray convertToJSONArray​(List<?> source, ValueConverter<?> valueConverter) throws JSONConversionException
      Convert a List to a JSONArray
      Parameters:
      source - the List
      valueConverter - the ValueConverter to apply to each item
      Returns:
      a JSONArray
      Throws:
      JSONConversionException - thrown if a conversion error occurs
    • convertToJSONArray

      JSONArray convertToJSONArray​(List<?> source, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass) throws JSONConversionException
      Convert a List to a JSONArray
      Parameters:
      source - the List
      valueConverter - an optional ValueConverter. If set, the targetClass parameter must be empty, or a JSONConversionException will be thrown
      targetClass - an optional targetClass. If set, the ValueConverter parameter must be empty, or a JSONConversionException will be thrown
      Returns:
      a JSONArray
      Throws:
      JSONConversionException - thrown if a conversion error occurs
    • convertValue

      JSONValue<?> convertValue​(Object value, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass) throws JSONConversionException
      Return a JSONValue
      Parameters:
      value - the value to convert
      valueConverter - an optional ValueConverter. If set, the targetClass parameter must be empty, or a JSONConversionException will be thrown
      targetClass - an optional targetClass. If set, the ValueConverter parameter must be empty, or a JSONConversionException will be thrown
      Returns:
      a JSONValue
      Throws:
      JSONConversionException - thrown if a conversion error occurs