com.ppiech.auto.value.jackson.LoganSquare Maven / Gradle / Ivy
package com.ppiech.auto.value.jackson;
import com.ppiech.auto.value.jackson.util.SimpleArrayMap;
import com.fasterxml.jackson.core.JsonFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/** The point of all interaction with this library. */
public class LoganSquare {
private static final com.ppiech.auto.value.jackson.internal.objectmappers.ListMapper
LIST_MAPPER = new com.ppiech.auto.value.jackson.internal.objectmappers.ListMapper();
private static final com.ppiech.auto.value.jackson.internal.objectmappers.MapMapper
MAP_MAPPER = new com.ppiech.auto.value.jackson.internal.objectmappers.MapMapper();
private static final Map OBJECT_MAPPERS = new ConcurrentHashMap();
static {
OBJECT_MAPPERS.put(String.class, new com.ppiech.auto.value.jackson.internal.objectmappers.StringMapper());
OBJECT_MAPPERS.put(Integer.class, new com.ppiech.auto.value.jackson.internal.objectmappers.IntegerMapper());
OBJECT_MAPPERS.put(Long.class, new com.ppiech.auto.value.jackson.internal.objectmappers.LongMapper());
OBJECT_MAPPERS.put(Float.class, new com.ppiech.auto.value.jackson.internal.objectmappers.FloatMapper());
OBJECT_MAPPERS.put(Double.class, new com.ppiech.auto.value.jackson.internal.objectmappers.DoubleMapper());
OBJECT_MAPPERS.put(Boolean.class, new com.ppiech.auto.value.jackson.internal.objectmappers.BooleanMapper());
OBJECT_MAPPERS.put(Object.class, new com.ppiech.auto.value.jackson.internal.objectmappers.ObjectMapper());
OBJECT_MAPPERS.put(List.class, LIST_MAPPER);
OBJECT_MAPPERS.put(ArrayList.class, LIST_MAPPER);
OBJECT_MAPPERS.put(Map.class, MAP_MAPPER);
OBJECT_MAPPERS.put(HashMap.class, MAP_MAPPER);
}
private static final ConcurrentHashMap PARAMETERIZED_OBJECT_MAPPERS = new ConcurrentHashMap();
private static final SimpleArrayMap TYPE_CONVERTERS = new SimpleArrayMap<>();
static {
registerTypeConverter(Date.class, new com.ppiech.auto.value.jackson.typeconverters.DefaultDateConverter());
registerTypeConverter(Calendar.class, new com.ppiech.auto.value.jackson.typeconverters.DefaultCalendarConverter());
}
/** The JsonFactory that should be used throughout the entire app. */
public static final JsonFactory JSON_FACTORY = new JsonFactory();
/**
* Parse an object from an InputStream.
*
* @param is The InputStream, most likely from your networking library.
* @param jsonObjectClass The @JsonObject class to parse the InputStream into
*/
public static E parse(InputStream is, Class jsonObjectClass) throws IOException {
return mapperFor(jsonObjectClass).parse(is);
}
/**
* Parse an object from a String. Note: parsing from an InputStream should be preferred over parsing from a String if possible.
*
* @param jsonString The JSON string being parsed.
* @param jsonObjectClass The @JsonObject class to parse the InputStream into
*/
public static E parse(String jsonString, Class jsonObjectClass) throws IOException {
return mapperFor(jsonObjectClass).parse(jsonString);
}
/**
* Parse a parameterized object from an InputStream.
*
* @param is The InputStream, most likely from your networking library.
* @param jsonObjectType The ParameterizedType describing the object. Ex: LoganSquare.parse(is, new ParameterizedType<MyModel<OtherModel>>() { });
*/
public static E parse(InputStream is, ParameterizedType jsonObjectType) throws IOException {
return mapperFor(jsonObjectType).parse(is);
}
/**
* Parse a parameterized object from a String. Note: parsing from an InputStream should be preferred over parsing from a String if possible.
*
* @param jsonString The JSON string being parsed.
* @param jsonObjectType The ParameterizedType describing the object. Ex: LoganSquare.parse(is, new ParameterizedType<MyModel<OtherModel>>() { });
*/
public static E parse(String jsonString, ParameterizedType jsonObjectType) throws IOException {
return mapperFor(jsonObjectType).parse(jsonString);
}
/**
* Parse a list of objects from an InputStream.
*
* @param is The inputStream, most likely from your networking library.
* @param jsonObjectClass The @JsonObject class to parse the InputStream into
*/
public static List parseList(InputStream is, Class jsonObjectClass) throws IOException {
return mapperFor(jsonObjectClass).parseList(is);
}
/**
* Parse a list of objects from a String. Note: parsing from an InputStream should be preferred over parsing from a String if possible.
*
* @param jsonString The JSON string being parsed.
* @param jsonObjectClass The @JsonObject class to parse the InputStream into
*/
public static List parseList(String jsonString, Class jsonObjectClass) throws IOException {
return mapperFor(jsonObjectClass).parseList(jsonString);
}
/**
* Parse a map of objects from an InputStream.
*
* @param is The inputStream, most likely from your networking library.
* @param jsonObjectClass The @JsonObject class to parse the InputStream into
*/
public static Map parseMap(InputStream is, Class jsonObjectClass) throws IOException {
return mapperFor(jsonObjectClass).parseMap(is);
}
/**
* Parse a map of objects from a String. Note: parsing from an InputStream should be preferred over parsing from a String if possible.
*
* @param jsonString The JSON string being parsed.
* @param jsonObjectClass The @JsonObject class to parse the InputStream into
*/
public static Map parseMap(String jsonString, Class jsonObjectClass) throws IOException {
return mapperFor(jsonObjectClass).parseMap(jsonString);
}
/**
* Serialize an object to a JSON String.
*
* @param object The object to serialize.
*/
@SuppressWarnings("unchecked")
public static String serialize(E object) throws IOException {
return mapperFor((Class)object.getClass()).serialize(object);
}
/**
* Serialize an object to an OutputStream.
*
* @param object The object to serialize.
* @param os The OutputStream being written to.
*/
@SuppressWarnings("unchecked")
public static void serialize(E object, OutputStream os) throws IOException {
mapperFor((Class)object.getClass()).serialize(object, os);
}
/**
* Serialize a parameterized object to a JSON String.
*
* @param object The object to serialize.
* @param parameterizedType The ParameterizedType describing the object. Ex: LoganSquare.serialize(object, new ParameterizedType<MyModel<OtherModel>>() { });
*/
@SuppressWarnings("unchecked")
public static String serialize(E object, ParameterizedType parameterizedType) throws IOException {
return mapperFor(parameterizedType).serialize(object);
}
/**
* Serialize a parameterized object to an OutputStream.
*
* @param object The object to serialize.
* @param parameterizedType The ParameterizedType describing the object. Ex: LoganSquare.serialize(object, new ParameterizedType<MyModel<OtherModel>>() { }, os);
* @param os The OutputStream being written to.
*/
@SuppressWarnings("unchecked")
public static void serialize(E object, ParameterizedType parameterizedType, OutputStream os) throws IOException {
mapperFor(parameterizedType).serialize(object, os);
}
/**
* Serialize a list of objects to a JSON String.
*
* @param list The list of objects to serialize.
* @param jsonObjectClass The @JsonObject class of the list elements
*/
public static String serialize(List list, Class jsonObjectClass) throws IOException {
return mapperFor(jsonObjectClass).serialize(list);
}
/**
* Serialize a list of objects to an OutputStream.
*
* @param list The list of objects to serialize.
* @param os The OutputStream to which the list should be serialized
* @param jsonObjectClass The @JsonObject class of the list elements
*/
public static void serialize(List list, OutputStream os, Class jsonObjectClass) throws IOException {
mapperFor(jsonObjectClass).serialize(list, os);
}
/**
* Serialize a map of objects to a JSON String.
*
* @param map The map of objects to serialize.
* @param jsonObjectClass The @JsonObject class of the list elements
*/
public static String serialize(Map map, Class jsonObjectClass) throws IOException {
return mapperFor(jsonObjectClass).serialize(map);
}
/**
* Serialize a map of objects to an OutputStream.
*
* @param map The map of objects to serialize.
* @param os The OutputStream to which the list should be serialized
* @param jsonObjectClass The @JsonObject class of the list elements
*/
public static void serialize(Map map, OutputStream os, Class jsonObjectClass) throws IOException {
mapperFor(jsonObjectClass).serialize(map, os);
}
@SuppressWarnings("unchecked")
/*package*/ static JsonMapper getMapper(Class cls) {
JsonMapper mapper = OBJECT_MAPPERS.get(cls);
if (mapper == null) {
// The only way the mapper wouldn't already be loaded into OBJECT_MAPPERS is if it was compiled separately, but let's handle it anyway
try {
Class> mapperClass = Class.forName(cls.getName() + Constants.MAPPER_CLASS_SUFFIX);
mapper = (JsonMapper)mapperClass.newInstance();
OBJECT_MAPPERS.put(cls, mapper);
} catch (Exception ignored) { }
}
return mapper;
}
@SuppressWarnings("unchecked")
private static JsonMapper getMapper(ParameterizedType type, SimpleArrayMap partialMappers) {
if (type.typeParameters.size() == 0) {
return getMapper((Class)type.rawType);
}
if (partialMappers == null) {
partialMappers = new SimpleArrayMap();
}
if (partialMappers.containsKey(type)) {
return partialMappers.get(type);
} else if (PARAMETERIZED_OBJECT_MAPPERS.containsKey(type)) {
return PARAMETERIZED_OBJECT_MAPPERS.get(type);
} else {
try {
Class> mapperClass = Class.forName(type.rawType.getName() + Constants.MAPPER_CLASS_SUFFIX);
Constructor constructor = mapperClass.getDeclaredConstructors()[0];
Object[] args = new Object[2 + type.typeParameters.size()];
args[0] = type;
args[args.length - 1] = partialMappers;
for (int i = 0; i < type.typeParameters.size(); i++) {
args[i + 1] = type.typeParameters.get(i);
}
JsonMapper mapper = (JsonMapper)constructor.newInstance(args);
PARAMETERIZED_OBJECT_MAPPERS.put(type, mapper);
return mapper;
} catch (Exception ignored) {
return null;
}
}
}
/**
* Returns whether or not LoganSquare can handle a given class.
*
* @param cls The class for which support is being checked.
*/
@SuppressWarnings("unchecked")
public static boolean supports(Class cls) {
return getMapper(cls) != null;
}
/**
* Returns whether or not LoganSquare can handle a given ParameterizedType.
*
* @param type The ParameterizedType for which support is being checked.
*/
@SuppressWarnings("unchecked")
public static boolean supports(ParameterizedType type) {
return getMapper(type, null) != null;
}
/**
* Returns a JsonMapper for a given class that has been annotated with @JsonObject.
*
* @param cls The class for which the JsonMapper should be fetched.
*/
public static JsonMapper mapperFor(Class cls) throws NoSuchMapperException {
JsonMapper mapper = getMapper(cls);
if (mapper == null) {
throw new NoSuchMapperException(cls);
} else {
return mapper;
}
}
/**
* Returns a JsonMapper for a given class that has been annotated with @JsonObject.
*
* @param type The ParameterizedType for which the JsonMapper should be fetched.
*/
@SuppressWarnings("unchecked")
public static JsonMapper mapperFor(ParameterizedType type) throws NoSuchMapperException {
return mapperFor(type, null);
}
public static JsonMapper mapperFor(ParameterizedType type, SimpleArrayMap partialMappers) throws
NoSuchMapperException {
JsonMapper mapper = getMapper(type, partialMappers);
if (mapper == null) {
throw new NoSuchMapperException(type.rawType);
} else {
return mapper;
}
}
/**
* Returns a TypeConverter for a given class.
*
* @param cls The class for which the TypeConverter should be fetched.
*/
@SuppressWarnings("unchecked")
public static com.ppiech.auto.value.jackson.typeconverters.TypeConverter typeConverterFor(Class cls) throws
com.ppiech.auto.value.jackson.NoSuchTypeConverterException {
com.ppiech.auto.value.jackson.typeconverters.TypeConverter typeConverter = TYPE_CONVERTERS.get(cls);
if (typeConverter == null) {
throw new com.ppiech.auto.value.jackson.NoSuchTypeConverterException(cls);
}
return typeConverter;
}
/**
* Register a new TypeConverter for parsing and serialization.
*
* @param cls The class for which the TypeConverter should be used.
* @param converter The TypeConverter
*/
public static void registerTypeConverter(Class cls, com.ppiech.auto.value.jackson.typeconverters.TypeConverter converter) {
TYPE_CONVERTERS.put(cls, converter);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy