feature(TypeHandlerLibary): remove `GsonSerializer`

develop
DarkWeird 2020-12-16 17:02:24 +03:00
parent 4c14011483
commit 3a938c1dd4
5 changed files with 118 additions and 339 deletions

View File

@ -4,8 +4,10 @@ package org.terasology.persistence.serializers;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import org.junit.jupiter.api.Test;
import org.terasology.ModuleEnvironmentTest;
import org.terasology.engine.TerasologyConstants;
import org.terasology.math.geom.Vector3f;
import org.terasology.naming.Name;
import org.terasology.nui.Color;
@ -13,16 +15,21 @@ import org.terasology.persistence.ModuleContext;
import org.terasology.persistence.typeHandling.TypeHandlerLibrary;
import org.terasology.persistence.typeHandling.TypeHandlerLibraryImpl;
import org.terasology.persistence.typeHandling.annotations.SerializedName;
import org.terasology.persistence.typeHandling.gson.GsonPersistedDataReader;
import org.terasology.persistence.typeHandling.gson.GsonPersistedDataSerializer;
import org.terasology.persistence.typeHandling.gson.GsonPersistedDataWriter;
import org.terasology.reflection.TypeInfo;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TypeSerializerTest extends ModuleEnvironmentTest {
class TypeSerializerTest extends ModuleEnvironmentTest {
private static final SomeClass<Integer> INSTANCE = new SomeClass<>(0xdeadbeef);
private static final String INSTANCE_JSON = "{\"generic-t\":-559038737,\"list\":[50,51,-52,-53],\"animals\":[{\"class\":\"org.terasology.persistence.serializers.TypeSerializerTest$Dog\",\"tailPosition\":[3.15,54.51,-0.001],\"headPosition\":[10.0,30.0,-0.001],\"data\":{\"class\":\"java.lang.Integer\",\"content\":1}},{\"class\":\"org.terasology.persistence.serializers.TypeSerializerTest$Cheetah\",\"spotColor\":[255,0,255,255],\"data\":{\"class\":\"java.lang.Integer\",\"content\":2}}]}";
@ -36,7 +43,7 @@ public class TypeSerializerTest extends ModuleEnvironmentTest {
private TypeHandlerLibrary typeHandlerLibrary;
private ProtobufSerializer protobufSerializer;
private GsonSerializer gsonSerializer;
private Serializer gsonSerializer;
@Override
public void setup() {
@ -45,27 +52,39 @@ public class TypeSerializerTest extends ModuleEnvironmentTest {
typeHandlerLibrary = TypeHandlerLibraryImpl.forModuleEnvironment(moduleManager, typeRegistry);
protobufSerializer = new ProtobufSerializer(typeHandlerLibrary);
gsonSerializer = new GsonSerializer(typeHandlerLibrary);
Gson gson = new Gson();
gsonSerializer = new Serializer<>(typeHandlerLibrary,
new GsonPersistedDataSerializer(),
new GsonPersistedDataWriter(gson),
new GsonPersistedDataReader(gson)
);
}
@Test
public void testJsonSerialize() {
String serializedJson = gsonSerializer.toJson(INSTANCE, new TypeInfo<SomeClass<Integer>>() {
void testJsonSerialize() {
Optional<byte[]> serialize = gsonSerializer.serialize(INSTANCE, new TypeInfo<SomeClass<Integer>>() {
});
assertTrue(serialize.isPresent());
String serializedJson = new String(serialize.get(), TerasologyConstants.CHARSET);
assertEquals(INSTANCE_JSON, serializedJson);
}
@Test
public void testDeserialize() {
void testDeserialize() {
Optional<SomeClass<Integer>> deserialize = gsonSerializer.deserialize(new TypeInfo<SomeClass<Integer>>() {
}, INSTANCE_JSON.getBytes(TerasologyConstants.CHARSET));
assertTrue(deserialize.isPresent());
SomeClass<Integer> deserialized =
gsonSerializer.fromJson(INSTANCE_JSON, new TypeInfo<SomeClass<Integer>>() {
});
deserialize.get();
assertEquals(INSTANCE, deserialized);
}
@Test
public void testSerializeDeserialize() throws IOException {
void testSerializeDeserialize() throws IOException {
byte[] bytes = protobufSerializer.toBytes(INSTANCE, new TypeInfo<SomeClass<Integer>>() {
});

View File

@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
package org.terasology.persistence.serializers;
import com.google.gson.Gson;
import org.joml.Vector2fc;
import org.joml.Vector3fc;
import org.joml.Vector4fc;
@ -14,36 +15,20 @@ import org.terasology.naming.Name;
import org.terasology.persistence.ModuleContext;
import org.terasology.persistence.typeHandling.TypeHandlerLibrary;
import org.terasology.persistence.typeHandling.TypeHandlerLibraryImpl;
import org.terasology.persistence.typeHandling.gson.GsonPersistedDataReader;
import org.terasology.persistence.typeHandling.gson.GsonPersistedDataSerializer;
import org.terasology.persistence.typeHandling.gson.GsonPersistedDataWriter;
import org.terasology.reflection.TypeInfo;
import org.terasology.testUtil.TeraAssert;
import java.io.IOException;
public class VectorTypeSerializerTest extends ModuleEnvironmentTest {
static class TestObject{
public Vector3f v1;
public Vector2f v2;
public Vector4f v3;
public org.joml.Vector3f v11;
public org.joml.Vector2f v22;
public org.joml.Vector4f v33;
}
static class TestObject1 {
public org.joml.Vector3f v1;
public org.joml.Vector2f v2;
public org.joml.Vector4f v3;
}
static class TestObject2 {
public Vector3fc v1;
public Vector4fc v2;
public Vector2fc v3;
}
class VectorTypeSerializerTest extends ModuleEnvironmentTest {
private TypeHandlerLibrary typeHandlerLibrary;
private ProtobufSerializer protobufSerializer;
private GsonSerializer gsonSerializer;
private Serializer<?> gsonSerializer;
private Gson gson = new Gson();
@Override
public void setup() {
@ -52,49 +37,31 @@ public class VectorTypeSerializerTest extends ModuleEnvironmentTest {
typeHandlerLibrary = TypeHandlerLibraryImpl.forModuleEnvironment(moduleManager, typeRegistry);
protobufSerializer = new ProtobufSerializer(typeHandlerLibrary);
gsonSerializer = new GsonSerializer(typeHandlerLibrary);
gsonSerializer = new Serializer<>(typeHandlerLibrary,
new GsonPersistedDataSerializer(),
new GsonPersistedDataWriter(gson),
new GsonPersistedDataReader(gson)
);
}
@Test
public void testSerializationConstant() throws IOException {
void testSerializationConstant() throws IOException {
TestObject2 a = new TestObject2();
a.v1 = new org.joml.Vector3f(1.0f, 2.0f, 3.0f);
a.v2 = new org.joml.Vector4f(1.0f, 2.0f, 3.0f, 5.0f);
a.v3 = new org.joml.Vector2f(1.0f, 2.0f);
String data = gsonSerializer.toJson(a, new TypeInfo<TestObject2>() {
});
byte[] data = gsonSerializer.serialize(a, new TypeInfo<TestObject2>() {
}).get();
TestObject2 o = gsonSerializer.fromJson(data, new TypeInfo<TestObject2>() {
});
TestObject2 o = gsonSerializer.deserialize(new TypeInfo<TestObject2>() {
},data).get();
TeraAssert.assertEquals(o.v1, new org.joml.Vector3f(1.0f, 2.0f, 3.0f), .00001f);
TeraAssert.assertEquals(o.v2, new org.joml.Vector4f(1.0f, 2.0f, 3.0f, 5.0f), .00001f);
TeraAssert.assertEquals(o.v3, new org.joml.Vector2f(1.0f, 2.0f), .00001f);
}
@Test
public void testJsonSerializeRemapped() throws IOException {
TestObject a = new TestObject();
a.v1 = new Vector3f(11.5f, 13.15f, 3);
a.v2 = new Vector2f(12, 13f);
a.v3 = new Vector4f(12, 12.2f, 3f, 15.5f);
a.v11 = new org.joml.Vector3f(11.5f, 13.15f, 3);
a.v22 = new org.joml.Vector2f(12, 13f);
a.v33 = new org.joml.Vector4f(12, 12.2f, 3f, 15.5f);
String data = gsonSerializer.toJson(a, new TypeInfo<TestObject>() {
});
TestObject1 o = gsonSerializer.fromJson(data, new TypeInfo<TestObject1>() {
});
TeraAssert.assertEquals(o.v1, new org.joml.Vector3f(11.5f, 13.15f, 3), .00001f);
TeraAssert.assertEquals(o.v2, new org.joml.Vector2f(12f, 13f), .00001f);
TeraAssert.assertEquals(o.v3, new org.joml.Vector4f(12, 12.2f, 3f, 15.5f), .00001f);
}
@Test
public void testProtobufSerializeRemapped() throws IOException {
void testJsonSerializeRemapped() throws IOException {
TestObject a = new TestObject();
a.v1 = new Vector3f(11.5f, 13.15f, 3);
a.v2 = new Vector2f(12, 13f);
@ -116,7 +83,7 @@ public class VectorTypeSerializerTest extends ModuleEnvironmentTest {
}
@Test
public void testJsonSerialize() throws IOException {
void testProtobufSerializeRemapped() throws IOException {
TestObject a = new TestObject();
a.v1 = new Vector3f(11.5f, 13.15f, 3);
a.v2 = new Vector2f(12, 13f);
@ -125,10 +92,32 @@ public class VectorTypeSerializerTest extends ModuleEnvironmentTest {
a.v22 = new org.joml.Vector2f(12, 13f);
a.v33 = new org.joml.Vector4f(12, 12.2f, 3f, 15.5f);
String data = gsonSerializer.toJson(a, new TypeInfo<TestObject>() {
byte[] data = protobufSerializer.toBytes(a, new TypeInfo<TestObject>() {
});
TestObject o = gsonSerializer.fromJson(data, new TypeInfo<TestObject>() {
TestObject1 o = protobufSerializer.fromBytes(data, new TypeInfo<TestObject1>() {
});
TeraAssert.assertEquals(o.v1, new org.joml.Vector3f(11.5f, 13.15f, 3), .00001f);
TeraAssert.assertEquals(o.v2, new org.joml.Vector2f(12f, 13f), .00001f);
TeraAssert.assertEquals(o.v3, new org.joml.Vector4f(12, 12.2f, 3f, 15.5f), .00001f);
}
@Test
void testJsonSerialize() throws IOException {
TestObject a = new TestObject();
a.v1 = new Vector3f(11.5f, 13.15f, 3);
a.v2 = new Vector2f(12, 13f);
a.v3 = new Vector4f(12, 12.2f, 3f, 15.5f);
a.v11 = new org.joml.Vector3f(11.5f, 13.15f, 3);
a.v22 = new org.joml.Vector2f(12, 13f);
a.v33 = new org.joml.Vector4f(12, 12.2f, 3f, 15.5f);
byte[] data = protobufSerializer.toBytes(a, new TypeInfo<TestObject>() {
});
TestObject o = protobufSerializer.fromBytes(data, new TypeInfo<TestObject>() {
});
TeraAssert.assertEquals(o.v1, new Vector3f(11.5f, 13.15f, 3), .00001f);
@ -141,7 +130,7 @@ public class VectorTypeSerializerTest extends ModuleEnvironmentTest {
}
@Test
public void testProtobufSerialize() throws IOException {
void testProtobufSerialize() throws IOException {
TestObject a = new TestObject();
a.v1 = new Vector3f(11.5f, 13.15f, 3);
a.v2 = new Vector2f(12, 13f);
@ -165,4 +154,26 @@ public class VectorTypeSerializerTest extends ModuleEnvironmentTest {
TeraAssert.assertEquals(o.v33, new org.joml.Vector4f(12, 12.2f, 3f, 15.5f), .00001f);
}
static class TestObject{
public Vector3f v1;
public Vector2f v2;
public Vector4f v3;
public org.joml.Vector3f v11;
public org.joml.Vector2f v22;
public org.joml.Vector4f v33;
}
static class TestObject1 {
public org.joml.Vector3f v1;
public org.joml.Vector2f v2;
public org.joml.Vector4f v3;
}
static class TestObject2 {
public Vector3fc v1;
public Vector4fc v2;
public Vector2fc v3;
}
}

View File

@ -1,261 +0,0 @@
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.persistence.serializers;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import org.terasology.persistence.typeHandling.PersistedData;
import org.terasology.persistence.typeHandling.PersistedDataSerializer;
import org.terasology.persistence.typeHandling.SerializationException;
import org.terasology.persistence.typeHandling.TypeHandlerLibrary;
import org.terasology.persistence.typeHandling.gson.GsonPersistedData;
import org.terasology.persistence.typeHandling.gson.GsonPersistedDataSerializer;
import org.terasology.reflection.TypeInfo;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Optional;
/**
* {@link GsonSerializer} provides the ability to serialize and deserialize objects to and from JSON.
* @deprecated use {@link Serializer} instead.
*/
@Deprecated
public class GsonSerializer {
private final TypeHandlerLibrary typeHandlerLibrary;
private final Gson gson;
private PersistedDataSerializer persistedDataSerializer;
/**
* Constructs a new {@link GsonSerializer} object with the given {@link TypeHandlerLibrary}.
*/
public GsonSerializer(TypeHandlerLibrary typeHandlerLibrary) {
this.typeHandlerLibrary = typeHandlerLibrary;
this.persistedDataSerializer = new GsonPersistedDataSerializer();
this.gson = new Gson();
}
/**
* Serializes the given object to JSON and returns the serialized JSON as a {@link String}.
*
* @param object The object to be serialized.
* @param typeInfo The {@link TypeInfo} specifying the type of the object to be serialized.
* @param <T> The type of the object to be serialized.
* @return The serialized JSON as a {@link String}.
* @throws SerializationException Thrown when serialization fails.
* @see #writeJson(Object, TypeInfo, Writer)
*/
public <T> String toJson(T object, TypeInfo<T> typeInfo) throws SerializationException {
StringWriter writer = new StringWriter();
writeJson(object, typeInfo, writer);
return writer.toString();
}
/**
* Serializes the given object to JSON and writes the serialized JSON object to the
* {@link Writer}.
*
* @param object The object to be serialized.
* @param typeInfo The {@link TypeInfo} specifying the type of the object to be serialized.
* @param writer The {@link Writer} to which the JSON will be written.
* @param <T> The type of the object to be serialized.
* @throws SerializationException Thrown when serialization fails.
*/
public <T> void writeJson(T object, TypeInfo<T> typeInfo, Writer writer) throws SerializationException {
Optional<PersistedData> serialized = this.serialize(object, typeInfo);
if (!serialized.isPresent()) {
throw new SerializationException("Could not find a TypeHandler for the type " + typeInfo);
}
GsonPersistedData persistedData = (GsonPersistedData) serialized.get();
try {
gson.toJson(persistedData.getElement(), writer);
} catch (JsonIOException e) {
throw new SerializationException("Could not write JSON to writer", e);
}
}
/**
* Serializes the given object to JSON and writes the serialized JSON object to the
* {@link OutputStream}.
*
* @param object The object to be serialized.
* @param typeInfo The {@link TypeInfo} specifying the type of the object to be serialized.
* @param stream The {@link OutputStream} to which the JSON will be written.
* @param <T> The type of the object to be serialized.
* @throws SerializationException Thrown when serialization fails.
* @throws IOException Thrown if there is an error in writing to the
* {@link OutputStream}.
* @see #writeJson(Object, TypeInfo, Writer)
*/
public <T> void writeJson(T object, TypeInfo<T> typeInfo, OutputStream stream)
throws IOException, SerializationException {
try (Writer writer = new BufferedWriter(new OutputStreamWriter(stream))) {
writeJson(object, typeInfo, writer);
}
}
/**
* Serializes the given object to JSON and writes the serialized JSON object to the
* {@link File}.
*
* @param object The object to be serialized.
* @param typeInfo The {@link TypeInfo} specifying the type of the object to be serialized.
* @param file The {@link File} that the JSON will be written to.
* @param <T> The type of the object to be serialized.
* @throws IOException Thrown if there is an issue writing to the file.
* @throws SerializationException Thrown when serialization fails.
* @see #writeJson(Object, TypeInfo, Writer)
*/
public <T> void writeJson(T object, TypeInfo<T> typeInfo, File file)
throws IOException, SerializationException {
try (Writer writer = new BufferedWriter(new FileWriter(file))) {
writeJson(object, typeInfo, writer);
}
}
/**
* Serializes the given object to JSON and writes the serialized JSON object to the
* file with the given file name.
*
* @param object The object to be serialized.
* @param typeInfo The {@link TypeInfo} specifying the type of the object to be serialized.
* @param fileName The name of the file that the JSON will be written to.
* @param <T> The type of the object to be serialized.
* @throws IOException Thrown if there is an issue writing to the file.
* @throws SerializationException Thrown when serialization fails.
* @see #writeJson(Object, TypeInfo, File)
*/
public <T> void writeJson(T object, TypeInfo<T> typeInfo, String fileName)
throws IOException, SerializationException {
writeJson(object, typeInfo, new File(fileName));
}
/**
* Deserializes an object of type {@link T} from the JSON in the {@link Reader}.
*
* @param reader The {@link Reader} that contains the JSON to be deserialized.
* @param typeInfo The {@link TypeInfo} specifying the type to deserialize the object as.
* @param <T> The type to deserialize the object as.
* @return The deserialized object of type {@link T}.
* @throws SerializationException Thrown if the deserialization fails.
*/
public <T> T fromJson(Reader reader, TypeInfo<T> typeInfo) throws SerializationException {
JsonElement jsonElement;
try {
jsonElement = gson.fromJson(reader, JsonElement.class);
} catch (JsonIOException | JsonSyntaxException e) {
throw new SerializationException("Could not read JSON from reader", e);
}
Optional<T> deserialized = deserialize(new GsonPersistedData(jsonElement), typeInfo);
if (!deserialized.isPresent()) {
throw new SerializationException("Could not deserialize object of type " + typeInfo);
}
return deserialized.get();
}
/**
* Deserializes an object of type {@link T} from the JSON in the {@link InputStream}.
*
* @param stream The {@link InputStream} containing the serialized JSON.
* @param typeInfo The {@link TypeInfo} specifying the type to deserialize the object as.
* @param <T> The type to deserialize the object as.
* @return The deserialized object of type {@link T}.
* @throws IOException Thrown if there is an issue reading from the {@link InputStream}.
* @throws SerializationException Thrown if the deserialization fails.
* @see #fromJson(Reader, TypeInfo)
*/
public <T> T fromJson(InputStream stream, TypeInfo<T> typeInfo) throws IOException, SerializationException {
try (Reader reader = new InputStreamReader(stream)) {
return fromJson(reader, typeInfo);
}
}
/**
* Deserializes an object of type {@link T} from the JSON in the {@link File}.
*
* @param file The file containing the JSON to be deserialized.
* @param typeInfo The {@link TypeInfo} specifying the type to deserialize the object as.
* @param <T> The type to deserialize the object as.
* @return The deserialized object of type {@link T}.
* @throws IOException Thrown if there is an issue reading from the {@link File}.
* @throws SerializationException Thrown if the deserialization fails.
* @see #fromJson(Reader, TypeInfo)
*/
public <T> T fromJson(File file, TypeInfo<T> typeInfo) throws IOException, SerializationException {
try (Reader reader = new FileReader(file)) {
return fromJson(reader, typeInfo);
}
}
/**
* Deserializes an object of type {@link T} from the JSON in the {@link File}.
*
* @param json The JSON to be deserialized
* @param typeInfo The {@link TypeInfo} specifying the type to deserialize the object as.
* @param <T> The type to deserialize the object as.
* @return The deserialized object of type {@link T}.
* @throws SerializationException Thrown if the deserialization fails.
* @see #fromJson(Reader, TypeInfo)
*/
public <T> T fromJson(String json, TypeInfo<T> typeInfo) throws SerializationException {
try (StringReader reader = new StringReader(json)) {
return fromJson(reader, typeInfo);
}
}
/**
* Serializes the given object to a {@link PersistedData} using the stored {@link #persistedDataSerializer} by
* loading a {@link org.terasology.persistence.typeHandling.TypeHandler TypeHandler} from the {@link
* #typeHandlerLibrary}.
*
* @param object The object to serialize.
* @param typeInfo A {@link TypeInfo} specifying the type of the object to serialize.
* @param <T> The type of the object to serialize.
* @return A {@link PersistedData}, if the serialization was successful. Serialization usually fails only because an
* appropriate type handler could not be found for the given type.
*/
private <T> Optional<PersistedData> serialize(T object, TypeInfo<T> typeInfo) {
return typeHandlerLibrary.getTypeHandler(typeInfo)
.map(typeHandler -> typeHandler.serialize(object, persistedDataSerializer));
}
/**
* Deserializes an object of the given type from a {@link PersistedData} using the stored {@link
* #persistedDataSerializer} by loading a {@link org.terasology.persistence.typeHandling.TypeHandler TypeHandler}
* from the {@link #typeHandlerLibrary}.
*
* @param data The {@link PersistedData} containing the serialized representation of the object.
* @param typeInfo The {@link TypeInfo} specifying the type to deserialize the object as.
* @param <T> The type to deserialize the object as.
* @return The deserialized object of type {@link T}, if the deserialization was successful. Deserialization usually
* fails when an appropriate type handler could not be found for the type {@link T} <i>or</i> if the
* serialized object representation in {@code data} does not represent an object of type {@link T}.
*/
private <T> Optional<T> deserialize(PersistedData data, TypeInfo<T> typeInfo) {
return typeHandlerLibrary.getTypeHandler(typeInfo).flatMap(typeHandler -> typeHandler.deserialize(data));
}
}

View File

@ -2,21 +2,26 @@
// SPDX-License-Identifier: Apache-2.0
package org.terasology.recording;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.engine.module.ModuleManager;
import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.entitySystem.entity.internal.EngineEntityManager;
import org.terasology.persistence.serializers.GsonSerializer;
import org.terasology.persistence.serializers.Serializer;
import org.terasology.persistence.typeHandling.SerializationException;
import org.terasology.persistence.typeHandling.TypeHandlerLibrary;
import org.terasology.persistence.typeHandling.TypeHandlerLibraryImpl;
import org.terasology.persistence.typeHandling.extensionTypes.EntityRefTypeHandler;
import org.terasology.persistence.typeHandling.gson.GsonPersistedDataReader;
import org.terasology.persistence.typeHandling.gson.GsonPersistedDataSerializer;
import org.terasology.persistence.typeHandling.gson.GsonPersistedDataWriter;
import org.terasology.reflection.TypeInfo;
import org.terasology.reflection.TypeRegistry;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@ -27,14 +32,18 @@ import java.util.List;
class RecordedEventSerializer {
private static final Logger logger = LoggerFactory.getLogger(RecordedEventSerializer.class);
private GsonSerializer gsonSerializer;
private final Serializer<?> serializer;
public RecordedEventSerializer(EntityManager entityManager, ModuleManager moduleManager, TypeRegistry typeRegistry) {
TypeHandlerLibrary typeHandlerLibrary = TypeHandlerLibraryImpl.forModuleEnvironment(moduleManager, typeRegistry);
typeHandlerLibrary.addTypeHandler(EntityRef.class, new EntityRefTypeHandler((EngineEntityManager) entityManager));
gsonSerializer = new GsonSerializer(typeHandlerLibrary);
Gson gson = new Gson();
serializer = new Serializer<>(
typeHandlerLibrary,
new GsonPersistedDataSerializer(),
new GsonPersistedDataWriter(gson),
new GsonPersistedDataReader(gson)
);
}
/**
@ -44,8 +53,9 @@ class RecordedEventSerializer {
* @param filePath path where the data should be saved.
*/
public void serializeRecordedEvents(List<RecordedEvent> events, String filePath) {
try {
gsonSerializer.writeJson(events, new TypeInfo<List<RecordedEvent>>() {}, filePath);
try (FileOutputStream fileOutputStream = new FileOutputStream(filePath)) {
serializer.serialize(events, new TypeInfo<List<RecordedEvent>>() {
}, fileOutputStream);
} catch (IOException | SerializationException e) {
logger.error("Error while serializing recorded events", e);
}
@ -59,9 +69,9 @@ class RecordedEventSerializer {
public List<RecordedEvent> deserializeRecordedEvents(String filePath) {
List<RecordedEvent> events = new ArrayList<>();
try {
try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
List<RecordedEvent> recordedEvents =
gsonSerializer.fromJson(new File(filePath), new TypeInfo<List<RecordedEvent>>() {});
serializer.deserialize(new TypeInfo<List<RecordedEvent>>() {}, fileInputStream).get();
events.addAll(recordedEvents);
} catch (SerializationException | IOException e) {
logger.error("Error while serializing recorded events", e);

View File

@ -26,10 +26,10 @@ public final class Serializer<D extends PersistedData> {
private static final Logger logger = LoggerFactory.getLogger(Serializer.class);
protected final TypeHandlerLibrary typeHandlerLibrary;
protected final PersistedDataSerializer persistedDataSerializer;
protected final PersistedDataWriter<D> writer;
protected final PersistedDataReader<D> reader;
private final TypeHandlerLibrary typeHandlerLibrary;
private final PersistedDataSerializer persistedDataSerializer;
private final PersistedDataWriter<D> writer;
private final PersistedDataReader<D> reader;
public Serializer(TypeHandlerLibrary typeHandlerLibrary, PersistedDataSerializer persistedDataSerializer,
PersistedDataWriter<D> writer, PersistedDataReader<D> reader) {