Package org.ghotibeaun.json.converters
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 Summary
Modifier and Type Method Description <T> JSONNode
convertToJSON(T source)
Convert an object to a JSONNodeJSONArray
convertToJSONArray(List<?> source)
Convert a list to a JSONArrayJSONArray
convertToJSONArray(List<?> source, Class<?> targetClass)
Convert a List to a JSONArrayJSONArray
convertToJSONArray(List<?> source, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass)
Convert a List to a JSONArrayJSONArray
convertToJSONArray(List<?> source, ValueConverter<?> valueConverter)
Convert a List to a JSONArrayJSONObject
convertToJSONObject(Map<String,?> source)
Convert a Map to a JSONObject.JSONObject
convertToJSONObject(Map<String,?> source, Class<?> targetClass)
Convert a Map to a JSONObject.JSONObject
convertToJSONObject(Map<String,?> source, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass)
Convert a Map to JSONObject.JSONObject
convertToJSONObject(Map<String,?> source, ValueConverter<?> converter)
Convert a Map to a JSONObject.<T> JSONObject
convertToJSONObject(T source)
Convert to a JSONObjectJSONValue<?>
convertValue(Object value, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass)
Return a JSONValueMethods inherited from interface org.ghotibeaun.json.converters.Converter
getConverterOptions
-
Method Details
-
convertToJSON
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()
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
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
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 JSONConversionExceptionConvert 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 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
JSONObject convertToJSONObject(Map<String,?> source, Class<?> targetClass) throws JSONConversionExceptionConvert a Map to a JSONObject. Map keys must be Strings. Map values are converted to instances of the targetClass- 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
JSONObject convertToJSONObject(Map<String,?> source, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass) throws JSONConversionExceptionConvert 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- 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
-
convertToJSONArray
Convert a list to a JSONArray- Parameters:
source
- the List to convert- Returns:
- A JSONArray
- Throws:
JSONConversionException
- thrown if a conversion error occurs
-
convertToJSONArray
Convert a List to a JSONArray- Parameters:
source
- the ListtargetClass
- 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 JSONConversionExceptionConvert a List to a JSONArray- Parameters:
source
- the ListvalueConverter
- 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 JSONConversionExceptionConvert a List to a JSONArray- 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
JSONValue<?> convertValue(Object value, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass) throws JSONConversionExceptionReturn a JSONValue- 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
-