mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2025-12-17 01:15:52 +01:00
Refactor Json helper functions to JsonUtils
This commit is contained in:
77
src/main/java/emu/grasscutter/utils/JsonUtils.java
Normal file
77
src/main/java/emu/grasscutter/utils/JsonUtils.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,7 +130,7 @@ public final class Language {
|
||||
languageCode = description.getLanguageCode();
|
||||
|
||||
try {
|
||||
languageData = Utils.jsonDecode(Utils.readFromInputStream(description.getLanguageFile()), JsonObject.class);
|
||||
languageData = JsonUtils.decode(Utils.readFromInputStream(description.getLanguageFile()), JsonObject.class);
|
||||
} catch (Exception exception) {
|
||||
Grasscutter.getLogger().warn("Failed to load language file: " + description.getLanguageCode(), exception);
|
||||
}
|
||||
|
||||
@@ -20,25 +20,12 @@ import it.unimi.dsi.fastutil.ints.IntList;
|
||||
|
||||
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 static emu.grasscutter.utils.Language.translate;
|
||||
|
||||
@SuppressWarnings({"UnusedReturnValue", "BooleanMethodIsAlwaysInverted"})
|
||||
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 int randomRange(int min, int max) {
|
||||
@@ -171,7 +158,7 @@ public final class Utils {
|
||||
* @param object The object to log.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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.
|
||||
* @param list The list from which to draw the element.
|
||||
|
||||
Reference in New Issue
Block a user