public abstract class Marshall extends Object
Serializer
and Parser
into a single class with convenience read/write methods.
The general idea is to combine a single serializer and parser inside a simplified API for reading and writing POJOs.
Modifier | Constructor and Description |
---|---|
protected |
Marshall(Serializer s,
Parser p)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
Marshall |
println(Object o)
Convenience method for calling
System.out.println(...) on the specified object after calling toString(Object) . |
<T> T |
read(Object input,
Class<T> type)
Same as
read(Object, Type, Type...) except optimized for a non-parameterized class. |
<T> T |
read(Object input,
Type type,
Type... args)
Parses input into the specified object type.
|
String |
toString(Object o)
Convenience method for serializing an object to a String.
|
Object |
write(Object o)
Serializes a POJO directly to either a
String or depending on the serializer type. |
void |
write(Object o,
Object output)
Serializes a POJO to the specified output stream or writer.
|
protected Marshall(Serializer s, Parser p)
s
- The serializer to use for serializing output.
p
- The parser to use for parsing input.
public Object write(Object o) throws SerializeException
String
or byte []
depending on the serializer type.o
- The object to serialize.String
byte []
SerializeException
- If a problem occurred trying to convert the output.public final void write(Object o, Object output) throws SerializeException
Equivalent to calling serializer.createSession().serialize(o, output);
o
- The object to serialize.output
- The output object.
Writer
OutputStream
- Output will be written as UTF-8 encoded stream.
File
- Output will be written as system-default encoded stream.
StringBuilder
- Output will be written to the specified string builder.
SerializeException
- If a problem occurred trying to convert the output.public final String toString(Object o)
For writer-based serializers, this is identical to calling Serializer.serialize(Object)
.
For stream-based serializers, this converts the returned byte array to a string based on
the OutputStreamSerializer.OSSERIALIZER_binaryFormat
setting.
o
- The object to serialize.public final Marshall println(Object o)
System.out.println(...)
on the specified object after calling toString(Object)
.o
- The object to serialize and then send to the console.public final <T> T read(Object input, Type type, Type... args) throws ParseException
The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps).
Marshall m = Json.
Collection
classes are assumed to be followed by zero or one objects indicating the element type.
Map
classes are assumed to be followed by zero or two meta objects indicating the key and value types.
The array can be arbitrarily long to indicate arbitrarily complex data structures.
read(Object, Class)
method instead if you don't need a parameterized map/collection.
T
- The class type of the object to create.input
- The input.
Reader
CharSequence
InputStream
containing UTF-8 encoded text (or charset defined by
ReaderParser.RPARSER_inputStreamCharset
property value).
byte []
containing UTF-8 encoded text (or charset defined by
ReaderParser.RPARSER_inputStreamCharset
property value).
File
containing system encoded text (or charset defined by
ReaderParser.RPARSER_fileCharset
property value).
InputStream
byte []
File
CharSequence
containing encoded bytes according to the InputStreamParser.ISPARSER_binaryFormat
setting.
type
- The object type to create.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
args
- The type arguments of the class if it's a collection or map.
ClassMeta
, Class
, ParameterizedType
, GenericArrayType
ParseException
- If the input contains a syntax error or is malformed, or is not valid for the specified type.for argument syntax for maps and collections.
public final <T> T read(Object input, Class<T> type) throws ParseException
read(Object, Type, Type...)
except optimized for a non-parameterized class.
This is the preferred parse method for simple types since you don't need to cast the results.
Marshall m = Json.
T
- The class type of the object being created.input
- The input.
See read(Object, Type, Type...)
for details.type
- The object type to create.ParseException
- If the input contains a syntax error or is malformed, or is not valid for the specified type.Copyright © 2018 Apache. All rights reserved.