Package org.ghotibeaun.json.converters
Class Converters
java.lang.Object
org.ghotibeaun.json.converters.Converters
public final class Converters extends Object
Utility entry point to access converter methods statically.
- Author:
- Jim Earley (jim.earley@fdiinc.com)
-
Method Summary
Modifier and Type Method Description static <T> TconvertToClass(Class<T> targetClass, JSONObject jsonObject, ConverterOption<?>... options)Convert a JSONObject into a Class instancestatic <T> JSONNodeconvertToJSON(T source, ConverterOption<?>... options)Convert an object to a JSONNodestatic JSONArrayconvertToJSONArray(List<?> source, Class<?> targetClass, ConverterOption<?>... options)static JSONArrayconvertToJSONArray(List<?> source, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass, ConverterOption<?>... options)static JSONArrayconvertToJSONArray(List<?> source, ConverterOption<?>... options)static JSONArrayconvertToJSONArray(List<?> source, ValueConverter<?> valueConverter, ConverterOption<?>... options)static JSONObjectconvertToJSONObject(Map<String,?> source, Class<?> targetClass, ConverterOption<?>... options)Convert a Map to a JSONObject.static JSONObjectconvertToJSONObject(Map<String,?> source, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass, ConverterOption<?>... options)Convert a Map to a JSONObject.static JSONObjectconvertToJSONObject(Map<String,?> source, ConverterOption<?>... options)Convert a Map to a JSONObject.static JSONObjectconvertToJSONObject(Map<String,?> source, ValueConverter<?> converter, ConverterOption<?>... options)Convert a Map to a JSONObject.static <T> JSONObjectconvertToJSONObject(T source, ConverterOption<?>... options)Convert to a JSONObjectstatic JSONValue<?>convertToJSONValue(Object value, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass, ConverterOption<?>... options)static <T> List<T>convertToList(Class<T> targetClass, JSONArray jsonArray, Optional<ValueConverter<?>> valueConverter, ConverterOption<?>... options)Deprecated.static <T> List<T>convertToList(JSONArray array, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass, ConverterOption<?>... options)static <T> TconvertValue(JSONValue<?> value, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass, ConverterOption<?>... options)
-
Method Details
-
convertToClass
public static <T> T convertToClass(Class<T> targetClass, JSONObject jsonObject, ConverterOption<?>... options) throws JSONConversionExceptionConvert a JSONObject into a Class instance- Type Parameters:
T- the Class type.- Parameters:
targetClass- the concrete class to instantiate. It must implement or inherit from the Class type.json- The JSONObject instance containing the data to initialize into the class instanceoptions- specifies JSON Conversion options to apply. Experimental - not fully implemented- Returns:
- the specified Class instance
- Throws:
JSONConversionException- thrown when an error occurs during the conversion- See Also:
JSONConverter.convertToClass(Class, JSONObject)
-
convertToList
@Deprecated public static <T> List<T> convertToList(Class<T> targetClass, JSONArray jsonArray, Optional<ValueConverter<?>> valueConverter, ConverterOption<?>... options) throws JSONConversionExceptionDeprecated.Convert a JSONArray into a List of values- Type Parameters:
T- The Class Type.- Parameters:
targetItemClass- The concrete class for each item in the list.array- the JSONArray instance containing the data to initialize the listoptions- specifies JSON Conversion options to apply. Experimental - not fully implemented- Returns:
- a List of values
- Throws:
JSONConversionException- thrown when an error occurs during conversion- See Also:
JSONConverter.convertToList(Class, JSONArray, Optional)
-
convertToList
public static <T> List<T> convertToList(JSONArray array, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass, ConverterOption<?>... options) throws JSONConversionException- Throws:
JSONConversionException
-
convertValue
public static <T> T convertValue(JSONValue<?> value, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass, ConverterOption<?>... options) throws JSONConversionException- Throws:
JSONConversionException
-
convertToJSON
public static <T> JSONNode convertToJSON(T source, ConverterOption<?>... options) throws JSONConversionExceptionConvert an object to a JSONNode- Type Parameters:
T- The source type- Parameters:
source- the object instance to convertoptions- specifies JSON Conversion options to apply. Experimental - not fully implemented- 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- See Also:
ClassConverter.convertToJSON(Object)
-
convertToJSONObject
public static <T> JSONObject convertToJSONObject(T source, ConverterOption<?>... options) throws JSONConversionExceptionConvert to a JSONObject- Type Parameters:
T- The source type- Parameters:
source- the object instanceoptions- specifies JSON Conversion options to apply. Experimental - not fully implemented- Returns:
- A JSONObject representing the object
- Throws:
JSONConversionException- thrown if a conversion error occurs- See Also:
ClassConverter.convertToJSONObject(Object)
-
convertToJSONObject
public static JSONObject convertToJSONObject(Map<String,?> source, ConverterOption<?>... options) throws JSONConversionExceptionConvert a Map to a JSONObject. Map keys must be Strings.- Parameters:
source- The Map to convertoptions- specifies JSON Conversion options to apply. Experimental - not fully implemented- 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- See Also:
ClassConverter.convertToJSONObject(Map)
-
convertToJSONObject
public static JSONObject convertToJSONObject(Map<String,?> source, ValueConverter<?> converter, ConverterOption<?>... options) throws JSONConversionExceptionConvert a Map to a JSONObject. Map keys must be Strings.- Parameters:
source- The Map to convertconverter- the ValueConverter that will be applied to each element in the mapoptions- specifies JSON Conversion options to apply. Experimental - not fully implemented- 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- See Also:
ClassConverter.convertToJSONObject(Map, ValueConverter)
-
convertToJSONObject
public static JSONObject convertToJSONObject(Map<String,?> source, Class<?> targetClass, ConverterOption<?>... options) throws JSONConversionExceptionConvert a Map to a JSONObject. Map keys must be Strings.- Parameters:
source- The Map to converttargetClass- the targetClass to cast each element in the mapoptions- specifies JSON Conversion options to apply. Experimental - not fully implemented- 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- See Also:
ClassConverter.convertToJSONObject(Map, Class)
-
convertToJSONObject
public static JSONObject convertToJSONObject(Map<String,?> source, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass, ConverterOption<?>... options) throws JSONConversionExceptionConvert a Map to a JSONObject. Map keys must be Strings.- Parameters:
source- The Map to convertvalueConverter- the ValueConvertertargetClass- the targetClass to cast each element in the mapoptions- specifies JSON Conversion options to apply. Experimental - not fully implemented- 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- See Also:
ClassConverter.convertToJSONObject(Map, Optional, Optional)
-
convertToJSONArray
public static JSONArray convertToJSONArray(List<?> source, ConverterOption<?>... options) throws JSONConversionException- Throws:
JSONConversionException- See Also:
ClassConverter.convertToJSONArray(List)
-
convertToJSONArray
public static JSONArray convertToJSONArray(List<?> source, Class<?> targetClass, ConverterOption<?>... options) throws JSONConversionException- Throws:
JSONConversionException- See Also:
ClassConverter.convertToJSONArray(List, Class)
-
convertToJSONArray
public static JSONArray convertToJSONArray(List<?> source, ValueConverter<?> valueConverter, ConverterOption<?>... options) throws JSONConversionException -
convertToJSONArray
public static JSONArray convertToJSONArray(List<?> source, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass, ConverterOption<?>... options) throws JSONConversionException -
convertToJSONValue
public static JSONValue<?> convertToJSONValue(Object value, Optional<ValueConverter<?>> valueConverter, Optional<Class<?>> targetClass, ConverterOption<?>... options) throws JSONConversionException
-