rapidyaml 0.16.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
YAML documents

Functions

void sample_docs ()
 deal with YAML docs

Detailed Description

Function Documentation

◆ sample_docs()

void sample_docs ( )

deal with YAML docs

Definition at line 6275 of file quickstart.cpp.

6276{
6277 ryml::csubstr yml = ""
6278 "---" "\n"
6279 "a: 0" "\n"
6280 "b: 1" "\n"
6281 "---" "\n"
6282 "c: 2" "\n"
6283 "d: 3" "\n"
6284 "---" "\n"
6285 "- 4" "\n"
6286 "- 5" "\n"
6287 "- 6" "\n"
6288 "- 7" "\n"
6289 "";
6290 ryml::Tree tree = ryml::parse_in_arena(yml);
6292
6293 // iteration through docs
6294 {
6295 // using the node API
6296 const ryml::ConstNodeRef stream = tree.rootref();
6297 CHECK(stream.is_root());
6298 CHECK(stream.is_stream());
6299 CHECK(!stream.is_doc());
6300 CHECK(stream.num_children() == 3);
6301 for(const ryml::ConstNodeRef doc : stream.children())
6302 CHECK(doc.is_doc());
6303 CHECK(tree.docref(0).id() == stream.child(0).id());
6304 CHECK(tree.docref(1).id() == stream.child(1).id());
6305 CHECK(tree.docref(2).id() == stream.child(2).id());
6306 // equivalent: using the lower level index API
6307 const ryml::id_type stream_id = tree.root_id();
6308 CHECK(tree.is_root(stream_id));
6309 CHECK(tree.is_stream(stream_id));
6310 CHECK(!tree.is_doc(stream_id));
6311 CHECK(tree.num_children(stream_id) == 3);
6312 for(ryml::id_type doc_id = tree.first_child(stream_id);
6313 doc_id != ryml::NONE;
6314 doc_id = tree.next_sibling(stream_id))
6315 CHECK(tree.is_doc(doc_id));
6316 CHECK(tree.doc(0) == tree.child(stream_id, 0));
6317 CHECK(tree.doc(1) == tree.child(stream_id, 1));
6318 CHECK(tree.doc(2) == tree.child(stream_id, 2));
6319
6320 // using the node API
6321 CHECK(stream[0].is_doc());
6322 CHECK(stream[0].is_map());
6323 CHECK(stream[0]["a"].val() == "0");
6324 CHECK(stream[0]["b"].val() == "1");
6325 // equivalent: using the index API
6326 const ryml::id_type doc0_id = tree.first_child(stream_id);
6327 CHECK(tree.is_doc(doc0_id));
6328 CHECK(tree.is_map(doc0_id));
6329 CHECK(tree.val(tree.find_child(doc0_id, "a")) == "0");
6330 CHECK(tree.val(tree.find_child(doc0_id, "b")) == "1");
6331
6332 // using the node API
6333 CHECK(stream[1].is_doc());
6334 CHECK(stream[1].is_map());
6335 CHECK(stream[1]["c"].val() == "2");
6336 CHECK(stream[1]["d"].val() == "3");
6337 // equivalent: using the index API
6338 const ryml::id_type doc1_id = tree.next_sibling(doc0_id);
6339 CHECK(tree.is_doc(doc1_id));
6340 CHECK(tree.is_map(doc1_id));
6341 CHECK(tree.val(tree.find_child(doc1_id, "c")) == "2");
6342 CHECK(tree.val(tree.find_child(doc1_id, "d")) == "3");
6343
6344 // using the node API
6345 CHECK(stream[2].is_doc());
6346 CHECK(stream[2].is_seq());
6347 CHECK(stream[2][0].val() == "4");
6348 CHECK(stream[2][1].val() == "5");
6349 CHECK(stream[2][2].val() == "6");
6350 CHECK(stream[2][3].val() == "7");
6351 // equivalent: using the index API
6352 const ryml::id_type doc2_id = tree.next_sibling(doc1_id);
6353 CHECK(tree.is_doc(doc2_id));
6354 CHECK(tree.is_seq(doc2_id));
6355 CHECK(tree.val(tree.child(doc2_id, 0)) == "4");
6356 CHECK(tree.val(tree.child(doc2_id, 1)) == "5");
6357 CHECK(tree.val(tree.child(doc2_id, 2)) == "6");
6358 CHECK(tree.val(tree.child(doc2_id, 3)) == "7");
6359 }
6360
6361 // Note: ryml emits streams as a JSON seq by default:
6363 "[" "\n"
6364 " {" "\n"
6365 " \"a\": 0," "\n"
6366 " \"b\": 1" "\n"
6367 " }," "\n"
6368 " {" "\n"
6369 " \"c\": 2," "\n"
6370 " \"d\": 3" "\n"
6371 " }," "\n"
6372 " [" "\n"
6373 " 4," "\n"
6374 " 5," "\n"
6375 " 6," "\n"
6376 " 7" "\n"
6377 " ]" "\n"
6378 "]\n"
6379 "");
6380 // ... but you can use EmitOptions{} to set the emitter to fail if
6381 // it finds a stream:
6382 {
6383 ScopedErrorHandlerExample errh; // calls ryml::set_callbacks()
6384 ryml::Tree err_tree = ryml::parse_in_arena(yml);
6385 CHECK(err_tree.callbacks() == errh.callbacks());
6386 auto err_opts = ryml::EmitOptions{}.json_err_on_stream(true);
6387 CHECK(errh.check_error_occurs([&]{
6388 return ryml::emitrs_json<std::string>(err_tree, err_opts);
6389 }));
6390 // in which case, you can avoid the error by emitting the
6391 // documents one-by-one:
6392 const std::string expected_json[] = {
6393 "{" "\n"
6394 " \"a\": 0," "\n"
6395 " \"b\": 1" "\n"
6396 "}" "\n"
6397 ,
6398 "{" "\n"
6399 " \"c\": 2," "\n"
6400 " \"d\": 3" "\n"
6401 "}" "\n"
6402 ,
6403 "[" "\n"
6404 " 4," "\n"
6405 " 5," "\n"
6406 " 6," "\n"
6407 " 7" "\n"
6408 "]" "\n"
6409 };
6410 ryml::id_type count = 0;
6411 const ryml::ConstNodeRef stream = err_tree;
6412 CHECK(stream.num_children() == (ryml::id_type)C4_COUNTOF(expected_json));
6413 for(ryml::ConstNodeRef doc : stream.children())
6414 CHECK(ryml::emitrs_json<std::string>(doc, err_opts) == expected_json[count++]);
6415 }
6416}
Holds a pointer to an existing tree, and a node id.
Definition node.hpp:737
ConstNodeRef child(id_type pos) const RYML_NOEXCEPT
Forward to Tree::child().
Definition node.hpp:856
id_type id() const noexcept
Definition node.hpp:822
const_children_view children() const RYML_NOEXCEPT
get an iterable view over children
Definition node.hpp:987
id_type id() const noexcept
Definition node.hpp:1208
id_type first_child(id_type node) const
Definition tree.hpp:577
bool is_stream(id_type node) const
Definition tree.hpp:477
id_type root_id() const
Get the id of the root node. The tree must not be empty. The tree can be empty only when constructed ...
Definition tree.hpp:386
NodeRef rootref()
Get the root as a NodeRef . Note that a non-const Tree implicitly converts to NodeRef.
Definition tree.cpp:56
bool is_map(id_type node) const
Definition tree.hpp:480
csubstr const & val(id_type node) const
Definition tree.hpp:460
bool is_root(id_type node) const
Definition tree.hpp:529
id_type next_sibling(id_type node) const
Definition tree.hpp:572
NodeRef docref(id_type i)
get the i-th document of the stream
Definition tree.cpp:104
bool is_doc(id_type node) const
Definition tree.hpp:478
Callbacks const & callbacks() const
Definition tree.hpp:349
id_type doc(id_type i) const
gets the i document node index.
Definition tree.hpp:605
bool is_seq(id_type node) const
Definition tree.hpp:481
id_type find_child(id_type node, csubstr const &key) const
find child by name, or NONE if no child is found with this key like Tree::child(),...
Definition tree.cpp:1249
id_type num_children(id_type node) const
O(num_children).
Definition tree.cpp:1216
id_type child(id_type node, id_type pos) const
find child by position, or NONE if there are less than pos children posi
Definition tree.cpp:1237
substr emitrs_json(Tree const &t, id_type id, EmitOptions const &opts, CharOwningContainer *cont, bool append=false)
(1) emit+resize: emit JSON to the given std::string/std::vector<char>-like container,...
substr emitrs_yaml(Tree const &t, id_type id, EmitOptions const &opts, CharOwningContainer *cont, bool append=false)
(1) emit+resize: emit YAML to the given std::string/std::vector<char>-like container,...
void parse_in_arena(Parser *parser, csubstr filename, csubstr yaml, Tree *tree, id_type node_id)
(1) parse YAML into an existing tree node. The filename will be used in any error messages arising du...
Definition parse.cpp:209
bool check_error_occurs(Fn &&fn)
checking that an error occurs while calling fn
#define CHECK(predicate)
a testing assertion, used only in this quickstart
ryml::Callbacks callbacks()
a helper to create the Callbacks object for the custom error handler
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2356
@ NONE
an index to none
Definition common.hpp:131
RYML_ID_TYPE id_type
The type of a node id in the YAML tree; to override the default type, define the macro RYML_ID_TYPE t...
Definition common.hpp:124
Shows how to create a scoped error handler.
A lightweight object containing options to be used when emitting.
EmitOptions & json_err_on_stream(bool enabled) noexcept
Whether to trigger an error (or emit as seq) when finding a stream in json mode.
bool is_root() const RYML_NOEXCEPT
Forward to Tree::is_root().
Definition node.hpp:278
bool is_stream() const RYML_NOEXCEPT
Forward to Tree::is_stream().
Definition node.hpp:204
bool is_doc() const RYML_NOEXCEPT
Forward to Tree::is_doc().
Definition node.hpp:205
id_type num_children() const RYML_NOEXCEPT
O(num_children).
Definition node.hpp:302

Referenced by main().