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 SummaryConstructors Constructor Description AbstractClassConverter(ConverterOption<?>... option)Constructor
- 
Method SummaryModifier 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.AbstractConvertercopyOptions, getConverterOptionsMethods inherited from class java.lang.Objectequals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.ghotibeaun.json.converters.ConvertergetConverterOptions
- 
Constructor Details- 
AbstractClassConverterConstructor
 
- 
- 
Method Details- 
getClassConverterReturns 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
 
- 
convertToJSONDescription copied from interface:ClassConverterConvert an object to a JSONNode- Specified by:
- convertToJSONin interface- ClassConverter
- 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
 
- 
convertToJSONObjectDescription copied from interface:ClassConverterConvert to a JSONObject- Specified by:
- convertToJSONObjectin interface- ClassConverter
- 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
 
- 
convertToJSONArrayDescription copied from interface:ClassConverterConvert a list to a JSONArray- Specified by:
- convertToJSONArrayin interface- ClassConverter
- Parameters:
- source- the List to convert
- Returns:
- A JSONArray
- Throws:
- JSONConversionException- thrown if a conversion error occurs
 
- 
convertToJSONArraypublic abstract JSONArray convertToJSONArray(List<?> source, Class<?> targetClass) throws JSONConversionExceptionDescription copied from interface:ClassConverterConvert a List to a JSONArray- Specified by:
- convertToJSONArrayin interface- ClassConverter
- Parameters:
- source- the List
- targetClass- the targetClass to cast each item
- Returns:
- a JSONArray
- Throws:
- JSONConversionException- thrown if a conversion error occurs
 
- 
convertToJSONArraypublic abstract JSONArray convertToJSONArray(List<?> source, ValueConverter<?> valueConverter) throws JSONConversionExceptionDescription copied from interface:ClassConverterConvert a List to a JSONArray- Specified by:
- convertToJSONArrayin interface- ClassConverter
- Parameters:
- source- the List
- valueConverter- the ValueConverter to apply to each item
- Returns:
- a JSONArray
- Throws:
- JSONConversionException- thrown if a conversion error occurs
 
- 
convertToJSONArraypublic 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 interface- ClassConverter
- Parameters:
- source- the List
- valueConverter- an optional ValueConverter. If set, the targetClass parameter must be empty, or a- JSONConversionExceptionwill be thrown
- targetClass- an optional- targetClass. If set, the ValueConverter parameter must be empty, or a- JSONConversionExceptionwill be thrown
- Returns:
- a JSONArray
- Throws:
- JSONConversionException- thrown if a conversion error occurs
 
- 
convertValuepublic abstract JSONValue<?> convertValue(Object value, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass) throws JSONConversionExceptionDescription copied from interface:ClassConverterReturn a JSONValue- Specified by:
- convertValuein interface- ClassConverter
- Parameters:
- value- the value to convert
- valueConverter- an optional ValueConverter. If set, the targetClass parameter must be empty, or a- JSONConversionExceptionwill be thrown
- targetClass- an optional- targetClass. If set, the ValueConverter parameter must be empty, or a- JSONConversionExceptionwill be thrown
- Returns:
- a JSONValue
- Throws:
- JSONConversionException- thrown if a conversion error occurs
 
- 
convertToJSONObjectpublic 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 interface- ClassConverter
- 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
 
- 
convertToJSONObjectpublic 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 interface- ClassConverter
- 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
 
- 
convertToJSONObjectpublic 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 interface- ClassConverter
- 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
 
- 
convertToJSONObjectpublic 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 interface- ClassConverter
- 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
 
 
-