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

Allow escaped json to survive rendering #274

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/inja/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ struct ParserConfig {
*/
struct RenderConfig {
bool throw_at_missing_includes {true};
bool escape_strings {};
};

} // namespace inja
Expand Down
5 changes: 5 additions & 0 deletions include/inja/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class Environment {
lexer_config.lstrip_blocks = lstrip_blocks;
}

/// Sets the config for rendering strings raw or escaped
void set_escape_strings(bool escape_strings) {
render_config.escape_strings = escape_strings;
}

/// Sets the element notation syntax
void set_search_included_templates_in_files(bool search_in_files) {
parser_config.search_included_templates_in_files = search_in_files;
Expand Down
15 changes: 14 additions & 1 deletion include/inja/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,20 @@ class Renderer : public NodeVisitor {

void print_data(const std::shared_ptr<json> value) {
if (value->is_string()) {
*output_stream << value->get_ref<const json::string_t&>();
std::string val;
if (config.escape_strings) {
// 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;
} else {
val = value->get_ref<const json::string_t&>();
}
*output_stream << val;
} else if (value->is_number_integer()) {
*output_stream << value->get<const json::number_integer_t>();
} else if (value->is_null()) {
Expand Down
21 changes: 20 additions & 1 deletion single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ struct ParserConfig {
*/
struct RenderConfig {
bool throw_at_missing_includes {true};
bool escape_strings {};
};

} // namespace inja
Expand Down Expand Up @@ -2124,7 +2125,20 @@ class Renderer : public NodeVisitor {

void print_data(const std::shared_ptr<json> value) {
if (value->is_string()) {
*output_stream << value->get_ref<const json::string_t&>();
std::string val;
if (config.escape_strings) {
// 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;
} else {
val = value->get_ref<const json::string_t&>();
}
*output_stream << val;
} else if (value->is_number_integer()) {
*output_stream << value->get<const json::number_integer_t>();
} else if (value->is_null()) {
Expand Down Expand Up @@ -2772,6 +2786,11 @@ class Environment {
lexer_config.lstrip_blocks = lstrip_blocks;
}

/// Sets the config for rendering strings raw or escaped
void set_escape_strings(bool escape_strings) {
render_config.escape_strings = escape_strings;
}

/// Sets the element notation syntax
void set_search_included_templates_in_files(bool search_in_files) {
parser_config.search_included_templates_in_files = search_in_files;
Expand Down
6 changes: 6 additions & 0 deletions test/test-renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ TEST_CASE("types") {
data["relatives"]["brother"] = "Chris";
data["relatives"]["sister"] = "Jenny";
data["vars"] = {2, 3, 4, 0, -1, -2, -3};
data["quoted"] = "\"quoted value\"";

SUBCASE("basic") {
CHECK(env.render("", data) == "");
Expand All @@ -39,6 +40,11 @@ TEST_CASE("types") {
CHECK(env.render("{{ @name }}", data) == "@name");
CHECK(env.render("{{ $name }}", data) == "$name");

CHECK(env.render("{\"Value\":\"{{ quoted }}\"}", data) == "{\"Value\":\"\"quoted value\"\"}");
env.set_escape_strings(true);
CHECK(env.render("{\"Value\":\"{{ quoted }}\"}", data) == "{\"Value\":\"\\\"quoted value\\\"\"}");
env.set_escape_strings(false);

CHECK_THROWS_WITH(env.render("{{unknown}}", data), "[inja.exception.render_error] (at 1:3) variable 'unknown' not found");
}

Expand Down