Search in sources :

Example 1 with TypeHandler

use of org.terasology.persistence.typeHandling.TypeHandler in project Terasology by MovingBlocks.

the class MappedContainerTypeHandler method serialize.

@Override
public PersistedData serialize(T value, SerializationContext context) {
    if (value == null) {
        return context.createNull();
    }
    Map<String, PersistedData> mappedData = Maps.newLinkedHashMap();
    for (Map.Entry<FieldMetadata<T, ?>, TypeHandler<?>> entry : mappedFields.entrySet()) {
        Object val = entry.getKey().getValue(value);
        if (val != null) {
            TypeHandler handler = entry.getValue();
            PersistedData fieldValue = handler.serialize(val, context);
            if (fieldValue != null) {
                mappedData.put(entry.getKey().getName(), fieldValue);
            }
        }
    }
    return context.create(mappedData);
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) PersistedData(org.terasology.persistence.typeHandling.PersistedData) SimpleTypeHandler(org.terasology.persistence.typeHandling.SimpleTypeHandler) TypeHandler(org.terasology.persistence.typeHandling.TypeHandler) Map(java.util.Map)

Example 2 with TypeHandler

use of org.terasology.persistence.typeHandling.TypeHandler in project Terasology by MovingBlocks.

the class EnvironmentSwitchHandler method registerTypeHandlers.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static void registerTypeHandlers(Context context, TypeSerializationLibrary library, ModuleEnvironment environment) {
    for (Class<? extends TypeHandler> handler : environment.getSubtypesOf(TypeHandler.class)) {
        RegisterTypeHandler register = handler.getAnnotation(RegisterTypeHandler.class);
        if (register != null) {
            Optional<Type> opt = GenericsUtil.getTypeParameterBindingForInheritedClass(handler, TypeHandler.class, 0);
            if (opt.isPresent()) {
                TypeHandler instance = InjectionHelper.createWithConstructorInjection(handler, context);
                InjectionHelper.inject(instance, context);
                library.add((Class) opt.get(), instance);
            }
        }
    }
}
Also used : Type(java.lang.reflect.Type) RegisterTypeHandler(org.terasology.persistence.typeHandling.RegisterTypeHandler) RegisterTypeHandler(org.terasology.persistence.typeHandling.RegisterTypeHandler) CollisionGroupTypeHandler(org.terasology.persistence.typeHandling.extensionTypes.CollisionGroupTypeHandler) TypeHandler(org.terasology.persistence.typeHandling.TypeHandler)

Example 3 with TypeHandler

use of org.terasology.persistence.typeHandling.TypeHandler in project Terasology by MovingBlocks.

the class MappedContainerTypeHandler method deserialize.

@Override
public T deserialize(PersistedData data, DeserializationContext context) {
    try {
        T result = clazz.newInstance();
        for (Map.Entry<String, PersistedData> entry : data.getAsValueMap().entrySet()) {
            FieldMetadata fieldInfo = fieldByName.get(UriUtil.normalise(entry.getKey()));
            if (fieldInfo != null) {
                TypeHandler handler = mappedFields.get(fieldInfo);
                Object val = handler.deserialize(entry.getValue(), context);
                fieldInfo.setValue(result, val);
            }
        }
        return result;
    } catch (Exception e) {
        logger.error("Unable to deserialize {}", data, e);
    }
    return null;
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) PersistedData(org.terasology.persistence.typeHandling.PersistedData) SimpleTypeHandler(org.terasology.persistence.typeHandling.SimpleTypeHandler) TypeHandler(org.terasology.persistence.typeHandling.TypeHandler) Map(java.util.Map)

Aggregations

TypeHandler (org.terasology.persistence.typeHandling.TypeHandler)3 Map (java.util.Map)2 PersistedData (org.terasology.persistence.typeHandling.PersistedData)2 SimpleTypeHandler (org.terasology.persistence.typeHandling.SimpleTypeHandler)2 FieldMetadata (org.terasology.reflection.metadata.FieldMetadata)2 Type (java.lang.reflect.Type)1 RegisterTypeHandler (org.terasology.persistence.typeHandling.RegisterTypeHandler)1 CollisionGroupTypeHandler (org.terasology.persistence.typeHandling.extensionTypes.CollisionGroupTypeHandler)1