问题
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