Class Serializer
- All Implemented Interfaces:
AnnotationProvider
- Direct Known Subclasses:
OutputStreamSerializer
,Serializer.Null
,SerializerSet.Inherit
,SerializerSet.NoInherit
,WriterSerializer
Description
Base serializer class that serves as the parent class for all serializers.
The purpose of this class is:
- Maintain a read-only configuration state of a serializer.
- Create session objects used for serializing POJOs (i.e.
SerializerSession
). - Provide convenience methods for serializing POJOs without having to construct session objects.
Subclasses should (but are not required to) extend directly from OutputStreamSerializer
or WriterSerializer
depending on
whether it's a stream or character based serializer.
Subclasses must implement parsing via one of the following methods:
doSerialize(SerializerSession, SerializerPipe, Object)
SerializerSession.doSerialize(SerializerPipe, Object)
Notes:
- This class is thread safe and reusable.
See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Builder class.static class
Represents no Serializer. -
Field Summary
Fields inherited from class org.apache.juneau.Context
CONTEXT_APPLY_FILTER
Fields inherited from interface org.apache.juneau.AnnotationProvider
DEFAULT, DISABLE_ANNOTATION_CACHING
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptioncopy()
Creates a builder from this context object.static Serializer.Builder
create()
Creates a new builder for this object.static Serializer.Builder
createSerializerBuilder
(Class<? extends Serializer> c) Instantiates a builder of the specified serializer class.Create a session builder based on the properties defined on this context.protected void
doSerialize
(SerializerSession session, SerializerPipe pipe, Object o) Serializes a POJO to the specified pipe.final Serializer
forEachAcceptMediaType
(Consumer<MediaType> action) Performs an action on the media types handled based on the value of theaccept parameter passed into the constructor.protected final Class<? extends SerializerListener>
Serializer listener.final MediaRanges
Returns the media types handled based on the value of theaccept parameter passed into the constructor.final MediaType
Returns the first entry in theaccept parameter passed into the constructor.final MediaType
Optional method that returns the responseContent-Type for this serializer if it is different from the matched media type.getResponseHeaders
(SerializerSession session) Optional method that specifies HTTP request headers for this serializer.Returns a session to use for this context.protected final UriContext
URI context bean.protected final UriRelativity
URI relativity.protected final UriResolution
URI resolution.protected boolean
Add"_type" properties when needed.protected final boolean
Add type attribute to root nodes.protected final boolean
Don't trim null bean property values.protected final boolean
Sort arrays and collections alphabetically.protected final boolean
Sort maps alphabetically.protected final boolean
Trim empty lists and arrays.protected final boolean
Trim empty maps.protected final boolean
Trim strings.boolean
Returnstrue if this serializer subclasses fromWriterSerializer
.protected JsonMap
Returns the properties on this bean as a map for debugging.Shortcut method for serializing objects directly to either aString or
depending on the serializer type.byte []final void
Serializes a POJO to the specified output stream or writer.final String
Convenience method for serializing an object to a String.Methods inherited from class org.apache.juneau.BeanTraverseContext
getInitialDepth, getMaxDepth, isDetectRecursions, isIgnoreRecursions
Methods inherited from class org.apache.juneau.BeanContextable
getBeanContext
Methods inherited from class org.apache.juneau.Context
createBuilder, firstAnnotation, firstAnnotation, firstAnnotation, firstAnnotation, firstDeclaredAnnotation, forEachAnnotation, forEachAnnotation, forEachAnnotation, forEachAnnotation, forEachDeclaredAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, init, isDebug, lastAnnotation, lastAnnotation, lastAnnotation, lastAnnotation, lastDeclaredAnnotation, toString
-
Constructor Details
-
Serializer
Constructor- Parameters:
builder
- The builder this object.
-
-
Method Details
-
create
Creates a new builder for this object.- Returns:
- A new builder.
-
createSerializerBuilder
Instantiates a builder of the specified serializer class.Looks for a public static method called
create that returns an object that can be passed into a public or protected constructor of the class.- Parameters:
c
- The builder to create.- Returns:
- A new builder.
-
copy
Description copied from class:Context
Creates a builder from this context object.Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.
- Specified by:
copy
in classBeanTraverseContext
- Returns:
- A new Builder object.
-
createSession
Description copied from class:Context
Create a session builder based on the properties defined on this context.Use this method for creating sessions where you want to override basic settings. Otherwise, use
Context.getSession()
directly.- Overrides:
createSession
in classContext
- Returns:
- A new session builder.
-
getSession
Description copied from class:Context
Returns a session to use for this context.Note that subclasses may opt to return a reusable non-modifiable session.
- Overrides:
getSession
in classContext
- Returns:
- A new session object.
-
isWriterSerializer
Returnstrue if this serializer subclasses fromWriterSerializer
.- Returns:
true if this serializer subclasses fromWriterSerializer
.
-
serialize
Serializes a POJO to the specified output stream or writer.Equivalent to calling
serializer.createSession().serialize(o, output); - Parameters:
o
- 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 the underlying stream.
-
serialize
Shortcut method for serializing objects directly to either aString or
depending on the serializer type.byte []- Parameters:
o
- The object to serialize.- Returns:
- The serialized object.
Character-based serializers will return aString
Stream-based serializers will return abyte [] - Throws:
SerializeException
- If a problem occurred trying to convert the output.
-
serializeToString
Convenience method for serializing an object to a String.For writer-based serializers, this is identical to calling
serialize(Object)
.
For stream-based serializers, this converts the returned byte array to a string based on theOutputStreamSerializer.Builder.binaryFormat(BinaryFormat)
setting.- Parameters:
o
- The object to serialize.- Returns:
- The output serialized to a string.
- Throws:
SerializeException
- If a problem occurred trying to convert the output.
-
doSerialize
protected void doSerialize(SerializerSession session, SerializerPipe pipe, Object o) throws IOException, SerializeException Serializes a POJO to the specified pipe.- Parameters:
session
- The current session.pipe
- Where to send the output from the serializer.o
- The object to serialize.- Throws:
IOException
- Thrown by underlying stream.SerializeException
- Problem occurred trying to serialize object.
-
getResponseHeaders
Optional method that specifies HTTP request headers for this serializer.For example,
SoapXmlSerializer
needs to set aSOAPAction header.This method is typically meaningless if the serializer is being used stand-alone (i.e. outside of a REST server or client).
- Parameters:
session
- The current session.- Returns:
- The HTTP headers to set on HTTP requests.
Never
null .
-
getMediaTypeRanges
Returns the media types handled based on the value of theaccept parameter passed into the constructor.Note that the order of these ranges are from high to low q-value.
- Returns:
- The list of media types. Never
null .
-
getPrimaryMediaType
Returns the first entry in theaccept parameter passed into the constructor.This signifies the 'primary' media type for this serializer.
- Returns:
- The media type. Never
null .
-
forEachAcceptMediaType
Performs an action on the media types handled based on the value of theaccept parameter passed into the constructor.The order of the media types are the same as those in the
accept parameter.- Parameters:
action
- The action to perform on the media types.- Returns:
- This object.
-
getResponseContentType
Optional method that returns the responseContent-Type for this serializer if it is different from the matched media type.This method is specified to override the content type for this serializer. For example, the
Json5Serializer
class returns that it handles media type"text/json5" , but returns"text/json" as the actual content type. This allows clients to request specific 'flavors' of content using specializedAccept header values.This method is typically meaningless if the serializer is being used stand-alone (i.e. outside of a REST server or client).
- Returns:
- The response content type. If
null , then the matched media type is used.
-
isAddBeanTypes
Add"_type" properties when needed.- Returns:
true if"_type" properties added to beans if their type cannot be inferred through reflection.- See Also:
-
isAddRootType
Add type attribute to root nodes.- Returns:
true if type property should be added to root node.- See Also:
-
getListener
Serializer listener.- Returns:
- Class used to listen for errors and warnings that occur during serialization.
- See Also:
-
isSortCollections
Sort arrays and collections alphabetically.- Returns:
true if arrays and collections are copied and sorted before serialization.- See Also:
-
isSortMaps
Sort maps alphabetically.- Returns:
true if maps are copied and sorted before serialization.- See Also:
-
isTrimEmptyCollections
Trim empty lists and arrays.- Returns:
true if empty lists and arrays are not serialized to the output.- See Also:
-
isTrimEmptyMaps
Trim empty maps.- Returns:
true if empty map values are not serialized to the output.- See Also:
-
isKeepNullProperties
Don't trim null bean property values.- Returns:
true if null bean values are serialized to the output.- See Also:
-
isTrimStrings
Trim strings.- Returns:
true if string values will be trimmed of whitespace usingString.trim()
before being serialized.- See Also:
-
getUriContext
URI context bean.- Returns:
- Bean used for resolution of URIs to absolute or root-relative form.
- See Also:
-
getUriRelativity
URI relativity.- Returns:
- Defines what relative URIs are relative to when serializing any of the following:
- See Also:
-
getUriResolution
URI resolution.- Returns:
- Defines the resolution level for URIs when serializing URIs.
- See Also:
-
properties
Description copied from class:Context
Returns the properties on this bean as a map for debugging.- Overrides:
properties
in classBeanTraverseContext
- Returns:
- The properties on this bean as a map for debugging.
-