Class Yaml
- java.lang.Object
-
- org.yaml.snakeyaml.Yaml
-
public class Yaml extends Object
Public YAML interface. This class is not thread-safe. Which means that all the methods of the same instance can be called only by one thread. It is better to create an instance for every YAML stream.
-
-
Field Summary
Fields Modifier and Type Field Description protected BaseConstructor
constructor
protected DumperOptions
dumperOptions
protected LoaderOptions
loadingConfig
protected Representer
representer
protected Resolver
resolver
-
Constructor Summary
Constructors Constructor Description Yaml()
Create Yaml instance.Yaml(BaseConstructor constructor)
Create Yaml instance.Yaml(BaseConstructor constructor, Representer representer)
Create Yaml instance.Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions)
Create Yaml instance.Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions, LoaderOptions loadingConfig)
Create Yaml instance.Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions, LoaderOptions loadingConfig, Resolver resolver)
Create Yaml instance.Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions, Resolver resolver)
Create Yaml instance.Yaml(DumperOptions dumperOptions)
Create Yaml instance.Yaml(LoaderOptions loadingConfig)
Create Yaml instance.Yaml(Representer representer)
Create Yaml instance.Yaml(Representer representer, DumperOptions dumperOptions)
Create Yaml instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addImplicitResolver(Tag tag, Pattern regexp, String first)
Add an implicit scalar detector.void
addImplicitResolver(Tag tag, Pattern regexp, String first, int limit)
Add an implicit scalar detector.void
addTypeDescription(TypeDescription td)
Node
compose(Reader yaml)
Parse the first YAML document in a stream and produce the corresponding representation tree.Iterable<Node>
composeAll(Reader yaml)
Parse all YAML documents in a stream and produce corresponding representation trees.String
dump(Object data)
Serialize a Java object into a YAML String.void
dump(Object data, Writer output)
Serialize a Java object into a YAML stream.String
dumpAll(Iterator<? extends Object> data)
Serialize a sequence of Java objects into a YAML String.void
dumpAll(Iterator<? extends Object> data, Writer output)
Serialize a sequence of Java objects into a YAML stream.String
dumpAs(Object data, Tag rootTag, DumperOptions.FlowStyle flowStyle)
Serialize a Java object into a YAML string.String
dumpAsMap(Object data)
Serialize a Java object into a YAML string.String
getName()
Get a meaningful name.<T> T
load(InputStream io)
Parse the only YAML document in a stream and produce the corresponding Java object.<T> T
load(Reader io)
Parse the only YAML document in a stream and produce the corresponding Java object.<T> T
load(String yaml)
Parse the only YAML document in a String and produce the corresponding Java object.Iterable<Object>
loadAll(InputStream yaml)
Parse all YAML documents in a stream and produce corresponding Java objects.Iterable<Object>
loadAll(Reader yaml)
Parse all YAML documents in the Reader and produce corresponding Java objects.Iterable<Object>
loadAll(String yaml)
Parse all YAML documents in a String and produce corresponding Java objects.<T> T
loadAs(InputStream input, Class<? super T> type)
Parse the only YAML document in a stream and produce the corresponding Java object.<T> T
loadAs(Reader io, Class<? super T> type)
Parse the only YAML document in a stream and produce the corresponding Java object.<T> T
loadAs(String yaml, Class<? super T> type)
Parse the only YAML document in a String and produce the corresponding Java object.Iterable<Event>
parse(Reader yaml)
Parse a YAML stream and produce parsing events.Node
represent(Object data)
Produce the corresponding representation tree for a given Object.List<Event>
serialize(Node data)
Serialize the representation tree into Events.void
serialize(Node node, Writer output)
Serialize (dump) a YAML node into a YAML stream.void
setBeanAccess(BeanAccess beanAccess)
void
setName(String name)
Set a meaningful name to be shown in toString()String
toString()
-
-
-
Field Detail
-
resolver
protected final Resolver resolver
-
constructor
protected BaseConstructor constructor
-
representer
protected Representer representer
-
dumperOptions
protected DumperOptions dumperOptions
-
loadingConfig
protected LoaderOptions loadingConfig
-
-
Constructor Detail
-
Yaml
public Yaml()
Create Yaml instance.
-
Yaml
public Yaml(DumperOptions dumperOptions)
Create Yaml instance.- Parameters:
dumperOptions
- DumperOptions to configure outgoing objects
-
Yaml
public Yaml(LoaderOptions loadingConfig)
Create Yaml instance.- Parameters:
loadingConfig
- LoadingConfig to control load behavior
-
Yaml
public Yaml(Representer representer)
Create Yaml instance.- Parameters:
representer
- Representer to emit outgoing objects
-
Yaml
public Yaml(BaseConstructor constructor)
Create Yaml instance.- Parameters:
constructor
- BaseConstructor to construct incoming documents
-
Yaml
public Yaml(BaseConstructor constructor, Representer representer)
Create Yaml instance.- Parameters:
constructor
- BaseConstructor to construct incoming documentsrepresenter
- Representer to emit outgoing objects
-
Yaml
public Yaml(Representer representer, DumperOptions dumperOptions)
Create Yaml instance. It is safe to create a few instances and use them in different Threads.- Parameters:
representer
- Representer to emit outgoing objectsdumperOptions
- DumperOptions to configure outgoing objects
-
Yaml
public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions)
Create Yaml instance. It is safe to create a few instances and use them in different Threads.- Parameters:
constructor
- BaseConstructor to construct incoming documents. Its LoaderOptions will be used everywhererepresenter
- Representer to emit outgoing objectsdumperOptions
- DumperOptions to configure outgoing objects
-
Yaml
public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions, LoaderOptions loadingConfig)
Create Yaml instance. It is safe to create a few instances and use them in different Threads.- Parameters:
constructor
- BaseConstructor to construct incoming documentsrepresenter
- Representer to emit outgoing objectsdumperOptions
- DumperOptions to configure outgoing objectsloadingConfig
- LoadingConfig to control load behavior
-
Yaml
public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions, Resolver resolver)
Create Yaml instance. It is safe to create a few instances and use them in different Threads.- Parameters:
constructor
- BaseConstructor to construct incoming documentsrepresenter
- Representer to emit outgoing objectsdumperOptions
- DumperOptions to configure outgoing objectsresolver
- Resolver to detect implicit type
-
Yaml
public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions, LoaderOptions loadingConfig, Resolver resolver)
Create Yaml instance. It is safe to create a few instances and use them in different Threads.- Parameters:
constructor
- BaseConstructor to construct incoming documentsrepresenter
- Representer to emit outgoing objectsdumperOptions
- DumperOptions to configure outgoing objectsloadingConfig
- LoadingConfig to control load behaviorresolver
- Resolver to detect implicit type
-
-
Method Detail
-
dump
public String dump(Object data)
Serialize a Java object into a YAML String.- Parameters:
data
- Java object to be Serialized to YAML- Returns:
- YAML String
-
represent
public Node represent(Object data)
Produce the corresponding representation tree for a given Object.- Parameters:
data
- instance to build the representation tree for- Returns:
- representation tree
- See Also:
- Figure 3.1. Processing Overview
-
dumpAll
public String dumpAll(Iterator<? extends Object> data)
Serialize a sequence of Java objects into a YAML String.- Parameters:
data
- Iterator with Objects- Returns:
- YAML String with all the objects in proper sequence
-
dump
public void dump(Object data, Writer output)
Serialize a Java object into a YAML stream.- Parameters:
data
- Java object to be serialized to YAMLoutput
- stream to write to
-
dumpAll
public void dumpAll(Iterator<? extends Object> data, Writer output)
Serialize a sequence of Java objects into a YAML stream.- Parameters:
data
- Iterator with Objectsoutput
- stream to write to
-
dumpAs
public String dumpAs(Object data, Tag rootTag, DumperOptions.FlowStyle flowStyle)
Serialize a Java object into a YAML string. Override the default root tag with
rootTag
.This method is similar to
Yaml.dump(data)
except that the root tag for the whole document is replaced with the given tag. This has two main uses.First, if the root tag is replaced with a standard YAML tag, such as
Tag.MAP
, then the object will be dumped as a map. The root tag will appear as!!map
, or blank (implicit !!map).Second, if the root tag is replaced by a different custom tag, then the document appears to be a different type when loaded. For example, if an instance of MyClass is dumped with the tag !!YourClass, then it will be handled as an instance of YourClass when loaded.
- Parameters:
data
- Java object to be serialized to YAMLrootTag
- the tag for the whole YAML document. The tag should be Tag.MAP for a JavaBean to make the tag disappear (to use implicit tag !!map). Ifnull
is provided then the standard tag with the full class name is used.flowStyle
- flow style for the whole document. See Chapter 10. Collection Styles http://yaml.org/spec/1.1/#id930798. Ifnull
is provided then the flow style from DumperOptions is used.- Returns:
- YAML String
-
dumpAsMap
public String dumpAsMap(Object data)
Serialize a Java object into a YAML string. Override the default root tag with
Tag.MAP
.This method is similar to
Yaml.dump(data)
except that the root tag for the whole document is replaced withTag.MAP
tag (implicit !!map).Block Mapping is used as the collection style. See 10.2.2. Block Mappings (http://yaml.org/spec/1.1/#id934537)
- Parameters:
data
- Java object to be serialized to YAML- Returns:
- YAML String
-
serialize
public void serialize(Node node, Writer output)
Serialize (dump) a YAML node into a YAML stream.- Parameters:
node
- YAML node to be serialized to YAMLoutput
- stream to write to
-
serialize
public List<Event> serialize(Node data)
Serialize the representation tree into Events.- Parameters:
data
- representation tree- Returns:
- Event list
- See Also:
- Processing Overview
-
load
public <T> T load(String yaml)
Parse the only YAML document in a String and produce the corresponding Java object. (Because the encoding in known BOM is not respected.)- Type Parameters:
T
- the class of the instance to be created- Parameters:
yaml
- YAML data to load from (BOM must not be present)- Returns:
- parsed object
-
load
public <T> T load(InputStream io)
Parse the only YAML document in a stream and produce the corresponding Java object.- Type Parameters:
T
- the class of the instance to be created- Parameters:
io
- data to load from (BOM is respected to detect encoding and removed from the data)- Returns:
- parsed object
-
load
public <T> T load(Reader io)
Parse the only YAML document in a stream and produce the corresponding Java object.- Type Parameters:
T
- the class of the instance to be created- Parameters:
io
- data to load from (BOM must not be present)- Returns:
- parsed object
-
loadAs
public <T> T loadAs(Reader io, Class<? super T> type)
Parse the only YAML document in a stream and produce the corresponding Java object.- Type Parameters:
T
- Class is defined by the second argument- Parameters:
io
- data to load from (BOM must not be present)type
- Class of the object to be created- Returns:
- parsed object
-
loadAs
public <T> T loadAs(String yaml, Class<? super T> type)
Parse the only YAML document in a String and produce the corresponding Java object. (Because the encoding in known BOM is not respected.)- Type Parameters:
T
- Class is defined by the second argument- Parameters:
yaml
- YAML data to load from (BOM must not be present)type
- Class of the object to be created- Returns:
- parsed object
-
loadAs
public <T> T loadAs(InputStream input, Class<? super T> type)
Parse the only YAML document in a stream and produce the corresponding Java object.- Type Parameters:
T
- Class is defined by the second argument- Parameters:
input
- data to load from (BOM is respected to detect encoding and removed from the data)type
- Class of the object to be created- Returns:
- parsed object
-
loadAll
public Iterable<Object> loadAll(Reader yaml)
Parse all YAML documents in the Reader and produce corresponding Java objects. The documents are parsed only when the iterator is invoked.- Parameters:
yaml
- YAML data to load from (BOM must not be present)- Returns:
- an Iterable over the parsed Java objects in this String in proper sequence
-
loadAll
public Iterable<Object> loadAll(String yaml)
Parse all YAML documents in a String and produce corresponding Java objects. (Because the encoding in known BOM is not respected.) The documents are parsed only when the iterator is invoked.- Parameters:
yaml
- YAML data to load from (BOM must not be present)- Returns:
- an Iterable over the parsed Java objects in this String in proper sequence
-
loadAll
public Iterable<Object> loadAll(InputStream yaml)
Parse all YAML documents in a stream and produce corresponding Java objects. The documents are parsed only when the iterator is invoked.- Parameters:
yaml
- YAML data to load from (BOM is respected to detect encoding and removed from the data)- Returns:
- an Iterable over the parsed Java objects in this stream in proper sequence
-
compose
public Node compose(Reader yaml)
Parse the first YAML document in a stream and produce the corresponding representation tree. (This is the opposite of the represent() method)- Parameters:
yaml
- YAML document- Returns:
- parsed root Node for the specified YAML document
- See Also:
- Figure 3.1. Processing Overview
-
composeAll
public Iterable<Node> composeAll(Reader yaml)
Parse all YAML documents in a stream and produce corresponding representation trees.- Parameters:
yaml
- stream of YAML documents- Returns:
- parsed root Nodes for all the specified YAML documents
- See Also:
- Processing Overview
-
addImplicitResolver
public void addImplicitResolver(Tag tag, Pattern regexp, String first)
Add an implicit scalar detector. If an implicit scalar value matches the given regexp, the corresponding tag is assigned to the scalar.- Parameters:
tag
- tag to assign to the noderegexp
- regular expression to match againstfirst
- a sequence of possible initial characters or null (which means any).
-
addImplicitResolver
public void addImplicitResolver(Tag tag, Pattern regexp, String first, int limit)
Add an implicit scalar detector. If an implicit scalar value matches the given regexp, the corresponding tag is assigned to the scalar.- Parameters:
tag
- tag to assign to the noderegexp
- regular expression to match againstfirst
- a sequence of possible initial characters or null (which means any).limit
- the max length of the value which may match the regular expression
-
getName
public String getName()
Get a meaningful name. It simplifies debugging in a multi-threaded environment. If nothing is set explicitly the address of the instance is returned.- Returns:
- human readable name
-
setName
public void setName(String name)
Set a meaningful name to be shown in toString()- Parameters:
name
- human readable name
-
parse
public Iterable<Event> parse(Reader yaml)
Parse a YAML stream and produce parsing events.- Parameters:
yaml
- YAML document(s)- Returns:
- parsed events
- See Also:
- Processing Overview
-
setBeanAccess
public void setBeanAccess(BeanAccess beanAccess)
-
addTypeDescription
public void addTypeDescription(TypeDescription td)
-
-