Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Figuring out if a string will round-trip #1276

Open
gyorokpeter opened this issue Mar 27, 2024 · 2 comments
Open

Figuring out if a string will round-trip #1276

gyorokpeter opened this issue Mar 27, 2024 · 2 comments

Comments

@gyorokpeter
Copy link

E.g. if I render a string with the value true or 1.1, when parsed back they become a boolean and a number respectively.
Is there a way to determine if a certain string doesn't round trip and therefore needs quotes added when emitting it?

@SGSSGene
Copy link
Contributor

I am not sure what you are asking for.
If you read them with node.as<std::string>() they will roundtrip just fine.

Maybe you can clarify?

@gyorokpeter
Copy link
Author

It has been a while since I raised this, and we made a workaround in the meantime, but the situation was roughly this:

  • We have a function that takes some object provided by the user and converts it to YAML
  • It uses YAML::Emitter's operator<< to output the object
  • Previously, if the user object was a string, it would just call out << str, which blindly outputs the string without consideration of whether it has some special format such that when parsed back it turns into something else such as a number, boolean, null etc.
  • This was causing problems when the user object was something like ["my", "true", "love"], where the "true" value would be printed out without the quotes, so when parsed back, it would become the boolean value true instead of the string "true"
  • The workaround was to create a dedicated "render string" function that matches the input against a laundry list of regexes, such as those for all the number formats, infinities, true/false etc., and if any of them match, does out << YAML::SingleQuoted first. Since this looks like a common operation for anyone who would like to output YAML from user data, I was wondering if there was some level of support in the library itself, such as an "is special value" function that encapsulates the check for whether the string would round-trip without the quotes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants