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 |
err(String msg,
Object... args)
Prints a message with arguments to
System.err . |
String |
format(String msg,
Object... args)
Formats a message with arguments.
|
Parser |
getParser()
Returns the parser associated with this marshall.
|
Serializer |
getSerializer()
Returns the serializer associated with this marshall.
|
Marshall |
out(String msg,
Object... args)
Prints a message with arguments to
System.out . |
Marshall |
println(Object o)
Convenience method for 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.
|
<T> T |
read(String input,
Class<T> type)
Same as
read(Object,Class) but reads from a string and thus doesn't throw an |
<T> T |
read(String input,
Type type,
Type... args)
Same as
read(Object,Type,Type...) but reads from a string and thus doesn't throw an |
String |
toString(Object o)
Convenience method for serializing an object to a String.
|
Object |
write(Object o)
Serializes a POJO directly to either a
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 Serializer getSerializer()
public Parser getParser()
public Object write(Object o) throws SerializeException
byte []
depending on the serializer type.o
- The object to serialize.byte []
SerializeException
- If a problem occurred trying to convert the output.public final void write(Object o, Object output) throws SerializeException, IOException
Equivalent to calling
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.IOException
- Thrown by underlying stream.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)
toString(Object)
.o
- The object to serialize and then send to the console.public final Marshall out(String msg, Object... args)
System.out
.
Arguments are automatically converted to strings using this marshaller.
Useful for debug messages.
SimpleJson.
msg
- The MessageFormat
-styled message.args
- The arguments sent to the the formatter after running them through this marshaller.public final Marshall err(String msg, Object... args)
System.err
.
Arguments are automatically converted to strings using this marshaller.
Useful for debug messages.
SimpleJson.
msg
- The MessageFormat
-styled message.args
- The arguments sent to the the formatter after running them through this marshaller.public final String format(String msg, Object... args)
Arguments are automatically converted to strings using this marshaller.
Useful for debug messages.
String msg = SimpleJson.
msg
- The MessageFormat
-styled message.args
- The arguments sent to the the formatter after running them through this marshaller.public final <T> T read(Object input, Type type, Type... args) throws ParseException, IOException
The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps).
Marshall m = Json.
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_streamCharset
property value).
byte []
containing UTF-8 encoded text (or charset defined by
ReaderParser.RPARSER_streamCharset
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
- Malformed input encountered.IOException
- Thrown by underlying stream.for argument syntax for maps and collections.
public final <T> T read(String input, Type type, Type... args) throws ParseException
read(Object,Type,Type...)
but reads from a string and thus doesn't throw an 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_streamCharset
property value).
byte []
containing UTF-8 encoded text (or charset defined by
ReaderParser.RPARSER_streamCharset
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
- Malformed input encountered.for argument syntax for maps and collections.
public final <T> T read(Object input, Class<T> type) throws ParseException, IOException
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
- Malformed input encountered.IOException
- Thrown by underlying stream.public final <T> T read(String input, Class<T> type) throws ParseException
read(Object,Class)
but reads from a string and thus doesn't throw an 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
- Malformed input encountered.Copyright © 2016–2019 The Apache Software Foundation. All rights reserved.