Skip to content

Commit

Permalink
strip leading and trailing " to preserve expected behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jbohanon committed Jul 19, 2023
1 parent 0d15c31 commit a7972bb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
13 changes: 11 additions & 2 deletions include/inja/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,17 @@ class Renderer : public NodeVisitor {
}

void print_data(const std::shared_ptr<json> value) {
if (value->is_string() && !config.escape_strings) {
*output_stream << value->get_ref<const json::string_t&>();
if (value->is_string()) {
std::string val;
if (config.escape_strings) {
val = value->dump();
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
13 changes: 11 additions & 2 deletions single_include/inja/inja.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2124,8 +2124,17 @@ class Renderer : public NodeVisitor {
}

void print_data(const std::shared_ptr<json> value) {
if (value->is_string() && !config.escape_strings) {
*output_stream << value->get_ref<const json::string_t&>();
if (value->is_string()) {
std::string val;
if (config.escape_strings) {
val = value->dump();
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
2 changes: 1 addition & 1 deletion test/test-renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ TEST_CASE("types") {

CHECK(env.render(R"EOF({"Value":"{{ quoted }}"})EOF", data) == R"EOF({"Value":""quoted value""})EOF");
env.set_escape_strings(true);
CHECK(env.render(R"EOF({"Value":{{ quoted }}})EOF", data) == R"EOF({"Value":"\"quoted value\""})EOF");
CHECK(env.render(R"EOF({"Value":"{{ quoted }}"})EOF", data) == R"EOF({"Value":"\"quoted value\""})EOF");
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

0 comments on commit a7972bb

Please sign in to comment.