Skip to content

Commit d93bc39

Browse files
committed
Fixing up backslash detection in json escaping and adding more tests.
1 parent 4e91147 commit d93bc39

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Release/src/json/json.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,8 @@ bool web::json::number::is_int64() const
340340

341341
bool web::json::details::_String::has_escape_chars(const _String &str)
342342
{
343-
static const std::array<::utility::string_t::value_type, 33> escapes =
344-
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 0x00 - 0x1F */ '\"' };
343+
static const std::array<::utility::string_t::value_type, 34> escapes =
344+
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 0x00 - 0x1F */ '\"', '\\' };
345345

346346
// Provide the range otherwise find_first_of will think the first null terminator character is the end.
347347
return str.m_string.find_first_of(escapes.data(), 0, escapes.size()) != utility::string_t::npos;

Release/tests/functional/json/parsing_tests.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,15 @@ TEST(escaped_unicode_string)
225225

226226
TEST(escaping_control_characters)
227227
{
228+
std::vector<int> chars;
228229
for (int i = 0; i <= 0x1F; ++i)
230+
{
231+
chars.push_back(i);
232+
}
233+
chars.push_back(0x5C); // backslash '\'
234+
chars.push_back(0x22); // quotation '"'
235+
236+
for (int i : chars)
229237
{
230238
::utility::stringstream_t ss;
231239
ss << U("\"\\u") << std::uppercase << std::setfill(U('0')) << std::setw(4) << std::hex << i << U("\"");
@@ -251,6 +259,14 @@ TEST(escaping_control_characters)
251259
{
252260
expectedStr = U("\"\\r\"");
253261
}
262+
else if (i == 0x5C)
263+
{
264+
expectedStr = U("\"\\\\\"");
265+
}
266+
else if (i == 0x22)
267+
{
268+
expectedStr = U("\"\\\"\"");
269+
}
254270

255271
// Try constructing a json string value directly.
256272
::utility::string_t schar;

0 commit comments

Comments
 (0)