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 SummaryNested ClassesModifier and TypeClassDescriptionstatic classBuilder class.static classRepresents no Serializer.
- 
Field SummaryFields inherited from class org.apache.juneau.ContextCONTEXT_APPLY_FILTERFields inherited from interface org.apache.juneau.AnnotationProviderDEFAULT, DISABLE_ANNOTATION_CACHING
- 
Constructor SummaryConstructors
- 
Method SummaryModifier 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.protected JsonMapReturns the properties on this bean as a map for debugging.Shortcut method for serializing objects directly to either aString orbyte []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.BeanTraverseContextgetInitialDepth, getMaxDepth, isDetectRecursions, isIgnoreRecursionsMethods inherited from class org.apache.juneau.BeanContextablegetBeanContextMethods inherited from class org.apache.juneau.ContextcreateBuilder, firstAnnotation, firstAnnotation, firstAnnotation, firstAnnotation, firstDeclaredAnnotation, forEachAnnotation, forEachAnnotation, forEachAnnotation, forEachAnnotation, forEachDeclaredAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, init, isDebug, lastAnnotation, lastAnnotation, lastAnnotation, lastAnnotation, lastDeclaredAnnotation, toString
- 
Constructor Details- 
SerializerConstructor- Parameters:
- builder- The builder this object.
 
 
- 
- 
Method Details- 
createCreates a new builder for this object.- Returns:
- A new builder.
 
- 
createSerializerBuilderInstantiates 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.
 
- 
copyDescription 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 class- BeanTraverseContext
- Returns:
- A new Builder object.
 
- 
createSessionDescription 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 class- Context
- Returns:
- A new session builder.
 
- 
getSessionDescription 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 class- Context
- Returns:
- A new session object.
 
- 
isWriterSerializerReturnstrue if this serializer subclasses fromWriterSerializer.- Returns:
- true if this serializer subclasses from- WriterSerializer.
 
- 
serializeSerializes 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.
 
- 
serializeShortcut method for serializing objects directly to either aString orbyte []- 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.
 
- 
serializeToStringConvenience 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.
 
- 
doSerializeprotected 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.
 
- 
getResponseHeadersOptional 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 .
 
- 
getMediaTypeRangesReturns 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 .
 
- 
getPrimaryMediaTypeReturns 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 .
 
- 
forEachAcceptMediaTypePerforms 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.
 
- 
getResponseContentTypeOptional 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.
 
- 
isAddBeanTypesAdd"_type" properties when needed.- Returns:
- true if- "_type" properties added to beans if their type cannot be inferred through reflection.
- See Also:
 
- 
isAddRootTypeAdd type attribute to root nodes.- Returns:
- true if type property should be added to root node.
- See Also:
 
- 
getListenerSerializer listener.- Returns:
- Class used to listen for errors and warnings that occur during serialization.
- See Also:
 
- 
isSortCollectionsSort arrays and collections alphabetically.- Returns:
- true if arrays and collections are copied and sorted before serialization.
- See Also:
 
- 
isSortMapsSort maps alphabetically.- Returns:
- true if maps are copied and sorted before serialization.
- See Also:
 
- 
isTrimEmptyCollectionsTrim empty lists and arrays.- Returns:
- true if empty lists and arrays are not serialized to the output.
- See Also:
 
- 
isTrimEmptyMapsTrim empty maps.- Returns:
- true if empty map values are not serialized to the output.
- See Also:
 
- 
isKeepNullPropertiesDon't trim null bean property values.- Returns:
- true if null bean values are serialized to the output.
- See Also:
 
- 
isTrimStringsTrim strings.- Returns:
- true if string values will be trimmed of whitespace using- String.trim()before being serialized.
- See Also:
 
- 
getUriContextURI context bean.- Returns:
- Bean used for resolution of URIs to absolute or root-relative form.
- See Also:
 
- 
getUriRelativityURI relativity.- Returns:
- Defines what relative URIs are relative to when serializing any of the following:
- See Also:
 
- 
getUriResolutionURI resolution.- Returns:
- Defines the resolution level for URIs when serializing URIs.
- See Also:
 
- 
propertiesDescription copied from class:ContextReturns the properties on this bean as a map for debugging.- Overrides:
- propertiesin class- BeanTraverseContext
- Returns:
- The properties on this bean as a map for debugging.
 
 
-