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 node, String name)
      Does the given JsonObject contain a string field with the given name?
    • isStringValue

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

      public static boolean isNumberValue(JsonObject node, String name)
    • isNumberValue

      public static boolean isNumberValue(JsonElement node)
    • isBooleanValue

      public static boolean isBooleanValue(JsonObject node, String name)
    • isBooleanValue

      public static boolean isBooleanValue(JsonElement node)
    • isArrayNode

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

      public static boolean isObjectNode(JsonObject node, String name)
    • isValidPrimitive

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

      public static JsonElement getNonNull(JsonObject object, String name)
    • convertToString

      public static String convertToString(JsonElement element, String name)
      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 object, String name)
      Gets the string value of the field on the JsonObject with the given name.
    • getAsString

      @Contract("_,_,!null->!null;_,_,null->_") public static @Nullable String getAsString(JsonObject object, String name, @Nullable String def)
      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 element, String name)
    • getAsItem

      public static Holder<Item> getAsItem(JsonObject object, String name)
    • getAsItem

      @Contract("_,_,!null->!null;_,_,null->_") public static @Nullable Holder<Item> getAsItem(JsonObject object, String name, @Nullable Holder<Item> def)
    • convertToBoolean

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

      public static boolean getAsBoolean(JsonObject object, String name, boolean def)
      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 element, String name)
    • getAsDouble

      public static double getAsDouble(JsonObject object, String name)
    • getAsDouble

      public static double getAsDouble(JsonObject object, String name, double def)
    • convertToFloat

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

      public static float getAsFloat(JsonObject object, String name, float def)
      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 element, String name)
      Gets a long from a JSON element and validates that the value is actually a number.
    • getAsLong

      public static long getAsLong(JsonObject object, String name)
      Gets a long from a JSON element, throws an error if the member does not exist.
    • getAsLong

      public static long getAsLong(JsonObject object, String name, long def)
    • convertToInt

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

      public static int getAsInt(JsonObject object, String name, int def)
      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 element, String name)
    • getAsByte

      public static byte getAsByte(JsonObject object, String name)
    • getAsByte

      public static byte getAsByte(JsonObject object, String name, byte def)
    • convertToCharacter

      public static char convertToCharacter(JsonElement element, String name)
    • getAsCharacter

      public static char getAsCharacter(JsonObject object, String name)
    • getAsCharacter

      public static char getAsCharacter(JsonObject object, String name, char def)
    • convertToBigDecimal

      public static BigDecimal convertToBigDecimal(JsonElement element, String name)
    • getAsBigDecimal

      public static BigDecimal getAsBigDecimal(JsonObject object, String name)
    • getAsBigDecimal

      public static BigDecimal getAsBigDecimal(JsonObject object, String name, BigDecimal def)
    • convertToBigInteger

      public static BigInteger convertToBigInteger(JsonElement element, String name)
    • getAsBigInteger

      public static BigInteger getAsBigInteger(JsonObject object, String name)
    • getAsBigInteger

      public static BigInteger getAsBigInteger(JsonObject object, String name, BigInteger def)
    • convertToShort

      public static short convertToShort(JsonElement element, String name)
    • getAsShort

      public static short getAsShort(JsonObject object, String name)
    • getAsShort

      public static short getAsShort(JsonObject object, String name, short def)
    • convertToJsonObject

      public static JsonObject convertToJsonObject(JsonElement element, String name)
      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 object, String name)
    • getAsJsonObject

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

      @Contract("_,_,!null->!null;_,_,null->_") public static @Nullable JsonArray getAsJsonArray(JsonObject object, String name, @Nullable JsonArray def)
      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 element, String name, JsonDeserializationContext context, Class<? extends T> clazz)
    • getAsObject

      public static <T> T getAsObject(JsonObject object, String name, JsonDeserializationContext context, Class<? extends T> clazz)
    • getAsObject

      @Contract("_,_,!null,_,_->!null;_,_,null,_,_->_") public static <T> @Nullable T getAsObject(JsonObject object, String name, @Nullable T def, JsonDeserializationContext context, Class<? extends T> clazz)
    • getType

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

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

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

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

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

      public static <T> T fromJson(Gson gson, String string, Class<T> type)
    • parse

      public static JsonObject parse(String string)
    • 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 jsonElement)
    • writeValue

      public static void writeValue(JsonWriter out, @Nullable JsonElement value, @Nullable Comparator<String> keyComparator) throws IOException
      Throws:
      IOException
    • sortByKeyIfNeeded

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

      public static boolean encodesLongerThan(JsonElement element, int limit)