Search in sources :

Example 11 with SerializableProperty

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();
}
Also used : JsonException(com.serotonin.json.JsonException) SerializableProperty(com.serotonin.json.util.SerializableProperty) Method(java.lang.reflect.Method) IOException(java.io.IOException) JsonException(com.serotonin.json.JsonException) 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