rapidyaml 0.15.2
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
Serialize/deserialize container types

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 user types. More...

Functions

template<class T>
void write (ryml::Tree *tree, ryml::id_type id, my_seq_type< T > const &seq)
void write (ryml::Tree *tree, ryml::id_type id, my_seq_type< std::string > const &seq)
template<class K, class V>
void write (ryml::Tree *tree, ryml::id_type id, my_map_type< K, V > const &map)
void write (ryml::Tree *tree, ryml::id_type id, my_type const &val)
template<class T>
ryml::ReadResult read (ryml::Tree const *tree, ryml::id_type id, my_seq_type< T > *seq)
template<class K, class V>
ryml::ReadResult read (ryml::Tree const *tree, ryml::id_type id, my_map_type< K, V > *map)
ryml::ReadResult read (ryml::ConstNodeRef const &n, my_type *val)
ryml::ReadResult read (ryml::Tree const *tree, ryml::id_type id, my_type *val)

Detailed Description

Function Documentation

◆ write() [1/4]

template<class T>
void write ( ryml::Tree * tree,
ryml::id_type id,
my_seq_type< T > const & seq )

Definition at line 4164 of file quickstart.cpp.

4165{
4166 tree->set_seq(id);
4167 for(T const& v : seq.seq_member)
4168 {
4169 // inside write(), prefer using .set_serialized() instead of .save()
4170 tree->set_serialized(tree->append_child(id), v);
4171 }
4172}
void set_serialized(id_type node, T const &val) RYML_NOEXCEPT
Definition tree.hpp:823
void set_seq(id_type node) RYML_NOEXCEPT
Definition tree.hpp:720
id_type append_child(id_type parent)
create and insert a node as the last child of parent
Definition tree.hpp:1180
std::vector< T > seq_member

Referenced by c4::yml::Tree::save(), c4::yml::Tree::save(), c4::yml::Tree::set_serialized(), and c4::yml::Tree::set_serialized().

◆ write() [2/4]

void write ( ryml::Tree * tree,
ryml::id_type id,
my_seq_type< std::string > const & seq )

Definition at line 4176 of file quickstart.cpp.

4177{
4178 tree->set_seq(id);
4179 for(std::string const& v : seq.seq_member)
4180 {
4181 // now the tree is pointing at seq's strings. using .set_val()
4182 // does not serialize, and this avoids the copy to the tree's
4183 // arena
4184 tree->set_val(tree->append_child(id), ryml::to_csubstr(v));
4185 }
4186}
void set_val(id_type node, csubstr val) RYML_NOEXCEPT
Definition tree.hpp:688
csubstr to_csubstr(const char(&s)[N]) noexcept
Definition substr.hpp:2380

◆ write() [3/4]

template<class K, class V>
void write ( ryml::Tree * tree,
ryml::id_type id,
my_map_type< K, V > const & map )

Definition at line 4190 of file quickstart.cpp.

4191{
4192 tree->set_map(id);
4193 for(auto const& v : map.map_member)
4194 {
4195 // inside write(), prefer using .set_serialized() instead of .save()
4196 ryml::id_type child_id = tree->append_child(id);
4197 tree->set_key_serialized(child_id, v.first); // we're serializing the key!
4198 tree->set_serialized(child_id, v.second);
4199 }
4200}
void set_key_serialized(id_type node, T const &key) RYML_NOEXCEPT
Definition tree.hpp:837
void set_map(id_type node) RYML_NOEXCEPT
Definition tree.hpp:731
RYML_ID_TYPE id_type
The type of a node id in the YAML tree; to override the default type, define the macro RYML_ID_TYPE t...
Definition common.hpp:124
std::map< K, V > map_member

◆ write() [4/4]

void write ( ryml::Tree * tree,
ryml::id_type id,
my_type const & val )

Definition at line 4202 of file quickstart.cpp.

4203{
4204 tree->set_map(id);
4205 // inside write(), prefer using .set_serialized() instead of .save()
4206 //
4207 ryml::id_type ch;
4208 // these are leaf nodes:
4209 ch = tree->append_child(id); tree->set_key(ch, "v2"); tree->set_serialized(ch, val.v2);
4210 ch = tree->append_child(id); tree->set_key(ch, "v3"); tree->set_serialized(ch, val.v3);
4211 ch = tree->append_child(id); tree->set_key(ch, "v4"); tree->set_serialized(ch, val.v4);
4212 // these are container nodes (note how the call is equal):
4213 ch = tree->append_child(id); tree->set_key(ch, "seq"); tree->set_serialized(ch, val.seq);
4214 ch = tree->append_child(id); tree->set_key(ch, "map"); tree->set_serialized(ch, val.map);
4215 // Note above that we're NOT serializing the keys. That works and
4216 // is correct here because the keys themselves are fixed, and are
4217 // static strings located in the executable. But if the keys came
4218 // from the data, they too would have to be serialized with
4219 // .set_key_serialized().
4220}
void set_key(id_type node, csubstr key) RYML_NOEXCEPT
Definition tree.hpp:705

◆ read() [1/4]

template<class T>
ryml::ReadResult read ( ryml::Tree const * tree,
ryml::id_type id,
my_seq_type< T > * seq )

Definition at line 4231 of file quickstart.cpp.

4232{
4233 if(!tree->is_seq(id)) return ryml::ReadResult(id); // id must be a seq
4234 seq->seq_member.clear(); // we'll overwrite the vector
4235 for(ryml::id_type child = tree->first_child(id);
4236 child != ryml::NONE;
4237 child = tree->next_sibling(child))
4238 {
4239 // create a new entry
4240 seq->seq_member.emplace_back();
4241 // inside read() you SHOULD NOT use .load() because of its
4242 // exceptional flow. Instead, you should use .deserialize() to
4243 // play nice with .deserialize() callers calling this function.
4244 // Read more at the doxygen page linked above.
4245 ryml::ReadResult r = tree->deserialize(child, &seq->seq_member.back());
4246 if(!r) return r; // return the inner-most result
4247 }
4248 return ryml::ReadResult(); // all good
4249}
@ NONE
an index to none
Definition common.hpp:131
A lightweight truthy type, used to enable reporting the offending node when a deserializing error hap...
Definition common.hpp:162

Referenced by c4::yml::detail::RoNodeMethods< NodeRef, ConstNodeRef >::deserialize(), c4::yml::detail::RoNodeMethods< NodeRef, ConstNodeRef >::deserialize(), c4::yml::Tree::deserialize(), c4::yml::Tree::deserialize(), c4::yml::detail::RoNodeMethods< NodeRef, ConstNodeRef >::load(), c4::yml::detail::RoNodeMethods< NodeRef, ConstNodeRef >::load(), c4::yml::Tree::load(), and c4::yml::Tree::load().

◆ read() [2/4]

template<class K, class V>
ryml::ReadResult read ( ryml::Tree const * tree,
ryml::id_type id,
my_map_type< K, V > * map )

Definition at line 4252 of file quickstart.cpp.

4253{
4254 if(!tree->is_map(id)) return ryml::ReadResult(id); // id must be a seq
4255 for(ryml::id_type child = tree->first_child(id);
4256 child != ryml::NONE;
4257 child = tree->next_sibling(child))
4258 {
4259 K k{};
4260 // again, we're using .deserialize() instead of .load()
4261 // because (1) we should gracefully return a ReadResult and
4262 // anyway (2) we're certain the node is readable (because
4263 // we're inside the loop).
4264 ryml::ReadResult r = tree->deserialize_key(child, &k);
4265 if(r) r = tree->deserialize(child, &map->map_member[std::move(k)]);
4266 if(!r) return r; // return the inner-most result
4267 }
4268 return ryml::ReadResult(); // all good.
4269}

◆ read() [3/4]

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

Definition at line 4287 of file quickstart.cpp.

4288{
4289 ryml::ReadResult r(n.is_map(), n.id()); // node must be a map
4290 if(r) r = n.deserialize_child("v2", &val->v2);
4291 if(r) r = n.deserialize_child("v3", &val->v3);
4292 if(r) r = n.deserialize_child("v4", &val->v4);
4293 if(r) r = n.deserialize_child("seq", &val->seq);
4294 if(r) r = n.deserialize_child("map", &val->map);
4295 // hint: you can also add a default argument for when no such child exists
4296 return r;
4297}
vec2< int > v2
my_map_type< int, int > map
vec3< int > v3
vec4< int > v4
my_seq_type< int > seq

◆ read() [4/4]

ryml::ReadResult read ( ryml::Tree const * tree,
ryml::id_type id,
my_type * val )

Definition at line 4304 of file quickstart.cpp.

4305{
4306 ryml::ReadResult r(tree->is_map(id), id); // node must be a map
4307 if(r) r = tree->deserialize_child(id, "v2", &val->v2);
4308 if(r) r = tree->deserialize_child(id, "v3", &val->v3);
4309 if(r) r = tree->deserialize_child(id, "v4", &val->v4);
4310 if(r) r = tree->deserialize_child(id, "seq", &val->seq);
4311 if(r) r = tree->deserialize_child(id, "map", &val->map);
4312 // hint: you can also add a default argument for when no such child exists
4313 return r;
4314}