Permalink
Browse files

Escape control characters in JSON strings

  • Loading branch information...
1 parent ec257f7 commit 9596c5d614eb58b507514c645da5d7017d20e36d @mhoran mhoran committed Dec 30, 2012
Showing with 8 additions and 11 deletions.
  1. +8 −11 src/JsonSerializer.cpp
View
@@ -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 {
@@ -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.