Class Serializer
- 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
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class.static classRepresents no Serializer. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Stringprotected final booleanprotected final booleanprotected final booleanprotected final Class<? extends SerializerListener>protected final Stringprotected final booleanprotected final booleanprotected final booleanprotected final booleanprotected final booleanFields inherited from class org.apache.juneau.BeanContextable
beanContextFields inherited from class org.apache.juneau.Context
CONTEXT_APPLY_FILTER -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncopy()Creates a builder from this context object.static Serializer.Buildercreate()Creates a new builder for this object.static Serializer.BuildercreateSerializerBuilder(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 voiddoSerialize(SerializerSession session, SerializerPipe pipe, Object o) Serializes a POJO to the specified pipe.final SerializerforEachAcceptMediaType(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 MediaRangesReturns the media types handled based on the value of theaccept parameter passed into the constructor.final MediaTypeReturns the first entry in theaccept parameter passed into the constructor.final MediaTypeOptional 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 UriContextURI context bean.protected final UriRelativityURI relativity.protected final UriResolutionURI resolution.protected booleanAdd"_type" properties when needed.protected final booleanAdd type attribute to root nodes.protected final booleanDon't trim null bean property values.protected final booleanSort arrays and collections alphabetically.protected final booleanSort maps alphabetically.protected final booleanTrim empty lists and arrays.protected final booleanTrim empty maps.protected final booleanTrim strings.booleanReturnstrue if this serializer subclasses fromWriterSerializer.Returns the properties on this bean as a map for debugging.Shortcut method for serializing objects directly to either aString ordepending on the serializer type.byte []final voidSerializes a POJO to the specified output stream or writer.final StringConvenience method for serializing an object to a String.Methods inherited from class org.apache.juneau.BeanTraverseContext
getDetectRecursions, getIgnoreRecursions, getInitialDepth, getMaxDepth, isDetectRecursions, isIgnoreRecursionsMethods inherited from class org.apache.juneau.BeanContextable
getBeanContextMethods inherited from class org.apache.juneau.Context
createBuilder, getAnnotationProvider, init, isDebug, toString
-
Field Details
-
addBeanTypes
-
addRootType
-
keepNullProperties
-
sortCollections
-
sortMaps
-
trimEmptyCollections
-
trimEmptyMaps
-
trimStrings
-
listener
-
accept
-
produces
-
-
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:ContextCreates a builder from this context object.Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.
- Specified by:
copyin classBeanTraverseContext- Returns:
- A new Builder object.
-
createSession
Description copied from class:ContextCreate 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:
createSessionin classContext- Returns:
- A new session builder.
-
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.
-
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 .
-
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
Json5Serializerclass 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.
-
getResponseHeaders
Optional method that specifies HTTP request headers for this serializer.For example,
SoapXmlSerializerneeds 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 .
-
getSession
Description copied from class:ContextReturns a session to use for this context.Note that subclasses may opt to return a reusable non-modifiable session.
- Overrides:
getSessionin classContext- Returns:
- A new session object.
-
isWriterSerializer
Returnstrue if this serializer subclasses fromWriterSerializer.- Returns:
true if this serializer subclasses fromWriterSerializer.
-
serialize
Shortcut method for serializing objects directly to either aString ordepending 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.
-
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: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 the underlying stream.
-
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.
-
getListener
Serializer listener.- Returns:
- Class used to listen for errors and warnings that occur during serialization.
- 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:
-
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:
-
isKeepNullProperties
Don't trim null bean property values.- Returns:
true if null bean values are serialized to the output.- 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:
-
isTrimStrings
Trim strings.- Returns:
true if string values will be trimmed of whitespace usingString.trim()before being serialized.- See Also:
-
properties
Description copied from class:ContextReturns the properties on this bean as a map for debugging.- Overrides:
propertiesin classBeanTraverseContext- Returns:
- The properties on this bean as a map for debugging.
-