Search in sources :

Example 6 with SerializableProperty

use of com.serotonin.json.util.SerializableProperty in project ma-core-public by infiniteautomation.

the class JsonSerializableUtility method findProperties.

public List<SerializableProperty> findProperties(Class<?> clazz) throws JsonException {
    // 
    // Introspect the class.
    List<SerializableProperty> properties = new ArrayList<>();
    boolean jsonSerializable = JsonSerializable.class.isAssignableFrom(clazz);
    if (!jsonSerializable) {
        // Is it a semi-primative
        if (clazz.isAssignableFrom(Boolean.class) || clazz.isAssignableFrom(Byte.class) || clazz.isAssignableFrom(Short.class) || clazz.isAssignableFrom(Integer.class) || clazz.isAssignableFrom(Long.class) || clazz.isAssignableFrom(Float.class) || clazz.isAssignableFrom(Double.class) || clazz.isAssignableFrom(BigInteger.class) || clazz.isAssignableFrom(BigDecimal.class) || clazz.isAssignableFrom(String.class) || clazz.isAssignableFrom(Object.class))
            return properties;
    }
    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);
        // Serotonin JSON searches for POJO properties here, we don't want to.
        if (!annotationsFound && !currentClazz.isAnnotationPresent(JsonEntity.class) && !jsonSerializable)
            addPojoProperties(currentClazz, descriptors, properties);
        currentClazz = currentClazz.getSuperclass();
    }
    return properties;
}
Also used : JsonException(com.serotonin.json.JsonException) 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) BigDecimal(java.math.BigDecimal) JsonObject(com.serotonin.json.type.JsonObject)

Example 7 with SerializableProperty

use of com.serotonin.json.util.SerializableProperty in project ma-core-public by infiniteautomation.

the class JsonContext method addPojoProperties.

private void addPojoProperties(Class<?> clazz, PropertyDescriptor[] descriptors, List<SerializableProperty> properties) {
    for (PropertyDescriptor descriptor : descriptors) {
        String name = descriptor.getName();
        // Don't implicitly marshall getClass()
        if (name.equals("class"))
            continue;
        // Ignore hibernate stuff too
        if (name.equals("hibernateLazyInitializer"))
            continue;
        Method readMethod = descriptor.getReadMethod();
        if (readMethod != null && readMethod.getDeclaringClass() != clazz)
            readMethod = null;
        Method writeMethod = descriptor.getWriteMethod();
        if (writeMethod != null && writeMethod.getDeclaringClass() != clazz)
            writeMethod = null;
        SerializableProperty prop = new SerializableProperty();
        prop.setName(name);
        prop.setReadMethod(readMethod);
        prop.setWriteMethod(writeMethod);
        maybeAddProperty(properties, prop);
    }
}
Also used : PropertyDescriptor(java.beans.PropertyDescriptor) SerializableProperty(com.serotonin.json.util.SerializableProperty) JsonString(com.serotonin.json.type.JsonString) Method(java.lang.reflect.Method)

Example 8 with SerializableProperty

use of com.serotonin.json.util.SerializableProperty 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)

Example 9 with SerializableProperty

use of com.serotonin.json.util.SerializableProperty in project ma-core-public by infiniteautomation.

the class JsonContext method addAnnotatedProperties.

private boolean addAnnotatedProperties(Class<?> clazz, PropertyDescriptor[] descriptors, List<SerializableProperty> properties) throws JsonException {
    Map<String, JsonProperty> jsonProperties = gatherJsonPropertyNames(clazz);
    for (PropertyDescriptor descriptor : descriptors) {
        String name = descriptor.getName();
        // Don't implicitly marshall getClass()
        if (name.equals("class"))
            continue;
        // Ignore hibernate stuff too
        if (name.equals("hibernateLazyInitializer"))
            continue;
        JsonProperty anno = jsonProperties.get(name);
        if (anno == null || (!anno.read() && !anno.write()))
            continue;
        Method readMethod = descriptor.getReadMethod();
        if (!anno.write() || (readMethod != null && readMethod.getDeclaringClass() != clazz))
            readMethod = null;
        Method writeMethod = descriptor.getWriteMethod();
        if (!anno.read() || (writeMethod != null && writeMethod.getDeclaringClass() != clazz))
            writeMethod = null;
        if (readMethod == null && writeMethod == null)
            continue;
        SerializableProperty prop = new SerializableProperty();
        prop.setName(name);
        // if (anno.typeFactory() != TypeFactory.class)
        // prop.setTypeFactory(anno.typeFactory());
        prop.setReadMethod(readMethod);
        prop.setWriteMethod(writeMethod);
        if (!Utils.isEmpty(anno.alias()))
            prop.setAlias(anno.alias());
        prop.setSuppressDefaultValue(anno.suppressDefaultValue());
        prop.setIncludeHints(anno.includeHints());
        maybeAddProperty(properties, prop);
    }
    return !jsonProperties.isEmpty();
}
Also used : JsonProperty(com.serotonin.json.spi.JsonProperty) PropertyDescriptor(java.beans.PropertyDescriptor) SerializableProperty(com.serotonin.json.util.SerializableProperty) JsonString(com.serotonin.json.type.JsonString) Method(java.lang.reflect.Method)

Example 10 with SerializableProperty

use of com.serotonin.json.util.SerializableProperty in project ma-core-public by infiniteautomation.

the class JsonPropertyConverter method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonValue jsonValue, Object obj, Type type) throws JsonException {
    JsonObject jsonObject = (JsonObject) jsonValue;
    if (jsonSerializable)
        ((JsonSerializable) obj).jsonRead(reader, jsonObject);
    if (properties != null) {
        for (SerializableProperty prop : properties) {
            // Check whether the property should be included
            if (!prop.include(reader.getIncludeHint()))
                continue;
            Method writeMethod = prop.getWriteMethod();
            if (writeMethod == null)
                continue;
            String name = prop.getNameToUse();
            JsonValue propJsonValue = jsonObject.get(name);
            if (propJsonValue == null)
                continue;
            Type propType = writeMethod.getGenericParameterTypes()[0];
            propType = TypeUtils.resolveTypeVariable(type, propType);
            Class<?> propClass = TypeUtils.getRawClass(propType);
            try {
                Object propValue = reader.read(propType, propJsonValue);
                if (propClass.isPrimitive() && propValue == null) {
                    if (propClass == Boolean.TYPE)
                        propValue = false;
                    else
                        propValue = 0;
                }
                prop.getWriteMethod().invoke(obj, propValue);
            } catch (Exception e) {
                throw new JsonException("JsonException writing property '" + prop.getName() + "' of class " + propClass.getName(), e);
            }
        }
    }
}
Also used : JsonException(com.serotonin.json.JsonException) Type(java.lang.reflect.Type) SerializableProperty(com.serotonin.json.util.SerializableProperty) JsonValue(com.serotonin.json.type.JsonValue) JsonObject(com.serotonin.json.type.JsonObject) JsonObject(com.serotonin.json.type.JsonObject) Method(java.lang.reflect.Method) IOException(java.io.IOException) JsonException(com.serotonin.json.JsonException)

Aggregations

SerializableProperty (com.serotonin.json.util.SerializableProperty)11 JsonObject (com.serotonin.json.type.JsonObject)7 PropertyDescriptor (java.beans.PropertyDescriptor)6 Method (java.lang.reflect.Method)6 JsonException (com.serotonin.json.JsonException)3 JsonSerializable (com.serotonin.json.spi.JsonSerializable)3 HashMap (java.util.HashMap)3 JsonEntity (com.serotonin.json.spi.JsonEntity)2 JsonProperty (com.serotonin.json.spi.JsonProperty)2 JsonString (com.serotonin.json.type.JsonString)2 BeanInfo (java.beans.BeanInfo)2 IntrospectionException (java.beans.IntrospectionException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 JsonPropertyConverter (com.serotonin.json.convert.JsonPropertyConverter)1 ClassConverter (com.serotonin.json.spi.ClassConverter)1 JsonArray (com.serotonin.json.type.JsonArray)1 JsonValue (com.serotonin.json.type.JsonValue)1 Array (java.lang.reflect.Array)1 Type (java.lang.reflect.Type)1