Interface JsonSerializable<T extends JsonSerializable<T>>
- Type Parameters:
T
- The type of the object that is JSON capable.
Since deserialization needs to work without an instance of the class, implementing this interface it's assumed the
class has a static method fromJson(JsonReader)
that deserializes an instance of that class. The contract for
reading JSON from JsonReader
is that the initial state of the reader on call will either be a null
JsonToken
or be the JsonToken
after the JsonToken.FIELD_NAME
for the object. So, for objects
calling out to other JsonSerializable
objects for deserialization, they'll pass the reader pointing to the token
after the JsonToken.FIELD_NAME
. This way objects reading JSON will be self-encapsulated for reading properly
formatted JSON. And, if an error occurs during deserialization an IllegalStateException
should be thrown.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends JsonSerializable<T>>
TfromJson
(JsonReader jsonReader) Reads a JSON stream into an object.toJson
(JsonWriter jsonWriter) Writes the object to the passedJsonWriter
.
-
Method Details
-
toJson
Writes the object to the passedJsonWriter
.The contract for writing JSON to
JsonWriter
is that the object being written will handle opening and closing its own JSON object. So, for objects calling out to otherJsonSerializable
objects for serialization, they'll write the field name only then pass theJsonWriter
to the otherJsonSerializable
object. This way objects writing JSON will be self-encapsulated for writing properly formatted JSON.- Parameters:
jsonWriter
- Where the object's JSON will be written.- Returns:
- The
JsonWriter
where the JSON was written.
-
fromJson
Reads a JSON stream into an object.Implementations of
JsonSerializable
must define this method, otherwise anUnsupportedOperationException
will be thrown.- Type Parameters:
T
- The type of the object.- Parameters:
jsonReader
- TheJsonReader
being read.- Returns:
- The object that the JSON stream represented, may return null.
-