Class YamlLikeObjectTypeAdapter

java.lang.Object
com.google.gson.TypeAdapter<Object>
valorless.valorlessutils.json.YamlLikeObjectTypeAdapter
Direct Known Subclasses:
BukkitAwareObjectTypeAdapter

public class YamlLikeObjectTypeAdapter extends com.google.gson.TypeAdapter<Object>
Modified version of Gson's default Object TypeAdapter.

Bukkit's serialization API is built around Yaml, and specifically SnakeYaml. Objects that are deserialized via Bukkit's serialization API may have strict expectations regarding the types of data they encounter during deserialization. This also applies to some of Bukkit's built-in serializable types, such as ItemMeta. For compatibility reasons, this TypeAdapter is supposed to produce types that more closely match those produced by SnakeYaml. Most notable differences to Gson's default types are:

  • We produce LinkedHashMap's instead of Gson's LinkedTreeMap.
  • We delegate the parsing of numbers to SnakeYaml, which produces integers, longs, or BigInteger for whole numbers and doubles for fractional numbers, whereas Gson always produces doubles by default.
  • Gson represents special numbers like Double.NaN or infinities as Strings, but doesn't automatically return them as double anymore. We check if a loaded String can be parsed as one of these special numbers, and then return this number instead.

Gson does not allow the default Object TypeAdapter to be overridden. This TypeAdapter therefore has to be explicitly invoked whenever it is supposed to be used.

Another alternative could be to deserialize objects from Json via a Yaml parser: In principle, since Json is supposed to be a subset of Yaml, this should work. However, there is no guarantee that the produced Json actually conforms to standard Json: For instance, Json does not support special numbers like Double.NaN or infinities. Their representation is therefore specific to the used Json generator and there is no guarantee that a certain Yaml parser is then able to parse those numbers correctly.

There are further limitations that make Json less suited for the serialization of ConfigurationSerializables: See BukkitAwareObjectTypeAdapter.

  • Field Summary

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

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

    Modifier and Type
    Method
    Description
    static com.google.gson.TypeAdapter<Object>
    create(com.google.gson.Gson gson)
     
    @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

    • YamlLikeObjectTypeAdapter

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

    • create

      public static com.google.gson.TypeAdapter<Object> create(com.google.gson.Gson gson)
    • read

      public @Nullable Object read(@Nullable com.google.gson.stream.JsonReader in) throws IOException
      Specified by:
      read in class com.google.gson.TypeAdapter<Object>
      Throws:
      IOException
    • write

      public void write(@Nullable com.google.gson.stream.JsonWriter out, @Nullable Object value) throws IOException
      Specified by:
      write in class com.google.gson.TypeAdapter<Object>
      Throws:
      IOException