Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit e695d24

Browse files
committed
fix: escape json
1 parent ac9894c commit e695d24

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

engine/extensions/template_renderer.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include <regex>
88
#include <stdexcept>
99
#include "utils/logging_utils.h"
10+
#include "utils/string_utils.h"
1011
namespace extensions {
12+
1113
TemplateRenderer::TemplateRenderer() {
1214
// Configure Inja environment
1315
env_.set_trim_blocks(true);
@@ -21,10 +23,9 @@ TemplateRenderer::TemplateRenderer() {
2123
const auto& value = *args[0];
2224

2325
if (value.is_string()) {
24-
std::string v = value.get<std::string>();
25-
v = std::regex_replace(v, std::regex("\""), "\\\"");
26-
v = std::regex_replace(v, std::regex("\n"), "\\n");
27-
return nlohmann::json(std::string("\"") + v + "\"");
26+
return nlohmann::json(std::string("\"") +
27+
string_utils::EscapeJson(value.get<std::string>()) +
28+
"\"");
2829
}
2930
return value;
3031
});
@@ -48,7 +49,7 @@ std::string TemplateRenderer::Render(const std::string& tmpl,
4849
std::string result = env_.render(tmpl, template_data);
4950

5051
// Clean up any potential double quotes in JSON strings
51-
result = std::regex_replace(result, std::regex("\\\"\\\""), "\"");
52+
// result = std::regex_replace(result, std::regex("\\\"\\\""), "\"");
5253

5354
LOG_DEBUG << "Result: " << result;
5455

engine/test/components/test_remote_engine.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ TEST_F(RemoteEngineTest, CohereResponse) {
337337
// non-stream
338338
message = R"(
339339
{
340-
"text": "Isaac Newton was 'born' on 25 \"December\" 1642 (Old Style) \n\nor 4 January 1643 (New Style).",
340+
"text": "Isaac \t\tNewton was 'born' on 25 \"December\" 1642 (Old Style) \n\nor 4 January 1643 (New Style).",
341341
"generation_id": "0385c7cf-4247-43a3-a450-b25b547a31e1",
342342
"citations": [
343343
{
@@ -411,7 +411,7 @@ TEST_F(RemoteEngineTest, CohereResponse) {
411411
res_json = json_helper::ParseJsonString(res);
412412
EXPECT_EQ(
413413
res_json["choices"][0]["message"]["content"].asString(),
414-
"Isaac Newton was 'born' on 25 \"December\" 1642 (Old Style) \n\nor 4 "
414+
"Isaac \t\tNewton was 'born' on 25 \"December\" 1642 (Old Style) \n\nor 4 "
415415
"January 1643 (New Style).");
416416
}
417417

engine/utils/string_utils.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <algorithm>
44
#include <cctype>
55
#include <chrono>
6+
#include <iomanip>
67
#include <sstream>
78
#include <string>
89
#include <vector>
@@ -162,4 +163,41 @@ inline std::string FormatTimeElapsed(uint64_t pastTimestamp) {
162163

163164
return oss.str();
164165
}
166+
167+
inline std::string EscapeJson(const std::string& s) {
168+
std::ostringstream o;
169+
for (auto c = s.cbegin(); c != s.cend(); c++) {
170+
switch (*c) {
171+
case '"':
172+
o << "\\\"";
173+
break;
174+
case '\\':
175+
o << "\\\\";
176+
break;
177+
case '\b':
178+
o << "\\b";
179+
break;
180+
case '\f':
181+
o << "\\f";
182+
break;
183+
case '\n':
184+
o << "\\n";
185+
break;
186+
case '\r':
187+
o << "\\r";
188+
break;
189+
case '\t':
190+
o << "\\t";
191+
break;
192+
default:
193+
if ('\x00' <= *c && *c <= '\x1f') {
194+
o << "\\u" << std::hex << std::setw(4) << std::setfill('0')
195+
<< static_cast<int>(*c);
196+
} else {
197+
o << *c;
198+
}
199+
}
200+
}
201+
return o.str();
202+
}
165203
} // namespace string_utils

0 commit comments

Comments
 (0)