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;
}
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;
}
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);
}
}
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();
}
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;
}
Aggregations