biojppm / rapidyaml

Rapid YAML - a library to parse and emit YAML, and do it fast.
MIT License
583 stars 100 forks source link

Serialize a vector<int> #371

Closed ftpronk closed 1 year ago

ftpronk commented 1 year ago

Hello,

How can a std::vector {1, 2, 3} be serialize correctly to [1, 2, 3]?

I've tried:

int main(int argc, char *argv[])
{
  ryml::Tree tree1;
  ryml::NodeRef root = tree.rootref();
  root |= ryml::MAP; // mark root as a map
  root["int_1"]   << 42;
  root["list_1"]  << std::vector<int>{1, 2, 3};

  std::string str_result = ryml::emitrs_yaml<std::string>(tree);
  cout << str_result << endl;

  return 0;
}

And the result is:

int_1: 42
list_1:
  - 1
  - 2
  - 3
biojppm commented 1 year ago

See #370, specifically this comment.

ftpronk commented 1 year ago

Addressed one issue before mine.. facepalm

Thank you for coming back to me, I'll take a look.

ftpronk commented 1 year ago

For posterity, to serialize a std::vector<> inline, use the experimental flag _WIP_STYLE_FLOW_SL.

My example code becomes:

int main(int argc, char *argv[])
{
  ryml::Tree tree1;
  ryml::NodeRef root = tree.rootref();
  root |= ryml::MAP; // mark root as a map
  root["int_1"]   << 42;
  root["list_1"]  << std::vector<int>{1, 2, 3};
  root["list_1"] |= c4::yml::_WIP_STYLE_FLOW_SL;

  std::string str_result = ryml::emitrs_yaml<std::string>(tree);
  std::cout << str_result << std::endl;

  return 0;
}

With the output:

int_1: 42
list_1: [1,2,3]