From: Felix K. <ya...@fl...> - 2021-08-16 19:19:36
|
Hi David, libyaml does contain such an API: https://github.com/yaml/libyaml/blob/master/include/yaml.h#L1457 This gives you a `yaml_document_t` with a root node accessible via `yaml_document_get_root_node`. The API is not well-documented but basically nodes can refer to sub-nodes via an `int` index and you use `yaml_document_get_node` to retrieve subnodes. mapping and sequence nodes store their children in a stack structure – you iterate them via ``` for (yaml_node_item_t *cur = node->data.sequence.start; cur != node->data.sequence.top; ++cur) { // ... } ``` Now if you load in a YAML that contains a mapping with scalar key/value pairs, your root node will be a mapping and the subnodes will be scalars. --- If you want a more convenient API, I did write this utility in the past: https://github.com/flyx/libyaml_constructor It is a code generator that reads in a C header containing type definitions and autogenerates code that loads YAML into those types (using libyaml). With this, you can define how your YAML structure must look via C types and some annotations in the given header. That tool was a part of a project which I abandoned, hence I never did a proper release. However it did work quite well for all I can remember. The compile-time dependency on libclang (to parse your C header) may be a bit of a hurdle. Cheers, Felix |