GsonHelper.java
net.minecraft.util.GsonHelper
信息
- 全限定名:net.minecraft.util.GsonHelper
- 类型:public class
- 包:net.minecraft.util
- 源码路径:src/main/java/net/minecraft/util/GsonHelper.java
- 起始行号:L37
- 职责:
TODO
字段/常量
GSON- 类型:
Gson - 修饰符:
private static final - 源码定位:
L38 - 说明:
TODO
- 类型:
内部类/嵌套类型
net.minecraft.util.GsonHelper.CountedAppendable- 类型:
class - 修饰符:
private static - 源码定位:
L553 - 说明:
TODO
- 类型:
构造器
- 无
方法
下面的方法块按源码顺序生成。
public static boolean isStringValue(JsonObject node, String name) @ L40
- 方法名:isStringValue
- 源码定位:L40
- 返回类型:boolean
- 修饰符:public static
参数:
- node: JsonObject
- name: String
说明:
TODO
public static boolean isStringValue(JsonElement node) @ L44
- 方法名:isStringValue
- 源码定位:L44
- 返回类型:boolean
- 修饰符:public static
参数:
- node: JsonElement
说明:
TODO
public static boolean isNumberValue(JsonObject node, String name) @ L48
- 方法名:isNumberValue
- 源码定位:L48
- 返回类型:boolean
- 修饰符:public static
参数:
- node: JsonObject
- name: String
说明:
TODO
public static boolean isNumberValue(JsonElement node) @ L52
- 方法名:isNumberValue
- 源码定位:L52
- 返回类型:boolean
- 修饰符:public static
参数:
- node: JsonElement
说明:
TODO
public static boolean isBooleanValue(JsonObject node, String name) @ L56
- 方法名:isBooleanValue
- 源码定位:L56
- 返回类型:boolean
- 修饰符:public static
参数:
- node: JsonObject
- name: String
说明:
TODO
public static boolean isBooleanValue(JsonElement node) @ L60
- 方法名:isBooleanValue
- 源码定位:L60
- 返回类型:boolean
- 修饰符:public static
参数:
- node: JsonElement
说明:
TODO
public static boolean isArrayNode(JsonObject node, String name) @ L64
- 方法名:isArrayNode
- 源码定位:L64
- 返回类型:boolean
- 修饰符:public static
参数:
- node: JsonObject
- name: String
说明:
TODO
public static boolean isObjectNode(JsonObject node, String name) @ L68
- 方法名:isObjectNode
- 源码定位:L68
- 返回类型:boolean
- 修饰符:public static
参数:
- node: JsonObject
- name: String
说明:
TODO
public static boolean isValidPrimitive(JsonObject node, String name) @ L72
- 方法名:isValidPrimitive
- 源码定位:L72
- 返回类型:boolean
- 修饰符:public static
参数:
- node: JsonObject
- name: String
说明:
TODO
public static boolean isValidNode(JsonObject node, String name) @ L76
- 方法名:isValidNode
- 源码定位:L76
- 返回类型:boolean
- 修饰符:public static
参数:
- node: JsonObject
- name: String
说明:
TODO
public static JsonElement getNonNull(JsonObject object, String name) @ L80
- 方法名:getNonNull
- 源码定位:L80
- 返回类型:JsonElement
- 修饰符:public static
参数:
- object: JsonObject
- name: String
说明:
TODO
public static String convertToString(JsonElement element, String name) @ L89
- 方法名:convertToString
- 源码定位:L89
- 返回类型:String
- 修饰符:public static
参数:
- element: JsonElement
- name: String
说明:
TODO
public static String getAsString(JsonObject object, String name) @ L97
- 方法名:getAsString
- 源码定位:L97
- 返回类型:String
- 修饰符:public static
参数:
- object: JsonObject
- name: String
说明:
TODO
public static String getAsString(JsonObject object, String name, String def) @ L105
- 方法名:getAsString
- 源码定位:L105
- 返回类型:String
- 修饰符:public static
参数:
- object: JsonObject
- name: String
- def: String
说明:
TODO
public static Holder<Item> convertToItem(JsonElement element, String name) @ L110
- 方法名:convertToItem
- 源码定位:L110
- 返回类型:Holder
- 修饰符:public static
参数:
- element: JsonElement
- name: String
说明:
TODO
public static Holder<Item> getAsItem(JsonObject object, String name) @ L121
- 方法名:getAsItem
- 源码定位:L121
- 返回类型:Holder
- 修饰符:public static
参数:
- object: JsonObject
- name: String
说明:
TODO
public static Holder<Item> getAsItem(JsonObject object, String name, Holder<Item> def) @ L129
- 方法名:getAsItem
- 源码定位:L129
- 返回类型:Holder
- 修饰符:public static
参数:
- object: JsonObject
- name: String
- def: Holder
说明:
TODO
public static boolean convertToBoolean(JsonElement element, String name) @ L134
- 方法名:convertToBoolean
- 源码定位:L134
- 返回类型:boolean
- 修饰符:public static
参数:
- element: JsonElement
- name: String
说明:
TODO
public static boolean getAsBoolean(JsonObject object, String name) @ L142
- 方法名:getAsBoolean
- 源码定位:L142
- 返回类型:boolean
- 修饰符:public static
参数:
- object: JsonObject
- name: String
说明:
TODO
public static boolean getAsBoolean(JsonObject object, String name, boolean def) @ L150
- 方法名:getAsBoolean
- 源码定位:L150
- 返回类型:boolean
- 修饰符:public static
参数:
- object: JsonObject
- name: String
- def: boolean
说明:
TODO
public static double convertToDouble(JsonElement element, String name) @ L154
- 方法名:convertToDouble
- 源码定位:L154
- 返回类型:double
- 修饰符:public static
参数:
- element: JsonElement
- name: String
说明:
TODO
public static double getAsDouble(JsonObject object, String name) @ L162
- 方法名:getAsDouble
- 源码定位:L162
- 返回类型:double
- 修饰符:public static
参数:
- object: JsonObject
- name: String
说明:
TODO
public static double getAsDouble(JsonObject object, String name, double def) @ L170
- 方法名:getAsDouble
- 源码定位:L170
- 返回类型:double
- 修饰符:public static
参数:
- object: JsonObject
- name: String
- def: double
说明:
TODO
public static float convertToFloat(JsonElement element, String name) @ L174
- 方法名:convertToFloat
- 源码定位:L174
- 返回类型:float
- 修饰符:public static
参数:
- element: JsonElement
- name: String
说明:
TODO
public static float getAsFloat(JsonObject object, String name) @ L182
- 方法名:getAsFloat
- 源码定位:L182
- 返回类型:float
- 修饰符:public static
参数:
- object: JsonObject
- name: String
说明:
TODO
public static float getAsFloat(JsonObject object, String name, float def) @ L190
- 方法名:getAsFloat
- 源码定位:L190
- 返回类型:float
- 修饰符:public static
参数:
- object: JsonObject
- name: String
- def: float
说明:
TODO
public static long convertToLong(JsonElement element, String name) @ L194
- 方法名:convertToLong
- 源码定位:L194
- 返回类型:long
- 修饰符:public static
参数:
- element: JsonElement
- name: String
说明:
TODO
public static long getAsLong(JsonObject object, String name) @ L202
- 方法名:getAsLong
- 源码定位:L202
- 返回类型:long
- 修饰符:public static
参数:
- object: JsonObject
- name: String
说明:
TODO
public static long getAsLong(JsonObject object, String name, long def) @ L210
- 方法名:getAsLong
- 源码定位:L210
- 返回类型:long
- 修饰符:public static
参数:
- object: JsonObject
- name: String
- def: long
说明:
TODO
public static int convertToInt(JsonElement element, String name) @ L214
- 方法名:convertToInt
- 源码定位:L214
- 返回类型:int
- 修饰符:public static
参数:
- element: JsonElement
- name: String
说明:
TODO
public static int getAsInt(JsonObject object, String name) @ L222
- 方法名:getAsInt
- 源码定位:L222
- 返回类型:int
- 修饰符:public static
参数:
- object: JsonObject
- name: String
说明:
TODO
public static int getAsInt(JsonObject object, String name, int def) @ L230
- 方法名:getAsInt
- 源码定位:L230
- 返回类型:int
- 修饰符:public static
参数:
- object: JsonObject
- name: String
- def: int
说明:
TODO
public static byte convertToByte(JsonElement element, String name) @ L234
- 方法名:convertToByte
- 源码定位:L234
- 返回类型:byte
- 修饰符:public static
参数:
- element: JsonElement
- name: String
说明:
TODO
public static byte getAsByte(JsonObject object, String name) @ L242
- 方法名:getAsByte
- 源码定位:L242
- 返回类型:byte
- 修饰符:public static
参数:
- object: JsonObject
- name: String
说明:
TODO
public static byte getAsByte(JsonObject object, String name, byte def) @ L250
- 方法名:getAsByte
- 源码定位:L250
- 返回类型:byte
- 修饰符:public static
参数:
- object: JsonObject
- name: String
- def: byte
说明:
TODO
public static char convertToCharacter(JsonElement element, String name) @ L254
- 方法名:convertToCharacter
- 源码定位:L254
- 返回类型:char
- 修饰符:public static
参数:
- element: JsonElement
- name: String
说明:
TODO
public static char getAsCharacter(JsonObject object, String name) @ L262
- 方法名:getAsCharacter
- 源码定位:L262
- 返回类型:char
- 修饰符:public static
参数:
- object: JsonObject
- name: String
说明:
TODO
public static char getAsCharacter(JsonObject object, String name, char def) @ L270
- 方法名:getAsCharacter
- 源码定位:L270
- 返回类型:char
- 修饰符:public static
参数:
- object: JsonObject
- name: String
- def: char
说明:
TODO
public static BigDecimal convertToBigDecimal(JsonElement element, String name) @ L274
- 方法名:convertToBigDecimal
- 源码定位:L274
- 返回类型:BigDecimal
- 修饰符:public static
参数:
- element: JsonElement
- name: String
说明:
TODO
public static BigDecimal getAsBigDecimal(JsonObject object, String name) @ L282
- 方法名:getAsBigDecimal
- 源码定位:L282
- 返回类型:BigDecimal
- 修饰符:public static
参数:
- object: JsonObject
- name: String
说明:
TODO
public static BigDecimal getAsBigDecimal(JsonObject object, String name, BigDecimal def) @ L290
- 方法名:getAsBigDecimal
- 源码定位:L290
- 返回类型:BigDecimal
- 修饰符:public static
参数:
- object: JsonObject
- name: String
- def: BigDecimal
说明:
TODO
public static BigInteger convertToBigInteger(JsonElement element, String name) @ L294
- 方法名:convertToBigInteger
- 源码定位:L294
- 返回类型:BigInteger
- 修饰符:public static
参数:
- element: JsonElement
- name: String
说明:
TODO
public static BigInteger getAsBigInteger(JsonObject object, String name) @ L302
- 方法名:getAsBigInteger
- 源码定位:L302
- 返回类型:BigInteger
- 修饰符:public static
参数:
- object: JsonObject
- name: String
说明:
TODO
public static BigInteger getAsBigInteger(JsonObject object, String name, BigInteger def) @ L310
- 方法名:getAsBigInteger
- 源码定位:L310
- 返回类型:BigInteger
- 修饰符:public static
参数:
- object: JsonObject
- name: String
- def: BigInteger
说明:
TODO
public static short convertToShort(JsonElement element, String name) @ L314
- 方法名:convertToShort
- 源码定位:L314
- 返回类型:short
- 修饰符:public static
参数:
- element: JsonElement
- name: String
说明:
TODO
public static short getAsShort(JsonObject object, String name) @ L322
- 方法名:getAsShort
- 源码定位:L322
- 返回类型:short
- 修饰符:public static
参数:
- object: JsonObject
- name: String
说明:
TODO
public static short getAsShort(JsonObject object, String name, short def) @ L330
- 方法名:getAsShort
- 源码定位:L330
- 返回类型:short
- 修饰符:public static
参数:
- object: JsonObject
- name: String
- def: short
说明:
TODO
public static JsonObject convertToJsonObject(JsonElement element, String name) @ L334
- 方法名:convertToJsonObject
- 源码定位:L334
- 返回类型:JsonObject
- 修饰符:public static
参数:
- element: JsonElement
- name: String
说明:
TODO
public static JsonObject getAsJsonObject(JsonObject object, String name) @ L342
- 方法名:getAsJsonObject
- 源码定位:L342
- 返回类型:JsonObject
- 修饰符:public static
参数:
- object: JsonObject
- name: String
说明:
TODO
public static JsonObject getAsJsonObject(JsonObject object, String name, JsonObject def) @ L350
- 方法名:getAsJsonObject
- 源码定位:L350
- 返回类型:JsonObject
- 修饰符:public static
参数:
- object: JsonObject
- name: String
- def: JsonObject
说明:
TODO
public static JsonArray convertToJsonArray(JsonElement element, String name) @ L355
- 方法名:convertToJsonArray
- 源码定位:L355
- 返回类型:JsonArray
- 修饰符:public static
参数:
- element: JsonElement
- name: String
说明:
TODO
public static JsonArray getAsJsonArray(JsonObject object, String name) @ L363
- 方法名:getAsJsonArray
- 源码定位:L363
- 返回类型:JsonArray
- 修饰符:public static
参数:
- object: JsonObject
- name: String
说明:
TODO
public static JsonArray getAsJsonArray(JsonObject object, String name, JsonArray def) @ L371
- 方法名:getAsJsonArray
- 源码定位:L371
- 返回类型:JsonArray
- 修饰符:public static
参数:
- object: JsonObject
- name: String
- def: JsonArray
说明:
TODO
public static <T> T convertToObject(JsonElement element, String name, JsonDeserializationContext context, Class<?extends T> clazz) @ L376
- 方法名:convertToObject
- 源码定位:L376
- 返回类型:
T - 修饰符:public static
参数:
- element: JsonElement
- name: String
- context: JsonDeserializationContext
- clazz: Class<?extends T>
说明:
TODO
public static <T> T getAsObject(JsonObject object, String name, JsonDeserializationContext context, Class<?extends T> clazz) @ L384
- 方法名:getAsObject
- 源码定位:L384
- 返回类型:
T - 修饰符:public static
参数:
- object: JsonObject
- name: String
- context: JsonDeserializationContext
- clazz: Class<?extends T>
说明:
TODO
public static <T> T getAsObject(JsonObject object, String name, T def, JsonDeserializationContext context, Class<?extends T> clazz) @ L392
- 方法名:getAsObject
- 源码定位:L392
- 返回类型:
T - 修饰符:public static
参数:
- object: JsonObject
- name: String
- def: T
- context: JsonDeserializationContext
- clazz: Class<?extends T>
说明:
TODO
public static String getType(JsonElement element) @ L397
- 方法名:getType
- 源码定位:L397
- 返回类型:String
- 修饰符:public static
参数:
- element: JsonElement
说明:
TODO
public static <T> T fromJson(Gson gson, Reader reader, Class<T> type) @ L423
- 方法名:fromJson
- 源码定位:L423
- 返回类型:
T - 修饰符:public static
参数:
- gson: Gson
- reader: Reader
- type: Class
说明:
TODO
public static <T> T fromNullableJson(Gson gson, Reader reader, TypeToken<T> type) @ L438
- 方法名:fromNullableJson
- 源码定位:L438
- 返回类型:
T - 修饰符:public static
参数:
- gson: Gson
- reader: Reader
- type: TypeToken
说明:
TODO
public static <T> T fromJson(Gson gson, Reader reader, TypeToken<T> type) @ L448
- 方法名:fromJson
- 源码定位:L448
- 返回类型:
T - 修饰符:public static
参数:
- gson: Gson
- reader: Reader
- type: TypeToken
说明:
TODO
public static <T> T fromNullableJson(Gson gson, String string, TypeToken<T> type) @ L457
- 方法名:fromNullableJson
- 源码定位:L457
- 返回类型:
T - 修饰符:public static
参数:
- gson: Gson
- string: String
- type: TypeToken
说明:
TODO
public static <T> T fromJson(Gson gson, String string, Class<T> type) @ L461
- 方法名:fromJson
- 源码定位:L461
- 返回类型:
T - 修饰符:public static
参数:
- gson: Gson
- string: String
- type: Class
说明:
TODO
public static JsonObject parse(String string) @ L465
- 方法名:parse
- 源码定位:L465
- 返回类型:JsonObject
- 修饰符:public static
参数:
- string: String
说明:
TODO
public static JsonObject parse(Reader reader) @ L469
- 方法名:parse
- 源码定位:L469
- 返回类型:JsonObject
- 修饰符:public static
参数:
- reader: Reader
说明:
TODO
public static JsonArray parseArray(String string) @ L473
- 方法名:parseArray
- 源码定位:L473
- 返回类型:JsonArray
- 修饰符:public static
参数:
- string: String
说明:
TODO
public static JsonArray parseArray(Reader reader) @ L477
- 方法名:parseArray
- 源码定位:L477
- 返回类型:JsonArray
- 修饰符:public static
参数:
- reader: Reader
说明:
TODO
public static String toStableString(JsonElement jsonElement) @ L481
- 方法名:toStableString
- 源码定位:L481
- 返回类型:String
- 修饰符:public static
参数:
- jsonElement: JsonElement
说明:
TODO
public static void writeValue(JsonWriter out, JsonElement value, Comparator<String> keyComparator) @ L494
- 方法名:writeValue
- 源码定位:L494
- 返回类型:void
- 修饰符:public static
参数:
- out: JsonWriter
- value: JsonElement
- keyComparator: Comparator
说明:
TODO
private static Collection<Entry<String,JsonElement>> sortByKeyIfNeeded(Collection<Entry<String,JsonElement>> elements, Comparator<String> keyComparator) @ L530
- 方法名:sortByKeyIfNeeded
- 源码定位:L530
- 返回类型:Collection<Entry<String,JsonElement>>
- 修饰符:private static
参数:
- elements: Collection<Entry<String,JsonElement>>
- keyComparator: Comparator
说明:
TODO
public static boolean encodesLongerThan(JsonElement element, int limit) @ L542
- 方法名:encodesLongerThan
- 源码定位:L542
- 返回类型:boolean
- 修饰符:public static
参数:
- element: JsonElement
- limit: int
说明:
TODO
代码
public class GsonHelper {
private static final Gson GSON = new GsonBuilder().create();
public static boolean isStringValue(JsonObject node, String name) {
return !isValidPrimitive(node, name) ? false : node.getAsJsonPrimitive(name).isString();
}
public static boolean isStringValue(JsonElement node) {
return !node.isJsonPrimitive() ? false : node.getAsJsonPrimitive().isString();
}
public static boolean isNumberValue(JsonObject node, String name) {
return !isValidPrimitive(node, name) ? false : node.getAsJsonPrimitive(name).isNumber();
}
public static boolean isNumberValue(JsonElement node) {
return !node.isJsonPrimitive() ? false : node.getAsJsonPrimitive().isNumber();
}
public static boolean isBooleanValue(JsonObject node, String name) {
return !isValidPrimitive(node, name) ? false : node.getAsJsonPrimitive(name).isBoolean();
}
public static boolean isBooleanValue(JsonElement node) {
return !node.isJsonPrimitive() ? false : node.getAsJsonPrimitive().isBoolean();
}
public static boolean isArrayNode(JsonObject node, String name) {
return !isValidNode(node, name) ? false : node.get(name).isJsonArray();
}
public static boolean isObjectNode(JsonObject node, String name) {
return !isValidNode(node, name) ? false : node.get(name).isJsonObject();
}
public static boolean isValidPrimitive(JsonObject node, String name) {
return !isValidNode(node, name) ? false : node.get(name).isJsonPrimitive();
}
public static boolean isValidNode(@Nullable JsonObject node, String name) {
return node == null ? false : node.get(name) != null;
}
public static JsonElement getNonNull(JsonObject object, String name) {
JsonElement result = object.get(name);
if (result != null && !result.isJsonNull()) {
return result;
} else {
throw new JsonSyntaxException("Missing field " + name);
}
}
public static String convertToString(JsonElement element, String name) {
if (element.isJsonPrimitive()) {
return element.getAsString();
} else {
throw new JsonSyntaxException("Expected " + name + " to be a string, was " + getType(element));
}
}
public static String getAsString(JsonObject object, String name) {
if (object.has(name)) {
return convertToString(object.get(name), name);
} else {
throw new JsonSyntaxException("Missing " + name + ", expected to find a string");
}
}
@Contract("_,_,!null->!null;_,_,null->_")
public static @Nullable String getAsString(JsonObject object, String name, @Nullable String def) {
return object.has(name) ? convertToString(object.get(name), name) : def;
}
public static Holder<Item> convertToItem(JsonElement element, String name) {
if (element.isJsonPrimitive()) {
String itemName = element.getAsString();
return BuiltInRegistries.ITEM
.get(Identifier.parse(itemName))
.orElseThrow(() -> new JsonSyntaxException("Expected " + name + " to be an item, was unknown string '" + itemName + "'"));
} else {
throw new JsonSyntaxException("Expected " + name + " to be an item, was " + getType(element));
}
}
public static Holder<Item> getAsItem(JsonObject object, String name) {
if (object.has(name)) {
return convertToItem(object.get(name), name);
} else {
throw new JsonSyntaxException("Missing " + name + ", expected to find an item");
}
}
@Contract("_,_,!null->!null;_,_,null->_")
public static @Nullable Holder<Item> getAsItem(JsonObject object, String name, @Nullable Holder<Item> def) {
return object.has(name) ? convertToItem(object.get(name), name) : def;
}
public static boolean convertToBoolean(JsonElement element, String name) {
if (element.isJsonPrimitive()) {
return element.getAsBoolean();
} else {
throw new JsonSyntaxException("Expected " + name + " to be a Boolean, was " + getType(element));
}
}
public static boolean getAsBoolean(JsonObject object, String name) {
if (object.has(name)) {
return convertToBoolean(object.get(name), name);
} else {
throw new JsonSyntaxException("Missing " + name + ", expected to find a Boolean");
}
}
public static boolean getAsBoolean(JsonObject object, String name, boolean def) {
return object.has(name) ? convertToBoolean(object.get(name), name) : def;
}
public static double convertToDouble(JsonElement element, String name) {
if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
return element.getAsDouble();
} else {
throw new JsonSyntaxException("Expected " + name + " to be a Double, was " + getType(element));
}
}
public static double getAsDouble(JsonObject object, String name) {
if (object.has(name)) {
return convertToDouble(object.get(name), name);
} else {
throw new JsonSyntaxException("Missing " + name + ", expected to find a Double");
}
}
public static double getAsDouble(JsonObject object, String name, double def) {
return object.has(name) ? convertToDouble(object.get(name), name) : def;
}
public static float convertToFloat(JsonElement element, String name) {
if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
return element.getAsFloat();
} else {
throw new JsonSyntaxException("Expected " + name + " to be a Float, was " + getType(element));
}
}
public static float getAsFloat(JsonObject object, String name) {
if (object.has(name)) {
return convertToFloat(object.get(name), name);
} else {
throw new JsonSyntaxException("Missing " + name + ", expected to find a Float");
}
}
public static float getAsFloat(JsonObject object, String name, float def) {
return object.has(name) ? convertToFloat(object.get(name), name) : def;
}
public static long convertToLong(JsonElement element, String name) {
if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
return element.getAsLong();
} else {
throw new JsonSyntaxException("Expected " + name + " to be a Long, was " + getType(element));
}
}
public static long getAsLong(JsonObject object, String name) {
if (object.has(name)) {
return convertToLong(object.get(name), name);
} else {
throw new JsonSyntaxException("Missing " + name + ", expected to find a Long");
}
}
public static long getAsLong(JsonObject object, String name, long def) {
return object.has(name) ? convertToLong(object.get(name), name) : def;
}
public static int convertToInt(JsonElement element, String name) {
if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
return element.getAsInt();
} else {
throw new JsonSyntaxException("Expected " + name + " to be a Int, was " + getType(element));
}
}
public static int getAsInt(JsonObject object, String name) {
if (object.has(name)) {
return convertToInt(object.get(name), name);
} else {
throw new JsonSyntaxException("Missing " + name + ", expected to find a Int");
}
}
public static int getAsInt(JsonObject object, String name, int def) {
return object.has(name) ? convertToInt(object.get(name), name) : def;
}
public static byte convertToByte(JsonElement element, String name) {
if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
return element.getAsByte();
} else {
throw new JsonSyntaxException("Expected " + name + " to be a Byte, was " + getType(element));
}
}
public static byte getAsByte(JsonObject object, String name) {
if (object.has(name)) {
return convertToByte(object.get(name), name);
} else {
throw new JsonSyntaxException("Missing " + name + ", expected to find a Byte");
}
}
public static byte getAsByte(JsonObject object, String name, byte def) {
return object.has(name) ? convertToByte(object.get(name), name) : def;
}
public static char convertToCharacter(JsonElement element, String name) {
if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
return element.getAsCharacter();
} else {
throw new JsonSyntaxException("Expected " + name + " to be a Character, was " + getType(element));
}
}
public static char getAsCharacter(JsonObject object, String name) {
if (object.has(name)) {
return convertToCharacter(object.get(name), name);
} else {
throw new JsonSyntaxException("Missing " + name + ", expected to find a Character");
}
}
public static char getAsCharacter(JsonObject object, String name, char def) {
return object.has(name) ? convertToCharacter(object.get(name), name) : def;
}
public static BigDecimal convertToBigDecimal(JsonElement element, String name) {
if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
return element.getAsBigDecimal();
} else {
throw new JsonSyntaxException("Expected " + name + " to be a BigDecimal, was " + getType(element));
}
}
public static BigDecimal getAsBigDecimal(JsonObject object, String name) {
if (object.has(name)) {
return convertToBigDecimal(object.get(name), name);
} else {
throw new JsonSyntaxException("Missing " + name + ", expected to find a BigDecimal");
}
}
public static BigDecimal getAsBigDecimal(JsonObject object, String name, BigDecimal def) {
return object.has(name) ? convertToBigDecimal(object.get(name), name) : def;
}
public static BigInteger convertToBigInteger(JsonElement element, String name) {
if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
return element.getAsBigInteger();
} else {
throw new JsonSyntaxException("Expected " + name + " to be a BigInteger, was " + getType(element));
}
}
public static BigInteger getAsBigInteger(JsonObject object, String name) {
if (object.has(name)) {
return convertToBigInteger(object.get(name), name);
} else {
throw new JsonSyntaxException("Missing " + name + ", expected to find a BigInteger");
}
}
public static BigInteger getAsBigInteger(JsonObject object, String name, BigInteger def) {
return object.has(name) ? convertToBigInteger(object.get(name), name) : def;
}
public static short convertToShort(JsonElement element, String name) {
if (element.isJsonPrimitive() && element.getAsJsonPrimitive().isNumber()) {
return element.getAsShort();
} else {
throw new JsonSyntaxException("Expected " + name + " to be a Short, was " + getType(element));
}
}
public static short getAsShort(JsonObject object, String name) {
if (object.has(name)) {
return convertToShort(object.get(name), name);
} else {
throw new JsonSyntaxException("Missing " + name + ", expected to find a Short");
}
}
public static short getAsShort(JsonObject object, String name, short def) {
return object.has(name) ? convertToShort(object.get(name), name) : def;
}
public static JsonObject convertToJsonObject(JsonElement element, String name) {
if (element.isJsonObject()) {
return element.getAsJsonObject();
} else {
throw new JsonSyntaxException("Expected " + name + " to be a JsonObject, was " + getType(element));
}
}
public static JsonObject getAsJsonObject(JsonObject object, String name) {
if (object.has(name)) {
return convertToJsonObject(object.get(name), name);
} else {
throw new JsonSyntaxException("Missing " + name + ", expected to find a JsonObject");
}
}
@Contract("_,_,!null->!null;_,_,null->_")
public static @Nullable JsonObject getAsJsonObject(JsonObject object, String name, @Nullable JsonObject def) {
return object.has(name) ? convertToJsonObject(object.get(name), name) : def;
}
public static JsonArray convertToJsonArray(JsonElement element, String name) {
if (element.isJsonArray()) {
return element.getAsJsonArray();
} else {
throw new JsonSyntaxException("Expected " + name + " to be a JsonArray, was " + getType(element));
}
}
public static JsonArray getAsJsonArray(JsonObject object, String name) {
if (object.has(name)) {
return convertToJsonArray(object.get(name), name);
} else {
throw new JsonSyntaxException("Missing " + name + ", expected to find a JsonArray");
}
}
@Contract("_,_,!null->!null;_,_,null->_")
public static @Nullable JsonArray getAsJsonArray(JsonObject object, String name, @Nullable JsonArray def) {
return object.has(name) ? convertToJsonArray(object.get(name), name) : def;
}
public static <T> T convertToObject(@Nullable JsonElement element, String name, JsonDeserializationContext context, Class<? extends T> clazz) {
if (element != null) {
return context.deserialize(element, clazz);
} else {
throw new JsonSyntaxException("Missing " + name);
}
}
public static <T> T getAsObject(JsonObject object, String name, JsonDeserializationContext context, Class<? extends T> clazz) {
if (object.has(name)) {
return convertToObject(object.get(name), name, context, clazz);
} else {
throw new JsonSyntaxException("Missing " + name);
}
}
@Contract("_,_,!null,_,_->!null;_,_,null,_,_->_")
public static <T> @Nullable T getAsObject(JsonObject object, String name, @Nullable T def, JsonDeserializationContext context, Class<? extends T> clazz) {
return object.has(name) ? convertToObject(object.get(name), name, context, clazz) : def;
}
public static String getType(@Nullable JsonElement element) {
String value = StringUtils.abbreviateMiddle(String.valueOf(element), "...", 10);
if (element == null) {
return "null (missing)";
} else if (element.isJsonNull()) {
return "null (json)";
} else if (element.isJsonArray()) {
return "an array (" + value + ")";
} else if (element.isJsonObject()) {
return "an object (" + value + ")";
} else {
if (element.isJsonPrimitive()) {
JsonPrimitive primitive = element.getAsJsonPrimitive();
if (primitive.isNumber()) {
return "a number (" + value + ")";
}
if (primitive.isBoolean()) {
return "a boolean (" + value + ")";
}
}
return value;
}
}
public static <T> T fromJson(Gson gson, Reader reader, Class<T> type) {
try {
JsonReader jsonReader = new JsonReader(reader);
jsonReader.setStrictness(Strictness.STRICT);
T result = gson.getAdapter(type).read(jsonReader);
if (result == null) {
throw new JsonParseException("JSON data was null or empty");
} else {
return result;
}
} catch (IOException var5) {
throw new JsonParseException(var5);
}
}
public static <T> @Nullable T fromNullableJson(Gson gson, Reader reader, TypeToken<T> type) {
try {
JsonReader jsonReader = new JsonReader(reader);
jsonReader.setStrictness(Strictness.STRICT);
return gson.getAdapter(type).read(jsonReader);
} catch (IOException var4) {
throw new JsonParseException(var4);
}
}
public static <T> T fromJson(Gson gson, Reader reader, TypeToken<T> type) {
T result = fromNullableJson(gson, reader, type);
if (result == null) {
throw new JsonParseException("JSON data was null or empty");
} else {
return result;
}
}
public static <T> @Nullable T fromNullableJson(Gson gson, String string, TypeToken<T> type) {
return fromNullableJson(gson, new StringReader(string), type);
}
public static <T> T fromJson(Gson gson, String string, Class<T> type) {
return fromJson(gson, new StringReader(string), type);
}
public static JsonObject parse(String string) {
return parse(new StringReader(string));
}
public static JsonObject parse(Reader reader) {
return fromJson(GSON, reader, JsonObject.class);
}
public static JsonArray parseArray(String string) {
return parseArray(new StringReader(string));
}
public static JsonArray parseArray(Reader reader) {
return fromJson(GSON, reader, JsonArray.class);
}
public static String toStableString(JsonElement jsonElement) {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter);
try {
writeValue(jsonWriter, jsonElement, Comparator.naturalOrder());
} catch (IOException var4) {
throw new AssertionError(var4);
}
return stringWriter.toString();
}
public static void writeValue(JsonWriter out, @Nullable JsonElement value, @Nullable Comparator<String> keyComparator) throws IOException {
if (value == null || value.isJsonNull()) {
out.nullValue();
} else if (value.isJsonPrimitive()) {
JsonPrimitive primitive = value.getAsJsonPrimitive();
if (primitive.isNumber()) {
out.value(primitive.getAsNumber());
} else if (primitive.isBoolean()) {
out.value(primitive.getAsBoolean());
} else {
out.value(primitive.getAsString());
}
} else if (value.isJsonArray()) {
out.beginArray();
for (JsonElement e : value.getAsJsonArray()) {
writeValue(out, e, keyComparator);
}
out.endArray();
} else {
if (!value.isJsonObject()) {
throw new IllegalArgumentException("Couldn't write " + value.getClass());
}
out.beginObject();
for (Entry<String, JsonElement> e : sortByKeyIfNeeded(value.getAsJsonObject().entrySet(), keyComparator)) {
out.name(e.getKey());
writeValue(out, e.getValue(), keyComparator);
}
out.endObject();
}
}
private static Collection<Entry<String, JsonElement>> sortByKeyIfNeeded(
Collection<Entry<String, JsonElement>> elements, @Nullable Comparator<String> keyComparator
) {
if (keyComparator == null) {
return elements;
} else {
List<Entry<String, JsonElement>> sorted = new ArrayList<>(elements);
sorted.sort(Entry.comparingByKey(keyComparator));
return sorted;
}
}
public static boolean encodesLongerThan(JsonElement element, int limit) {
try {
Streams.write(element, new JsonWriter(Streams.writerForAppendable(new GsonHelper.CountedAppendable(limit))));
return false;
} catch (IllegalStateException var3) {
return true;
} catch (IOException var4) {
throw new UncheckedIOException(var4);
}
}
private static class CountedAppendable implements Appendable {
private int totalCount;
private final int limit;
public CountedAppendable(int limit) {
this.limit = limit;
}
private Appendable accountChars(int count) {
this.totalCount += count;
if (this.totalCount > this.limit) {
throw new IllegalStateException("Character count over limit: " + this.totalCount + " > " + this.limit);
} else {
return this;
}
}
@Override
public Appendable append(CharSequence csq) {
return this.accountChars(csq.length());
}
@Override
public Appendable append(CharSequence csq, int start, int end) {
return this.accountChars(end - start);
}
@Override
public Appendable append(char c) {
return this.accountChars(1);
}
}
}引用的其他类
-
- 引用位置:
参数/返回值
- 引用位置:
-
ClientboundPlayerInfoUpdatePacket
- 引用位置:
参数
- 引用位置:
-
- 引用位置:
方法调用 - 关联成员:
Identifier.parse()
- 引用位置:
-
- 引用位置:
参数/返回值
- 引用位置: