-
Notifications
You must be signed in to change notification settings - Fork 0
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
Allow escaped json to survive rendering #5
Conversation
don't run push CI
// get the value as a dump() to properly escape values | ||
val = value->dump(); | ||
|
||
// strip the leading and trailing " characters that are added by dump() | ||
// if C++20 is adopted, val.starts_with and val.ends_with would clean this up a bit | ||
val = val.substr(0,1) == "\"" && val.substr(val.length()-1,1) == "\"" | ||
? val.substr(1, val.length()-2) | ||
: val; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one last thing i just noticed.. if this is already known to be a string, can we just get the string value instead of dumping as json?
i.e.
// store a string in a JSON value
json j_string = "this is a string";
// retrieve the string value
auto cpp_string = j_string.template get<std::string>();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
per readme in https://github.com/nlohmann/json
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The .get<std::string>()
does not escape characters, whereas .dump()
does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah thanks, i misunderstood. lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add option to .dump() strings instead of .getstd::string().
this is useful in cases where the output of the rendered template is expected to be valid JSON. For example:
Input:
Template:
Output (without allowing escaped strings):
Output (allowing escaped strings):