JsonUtils.java

com.mojang.realmsclient.util.JsonUtils

信息

  • 全限定名:com.mojang.realmsclient.util.JsonUtils
  • 类型:public class
  • 包:com.mojang.realmsclient.util
  • 源码路径:src/main/java/com/mojang/realmsclient/util/JsonUtils.java
  • 起始行号:L15
  • 职责:

    TODO

字段/常量

内部类/嵌套类型

构造器

方法

下面的方法块按源码顺序生成。

public static <T> T getRequired(String key, JsonObject node, Function<JsonObject,T> parser) @ L16

  • 方法名:getRequired
  • 源码定位:L16
  • 返回类型: T
  • 修饰符:public static

参数:

  • key: String
  • node: JsonObject
  • parser: Function<JsonObject,T>

说明:

TODO

public static <T> T getOptional(String key, JsonObject node, Function<JsonObject,T> parser) @ L27

  • 方法名:getOptional
  • 源码定位:L27
  • 返回类型: T
  • 修饰符:public static

参数:

  • key: String
  • node: JsonObject
  • parser: Function<JsonObject,T>

说明:

TODO

public static String getRequiredString(String key, JsonObject node) @ L38

  • 方法名:getRequiredString
  • 源码定位:L38
  • 返回类型:String
  • 修饰符:public static

参数:

  • key: String
  • node: JsonObject

说明:

TODO

public static String getStringOr(String key, JsonObject node, String defaultValue) @ L47

  • 方法名:getStringOr
  • 源码定位:L47
  • 返回类型:String
  • 修饰符:public static

参数:

  • key: String
  • node: JsonObject
  • defaultValue: String

说明:

TODO

public static UUID getUuidOr(String key, JsonObject node, UUID defaultValue) @ L57

  • 方法名:getUuidOr
  • 源码定位:L57
  • 返回类型:UUID
  • 修饰符:public static

参数:

  • key: String
  • node: JsonObject
  • defaultValue: UUID

说明:

TODO

public static int getIntOr(String key, JsonObject node, int defaultValue) @ L63

  • 方法名:getIntOr
  • 源码定位:L63
  • 返回类型:int
  • 修饰符:public static

参数:

  • key: String
  • node: JsonObject
  • defaultValue: int

说明:

TODO

public static long getLongOr(String key, JsonObject node, long defaultValue) @ L72

  • 方法名:getLongOr
  • 源码定位:L72
  • 返回类型:long
  • 修饰符:public static

参数:

  • key: String
  • node: JsonObject
  • defaultValue: long

说明:

TODO

public static boolean getBooleanOr(String key, JsonObject node, boolean defaultValue) @ L81

  • 方法名:getBooleanOr
  • 源码定位:L81
  • 返回类型:boolean
  • 修饰符:public static

参数:

  • key: String
  • node: JsonObject
  • defaultValue: boolean

说明:

TODO

public static Instant getDateOr(String key, JsonObject node) @ L90

  • 方法名:getDateOr
  • 源码定位:L90
  • 返回类型:Instant
  • 修饰符:public static

参数:

  • key: String
  • node: JsonObject

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class JsonUtils {
    public static <T> T getRequired(String key, JsonObject node, Function<JsonObject, T> parser) {
        JsonElement property = node.get(key);
        if (property == null || property.isJsonNull()) {
            throw new IllegalStateException("Missing required property: " + key);
        } else if (!property.isJsonObject()) {
            throw new IllegalStateException("Required property " + key + " was not a JsonObject as espected");
        } else {
            return parser.apply(property.getAsJsonObject());
        }
    }
 
    public static <T> @Nullable T getOptional(String key, JsonObject node, Function<JsonObject, T> parser) {
        JsonElement property = node.get(key);
        if (property == null || property.isJsonNull()) {
            return null;
        } else if (!property.isJsonObject()) {
            throw new IllegalStateException("Required property " + key + " was not a JsonObject as espected");
        } else {
            return parser.apply(property.getAsJsonObject());
        }
    }
 
    public static String getRequiredString(String key, JsonObject node) {
        String result = getStringOr(key, node, null);
        if (result == null) {
            throw new IllegalStateException("Missing required property: " + key);
        } else {
            return result;
        }
    }
 
    @Contract("_,_,!null->!null;_,_,null->_")
    public static @Nullable String getStringOr(String key, JsonObject node, @Nullable String defaultValue) {
        JsonElement element = node.get(key);
        if (element != null) {
            return element.isJsonNull() ? defaultValue : element.getAsString();
        } else {
            return defaultValue;
        }
    }
 
    @Contract("_,_,!null->!null;_,_,null->_")
    public static @Nullable UUID getUuidOr(String key, JsonObject node, @Nullable UUID defaultValue) {
        String uuidAsString = getStringOr(key, node, null);
        return uuidAsString == null ? defaultValue : UndashedUuid.fromStringLenient(uuidAsString);
    }
 
    public static int getIntOr(String key, JsonObject node, int defaultValue) {
        JsonElement element = node.get(key);
        if (element != null) {
            return element.isJsonNull() ? defaultValue : element.getAsInt();
        } else {
            return defaultValue;
        }
    }
 
    public static long getLongOr(String key, JsonObject node, long defaultValue) {
        JsonElement element = node.get(key);
        if (element != null) {
            return element.isJsonNull() ? defaultValue : element.getAsLong();
        } else {
            return defaultValue;
        }
    }
 
    public static boolean getBooleanOr(String key, JsonObject node, boolean defaultValue) {
        JsonElement element = node.get(key);
        if (element != null) {
            return element.isJsonNull() ? defaultValue : element.getAsBoolean();
        } else {
            return defaultValue;
        }
    }
 
    public static Instant getDateOr(String key, JsonObject node) {
        JsonElement element = node.get(key);
        return element != null ? Instant.ofEpochMilli(Long.parseLong(element.getAsString())) : Instant.EPOCH;
    }
}

引用的其他类