diff --git a/L2BotDll/Serializers/JsonSerializer.h b/L2BotDll/Serializers/JsonSerializer.h index f9ecc1a..08d81ac 100644 --- a/L2BotDll/Serializers/JsonSerializer.h +++ b/L2BotDll/Serializers/JsonSerializer.h @@ -24,7 +24,7 @@ public: } else { - result += L"\"" + it->value + L"\""; + result += L"\"" + EscapeJson(it->value) + L"\""; } if (std::next(it) != nodes.end()) { @@ -36,4 +36,30 @@ public: return result; } + +private: + const std::wstring EscapeJson(const std::wstring& input) const + { + std::wstringstream o; + for (auto c = input.cbegin(); c != input.cend(); c++) { + switch (*c) { + case '"': o << "\\\""; break; + case '\\': o << "\\\\"; break; + case '\b': o << "\\b"; break; + case '\f': o << "\\f"; break; + case '\n': o << "\\n"; break; + case '\r': o << "\\r"; break; + case '\t': o << "\\t"; break; + default: + if ('\x00' <= *c && *c <= '\x1f') { + o << "\\u" + << std::hex << std::setw(4) << std::setfill(L'0') << static_cast(*c); + } + else { + o << *c; + } + } + } + return o.str(); + } }; \ No newline at end of file