use of org.terasology.persistence.typeHandling.DeserializationContext in project Terasology by MovingBlocks.
the class ComponentSerializer method deserializeOnto.
private <T extends Component> Component deserializeOnto(Component targetComponent, EntityData.Component componentData, ComponentMetadata<T> componentMetadata, FieldSerializeCheck<Component> fieldCheck) {
Serializer serializer = typeSerializationLibrary.getSerializerFor(componentMetadata);
DeserializationContext context = new ProtobufDeserializationContext(typeSerializationLibrary);
Map<FieldMetadata<?, ?>, PersistedData> dataMap = Maps.newHashMapWithExpectedSize(componentData.getFieldCount());
for (EntityData.NameValue field : componentData.getFieldList()) {
FieldMetadata<?, ?> fieldInfo = null;
if (field.hasNameIndex()) {
fieldInfo = componentMetadata.getField(field.getNameIndex());
} else if (field.hasName()) {
fieldInfo = componentMetadata.getField(field.getName());
}
if (fieldInfo != null) {
dataMap.put(fieldInfo, new ProtobufPersistedData(field.getValue()));
} else if (field.hasName()) {
logger.warn("Cannot deserialize unknown field '{}' onto '{}'", field.getName(), componentMetadata.getUri());
}
}
serializer.deserializeOnto(targetComponent, dataMap, context, fieldCheck);
return targetComponent;
}
use of org.terasology.persistence.typeHandling.DeserializationContext in project Terasology by MovingBlocks.
the class EnumTypeHandlerSerializerTest method testNonNullValue.
@Test
public void testNonNullValue() throws Exception {
PersistedData data = mock(PersistedData.class);
when(data.getAsString()).thenReturn(TestEnum.NON_NULL.toString());
when(data.isString()).thenReturn(true);
SerializationContext serializationContext = mock(SerializationContext.class);
when(serializationContext.create(TestEnum.NON_NULL.toString())).thenReturn(data);
EnumTypeHandler<TestEnum> handler = new EnumTypeHandler<>(TestEnum.class);
PersistedData serializedNonNull = handler.serialize(TestEnum.NON_NULL, serializationContext);
assertEquals(data, serializedNonNull);
DeserializationContext deserializationContext = mock(DeserializationContext.class);
TestEnum deserializedValue = handler.deserialize(data, deserializationContext);
assertEquals(TestEnum.NON_NULL, deserializedValue);
}
use of org.terasology.persistence.typeHandling.DeserializationContext in project Terasology by MovingBlocks.
the class EnumTypeHandlerSerializerTest method testNullValue.
@Test
public void testNullValue() throws Exception {
PersistedData nullData = mock(PersistedData.class);
when(nullData.isNull()).thenReturn(true);
SerializationContext serializationContext = mock(SerializationContext.class);
when(serializationContext.createNull()).thenReturn(nullData);
EnumTypeHandler<TestEnum> handler = new EnumTypeHandler<>(TestEnum.class);
PersistedData serializedNull = handler.serialize(null, serializationContext);
assertEquals(nullData, serializedNull);
DeserializationContext deserializationContext = mock(DeserializationContext.class);
TestEnum deserializedValue = handler.deserialize(nullData, deserializationContext);
assertEquals(null, deserializedValue);
}
Aggregations