File size: 3,869 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package com.mojang.realmsclient.util;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.util.UndashedUuid;
import java.util.Date;
import java.util.UUID;
import java.util.function.Function;
import javax.annotation.Nullable;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class JsonUtils {
    public static <T> T getRequired(String p_275573_, JsonObject p_275650_, Function<JsonObject, T> p_275655_) {
        JsonElement jsonelement = p_275650_.get(p_275573_);
        if (jsonelement == null || jsonelement.isJsonNull()) {
            throw new IllegalStateException("Missing required property: " + p_275573_);
        } else if (!jsonelement.isJsonObject()) {
            throw new IllegalStateException("Required property " + p_275573_ + " was not a JsonObject as espected");
        } else {
            return p_275655_.apply(jsonelement.getAsJsonObject());
        }
    }

    @Nullable
    public static <T> T getOptional(String p_309589_, JsonObject p_310739_, Function<JsonObject, T> p_310530_) {
        JsonElement jsonelement = p_310739_.get(p_309589_);
        if (jsonelement == null || jsonelement.isJsonNull()) {
            return null;
        } else if (!jsonelement.isJsonObject()) {
            throw new IllegalStateException("Required property " + p_309589_ + " was not a JsonObject as espected");
        } else {
            return p_310530_.apply(jsonelement.getAsJsonObject());
        }
    }

    public static String getRequiredString(String p_275692_, JsonObject p_275706_) {
        String s = getStringOr(p_275692_, p_275706_, null);
        if (s == null) {
            throw new IllegalStateException("Missing required property: " + p_275692_);
        } else {
            return s;
        }
    }

    public static String getRequiredStringOr(String p_309497_, JsonObject p_310406_, String p_312706_) {
        return getStringOr(p_309497_, p_310406_, p_312706_);
    }

    @Nullable
    public static String getStringOr(String p_90162_, JsonObject p_90163_, @Nullable String p_90164_) {
        JsonElement jsonelement = p_90163_.get(p_90162_);
        if (jsonelement != null) {
            return jsonelement.isJsonNull() ? p_90164_ : jsonelement.getAsString();
        } else {
            return p_90164_;
        }
    }

    @Nullable
    public static UUID getUuidOr(String p_275342_, JsonObject p_275515_, @Nullable UUID p_275232_) {
        String s = getStringOr(p_275342_, p_275515_, null);
        return s == null ? p_275232_ : UndashedUuid.fromStringLenient(s);
    }

    public static int getIntOr(String p_90154_, JsonObject p_90155_, int p_90156_) {
        JsonElement jsonelement = p_90155_.get(p_90154_);
        if (jsonelement != null) {
            return jsonelement.isJsonNull() ? p_90156_ : jsonelement.getAsInt();
        } else {
            return p_90156_;
        }
    }

    public static long getLongOr(String p_90158_, JsonObject p_90159_, long p_90160_) {
        JsonElement jsonelement = p_90159_.get(p_90158_);
        if (jsonelement != null) {
            return jsonelement.isJsonNull() ? p_90160_ : jsonelement.getAsLong();
        } else {
            return p_90160_;
        }
    }

    public static boolean getBooleanOr(String p_90166_, JsonObject p_90167_, boolean p_90168_) {
        JsonElement jsonelement = p_90167_.get(p_90166_);
        if (jsonelement != null) {
            return jsonelement.isJsonNull() ? p_90168_ : jsonelement.getAsBoolean();
        } else {
            return p_90168_;
        }
    }

    public static Date getDateOr(String p_90151_, JsonObject p_90152_) {
        JsonElement jsonelement = p_90152_.get(p_90151_);
        return jsonelement != null ? new Date(Long.parseLong(jsonelement.getAsString())) : new Date();
    }
}