Function sample_std_types

Synopsis

#include <samples/quickstart.cpp>

void sample_std_types()

Description

serialize/deserialize STL containers

Please take note of the following pitfall when using serialization functions: you have to include the header with the serialization before any other headers that use functions from it. see the include order at the top of this file.

This constraint also applies to the conversion functions for your types; just like with the STL's headers, they should be included prior to ryml's headers. demonstrates usage with the std implementations provided by ryml in the ryml_std.hpp header

Mentioned in

Source

Lines 2751-2809 in samples/quickstart.cpp. Line 64 in samples/quickstart.cpp.

void sample_std_types()
{
    std::string yml_std_string = R"(- v2: '(20,21)'
  v3: '(30,31,32)'
  v4: '(40,41,42,43)'
  seq:
    - 101
    - 102
    - 103
    - 104
    - 105
    - 106
    - 107
  map:
    1001: 2001
    1002: 2002
    1003: 2003
- v2: '(120,121)'
  v3: '(130,131,132)'
  v4: '(140,141,142,143)'
  seq:
    - 1101
    - 1102
    - 1103
    - 1104
    - 1105
    - 1106
    - 1107
  map:
    11001: 12001
    11002: 12002
    11003: 12003
- v2: '(220,221)'
  v3: '(230,231,232)'
  v4: '(240,241,242,243)'
  seq:
    - 2101
    - 2102
    - 2103
    - 2104
    - 2105
    - 2106
    - 2107
  map:
    21001: 22001
    21002: 22002
    21003: 22003
)";
    // parse in-place using the std::string above
    auto tree = ryml::parse(ryml::to_substr(yml_std_string));
    // my_type is a container-of-containers type. see above its
    // definition implementation for ryml.
    std::vector<my_type> vmt;
    tree.rootref() >> vmt;
    CHECK(vmt.size() == 3);
    ryml::Tree tree_out;
    tree_out.rootref() << vmt;
    CHECK(ryml::emitrs<std::string>(tree_out) == yml_std_string);
}





Add Discussion as Guest

Log in