Class GsonHelper

java.lang.Object
net.minecraft.util.GsonHelper

public class GsonHelper extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final com.google.gson.Gson
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static BigDecimal
    convertToBigDecimal(com.google.gson.JsonElement json, String memberName)
     
    static BigInteger
    convertToBigInteger(com.google.gson.JsonElement json, String memberName)
     
    static boolean
    convertToBoolean(com.google.gson.JsonElement json, String memberName)
    Gets the boolean value of the given JsonElement.
    static byte
    convertToByte(com.google.gson.JsonElement json, String memberName)
     
    static char
    convertToCharacter(com.google.gson.JsonElement json, String memberName)
     
    static double
    convertToDouble(com.google.gson.JsonElement json, String memberName)
     
    static float
    convertToFloat(com.google.gson.JsonElement json, String memberName)
    Gets the float value of the given JsonElement.
    static int
    convertToInt(com.google.gson.JsonElement json, String memberName)
    Gets the integer value of the given JsonElement.
    static Holder<Item>
    convertToItem(com.google.gson.JsonElement json, String memberName)
     
    static com.google.gson.JsonArray
    convertToJsonArray(com.google.gson.JsonElement json, String memberName)
    Gets the given JsonElement as a JsonArray.
    static com.google.gson.JsonObject
    convertToJsonObject(com.google.gson.JsonElement json, String memberName)
    Gets the given JsonElement as a JsonObject.
    static long
    convertToLong(com.google.gson.JsonElement json, String memberName)
    Gets a long from a JSON element and validates that the value is actually a number.
    static <T> T
    convertToObject(com.google.gson.JsonElement json, String memberName, com.google.gson.JsonDeserializationContext context, Class<? extends T> adapter)
     
    static short
    convertToShort(com.google.gson.JsonElement json, String memberName)
     
    static String
    convertToString(com.google.gson.JsonElement json, String memberName)
    Gets the string value of the given JsonElement.
    static <T> T
    fromJson(com.google.gson.Gson gson, Reader reader, com.google.gson.reflect.TypeToken<T> type)
     
    static <T> T
    fromJson(com.google.gson.Gson gson, Reader reader, com.google.gson.reflect.TypeToken<T> type, boolean lenient)
     
    static <T> T
    fromJson(com.google.gson.Gson gson, Reader reader, Class<T> jsonClass)
     
    static <T> T
    fromJson(com.google.gson.Gson gson, Reader reader, Class<T> adapter, boolean lenient)
     
    static <T> T
    fromJson(com.google.gson.Gson gson, String json, Class<T> adapter)
     
    static <T> T
    fromJson(com.google.gson.Gson gson, String json, Class<T> adapter, boolean lenient)
     
    static <T> T
    fromNullableJson(com.google.gson.Gson gson, Reader reader, com.google.gson.reflect.TypeToken<T> type, boolean lenient)
     
    static <T> T
    fromNullableJson(com.google.gson.Gson gson, Reader reader, Class<T> adapter, boolean lenient)
     
    static <T> T
    fromNullableJson(com.google.gson.Gson gson, String json, com.google.gson.reflect.TypeToken<T> type)
     
    static <T> T
    fromNullableJson(com.google.gson.Gson gson, String json, com.google.gson.reflect.TypeToken<T> type, boolean lenient)
     
    static <T> T
    fromNullableJson(com.google.gson.Gson gson, String json, Class<T> adapter, boolean lenient)
     
    static BigDecimal
    getAsBigDecimal(com.google.gson.JsonObject json, String memberName)
     
    static BigDecimal
    getAsBigDecimal(com.google.gson.JsonObject json, String memberName, BigDecimal fallback)
     
    static BigInteger
    getAsBigInteger(com.google.gson.JsonObject json, String memberName)
     
    static BigInteger
    getAsBigInteger(com.google.gson.JsonObject json, String memberName, BigInteger fallback)
     
    static boolean
    getAsBoolean(com.google.gson.JsonObject json, String memberName)
    Gets the boolean value of the field on the JsonObject with the given name.
    static boolean
    getAsBoolean(com.google.gson.JsonObject json, String memberName, boolean fallback)
    Gets the boolean value of the field on the JsonObject with the given name, or the given default value if the field is missing.
    static byte
    getAsByte(com.google.gson.JsonObject json, String memberName)
     
    static byte
    getAsByte(com.google.gson.JsonObject json, String memberName, byte fallback)
     
    static char
    getAsCharacter(com.google.gson.JsonObject json, String memberName)
     
    static char
    getAsCharacter(com.google.gson.JsonObject json, String memberName, char fallback)
     
    static double
    getAsDouble(com.google.gson.JsonObject json, String memberName)
     
    static double
    getAsDouble(com.google.gson.JsonObject json, String memberName, double fallback)
     
    static float
    getAsFloat(com.google.gson.JsonObject json, String memberName)
    Gets the float value of the field on the JsonObject with the given name.
    static float
    getAsFloat(com.google.gson.JsonObject json, String memberName, float fallback)
    Gets the float value of the field on the JsonObject with the given name, or the given default value if the field is missing.
    static int
    getAsInt(com.google.gson.JsonObject json, String memberName)
    Gets the integer value of the field on the JsonObject with the given name.
    static int
    getAsInt(com.google.gson.JsonObject json, String memberName, int fallback)
    Gets the integer value of the field on the JsonObject with the given name, or the given default value if the field is missing.
    static Holder<Item>
    getAsItem(com.google.gson.JsonObject json, String memberName)
     
    static Holder<Item>
    getAsItem(com.google.gson.JsonObject json, String memberName, Holder<Item> fallback)
     
    static com.google.gson.JsonArray
    getAsJsonArray(com.google.gson.JsonObject json, String memberName)
    Gets the JsonArray field on the JsonObject with the given name.
    static com.google.gson.JsonArray
    getAsJsonArray(com.google.gson.JsonObject json, String memberName, com.google.gson.JsonArray fallback)
    Gets the JsonArray field on the JsonObject with the given name, or the given default value if the field is missing.
    static com.google.gson.JsonObject
    getAsJsonObject(com.google.gson.JsonObject json, String memberName)
     
    static com.google.gson.JsonObject
    getAsJsonObject(com.google.gson.JsonObject json, String memberName, com.google.gson.JsonObject fallback)
    Gets the JsonObject field on the JsonObject with the given name, or the given default value if the field is missing.
    static long
    getAsLong(com.google.gson.JsonObject json, String memberName)
    Gets a long from a JSON element, throws an error if the member does not exist.
    static long
    getAsLong(com.google.gson.JsonObject json, String memberName, long fallback)
     
    static <T> T
    getAsObject(com.google.gson.JsonObject json, String memberName, com.google.gson.JsonDeserializationContext context, Class<? extends T> adapter)
     
    static <T> T
    getAsObject(com.google.gson.JsonObject json, String memberName, T fallback, com.google.gson.JsonDeserializationContext context, Class<? extends T> adapter)
     
    static short
    getAsShort(com.google.gson.JsonObject json, String memberName)
     
    static short
    getAsShort(com.google.gson.JsonObject json, String memberName, short fallback)
     
    static String
    getAsString(com.google.gson.JsonObject json, String memberName)
    Gets the string value of the field on the JsonObject with the given name.
    static String
    getAsString(com.google.gson.JsonObject json, String memberName, String fallback)
    Gets the string value of the field on the JsonObject with the given name, or the given default value if the field is missing.
    static com.google.gson.JsonElement
    getNonNull(com.google.gson.JsonObject json, String memberName)
     
    static String
    getType(com.google.gson.JsonElement json)
    Gets a human-readable description of the given JsonElement's type.
    static boolean
    isArrayNode(com.google.gson.JsonObject json, String memberName)
    Does the given JsonObject contain an array field with the given name?
    static boolean
    isBooleanValue(com.google.gson.JsonElement json)
     
    static boolean
    isBooleanValue(com.google.gson.JsonObject json, String memberName)
     
    static boolean
    isNumberValue(com.google.gson.JsonElement json)
     
    static boolean
    isNumberValue(com.google.gson.JsonObject json, String memberName)
     
    static boolean
    isObjectNode(com.google.gson.JsonObject json, String memberName)
     
    static boolean
    isStringValue(com.google.gson.JsonElement json)
    Is the given JsonElement a string?
    static boolean
    isStringValue(com.google.gson.JsonObject json, String memberName)
    Does the given JsonObject contain a string field with the given name?
    static boolean
    isValidNode(com.google.gson.JsonObject json, String memberName)
    Does the given JsonObject contain a field with the given name?
    static boolean
    isValidPrimitive(com.google.gson.JsonObject json, String memberName)
    Does the given JsonObject contain a field with the given name whose type is primitive (String, Java primitive, or Java primitive wrapper)?
    static com.google.gson.JsonObject
    parse(Reader reader)
     
    static com.google.gson.JsonObject
    parse(Reader reader, boolean lenient)
     
    static com.google.gson.JsonObject
    parse(String json)
     
    static com.google.gson.JsonObject
    parse(String json, boolean lenient)
     
    static com.google.gson.JsonArray
     
    static com.google.gson.JsonArray
     
    private static Collection<Map.Entry<String,com.google.gson.JsonElement>>
    sortByKeyIfNeeded(Collection<Map.Entry<String,com.google.gson.JsonElement>> entries, Comparator<String> sorter)
     
    static String
    toStableString(com.google.gson.JsonElement json)
     
    static void
    writeValue(com.google.gson.stream.JsonWriter writer, com.google.gson.JsonElement jsonElement, Comparator<String> sorter)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • GSON

      private static final com.google.gson.Gson GSON
  • Constructor Details

    • GsonHelper

      public GsonHelper()
  • Method Details

    • isStringValue

      public static boolean isStringValue(com.google.gson.JsonObject json, String memberName)
      Does the given JsonObject contain a string field with the given name?
    • isStringValue

      public static boolean isStringValue(com.google.gson.JsonElement json)
      Is the given JsonElement a string?
    • isNumberValue

      public static boolean isNumberValue(com.google.gson.JsonObject json, String memberName)
    • isNumberValue

      public static boolean isNumberValue(com.google.gson.JsonElement json)
    • isBooleanValue

      public static boolean isBooleanValue(com.google.gson.JsonObject json, String memberName)
    • isBooleanValue

      public static boolean isBooleanValue(com.google.gson.JsonElement json)
    • isArrayNode

      public static boolean isArrayNode(com.google.gson.JsonObject json, String memberName)
      Does the given JsonObject contain an array field with the given name?
    • isObjectNode

      public static boolean isObjectNode(com.google.gson.JsonObject json, String memberName)
    • isValidPrimitive

      public static boolean isValidPrimitive(com.google.gson.JsonObject json, String memberName)
      Does the given JsonObject contain a field with the given name whose type is primitive (String, Java primitive, or Java primitive wrapper)?
    • isValidNode

      public static boolean isValidNode(@Nullable com.google.gson.JsonObject json, String memberName)
      Does the given JsonObject contain a field with the given name?
    • getNonNull

      public static com.google.gson.JsonElement getNonNull(com.google.gson.JsonObject json, String memberName)
    • convertToString

      public static String convertToString(com.google.gson.JsonElement json, String memberName)
      Gets the string value of the given JsonElement. Expects the second parameter to be the name of the element's field if an error message needs to be thrown.
    • getAsString

      public static String getAsString(com.google.gson.JsonObject json, String memberName)
      Gets the string value of the field on the JsonObject with the given name.
    • getAsString

      @Nullable @Contract("_,_,!null->!null;_,_,null->_") public static String getAsString(com.google.gson.JsonObject json, String memberName, @Nullable String fallback)
      Gets the string value of the field on the JsonObject with the given name, or the given default value if the field is missing.
    • convertToItem

      public static Holder<Item> convertToItem(com.google.gson.JsonElement json, String memberName)
    • getAsItem

      public static Holder<Item> getAsItem(com.google.gson.JsonObject json, String memberName)
    • getAsItem

      @Nullable @Contract("_,_,!null->!null;_,_,null->_") public static Holder<Item> getAsItem(com.google.gson.JsonObject json, String memberName, @Nullable Holder<Item> fallback)
    • convertToBoolean

      public static boolean convertToBoolean(com.google.gson.JsonElement json, String memberName)
      Gets the boolean value of the given JsonElement. Expects the second parameter to be the name of the element's field if an error message needs to be thrown.
    • getAsBoolean

      public static boolean getAsBoolean(com.google.gson.JsonObject json, String memberName)
      Gets the boolean value of the field on the JsonObject with the given name.
    • getAsBoolean

      public static boolean getAsBoolean(com.google.gson.JsonObject json, String memberName, boolean fallback)
      Gets the boolean value of the field on the JsonObject with the given name, or the given default value if the field is missing.
    • convertToDouble

      public static double convertToDouble(com.google.gson.JsonElement json, String memberName)
    • getAsDouble

      public static double getAsDouble(com.google.gson.JsonObject json, String memberName)
    • getAsDouble

      public static double getAsDouble(com.google.gson.JsonObject json, String memberName, double fallback)
    • convertToFloat

      public static float convertToFloat(com.google.gson.JsonElement json, String memberName)
      Gets the float value of the given JsonElement. Expects the second parameter to be the name of the element's field if an error message needs to be thrown.
    • getAsFloat

      public static float getAsFloat(com.google.gson.JsonObject json, String memberName)
      Gets the float value of the field on the JsonObject with the given name.
    • getAsFloat

      public static float getAsFloat(com.google.gson.JsonObject json, String memberName, float fallback)
      Gets the float value of the field on the JsonObject with the given name, or the given default value if the field is missing.
    • convertToLong

      public static long convertToLong(com.google.gson.JsonElement json, String memberName)
      Gets a long from a JSON element and validates that the value is actually a number.
    • getAsLong

      public static long getAsLong(com.google.gson.JsonObject json, String memberName)
      Gets a long from a JSON element, throws an error if the member does not exist.
    • getAsLong

      public static long getAsLong(com.google.gson.JsonObject json, String memberName, long fallback)
    • convertToInt

      public static int convertToInt(com.google.gson.JsonElement json, String memberName)
      Gets the integer value of the given JsonElement. Expects the second parameter to be the name of the element's field if an error message needs to be thrown.
    • getAsInt

      public static int getAsInt(com.google.gson.JsonObject json, String memberName)
      Gets the integer value of the field on the JsonObject with the given name.
    • getAsInt

      public static int getAsInt(com.google.gson.JsonObject json, String memberName, int fallback)
      Gets the integer value of the field on the JsonObject with the given name, or the given default value if the field is missing.
    • convertToByte

      public static byte convertToByte(com.google.gson.JsonElement json, String memberName)
    • getAsByte

      public static byte getAsByte(com.google.gson.JsonObject json, String memberName)
    • getAsByte

      public static byte getAsByte(com.google.gson.JsonObject json, String memberName, byte fallback)
    • convertToCharacter

      public static char convertToCharacter(com.google.gson.JsonElement json, String memberName)
    • getAsCharacter

      public static char getAsCharacter(com.google.gson.JsonObject json, String memberName)
    • getAsCharacter

      public static char getAsCharacter(com.google.gson.JsonObject json, String memberName, char fallback)
    • convertToBigDecimal

      public static BigDecimal convertToBigDecimal(com.google.gson.JsonElement json, String memberName)
    • getAsBigDecimal

      public static BigDecimal getAsBigDecimal(com.google.gson.JsonObject json, String memberName)
    • getAsBigDecimal

      public static BigDecimal getAsBigDecimal(com.google.gson.JsonObject json, String memberName, BigDecimal fallback)
    • convertToBigInteger

      public static BigInteger convertToBigInteger(com.google.gson.JsonElement json, String memberName)
    • getAsBigInteger

      public static BigInteger getAsBigInteger(com.google.gson.JsonObject json, String memberName)
    • getAsBigInteger

      public static BigInteger getAsBigInteger(com.google.gson.JsonObject json, String memberName, BigInteger fallback)
    • convertToShort

      public static short convertToShort(com.google.gson.JsonElement json, String memberName)
    • getAsShort

      public static short getAsShort(com.google.gson.JsonObject json, String memberName)
    • getAsShort

      public static short getAsShort(com.google.gson.JsonObject json, String memberName, short fallback)
    • convertToJsonObject

      public static com.google.gson.JsonObject convertToJsonObject(com.google.gson.JsonElement json, String memberName)
      Gets the given JsonElement as a JsonObject. Expects the second parameter to be the name of the element's field if an error message needs to be thrown.
    • getAsJsonObject

      public static com.google.gson.JsonObject getAsJsonObject(com.google.gson.JsonObject json, String memberName)
    • getAsJsonObject

      @Nullable @Contract("_,_,!null->!null;_,_,null->_") public static com.google.gson.JsonObject getAsJsonObject(com.google.gson.JsonObject json, String memberName, @Nullable com.google.gson.JsonObject fallback)
      Gets the JsonObject field on the JsonObject with the given name, or the given default value if the field is missing.
    • convertToJsonArray

      public static com.google.gson.JsonArray convertToJsonArray(com.google.gson.JsonElement json, String memberName)
      Gets the given JsonElement as a JsonArray. Expects the second parameter to be the name of the element's field if an error message needs to be thrown.
    • getAsJsonArray

      public static com.google.gson.JsonArray getAsJsonArray(com.google.gson.JsonObject json, String memberName)
      Gets the JsonArray field on the JsonObject with the given name.
    • getAsJsonArray

      @Nullable @Contract("_,_,!null->!null;_,_,null->_") public static com.google.gson.JsonArray getAsJsonArray(com.google.gson.JsonObject json, String memberName, @Nullable com.google.gson.JsonArray fallback)
      Gets the JsonArray field on the JsonObject with the given name, or the given default value if the field is missing.
    • convertToObject

      public static <T> T convertToObject(@Nullable com.google.gson.JsonElement json, String memberName, com.google.gson.JsonDeserializationContext context, Class<? extends T> adapter)
    • getAsObject

      public static <T> T getAsObject(com.google.gson.JsonObject json, String memberName, com.google.gson.JsonDeserializationContext context, Class<? extends T> adapter)
    • getAsObject

      @Nullable @Contract("_,_,!null,_,_->!null;_,_,null,_,_->_") public static <T> T getAsObject(com.google.gson.JsonObject json, String memberName, @Nullable T fallback, com.google.gson.JsonDeserializationContext context, Class<? extends T> adapter)
    • getType

      public static String getType(@Nullable com.google.gson.JsonElement json)
      Gets a human-readable description of the given JsonElement's type. For example: "a number (4)"
    • fromNullableJson

      @Nullable public static <T> T fromNullableJson(com.google.gson.Gson gson, Reader reader, Class<T> adapter, boolean lenient)
    • fromJson

      public static <T> T fromJson(com.google.gson.Gson gson, Reader reader, Class<T> adapter, boolean lenient)
    • fromNullableJson

      @Nullable public static <T> T fromNullableJson(com.google.gson.Gson gson, Reader reader, com.google.gson.reflect.TypeToken<T> type, boolean lenient)
    • fromJson

      public static <T> T fromJson(com.google.gson.Gson gson, Reader reader, com.google.gson.reflect.TypeToken<T> type, boolean lenient)
    • fromNullableJson

      @Nullable public static <T> T fromNullableJson(com.google.gson.Gson gson, String json, com.google.gson.reflect.TypeToken<T> type, boolean lenient)
    • fromJson

      public static <T> T fromJson(com.google.gson.Gson gson, String json, Class<T> adapter, boolean lenient)
    • fromNullableJson

      @Nullable public static <T> T fromNullableJson(com.google.gson.Gson gson, String json, Class<T> adapter, boolean lenient)
    • fromJson

      public static <T> T fromJson(com.google.gson.Gson gson, Reader reader, com.google.gson.reflect.TypeToken<T> type)
    • fromNullableJson

      @Nullable public static <T> T fromNullableJson(com.google.gson.Gson gson, String json, com.google.gson.reflect.TypeToken<T> type)
    • fromJson

      public static <T> T fromJson(com.google.gson.Gson gson, Reader reader, Class<T> jsonClass)
    • fromJson

      public static <T> T fromJson(com.google.gson.Gson gson, String json, Class<T> adapter)
    • parse

      public static com.google.gson.JsonObject parse(String json, boolean lenient)
    • parse

      public static com.google.gson.JsonObject parse(Reader reader, boolean lenient)
    • parse

      public static com.google.gson.JsonObject parse(String json)
    • parse

      public static com.google.gson.JsonObject parse(Reader reader)
    • parseArray

      public static com.google.gson.JsonArray parseArray(String string)
    • parseArray

      public static com.google.gson.JsonArray parseArray(Reader reader)
    • toStableString

      public static String toStableString(com.google.gson.JsonElement json)
    • writeValue

      public static void writeValue(com.google.gson.stream.JsonWriter writer, @Nullable com.google.gson.JsonElement jsonElement, @Nullable Comparator<String> sorter) throws IOException
      Throws:
      IOException
    • sortByKeyIfNeeded

      private static Collection<Map.Entry<String,com.google.gson.JsonElement>> sortByKeyIfNeeded(Collection<Map.Entry<String,com.google.gson.JsonElement>> entries, @Nullable Comparator<String> sorter)