Class Marshaller
- Direct Known Subclasses:
CharMarshaller
,StreamMarshaller
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.
Examples:
See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionReturns the parser associated with this marshaller.Returns the serializer associated with this marshaller.final <T> T
Same asread(Object, Type, Type...)
except optimized for a non-parameterized class.final <T> T
Parses input into the specified object type.final void
Serializes a POJO to the specified output stream or writer.
-
Constructor Details
-
Marshaller
Constructor.- Parameters:
s
- The serializer to use for serializing output.
Must not benull .p
- The parser to use for parsing input.
Must not benull .
-
-
Method Details
-
getSerializer
Returns the serializer associated with this marshaller.- Returns:
- The serializer associated with this marshaller.
-
getParser
Returns the parser associated with this marshaller.- Returns:
- The parser associated with this marshaller.
-
write
Serializes a POJO to the specified output stream or writer.Equivalent to calling
serializer.createSession().serialize(o, output); - Parameters:
object
- The object to serialize.output
- The output object.
Character-based serializers can handle the following output class types: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.
Stream-based serializers can handle the following output class types:- Throws:
SerializeException
- If a problem occurred trying to convert the output.IOException
- Thrown by underlying stream.
-
read
Parses input into the specified object type.The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps).
Examples:
Marshaller
marshaller = Json.DEFAULT ;// Parse into a linked-list of strings. Listlist1 =marshaller .read(json , LinkedList.class , String.class );// Parse into a linked-list of beans. Listlist2 =marshaller .read(json , LinkedList.class , MyBean.class );// Parse into a linked-list of linked-lists of strings. Listlist3 =marshaller .read(json , LinkedList.class , LinkedList.class , String.class );// Parse into a map of string keys/values. Mapmap1 =marshaller .read(json , TreeMap.class , String.class , String.class );// Parse into a map containing string keys and values of lists containing beans. Mapmap2 =marshaller .read(json , TreeMap.class , String.class , List.class , MyBean.class );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.
Notes:
-
Use the
read(Object, Class)
method instead if you don't need a parameterized map/collection.
- Type Parameters:
T
- The class type of the object to create.- Parameters:
input
- The input.
Character-based parsers can handle the following input class types:null Reader
CharSequence
InputStream
containing UTF-8 encoded text (or charset defined byReaderParser.Builder.streamCharset(Charset)
property value).
containing UTF-8 encoded text (or charset defined bybyte []ReaderParser.Builder.streamCharset(Charset)
property value).File
containing system encoded text (or charset defined byReaderParser.Builder.fileCharset(Charset)
property value).
Stream-based parsers can handle the following input class types:null InputStream
byte []File
CharSequence
containing encoded bytes according to theInputStreamParser.Builder.binaryFormat(BinaryFormat)
setting.
type
- The object type to create.
Can be any of the following:ClassMeta
,Class
,ParameterizedType
,GenericArrayType
args
- The type arguments of the class if it's a collection or map.
Can be any of the following:ClassMeta
,Class
,ParameterizedType
,GenericArrayType
Ignored if the main type is not a map or collection.- Returns:
- The parsed object.
- Throws:
ParseException
- Malformed input encountered.IOException
- Thrown by underlying stream.- See Also:
-
Use the
-
read
Same asread(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.
Examples:
Marshaller
marshaller = Json.DEFAULT ;// Parse into a string. Stringstring =marshaller .read(json , String.class );// Parse into a bean. MyBeanbean =marshaller .read(json , MyBean.class );// Parse into a bean array. MyBean[]beanArray =marshaller .read(json , MyBean[].class );// Parse into a linked-list of objects. Listlist =marshaller .read(json , LinkedList.class );// Parse into a map of object keys/values. Mapmap =marshaller .read(json , TreeMap.class );- Type Parameters:
T
- The class type of the object being created.- Parameters:
input
- The input.
Character-based parsers can handle the following input class types:null Reader
CharSequence
InputStream
containing UTF-8 encoded text (or charset defined byReaderParser.Builder.streamCharset(Charset)
property value).
containing UTF-8 encoded text (or charset defined bybyte []ReaderParser.Builder.streamCharset(Charset)
property value).File
containing system encoded text (or charset defined byReaderParser.Builder.fileCharset(Charset)
property value).
Stream-based parsers can handle the following input class types:null InputStream
byte []File
CharSequence
containing encoded bytes according to theInputStreamParser.Builder.binaryFormat(BinaryFormat)
setting.
type
- The object type to create.- Returns:
- The parsed object.
- Throws:
ParseException
- Malformed input encountered.IOException
- Thrown by underlying stream.
-