rapidyaml 0.15.2
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 6191 of file quickstart.cpp.

6192{
6193 ryml::csubstr yml = ""
6194 "---" "\n"
6195 "a: 0" "\n"
6196 "b: 1" "\n"
6197 "---" "\n"
6198 "c: 2" "\n"
6199 "d: 3" "\n"
6200 "---" "\n"
6201 "- 4" "\n"
6202 "- 5" "\n"
6203 "- 6" "\n"
6204 "- 7" "\n"
6205 "";
6206 ryml::Tree tree = ryml::parse_in_arena(yml);
6208
6209 // iteration through docs
6210 {
6211 // using the node API
6212 const ryml::ConstNodeRef stream = tree.rootref();
6213 CHECK(stream.is_root());
6214 CHECK(stream.is_stream());
6215 CHECK(!stream.is_doc());
6216 CHECK(stream.num_children() == 3);
6217 for(const ryml::ConstNodeRef doc : stream.children())
6218 CHECK(doc.is_doc());
6219 CHECK(tree.docref(0).id() == stream.child(0).id());
6220 CHECK(tree.docref(1).id() == stream.child(1).id());
6221 CHECK(tree.docref(2).id() == stream.child(2).id());
6222 // equivalent: using the lower level index API
6223 const ryml::id_type stream_id = tree.root_id();
6224 CHECK(tree.is_root(stream_id));
6225 CHECK(tree.is_stream(stream_id));
6226 CHECK(!tree.is_doc(stream_id));
6227 CHECK(tree.num_children(stream_id) == 3);
6228 for(ryml::id_type doc_id = tree.first_child(stream_id);
6229 doc_id != ryml::NONE;
6230 doc_id = tree.next_sibling(stream_id))
6231 CHECK(tree.is_doc(doc_id));
6232 CHECK(tree.doc(0) == tree.child(stream_id, 0));
6233 CHECK(tree.doc(1) == tree.child(stream_id, 1));
6234 CHECK(tree.doc(2) == tree.child(stream_id, 2));
6235
6236 // using the node API
6237 CHECK(stream[0].is_doc());
6238 CHECK(stream[0].is_map());
6239 CHECK(stream[0]["a"].val() == "0");
6240 CHECK(stream[0]["b"].val() == "1");
6241 // equivalent: using the index API
6242 const ryml::id_type doc0_id = tree.first_child(stream_id);
6243 CHECK(tree.is_doc(doc0_id));
6244 CHECK(tree.is_map(doc0_id));
6245 CHECK(tree.val(tree.find_child(doc0_id, "a")) == "0");
6246 CHECK(tree.val(tree.find_child(doc0_id, "b")) == "1");
6247
6248 // using the node API
6249 CHECK(stream[1].is_doc());
6250 CHECK(stream[1].is_map());
6251 CHECK(stream[1]["c"].val() == "2");
6252 CHECK(stream[1]["d"].val() == "3");
6253 // equivalent: using the index API
6254 const ryml::id_type doc1_id = tree.next_sibling(doc0_id);
6255 CHECK(tree.is_doc(doc1_id));
6256 CHECK(tree.is_map(doc1_id));
6257 CHECK(tree.val(tree.find_child(doc1_id, "c")) == "2");
6258 CHECK(tree.val(tree.find_child(doc1_id, "d")) == "3");
6259
6260 // using the node API
6261 CHECK(stream[2].is_doc());
6262 CHECK(stream[2].is_seq());
6263 CHECK(stream[2][0].val() == "4");
6264 CHECK(stream[2][1].val() == "5");
6265 CHECK(stream[2][2].val() == "6");
6266 CHECK(stream[2][3].val() == "7");
6267 // equivalent: using the index API
6268 const ryml::id_type doc2_id = tree.next_sibling(doc1_id);
6269 CHECK(tree.is_doc(doc2_id));
6270 CHECK(tree.is_seq(doc2_id));
6271 CHECK(tree.val(tree.child(doc2_id, 0)) == "4");
6272 CHECK(tree.val(tree.child(doc2_id, 1)) == "5");
6273 CHECK(tree.val(tree.child(doc2_id, 2)) == "6");
6274 CHECK(tree.val(tree.child(doc2_id, 3)) == "7");
6275 }
6276
6277 // Note: ryml emits streams as a JSON seq by default:
6279 "[" "\n"
6280 " {" "\n"
6281 " \"a\": 0," "\n"
6282 " \"b\": 1" "\n"
6283 " }," "\n"
6284 " {" "\n"
6285 " \"c\": 2," "\n"
6286 " \"d\": 3" "\n"
6287 " }," "\n"
6288 " [" "\n"
6289 " 4," "\n"
6290 " 5," "\n"
6291 " 6," "\n"
6292 " 7" "\n"
6293 " ]" "\n"
6294 "]\n"
6295 "");
6296 // ... but you can use EmitOptions{} to set the emitter to fail if
6297 // it finds a stream:
6298 {
6299 ScopedErrorHandlerExample errh; // calls ryml::set_callbacks()
6300 ryml::Tree err_tree = ryml::parse_in_arena(yml);
6301 CHECK(err_tree.callbacks() == errh.callbacks());
6302 auto err_opts = ryml::EmitOptions{}.json_err_on_stream(true);
6303 CHECK(errh.check_error_occurs([&]{
6304 return ryml::emitrs_json<std::string>(err_tree, err_opts);
6305 }));
6306 // in which case, you can avoid the error by emitting the
6307 // documents one-by-one:
6308 const std::string expected_json[] = {
6309 "{" "\n"
6310 " \"a\": 0," "\n"
6311 " \"b\": 1" "\n"
6312 "}" "\n"
6313 ,
6314 "{" "\n"
6315 " \"c\": 2," "\n"
6316 " \"d\": 3" "\n"
6317 "}" "\n"
6318 ,
6319 "[" "\n"
6320 " 4," "\n"
6321 " 5," "\n"
6322 " 6," "\n"
6323 " 7" "\n"
6324 "]" "\n"
6325 };
6326 ryml::id_type count = 0;
6327 const ryml::ConstNodeRef stream = err_tree;
6328 CHECK(stream.num_children() == (ryml::id_type)C4_COUNTOF(expected_json));
6329 for(ryml::ConstNodeRef doc : stream.children())
6330 CHECK(ryml::emitrs_json<std::string>(doc, err_opts) == expected_json[count++]);
6331 }
6332}
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
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
@ NONE
an index to none
Definition common.hpp:131
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().