How can I dump a JsonNode to yaml file using NimYAML?

Viewed 139

I'm starting to use nim-lang and I need to save a JsonNode in a YAML file.

I am using NimYAML successfully to load a yaml file into a JsonNode. But it is not yet clear to me how to do the opposite or if it is possible.

Can anyone give me some help or advice?

thanks

1 Answers

I am the author of NimYAML.

The JsonNode loader exists as bridge to existing code that processes JsonNode structures, so that you can enable that code to take input as YAML.

However, if you load YAML code with the goal to serialize it to YAML again then JsonNode is simply the wrong API to do that – you should use YamlNode instead.

If you have an existing JsonNode and want to output it as YAML, you can always serialize it as JSON – since JSON is a subset of YAML, a JSON serialization will also be valid YAML. If you explicitly want to have YAML's block-style collections, you need to serialize the JsonNode as JSON, load the result with NimYAML, change the style, and serialize it again as YAML.

Related