Skip to content

Commit

Permalink
refactor: extract Serializer's JsonMapper into an explicit adapter class
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanGerlek committed Dec 4, 2024
1 parent 7e2a8fb commit dc70b35
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
25 changes: 25 additions & 0 deletions src/main/java/edu/byu/cs/server/SerializerAdapter.java
Original file line number Diff line number Diff line change
@@ -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> 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);
}
}
3 changes: 1 addition & 2 deletions src/main/java/edu/byu/cs/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
16 changes: 0 additions & 16 deletions src/main/java/edu/byu/cs/util/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,20 +23,6 @@ public class Serializer {
.registerTypeAdapter(CanvasRubricAssessment.class, new RubricAssessmentAdapter())
.create();

public static final JsonMapper jsonMapper = new JsonMapper() {
@NotNull
@Override
public <T> 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);
Expand Down

0 comments on commit dc70b35

Please sign in to comment.