rapidyaml  0.13.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  my_seq_type< T >
 example user container type: seq-like More...
 
struct  my_map_type< K, V >
 example user container type: map-like More...
 
struct  my_type
 example user container type with nested container members. More...
 

Functions

template<class T >
void write (ryml::NodeRef *n, my_seq_type< T > const &seq)
 
template<class K , class V >
void write (ryml::NodeRef *n, my_map_type< K, V > const &map)
 
void write (ryml::NodeRef *n, my_type const &val)
 
template<class T >
bool read (ryml::ConstNodeRef const &n, my_seq_type< T > *seq)
 
template<class K , class V >
bool read (ryml::ConstNodeRef const &n, my_map_type< K, V > *map)
 
bool 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:856
A reference to a node in an existing yaml tree, offering a more convenient API than the index-based A...
Definition: node.hpp:996
void write(NodeRef *n, T const &v)
Definition: node.hpp:1633
bool read(ConstNodeRef const &n, T *v)
Definition: node.hpp:1639
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_container_types
sample_std_types

Function Documentation

◆ write() [1/3]

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

Definition at line 3764 of file quickstart.cpp.

3765 {
3766  *n |= ryml::SEQ;
3767  for(auto const& v : seq.seq_member)
3768  n->append_child() << v;
3769 }
NodeRef append_child()
Definition: node.hpp:1414
@ SEQ
a seq: a parent of VAL/SEQ/MAP nodes
Definition: node_type.hpp:40
std::vector< T > seq_member

◆ write() [2/3]

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

Definition at line 3771 of file quickstart.cpp.

3772 {
3773  *n |= ryml::MAP;
3774  for(auto const& v : map.map_member)
3775  n->append_child() << ryml::key(v.first) << v.second;
3776 }
@ MAP
a map: a parent of KEYVAL/KEYSEQ/KEYMAP nodes
Definition: node_type.hpp:39
Key< K > key(K &k)
Definition: node.hpp:46
std::map< K, V > map_member

◆ write() [3/3]

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

Definition at line 3777 of file quickstart.cpp.

3778 {
3779  *n |= ryml::MAP;
3780  // these are leaf nodes:
3781  n->append_child() << ryml::key("v2") << val.v2;
3782  n->append_child() << ryml::key("v3") << val.v3;
3783  n->append_child() << ryml::key("v4") << val.v4;
3784  // these are container nodes:
3785  n->append_child() << ryml::key("seq") << val.seq;
3786  n->append_child() << ryml::key("map") << val.map;
3787 }

◆ read() [1/3]

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

Definition at line 3790 of file quickstart.cpp.

3791 {
3792  seq->seq_member.resize(static_cast<size_t>(n.num_children())); // num_children() is O(N)
3793  size_t pos = 0;
3794  for(auto const ch : n.children())
3795  ch >> seq->seq_member[pos++];
3796  return true;
3797 }

◆ read() [2/3]

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

Definition at line 3799 of file quickstart.cpp.

3800 {
3801  K k{};
3802  V v{};
3803  for(auto const ch : n)
3804  {
3805  ch >> c4::yml::key(k) >> v;
3806  map->map_member.emplace(std::make_pair(std::move(k), std::move(v)));
3807  }
3808  return true;
3809 }

◆ read() [3/3]

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

Definition at line 3810 of file quickstart.cpp.

3811 {
3812  // these are leaf nodes:
3813  n["v2"] >> val->v2;
3814  n["v3"] >> val->v3;
3815  n["v4"] >> val->v4;
3816  // these are container nodes:
3817  n["seq"] >> val->seq;
3818  n["map"] >> val->map;
3819  return true;
3820 }
vec2< int > v2
my_map_type< int, int > map
vec3< int > v3
vec4< int > v4
my_seq_type< int > seq