public interface Json
stringify(Writer, Object) and
parse(Reader, Type) values to and from JSON character streams. There are also convenient methods for values to JSON
strings conversion, see stringify(Object) and parse(String, Type).
Parsing of not homogeneous arrays is supported but caller should supplies the type of every array item, see
parse(Reader, Type[]).
JSON facade also provides methods for (de)serialization with inband type information, see
stringifyObject(Writer, Object) and parseObject(Reader). This is an extension to JSON protocol that include
class property before the actual object JSON stream.
A JSON stream with inbad type information may look like below. Please note class property from JSON start.
{"class":"js.net.Event","id":1234 ... }
| Modifier and Type | Method and Description |
|---|---|
<T> T |
parse(Reader reader,
Type type)
Deserialize value of expected type.
|
Object[] |
parse(Reader reader,
Type[] types)
Deserialize array of mixed types.
|
<T> T |
parse(String value,
Type type)
Parse JSON encoded value and return instance of requested type.
|
<T> T |
parseObject(Reader reader)
Deserialize JSON encoded object with inband type information.
|
String |
stringify(Object value)
Handy method for value object serialization to JSON formatted string.
|
void |
stringify(Writer writer,
Object value)
Serialize value to JSON character stream and left it unclosed.
|
void |
stringifyObject(Writer writer,
Object value)
Serialize object instance with inband type information.
|
void stringify(Writer writer, Object value) throws IOException
If value argument is null serialize JSON null keyword.
After serialization completes writer is flushed but left unclosed.
writer - character stream to write value on,value - value to serialize, null accepted.IOException - if IO write operation fails.<T> T parse(Reader reader, Type type) throws IOException, JsonException, ClassCastException
reader remains opened.
This method uses auto cast in order to simplify user code but is caller responsibility to ensure requested
type is cast compatible with type of variable to assign to.
This JSON parser method is relaxed and follows a best effort approach. If a named property from JSON stream does not have the same name field into target object, parser just warn on log and ignore. Also, fields from target object with no values into JSON stream are set to null.
T - type to auto cast on return, cast compatible with type argument.reader - character stream to read from,type - expected type.IOException - if read operation fails.JsonException - if parsing process fails perhaps due to syntax violation on input.ClassCastException - if given type cannot cast to expected type variable T.Object[] parse(Reader reader, Type[] types) throws IOException, JsonException
parse(Reader, Type) with type set to desired array class. Every object from JSON
stream array must have the type defined by types parameter. It is caller responsibility to ensure that JSON
stream array types number and order match requested types. Return empty object array if types
is empty.
This method uses the same best effort approach as parse(Reader, Type). Note that after parsing completion used
reader remains opened.
reader - character stream to read from,types - expected types, empty array accepted.IOException - if IO read operation fails.JsonException - if parsing process fails perhaps due to syntax violation on input.void stringifyObject(Writer writer, Object value) throws IOException
parseObject(Reader) companion method. After serialization completes writer is flushed but left
unclosed.
Implementation note: this method uses a proprietary extension to JSON protocol.
writer - characters stream writer,value - object instance to serialize.IOException - if write operation fails.<T> T parseObject(Reader reader) throws IOException, JsonException, ClassCastException
stringifyObject(Writer, Object) companion method. After parsing completion used reader remains
opened.
Implementation note: this method uses a proprietary extension to JSON protocol.
T - object type.reader - characters stream reader.T.IOException - if read operation fails.JsonException - if parsing process fails perhaps due to syntax violation on input.ClassCastException - if inband type cannot be cast to expected parameterized type T.String stringify(Object value)
value - primitive or aggregate value.<T> T parse(String value, Type type) throws JsonException
type and
initialize its fields from JSON string. It uses auto cast in order to simplify user code but is caller responsibility to
ensure requested type is cast compatible with type of variable to assign to.
This method uses the same best effort approach as parse(Reader, Type).
T - type to auto cast on return, cast compatible with type argument.value - JSON encode value,type - desired value type.JsonException - if given string value is not valid JSON format.Copyright © 2018. All rights reserved.