Search in sources :

Example 1 with JsonSerializable

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

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

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

JsonSerializable (com.serotonin.json.spi.JsonSerializable)3 JsonObject (com.serotonin.json.type.JsonObject)3 SerializableProperty (com.serotonin.json.util.SerializableProperty)3 HashMap (java.util.HashMap)2