rapidyaml  0.10.0
parse and emit YAML, and do it fast
Serialize/deserialize container types

To serialize/deserialize container types to a tree, implement the appropriate functions: More...

Classes

struct  sample::my_seq_type< T >
 example user container type: seq-like More...
 
struct  sample::my_map_type< K, V >
 example user container type: map-like More...
 
struct  sample::my_type
 example user container type with nested container members. More...
 

Functions

template<class T >
void sample::write (ryml::NodeRef *n, my_seq_type< T > const &seq)
 
template<class K , class V >
void sample::write (ryml::NodeRef *n, my_map_type< K, V > const &map)
 
void sample::write (ryml::NodeRef *n, my_type const &val)
 
template<class T >
bool sample::read (ryml::ConstNodeRef const &n, my_seq_type< T > *seq)
 
template<class K , class V >
bool sample::read (ryml::ConstNodeRef const &n, my_map_type< K, V > *map)
 
bool sample::read (ryml::ConstNodeRef const &n, my_type *val)
 

Detailed Description

To serialize/deserialize container types to a tree, implement the appropriate functions:

void write(ryml::NodeRef *n, T const& seq);
bool read(ryml::ConstNodeRef const& n, T *seq);
Holds a pointer to an existing tree, and a node id.
Definition: node.hpp:855
A reference to a node in an existing yaml tree, offering a more convenient API than the index-based A...
Definition: node.hpp:995
void write(ryml::NodeRef *n, my_seq_type< T > const &seq)
bool read(ryml::ConstNodeRef const &n, my_seq_type< T > *seq)
Warning
Because of C++'s ADL rules, it is required to overload these functions in the namespace of the type you're serializing (or in the c4 namespace, or in the c4::yml namespace). [Here's an example of an issue where failing to do this was causing problems in some platforms](https://github.com/biojppm/rapidyaml/issues/424)
Note
Please take note of the following pitfall when using serialization functions: you may have to include the header with your write() or read() implementation before any other headers that use functions from it. See the include order at the top of this source 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. Lately, some effort was directed to provide forward declarations to alleviate this problem, but it may still occur.
See also
sample::sample_container_types
sample::sample_std_types

Function Documentation

◆ write() [1/3]

template<class T >
void sample::write ( ryml::NodeRef n,
my_seq_type< T > const &  seq 
)

Definition at line 3641 of file quickstart.cpp.

3642 {
3643  *n |= ryml::SEQ;
3644  for(auto const& v : seq.seq_member)
3645  n->append_child() << v;
3646 }
NodeRef append_child()
Definition: node.hpp:1413
@ SEQ
a seq: a parent of VAL/SEQ/MAP nodes
Definition: node_type.hpp:39

References c4::yml::NodeRef::append_child(), c4::yml::SEQ, and sample::my_seq_type< T >::seq_member.

◆ write() [2/3]

template<class K , class V >
void sample::write ( ryml::NodeRef n,
my_map_type< K, V > const &  map 
)

Definition at line 3648 of file quickstart.cpp.

3649 {
3650  *n |= ryml::MAP;
3651  for(auto const& v : map.map_member)
3652  n->append_child() << ryml::key(v.first) << v.second;
3653 }
@ MAP
a map: a parent of KEYVAL/KEYSEQ/KEYMAP nodes
Definition: node_type.hpp:38
Key< K > key(K &k)
Definition: node.hpp:43

References c4::yml::NodeRef::append_child(), c4::yml::key(), c4::yml::MAP, and sample::my_map_type< K, V >::map_member.

◆ write() [3/3]

void sample::write ( ryml::NodeRef n,
my_type const &  val 
)

Definition at line 3654 of file quickstart.cpp.

3655 {
3656  *n |= ryml::MAP;
3657  // these are leaf nodes:
3658  n->append_child() << ryml::key("v2") << val.v2;
3659  n->append_child() << ryml::key("v3") << val.v3;
3660  n->append_child() << ryml::key("v4") << val.v4;
3661  // these are container nodes:
3662  n->append_child() << ryml::key("seq") << val.seq;
3663  n->append_child() << ryml::key("map") << val.map;
3664 }

References c4::yml::NodeRef::append_child(), c4::yml::key(), sample::my_type::map, c4::yml::MAP, sample::my_type::seq, sample::my_type::v2, sample::my_type::v3, and sample::my_type::v4.

◆ read() [1/3]

template<class T >
bool sample::read ( ryml::ConstNodeRef const &  n,
my_seq_type< T > *  seq 
)

Definition at line 3667 of file quickstart.cpp.

3668 {
3669  seq->seq_member.resize(static_cast<size_t>(n.num_children())); // num_children() is O(N)
3670  size_t pos = 0;
3671  for(auto const ch : n.children())
3672  ch >> seq->seq_member[pos++];
3673  return true;
3674 }

References c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::children(), c4::yml::detail::RoNodeMethods< Impl, ConstImpl >::num_children(), and sample::my_seq_type< T >::seq_member.

◆ read() [2/3]

template<class K , class V >
bool sample::read ( ryml::ConstNodeRef const &  n,
my_map_type< K, V > *  map 
)

Definition at line 3676 of file quickstart.cpp.

3677 {
3678  K k{};
3679  V v{};
3680  for(auto const ch : n)
3681  {
3682  ch >> c4::yml::key(k) >> v;
3683  map->map_member.emplace(std::make_pair(std::move(k), std::move(v)));
3684  }
3685  return true;
3686 }

References c4::yml::key(), and sample::my_map_type< K, V >::map_member.

◆ read() [3/3]

bool sample::read ( ryml::ConstNodeRef const &  n,
my_type val 
)

Definition at line 3687 of file quickstart.cpp.

3688 {
3689  // these are leaf nodes:
3690  n["v2"] >> val->v2;
3691  n["v3"] >> val->v3;
3692  n["v4"] >> val->v4;
3693  // these are container nodes:
3694  n["seq"] >> val->seq;
3695  n["map"] >> val->map;
3696  return true;
3697 }

References sample::my_type::map, sample::my_type::seq, sample::my_type::v2, sample::my_type::v3, and sample::my_type::v4.