From 1e5deb06416b6efc33a2009d9678fd8f743c5ce7 Mon Sep 17 00:00:00 2001 From: Michal Klocek Date: Thu, 2 May 2019 18:02:45 +0200 Subject: Add 'well-formated' JSON string values Add support for surrogate code points U+D800 through U+DFFF, represent them with JSON escape sequences. https://github.com/tc39/proposal-well-formed-stringify Change-Id: I84fea53a8ef400beebefdba10ea82dc510fe7dda Reviewed-by: Thiago Macieira --- src/corelib/serialization/qjsonwriter.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/corelib/serialization/qjsonwriter.cpp') diff --git a/src/corelib/serialization/qjsonwriter.cpp b/src/corelib/serialization/qjsonwriter.cpp index 12ce20ef09..5b246837d2 100644 --- a/src/corelib/serialization/qjsonwriter.cpp +++ b/src/corelib/serialization/qjsonwriter.cpp @@ -58,7 +58,6 @@ static inline uchar hexdig(uint u) static QByteArray escapedString(const QString &s) { - const uchar replacement = '?'; QByteArray ba(s.length(), Qt::Uninitialized); uchar *cursor = reinterpret_cast(const_cast(ba.constData())); @@ -111,9 +110,14 @@ static QByteArray escapedString(const QString &s) } else { *cursor++ = (uchar)u; } - } else { - if (QUtf8Functions::toUtf8(u, cursor, src, end) < 0) - *cursor++ = replacement; + } else if (QUtf8Functions::toUtf8(u, cursor, src, end) < 0) { + // failed to get valid utf8 use JSON escape sequence + *cursor++ = '\\'; + *cursor++ = 'u'; + *cursor++ = hexdig(u>>12 & 0x0f); + *cursor++ = hexdig(u>>8 & 0x0f); + *cursor++ = hexdig(u>>4 & 0x0f); + *cursor++ = hexdig(u & 0x0f); } } -- cgit v1.2.3