Browse Source

Added Json encode support for QVariantList and QStringList

adaptive-webui-19844
Christophe Dumez 15 years ago
parent
commit
8f28804f8c
  1. 5
      src/eventmanager.cpp
  2. 48
      src/json.h

5
src/eventmanager.cpp

@ -231,9 +231,8 @@ QVariantMap EventManager::getGlobalPreferences() const {
data["save_path"] = Preferences::getSavePath(); data["save_path"] = Preferences::getSavePath();
data["temp_path_enabled"] = Preferences::isTempPathEnabled(); data["temp_path_enabled"] = Preferences::isTempPathEnabled();
data["temp_path"] = Preferences::getTempPath(); data["temp_path"] = Preferences::getTempPath();
// XXX: json.h does not encode QStringList correctly data["scan_dirs"] = Preferences::getScanDirs();
//data["scan_dirs"] = Preferences::getScanDirs(); data["download_in_scan_dirs"] = Preferences::getDownloadInScanDirs();
//data["download_in_scan_dirs"] = Preferences::getDownloadInScanDirs();
data["export_dir_enabled"] = Preferences::isTorrentExportEnabled(); data["export_dir_enabled"] = Preferences::isTorrentExportEnabled();
data["export_dir"] = Preferences::getExportDir(); data["export_dir"] = Preferences::getExportDir();
data["preallocate_all"] = Preferences::preAllocateAllFiles(); data["preallocate_all"] = Preferences::preAllocateAllFiles();

48
src/json.h

@ -41,15 +41,23 @@ namespace json {
return "null"; return "null";
switch(v.type()) switch(v.type())
{ {
case QVariant::Bool: case QVariant::Bool:
case QVariant::Double: case QVariant::Double:
case QVariant::Int: case QVariant::Int:
case QVariant::LongLong: case QVariant::LongLong:
case QVariant::UInt: case QVariant::UInt:
case QVariant::ULongLong: case QVariant::ULongLong:
case QMetaType::Float: case QMetaType::Float:
return v.value<QString>(); return v.value<QString>();
case QVariant::String: case QVariant::StringList:
case QVariant::List: {
QStringList strList;
foreach(const QVariant &var, v.toList()) {
strList << toJson(var);
}
return "["+strList.join(",")+"]";
}
case QVariant::String:
{ {
QString s = v.value<QString>(); QString s = v.value<QString>();
QString result = "\""; QString result = "\"";
@ -58,35 +66,35 @@ namespace json {
QChar ch = s[i]; QChar ch = s[i];
switch(ch.toAscii()) switch(ch.toAscii())
{ {
case '\b': case '\b':
result += "\\b"; result += "\\b";
break; break;
case '\f': case '\f':
result += "\\f"; result += "\\f";
break; break;
case '\n': case '\n':
result += "\\n"; result += "\\n";
break; break;
case '\r': case '\r':
result += "\\r"; result += "\\r";
break; break;
case '\t': case '\t':
result += "\\t"; result += "\\t";
break; break;
case '\"': case '\"':
case '\'': case '\'':
case '\\': case '\\':
case '&': case '&':
result += '\\'; result += '\\';
case '\0': case '\0':
default: default:
result += ch; result += ch;
} }
} }
result += "\""; result += "\"";
return result; return result;
} }
default: default:
qDebug("Unknown QVariantType: %d", (int)v.type()); qDebug("Unknown QVariantType: %d", (int)v.type());
return "undefined"; return "undefined";
} }

Loading…
Cancel
Save