aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/kubatech/api/utils/GSONUtils.java
blob: 90f777000cc97a3592a0745237746bd6926d5f76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package kubatech.api.utils;

import com.google.gson.*;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTSizeTracker;
import net.minecraft.nbt.NBTTagCompound;

public class GSONUtils {
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface SkipGSON {}

    private static final ExclusionStrategy GSONStrategy = new ExclusionStrategy() {
        @Override
        public boolean shouldSkipField(FieldAttributes f) {
            return f.getAnnotation(SkipGSON.class) != null;
        }

        @Override
        public boolean shouldSkipClass(Class<?> clazz) {
            return false;
        }
    };

    private static final JsonSerializer<NBTTagCompound> NBTTagCompoundSerializer =
            new JsonSerializer<NBTTagCompound>() {

                @Override
                public JsonElement serialize(NBTTagCompound src, Type typeOfSrc, JsonSerializationContext context) {
                    try {
                        JsonArray array = new JsonArray();
                        for (byte b : CompressedStreamTools.compress(src)) {
                            array.add(new JsonPrimitive(b));
                        }
                        return array;
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            };

    private static final JsonDeserializer<NBTTagCompound> NBTTagCompoundDeserializer =
            new JsonDeserializer<NBTTagCompound>() {
                @Override
                public NBTTagCompound deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                        throws JsonParseException {
                    try {
                        if (!(json instanceof JsonArray)) return null;
                        byte[] bytes = new byte[((JsonArray) json).size()];
                        for (int i = 0; i < bytes.length; i++)
                            bytes[i] = ((JsonArray) json).get(i).getAsByte();
                        return CompressedStreamTools.func_152457_a(bytes, new NBTSizeTracker(2097152L));
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            };

    public static final GsonBuilder GSON_BUILDER = new GsonBuilder()
            .addSerializationExclusionStrategy(GSONStrategy)
            .addDeserializationExclusionStrategy(GSONStrategy)
            .registerTypeAdapter(NBTTagCompound.class, NBTTagCompoundDeserializer)
            .registerTypeAdapter(NBTTagCompound.class, NBTTagCompoundSerializer)
            .serializeNulls();
    public static final GsonBuilder GSON_BUILDER_PRETTY = new GsonBuilder()
            .addSerializationExclusionStrategy(GSONStrategy)
            .addDeserializationExclusionStrategy(GSONStrategy)
            .registerTypeAdapter(NBTTagCompound.class, NBTTagCompoundDeserializer)
            .registerTypeAdapter(NBTTagCompound.class, NBTTagCompoundSerializer)
            .serializeNulls()
            .setPrettyPrinting();

    public static <T> T readFile(Gson gson, File file, Class<T> tClass) {
        if (!file.exists()) return null;
        if (!file.isFile()) return null;
        T t = null;
        Reader reader = null;
        try {
            reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8);
            t = gson.fromJson(reader, tClass);
        } catch (Exception ignored) {
        } finally {
            if (reader != null)
                try {
                    reader.close();
                } catch (Exception ignored) {
                }
        }
        return t;
    }
}