use of com.serotonin.json.util.SerializableProperty in project ma-core-public by infiniteautomation.
the class JsonPropertyConverter method jsonWrite.
public void jsonWrite(String includeHint, Object value, ObjectWriter objectWriter) throws IOException, JsonException {
if (jsonSerializable)
((JsonSerializable) value).jsonWrite(objectWriter);
if (properties != null) {
for (SerializableProperty prop : properties) {
// Check whether the property should be included
if (!prop.include(includeHint))
continue;
Method readMethod = prop.getReadMethod();
if (readMethod == null)
continue;
String name = prop.getNameToUse();
Object propertyValue;
try {
propertyValue = readMethod.invoke(value);
} catch (Exception e) {
throw new JsonException("Error reading '" + prop.getName() + "' from value " + value + " of class " + value.getClass(), e);
}
// Check if the value should be ignored.
boolean ignore = false;
if (prop.isSuppressDefaultValue()) {
if (propertyValue == null)
ignore = true;
else {
Class<?> propertyClass = readMethod.getReturnType();
// Check if this value is the properties default value.
if (propertyClass == Boolean.TYPE)
ignore = ((Boolean) propertyValue) == false;
else if (propertyClass == Double.TYPE)
ignore = (Double) propertyValue == 0;
else if (propertyClass == Long.TYPE)
ignore = (Long) propertyValue == 0;
else if (propertyClass == Float.TYPE)
ignore = (Float) propertyValue == 0;
else if (propertyClass == Integer.TYPE)
ignore = (Integer) propertyValue == 0;
else if (propertyClass == Short.TYPE)
ignore = (Short) propertyValue == 0;
else if (propertyClass == Byte.TYPE)
ignore = (Byte) propertyValue == 0;
else if (propertyClass == Character.TYPE)
ignore = (Character) propertyValue == 0;
}
}
if (!ignore)
objectWriter.writeEntry(name, propertyValue);
}
}
objectWriter.finish();
}
Aggregations