Search in sources :

Example 1 with DeserializationContext

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;
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) ReplicatedFieldMetadata(org.terasology.entitySystem.metadata.ReplicatedFieldMetadata) ProtobufPersistedData(org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData) EntityData(org.terasology.protobuf.EntityData) DeserializationContext(org.terasology.persistence.typeHandling.DeserializationContext) ProtobufDeserializationContext(org.terasology.persistence.typeHandling.protobuf.ProtobufDeserializationContext) ProtobufDeserializationContext(org.terasology.persistence.typeHandling.protobuf.ProtobufDeserializationContext) PersistedData(org.terasology.persistence.typeHandling.PersistedData) ProtobufPersistedData(org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData) Serializer(org.terasology.persistence.typeHandling.Serializer)

Example 2 with DeserializationContext

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);
}
Also used : SerializationContext(org.terasology.persistence.typeHandling.SerializationContext) PersistedData(org.terasology.persistence.typeHandling.PersistedData) DeserializationContext(org.terasology.persistence.typeHandling.DeserializationContext) Test(org.junit.Test)

Example 3 with DeserializationContext

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);
}
Also used : SerializationContext(org.terasology.persistence.typeHandling.SerializationContext) PersistedData(org.terasology.persistence.typeHandling.PersistedData) DeserializationContext(org.terasology.persistence.typeHandling.DeserializationContext) Test(org.junit.Test)

Aggregations

DeserializationContext (org.terasology.persistence.typeHandling.DeserializationContext)3 PersistedData (org.terasology.persistence.typeHandling.PersistedData)3 Test (org.junit.Test)2 SerializationContext (org.terasology.persistence.typeHandling.SerializationContext)2 ReplicatedFieldMetadata (org.terasology.entitySystem.metadata.ReplicatedFieldMetadata)1 Serializer (org.terasology.persistence.typeHandling.Serializer)1 ProtobufDeserializationContext (org.terasology.persistence.typeHandling.protobuf.ProtobufDeserializationContext)1 ProtobufPersistedData (org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData)1 EntityData (org.terasology.protobuf.EntityData)1 FieldMetadata (org.terasology.reflection.metadata.FieldMetadata)1