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