From dc70b35d06cbf85d1da51cd75648de941f70161a Mon Sep 17 00:00:00 2001 From: ThanGerlek Date: Wed, 4 Dec 2024 20:46:27 +0000 Subject: [PATCH] refactor: extract Serializer's JsonMapper into an explicit adapter class --- .../edu/byu/cs/server/SerializerAdapter.java | 25 +++++++++++++++++++ src/main/java/edu/byu/cs/server/Server.java | 3 +-- src/main/java/edu/byu/cs/util/Serializer.java | 16 ------------ 3 files changed, 26 insertions(+), 18 deletions(-) create mode 100644 src/main/java/edu/byu/cs/server/SerializerAdapter.java diff --git a/src/main/java/edu/byu/cs/server/SerializerAdapter.java b/src/main/java/edu/byu/cs/server/SerializerAdapter.java new file mode 100644 index 00000000..ef91855d --- /dev/null +++ b/src/main/java/edu/byu/cs/server/SerializerAdapter.java @@ -0,0 +1,25 @@ +package edu.byu.cs.server; + +import edu.byu.cs.util.Serializer; +import io.javalin.json.JsonMapper; +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.Type; + +/** + * A Serializer class adapter which implements Javalin's JsonMapper interface. + * Allows Javalin's built-in serialization to use Serializer. + */ +public class SerializerAdapter implements JsonMapper { + @NotNull + @Override + public T fromJsonString(@NotNull String json, @NotNull Type targetType) { + return Serializer.deserialize(json, targetType); + } + + @NotNull + @Override + public String toJsonString(@NotNull Object obj, @NotNull Type type) { + return Serializer.serialize(obj, type); + } +} diff --git a/src/main/java/edu/byu/cs/server/Server.java b/src/main/java/edu/byu/cs/server/Server.java index b122a7ca..8821834b 100644 --- a/src/main/java/edu/byu/cs/server/Server.java +++ b/src/main/java/edu/byu/cs/server/Server.java @@ -3,7 +3,6 @@ import edu.byu.cs.controller.WebSocketController; import edu.byu.cs.controller.exception.*; import edu.byu.cs.server.endpointprovider.EndpointProvider; -import edu.byu.cs.util.Serializer; import io.javalin.Javalin; import io.javalin.http.ExceptionHandler; import io.javalin.http.HandlerType; @@ -33,7 +32,7 @@ private int setupEndpoints(int port) { app = Javalin.create(config -> { config.staticFiles.add("/frontend/dist"); - config.jsonMapper(Serializer.jsonMapper); + config.jsonMapper(new SerializerAdapter()); config.router.apiBuilder(() -> { before(provider.beforeAll()); diff --git a/src/main/java/edu/byu/cs/util/Serializer.java b/src/main/java/edu/byu/cs/util/Serializer.java index 0906542a..4f984c74 100644 --- a/src/main/java/edu/byu/cs/util/Serializer.java +++ b/src/main/java/edu/byu/cs/util/Serializer.java @@ -6,8 +6,6 @@ import com.google.gson.stream.JsonWriter; import edu.byu.cs.canvas.model.CanvasRubricAssessment; import edu.byu.cs.canvas.model.CanvasRubricItem; -import io.javalin.json.JsonMapper; -import org.jetbrains.annotations.NotNull; import java.io.IOException; import java.io.Reader; @@ -25,20 +23,6 @@ public class Serializer { .registerTypeAdapter(CanvasRubricAssessment.class, new RubricAssessmentAdapter()) .create(); - public static final JsonMapper jsonMapper = new JsonMapper() { - @NotNull - @Override - public T fromJsonString(@NotNull String json, @NotNull Type targetType) { - return deserialize(json, targetType); - } - - @NotNull - @Override - public String toJsonString(@NotNull Object obj, @NotNull Type type) { - return serialize(obj, type); - } - }; - public static String serialize(Object obj) { try { return GSON.toJson(obj);