Class YamlSerializer
- All Implemented Interfaces:
Function<Object,,String> ThrowingFunction<Object,,String> ArrayRecordWritable,RecordWritable,YamlMetaProvider
- Direct Known Subclasses:
YamlSerializer.Readable
Media types
Handles
Produces
Description
The conversion is as follows...
-
Maps (e.g.
HashMaps,TreeMaps) are converted to YAML mappings. -
Collections (e.g.
HashSets,LinkedLists) and Java arrays are converted to YAML sequences. -
Stringsare converted to YAML scalars. -
Numbers(e.g.Integer,Long,Double) are converted to YAML numbers. -
Booleansare converted to YAML booleans. -
nullsare converted to YAML nulls. -
arraysare converted to YAML sequences. -
beansare converted to YAML mappings.
The types above are considered "YAML-primitive" object types.
Any non-YAML-primitive object types are transformed into YAML-primitive object types through
ObjectSwaps associated through the
MarshallingContext.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.
Example:
Example output (LinkedHashMap of name/age):
name: Alice age: 30
Complex (nested bean + array; bean properties are sorted alphabetically by default):
address: city: Boston state: MA street: "123 Main St" age: 30 name: Alice tags: - a - b - c
Limitations compared to JSON
The YAML serializer has fewer configuration options than JsonSerializer:
- No compact single-line output mode; YAML is always emitted in block-style (indentation-based) format.
- No equivalent to JSON's simple mode or attribute quoting style variants (single vs double quotes).
- No strict vs lax output modes; YAML output follows a consistent, human-readable style.
Notes:
- This class is thread safe and reusable.
See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class.static classDefault serializer, with whitespace.Nested classes/interfaces inherited from class org.apache.juneau.marshall.serializer.WriterSerializer
WriterSerializer.DefaultBuilderNested classes/interfaces inherited from class org.apache.juneau.marshall.serializer.Serializer
Serializer.Null -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final booleanstatic final YamlSerializerDefault serializer, all default settings.static final YamlSerializerDefault serializer, with whitespace.Fields inherited from class org.apache.juneau.marshall.serializer.WriterSerializer
maxIndent, quoteChar, quoteCharOverride, useWhitespaceFields inherited from class org.apache.juneau.marshall.serializer.Serializer
accept, addBeanTypes, addRootType, keepNullProperties, listener, nonDefault, produces, sortCollections, sortMaps, trimEmptyCollections, trimEmptyMaps, trimStringsFields inherited from class org.apache.juneau.marshall.MarshallingContextable
marshallingContextFields inherited from class org.apache.juneau.marshall.Context
CONTEXT_APPLY_FILTER -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncopy()Creates a builder from this context object.static YamlSerializer.Buildercreate()Creates a new builder for this object.Create a session builder based on the properties defined on this context.Returns a session to use for this context.Returns the YAML-specific metadata on the specified bean property.getYamlClassMeta(ClassMeta<?> cm) Returns the YAML-specific metadata on the specified class.protected final booleanAdd"_type" properties when needed.booleanThe YAML array-record writer is buffered/RecordAdapter-backed, not O(1) streaming.booleanThe YAML record writer is buffered/RecordAdapter-backed, not O(1) streaming.Returns the properties on this bean as a map for debugging.writeArrayRecords(Object output) Convenience delegator for the buffered array-elementRecordWriterusing default session arguments.writeRecords(Object output) Convenience delegator for the whole-valueRecordWriterusing default session arguments.Methods inherited from class org.apache.juneau.marshall.serializer.WriterSerializer
applyThrows, getMaxIndent, getQuoteChar, getStreamCharset, isUseWhitespace, isWriterSerializer, println, quoteChar, toString, writeMethods inherited from class org.apache.juneau.marshall.serializer.Serializer
createSerializerBuilder, doWrite, forEachAcceptMediaType, getListener, getMediaTypeRanges, getPrimaryMediaType, getResponseContentType, getResponseHeaders, getUriContext, getUriRelativity, getUriResolution, isAddRootType, isKeepNullProperties, isNonDefault, isSortCollections, isSortMaps, isTrimEmptyCollections, isTrimEmptyMaps, isTrimStrings, write, writeToStringMethods inherited from class org.apache.juneau.marshall.MarshallingTraverseContext
getDetectRecursions, getIgnoreRecursions, getInitialDepth, getMaxDepth, isDetectRecursions, isIgnoreRecursions, shouldDetectRecursionsMethods inherited from class org.apache.juneau.marshall.MarshallingContextable
getDurationFormat, getMarshallingContext, getPeriodFormatMethods inherited from class org.apache.juneau.marshall.Context
createBuilder, getAnnotationProvider, init, isDebug, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.juneau.marshall.stream.ArrayRecordWritable
writeArrayRecordsMethods inherited from interface org.apache.juneau.commons.function.ThrowingFunction
apply
-
Field Details
-
DEFAULT
Default serializer, all default settings. -
DEFAULT_READABLE
Default serializer, with whitespace. -
addBeanTypesYaml
-
-
Constructor Details
-
YamlSerializer
Constructor.- Parameters:
builder- The builder for this object.
-
-
Method Details
-
create
Creates a new builder for this object.- Returns:
- A new builder.
-
getYamlBeanPropertyMeta
Description copied from interface:YamlMetaProviderReturns the YAML-specific metadata on the specified bean property.- Specified by:
getYamlBeanPropertyMetain interfaceYamlMetaProvider- Parameters:
bpm- The bean property to return the metadata on.- Returns:
- The metadata.
-
getYamlClassMeta
Description copied from interface:YamlMetaProviderReturns the YAML-specific metadata on the specified class.- Specified by:
getYamlClassMetain interfaceYamlMetaProvider- Parameters:
cm- The class to return the metadata on.- Returns:
- The metadata.
-
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.
-
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:
-
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.
-
writeRecords
Convenience delegator for the whole-valueRecordWriterusing default session arguments. The real implementation lives onYamlSerializerSession.writeRecords(Object).- Specified by:
writeRecordsin interfaceRecordWritable- Parameters:
output- The output.- Returns:
- A new
RecordWriter. - Throws:
IOException- If a problem occurred opening the underlying output.
-
writeArrayRecords
Convenience delegator for the buffered array-elementRecordWriterusing default session arguments. The real implementation lives onYamlSerializerSession.writeArrayRecords(Object).- Specified by:
writeArrayRecordsin interfaceArrayRecordWritable- Parameters:
output- The output.- Returns:
- A new
RecordWriter. - Throws:
IOException- If a problem occurred opening the underlying output.
-
isRecordStreaming
The YAML record writer is buffered/RecordAdapter-backed, not O(1) streaming.- Specified by:
isRecordStreamingin interfaceRecordWritable- Returns:
- Always
false .
-
isArrayRecordStreaming
The YAML array-record writer is buffered/RecordAdapter-backed, not O(1) streaming.- Specified by:
isArrayRecordStreamingin interfaceArrayRecordWritable- Returns:
- Always
false .
-