Class JsonSerializer
- All Implemented Interfaces:
AnnotationProvider
,JsonMetaProvider
- Direct Known Subclasses:
Json5Serializer
,JsonSchemaSerializer
,JsonSerializer.Readable
,JsonSerializer.ReadableSafe
Media types
Handles
Produces
Description
The conversion is as follows...
-
Maps (e.g.
HashMaps
,TreeMaps
) are converted to JSON objects. -
Collections (e.g.
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
ObjectSwaps
associated through the
BeanContext.Builder.swaps(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.
Behavior-specific subclasses
The following direct subclasses are provided for convenience:
-
Json5Serializer
- Default serializer, single quotes, simple mode. -
Json5Serializer.Readable
- Default serializer, single quotes, simple mode, with whitespace.
Example:
Notes:
- This class is thread safe and reusable.
See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Builder class.static class
Default serializer, with whitespace.static class
Default serializer, single quotes, simple mode, with whitespace and recursion detection.Nested classes/interfaces inherited from class org.apache.juneau.serializer.Serializer
Serializer.Null
-
Field Summary
Modifier and TypeFieldDescriptionstatic final JsonSerializer
Default serializer, all default settings.static final JsonSerializer
Default serializer, all default settings.Fields inherited from class org.apache.juneau.Context
CONTEXT_APPLY_FILTER
Fields inherited from interface org.apache.juneau.AnnotationProvider
DISABLE_ANNOTATION_CACHING
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptioncopy()
Creates a builder from this context object.static JsonSerializer.Builder
create()
Creates a new builder for this object.Create a session builder based on the properties defined on this context.Returns the language-specific metadata on the specified bean property.getJsonClassMeta
(ClassMeta<?> cm) Returns the language-specific metadata on the specified class.Returns the schema serializer based on the settings of this serializer.Returns a session to use for this context.protected final boolean
Add"_type" properties when needed.protected final boolean
Prefix solidus'/' characters with escapes.protected final boolean
Simple JSON mode.protected JsonMap
Returns the properties on this bean as a map for debugging.Methods inherited from class org.apache.juneau.serializer.WriterSerializer
getFileCharset, getMaxIndent, getQuoteChar, getStreamCharset, isUseWhitespace, isWriterSerializer, println, quoteChar, serialize, toString
Methods inherited from class org.apache.juneau.serializer.Serializer
createSerializerBuilder, doSerialize, forEachAcceptMediaType, getListener, getMediaTypeRanges, getPrimaryMediaType, getResponseContentType, getResponseHeaders, getUriContext, getUriRelativity, getUriResolution, isAddRootType, isKeepNullProperties, isSortCollections, isSortMaps, isTrimEmptyCollections, isTrimEmptyMaps, isTrimStrings, serialize, serializeToString
Methods inherited from class org.apache.juneau.BeanTraverseContext
getInitialDepth, getMaxDepth, isDetectRecursions, isIgnoreRecursions
Methods inherited from class org.apache.juneau.BeanContextable
getBeanContext
Methods inherited from class org.apache.juneau.Context
createBuilder, firstAnnotation, firstAnnotation, firstAnnotation, firstAnnotation, firstDeclaredAnnotation, forEachAnnotation, forEachAnnotation, forEachAnnotation, forEachAnnotation, forEachDeclaredAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, hasAnnotation, init, isDebug, lastAnnotation, lastAnnotation, lastAnnotation, lastAnnotation, lastDeclaredAnnotation, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.apache.juneau.AnnotationProvider
firstAnnotation, firstAnnotation, firstAnnotation, firstAnnotation, firstDeclaredAnnotation, forEachAnnotation, forEachAnnotation, forEachAnnotation, forEachAnnotation, forEachDeclaredAnnotation, lastAnnotation, lastAnnotation, lastAnnotation, lastAnnotation, lastDeclaredAnnotation
-
Field Details
-
DEFAULT
Default serializer, all default settings. -
DEFAULT_READABLE
Default serializer, all default settings.
-
-
Constructor Details
-
JsonSerializer
Constructor.- Parameters:
builder
- The builder for this object.
-
-
Method Details
-
create
Creates a new builder for this object.- Returns:
- A new builder.
-
copy
Description copied from class:Context
Creates a builder from this context object.Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.
- Overrides:
copy
in classWriterSerializer
- Returns:
- A new Builder object.
-
createSession
Description copied from class:Context
Create 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:
createSession
in classWriterSerializer
- Returns:
- A new session builder.
-
getSession
Description copied from class:Context
Returns a session to use for this context.Note that subclasses may opt to return a reusable non-modifiable session.
- Overrides:
getSession
in classWriterSerializer
- Returns:
- A new session object.
-
getSchemaSerializer
Returns the schema serializer based on the settings of this serializer.Note that this method creates a builder initialized to all default settings, whereas
copy()
copies the settings of the object called on.- Returns:
- The schema serializer.
-
getJsonClassMeta
Description copied from interface:JsonMetaProvider
Returns the language-specific metadata on the specified class.- Specified by:
getJsonClassMeta
in interfaceJsonMetaProvider
- Parameters:
cm
- The class to return the metadata on.- Returns:
- The metadata.
-
getJsonBeanPropertyMeta
Description copied from interface:JsonMetaProvider
Returns the language-specific metadata on the specified bean property.- Specified by:
getJsonBeanPropertyMeta
in interfaceJsonMetaProvider
- Parameters:
bpm
- The bean property to return the metadata on.- Returns:
- The metadata.
-
isAddBeanTypes
Add"_type" properties when needed.- Overrides:
isAddBeanTypes
in classSerializer
- Returns:
true if"_type" properties will be added to beans if their type cannot be inferred through reflection.- See Also:
-
isEscapeSolidus
Prefix solidus'/' characters with escapes.- Returns:
true if solidus (e.g. slash) characters should be escaped.- See Also:
-
isSimpleAttrs
Simple JSON mode.- Returns:
true if JSON attribute names will only be quoted when necessary.
Otherwise, they are always quoted.- See Also:
-
properties
Description copied from class:Context
Returns the properties on this bean as a map for debugging.- Overrides:
properties
in classWriterSerializer
- Returns:
- The properties on this bean as a map for debugging.
-