Store data in yaml format using Fortran

I want to store data (scalars, strings, and arrays) created as output in Fortran program in a yaml file so as to use it in other Fortran programs or in python.

Google search shows some yaml parser for Fortran such as fortran-yaml-cpp, http://yaml-fortran.com. However, I want to store data in yaml format rather than parse it using Fortran.

Any suggestion will be helpful.

Writing in a format doesn’t require a dedicated library, unless arbitrary data structures should be serialized. Depending on the complexity of the data, just writing the values in a handcrafted routine might be sufficient.

Maybe it is fine for fewer data with different variables. However, the YAML has many features (see the link YAML ) and a handcrafted routine, in the long run, might not be a good idea.

Maybe, for the time being, it’s good to just save the data in a non-standard format and read it using python and then parse it using another Fortran program.

thanks for your reply.

For some of my projects dumping JSON I usually have a handcrafted writer and an optional reader implemented via one of the available JSON libraries. The writer can be easily tested by roundtripping data in the testsuite. I agree that this approach is usually insufficient for more general data.

If you need complete YAML support (reading + writing), you might want to try yaFyaml:

2 Likes

Thanks for the suggestion.

By the way, what is your opinion about JSON versus YAML formats for data handling in Fortran? Maybe there should be another thread for this comparison.

Personally, I like JSON for machine produced data which is read by another automatic tool. For human facing configuration / input data I prefer TOML, since the verbosity of format limits the complexity you can create in your configuration / input files. However, if you want to express more dynamic configuration like a workflow or pipeline, YAML together with a small embedded interpreter seems like a decent choice, since it allows deep nesting and is quite flexible in interpreting the data.

Dynamic configuration is always a difficult topic, having a bespoke interpreter to process the internal state is another source of bugs and you are half-way to having created your own domain specific language. At this point it might be already worth to consider embedding an interpreted language, like Lua, or reconsider the complexity of the configuration input files by reducing the input complexity and expanding the API, which is exported to an interpreted language, like Python.

2 Likes

A good rule of thumb is that you use JSON for primarily machine-created content that you also want humans to be able to read, and you use YAML for primarily human-created content that you also want machines to read. (TOML would fit in the latter category, most likely.)

Based on the data structures you’ve mentioned (scalars, strings, arrays), JSON will do all that for you while keeping the parsing simple. I’d start with JSON and move to YAML/TOML if needed.

2 Likes

yaFyaml does not fully support yaml. It can not parse all of yaml.

Also @snano, You should use fortran-yaml-c instead of fortran-yaml-cpp. Long story short, Fortran and C are more interoperable than Fortran and C++.

It is possible to output yaml with fortran-yaml-c (or fortran-yaml-cpp - the API is almost the same). but it is a bit clumsy. Here is an example

1 Like