How to dump a digit string with leading zeros, as a valid yaml string in yaml-cpp?

荒凉一梦 提交于 2019-12-11 16:54:41

问题


Creating a yaml string with leading zeros is not escaped with quotes in yaml-cpp. So writing the string to a texfile is not a valid yaml-string.leading_zeros: 00005 is 5 according to the specification yaml 1.2 (Try yourself: http://www.yamllint.com/)

YAML::Node node;
node["leading_zeros"] = "00005";
std::cout << YAML::Dump(node)<<std::endl;
// output: leading_zeros: 00005
// instead of:leading_zeros: "00005"

How to bring yaml-cpp to escape a string with leading zeros? So that is would not be interpreted as integer from other yaml parser?

Escaping manually does not seem to be the correct answer.

node["leading_zeros"] = "\"00005\"";

Update: The digit value is stored in a YAML::Node! I am pretty sure it is a bug.


回答1:


Use the YAML::Emitter directly:

YAML::Emitter out;
out << YAML::BeginMap;
out << YAML::Key << "leading_zeroes" << YAML::Value;
out << YAML::Value << YAML::DoubleQuoted << "00005";
out << YAML::EndMap;


来源:https://stackoverflow.com/questions/54820610/how-to-dump-a-digit-string-with-leading-zeros-as-a-valid-yaml-string-in-yaml-cp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!