Function sample_parse_read_only

Synopsis

#include <samples/quickstart.cpp>

void sample_parse_read_only()

Description

parse a read-only YAML source buffer

demonstrate parsing of a read-only YAML source buffer

Mentioned in

Source

Lines 1264-1289 in samples/quickstart.cpp. Line 51 in samples/quickstart.cpp.

void sample_parse_read_only()
{
    // to parse read-only memory, ryml will copy first to the tree's arena.
    // but usage is similar to in-situ
    ryml::Tree tree = ryml::parse("{foo: 1, bar: [2, 3]}");
    ryml::NodeRef root = tree.rootref(); // get a reference to the root

    CHECK(root.is_map());
    CHECK(root["foo"].is_keyval());
    CHECK(root["foo"].key() == "foo");
    CHECK(root["foo"].val() == "1");
    CHECK(root["bar"].is_seq());
    CHECK(root["bar"].has_key());
    CHECK(root["bar"].key() == "bar");
    CHECK(root["bar"][0].val() == "2");
    CHECK(root["bar"][1].val() == "3");

    // deserializing:
    int foo = 0, bar0 = 0, bar1 = 0;
    root["foo"] >> foo;
    root["bar"][0] >> bar0;
    root["bar"][1] >> bar1;
    CHECK(foo == 1);
    CHECK(bar0 == 2);
    CHECK(bar1 == 3);
}





Add Discussion as Guest

Log in