I have a YAML file that looks something like this:
---
name: Sam
tags:
- Dev
- Java
----
name: Bob
tags:
- PM
I'd like to use Jackson to deserialize all documents from the file, but I don't see a way to use a normal ObjectMapper
to do it. If I use the Looks like the parser created by my YAMLFactory only parses a single document out of the file.YAMLFactory
to create a parser for my file I can step through all tokens, so the parser is obviously capable of dealing with multiple documents - but how do I tie them together?
I've also tried creating a YAMLParser directly and using ObjectMapper#readValue(JsonParser, Class)
, but the ObjectMapper exhausts the entire YAMLParser to deserialize a single instance.
This is years later but it's worth pointing out that this is supported.
The Jackson semantics are slightly different, probably due to it's JSON origins. This can be achieved by using the MappingIterator
from ObjectMapper
.
YAMLFactory yaml;
ObjectMapper mapper;
YAMLParser yamlParser = yaml.createParser("file-with-multiple-docs.yaml")
List<ObjectNode> docs = mapper
.readValues<ObjectNode>(yamlParser, new TypeReference<ObjectNode> {})
.readAll();
Replace ObjectNode
with your own POJOs if desired.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With