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

48
src/json.h

@ -41,15 +41,23 @@ namespace json { @@ -41,15 +41,23 @@ namespace json {
return "null";
switch(v.type())
{
case QVariant::Bool:
case QVariant::Double:
case QVariant::Int:
case QVariant::LongLong:
case QVariant::UInt:
case QVariant::ULongLong:
case QMetaType::Float:
case QVariant::Bool:
case QVariant::Double:
case QVariant::Int:
case QVariant::LongLong:
case QVariant::UInt:
case QVariant::ULongLong:
case QMetaType::Float:
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 result = "\"";
@ -58,35 +66,35 @@ namespace json { @@ -58,35 +66,35 @@ namespace json {
QChar ch = s[i];
switch(ch.toAscii())
{
case '\b':
case '\b':
result += "\\b";
break;
case '\f':
case '\f':
result += "\\f";
break;
case '\n':
case '\n':
result += "\\n";
break;
case '\r':
case '\r':
result += "\\r";
break;
case '\t':
case '\t':
result += "\\t";
break;
case '\"':
case '\'':
case '\\':
case '&':
case '\"':
case '\'':
case '\\':
case '&':
result += '\\';
case '\0':
default:
case '\0':
default:
result += ch;
}
}
result += "\"";
return result;
}
default:
default:
qDebug("Unknown QVariantType: %d", (int)v.type());
return "undefined";
}

Loading…
Cancel
Save