Class JsonSerializer
- All Implemented Interfaces:
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. -
Stringsare converted to JSON strings. -
Numbers(e.g.Integer,Long,Double) are converted to JSON numbers. -
Booleansare converted to JSON booleans. -
nullsare converted to JSON nulls. -
arraysare converted to JSON arrays. -
beansare 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
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class.static classDefault serializer, with whitespace.static classDefault 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
FieldsModifier and TypeFieldDescriptionprotected final booleanstatic final JsonSerializerDefault serializer, all default settings.static final JsonSerializerDefault serializer, all default settings.static final JsonSerializerDefault serializer, single quotes,simple mode, sorted bean properties.protected final booleanprotected final booleanFields inherited from class org.apache.juneau.serializer.WriterSerializer
maxIndent, quoteChar, quoteCharOverride, useWhitespaceFields inherited from class org.apache.juneau.serializer.Serializer
accept, addRootType, keepNullProperties, listener, produces, sortCollections, sortMaps, trimEmptyCollections, trimEmptyMaps, trimStringsFields inherited from class org.apache.juneau.BeanContextable
beanContextFields inherited from class org.apache.juneau.Context
CONTEXT_APPLY_FILTER -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncopy()Creates a builder from this context object.static JsonSerializer.Buildercreate()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 booleanAdd"_type" properties when needed.protected final booleanPrefix solidus'/' characters with escapes.protected final booleanSimple JSON mode.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, toStringMethods 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, serializeToStringMethods inherited from class org.apache.juneau.BeanTraverseContext
getDetectRecursions, getIgnoreRecursions, getInitialDepth, getMaxDepth, isDetectRecursions, isIgnoreRecursionsMethods inherited from class org.apache.juneau.BeanContextable
getBeanContextMethods inherited from class org.apache.juneau.Context
createBuilder, getAnnotationProvider, init, isDebug, toString
-
Field Details
-
DEFAULT
Default serializer, all default settings. -
DEFAULT_SORTED
Default serializer, single quotes,simple mode, sorted bean properties. -
DEFAULT_READABLE
Default serializer, all default settings. -
addBeanTypesJson
-
escapeSolidus
-
simpleAttrs
-
-
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:ContextCreates a builder from this context object.Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.
- Overrides:
copyin classWriterSerializer- Returns:
- A new Builder object.
-
createSession
Description 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 classWriterSerializer- Returns:
- A new session builder.
-
getJsonBeanPropertyMeta
Description copied from interface:JsonMetaProviderReturns the language-specific metadata on the specified bean property.- Specified by:
getJsonBeanPropertyMetain interfaceJsonMetaProvider- Parameters:
bpm- The bean property to return the metadata on.- Returns:
- The metadata.
-
getJsonClassMeta
Description copied from interface:JsonMetaProviderReturns the language-specific metadata on the specified class.- Specified by:
getJsonClassMetain interfaceJsonMetaProvider- Parameters:
cm- The class to return the metadata on.- Returns:
- The metadata.
-
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.
-
getSession
Description 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 classWriterSerializer- Returns:
- A new session object.
-
isAddBeanTypes
Add"_type" properties when needed.- Overrides:
isAddBeanTypesin 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:ContextReturns the properties on this bean as a map for debugging.- Overrides:
propertiesin classWriterSerializer- Returns:
- The properties on this bean as a map for debugging.
-