diff --git a/LogMonitor/LogMonitorTests/UtilityTests.cpp b/LogMonitor/LogMonitorTests/UtilityTests.cpp index beb9efee..62e17e97 100644 --- a/LogMonitor/LogMonitorTests/UtilityTests.cpp +++ b/LogMonitor/LogMonitorTests/UtilityTests.cpp @@ -82,6 +82,12 @@ namespace UtilityTests expect = L"C:\\\\Drive\\\\XX"; Utility::SanitizeJson(str); Assert::IsTrue(str == expect, L"should escape \\"); + + str = L"{\"foo\":\"bar\",\"json_in_json\":\"{\\\"key_inner\\\":\\\"value_inner\\\"}\"}"; + expect = L"{\\\"foo\\\":\\\"bar\\\",\\\"json_in_json\\\":\\\"{\\\\\\\"key_inner\\\\\\\":\\\\\\\"value_inner\\\\\\\"}\\\"}"; + Utility::SanitizeJson(str); + Assert::IsTrue(str == expect, L"should properly escape json inside json"); + } }; } diff --git a/LogMonitor/src/LogMonitor/JsonFileParser.cpp b/LogMonitor/src/LogMonitor/JsonFileParser.cpp index 54f4cde0..79c59a17 100644 --- a/LogMonitor/src/LogMonitor/JsonFileParser.cpp +++ b/LogMonitor/src/LogMonitor/JsonFileParser.cpp @@ -250,7 +250,6 @@ JsonFileParser::ParseNumber() // // End of string. // - offset++; AdvanceBufferPointer(offset); m_doubleValue = negativeValue ? -parsedValue : parsedValue; diff --git a/LogMonitor/src/LogMonitor/Utility.cpp b/LogMonitor/src/LogMonitor/Utility.cpp index f5b08e34..e1471cab 100644 --- a/LogMonitor/src/LogMonitor/Utility.cpp +++ b/LogMonitor/src/LogMonitor/Utility.cpp @@ -265,43 +265,25 @@ bool Utility::isJsonNumber(_In_ std::wstring& str) /// void Utility::SanitizeJson(_Inout_ std::wstring& str) { - size_t i = 0; - while (i < str.size()) { + size_t num_esc = 0; + for (size_t i = 0; i < str.size(); i++) { auto sub = str.substr(i, 1); - if (sub == L"\"") { - if ((i > 0 && str.substr(i - 1, 1) != L"\\") - || i == 0) - { - str.replace(i, 1, L"\\\""); - i++; - } + if (sub == L"\"" || sub == L"\\" || sub == L"\n" || sub == L"\r") { + num_esc++; } - else if (sub == L"\\") { - if ((i < str.size() - 1 && str.substr(i + 1, 1) != L"\\") - || i == str.size() - 1) - { - str.replace(i, 1, L"\\\\"); - i++; - } - else { - i += 2; - } - } - else if (sub == L"\n") { - if ((i > 0 && str.substr(i - 1, 1) != L"\\") - || i == 0) - { - str.replace(i, 1, L"\\n"); - i++; - } - } - else if (sub == L"\r") { - if ((i > 0 && str.substr(i - 1, 1) != L"\\") - || i == 0) - { - str.replace(i, 1, L"\\r"); - i++; - } + } + + str.reserve(str.size() + num_esc); + for (size_t i = 0; i < str.size(); i++) { + auto sub = str.substr(i, 1); + if (sub == L"\n") { + str.replace(i, 1, L"\\n"); + } else if (sub == L"\r") { + str.replace(i, 1, L"\\r"); + } else if (sub == L"\"" || sub == L"\\") { + str.insert(i, L"\\"); + } else { + continue; } i++; }