你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
JsonSerializable<T> Interface
Type Parameters
- T
The type of the object that is JSON serializable.
public interface JsonSerializable
Indicates that the implementing class can be serialized to and deserialized from JSON.
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 FIELD_NAME for the object. So, for objects calling out to other JsonSerializable<T> objects for deserialization, they'll pass the reader pointing to the token after the 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 Type | Method and Description |
---|---|
static T |
fromJson(byte[] bytes)
Convenience method for reading a JSON byte array into an object. |
static T |
fromJson(JsonReader jsonReader)
Reads a JSON stream into an object. |
static T |
fromJson(InputStream inputStream)
Convenience method for reading a JSON InputStream into an object. |
static T |
fromJson(Reader reader)
Convenience method for reading a JSON Reader into an object. |
static T |
fromJson(String string)
Convenience method for reading a JSON string into an object. |
abstract
Json |
toJson(JsonWriter jsonWriter)
Writes the object to the passed JsonWriter. |
default void |
toJson(OutputStream outputStream)
Convenience method for writing the JsonSerializable<T> to the passed OutputStream. |
default void |
toJson(Writer writer)
Convenience method for writing the JsonSerializable<T> to the passed Writer. |
default byte[] |
toJsonBytes()
Convenience method for writing the JsonSerializable<T> to a byte array. |
default String |
toJsonString()
Convenience method for writing the JsonSerializable<T> to a JSON string. |
Method Details
fromJson
public static T
Convenience method for reading a JSON byte array into an object.
Parameters:
Returns:
Throws:
bytes
.
fromJson
public static T
Reads a JSON stream into an object.
Implementations of JsonSerializable<T> must define this method, otherwise an UnsupportedOperationException will be thrown.
Parameters:
Returns:
Throws:
jsonReader
.
fromJson
public static T
Convenience method for reading a JSON InputStream into an object.
Parameters:
Returns:
Throws:
inputStream
.
fromJson
public static T
Convenience method for reading a JSON Reader into an object.
Parameters:
Returns:
Throws:
reader
.
fromJson
public static T
Convenience method for reading a JSON string into an object.
Parameters:
Returns:
Throws:
string
.
toJson
public abstract JsonWriter toJson(JsonWriter jsonWriter)
Writes the object to the passed JsonWriter.
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 other JsonSerializable<T> objects for serialization, they'll write the field name only then pass the JsonWriter to the other JsonSerializable<T> object. This way objects writing JSON will be self-encapsulated for writing properly formatted JSON.
Parameters:
Returns:
Throws:
jsonWriter
.
toJson
public default void toJson(OutputStream outputStream)
Convenience method for writing the JsonSerializable<T> to the passed OutputStream.
Parameters:
Throws:
outputStream
.
toJson
public default void toJson(Writer writer)
Convenience method for writing the JsonSerializable<T> to the passed Writer.
Parameters:
Throws:
writer
.
toJsonBytes
public default byte[] toJsonBytes()
Convenience method for writing the JsonSerializable<T> to a byte array.
Returns:
Throws:
toJsonString
public default String toJsonString()
Convenience method for writing the JsonSerializable<T> to a JSON string.
Returns:
Throws: