yaml-cpp icon indicating copy to clipboard operation
yaml-cpp copied to clipboard

YAML::Binary emitting and parsing

Open atomheartother opened this issue 3 years ago • 2 comments

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?

atomheartother avatar May 21 '21 13:05 atomheartother

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

r08ertliu avatar Jun 23 '21 21:06 r08ertliu

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 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?

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();

SupSuper avatar Sep 09 '21 10:09 SupSuper