Refactor Json helper functions to JsonUtils

This commit is contained in:
AnimeGitB 2022-08-11 17:54:59 +09:30 committed by Luke H-W
parent c6323e9759
commit 46223c8976
12 changed files with 117 additions and 105 deletions

View File

@ -12,7 +12,6 @@ import emu.grasscutter.command.PermissionHandler;
import emu.grasscutter.config.ConfigContainer; import emu.grasscutter.config.ConfigContainer;
import emu.grasscutter.data.ResourceLoader; import emu.grasscutter.data.ResourceLoader;
import emu.grasscutter.database.DatabaseManager; import emu.grasscutter.database.DatabaseManager;
import emu.grasscutter.net.packet.PacketOpcodesUtils;
import emu.grasscutter.plugin.PluginManager; import emu.grasscutter.plugin.PluginManager;
import emu.grasscutter.plugin.api.ServerHook; import emu.grasscutter.plugin.api.ServerHook;
import emu.grasscutter.scripts.ScriptLoader; import emu.grasscutter.scripts.ScriptLoader;
@ -27,6 +26,7 @@ import emu.grasscutter.server.http.handlers.GenericHandler;
import emu.grasscutter.server.http.handlers.LogHandler; import emu.grasscutter.server.http.handlers.LogHandler;
import emu.grasscutter.tools.Tools; import emu.grasscutter.tools.Tools;
import emu.grasscutter.utils.Crypto; import emu.grasscutter.utils.Crypto;
import emu.grasscutter.utils.JsonUtils;
import emu.grasscutter.utils.Language; import emu.grasscutter.utils.Language;
import emu.grasscutter.utils.StartupArguments; import emu.grasscutter.utils.StartupArguments;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.Utils;
@ -202,7 +202,7 @@ public final class Grasscutter {
// If the file already exists, we attempt to load it. // If the file already exists, we attempt to load it.
try { try {
config = Utils.loadJsonToClass(configFile.getPath(), ConfigContainer.class); config = JsonUtils.loadToClass(configFile.getPath(), ConfigContainer.class);
} catch (Exception exception) { } catch (Exception exception) {
getLogger().error("There was an error while trying to load the configuration from config.json. Please make sure that there are no syntax errors. If you want to start with a default configuration, delete your existing config.json."); getLogger().error("There was an error while trying to load the configuration from config.json. Please make sure that there are no syntax errors. If you want to start with a default configuration, delete your existing config.json.");
System.exit(1); System.exit(1);
@ -218,7 +218,7 @@ public final class Grasscutter {
if (config == null) config = new ConfigContainer(); if (config == null) config = new ConfigContainer();
try (FileWriter file = new FileWriter(configFile)) { try (FileWriter file = new FileWriter(configFile)) {
file.write(Utils.jsonEncode(config)); file.write(JsonUtils.encode(config));
} catch (IOException ignored) { } catch (IOException ignored) {
Grasscutter.getLogger().error("Unable to write to config file."); Grasscutter.getLogger().error("Unable to write to config file.");
} catch (Exception e) { } catch (Exception e) {
@ -272,7 +272,7 @@ public final class Grasscutter {
@Deprecated(forRemoval = true) @Deprecated(forRemoval = true)
public static Gson getGsonFactory() { public static Gson getGsonFactory() {
return Utils.getGsonFactory(); return JsonUtils.getGsonFactory();
} }
public static HttpServer getHttpServer() { public static HttpServer getHttpServer() {

View File

@ -4,7 +4,7 @@ import com.google.gson.JsonObject;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.Grasscutter.ServerDebugMode; import emu.grasscutter.Grasscutter.ServerDebugMode;
import emu.grasscutter.Grasscutter.ServerRunMode; import emu.grasscutter.Grasscutter.ServerRunMode;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.JsonUtils;
import java.util.Set; import java.util.Set;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -26,7 +26,7 @@ public class ConfigContainer {
*/ */
public static void updateConfig() { public static void updateConfig() {
try { // Check if the server is using a legacy config. try { // Check if the server is using a legacy config.
JsonObject configObject = Utils.loadJsonToClass(Grasscutter.configFile.getPath(), JsonObject.class); JsonObject configObject = JsonUtils.loadToClass(Grasscutter.configFile.getPath(), JsonObject.class);
if (!configObject.has("version")) { if (!configObject.has("version")) {
Grasscutter.getLogger().info("Updating legacy .."); Grasscutter.getLogger().info("Updating legacy ..");
Grasscutter.saveConfig(null); Grasscutter.saveConfig(null);

View File

@ -4,6 +4,7 @@ import emu.grasscutter.Grasscutter;
import emu.grasscutter.server.http.handlers.GachaHandler; import emu.grasscutter.server.http.handlers.GachaHandler;
import emu.grasscutter.tools.Tools; import emu.grasscutter.tools.Tools;
import emu.grasscutter.utils.FileUtils; import emu.grasscutter.utils.FileUtils;
import emu.grasscutter.utils.JsonUtils;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.Utils;
import static emu.grasscutter.config.Configuration.DATA; import static emu.grasscutter.config.Configuration.DATA;
@ -72,19 +73,19 @@ public class DataLoader {
public static <T> T loadClass(String resourcePath, Class<T> classType) throws IOException { public static <T> T loadClass(String resourcePath, Class<T> classType) throws IOException {
try (InputStreamReader reader = loadReader(resourcePath)) { try (InputStreamReader reader = loadReader(resourcePath)) {
return Utils.loadJsonToClass(reader, classType); return JsonUtils.loadToClass(reader, classType);
} }
} }
public static <T> List<T> loadList(String resourcePath, Class<T> classType) throws IOException { public static <T> List<T> loadList(String resourcePath, Class<T> classType) throws IOException {
try (InputStreamReader reader = loadReader(resourcePath)) { try (InputStreamReader reader = loadReader(resourcePath)) {
return Utils.loadJsonToList(reader, classType); return JsonUtils.loadToList(reader, classType);
} }
} }
public static <T1,T2> Map<T1,T2> loadMap(String resourcePath, Class<T1> keyType, Class<T2> valueType) throws IOException { public static <T1,T2> Map<T1,T2> loadMap(String resourcePath, Class<T1> keyType, Class<T2> valueType) throws IOException {
try (InputStreamReader reader = loadReader(resourcePath)) { try (InputStreamReader reader = loadReader(resourcePath)) {
return Utils.loadJsonToMap(reader, keyType, valueType); return JsonUtils.loadToMap(reader, keyType, valueType);
} }
} }

View File

@ -14,6 +14,7 @@ import emu.grasscutter.game.world.SpawnDataEntry;
import emu.grasscutter.game.world.SpawnDataEntry.GridBlockId; import emu.grasscutter.game.world.SpawnDataEntry.GridBlockId;
import emu.grasscutter.game.world.SpawnDataEntry.SpawnGroupEntry; import emu.grasscutter.game.world.SpawnDataEntry.SpawnGroupEntry;
import emu.grasscutter.scripts.SceneIndexManager; import emu.grasscutter.scripts.SceneIndexManager;
import emu.grasscutter.utils.JsonUtils;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.Utils;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import lombok.SneakyThrows; import lombok.SneakyThrows;
@ -119,7 +120,7 @@ public class ResourceLoader {
@SuppressWarnings({"rawtypes", "unchecked"}) @SuppressWarnings({"rawtypes", "unchecked"})
protected static <T> void loadFromResource(Class<T> c, String fileName, Int2ObjectMap map) throws Exception { protected static <T> void loadFromResource(Class<T> c, String fileName, Int2ObjectMap map) throws Exception {
List<T> list = Utils.loadJsonToList(RESOURCE("ExcelBinOutput/" + fileName), c); List<T> list = JsonUtils.loadToList(RESOURCE("ExcelBinOutput/" + fileName), c);
for (T o : list) { for (T o : list) {
GameResource res = (GameResource) o; GameResource res = (GameResource) o;
@ -149,7 +150,7 @@ public class ResourceLoader {
} }
try { try {
config = Utils.loadJsonToClass(file.getPath(), ScenePointConfig.class); config = JsonUtils.loadToClass(file.getPath(), ScenePointConfig.class);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
continue; continue;
@ -163,7 +164,7 @@ public class ResourceLoader {
for (Map.Entry<String, JsonElement> entry : config.points.entrySet()) { for (Map.Entry<String, JsonElement> entry : config.points.entrySet()) {
int id = Integer.parseInt(entry.getKey()); int id = Integer.parseInt(entry.getKey());
String name = sceneId + "_" + entry.getKey(); String name = sceneId + "_" + entry.getKey();
PointData pointData = Utils.jsonDecode(entry.getValue(), PointData.class); PointData pointData = JsonUtils.decode(entry.getValue(), PointData.class);
pointData.setId(id); pointData.setId(id);
GameData.getScenePointIdList().add(id); GameData.getScenePointIdList().add(id);
@ -181,7 +182,7 @@ public class ResourceLoader {
// Read from cached file if exists // Read from cached file if exists
try { try {
embryoList = Utils.loadJsonToList(DATA("AbilityEmbryos.json"), AbilityEmbryoEntry.class); embryoList = JsonUtils.loadToList(DATA("AbilityEmbryos.json"), AbilityEmbryoEntry.class);
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (embryoList == null) { if (embryoList == null) {
@ -208,7 +209,7 @@ public class ResourceLoader {
} }
try { try {
config = Utils.loadJsonToClass(file.getPath(), AvatarConfig.class); config = JsonUtils.loadToClass(file.getPath(), AvatarConfig.class);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
continue; continue;
@ -224,7 +225,7 @@ public class ResourceLoader {
} }
try { try {
GameDepot.setPlayerAbilities(Utils.loadJsonToMap(RESOURCE("BinOutput/AbilityGroup/AbilityGroup_Other_PlayerElementAbility.json"), String.class, AvatarConfig.class)); GameDepot.setPlayerAbilities(JsonUtils.loadToMap(RESOURCE("BinOutput/AbilityGroup/AbilityGroup_Other_PlayerElementAbility.json"), String.class, AvatarConfig.class));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -253,7 +254,7 @@ public class ResourceLoader {
List<AbilityConfigData> abilityConfigList; List<AbilityConfigData> abilityConfigList;
try { try {
abilityConfigList = Utils.loadJsonToList(file.getPath(), AbilityConfigData.class); abilityConfigList = JsonUtils.loadToList(file.getPath(), AbilityConfigData.class);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
continue; continue;
@ -311,7 +312,7 @@ public class ResourceLoader {
// Load spawn entries from file // Load spawn entries from file
try (InputStreamReader reader = DataLoader.loadReader(name)) { try (InputStreamReader reader = DataLoader.loadReader(name)) {
// Add spawns to group if it already exists in our spawn group map // Add spawns to group if it already exists in our spawn group map
spawnEntryMap.addAll(Utils.loadJsonToList(reader, SpawnGroupEntry.class)); spawnEntryMap.addAll(JsonUtils.loadToList(reader, SpawnGroupEntry.class));
} catch (Exception ignored) {} } catch (Exception ignored) {}
} }
@ -342,7 +343,7 @@ public class ResourceLoader {
List<OpenConfigEntry> list = null; List<OpenConfigEntry> list = null;
try { try {
list = Utils.loadJsonToList(DATA("OpenConfig.json"), OpenConfigEntry.class); list = JsonUtils.loadToList(DATA("OpenConfig.json"), OpenConfigEntry.class);
} catch (Exception ignored) {} } catch (Exception ignored) {}
if (list == null) { if (list == null) {
@ -363,7 +364,7 @@ public class ResourceLoader {
Map<String, OpenConfigData[]> config; Map<String, OpenConfigData[]> config;
try { try {
config = Utils.loadJsonToMap(file.getPath(), String.class, OpenConfigData[].class); config = JsonUtils.loadToMap(file.getPath(), String.class, OpenConfigData[].class);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
continue; continue;
@ -400,7 +401,7 @@ public class ResourceLoader {
MainQuestData mainQuest = null; MainQuestData mainQuest = null;
try { try {
mainQuest = Utils.loadJsonToClass(file.getPath(), MainQuestData.class); mainQuest = JsonUtils.loadToClass(file.getPath(), MainQuestData.class);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
continue; continue;
@ -434,7 +435,7 @@ public class ResourceLoader {
for (File file : folder.listFiles()) { for (File file : folder.listFiles()) {
ScriptSceneData sceneData; ScriptSceneData sceneData;
try { try {
sceneData = Utils.loadJsonToClass(file.getPath(), ScriptSceneData.class); sceneData = JsonUtils.loadToClass(file.getPath(), ScriptSceneData.class);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
continue; continue;
@ -457,7 +458,7 @@ public class ResourceLoader {
} }
try { try {
var sceneId = Integer.parseInt(matcher.group(1)); var sceneId = Integer.parseInt(matcher.group(1));
var data = Utils.loadJsonToClass(filename, HomeworldDefaultSaveData.class); var data = JsonUtils.loadToClass(filename, HomeworldDefaultSaveData.class);
GameData.getHomeworldDefaultSaveData().put(sceneId, data); GameData.getHomeworldDefaultSaveData().put(sceneId, data);
} catch (Exception ignored) {} } catch (Exception ignored) {}
}); });
@ -472,7 +473,7 @@ public class ResourceLoader {
return; return;
} }
try { try {
var data = Utils.loadJsonToClass(file.getFileName().toString(), SceneNpcBornData.class); var data = JsonUtils.loadToClass(file.getFileName().toString(), SceneNpcBornData.class);
if (data.getBornPosList() == null || data.getBornPosList().size() == 0) { if (data.getBornPosList() == null || data.getBornPosList().size() == 0) {
return; return;
} }

View File

@ -3,7 +3,6 @@ package emu.grasscutter.game.activity;
import dev.morphia.annotations.Entity; import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id; import dev.morphia.annotations.Id;
import dev.morphia.annotations.Transient; import dev.morphia.annotations.Transient;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.data.GameData; import emu.grasscutter.data.GameData;
import emu.grasscutter.data.common.ItemParamData; import emu.grasscutter.data.common.ItemParamData;
import emu.grasscutter.data.excels.ActivityWatcherData; import emu.grasscutter.data.excels.ActivityWatcherData;
@ -13,7 +12,7 @@ import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.props.ActionReason; import emu.grasscutter.game.props.ActionReason;
import emu.grasscutter.net.proto.ActivityWatcherInfoOuterClass; import emu.grasscutter.net.proto.ActivityWatcherInfoOuterClass;
import emu.grasscutter.server.packet.send.PacketActivityUpdateWatcherNotify; import emu.grasscutter.server.packet.send.PacketActivityUpdateWatcherNotify;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.JsonUtils;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
@ -69,7 +68,7 @@ public class PlayerActivityData {
} }
public void setDetail(Object detail){ public void setDetail(Object detail){
this.detail = Utils.jsonEncode(detail); this.detail = JsonUtils.encode(detail);
} }
public void takeWatcherReward(int watcherId) { public void takeWatcherReward(int watcherId) {

View File

@ -1,6 +1,5 @@
package emu.grasscutter.game.activity.musicgame; package emu.grasscutter.game.activity.musicgame;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.activity.ActivityHandler; import emu.grasscutter.game.activity.ActivityHandler;
import emu.grasscutter.game.activity.GameActivity; import emu.grasscutter.game.activity.GameActivity;
import emu.grasscutter.game.activity.PlayerActivityData; import emu.grasscutter.game.activity.PlayerActivityData;
@ -8,7 +7,7 @@ import emu.grasscutter.game.props.ActivityType;
import emu.grasscutter.net.proto.ActivityInfoOuterClass; import emu.grasscutter.net.proto.ActivityInfoOuterClass;
import emu.grasscutter.net.proto.MusicBriefInfoOuterClass; import emu.grasscutter.net.proto.MusicBriefInfoOuterClass;
import emu.grasscutter.net.proto.MusicGameActivityDetailInfoOuterClass; import emu.grasscutter.net.proto.MusicGameActivityDetailInfoOuterClass;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.JsonUtils;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -48,7 +47,7 @@ public class MusicGameActivityHandler extends ActivityHandler {
playerActivityData.save(); playerActivityData.save();
} }
return Utils.jsonDecode(playerActivityData.getDetail(), MusicGamePlayerData.class); return JsonUtils.decode(playerActivityData.getDetail(), MusicGamePlayerData.class);
} }
public boolean setMusicGameRecord(PlayerActivityData playerActivityData, MusicGamePlayerData.MusicGameRecord newRecord) { public boolean setMusicGameRecord(PlayerActivityData playerActivityData, MusicGamePlayerData.MusicGameRecord newRecord) {

View File

@ -1,10 +1,8 @@
package emu.grasscutter.net.packet; package emu.grasscutter.net.packet;
import java.io.BufferedWriter;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
@ -12,7 +10,7 @@ import java.util.stream.Collectors;
import emu.grasscutter.GameConstants; import emu.grasscutter.GameConstants;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.JsonUtils;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
@ -60,7 +58,7 @@ public class PacketOpcodesUtils {
.filter(e -> e.getIntKey() > 0) .filter(e -> e.getIntKey() > 0)
.collect(Collectors.toMap(Int2ObjectMap.Entry::getIntKey, Int2ObjectMap.Entry::getValue, (k, v) -> v, TreeMap::new)); .collect(Collectors.toMap(Int2ObjectMap.Entry::getIntKey, Int2ObjectMap.Entry::getValue, (k, v) -> v, TreeMap::new));
// Write to file // Write to file
writer.write(Utils.jsonEncode(packetIds)); writer.write(JsonUtils.encode(packetIds));
Grasscutter.getLogger().info("Dumped packet ids."); Grasscutter.getLogger().info("Dumped packet ids.");
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -2,6 +2,7 @@ package emu.grasscutter.plugin;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.server.event.*; import emu.grasscutter.server.event.*;
import emu.grasscutter.utils.JsonUtils;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.Utils;
import lombok.*; import lombok.*;
@ -82,7 +83,7 @@ public final class PluginManager {
InputStreamReader fileReader = new InputStreamReader(configFile.openStream()); InputStreamReader fileReader = new InputStreamReader(configFile.openStream());
// Create a plugin config instance from the config file. // Create a plugin config instance from the config file.
PluginConfig pluginConfig = Utils.loadJsonToClass(fileReader, PluginConfig.class); PluginConfig pluginConfig = JsonUtils.loadToClass(fileReader, PluginConfig.class);
// Check if the plugin config is valid. // Check if the plugin config is valid.
if (!pluginConfig.validate()) { if (!pluginConfig.validate()) {
Grasscutter.getLogger().warn("Plugin " + plugin.getName() + " has an invalid config file."); Grasscutter.getLogger().warn("Plugin " + plugin.getName() + " has an invalid config file.");

View File

@ -7,7 +7,7 @@ import emu.grasscutter.auth.OAuthAuthenticator.ClientType;
import emu.grasscutter.server.http.Router; import emu.grasscutter.server.http.Router;
import emu.grasscutter.server.http.objects.*; import emu.grasscutter.server.http.objects.*;
import emu.grasscutter.server.http.objects.ComboTokenReqJson.LoginTokenData; import emu.grasscutter.server.http.objects.ComboTokenReqJson.LoginTokenData;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.JsonUtils;
import express.Express; import express.Express;
import express.http.Request; import express.http.Request;
import express.http.Response; import express.http.Response;
@ -53,7 +53,7 @@ public final class DispatchHandler implements Router {
private static void clientLogin(Request request, Response response) { private static void clientLogin(Request request, Response response) {
// Parse body data. // Parse body data.
String rawBodyData = request.ctx().body(); String rawBodyData = request.ctx().body();
var bodyData = Utils.jsonDecode(rawBodyData, LoginAccountRequestJson.class); var bodyData = JsonUtils.decode(rawBodyData, LoginAccountRequestJson.class);
// Validate body data. // Validate body data.
if(bodyData == null) if(bodyData == null)
@ -76,7 +76,7 @@ public final class DispatchHandler implements Router {
private static void tokenLogin(Request request, Response response) { private static void tokenLogin(Request request, Response response) {
// Parse body data. // Parse body data.
String rawBodyData = request.ctx().body(); String rawBodyData = request.ctx().body();
var bodyData = Utils.jsonDecode(rawBodyData, LoginTokenRequestJson.class); var bodyData = JsonUtils.decode(rawBodyData, LoginTokenRequestJson.class);
// Validate body data. // Validate body data.
if(bodyData == null) if(bodyData == null)
@ -99,14 +99,14 @@ public final class DispatchHandler implements Router {
private static void sessionKeyLogin(Request request, Response response) { private static void sessionKeyLogin(Request request, Response response) {
// Parse body data. // Parse body data.
String rawBodyData = request.ctx().body(); String rawBodyData = request.ctx().body();
var bodyData = Utils.jsonDecode(rawBodyData, ComboTokenReqJson.class); var bodyData = JsonUtils.decode(rawBodyData, ComboTokenReqJson.class);
// Validate body data. // Validate body data.
if(bodyData == null || bodyData.data == null) if(bodyData == null || bodyData.data == null)
return; return;
// Decode additional body data. // Decode additional body data.
var tokenData = Utils.jsonDecode(bodyData.data, LoginTokenData.class); var tokenData = JsonUtils.decode(bodyData.data, LoginTokenData.class);
// Pass data to authentication handler. // Pass data to authentication handler.
var responseData = Grasscutter.getAuthenticationSystem() var responseData = Grasscutter.getAuthenticationSystem()

View File

@ -0,0 +1,77 @@
package emu.grasscutter.utils;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
public final class JsonUtils {
static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
@Deprecated(forRemoval = true)
public static Gson getGsonFactory() {
return gson;
}
/*
* Encode an object to a JSON string
*/
public static String encode(Object object) {
return gson.toJson(object);
}
public static <T> T decode(JsonElement jsonElement, Class<T> classType) throws JsonSyntaxException {
return gson.fromJson(jsonElement, classType);
}
public static <T> T loadToClass(InputStreamReader fileReader, Class<T> classType) throws IOException {
return gson.fromJson(fileReader, classType);
}
public static <T> T loadToClass(String filename, Class<T> classType) throws IOException {
try (InputStreamReader fileReader = new InputStreamReader(new FileInputStream(Utils.toFilePath(filename)), StandardCharsets.UTF_8)) {
return loadToClass(fileReader, classType);
}
}
public static <T> List<T> loadToList(InputStreamReader fileReader, Class<T> classType) throws IOException {
return gson.fromJson(fileReader, TypeToken.getParameterized(List.class, classType).getType());
}
public static <T> List<T> loadToList(String filename, Class<T> classType) throws IOException {
try (InputStreamReader fileReader = new InputStreamReader(new FileInputStream(Utils.toFilePath(filename)), StandardCharsets.UTF_8)) {
return loadToList(fileReader, classType);
}
}
public static <T1,T2> Map<T1,T2> loadToMap(InputStreamReader fileReader, Class<T1> keyType, Class<T2> valueType) throws IOException {
return gson.fromJson(fileReader, TypeToken.getParameterized(Map.class, keyType, valueType).getType());
}
public static <T1,T2> Map<T1,T2> loadToMap(String filename, Class<T1> keyType, Class<T2> valueType) throws IOException {
try (InputStreamReader fileReader = new InputStreamReader(new FileInputStream(Utils.toFilePath(filename)), StandardCharsets.UTF_8)) {
return loadToMap(fileReader, keyType, valueType);
}
}
/**
* Safely JSON decodes a given string.
* @param jsonData The JSON-encoded data.
* @return JSON decoded data, or null if an exception occurred.
*/
public static <T> T decode(String jsonData, Class<T> classType) {
try {
return gson.fromJson(jsonData, classType);
} catch (Exception ignored) {
return null;
}
}
}

View File

@ -130,7 +130,7 @@ public final class Language {
languageCode = description.getLanguageCode(); languageCode = description.getLanguageCode();
try { try {
languageData = Utils.jsonDecode(Utils.readFromInputStream(description.getLanguageFile()), JsonObject.class); languageData = JsonUtils.decode(Utils.readFromInputStream(description.getLanguageFile()), JsonObject.class);
} catch (Exception exception) { } catch (Exception exception) {
Grasscutter.getLogger().warn("Failed to load language file: " + description.getLanguageCode(), exception); Grasscutter.getLogger().warn("Failed to load language file: " + description.getLanguageCode(), exception);
} }

View File

@ -20,25 +20,12 @@ import it.unimi.dsi.fastutil.ints.IntList;
import org.slf4j.Logger; import org.slf4j.Logger;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import static emu.grasscutter.utils.Language.translate; import static emu.grasscutter.utils.Language.translate;
@SuppressWarnings({"UnusedReturnValue", "BooleanMethodIsAlwaysInverted"}) @SuppressWarnings({"UnusedReturnValue", "BooleanMethodIsAlwaysInverted"})
public final class Utils { public final class Utils {
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
@Deprecated(forRemoval = true)
public static Gson getGsonFactory() {
return gson;
}
public static final Random random = new Random(); public static final Random random = new Random();
public static int randomRange(int min, int max) { public static int randomRange(int min, int max) {
@ -171,7 +158,7 @@ public final class Utils {
* @param object The object to log. * @param object The object to log.
*/ */
public static void logObject(Object object) { public static void logObject(Object object) {
Grasscutter.getLogger().info(jsonEncode(object)); Grasscutter.getLogger().info(JsonUtils.encode(object));
} }
/** /**
@ -369,57 +356,6 @@ public final class Utils {
return Base64.getDecoder().decode(toDecode); return Base64.getDecoder().decode(toDecode);
} }
/*
* Encode an object to a JSON string
*/
public static String jsonEncode(Object object) {
return gson.toJson(object);
}
public static <T> T jsonDecode(JsonElement jsonElement, Class<T> classType) throws JsonSyntaxException {
return gson.fromJson(jsonElement, classType);
}
public static <T> T loadJsonToClass(InputStreamReader fileReader, Class<T> classType) throws IOException {
return gson.fromJson(fileReader, classType);
}
public static <T> T loadJsonToClass(String filename, Class<T> classType) throws IOException {
try (InputStreamReader fileReader = new InputStreamReader(new FileInputStream(Utils.toFilePath(filename)), StandardCharsets.UTF_8)) {
return loadJsonToClass(fileReader, classType);
}
}
public static <T> List<T> loadJsonToList(InputStreamReader fileReader, Class<T> classType) throws IOException {
return gson.fromJson(fileReader, TypeToken.getParameterized(List.class, classType).getType());
}
public static <T> List<T> loadJsonToList(String filename, Class<T> classType) throws IOException {
try (InputStreamReader fileReader = new InputStreamReader(new FileInputStream(Utils.toFilePath(filename)), StandardCharsets.UTF_8)) {
return loadJsonToList(fileReader, classType);
}
}
public static <T1,T2> Map<T1,T2> loadJsonToMap(InputStreamReader fileReader, Class<T1> keyType, Class<T2> valueType) throws IOException {
return gson.fromJson(fileReader, TypeToken.getParameterized(Map.class, keyType, valueType).getType());
}
public static <T1,T2> Map<T1,T2> loadJsonToMap(String filename, Class<T1> keyType, Class<T2> valueType) throws IOException {
try (InputStreamReader fileReader = new InputStreamReader(new FileInputStream(Utils.toFilePath(filename)), StandardCharsets.UTF_8)) {
return loadJsonToMap(fileReader, keyType, valueType);
}
}
/**
* Safely JSON decodes a given string.
* @param jsonData The JSON-encoded data.
* @return JSON decoded data, or null if an exception occurred.
*/
public static <T> T jsonDecode(String jsonData, Class<T> classType) {
try {
return gson.fromJson(jsonData, classType);
} catch (Exception ignored) {
return null;
}
}
/*** /***
* Draws a random element from the given list, following the given probability distribution, if given. * Draws a random element from the given list, following the given probability distribution, if given.
* @param list The list from which to draw the element. * @param list The list from which to draw the element.