Search in sources :

Example 11 with PersistedData

use of org.terasology.persistence.typeHandling.PersistedData 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 12 with PersistedData

use of org.terasology.persistence.typeHandling.PersistedData 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)

Example 13 with PersistedData

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

the class ComponentSerializer method serialize.

/**
 * Serializes the differences between two components.
 *
 * @param base  The base component to compare against.
 * @param delta The component whose differences will be serialized
 * @param check A check to use to see if each field should be serialized.
 * @return The serialized component, or null if it could not be serialized
 */
public EntityData.Component serialize(Component base, Component delta, FieldSerializeCheck<Component> check) {
    ComponentMetadata<?> componentMetadata = componentLibrary.getMetadata(base.getClass());
    if (componentMetadata == null) {
        logger.error("Unregistered component type: {}", base.getClass());
        return null;
    }
    EntityData.Component.Builder componentMessage = EntityData.Component.newBuilder();
    serializeComponentType(componentMetadata, componentMessage);
    Serializer serializer = typeSerializationLibrary.getSerializerFor(componentMetadata);
    boolean changed = false;
    for (ReplicatedFieldMetadata field : componentMetadata.getFields()) {
        if (check.shouldSerializeField(field, delta) && serializer.getHandlerFor(field) != null) {
            Object origValue = field.getValue(base);
            Object deltaValue = field.getValue(delta);
            if (!Objects.equal(origValue, deltaValue)) {
                PersistedData value = serializer.serializeValue(field, deltaValue, serializationContext);
                if (!value.isNull()) {
                    EntityData.Value dataValue = ((ProtobufPersistedData) value).getValue();
                    if (usingFieldIds) {
                        componentMessage.addField(EntityData.NameValue.newBuilder().setNameIndex(field.getId()).setValue(dataValue).build());
                    } else {
                        componentMessage.addField(EntityData.NameValue.newBuilder().setName(field.getName()).setValue(dataValue).build());
                    }
                    changed = true;
                }
            }
        }
    }
    if (changed) {
        return componentMessage.build();
    }
    return null;
}
Also used : ProtobufPersistedData(org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData) EntityData(org.terasology.protobuf.EntityData) ReplicatedFieldMetadata(org.terasology.entitySystem.metadata.ReplicatedFieldMetadata) PersistedData(org.terasology.persistence.typeHandling.PersistedData) ProtobufPersistedData(org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData) Component(org.terasology.entitySystem.Component) Serializer(org.terasology.persistence.typeHandling.Serializer)

Example 14 with PersistedData

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

the class EnumTypeHandler method deserializeCollection.

@Override
public List<T> deserializeCollection(PersistedData data, DeserializationContext context) {
    if (data.isArray()) {
        PersistedDataArray array = data.getAsArray();
        List<T> result = Lists.newArrayListWithCapacity(array.size());
        for (PersistedData item : array) {
            result.add(deserialize(item, context));
        }
        return result;
    }
    return Lists.newArrayList();
}
Also used : PersistedDataArray(org.terasology.persistence.typeHandling.PersistedDataArray) PersistedData(org.terasology.persistence.typeHandling.PersistedData)

Example 15 with PersistedData

use of org.terasology.persistence.typeHandling.PersistedData 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

PersistedData (org.terasology.persistence.typeHandling.PersistedData)17 PersistedDataArray (org.terasology.persistence.typeHandling.PersistedDataArray)6 Test (org.junit.Test)4 ReplicatedFieldMetadata (org.terasology.entitySystem.metadata.ReplicatedFieldMetadata)4 Serializer (org.terasology.persistence.typeHandling.Serializer)4 ProtobufPersistedData (org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData)4 DeserializationContext (org.terasology.persistence.typeHandling.DeserializationContext)3 FieldMetadata (org.terasology.reflection.metadata.FieldMetadata)3 Map (java.util.Map)2 SerializationContext (org.terasology.persistence.typeHandling.SerializationContext)2 SimpleTypeHandler (org.terasology.persistence.typeHandling.SimpleTypeHandler)2 TypeHandler (org.terasology.persistence.typeHandling.TypeHandler)2 EntityData (org.terasology.protobuf.EntityData)2 Color (org.terasology.rendering.nui.Color)2 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 Component (org.terasology.entitySystem.Component)1 EntityRef (org.terasology.entitySystem.entity.EntityRef)1 Vector3i (org.terasology.math.geom.Vector3i)1 NetworkComponent (org.terasology.network.NetworkComponent)1