Package org.ghotibeaun.json.converters
Class AbstractClassConverter
java.lang.Object
org.ghotibeaun.json.converters.AbstractConverter
org.ghotibeaun.json.converters.AbstractClassConverter
- All Implemented Interfaces:
ClassConverter
,Converter
public abstract class AbstractClassConverter extends AbstractConverter implements ClassConverter
Base class for ClassConverters
- Author:
- Jim Earley (xml.jim@gmail.com)
-
Constructor Summary
Constructors Constructor Description AbstractClassConverter(ConverterOption<?>... option)
Constructor -
Method Summary
Modifier and Type Method Description abstract <T> JSONNode
convertToJSON(T source)
Convert an object to a JSONNodeabstract JSONArray
convertToJSONArray(List<?> source)
Convert a list to a JSONArrayabstract JSONArray
convertToJSONArray(List<?> source, Class<?> targetClass)
Convert a List to a JSONArrayabstract JSONArray
convertToJSONArray(List<?> source, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass)
Convert a List to a JSONArrayabstract JSONArray
convertToJSONArray(List<?> source, ValueConverter<?> valueConverter)
Convert a List to a JSONArrayabstract JSONObject
convertToJSONObject(Map<String,?> source)
Convert a Map to a JSONObject.abstract JSONObject
convertToJSONObject(Map<String,?> source, Class<?> targetClass)
Convert a Map to a JSONObject.abstract JSONObject
convertToJSONObject(Map<String,?> source, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass)
Convert a Map to JSONObject.abstract JSONObject
convertToJSONObject(Map<String,?> source, ValueConverter<?> converter)
Convert a Map to a JSONObject.abstract <T> JSONObject
convertToJSONObject(T source)
Convert to a JSONObjectabstract JSONValue<?>
convertValue(Object value, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass)
Return a JSONValuestatic ClassConverter
getClassConverter(ConverterOption<?>... option)
Returns the ClassConverter implementation.Methods inherited from class org.ghotibeaun.json.converters.AbstractConverter
copyOptions, getConverterOptions
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.ghotibeaun.json.converters.Converter
getConverterOptions
-
Constructor Details
-
AbstractClassConverter
Constructor
-
-
Method Details
-
getClassConverter
Returns the ClassConverter implementation. The implementation class is specified using theFactorySettings#JSON_CLASS_CONVERTER_CLASS
property- Parameters:
option
- Any converter options to set on the Converter- Returns:
- the ClassConverter instance
-
convertToJSON
Description copied from interface:ClassConverter
Convert an object to a JSONNode- Specified by:
convertToJSON
in interfaceClassConverter
- Type Parameters:
T
- The source type- Parameters:
source
- the object instance to convert- Returns:
- a JSONNode. Use
JSONNode.isArray()
orJSONNode.isObject()
to determine how to cast. The useJSONNode.asJSONArray()
orJSONNode.asJSONObject()
to cast and access the values of the JSONNode - Throws:
JSONConversionException
- thrown if a conversion error occurs
-
convertToJSONObject
Description copied from interface:ClassConverter
Convert to a JSONObject- Specified by:
convertToJSONObject
in interfaceClassConverter
- 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
-
convertToJSONArray
Description copied from interface:ClassConverter
Convert a list to a JSONArray- Specified by:
convertToJSONArray
in interfaceClassConverter
- Parameters:
source
- the List to convert- Returns:
- A JSONArray
- Throws:
JSONConversionException
- thrown if a conversion error occurs
-
convertToJSONArray
public abstract JSONArray convertToJSONArray(List<?> source, Class<?> targetClass) throws JSONConversionExceptionDescription copied from interface:ClassConverter
Convert a List to a JSONArray- Specified by:
convertToJSONArray
in interfaceClassConverter
- Parameters:
source
- the ListtargetClass
- the targetClass to cast each item- Returns:
- a JSONArray
- Throws:
JSONConversionException
- thrown if a conversion error occurs
-
convertToJSONArray
public abstract JSONArray convertToJSONArray(List<?> source, ValueConverter<?> valueConverter) throws JSONConversionExceptionDescription copied from interface:ClassConverter
Convert a List to a JSONArray- Specified by:
convertToJSONArray
in interfaceClassConverter
- Parameters:
source
- the ListvalueConverter
- the ValueConverter to apply to each item- Returns:
- a JSONArray
- Throws:
JSONConversionException
- thrown if a conversion error occurs
-
convertToJSONArray
public abstract JSONArray convertToJSONArray(List<?> source, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass) throws JSONConversionExceptionDescription copied from interface:ClassConverter
Convert a List to a JSONArray- Specified by:
convertToJSONArray
in interfaceClassConverter
- Parameters:
source
- the ListvalueConverter
- an optional ValueConverter. If set, the targetClass parameter must be empty, or aJSONConversionException
will be throwntargetClass
- an optionaltargetClass
. If set, the ValueConverter parameter must be empty, or aJSONConversionException
will be thrown- Returns:
- a JSONArray
- Throws:
JSONConversionException
- thrown if a conversion error occurs
-
convertValue
public abstract JSONValue<?> convertValue(Object value, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass) throws JSONConversionExceptionDescription copied from interface:ClassConverter
Return a JSONValue- Specified by:
convertValue
in interfaceClassConverter
- Parameters:
value
- the value to convertvalueConverter
- an optional ValueConverter. If set, the targetClass parameter must be empty, or aJSONConversionException
will be throwntargetClass
- an optionaltargetClass
. If set, the ValueConverter parameter must be empty, or aJSONConversionException
will be thrown- Returns:
- a JSONValue
- Throws:
JSONConversionException
- thrown if a conversion error occurs
-
convertToJSONObject
public abstract JSONObject convertToJSONObject(Map<String,?> source) throws JSONConversionExceptionDescription copied from interface:ClassConverter
Convert a Map to a JSONObject. Map keys must be Strings.- Specified by:
convertToJSONObject
in interfaceClassConverter
- 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
public abstract JSONObject convertToJSONObject(Map<String,?> source, ValueConverter<?> converter) throws JSONConversionExceptionDescription copied from interface:ClassConverter
Convert a Map to a JSONObject. Map keys must be Strings. Map Entry values are passed through a ValueConverter prior to setting the value- Specified by:
convertToJSONObject
in interfaceClassConverter
- Parameters:
source
- The Map to convertconverter
- 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
public abstract JSONObject convertToJSONObject(Map<String,?> source, Class<?> targetClass) throws JSONConversionExceptionDescription copied from interface:ClassConverter
Convert a Map to a JSONObject. Map keys must be Strings. Map values are converted to instances of the targetClass- Specified by:
convertToJSONObject
in interfaceClassConverter
- Parameters:
source
- The Map to converttargetClass
- 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
public abstract JSONObject convertToJSONObject(Map<String,?> source, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass) throws JSONConversionExceptionDescription copied from interface:ClassConverter
Convert a Map to JSONObject. Optionally uses aValueConverter
ortargetClass
to set the values of each entry. Note: You can use either a ValueConverter OR targetClass, but not both. Otherwise, aJSONConversionException
will be thrown- Specified by:
convertToJSONObject
in interfaceClassConverter
- Parameters:
source
- The map to convertvalueConverter
- The optional ValueConverter. If a ValueConverter is not being used, applyOptional.empty()
targetClass
- The optional target class. If a target class is not used, applyOptional.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
-