Search in sources :

Example 1 with JsonProperty

use of com.serotonin.json.spi.JsonProperty in project ma-core-public by infiniteautomation.

the class JsonContext method gatherJsonPropertyNames.

private Map<String, JsonProperty> gatherJsonPropertyNames(Class<?> clazz) throws JsonException {
    // Ignore Object.
    if (clazz == Object.class)
        return null;
    Map<String, JsonProperty> jsonProperties = new HashMap<>();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        JsonProperty anno = field.getAnnotation(JsonProperty.class);
        if (anno != null)
            jsonProperties.put(field.getName(), anno);
    }
    Method[] methods = clazz.getDeclaredMethods();
    for (Method method : methods) {
        JsonProperty anno = method.getAnnotation(JsonProperty.class);
        if (anno == null)
            continue;
        // Convert the method name to a property name using the JavaBean rules.
        String name = method.getName();
        if (method.getReturnType() == Boolean.TYPE) {
            if (!name.startsWith("is"))
                throw new JsonException("Non-JavaBean get methods cannot be marked with JsonRemoteProperty: " + clazz.getName() + "." + name);
            name = Character.toLowerCase(name.charAt(2)) + name.substring(3);
        } else {
            if (!name.startsWith("get") && !name.startsWith("set"))
                throw new JsonException("Non-JavaBean get methods cannot be marked with JsonRemoteProperty: " + clazz.getName() + "." + name);
            name = Character.toLowerCase(name.charAt(3)) + name.substring(4);
        }
        jsonProperties.put(name, anno);
    }
    return jsonProperties;
}
Also used : Field(java.lang.reflect.Field) JsonProperty(com.serotonin.json.spi.JsonProperty) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) JsonString(com.serotonin.json.type.JsonString) Method(java.lang.reflect.Method)

Example 2 with JsonProperty

use of com.serotonin.json.spi.JsonProperty in project ma-core-public by infiniteautomation.

the class JsonSerializableUtility 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);
        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) Method(java.lang.reflect.Method)

Example 3 with JsonProperty

use of com.serotonin.json.spi.JsonProperty in project ma-core-public by infiniteautomation.

the class JsonSerializableUtility method gatherJsonPropertyNames.

private Map<String, JsonProperty> gatherJsonPropertyNames(Class<?> clazz) throws JsonException {
    // Ignore Object.
    if (clazz == Object.class)
        return null;
    Map<String, JsonProperty> jsonProperties = new HashMap<>();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        JsonProperty anno = field.getAnnotation(JsonProperty.class);
        if (anno != null)
            jsonProperties.put(field.getName(), anno);
    }
    Method[] methods = clazz.getDeclaredMethods();
    for (Method method : methods) {
        JsonProperty anno = method.getAnnotation(JsonProperty.class);
        if (anno == null)
            continue;
        // Convert the method name to a property name using the JavaBean rules.
        String name = method.getName();
        if (method.getReturnType() == Boolean.TYPE) {
            if (!name.startsWith("is"))
                throw new JsonException("Non-JavaBean get methods cannot be marked with JsonRemoteProperty: " + clazz.getName() + "." + name);
            name = Character.toLowerCase(name.charAt(2)) + name.substring(3);
        } else {
            if (!name.startsWith("get") && !name.startsWith("set"))
                throw new JsonException("Non-JavaBean get methods cannot be marked with JsonRemoteProperty: " + clazz.getName() + "." + name);
            name = Character.toLowerCase(name.charAt(3)) + name.substring(4);
        }
        jsonProperties.put(name, anno);
    }
    return jsonProperties;
}
Also used : JsonException(com.serotonin.json.JsonException) Field(java.lang.reflect.Field) JsonProperty(com.serotonin.json.spi.JsonProperty) HashMap(java.util.HashMap) Method(java.lang.reflect.Method)

Example 4 with JsonProperty

use of com.serotonin.json.spi.JsonProperty 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)

Aggregations

JsonProperty (com.serotonin.json.spi.JsonProperty)4 Method (java.lang.reflect.Method)4 JsonString (com.serotonin.json.type.JsonString)2 SerializableProperty (com.serotonin.json.util.SerializableProperty)2 PropertyDescriptor (java.beans.PropertyDescriptor)2 Field (java.lang.reflect.Field)2 HashMap (java.util.HashMap)2 JsonException (com.serotonin.json.JsonException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1