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> JSONNodeconvertToJSON(T source)Convert an object to a JSONNodeabstract JSONArrayconvertToJSONArray(List<?> source)Convert a list to a JSONArrayabstract JSONArrayconvertToJSONArray(List<?> source, Class<?> targetClass)Convert a List to a JSONArrayabstract JSONArrayconvertToJSONArray(List<?> source, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass)Convert a List to a JSONArrayabstract JSONArrayconvertToJSONArray(List<?> source, ValueConverter<?> valueConverter)Convert a List to a JSONArrayabstract JSONObjectconvertToJSONObject(Map<String,?> source)Convert a Map to a JSONObject.abstract JSONObjectconvertToJSONObject(Map<String,?> source, Class<?> targetClass)Convert a Map to a JSONObject.abstract JSONObjectconvertToJSONObject(Map<String,?> source, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass)Convert a Map to JSONObject.abstract JSONObjectconvertToJSONObject(Map<String,?> source, ValueConverter<?> converter)Convert a Map to a JSONObject.abstract <T> JSONObjectconvertToJSONObject(T source)Convert to a JSONObjectabstract JSONValue<?>convertValue(Object value, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass)Return a JSONValuestatic ClassConvertergetClassConverter(ConverterOption<?>... option)Returns the ClassConverter implementation.Methods inherited from class org.ghotibeaun.json.converters.AbstractConverter
copyOptions, getConverterOptionsMethods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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_CLASSproperty- Parameters:
option- Any converter options to set on the Converter- Returns:
- the ClassConverter instance
-
convertToJSON
Description copied from interface:ClassConverterConvert an object to a JSONNode- Specified by:
convertToJSONin 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:ClassConverterConvert to a JSONObject- Specified by:
convertToJSONObjectin 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:ClassConverterConvert a list to a JSONArray- Specified by:
convertToJSONArrayin 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:ClassConverterConvert a List to a JSONArray- Specified by:
convertToJSONArrayin 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:ClassConverterConvert a List to a JSONArray- Specified by:
convertToJSONArrayin 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:ClassConverterConvert a List to a JSONArray- Specified by:
convertToJSONArrayin interfaceClassConverter- Parameters:
source- the ListvalueConverter- an optional ValueConverter. If set, the targetClass parameter must be empty, or aJSONConversionExceptionwill be throwntargetClass- an optionaltargetClass. If set, the ValueConverter parameter must be empty, or aJSONConversionExceptionwill 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:ClassConverterReturn a JSONValue- Specified by:
convertValuein interfaceClassConverter- Parameters:
value- the value to convertvalueConverter- an optional ValueConverter. If set, the targetClass parameter must be empty, or aJSONConversionExceptionwill be throwntargetClass- an optionaltargetClass. If set, the ValueConverter parameter must be empty, or aJSONConversionExceptionwill 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:ClassConverterConvert a Map to a JSONObject. Map keys must be Strings.- Specified by:
convertToJSONObjectin 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:ClassConverterConvert 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:
convertToJSONObjectin 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:ClassConverterConvert a Map to a JSONObject. Map keys must be Strings. Map values are converted to instances of the targetClass- Specified by:
convertToJSONObjectin 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:ClassConverterConvert a Map to JSONObject. Optionally uses aValueConverterortargetClassto set the values of each entry. Note: You can use either a ValueConverter OR targetClass, but not both. Otherwise, aJSONConversionExceptionwill be thrown- Specified by:
convertToJSONObjectin 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
-