Best way to save and load a HashMap as a sequence
See original GitHub issueHello, thank you for eo-yaml.
I am trying to use it to save/load my datastructure, which has a HashMap<String, CombinedEvent>.
I am confused as to how to use YamlSequence and it is not clear from the documentation how to do this. Documented sequences are basically lists of strings.
My code to dump and load the HashMap is as follows:
public Class CombinedEvents() {
private HashMap<String, CombinedEvent> all = new HashMap<>();
.
.
.
public YamlMapping toEOYaml() {
YamlSequenceBuilder combinedEventSeq = Yaml.createYamlSequenceBuilder();
for (string ceName : this.keySet()) {
combinedEventSeq = combinedEventSeq.add(this.get(ceName.toEOYaml(ceName));
}
return Yaml.createYamlMappingBuilder()
.add("all", combinedEventSeq.build()
).build();
public static CombinedEvents fromEOYaml(YamlMapping yml) {
CombinedEvents ces = new CombinedEvents();
final YamlSequence combinedEventSeq = ym.yamlSequence("all');
for (final YamlNode ceNode : combinedEventSeq) {
// -- not sure how to read back the sequence of CombinedEvents
}
This writes out a good yaml file, but I cannot figure out how to read it back. Also, I am not sure what the best OO way is to write a sequence from a HashMap. All the examples are writing a fixed length sequence. My code looks a bit kludgy to me in not having combinedEventSeq final.
It would be good to have a working example of saving and loading such a hashmap and I would be willing to provide one for inclusion, if only I could get it to work.
Any help would be appreciated.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (7 by maintainers)
Top GitHub Comments
@piet8stevens you’re welcome!
This is the way we’ve implemented eo-yaml: mapping elements in sequences start on a second line after the
-
. We had quite a few complaints about that because apparently the YAML specification allows both options (on the same line and after).It was only after we implemented this project that we realized the YAML spec is actually too flexible. Its authors are literally saying: “that should work, and that one as well… oh, let’s make it work in that other way too”…
@amihaiemil thank you for resolving this ticket. I’ve just added it to your active invoice. You can always check all your invoices and more on the Contributor Dashboard.