Function sample_emit_to_stream
Synopsis
#include <samples/quickstart.cpp>
void sample_emit_to_stream()
Description
emit to a stream, eg std::ostream
demonstrates how to emit to a stream-like structure
Mentioned in
- Getting Started / Quick start
Source
Lines 2936-2994 in samples/quickstart.cpp. Line 66 in samples/quickstart.cpp.
void sample_emit_to_stream()
{
ryml::csubstr ymlb = R"(- a
- b
- x0: 1
x1: 2
- champagne: Dom Perignon
coffee: Arabica
more:
vinho verde: Soalheiro
vinho tinto: Redoma 2017
beer:
- Rochefort 10
- Busch
- Leffe Rituel
- foo
- bar
- baz
- bat
)";
auto tree = ryml::parse(ymlb);
std::string s;
// emit a full tree
{
std::stringstream ss;
ss << tree; // works with any stream having .operator<<() and .write()
s = ss.str();
CHECK(ryml::to_csubstr(s) == ymlb);
}
// emit a full tree as json
{
std::stringstream ss;
ss << ryml::as_json(tree); // works with any stream having .operator<<() and .write()
s = ss.str();
CHECK(ryml::to_csubstr(s) == R"(["a","b",{"x0": 1,"x1": 2},{"champagne": "Dom Perignon","coffee": "Arabica","more": {"vinho verde": "Soalheiro","vinho tinto": "Redoma 2017"},"beer": ["Rochefort 10","Busch","Leffe Rituel"]},"foo","bar","baz","bat"])");
}
// emit a nested node
{
std::stringstream ss;
ss << tree[3][2]; // works with any stream having .operator<<() and .write()
s = ss.str();
CHECK(ryml::to_csubstr(s) == R"(more:
vinho verde: Soalheiro
vinho tinto: Redoma 2017
)");
}
// emit a nested node as json
{
std::stringstream ss;
ss << ryml::as_json(tree[3][2]); // works with any stream having .operator<<() and .write()
s = ss.str();
CHECK(ryml::to_csubstr(s) == R"("more": {"vinho verde": "Soalheiro","vinho tinto": "Redoma 2017"})");
}
}