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[]).
| 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.
|
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 stringify(Writer writer, Object value) throws IllegalArgumentException, 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.IllegalArgumentException - if writer argument is null.IOException - if IO write operation fails.<T> T parse(Reader reader, Type type) throws IllegalArgumentException, 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.IllegalArgumentException - if reader or type argument is null.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 IllegalArgumentException, 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.IllegalArgumentException - if reader or types argument is null.IOException - if IO read operation fails.JsonException - if parsing process fails perhaps due to syntax violation on input.String stringify(Object value)
value - primitive or aggregate value, null accepted.<T> T parse(String value, Type type) throws IllegalArgumentException, 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, null accepted,type - desired value type.value argument is null.IllegalArgumentException - if type argument is null.JsonException - if given string value is not valid JSON format.Copyright © 2020. All rights reserved.