Skip to content

Commit

Permalink
Split up JSON and other serialization code
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Sep 18, 2024
1 parent c02688a commit e126b3e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,50 +1,32 @@
package dynamic_fps.impl.config;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import dynamic_fps.impl.Constants;
import dynamic_fps.impl.service.Platform;
import dynamic_fps.impl.util.JsonUtil;
import dynamic_fps.impl.util.Logging;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Locale;
import java.util.Map;

public class Serialization {
private static final Gson GSON = new GsonBuilder()
.setLenient()
.serializeNulls()
.setPrettyPrinting()
.enableComplexMapKeySerialization()
.registerTypeHierarchyAdapter(Enum.class, new EnumSerializer<>())
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();

private static final String CONFIG_FILE = Constants.MOD_ID + ".json";

public static void save() {
JsonObject config = (JsonObject) GSON.toJsonTree(DynamicFPSConfig.INSTANCE);
JsonObject parent = (JsonObject) GSON.toJsonTree(DynamicFPSConfig.DEFAULTS);
JsonObject config = (JsonObject) JsonUtil.toJsonTree(DynamicFPSConfig.INSTANCE);
JsonObject parent = (JsonObject) JsonUtil.toJsonTree(DynamicFPSConfig.DEFAULTS);

String data = GSON.toJson(removeUnchangedFields(config, parent));
String data = JsonUtil.toJson(removeUnchangedFields(config, parent));

try {
write(data);
Expand Down Expand Up @@ -131,7 +113,7 @@ public static DynamicFPSConfig loadPersonalized() {
}

upgradeConfig(root);
return GSON.fromJson(root, DynamicFPSConfig.class); // Ignores regular constructor!
return JsonUtil.fromJson(root, DynamicFPSConfig.class); // Ignores regular constructor!
}

public static DynamicFPSConfig loadDefault() {
Expand All @@ -147,7 +129,7 @@ public static DynamicFPSConfig loadDefault() {
throw new RuntimeException("Failed to load Dynamic FPS config.", e);
}

return GSON.fromJson(new String(data, StandardCharsets.UTF_8), DynamicFPSConfig.class);
return JsonUtil.fromJson(new String(data, StandardCharsets.UTF_8), DynamicFPSConfig.class);
}

private static void upgradeConfig(JsonObject config) {
Expand All @@ -158,7 +140,7 @@ private static void upgradeConfig(JsonObject config) {
upgradeIdleConfig(config);

// version agnostic
addMissingFields(config, (JsonObject) GSON.toJsonTree(DynamicFPSConfig.DEFAULTS));
addMissingFields(config, (JsonObject) JsonUtil.toJsonTree(DynamicFPSConfig.DEFAULTS));
}

private static void addMissingFields(JsonObject config, JsonObject parent) {
Expand Down Expand Up @@ -253,21 +235,4 @@ private static void upgradeIdleConfig(JsonObject root) {

return root.getAsJsonObject("states");
}

private static final class EnumSerializer<T extends Enum<T>> implements JsonSerializer<T>, JsonDeserializer<T> {
@Override
public JsonElement serialize(T instance, Type type, JsonSerializationContext context) {
return new JsonPrimitive(instance.toString().toLowerCase(Locale.ROOT));
}

@Override
public T deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
try {
Class<T> class_ = (Class<T>) Class.forName(type.getTypeName());
return Enum.valueOf(class_, element.getAsString().toUpperCase(Locale.ROOT));
} catch (ClassNotFoundException | IllegalArgumentException e) {
throw new JsonParseException(e);
}
}
}
}
54 changes: 54 additions & 0 deletions platforms/common/src/main/java/dynamic_fps/impl/util/JsonUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package dynamic_fps.impl.util;

import com.google.gson.*;

import java.lang.reflect.Type;
import java.util.Locale;

public class JsonUtil {
private static final Gson GSON = new GsonBuilder()
.setLenient()
.serializeNulls()
.setPrettyPrinting()
.enableComplexMapKeySerialization()
.registerTypeHierarchyAdapter(Enum.class, new EnumSerializer<>())
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();

public static String toJson(Object object) {
return GSON.toJson(object);
}

public static String toJson(JsonElement element) {
return GSON.toJson(element);
}

public static JsonElement toJsonTree(Object object) {
return GSON.toJsonTree(object);
}

public static <T> T fromJson(String data, Class<T> type) {
return GSON.fromJson(data, type);
}

public static <T> T fromJson(JsonElement data, Class<T> type) {
return GSON.fromJson(data, type);
}

private static final class EnumSerializer<T extends Enum<T>> implements JsonSerializer<T>, JsonDeserializer<T> {
@Override
public JsonElement serialize(T instance, Type type, JsonSerializationContext context) {
return new JsonPrimitive(instance.toString().toLowerCase(Locale.ROOT));
}

@Override
public T deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException {
try {
Class<T> class_ = (Class<T>) Class.forName(type.getTypeName());
return Enum.valueOf(class_, element.getAsString().toUpperCase(Locale.ROOT));
} catch (ClassNotFoundException | IllegalArgumentException e) {
throw new JsonParseException(e);
}
}
}
}

0 comments on commit e126b3e

Please sign in to comment.