Class GsonHelper

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

public class GsonHelper extends Object
  • Field Details

    • GSON

      private static final Gson GSON
  • Constructor Details

    • GsonHelper

      public GsonHelper()
  • Method Details

    • isStringValue

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

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

      public static boolean isNumberValue(JsonObject json, String memberName)
    • isNumberValue

      public static boolean isNumberValue(JsonElement json)
    • isBooleanValue

      public static boolean isBooleanValue(JsonObject json, String memberName)
    • isBooleanValue

      public static boolean isBooleanValue(JsonElement json)
    • isArrayNode

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

      public static boolean isObjectNode(JsonObject json, String memberName)
    • isValidPrimitive

      public static boolean isValidPrimitive(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 JsonObject json, String memberName)
      Does the given JsonObject contain a field with the given name?
    • getNonNull

      public static JsonElement getNonNull(JsonObject json, String memberName)
    • convertToString

      public static String convertToString(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(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(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(JsonElement json, String memberName)
    • getAsItem

      public static Holder<Item> getAsItem(JsonObject json, String memberName)
    • getAsItem

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

      public static boolean convertToBoolean(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(JsonObject json, String memberName)
      Gets the boolean value of the field on the JsonObject with the given name.
    • getAsBoolean

      public static boolean getAsBoolean(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(JsonElement json, String memberName)
    • getAsDouble

      public static double getAsDouble(JsonObject json, String memberName)
    • getAsDouble

      public static double getAsDouble(JsonObject json, String memberName, double fallback)
    • convertToFloat

      public static float convertToFloat(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(JsonObject json, String memberName)
      Gets the float value of the field on the JsonObject with the given name.
    • getAsFloat

      public static float getAsFloat(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(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(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(JsonObject json, String memberName, long fallback)
    • convertToInt

      public static int convertToInt(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(JsonObject json, String memberName)
      Gets the integer value of the field on the JsonObject with the given name.
    • getAsInt

      public static int getAsInt(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(JsonElement json, String memberName)
    • getAsByte

      public static byte getAsByte(JsonObject json, String memberName)
    • getAsByte

      public static byte getAsByte(JsonObject json, String memberName, byte fallback)
    • convertToCharacter

      public static char convertToCharacter(JsonElement json, String memberName)
    • getAsCharacter

      public static char getAsCharacter(JsonObject json, String memberName)
    • getAsCharacter

      public static char getAsCharacter(JsonObject json, String memberName, char fallback)
    • convertToBigDecimal

      public static BigDecimal convertToBigDecimal(JsonElement json, String memberName)
    • getAsBigDecimal

      public static BigDecimal getAsBigDecimal(JsonObject json, String memberName)
    • getAsBigDecimal

      public static BigDecimal getAsBigDecimal(JsonObject json, String memberName, BigDecimal fallback)
    • convertToBigInteger

      public static BigInteger convertToBigInteger(JsonElement json, String memberName)
    • getAsBigInteger

      public static BigInteger getAsBigInteger(JsonObject json, String memberName)
    • getAsBigInteger

      public static BigInteger getAsBigInteger(JsonObject json, String memberName, BigInteger fallback)
    • convertToShort

      public static short convertToShort(JsonElement json, String memberName)
    • getAsShort

      public static short getAsShort(JsonObject json, String memberName)
    • getAsShort

      public static short getAsShort(JsonObject json, String memberName, short fallback)
    • convertToJsonObject

      public static JsonObject convertToJsonObject(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 JsonObject getAsJsonObject(JsonObject json, String memberName)
    • getAsJsonObject

      @Nullable @Contract("_,_,!null->!null;_,_,null->_") public static JsonObject getAsJsonObject(JsonObject json, String memberName, @Nullable 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 JsonArray convertToJsonArray(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 JsonArray getAsJsonArray(JsonObject json, String memberName)
      Gets the JsonArray field on the JsonObject with the given name.
    • getAsJsonArray

      @Nullable @Contract("_,_,!null->!null;_,_,null->_") public static JsonArray getAsJsonArray(JsonObject json, String memberName, @Nullable 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 JsonElement json, String memberName, JsonDeserializationContext context, Class<? extends T> adapter)
    • getAsObject

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

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

      public static String getType(@Nullable 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(Gson gson, Reader reader, Class<T> adapter, boolean lenient)
    • fromJson

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

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

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

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

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

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

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

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

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

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

      public static JsonObject parse(String json, boolean lenient)
    • parse

      public static JsonObject parse(Reader reader, boolean lenient)
    • parse

      public static JsonObject parse(String json)
    • parse

      public static JsonObject parse(Reader reader)
    • parseArray

      public static JsonArray parseArray(String string)
    • parseArray

      public static JsonArray parseArray(Reader reader)
    • toStableString

      public static String toStableString(JsonElement json)
    • writeValue

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

      private static Collection<Map.Entry<String,JsonElement>> sortByKeyIfNeeded(Collection<Map.Entry<String,JsonElement>> entries, @Nullable Comparator<String> sorter)
    • encodesLongerThan

      public static boolean encodesLongerThan(JsonElement element, int maxLength)