Interface JsonSerializable<T extends JsonSerializable<T>>
- Type Parameters:
T- The type of the object that is JSON serializable.
- All Known Implementing Classes:
JsonArray,JsonBoolean,JsonElement,JsonNull,JsonNumber,JsonObject,JsonString
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.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends JsonSerializable<T>>
TfromJson(byte[] bytes) Convenience method for reading a JSON byte array into an object.static <T extends JsonSerializable<T>>
TfromJson(JsonReader jsonReader) Reads a JSON stream into an object.static <T extends JsonSerializable<T>>
TfromJson(InputStream inputStream) Convenience method for reading a JSONInputStreaminto an object.static <T extends JsonSerializable<T>>
TConvenience method for reading a JSONReaderinto an object.static <T extends JsonSerializable<T>>
TConvenience method for reading a JSON string into an object.toJson(JsonWriter jsonWriter) Writes the object to the passedJsonWriter.default voidtoJson(OutputStream outputStream) Convenience method for writing theJsonSerializableto the passedOutputStream.default voidConvenience method for writing theJsonSerializableto the passedWriter.default byte[]Convenience method for writing theJsonSerializableto a byte array.default StringConvenience method for writing theJsonSerializableto a JSON string.
-
Method Details
-
toJson
Writes the object to the passedJsonWriter.The contract for writing JSON to
JsonWriteris that the object being written will handle opening and closing its own JSON object. So, for objects calling out to otherJsonSerializableobjects for serialization, they'll write the field name only then pass theJsonWriterto the otherJsonSerializableobject. 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
JsonWriterwhere the JSON was written. - Throws:
IOException- If the object fails to be written to thejsonWriter.
-
toJson
Convenience method for writing theJsonSerializableto the passedOutputStream.- Parameters:
outputStream- TheOutputStreamto write the JSON to.- Throws:
IOException- If the object fails to be written to theoutputStream.
-
toJson
Convenience method for writing theJsonSerializableto the passedWriter.- Parameters:
writer- TheWriterto write the JSON to.- Throws:
IOException- If the object fails to be written to thewriter.
-
toJsonString
Convenience method for writing theJsonSerializableto a JSON string.- Returns:
- The JSON string representing the object.
- Throws:
IOException- If the object fails to be written as a JSON string.
-
toJsonBytes
Convenience method for writing theJsonSerializableto a byte array.- Returns:
- The byte array representing the object.
- Throws:
IOException- If the object fails to be written as a byte array.
-
fromJson
Reads a JSON stream into an object.Implementations of
JsonSerializablemust define this method, otherwise anUnsupportedOperationExceptionwill be thrown.- Type Parameters:
T- The type of the object.- Parameters:
jsonReader- TheJsonReaderbeing read.- Returns:
- The object that the JSON stream represented, may return null.
- Throws:
IOException- If an object fails to be read from thejsonReader.
-
fromJson
Convenience method for reading a JSON string into an object.- Type Parameters:
T- The type of the object.- Parameters:
string- The JSON string to read.- Returns:
- The object that the JSON string represented, may return null.
- Throws:
IOException- If an object fails to be read from thestring.
-
fromJson
Convenience method for reading a JSON byte array into an object.- Type Parameters:
T- The type of the object.- Parameters:
bytes- The JSON byte array to read.- Returns:
- The object that the JSON byte array represented, may return null.
- Throws:
IOException- If an object fails to be read from thebytes.
-
fromJson
Convenience method for reading a JSONInputStreaminto an object.- Type Parameters:
T- The type of the object.- Parameters:
inputStream- The JSONInputStreamto read.- Returns:
- The object that the JSON
InputStreamrepresented, may return null. - Throws:
IOException- If an object fails to be read from theinputStream.
-
fromJson
Convenience method for reading a JSONReaderinto an object.- Type Parameters:
T- The type of the object.- Parameters:
reader- The JSONReaderto read.- Returns:
- The object that the JSON
Readerrepresented, may return null. - Throws:
IOException- If an object fails to be read from thereader.
-