yaml-cpp
yaml-cpp copied to clipboard
YAML::Binary emitting and parsing
Hello, I'm having trouble finding usage information on this entire namespace outside of this 11 year old Stackoverflow post.
Our use case is that parsing YAML from an actual YAML file takes too long, so we'd like to use some sort of binary serialization, and we're looking at YAML::Binary
as a way to save our files as a binary buffer, then load them.
So, according to an answer on this thread, from our YAML::Node
, we can do:
YAML::Binary binary = node.as<YAML::Binary>();
const unsigned char * data = binary.data();
std::size_t size = binary.size();
auto myfile = std::fstream("serializedYaml", std::ios::out | std::ios::binary);
myfile.write(data, size);
myfile.close();
The above doesn't work anymore, i assume it worked at the time:
terminate called after throwing an instance of 'YAML::TypedBadConversion<YAML::Binary>'
what(): yaml-cpp: error at line 1, column 1: bad conversion
Assuming i can serialize a node into a file, what's the fastest way I can go from this binary file to a YAML::Node
? I can make a YAML::Binary
from it, but how do I then make a YAML::Node
? And is it actually significantly faster than parsing the regular yaml file?
Encounter the same problem too. Even use the same test case in load_node_test.cpp.
terminate called after throwing an instance of 'YAML::TypedBadConversion<YAML::Binary>'
what(): bad conversion
Aborted
Hello, I'm having trouble finding usage information on this entire namespace outside of this 11 year old Stackoverflow post.
Our use case is that parsing YAML from an actual YAML file takes too long, so we'd like to use some sort of binary serialization, and we're looking at
YAML::Binary
as a way to save our files as a binary buffer, then load them.
YAML::Binary
does not convert YAML into binary. It's simply a "type" (like an int, float, etc) that represents an existing char* as a base64-encoded string in YAML, see https://yaml.org/type/binary.html
Assuming i can serialize a node into a file, what's the fastest way I can go from this binary file to a
YAML::Node
? I can make aYAML::Binary
from it, but how do I then make aYAML::Node
? And is it actually significantly faster than parsing the regular yaml file?
Binary is significantly faster to "parse" than the tree-like structure of YAML, and can be useful for compressing tightly packed data that would otherwise take up hundreds or thousands of nodes (eg. static arrays). But you still have to convert it to/from a char* yourself.
Example usage:
// encode
char *test = "abcdefghijklmnopqrstuvwxyz";
size_t len = 26;
YAML::Node node = YAML::Binary(test, len);
// decode
YAML::Binary bin = node.as<YAML::Binary>();
char *test = bin.data();
size_t len = bin.size();