@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 | 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_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
Configuration property: Trim null bean property values.
|
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_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_beanTypePropertyName, BEAN_debug, BEAN_examples, BEAN_excludeProperties, BEAN_fluentSetters, BEAN_ignoreInvocationExceptionsOnGetters, BEAN_ignoreInvocationExceptionsOnSetters, BEAN_ignorePropertiesWithoutSetters, BEAN_ignoreUnknownBeanProperties, BEAN_ignoreUnknownNullBeanProperties, BEAN_implClasses, BEAN_includeProperties, BEAN_locale, BEAN_mediaType, 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_timeZone, BEAN_useEnumNames, BEAN_useInterfaceProxies, BEAN_useJavaBeanIntrospector, DEFAULT_SORTED
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()
Configuration property: Serializer listener.
|
MediaTypeRange[] |
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()
Configuration property: URI context bean.
|
protected UriRelativity |
getUriRelativity()
Configuration property: URI relativity.
|
protected UriResolution |
getUriResolution()
Configuration property: URI resolution.
|
protected boolean |
isAddBeanTypes()
Configuration property: Add
|
protected boolean |
isAddRootType()
Configuration property: Add type attribute to root nodes.
|
protected boolean |
isSortCollections()
Configuration property: Sort arrays and collections alphabetically.
|
protected boolean |
isSortMaps()
Configuration property: Sort maps alphabetically.
|
protected boolean |
isTrimEmptyCollections()
Configuration property: Trim empty lists and arrays.
|
protected boolean |
isTrimEmptyMaps()
Configuration property: Trim empty maps.
|
protected boolean |
isTrimNullProperties()
Configuration property: Trim null bean property values.
|
protected boolean |
isTrimStrings()
Configuration property: 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.
|
ObjectMap |
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, getBeanClassVisibility, getBeanConstructorVisibility, getBeanDictionaryClasses, getBeanFieldVisibility, getBeanFilters, getBeanMeta, getBeanMethodVisibility, getBeanRegistry, getBeanTypePropertyName, getClassMeta, getClassMeta, getClassMetaForObject, getExamples, getExcludeProperties, getExcludeProperties, getImplClassConstructor, getImplClasses, getIncludeProperties, getIncludeProperties, getLocale, getMediaType, getNotBeanClasses, getNotBeanPackagesNames, getNotBeanPackagesPrefixes, getPojoSwaps, getPropertyNamer, getTimeZone, hasSameCache, isBean, isBeanMapPutReturnsOldValue, isBeansRequireDefaultConstructor, isBeansRequireSerializable, isBeansRequireSettersForGetters, isBeansRequireSomeProperties, isDebug, isFluentSetters, isIgnoreInvocationExceptionsOnGetters, isIgnoreInvocationExceptionsOnSetters, isIgnorePropertiesWithoutSetters, 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, getInstanceArrayProperty, getInstanceArrayProperty, getInstanceArrayProperty, getInstanceProperty, getInstanceProperty, getInstanceProperty, getIntegerProperty, getListProperty, getListProperty, getLongProperty, getMapProperty, getProperty, getProperty, getPropertyKeys, getPropertyStore, getSetProperty, getSetProperty, getStringProperty, getStringPropertyWithNone, hashCode, identityCode, toString
public static final String SERIALIZER_addBeanTypes
If
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
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
Class used to listen for errors and warnings that occur during serialization.
public static final String SERIALIZER_sortCollections
Copies and sorts the contents of arrays and collections before serializing them.
Note that this introduces a performance penalty.
public static final String SERIALIZER_sortMaps
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
If
Note that enabling this setting has the following effects on parsing:
public static final String SERIALIZER_trimEmptyMaps
If
Note that enabling this setting has the following effects on parsing:
public static final String SERIALIZER_trimNullProperties
If
Note that enabling this setting has the following effects on parsing:
public static final String SERIALIZER_trimStrings
If String.trim()
before being serialized.
public static final String SERIALIZER_uriContext
UriContext
)
Bean used for resolution of URIs to absolute or root-relative form.
public static final String 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.
public static final String 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.
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 MediaTypeRange[] 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 isTrimNullProperties()
SERIALIZER_trimNullProperties
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 ObjectMap toMap()
Context
toMap
in class BeanTraverseContext
Copyright © 2016–2019 The Apache Software Foundation. All rights reserved.