Class BukkitAwareObjectTypeAdapter

java.lang.Object
com.google.gson.TypeAdapter<Object>
valorless.valorlessutils.json.YamlLikeObjectTypeAdapter
valorless.valorlessutils.json.BukkitAwareObjectTypeAdapter

public class BukkitAwareObjectTypeAdapter extends YamlLikeObjectTypeAdapter
A Gson TypeAdapter that deserializes any kind of Json primitive (Objects/Maps, Lists, Strings, numbers, etc.), but additionally also deserializes any contained ConfigurationSerializables.

For objects that are not serialized ConfigurationSerializables, this TypeAdapter behaves similar to Gson's default Object TypeAdapter, with the few exceptions mentioned by YamlLikeObjectTypeAdapter. This TypeAdapter can therefore be used when the type of the object to deserialize is not known in advance, but may contain serialized ConfigurationSerializables.

This TypeAdapter can be registered with a Gson instance, which is then able to serialize any ConfigurationSerializables. However, since Gson does not allow its default Object TypeAdapter to be overridden, this TypeAdapter has to be explicitly invoked when deserializing objects of unknown type: Calling Gson.fromJson(json, Object.class) will not invoke this TypeAdapter and therefore also not deserialize any ConfigurationSerializables. If you have a Gson instance that has this TypeAdapter registered, you can use fromJson(Gson, String) as a convenient way to parse a given Json input using this registered TypeAdapter.

However, note that even though the serialization and deserialization of ConfigurationSerializables may work in most cases, Json has some differences to Yaml that make it unsuited to reliably serialize and deserialize ConfigurationSerializables in general. Some of these differences are handled by YamlLikeObjectTypeAdapter. Another noteworthy difference is that Yaml supports the representation of object hierarchies, i.e. the references between objects, via so called 'anchors': If one object is referenced by multiple other objects, Yaml can restore this object hierarchy, whereas Json by default can not. There are some extensions to Json that intend to make it possible to persist such object references. However, neither Gson nor this TypeAdapter implement one of these solutions yet. For maximum compatibility with Bukkit's serialization API, it is therefore recommended sticking to Yaml when the Json format is not strictly required.

  • Field Summary

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

    Constructors
    Modifier
    Constructor
    Description
    protected
    BukkitAwareObjectTypeAdapter(com.google.gson.Gson gson)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static com.google.gson.TypeAdapter<Object>
    create(com.google.gson.Gson gson)
    Creates a BukkitAwareObjectTypeAdapter for the given Gson instance.
    static <T> @Nullable T
    fromJson(com.google.gson.Gson gson, @Nullable String json)
    Shortcut to conveniently parse an object of unknown type from the given Json String using the BukkitAwareObjectTypeAdapter from the given Gson instance.
    @Nullable Object
    read(@Nullable com.google.gson.stream.JsonReader in)
     
    void
    write(@Nullable com.google.gson.stream.JsonWriter out, @Nullable Object value)
     

    Methods inherited from class com.google.gson.TypeAdapter

    fromJson, fromJson, fromJsonTree, nullSafe, toJson, toJson, toJsonTree

    Methods inherited from class java.lang.Object

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

    • FACTORY

      public static final com.google.gson.TypeAdapterFactory FACTORY
  • Constructor Details

    • BukkitAwareObjectTypeAdapter

      protected BukkitAwareObjectTypeAdapter(com.google.gson.Gson gson)
  • Method Details

    • create

      public static com.google.gson.TypeAdapter<Object> create(com.google.gson.Gson gson)
      Creates a BukkitAwareObjectTypeAdapter for the given Gson instance.
      Parameters:
      gson - the Gson instance, not null
      Returns:
      the created TypeAdapter
    • fromJson

      public static <T> @Nullable T fromJson(com.google.gson.Gson gson, @Nullable String json) throws IllegalArgumentException
      Shortcut to conveniently parse an object of unknown type from the given Json String using the BukkitAwareObjectTypeAdapter from the given Gson instance.

      The Gson instance is expected to have the BukkitAwareObjectTypeAdapter registered as the TypeAdapter for ConfigurationSerializable. For best compatibility with Bukkit's serialization API, it is also recommended for the Gson instance to be configured to be lenient.

      Type Parameters:
      T - the type of the expected object, or Object if unknown
      Parameters:
      gson - the Gson instance, not null
      json - the Json String to parse
      Returns:
      the parsed object, or null if the Json input is null or empty
      Throws:
      IllegalArgumentException - if the Json could not be parsed or the object could not be deserialized correctly
    • read

      public @Nullable Object read(@Nullable com.google.gson.stream.JsonReader in) throws IOException
      Overrides:
      read in class YamlLikeObjectTypeAdapter
      Throws:
      IOException
    • write

      public void write(@Nullable com.google.gson.stream.JsonWriter out, @Nullable Object value) throws IOException
      Overrides:
      write in class YamlLikeObjectTypeAdapter
      Throws:
      IOException