diff --git a/src/json.hpp b/src/json.hpp index aa262ae3f..3e8c873f2 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -4696,11 +4696,19 @@ class basic_json { if (c >= 0x00 and c <= 0x1f) { + // convert a number 0..15 to its hex representation (0..f) + auto hexify = [](const char v) -> char + { + return (v < 10) ? ('0' + v) : ('a' + v - 10); + }; + // print character c as \uxxxx - sprintf(&result[pos + 1], "u%04x", int(c)); - pos += 6; - // overwrite trailing null character - result[pos] = '\\'; + for(const char m : { 'u', '0', '0', hexify(c >> 4), hexify(c & 0x0f) }) + { + result[++pos] = m; + } + + ++pos; } else { diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 88391d61d..a69991498 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -4696,11 +4696,19 @@ class basic_json { if (c >= 0x00 and c <= 0x1f) { + // convert a number 0..15 to its hex representation (0..f) + auto hexify = [](const char v) -> char + { + return (v < 10) ? ('0' + v) : ('a' + v - 10); + }; + // print character c as \uxxxx - sprintf(&result[pos + 1], "u%04x", int(c)); - pos += 6; - // overwrite trailing null character - result[pos] = '\\'; + for(const char m : { 'u', '0', '0', hexify(c >> 4), hexify(c & 0x0f) }) + { + result[++pos] = m; + } + + ++pos; } else {