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
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the parser associated with this marshaller.Returns the serializer associated with this marshaller.final <T> TSame asread(Object, Type, Type...)except optimized for a non-parameterized class.final <T> TParses input into the specified object type.readArrayRecords(Object input) Opens aRecordReaderthat yields each element of a top-level wire array as a separate record.readRecords(Object input) Opens a low-levelRecordReadercursor that yields each top-level value as a record.readTokens(Object input) Opens a low-levelTokenReadercursor over the specified input.final voidSerializes a POJO to the specified output stream or writer.writeArrayRecords(Object output) Opens aRecordWriterthat wraps each written value as one element of a top-level wire array.writeRecords(Object output) Opens a low-levelRecordWritercursor that emits each whole value as a record.writeTokens(Object output) Opens a low-level push generator that emits structural events one at a time.
-
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
-
getParser
Returns the parser associated with this marshaller.- Returns:
- The parser associated with this marshaller.
-
getSerializer
Returns the serializer associated with this marshaller.- Returns:
- The serializer associated with this marshaller.
-
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 ReaderCharSequenceInputStreamcontaining 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).Filecontaining system encoded text.
Stream-based parsers can handle the following input class types:null InputStreambyte []FileCharSequencecontaining 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.
-
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 ReaderCharSequenceInputStreamcontaining 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).Filecontaining system encoded text.
Stream-based parsers can handle the following input class types:null InputStreambyte []FileCharSequencecontaining encoded bytes according to theInputStreamParser.Builder#binaryFormat(BinaryFormat)setting.
type- The object type to create.
Can be any of the following:ClassMeta,Class,ParameterizedType,GenericArrayTypeargs- 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
-
write
Serializes a POJO to the specified output stream or writer.Equivalent to calling
serializer.createSession().write(o, output); - Parameters:
object- The object to serialize.output- The output object.
Character-based serializers can handle the following output class types:WriterOutputStream- 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.
-
readTokens
Opens a low-levelTokenReadercursor over the specified input.Shortcut for
((TokenReadable)getParser()).readTokens( . Seeinput )TokenReaderfor the cursor contract.Notes:
- This capability depends on the underlying parser. Formats whose parser
does not implement
TokenReadable(the structural-token role) yield aClassCastException.
- Parameters:
input- The input. AcceptsReader,CharSequence,InputStream,, orbyte []File.- Returns:
- A new
TokenReadercursor. - Throws:
IOException- If a problem occurred opening the underlying input.
- This capability depends on the underlying parser. Formats whose parser
does not implement
-
writeTokens
Opens a low-level push generator that emits structural events one at a time.Shortcut for
((TokenWritable)getSerializer()).writeTokens( . Seeoutput )TokenWriterfor the generator contract.Notes:
- This capability depends on the underlying serializer. Formats whose
serializer does not implement
TokenWritable(the structural-token role) yield aClassCastException.
- Parameters:
output- The output. AcceptsWriter,OutputStream,File, orStringBuilder.- Returns:
- A new
TokenWriter. - Throws:
IOException- If the output type is not supported or could not be opened.
- This capability depends on the underlying serializer. Formats whose
serializer does not implement
-
readRecords
Opens a low-levelRecordReadercursor that yields each top-level value as a record.Shortcut for
((RecordReadable)getParser()).readRecords( . Seeinput )RecordReaderfor the cursor contract.Notes:
- This capability depends on the underlying parser. Formats whose parser
does not implement
RecordReadableyield aClassCastException.
- Parameters:
input- The input. AcceptsReader,CharSequence,InputStream,, orbyte []File.- Returns:
- A new
RecordReadercursor. - Throws:
IOException- If a problem occurred opening the underlying input.
- This capability depends on the underlying parser. Formats whose parser
does not implement
-
writeRecords
Opens a low-levelRecordWritercursor that emits each whole value as a record.Shortcut for
((RecordWritable)getSerializer()).writeRecords( . Seeoutput )RecordWriterfor the cursor contract.Notes:
- This capability depends on the underlying serializer. Formats whose
serializer does not implement
RecordWritableyield aClassCastException.
- Parameters:
output- The output. AcceptsWriter,OutputStream,File, orStringBuilder.- Returns:
- A new
RecordWritercursor. - Throws:
IOException- If a problem occurred opening the underlying output.
- This capability depends on the underlying serializer. Formats whose
serializer does not implement
-
readArrayRecords
Opens aRecordReaderthat yields each element of a top-level wire array as a separate record.Shortcut for
((ArrayRecordReadable)getParser()).readArrayRecords( .input )Notes:
- This capability depends on the underlying parser. Formats whose parser
does not implement
ArrayRecordReadableyield aClassCastException.
- Parameters:
input- The input. AcceptsReader,CharSequence,InputStream,, orbyte []File.- Returns:
- A new element-streamed
RecordReadercursor. - Throws:
IOException- If a problem occurred opening the underlying input.
- This capability depends on the underlying parser. Formats whose parser
does not implement
-
writeArrayRecords
Opens aRecordWriterthat wraps each written value as one element of a top-level wire array.Shortcut for
((ArrayRecordWritable)getSerializer()).writeArrayRecords( .output )Notes:
- This capability depends on the underlying serializer. Formats whose
serializer does not implement
ArrayRecordWritableyield aClassCastException.
- Parameters:
output- The output. AcceptsWriter,OutputStream,File, orStringBuilder.- Returns:
- A new element-streamed
RecordWritercursor. - Throws:
IOException- If a problem occurred opening the underlying output.
- This capability depends on the underlying serializer. Formats whose
serializer does not implement
-