Class YamlLikeObjectTypeAdapter
- Direct Known Subclasses:
BukkitAwareObjectTypeAdapter
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 -
Constructor Summary
Constructors -
Method Summary
Methods inherited from class com.google.gson.TypeAdapter
fromJson, fromJson, fromJsonTree, nullSafe, toJson, toJson, toJsonTree
-
Field Details
-
FACTORY
public static final com.google.gson.TypeAdapterFactory FACTORY
-
-
Constructor Details
-
YamlLikeObjectTypeAdapter
protected YamlLikeObjectTypeAdapter(com.google.gson.Gson gson)
-
-
Method Details
-
create
-
read
- Specified by:
read
in classcom.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 classcom.google.gson.TypeAdapter<Object>
- Throws:
IOException
-