@ConfigurableContext public class JsonSerializer extends WriterSerializer
Produces
HashMaps
, TreeMaps
) are converted to JSON objects.
HashSets
, LinkedLists
) and Java arrays are converted to
JSON arrays.
Strings
are converted to JSON strings.
Numbers
(e.g. Integer
, Long
, Double
) are converted to JSON numbers.
Booleans
are converted to JSON booleans.
nulls
are converted to JSON nulls.
arrays
are converted to JSON arrays.
beans
are converted to JSON objects.
The types above are considered "JSON-primitive" object types.
Any non-JSON-primitive object types are transformed into JSON-primitive object types through
PojoSwaps
associated through the
BeanContextBuilder.pojoSwaps(Class...)
method.
Several default transforms are provided for transforming Dates, Enums, Iterators, etc...
This serializer provides several serialization options. Typically, one of the predefined DEFAULT serializers will be sufficient. However, custom serializers can be constructed to fine-tune behavior.
SimpleJsonSerializer
- Default serializer, single quotes, simple mode.
SimpleJsonSerializer.Readable
- Default serializer, single quotes, simple mode, with whitespace.
Modifier and Type | Class and Description |
---|---|
static class |
JsonSerializer.Readable
Default serializer, with whitespace.
|
static class |
JsonSerializer.ReadableSafe
Default serializer, single quotes, simple mode, with whitespace and recursion detection.
|
Modifier and Type | Field and Description |
---|---|
static JsonSerializer |
DEFAULT
Default serializer, all default settings.
|
static JsonSerializer |
DEFAULT_READABLE
Default serializer, all default settings.
|
static String |
JSON_addBeanTypes
Configuration property: Add
|
static String |
JSON_escapeSolidus
Configuration property: Prefix solidus
|
static String |
JSON_simpleMode
Configuration property: Simple JSON mode.
|
WSERIALIZER_fileCharset, WSERIALIZER_maxIndent, WSERIALIZER_quoteChar, WSERIALIZER_streamCharset, WSERIALIZER_useWhitespace
SERIALIZER_addBeanTypes, SERIALIZER_addRootType, SERIALIZER_listener, SERIALIZER_sortCollections, SERIALIZER_sortMaps, SERIALIZER_trimEmptyCollections, SERIALIZER_trimEmptyMaps, SERIALIZER_trimNullProperties, SERIALIZER_trimStrings, SERIALIZER_uriContext, SERIALIZER_uriRelativity, SERIALIZER_uriResolution
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
Constructor and Description |
---|
JsonSerializer(PropertyStore ps)
Constructor.
|
JsonSerializer(PropertyStore ps,
String produces,
String accept)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
JsonSerializerBuilder |
builder()
Creates a builder from this context object.
|
static JsonSerializerBuilder |
create()
Instantiates a new clean-slate
JsonSerializerBuilder object. |
JsonSerializerSession |
createSession()
Create a new bean session based on the properties defined on this context.
|
JsonSerializerSession |
createSession(SerializerSessionArgs args)
Create the session object used for actual serialization of objects.
|
JsonSchemaSerializer |
getSchemaSerializer()
Returns the schema serializer based on the settings of this serializer.
|
protected boolean |
isAddBeanTypes()
Configuration property: Add
|
protected boolean |
isEscapeSolidus()
Configuration property: Prefix solidus
|
protected boolean |
isSimpleMode()
Configuration property: Simple JSON mode.
|
ObjectMap |
toMap()
Returns the properties defined on this bean context as a simple map for debugging purposes.
|
getFileCharset, getMaxIndent, getQuoteChar, getStreamCharset, isUseWhitespace, isWriterSerializer, println, serialize, toString, toStringObject
createDefaultSessionArgs, getAcceptMediaTypes, getListener, getMediaTypeRanges, getPrimaryMediaType, getResponseContentType, getUriContext, getUriRelativity, getUriResolution, isAddRootType, isSortCollections, isSortMaps, isTrimEmptyCollections, isTrimEmptyMaps, isTrimNullProperties, isTrimStrings, serialize, serializeToString
createSession, getInitialDepth, getMaxDepth, isDetectRecursions, isIgnoreRecursions
_class, 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 JSON_addBeanTypes
If
When present, this value overrides the Serializer.SERIALIZER_addBeanTypes
setting and is
provided to customize the behavior of specific serializers in a SerializerGroup
.
public static final String JSON_escapeSolidus
If
However, if you're embedding JSON in an HTML script tag, this setting prevents confusion when trying to serialize
public static final String JSON_simpleMode
If
Otherwise, they are always quoted.
Attributes do not need to be quoted when they conform to the following:
arguments, break, case, catch, class, const, continue, debugger, default, delete, do, else, enum, eval, export, extends, false, finally, for, function, if, implements, import, in, instanceof, interface, let, new, null, package, private, protected, public, return, static, super, switch, this, throw, true, try, typeof, var, void, while, with, undefined, yield
public static final JsonSerializer DEFAULT
public static final JsonSerializer DEFAULT_READABLE
public JsonSerializer(PropertyStore ps)
ps
- The property store containing all the settings for this object.public JsonSerializer(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 JsonSerializerBuilder builder()
Context
Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.
builder
in class Serializer
public static JsonSerializerBuilder create()
JsonSerializerBuilder
object.
This is equivalent to simply calling
.
JsonSerializerBuilder
object.public JsonSchemaSerializer getSchemaSerializer()
Note that this method creates a builder initialized to all default settings, whereas builder()
copies
the settings of the object called on.
public JsonSerializerSession 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 WriterSerializer
public JsonSerializerSession createSession(SerializerSessionArgs args)
Serializer
createSession
in class WriterSerializer
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.protected final boolean isAddBeanTypes()
isAddBeanTypes
in class Serializer
JSON_addBeanTypes
protected final boolean isEscapeSolidus()
JSON_escapeSolidus
protected final boolean isSimpleMode()
JSON_simpleMode
public ObjectMap toMap()
Context
toMap
in class WriterSerializer
Copyright © 2016–2019 The Apache Software Foundation. All rights reserved.