rapidyaml  0.7.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:836
A reference to a node in an existing yaml tree, offering a more convenient API than the index-based A...
Definition: node.hpp:975
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 3608 of file quickstart.cpp.

3609 {
3610  *n |= ryml::SEQ;
3611  for(auto const& v : seq.seq_member)
3612  n->append_child() << v;
3613 }
NodeRef append_child()
Definition: node.hpp:1385
@ SEQ
a seq: a parent of VAL/SEQ/MAP nodes
Definition: node_type.hpp:36

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 3615 of file quickstart.cpp.

3616 {
3617  *n |= ryml::MAP;
3618  for(auto const& v : map.map_member)
3619  n->append_child() << ryml::key(v.first) << v.second;
3620 }
@ MAP
a map: a parent of KEYVAL/KEYSEQ/KEYMAP nodes
Definition: node_type.hpp:35
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 3621 of file quickstart.cpp.

3622 {
3623  *n |= ryml::MAP;
3624  // these are leaf nodes:
3625  n->append_child() << ryml::key("v2") << val.v2;
3626  n->append_child() << ryml::key("v3") << val.v3;
3627  n->append_child() << ryml::key("v4") << val.v4;
3628  // these are container nodes:
3629  n->append_child() << ryml::key("seq") << val.seq;
3630  n->append_child() << ryml::key("map") << val.map;
3631 }

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 3634 of file quickstart.cpp.

3635 {
3636  seq->seq_member.resize(static_cast<size_t>(n.num_children())); // num_children() is O(N)
3637  size_t pos = 0;
3638  for(auto const ch : n.children())
3639  ch >> seq->seq_member[pos++];
3640  return true;
3641 }

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 3643 of file quickstart.cpp.

3644 {
3645  K k{};
3646  V v{};
3647  for(auto const ch : n)
3648  {
3649  ch >> c4::yml::key(k) >> v;
3650  map->map_member.emplace(std::make_pair(std::move(k), std::move(v)));
3651  }
3652  return true;
3653 }

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 3654 of file quickstart.cpp.

3655 {
3656  // these are leaf nodes:
3657  n["v2"] >> val->v2;
3658  n["v3"] >> val->v3;
3659  n["v4"] >> val->v4;
3660  // these are container nodes:
3661  n["seq"] >> val->seq;
3662  n["map"] >> val->map;
3663  return true;
3664 }

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