Search in sources :

Example 1 with SerializableProperty

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

the class JsonSerializableUtility method differentRecursive.

protected boolean differentRecursive(Object from, Object to) throws JsonException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {
    if (from == null && to != null || to == null && from != null)
        return true;
    if (from == null && to == null)
        return false;
    if (!from.getClass().equals(to.getClass()))
        return true;
    List<SerializableProperty> properties = findProperties(from.getClass());
    // Check the serialized annotations
    for (SerializableProperty property : properties) if (different(property.getReadMethod().invoke(from), property.getReadMethod().invoke(to)))
        return true;
    // Second if we are JsonSerializable check the values returned from that
    JsonMapEntryWriter fromWriter = new JsonMapEntryWriter();
    if (from instanceof JsonSerializable) {
        ((JsonSerializable) from).jsonWrite(fromWriter);
    }
    JsonMapEntryWriter toWriter = new JsonMapEntryWriter();
    if (to instanceof JsonSerializable) {
        ((JsonSerializable) to).jsonWrite(toWriter);
    }
    // Compare the 2 maps and if different add the toValues
    Iterator<String> it = toWriter.keySet().iterator();
    if (it.hasNext()) {
        while (it.hasNext()) {
            String name = it.next();
            Object fromValue = fromWriter.get(name);
            Object toValue = toWriter.get(name);
            if (different(fromValue, toValue))
                return true;
        }
    } else if (properties.size() == 0) {
        // No Sero Json Properties at all, hopefully something that implements .equals()
        return !Objects.equals(from, to);
    }
    return false;
}
Also used : SerializableProperty(com.serotonin.json.util.SerializableProperty) JsonSerializable(com.serotonin.json.spi.JsonSerializable) JsonObject(com.serotonin.json.type.JsonObject)

Example 2 with SerializableProperty

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

the class JsonSerializableUtility method findValues.

public Map<String, Object> findValues(Object o) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonException, IOException {
    Map<String, Object> allProperties = new HashMap<String, Object>();
    // First check the annotated properties
    List<SerializableProperty> properties = findProperties(o.getClass());
    for (SerializableProperty property : properties) {
        allProperties.put(property.getName(), property.getReadMethod().invoke(o));
    }
    // Second Check the JsonSerialization
    JsonMapEntryWriter writer = new JsonMapEntryWriter();
    if (o instanceof JsonSerializable) {
        ((JsonSerializable) o).jsonWrite(writer);
    }
    // Compare the 2 maps and if different add the toValues
    Iterator<String> it = writer.keySet().iterator();
    while (it.hasNext()) {
        String name = it.next();
        allProperties.put(name, writer.get(name));
    }
    return allProperties;
}
Also used : HashMap(java.util.HashMap) SerializableProperty(com.serotonin.json.util.SerializableProperty) JsonSerializable(com.serotonin.json.spi.JsonSerializable) JsonObject(com.serotonin.json.type.JsonObject)

Example 3 with SerializableProperty

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

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

Example 4 with SerializableProperty

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

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

the class JsonSerializableUtility method findChanges.

/**
 * Get all changes between to Objects based on JsonSerialization properties
 * @param from
 * @param to
 * @return
 * @throws IllegalAccessException
 * @throws IllegalArgumentException
 * @throws InvocationTargetException
 * @throws JsonException
 * @throws IOException
 */
public Map<String, Object> findChanges(Object from, Object to) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JsonException, IOException {
    Map<String, Object> allChanges = new HashMap<String, Object>();
    // First check the annotated properties
    List<SerializableProperty> properties = findProperties(from.getClass());
    for (SerializableProperty property : properties) // Compare the property and if it has members, compare them.
    if (different(property.getReadMethod().invoke(from), property.getReadMethod().invoke(to))) {
        allChanges.put(property.getName(), property.getReadMethod().invoke(to));
    }
    // Second if we are JsonSerializable check the values returned from that
    JsonMapEntryWriter fromWriter = new JsonMapEntryWriter();
    if (from instanceof JsonSerializable) {
        ((JsonSerializable) from).jsonWrite(fromWriter);
    }
    JsonMapEntryWriter toWriter = new JsonMapEntryWriter();
    if (to instanceof JsonSerializable) {
        ((JsonSerializable) to).jsonWrite(toWriter);
    }
    // Compare the 2 maps and if different add the toValues (use toWriter to pick up new properties/fields)
    Iterator<String> it = toWriter.keySet().iterator();
    while (it.hasNext()) {
        String name = it.next();
        Object fromValue = fromWriter.get(name);
        Object toValue = toWriter.get(name);
        if (different(fromValue, toValue))
            allChanges.put(name, toValue);
    }
    return allChanges;
}
Also used : HashMap(java.util.HashMap) SerializableProperty(com.serotonin.json.util.SerializableProperty) JsonSerializable(com.serotonin.json.spi.JsonSerializable) JsonObject(com.serotonin.json.type.JsonObject)

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