Single header YAML 1.0 C++11 serializer/deserializer.
Looking for mini-yaml by jimmiebergman?
key: foo bar
list:
- hello world
- integer: 123
boolean: true
// NOTE: Do not forget to #define YAML_DEF before you import the library!
YAML::Node root;
YAML::Parse(root, "file.txt");
// Print all scalars.
std::cout << root["key"].As<std::string>() << std::endl;
std::cout << root["list"][0].As<std::string>() << std::endl;
std::cout << root["list"][1]["integer"].As<int>() << std::endl;
std::cout << root["list"][1]["boolean"].As<bool>() << std::endl;
// Iterate second sequence item.
Node & item = root[1];
for(auto it = item.Begin(); it != item.End(); it++)
{
std::cout << (*it).first << ": " << (*it).second.As<string>() << std::endl;
}foo bar
hello world
123
1
integer: 123
boolean: true
See Best practice.
Put yaml.hpp in your project directory and
#define YAML_DEF
#include <yaml.hpp>See examples/FirstExample.cpp for additional examples.
Always use references when accessing node content, if not intended to make a copy. Modifying copied node wont affect the original node content.
See example:
YAML::Node root;
YAML::Node & ref = root; // The content of "root" is not being copied.
ref["key"] = "value"; // Modifying "root" node content.
YAML::Node copy = root; // The content of "root" is copied to "copy".
// Slow operation if "root" contains a lot of content.
copy["key"] = "value"; // Modifying "copy" node content. "root" is left untouched.You can create sequences in the following manors;
std::vector<std::string> names {
"Jarod", "Mark", "Luise", "Felicia"
};
YAML::Node method1; // The standard method, simply manually add each value into the node sequence
for (int i = 0; i < names.size(); i++)
{
method1["names"].PushBack();
method1["names"][i] = names.at(i);
}
YAML::Node method2; // The added method, simply call the AddSequence function
YAML::AddSequence(method2["names"], names);
YAML::Node method3; // The operator method, this will call this above AddSequence function
method3["names"] = names;Builds are passed if all tests are good and no memory leaks were found.
| Branch | Status |
|---|---|
| master |
- Parse/serialize tags(!!type).
- Parse anchors.
- Parse flow sequences/maps.
- Parse complex keys.
- Parse sets.
Changes from jimmiebergmann-mini-yaml
- Moved to a single header with a define instead of a header and cpp combo.
- Added type abstraction layer so you don't have to cast things to strings before serializing them.
- Removed unnecessary work folders.
- Updated gitignore to ignore vscode stuff.
- Added
AddSequence(node, std::vector)to YAML namespace. - Added sequence operator to Node
- Added empty check to iterators, preventing looping endlessly. NOTE: This does NOT fix the iterator bug.
- Fixed
~ is not included