Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
Escape control characters in JSON strings
Browse files Browse the repository at this point in the history
  • Loading branch information
mhoran committed Jan 3, 2013
1 parent ec257f7 commit 9596c5d
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/JsonSerializer.cpp
Expand Up @@ -84,14 +84,19 @@ void JsonSerializer::addMap(const QVariantMap &map) {

QString JsonSerializer::sanitizeString(QString str) {
str.replace("\\", "\\\\");
str.replace("\"", "\\\"");
str.replace("\b", "\\b");
str.replace("\f", "\\f");
str.replace("\n", "\\n");
str.replace("\r", "\\r");
str.replace("\t", "\\t");

// escape unicode chars
QString result;
const ushort* unicode = str.utf16();
unsigned int i = 0;

while (unicode[i]) {
if (unicode[i] < 128) {
if (unicode[i] > 31 && unicode[i] < 128) {
result.append(unicode[i]);
}
else {
Expand All @@ -101,15 +106,7 @@ QString JsonSerializer::sanitizeString(QString str) {
}
++i;
}
str = result;

str.replace("\"", "\\\"");
str.replace("\b", "\\b");
str.replace("\f", "\\f");
str.replace("\n", "\\n");
str.replace("\r", "\\r");
str.replace("\t", "\\t");

return str;
return result;
}

0 comments on commit 9596c5d

Please sign in to comment.