|
rapidyaml
0.11.0
parse and emit YAML, and do it fast
|
Example code for every feature. More...
Modules | |
| Sample helpers | |
| Helper utilities used in the sample. | |
Functions | |
| void | sample_lightning_overview () |
| a lightning tour over most features see sample_quick_overview More... | |
| void | sample_quick_overview () |
| a brief tour over most features More... | |
| void | sample_substr () |
| demonstrate usage of ryml::substr and ryml::csubstr More... | |
| void | sample_parse_file () |
| demonstrate how to load a YAML file from disk to parse with ryml. More... | |
| void | sample_parse_in_place () |
| demonstrate in-place parsing of a mutable YAML source buffer. More... | |
| void | sample_parse_in_arena () |
| demonstrate parsing of a read-only YAML source buffer More... | |
| void | sample_parse_reuse_tree () |
| demonstrate reuse/modification of tree when parsing More... | |
| void | sample_parse_reuse_parser () |
| Demonstrates reuse of an existing parser. More... | |
| void | sample_parse_reuse_tree_and_parser () |
| for ultimate speed when parsing multiple times, reuse both the tree and parser More... | |
| void | sample_iterate_trees () |
| shows how to programatically iterate through trees More... | |
| void | sample_create_trees () |
| shows how to programatically create trees More... | |
| void | sample_tree_arena () |
| demonstrates explicit and implicit interaction with the tree's string arena. More... | |
| void | sample_fundamental_types () |
| ryml provides facilities for serializing and deserializing the C++ fundamental types, including boolean and null values; this is provided by the several overloads in to_chars: generalized chars to value and from_chars: generalized chars to value. More... | |
| void | sample_empty_null_values () |
| Shows how to deal with empty/null values. More... | |
| void | sample_formatting () |
| ryml provides facilities for formatting/deformatting (imported from c4core into the ryml namespace). More... | |
| void | sample_base64 () |
| demonstrates how to read and write base64-encoded blobs. More... | |
| void | sample_user_scalar_types () |
| to add scalar types (ie leaf types converting to/from string), define the functions above for those types. More... | |
| void | sample_user_container_types () |
| shows how to serialize/deserialize container types. More... | |
| void | sample_std_types () |
| demonstrates usage with the std implementations provided by ryml in the ryml_std.hpp header More... | |
| void | sample_float_precision () |
| control precision of serialized floats More... | |
| void | sample_emit_to_container () |
| demonstrates how to emit to a linear container of char More... | |
| void | sample_emit_to_stream () |
| demonstrates how to emit to a stream-like structure More... | |
| void | sample_emit_to_file () |
| demonstrates how to emit to a FILE* More... | |
| void | sample_emit_nested_node () |
| just like parsing into a nested node, you can also emit from a nested node. More... | |
| void | sample_style () |
| [experimental] query/set/modify node style to control formatting of emitted YAML code. More... | |
| void | sample_style_flow_ml_indent () |
| [experimental] control the indentation of emitted FLOW_ML containers More... | |
| void | sample_style_flow_ml_filter () |
| [experimental] set the parser to pick FLOW_SL even if the container being parsed is FLOW_ML More... | |
| void | sample_json () |
| shows how to parse and emit JSON. More... | |
| void | sample_anchors_and_aliases () |
| demonstrates usage with anchors and alias references. More... | |
| void | sample_anchors_and_aliases_create () |
| demonstrates how to use the API to programatically create anchors and aliases More... | |
| void | sample_tags () |
| void | sample_tag_directives () |
| void | sample_docs () |
| void | sample_error_handler () |
| demonstrates how to set a custom error handler for ryml More... | |
| void | sample_error_basic () |
| void | sample_error_parse () |
| void | sample_error_visit () |
| Visit errors happen when an error is triggered while reading from a node. More... | |
| void | sample_error_visit_location () |
| It is possible to obtain the YAML location from a visit error: when the tree is obtained from parsing YAML, the messages may be enriched by using a parser set to track the locations. More... | |
| void | sample_global_allocator () |
| demonstrates how to set the global allocator for ryml More... | |
| void | sample_per_tree_allocator () |
| void | sample_static_trees () |
| shows how to work around the static initialization order fiasco when using a static-duration ryml tree More... | |
| void | sample_location_tracking () |
| demonstrates how to obtain the (zero-based) location of a node from a recently parsed tree More... | |
Example code for every feature.
This file does a quick tour of ryml.
It has multiple self-contained and well-commented samples that illustrate how to use ryml, and how it works.
Although this is not a unit test, the samples are written as a sequence of actions and predicate checks to better convey what is the expected result at any stage. And to ensure the code here is correct and up to date, it's also run as part of the CI tests.
If something is unclear, please open an issue or send a pull request at https://github.com/biojppm/rapidyaml . If you have an issue while using ryml, it is also encouraged to try to reproduce the issue here, or look first through the relevant section.
Happy ryml'ing!
The directories that exist side-by-side with this file contain several examples on how to build this with cmake, such that you can hit the ground running. See the relevant section of the main README for an overview of the different choices. I suggest starting first with the add_subdirectory example, treating it just like any other self-contained cmake project.
Or very quickly, to build and run this sample on your PC, start by creating this CMakeLists.txt:
Now run the following commands in the same folder:
| void sample_lightning_overview | ( | ) |
a lightning tour over most features see sample_quick_overview
Definition at line 310 of file quickstart.cpp.
References c4::yml::NodeRef::append_child(), CHECK, c4::yml::emit_yaml(), and c4::yml::parse_in_place().
| void sample_quick_overview | ( | ) |
a brief tour over most features
Definition at line 358 of file quickstart.cpp.
References c4::yml::NodeRef::append_child(), c4::yml::Tree::arena(), ErrorHandlerExample::callbacks(), c4::yml::Tree::callbacks(), CHECK, ErrorHandlerExample::check_assertion_occurs(), ErrorHandlerExample::check_error_occurs(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::children(), c4::yml::Location::col, c4::yml::emit_yaml(), c4::yml::Tree::find_child(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::first_child(), c4::yml::Tree::first_child(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::first_sibling(), c4::yml::Tree::first_sibling(), c4::FTOA_FLOAT, c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::has_child(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::has_key(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::has_val(), c4::yml::ConstNodeRef::id(), c4::yml::Tree::in_arena(), c4::yml::ConstNodeRef::invalid(), c4::yml::NodeRef::invalid(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::is_map(), c4::yml::Tree::is_map(), c4::yml::NodeRef::is_seed(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::is_seq(), c4::yml::Tree::is_seq(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::is_val(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::key(), c4::yml::Tree::key(), c4::yml::key(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::last_child(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::last_sibling(), c4::yml::Location::line, c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::location(), c4::yml::ParseEngine< EventHandler >::location_contents(), c4::yml::MAP, c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::next_sibling(), c4::yml::Tree::next_sibling(), c4::yml::NONE, c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::num_children(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::num_siblings(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::parent(), c4::yml::parse_in_arena(), c4::yml::parse_in_place(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::prev_sibling(), c4::yml::ConstNodeRef::readable(), c4::yml::NodeRef::readable(), c4::fmt::real(), c4::yml::Tree::root_id(), c4::yml::Tree::rootref(), c4::yml::SEQ, c4::yml::Tree::size(), c4::to_csubstr(), c4::yml::ConstNodeRef::tree(), and c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::val().
| void sample_substr | ( | ) |
demonstrate usage of ryml::substr and ryml::csubstr
These types are imported from the c4core library into the ryml namespace You may have noticed above the use of a csubstr class. This class is defined in another library, c4core, which is imported by ryml. This is a library I use with my projects consisting of multiplatform low-level utilities. One of these is c4::csubstr (the name comes from "constant substring") which is a non-owning read-only string view, with many methods that make it practical to use (I would certainly argue more practical than std::string). In fact, c4::csubstr and its writeable counterpart c4::substr are the workhorses of the ryml parsing and serialization code.
Definition at line 982 of file quickstart.cpp.
References CHECK, c4::yml::npos, c4::to_csubstr(), and c4::to_substr().
| void sample_parse_file | ( | ) |
demonstrate how to load a YAML file from disk to parse with ryml.
ryml offers no overload to directly parse files from disk; it only parses source buffers (which may be mutable or immutable). It is up to the caller to load the file contents into a buffer before parsing with ryml.
But that does not mean that loading a file is unimportant. There are many ways to achieve this in C++, but for convenience and to enable you to quickly get up to speed, here is an example implementation loading a file from disk and then parsing the resulting buffer with ryml.
Definition at line 1789 of file quickstart.cpp.
References CHECK, file_put_contents(), c4::yml::parse_in_arena(), c4::yml::parse_in_place(), c4::to_csubstr(), and c4::to_substr().
| void sample_parse_in_place | ( | ) |
demonstrate in-place parsing of a mutable YAML source buffer.
Definition at line 1835 of file quickstart.cpp.
References CHECK, c4::yml::Tree::crootref(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::is_map(), c4::yml::key(), and c4::yml::parse_in_place().
| void sample_parse_in_arena | ( | ) |
demonstrate parsing of a read-only YAML source buffer
Definition at line 1889 of file quickstart.cpp.
References CHECK, c4::yml::Tree::crootref(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::is_map(), c4::yml::key(), and c4::yml::parse_in_arena().
| void sample_parse_reuse_tree | ( | ) |
demonstrate reuse/modification of tree when parsing
Definition at line 1935 of file quickstart.cpp.
References c4::yml::NodeRef::append_child(), CHECK, c4::yml::Tree::clear(), c4::yml::Tree::clear_arena(), c4::yml::Tree::crootref(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::is_map(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::is_seq(), c4::yml::key(), c4::yml::KEY, c4::yml::KEYMAP, c4::yml::KEYSEQ, c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::num_children(), c4::yml::parse_in_arena(), c4::yml::Tree::reserve(), c4::yml::Tree::reserve_arena(), and c4::yml::Tree::rootref().
| void sample_parse_reuse_parser | ( | ) |
Demonstrates reuse of an existing parser.
Doing this is recommended when multiple files are parsed.
Definition at line 2087 of file quickstart.cpp.
References CHECK, c4::yml::parse_in_arena(), and c4::yml::ParseEngine< EventHandler >::reserve_stack().
| void sample_parse_reuse_tree_and_parser | ( | ) |
for ultimate speed when parsing multiple times, reuse both the tree and parser
Definition at line 2114 of file quickstart.cpp.
References CHECK, c4::yml::Tree::clear(), c4::yml::parse_in_arena(), c4::yml::Tree::reserve(), and c4::yml::ParseEngine< EventHandler >::reserve_stack().
| void sample_iterate_trees | ( | ) |
shows how to programatically iterate through trees
Definition at line 2172 of file quickstart.cpp.
References CHECK, c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::children(), c4::yml::Tree::crootref(), c4::yml::key(), and c4::yml::parse_in_arena().
| void sample_create_trees | ( | ) |
shows how to programatically create trees
Definition at line 2238 of file quickstart.cpp.
References c4::yml::NodeRef::append_child(), c4::fmt::boolalpha(), CHECK, c4::yml::NodeRef::invalid(), c4::yml::NodeRef::is_seed(), c4::yml::MAP, c4::fmt::real(), c4::yml::Tree::rootref(), c4::yml::SEQ, and c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::val().
| void sample_tree_arena | ( | ) |
demonstrates explicit and implicit interaction with the tree's string arena.
Notice that ryml only holds strings in the tree's nodes.
Definition at line 2319 of file quickstart.cpp.
References c4::yml::Tree::alloc_arena(), c4::yml::Tree::arena(), c4::yml::Tree::arena_capacity(), CHECK, c4::yml::Tree::copy_to_arena(), c4::yml::Tree::key(), c4::yml::parse_in_arena(), c4::yml::parse_in_place(), c4::yml::Tree::reserve_arena(), c4::yml::Tree::rootref(), c4::yml::Tree::set_val(), c4::yml::NodeRef::set_val_serialized(), c4::yml::Tree::to_arena(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::val(), and c4::yml::Tree::val().
| void sample_fundamental_types | ( | ) |
ryml provides facilities for serializing and deserializing the C++ fundamental types, including boolean and null values; this is provided by the several overloads in to_chars: generalized chars to value and from_chars: generalized chars to value.
To add serialization for user scalar types (ie, those types that should be serialized as strings in leaf nodes), you just need to define the appropriate overloads of to_chars and from_chars for those types; see sample_user_scalar_types for an example on how to achieve this, and see Serialization/deserialization for more information on serialization.
Definition at line 2484 of file quickstart.cpp.
References c4::yml::Tree::arena(), c4::fmt::boolalpha(), ErrorHandlerExample::callbacks(), CHECK, ErrorHandlerExample::check_error_occurs(), c4::yml::parse_in_arena(), and c4::yml::Tree::to_arena().
| void sample_empty_null_values | ( | ) |
Shows how to deal with empty/null values.
See also c4::yml::Tree::val_is_null
Definition at line 2577 of file quickstart.cpp.
References c4::yml::Tree::arena(), CHECK, c4::yml::Tree::clear(), c4::yml::Tree::clear_arena(), c4::yml::MAP, c4::yml::parse_in_arena(), c4::yml::Tree::rootref(), and c4::yml::scalar_is_null().
| void sample_formatting | ( | ) |
ryml provides facilities for formatting/deformatting (imported from c4core into the ryml namespace).
See Format utilities . These functions are very useful to serialize and deserialize scalar types; see Serialization/deserialization .
Definition at line 2744 of file quickstart.cpp.
References c4::fmt::bin, c4::fmt::boolalpha(), c4::cat(), c4::cat_sub(), c4::catrs(), c4::catrs_append(), c4::catsep(), c4::catsep_sub(), c4::catseprs(), c4::catseprs_append(), CHECK, c4::fmt::craw(), c4::format(), c4::format_sub(), c4::formatrs(), c4::formatrs_append(), c4::FTOA_FLEX, c4::FTOA_FLOAT, c4::FTOA_HEXA, c4::FTOA_SCIENT, c4::fmt::hex, c4::fmt::left(), c4::yml::npos, c4::fmt::oct, c4::fmt::raw(), c4::fmt::real(), c4::fmt::right(), c4::to_csubstr(), c4::uncat(), c4::uncatsep(), c4::unformat(), and c4::fmt::zpad().
| void sample_base64 | ( | ) |
demonstrates how to read and write base64-encoded blobs.
Definition at line 3178 of file quickstart.cpp.
References c4::yml::NodeRef::append_child(), c4::fmt::base64(), c4::base64_decode(), CHECK, c4::yml::Tree::key(), c4::yml::key(), c4::yml::MAP, c4::yml::Tree::rootref(), c4::yml::Tree::size(), c4::to_substr(), and c4::yml::Tree::val().
| void sample_user_scalar_types | ( | ) |
to add scalar types (ie leaf types converting to/from string), define the functions above for those types.
See Serialize/deserialize scalar types.
Definition at line 3562 of file quickstart.cpp.
References CHECK, c4::yml::MAP, and c4::yml::Tree::rootref().
| void sample_user_container_types | ( | ) |
shows how to serialize/deserialize container types.
Definition at line 3758 of file quickstart.cpp.
References CHECK, c4::yml::Tree::crootref(), my_type::map, my_map_type< K, V >::map_member, c4::yml::Tree::rootref(), my_type::seq, my_seq_type< T >::seq_member, my_type::v2, my_type::v3, my_type::v4, vec4< T >::w, vec2< T >::x, vec3< T >::x, vec4< T >::x, vec2< T >::y, vec3< T >::y, vec4< T >::y, vec3< T >::z, and vec4< T >::z.
| void sample_std_types | ( | ) |
demonstrates usage with the std implementations provided by ryml in the ryml_std.hpp header
Definition at line 3819 of file quickstart.cpp.
References CHECK, c4::yml::parse_in_place(), c4::yml::Tree::rootref(), and c4::to_substr().
| void sample_float_precision | ( | ) |
control precision of serialized floats
Definition at line 3883 of file quickstart.cpp.
References c4::yml::NodeRef::append_child(), CHECK, c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::children(), c4::FTOA_FLOAT, c4::yml::parse_in_arena(), c4::fmt::real(), c4::yml::Tree::rootref(), c4::yml::SEQ, and c4::to_csubstr().
| void sample_emit_to_container | ( | ) |
demonstrates how to emit to a linear container of char
Definition at line 4016 of file quickstart.cpp.
References CHECK, c4::yml::emit_yaml(), c4::yml::emitrs_yaml(), c4::yml::parse_in_arena(), c4::yml::Tree::root_id(), c4::to_csubstr(), and c4::to_substr().
| void sample_emit_to_stream | ( | ) |
demonstrates how to emit to a stream-like structure
Definition at line 4137 of file quickstart.cpp.
References CHECK, c4::yml::parse_in_arena(), and c4::to_csubstr().
| void sample_emit_to_file | ( | ) |
demonstrates how to emit to a FILE*
Definition at line 4230 of file quickstart.cpp.
References CHECK, c4::yml::emit_yaml(), c4::yml::parse_in_arena(), and c4::yml::Tree::root_id().
| void sample_emit_nested_node | ( | ) |
just like parsing into a nested node, you can also emit from a nested node.
Definition at line 4263 of file quickstart.cpp.
References CHECK, and c4::yml::parse_in_arena().
| void sample_style | ( | ) |
[experimental] query/set/modify node style to control formatting of emitted YAML code.
Definition at line 4306 of file quickstart.cpp.
References c4::yml::BLOCK, CHECK, c4::yml::NodeRef::clear_style(), c4::yml::Tree::clear_style(), c4::yml::FLOW_ML, c4::yml::FLOW_SL, c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::is_block(), c4::yml::KEY_DQUO, c4::yml::KEY_FOLDED, c4::yml::KEY_SQUO, c4::yml::parse_in_arena(), c4::yml::Tree::rootref(), c4::yml::NodeRef::set_container_style(), c4::yml::NodeRef::set_key_style(), c4::yml::NodeRef::set_val_style(), c4::yml::VAL_DQUO, c4::yml::VAL_LITERAL, c4::yml::VAL_PLAIN, and c4::yml::VAL_SQUO.
| void sample_style_flow_ml_indent | ( | ) |
[experimental] control the indentation of emitted FLOW_ML containers
Definition at line 4478 of file quickstart.cpp.
| void sample_style_flow_ml_filter | ( | ) |
[experimental] set the parser to pick FLOW_SL even if the container being parsed is FLOW_ML
Definition at line 4517 of file quickstart.cpp.
| void sample_json | ( | ) |
shows how to parse and emit JSON.
To emit YAML parsed from JSON, see also sample_style() for info on clearing the style flags (example below).
Definition at line 4584 of file quickstart.cpp.
| void sample_anchors_and_aliases | ( | ) |
demonstrates usage with anchors and alias references.
Note that dereferencing is opt-in; after parsing, you have to call c4::yml::Tree::resolve() explicitly if you want resolved references in the tree. This method will resolve all references and substitute the anchored values in place of the reference.
The c4::yml::Tree::resolve() method first does a full traversal of the tree to gather all anchors and references in a separate collection, then it goes through that collection to locate the names, which it does by obeying the YAML standard diktat that
> an alias node refers to the most recent node in > the serialization having the specified anchor
So, depending on the number of anchor/alias nodes, this is a potentially expensive operation, with a best-case linear complexity (from the initial traversal) and a worst-case quadratic complexity (if every node has an alias/anchor). This potential cost is the reason for requiring an explicit call to c4::yml::Tree::resolve()
Definition at line 4672 of file quickstart.cpp.
References CHECK, and c4::yml::parse_in_arena().
| void sample_anchors_and_aliases_create | ( | ) |
demonstrates how to use the API to programatically create anchors and aliases
Definition at line 4747 of file quickstart.cpp.
| void sample_tags | ( | ) |
Definition at line 4830 of file quickstart.cpp.
| void sample_tag_directives | ( | ) |
Definition at line 4970 of file quickstart.cpp.
| void sample_docs | ( | ) |
Definition at line 5004 of file quickstart.cpp.
| void sample_error_handler | ( | ) |
demonstrates how to set a custom error handler for ryml
Definition at line 5144 of file quickstart.cpp.
| void sample_error_basic | ( | ) |
Definition at line 5164 of file quickstart.cpp.
| void sample_error_parse | ( | ) |
Definition at line 5191 of file quickstart.cpp.
| void sample_error_visit | ( | ) |
Visit errors happen when an error is triggered while reading from a node.
Definition at line 5340 of file quickstart.cpp.
References ErrorHandlerExample::callbacks(), CHECK, ErrorHandlerExample::check_error_occurs(), c4::yml::Location::col, ErrorHandlerExample::defaults, c4::yml::ExceptionParse::errdata_parse, c4::yml::get_callbacks(), c4::yml::Location::line, c4::yml::location_format_with_context(), c4::yml::Callbacks::m_error_parse, c4::yml::Location::name, c4::yml::npos, c4::yml::Location::offset, c4::yml::parse_in_arena(), ErrorHandlerExample::saved_basic_loc, ErrorHandlerExample::saved_msg_full, ErrorHandlerExample::saved_msg_short, ErrorHandlerExample::saved_parse_loc, c4::yml::set_callbacks(), c4::to_csubstr(), c4::yml::ExceptionBasic::what(), and c4::yml::ErrorDataParse::ymlloc.
| void sample_error_visit_location | ( | ) |
It is possible to obtain the YAML location from a visit error: when the tree is obtained from parsing YAML, the messages may be enriched by using a parser set to track the locations.
See sample_location_tracking() for more details on how to use locations.
Definition at line 5444 of file quickstart.cpp.
| void sample_global_allocator | ( | ) |
demonstrates how to set the global allocator for ryml
Definition at line 5616 of file quickstart.cpp.
| void sample_per_tree_allocator | ( | ) |
Definition at line 5739 of file quickstart.cpp.
| void sample_static_trees | ( | ) |
shows how to work around the static initialization order fiasco when using a static-duration ryml tree
Definition at line 5773 of file quickstart.cpp.
| void sample_location_tracking | ( | ) |
demonstrates how to obtain the (zero-based) location of a node from a recently parsed tree
Definition at line 5799 of file quickstart.cpp.