Search in sources :

Example 1 with JsonPropertyConverter

use of com.serotonin.json.convert.JsonPropertyConverter in project ma-core-public by infiniteautomation.

the class JsonContext method getConverter.

/**
 * Returns the class converter for the given class.
 *
 * @param clazz
 *            the class to look up
 * @return the converter that is bound to the class. May be null if no converter was registered.
 * @throws JsonException
 */
public ClassConverter getConverter(Class<?> clazz) throws JsonException {
    // First check if the class is already in the map.
    ClassConverter converter = classConverters.get(clazz);
    if (converter != null)
        return converter;
    // Check for inheritance
    Class<?> sc = clazz.getSuperclass();
    while (sc != null && sc != Object.class) {
        converter = classConverters.get(sc);
        if (converter != null) {
            classConverters.put(clazz, converter);
            return converter;
        }
        sc = sc.getSuperclass();
    }
    // Check for enum
    if (Enum.class.isAssignableFrom(clazz)) {
        converter = classConverters.get(Enum.class);
        if (converter != null) {
            classConverters.put(clazz, converter);
            return converter;
        }
    }
    // Check for map
    if (Map.class.isAssignableFrom(clazz)) {
        converter = classConverters.get(Map.class);
        if (converter != null) {
            classConverters.put(clazz, converter);
            return converter;
        }
    }
    // Check for collection
    if (Collection.class.isAssignableFrom(clazz)) {
        converter = classConverters.get(Collection.class);
        if (converter != null) {
            classConverters.put(clazz, converter);
            return converter;
        }
    }
    // Check for array
    if (clazz.isArray()) {
        converter = classConverters.get(Array.class);
        if (converter != null) {
            classConverters.put(clazz, converter);
            return converter;
        }
    }
    // 
    // Introspect the class.
    boolean jsonSerializable = JsonSerializable.class.isAssignableFrom(clazz);
    boolean jsonEntity = clazz.isAnnotationPresent(JsonEntity.class);
    List<SerializableProperty> properties = new ArrayList<>();
    BeanInfo info;
    try {
        info = Introspector.getBeanInfo(clazz);
    } catch (IntrospectionException e) {
        throw new JsonException(e);
    }
    PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
    // Annotations or beans
    Class<?> currentClazz = clazz;
    while (currentClazz != Object.class) {
        boolean annotationsFound = addAnnotatedProperties(currentClazz, descriptors, properties);
        if (!annotationsFound && !currentClazz.isAnnotationPresent(JsonEntity.class) && !jsonSerializable)
            // Not annotated and no property annotations were found. Consider it a POJO.
            addPojoProperties(currentClazz, descriptors, properties);
        currentClazz = currentClazz.getSuperclass();
    }
    if (properties.isEmpty())
        properties = null;
    // Create a converter?
    if (jsonSerializable || jsonEntity || properties != null) {
        converter = new JsonPropertyConverter(jsonSerializable, properties);
        classConverters.put(clazz, converter);
        return converter;
    }
    // Give up
    throw new JsonException("No converter for class " + clazz);
}
Also used : JsonEntity(com.serotonin.json.spi.JsonEntity) PropertyDescriptor(java.beans.PropertyDescriptor) SerializableProperty(com.serotonin.json.util.SerializableProperty) BeanInfo(java.beans.BeanInfo) ArrayList(java.util.ArrayList) IntrospectionException(java.beans.IntrospectionException) JsonPropertyConverter(com.serotonin.json.convert.JsonPropertyConverter) Array(java.lang.reflect.Array) JsonArray(com.serotonin.json.type.JsonArray) Collection(java.util.Collection) JsonObject(com.serotonin.json.type.JsonObject) ClassConverter(com.serotonin.json.spi.ClassConverter) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Aggregations

JsonPropertyConverter (com.serotonin.json.convert.JsonPropertyConverter)1 ClassConverter (com.serotonin.json.spi.ClassConverter)1 JsonEntity (com.serotonin.json.spi.JsonEntity)1 JsonArray (com.serotonin.json.type.JsonArray)1 JsonObject (com.serotonin.json.type.JsonObject)1 SerializableProperty (com.serotonin.json.util.SerializableProperty)1 BeanInfo (java.beans.BeanInfo)1 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 Array (java.lang.reflect.Array)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1