Class YamlParser
- All Implemented Interfaces:
ArrayRecordReadable,RecordReadable,YamlMetaProvider
Media types
Handles
Description
This parser uses an indentation-aware state machine to parse YAML directly into POJOs without intermediate DOM objects. It supports block-style and flow-style mappings and sequences, plain and quoted scalars, comments, document markers, and standard YAML scalar types.
Limitations compared to JSON
The YAML parser has some limitations when compared to JsonParser:
-
Maps with non-String keys (
Boolean,Date,Temporal,Enum) may not round-trip correctly when the target type is a genericMapwith those key types.LinkedHashMapandTreeMapwithStringkeys work reliably. - No strict vs non-strict mode; unlike JSON, there is no equivalent to JSON's lax parsing of comments, unquoted attributes, or concatenated strings.
- YAML's indentation-based structure requires consistent formatting; malformed indentation can cause parsing errors where equivalent JSON would parse successfully.
Example input (Map of name/age):
name: Alice age: 30
Complex (nested object + array):
name: Alice age: 30 address: street: 123 Main St city: Boston state: MA tags: - a - b - c
Notes:
- This class is thread safe and reusable.
See Also:
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class org.apache.juneau.marshall.parser.ReaderParser
ReaderParser.DefaultBuilderNested classes/interfaces inherited from class org.apache.juneau.marshall.parser.Parser
Parser.Null -
Field Summary
FieldsFields inherited from class org.apache.juneau.marshall.parser.Parser
autoCloseStreams, consumes, debugOutputLines, listener, nulls, trimStrings, unbufferedFields 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 YamlParser.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.booleanThe YAML array-record cursor is buffered/RecordAdapter-backed, not O(1) streaming.booleanThe YAML record cursor is buffered/RecordAdapter-backed, not O(1) streaming.readArrayRecords(Object input) Convenience delegator for the buffered array-elementRecordReaderusing default session arguments.readRecords(Object input) Convenience delegator for the whole-valueRecordReaderusing default session arguments.Methods inherited from class org.apache.juneau.marshall.parser.ReaderParser
getStreamCharset, isReaderParser, propertiesMethods inherited from class org.apache.juneau.marshall.parser.Parser
canHandle, createParserBuilder, doRead, getDebugOutputLines, getListener, getMediaTypes, getNulls, getPrimaryMediaType, isAutoCloseStreams, isTrimStrings, isUnbuffered, read, read, read, read, read, read, readArgs, readIntoCollection, readIntoMap, readWithOuterMethods 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.ArrayRecordReadable
readArrayRecords
-
Field Details
-
DEFAULT
Default parser, all default settings.
-
-
Constructor Details
-
YamlParser
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.
-
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 classReaderParser- 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 classReaderParser- Returns:
- A new session object.
-
readRecords
Convenience delegator for the whole-valueRecordReaderusing default session arguments. The real implementation lives onYamlParserSession.readRecords(Object).- Specified by:
readRecordsin interfaceRecordReadable- Parameters:
input- The input.- Returns:
- A new
RecordReadercursor. - Throws:
IOException- If a problem occurred opening the underlying input.
-
readArrayRecords
Convenience delegator for the buffered array-elementRecordReaderusing default session arguments. The real implementation lives onYamlParserSession.readArrayRecords(Object).- Specified by:
readArrayRecordsin interfaceArrayRecordReadable- Parameters:
input- The input.- Returns:
- A buffered
RecordReader. - Throws:
IOException- If a problem occurred reading the input.
-
isRecordStreaming
The YAML record cursor is buffered/RecordAdapter-backed, not O(1) streaming.- Specified by:
isRecordStreamingin interfaceRecordReadable- Returns:
- Always
false .
-
isArrayRecordStreaming
The YAML array-record cursor is buffered/RecordAdapter-backed, not O(1) streaming.- Specified by:
isArrayRecordStreamingin interfaceArrayRecordReadable- Returns:
- Always
false .
-