@ConfigurableContext public abstract class Serializer extends BeanTraverseContext
The purpose of this class is:
SerializerSession
).
Subclasses should extend directly from OutputStreamSerializer
or WriterSerializer
depending on
whether it's a stream or character based serializer.
Modifier and Type | Class and Description |
---|---|
static class |
Serializer.Null
Represents no Serializer.
|
Modifier and Type | Field and Description |
---|---|
static String |
SERIALIZER_addBeanTypes
Configuration property: Add
|
static String |
SERIALIZER_addRootType
Configuration property: Add type attribute to root nodes.
|
static String |
SERIALIZER_keepNullProperties
Configuration property: Don't trim null bean property values.
|
static String |
SERIALIZER_listener
Configuration property: Serializer listener.
|
static String |
SERIALIZER_sortCollections
Configuration property: Sort arrays and collections alphabetically.
|
static String |
SERIALIZER_sortMaps
Configuration property: Sort maps alphabetically.
|
static String |
SERIALIZER_trimEmptyCollections
Configuration property: Trim empty lists and arrays.
|
static String |
SERIALIZER_trimEmptyMaps
Configuration property: Trim empty maps.
|
static String |
SERIALIZER_trimNullProperties
Deprecated.
|
static String |
SERIALIZER_trimStrings
Configuration property: Trim strings.
|
static String |
SERIALIZER_uriContext
Configuration property: URI context bean.
|
static String |
SERIALIZER_uriRelativity
Configuration property: URI relativity.
|
static String |
SERIALIZER_uriResolution
Configuration property: URI resolution.
|
BEANTRAVERSE_detectRecursions, BEANTRAVERSE_ignoreRecursions, BEANTRAVERSE_initialDepth, BEANTRAVERSE_maxDepth
BEAN_annotations, BEAN_beanClassVisibility, BEAN_beanConstructorVisibility, BEAN_beanDictionary, BEAN_beanDictionary_add, BEAN_beanDictionary_remove, BEAN_beanFieldVisibility, BEAN_beanFilters, BEAN_beanFilters_add, BEAN_beanFilters_remove, BEAN_beanMapPutReturnsOldValue, BEAN_beanMethodVisibility, BEAN_beansRequireDefaultConstructor, BEAN_beansRequireSerializable, BEAN_beansRequireSettersForGetters, BEAN_beansRequireSomeProperties, BEAN_bpi, BEAN_bpro, BEAN_bpwo, BEAN_bpx, BEAN_examples, BEAN_excludeProperties, BEAN_fluentSetters, BEAN_ignoreInvocationExceptionsOnGetters, BEAN_ignoreInvocationExceptionsOnSetters, BEAN_ignorePropertiesWithoutSetters, BEAN_ignoreTransientFields, BEAN_ignoreUnknownBeanProperties, BEAN_ignoreUnknownNullBeanProperties, BEAN_implClasses, BEAN_includeProperties, BEAN_notBeanClasses, BEAN_notBeanClasses_add, BEAN_notBeanClasses_remove, BEAN_notBeanPackages, BEAN_notBeanPackages_add, BEAN_notBeanPackages_remove, BEAN_pojoSwaps, BEAN_pojoSwaps_add, BEAN_pojoSwaps_remove, BEAN_propertyNamer, BEAN_sortProperties, BEAN_swaps, BEAN_swaps_add, BEAN_swaps_remove, BEAN_typePropertyName, BEAN_useEnumNames, BEAN_useInterfaceProxies, BEAN_useJavaBeanIntrospector, DEFAULT_SORTED
CONTEXT_debug, CONTEXT_locale, CONTEXT_mediaType, CONTEXT_timeZone
Modifier | Constructor and Description |
---|---|
protected |
Serializer(PropertyStore ps,
String produces,
String accept)
Constructor
|
Modifier and Type | Method and Description |
---|---|
SerializerBuilder |
builder()
Creates a builder from this context object.
|
SerializerSessionArgs |
createDefaultSessionArgs()
Defines default session arguments used when calling the
Context.createSession() method. |
SerializerSession |
createSession()
Create a new bean session based on the properties defined on this context.
|
abstract SerializerSession |
createSession(SerializerSessionArgs args)
Create the session object used for actual serialization of objects.
|
MediaType[] |
getAcceptMediaTypes()
Returns the media types handled based on the value of the
|
protected Class<? extends SerializerListener> |
getListener()
Serializer listener.
|
MediaRanges |
getMediaTypeRanges()
Returns the media types handled based on the value of the
|
MediaType |
getPrimaryMediaType()
Returns the first entry in the
|
MediaType |
getResponseContentType()
Optional method that returns the response
|
protected UriContext |
getUriContext()
URI context bean.
|
protected UriRelativity |
getUriRelativity()
URI relativity.
|
protected UriResolution |
getUriResolution()
URI resolution.
|
protected boolean |
isAddBeanTypes()
Add
|
protected boolean |
isAddRootType()
Add type attribute to root nodes.
|
protected boolean |
isKeepNullProperties()
Don't trim null bean property values.
|
protected boolean |
isSortCollections()
Sort arrays and collections alphabetically.
|
protected boolean |
isSortMaps()
Sort maps alphabetically.
|
protected boolean |
isTrimEmptyCollections()
Trim empty lists and arrays.
|
protected boolean |
isTrimEmptyMaps()
Trim empty maps.
|
protected boolean |
isTrimStrings()
Trim strings.
|
boolean |
isWriterSerializer()
Returns
WriterSerializer . |
Object |
serialize(Object o)
Shortcut method for serializing objects directly to either a
depending on the serializer type. |
void |
serialize(Object o,
Object output)
Serializes a POJO to the specified output stream or writer.
|
String |
serializeToString(Object o)
Convenience method for serializing an object to a String.
|
OMap |
toMap()
Returns the properties defined on this bean context as a simple map for debugging purposes.
|
createSession, getInitialDepth, getMaxDepth, isDetectRecursions, isIgnoreRecursions
_class, create, createBeanSession, createBeanSession, createDefaultBeanSessionArgs, createSession, dumpCacheStats, getAnnotations, getAnnotations, getAnnotations, getAnnotations, getAnnotations, getAnnotations, getAnnotations, getAnnotations, getBeanClassVisibility, getBeanConstructorVisibility, getBeanDictionaryClasses, getBeanFieldVisibility, getBeanFilters, getBeanMeta, getBeanMethodVisibility, getBeanRegistry, getBeanToStringSerializer, getBeanTypePropertyName, getBpi, getBpi, getBpro, getBpro, getBpwo, getBpwo, getBpx, getBpx, getClassMeta, getClassMeta, getClassMetaForObject, getDeclaredAnnotations, getDeclaredAnnotations, getExamples, getImplClassConstructor, getImplClasses, getLastAnnotation, getLastAnnotation, getLastAnnotation, getLastAnnotation, getLastAnnotation, getLastAnnotation, getLastAnnotation, getLastAnnotation, getLastDeclaredAnnotation, getLastDeclaredAnnotation, getNotBeanClasses, getNotBeanPackagesNames, getNotBeanPackagesPrefixes, getPropertyNamer, getSwaps, hasAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, hasDeclaredAnnotation, hasDeclaredAnnotation, hasSameCache, isBean, isBeanMapPutReturnsOldValue, isBeansRequireDefaultConstructor, isBeansRequireSerializable, isBeansRequireSettersForGetters, isBeansRequireSomeProperties, isFluentSetters, isIgnoreInvocationExceptionsOnGetters, isIgnoreInvocationExceptionsOnSetters, isIgnorePropertiesWithoutSetters, isIgnoreTransientFields, isIgnoreUnknownBeanProperties, isIgnoreUnknownNullBeanProperties, isNotABean, isSortProperties, isUseEnumNames, isUseInterfaceProxies, isUseJavaBeanIntrospector, object, resolveClassMeta, string
equals, getArrayProperty, getArrayProperty, getBooleanProperty, getCdlProperty, getClassArrayProperty, getClassArrayProperty, getClassArrayProperty, getClassListProperty, getClassListProperty, getClassMapProperty, getClassMapProperty, getClassProperty, getClassSetProperty, getClassSetProperty, getDefaultLocale, getDefaultMediaType, getDefaultTimeZone, getInstanceArrayProperty, getInstanceArrayProperty, getInstanceArrayProperty, getInstanceProperty, getInstanceProperty, getInstanceProperty, getIntegerProperty, getListProperty, getListProperty, getLongProperty, getMapProperty, getProperty, getProperty, getPropertyKeys, getPropertyStore, getSetProperty, getSetProperty, getStringProperty, getStringPropertyWithNone, hashCode, identityCode, isDebug, toString
public static final String SERIALIZER_addBeanTypes
SERIALIZER_addBeanTypes
When enabled, then
This is used to recreate the correct objects during parsing if the object types cannot be inferred.
For example, when serializing a
Note the differences between the following settings:
SERIALIZER_addRootType
- Affects whether SERIALIZER_addBeanTypes
- Affects whether
public static final String SERIALIZER_addRootType
SERIALIZER_addRootType
When enabled,
When disabled, it is assumed that the parser knows the exact Java POJO type being parsed, and therefore top-level type information that might normally be included to determine the data type will not be serialized.
For example, when serializing a top-level POJO with a @Bean(typeName)
value, a
Note the differences between the following settings:
SERIALIZER_addRootType
- Affects whether SERIALIZER_addBeanTypes
- Affects whether
public static final String SERIALIZER_listener
SERIALIZER_listener
SerializerListener
>Class used to listen for errors and warnings that occur during serialization.
public static final String SERIALIZER_keepNullProperties
SERIALIZER_keepNullProperties
When enabled, null bean values will be serialized to the output.
public static final String SERIALIZER_sortCollections
SERIALIZER_sortCollections
When enabled, copies and sorts the contents of arrays and collections before serializing them.
Note that this introduces a performance penalty since it requires copying the existing collection.
public static final String SERIALIZER_sortMaps
SERIALIZER_sortMaps
When enabled, copies and sorts the contents of maps by their keys before serializing them.
Note that this introduces a performance penalty.
public static final String SERIALIZER_trimEmptyCollections
SERIALIZER_trimEmptyCollections
When enabled, empty lists and arrays will not be serialized.
Note that enabling this setting has the following effects on parsing:
public static final String SERIALIZER_trimEmptyMaps
SERIALIZER_trimEmptyMaps
When enabled, empty map values will not be serialized to the output.
Note that enabling this setting has the following effects on parsing:
@Deprecated public static final String SERIALIZER_trimNullProperties
SERIALIZER_keepNullProperties
SERIALIZER_trimNullProperties
When enabled, null bean values will not be serialized to the output.
Note that enabling this setting has the following effects on parsing:
public static final String SERIALIZER_trimStrings
SERIALIZER_trimStrings
When enabled, string values will be trimmed of whitespace using String.trim()
before being serialized.
public static final String SERIALIZER_uriContext
SERIALIZER_uriContext
UriContext
Bean used for resolution of URIs to absolute or root-relative form.
public static final String SERIALIZER_uriRelativity
SERIALIZER_uriRelativity
UriRelativity
Defines what relative URIs are relative to when serializing any of the following:
Possible values are:
UriRelativity.RESOURCE
- Relative URIs should be considered relative to the servlet URI.
UriRelativity.PATH_INFO
- Relative URIs should be considered relative to the request URI.
See SERIALIZER_uriContext
for examples.
public static final String SERIALIZER_uriResolution
SERIALIZER_uriResolution
UriResolution
Defines the resolution level for URIs when serializing any of the following:
Possible values are:
UriResolution.ABSOLUTE
- Resolve to an absolute URL (e.g. UriResolution.ROOT_RELATIVE
- Resolve to a root-relative URL (e.g. UriResolution.NONE
- Don't do any URL resolution.
See SERIALIZER_uriContext
for examples.
protected Serializer(PropertyStore ps, String produces, String accept)
ps
- The property store containing all the settings for this object.produces
- The media type that this serializer produces.accept
- The accept media types that the serializer can handle.
Can contain meta-characters per the
If empty, then assumes the only media type supported is
For example, if this serializer produces
The accept value can also contain q-values.
public SerializerBuilder builder()
Context
Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.
builder
in class BeanTraverseContext
public boolean isWriterSerializer()
WriterSerializer
.WriterSerializer
.public abstract SerializerSession createSession(SerializerSessionArgs args)
args
- Runtime arguments.
These specify session-level information such as locale and URI context.
It also include session-level properties that override the properties defined on the bean and serializer
contexts.public SerializerSession createSession()
BeanContext
Use this method for creating sessions if you don't need to override any properties or locale/timezone currently set on this context.
createSession
in class BeanTraverseContext
public final SerializerSessionArgs createDefaultSessionArgs()
Context
Context.createSession()
method.createDefaultSessionArgs
in class BeanContext
public final void serialize(Object o, Object output) throws SerializeException, IOException
Equivalent to calling
o
- The object to serialize.output
- The output object.
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.
SerializeException
- If a problem occurred trying to convert the output.IOException
- Thrown by the underlying stream.public Object serialize(Object o) throws SerializeException
byte []
depending on the serializer type.o
- The object to serialize.byte []
SerializeException
- If a problem occurred trying to convert the output.public final String serializeToString(Object o) throws SerializeException
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
the OutputStreamSerializer.OSSERIALIZER_binaryFormat
setting.
o
- The object to serialize.SerializeException
- If a problem occurred trying to convert the output.public final MediaRanges getMediaTypeRanges()
Note that the order of these ranges are from high to low q-value.
public final MediaType getPrimaryMediaType()
This signifies the 'primary' media type for this serializer.
public final MediaType[] getAcceptMediaTypes()
The order of the media types are the same as those in the
public final MediaType getResponseContentType()
This method is specified to override the content type for this serializer.
For example, the SimpleJsonSerializer
class returns that it handles media type
This method is typically meaningless if the serializer is being used stand-alone (i.e. outside of a REST server or client).
protected boolean isAddBeanTypes()
SERIALIZER_addBeanTypes
protected final boolean isAddRootType()
SERIALIZER_addRootType
protected final Class<? extends SerializerListener> getListener()
SERIALIZER_listener
protected final boolean isSortCollections()
SERIALIZER_sortCollections
protected final boolean isSortMaps()
SERIALIZER_sortMaps
protected final boolean isTrimEmptyCollections()
SERIALIZER_trimEmptyCollections
protected final boolean isTrimEmptyMaps()
SERIALIZER_trimEmptyMaps
protected final boolean isKeepNullProperties()
SERIALIZER_keepNullProperties
protected final boolean isTrimStrings()
String.trim()
before being serialized.SERIALIZER_trimStrings
protected final UriContext getUriContext()
SERIALIZER_uriContext
protected final UriRelativity getUriRelativity()
SERIALIZER_uriRelativity
protected final UriResolution getUriResolution()
SERIALIZER_uriResolution
public OMap toMap()
Context
toMap
in class BeanTraverseContext
Copyright © 2016–2020 The Apache Software Foundation. All rights reserved.