Class JsonUtils

java.lang.Object
valorless.valorlessutils.json.JsonUtils

public final class JsonUtils extends Object
Utility class for working with JSON using Gson. Provides methods for serializing and deserializing objects, including pretty-printing and Bukkit-aware handling.
  • Method Details

    • toJson

      public static String toJson(@Nullable Object object)
      Converts an object to a JSON string using the standard Gson instance. The object may be a primitive, Map, List, null, or custom supported types.
      Parameters:
      object - The object to serialize (nullable)
      Returns:
      JSON string representation
    • toPrettyJson

      public static String toPrettyJson(@Nullable Object object)
      Converts an object to a pretty-printed JSON string.
      Parameters:
      object - The object to serialize (nullable)
      Returns:
      JSON string with pretty formatting
    • fromPlainJson

      public static <T> T fromPlainJson(@Nullable String json) throws IllegalArgumentException
      Deserializes a JSON string into a plain Java object. Does not handle ConfigurationSerializable objects.
      Type Parameters:
      T - The expected return type
      Parameters:
      json - JSON string to deserialize
      Returns:
      Deserialized object
      Throws:
      IllegalArgumentException - if deserialization fails
    • fromJson

      public static <T> @Nullable T fromJson(@Nullable String json) throws IllegalArgumentException
      Deserializes a JSON string into an object using Bukkit-aware handling. Can deserialize ConfigurationSerializable objects.
      Type Parameters:
      T - The expected return type
      Parameters:
      json - JSON string to deserialize
      Returns:
      Deserialized object or null if input is null
      Throws:
      IllegalArgumentException - if deserialization fails