Search in sources :

Example 1 with OrderedMapValues

use of com.badlogic.gdx.utils.OrderedMap.OrderedMapValues in project libgdx by libgdx.

the class Json method writeFields.

/** Writes all fields of the specified object to the current JSON object. */
public void writeFields(Object object) {
    Class type = object.getClass();
    Object[] defaultValues = getDefaultValues(type);
    OrderedMap<String, FieldMetadata> fields = getFields(type);
    int i = 0;
    for (FieldMetadata metadata : new OrderedMapValues<FieldMetadata>(fields)) {
        Field field = metadata.field;
        try {
            Object value = field.get(object);
            if (defaultValues != null) {
                Object defaultValue = defaultValues[i++];
                if (value == null && defaultValue == null)
                    continue;
                if (value != null && defaultValue != null) {
                    if (value.equals(defaultValue))
                        continue;
                    if (value.getClass().isArray() && defaultValue.getClass().isArray()) {
                        equals1[0] = value;
                        equals2[0] = defaultValue;
                        if (Arrays.deepEquals(equals1, equals2))
                            continue;
                    }
                }
            }
            if (debug)
                System.out.println("Writing field: " + field.getName() + " (" + type.getName() + ")");
            writer.name(field.getName());
            writeValue(value, field.getType(), metadata.elementType);
        } catch (ReflectionException ex) {
            throw new SerializationException("Error accessing field: " + field.getName() + " (" + type.getName() + ")", ex);
        } catch (SerializationException ex) {
            ex.addTrace(field + " (" + type.getName() + ")");
            throw ex;
        } catch (Exception runtimeEx) {
            SerializationException ex = new SerializationException(runtimeEx);
            ex.addTrace(field + " (" + type.getName() + ")");
            throw ex;
        }
    }
}
Also used : Field(com.badlogic.gdx.utils.reflect.Field) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) OrderedMapValues(com.badlogic.gdx.utils.OrderedMap.OrderedMapValues) IOException(java.io.IOException) AccessControlException(java.security.AccessControlException) ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException)

Aggregations

OrderedMapValues (com.badlogic.gdx.utils.OrderedMap.OrderedMapValues)1 Field (com.badlogic.gdx.utils.reflect.Field)1 ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)1 IOException (java.io.IOException)1 AccessControlException (java.security.AccessControlException)1