-
-
Notifications
You must be signed in to change notification settings - Fork 52
YAML Streams
A YAML Stream is a collection of YAMLs coming altogether, separated by ---
(Start Marker). The end of a YAML can also be marked by ...
(End Marker), but this is optional. The first Start marker in the Stream is also optional. Here is an example of YAML Stream:
---
temperature: 25C
time: 12:00
...
---
temperature: 20C
time: 16:00
...
---
temperature: 19C
time: 20:00
...
However, pay attention that the documents in a YAML Stream don't necessarily have to have the same format. There can be different types of YAML coming. Maybe the first is a mapping, the second is a sequence etc.
final YamlStream built = Yaml.createYamlStreamBuilder()
.add(
Yaml.createYamlMappingBuilder()
.add("temperature", "25C")
.add("time", "12:00")
.build()
)
.add(...)
.add(...)
.build();
final Collection<YamlNode> documents = built.values()
//OR, you can use Java 8's Stream API.
built.filter(...).map(...).collect(...)
Put the above YAML Stream in a file called temps.yml
, and read it as follows:
final YamlStream read = Yaml.createYamlInput(
new File("temps.yml")
).readYamlStream();
final Collection<YamlNode> documents = read.values()
//OR, you can use Java 8's Stream API.
read.filter(...).map(...).collect(...)
To print the above read
YamlStream, just call toString()
:
---
temperature: 25C
time: 12:00
---
temperature: 20C
time: 16:00
---
temperature: 19C
time: 20:00
Note that our print does not have the End Markers (...
). We try to keep our printed YAMLs as simple as possible and, since they are optional, we chose to not print them.