JsonUtils.java 4.35 KB
Newer Older
1
2
3
4
5
package emu.grasscutter.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
6
import java.io.Reader;
Luke H-W's avatar
Luke H-W committed
7
import java.lang.reflect.Type;
8
import java.nio.charset.StandardCharsets;
9
10
import java.nio.file.Files;
import java.nio.file.Path;
11
12
13
14
15
16
17
18
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;
AnimeGitB's avatar
AnimeGitB committed
19
20

import emu.grasscutter.data.common.DynamicFloat;
AnimeGitB's avatar
AnimeGitB committed
21
import emu.grasscutter.utils.JsonAdapters.*;
Luke H-W's avatar
Luke H-W committed
22

AnimeGitB's avatar
AnimeGitB committed
23
import it.unimi.dsi.fastutil.ints.IntList;
24
25

public final class JsonUtils {
AnimeGitB's avatar
AnimeGitB committed
26
27
28
    static final Gson gson = new GsonBuilder()
        .setPrettyPrinting()
        .registerTypeAdapter(DynamicFloat.class, new DynamicFloatAdapter())
AnimeGitB's avatar
AnimeGitB committed
29
        .registerTypeAdapter(IntList.class, new IntListAdapter())
30
        .registerTypeAdapter(Position.class, new PositionAdapter())
AnimeGitB's avatar
AnimeGitB committed
31
        .registerTypeAdapterFactory(new EnumTypeAdapterFactory())
AnimeGitB's avatar
AnimeGitB committed
32
        .create();
33
34
35
36
37
38
39
40
41
42
43
44

    /*
     * 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);
    }

45
    public static <T> T loadToClass(Reader fileReader, Class<T> classType) throws IOException {
46
47
48
        return gson.fromJson(fileReader, classType);
    }

49
    @Deprecated(forRemoval = true)
50
51
52
53
54
55
    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);
        }
    }

56
57
58
59
60
61
62
    public static <T> T loadToClass(Path filename, Class<T> classType) throws IOException {
        try (var fileReader = Files.newBufferedReader(filename, StandardCharsets.UTF_8)) {
            return loadToClass(fileReader, classType);
        }
    }

    public static <T> List<T> loadToList(Reader fileReader, Class<T> classType) throws IOException {
63
64
65
        return gson.fromJson(fileReader, TypeToken.getParameterized(List.class, classType).getType());
    }

66
    @Deprecated(forRemoval = true)
67
68
69
70
71
72
    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);
        }
    }

73
74
75
76
77
78
79
    public static <T> List<T> loadToList(Path filename, Class<T> classType) throws IOException {
        try (var fileReader = Files.newBufferedReader(filename, StandardCharsets.UTF_8)) {
            return loadToList(fileReader, classType);
        }
    }

    public static <T1,T2> Map<T1,T2> loadToMap(Reader fileReader, Class<T1> keyType, Class<T2> valueType) throws IOException {
80
81
82
        return gson.fromJson(fileReader, TypeToken.getParameterized(Map.class, keyType, valueType).getType());
    }

83
    @Deprecated(forRemoval = true)
84
85
86
87
88
89
    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);
        }
    }

90
91
92
93
94
95
    public static <T1,T2> Map<T1,T2> loadToMap(Path filename, Class<T1> keyType, Class<T2> valueType) throws IOException {
        try (var fileReader = Files.newBufferedReader(filename, StandardCharsets.UTF_8)) {
            return loadToMap(fileReader, keyType, valueType);
        }
    }

96
97
98
99
100
101
102
103
104
105
106
107
    /**
     * 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;
        }
    }
Luke H-W's avatar
Luke H-W committed
108
109
110
111
112
113
114
115

    public static <T> T decode(String jsonData, Type type) {
        try {
            return gson.fromJson(jsonData, type);
        } catch (Exception ignored) {
            return null;
        }
    }
116
}