From e12ab41b40a32c46a19bce2a580f5cb7436e35c7 Mon Sep 17 00:00:00 2001 From: Yuji Hirose Date: Fri, 8 Feb 2019 15:19:40 -0500 Subject: [PATCH] Fixed problem with JSON invalid characters --- lint/server.cc | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lint/server.cc b/lint/server.cc index 65cbe95..9aa2444 100644 --- a/lint/server.cc +++ b/lint/server.cc @@ -2,6 +2,8 @@ #include "peglib.h" #include #include +#include +#include using namespace httplib; using namespace std; @@ -238,6 +240,20 @@ body { )"; +// https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c/33799784#33799784 +std::string escape_json(const std::string &s) { + std::ostringstream o; + for (auto c : s) { + if (c == '"' || c == '\\' || ('\x00' <= c && c <= '\x1f')) { + o << "\\u" + << std::hex << std::setw(4) << std::setfill('0') << (int)c; + } else { + o << c; + } + } + return o.str(); +} + function makeJSONFormatter(string& json) { auto init = make_shared(true); @@ -249,7 +265,7 @@ function makeJSONFormatter(string& json) json += "{"; json += R"("ln":)" + to_string(ln) + ","; json += R"("col":)" + to_string(col) + ","; - json += R"("msg":")" + msg + R"(")"; + json += R"("msg":")" + escape_json(msg) + R"(")"; json += "}"; *init = false; }; @@ -321,13 +337,8 @@ int run_server(int port, const vector& syntax, const vector& source) const auto& codeText = req.get_param_value("code"); shared_ptr ast; if (parse_code(codeText, peg, codeResult, ast)) { - astResult = peg::ast_to_s(ast); - astResult = replace_all(astResult, "\n", "\\n"); - astResult = replace_all(astResult, "\"", "%22"); - - astResultOptimized = peg::ast_to_s(peg::AstOptimizer(true).optimize(ast)); - astResultOptimized = replace_all(astResultOptimized, "\n", "\\n"); - astResultOptimized = replace_all(astResultOptimized, "\"", "%22"); + astResult = escape_json(peg::ast_to_s(ast)); + astResultOptimized = escape_json(peg::ast_to_s(peg::AstOptimizer(true).optimize(ast))); } }