Skip to content

Instantly share code, notes, and snippets.

@kerrickstaley
Created March 10, 2021 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerrickstaley/1e1e7c56eede6015b99f4ca47912f7d8 to your computer and use it in GitHub Desktop.
Save kerrickstaley/1e1e7c56eede6015b99f4ca47912f7d8 to your computer and use it in GitHub Desktop.
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <string>
int main() {
YAML::Node node = YAML::Load(R"foo(
a:
b: c
d: 1
)foo");
// Method without BeginSeq
YAML::Emitter emitter1;
emitter1 << YAML::DoubleQuoted << YAML::Flow << node;
std::string out1(emitter1.c_str());
std::cout << "Output without BeginSeq:\n" << out1 << '\n';
// Approach using BeginSeq
YAML::Emitter emitter2;
emitter2 << YAML::DoubleQuoted << YAML::Flow << YAML::BeginSeq << node;
std::string out2(emitter2.c_str() + 1); // Strip leading [ character
std::cout << "Output with BeginSeq:\n" << out2 << '\n';
}
/*
On my system, this program outputs:
Output without BeginSeq:
"a":
"b": "c"
"d": "1"
Output with BeginSeq:
{"a": {"b": "c", "d": "1"}}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment