Class BukkitAwareObjectTypeAdapter
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 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic com.google.gson.TypeAdapter<Object>
create
(com.google.gson.Gson gson) Creates aBukkitAwareObjectTypeAdapter
for the givenGson
instance.static <T> @Nullable T
Shortcut to conveniently parse an object of unknown type from the given Json String using theBukkitAwareObjectTypeAdapter
from the givenGson
instance.@Nullable Object
read
(@Nullable com.google.gson.stream.JsonReader in) void
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
-
BukkitAwareObjectTypeAdapter
protected BukkitAwareObjectTypeAdapter(com.google.gson.Gson gson)
-
-
Method Details
-
create
Creates aBukkitAwareObjectTypeAdapter
for the givenGson
instance.- Parameters:
gson
- the Gson instance, notnull
- 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 theBukkitAwareObjectTypeAdapter
from the givenGson
instance.The Gson instance is expected to have the
BukkitAwareObjectTypeAdapter
registered as theTypeAdapter
forConfigurationSerializable
. For best compatibility with Bukkit's serialization API, it is also recommended for the Gson instance to be configured to belenient
.- Type Parameters:
T
- the type of the expected object, orObject
if unknown- Parameters:
gson
- the Gson instance, notnull
json
- the Json String to parse- Returns:
- the parsed object, or
null
if the Json input isnull
or empty - Throws:
IllegalArgumentException
- if the Json could not be parsed or the object could not be deserialized correctly
-
read
- Overrides:
read
in classYamlLikeObjectTypeAdapter
- Throws:
IOException
-
write
public void write(@Nullable com.google.gson.stream.JsonWriter out, @Nullable Object value) throws IOException - Overrides:
write
in classYamlLikeObjectTypeAdapter
- Throws:
IOException
-