- Simplified and optimized json code

This commit is contained in:
Christophe Dumez 2008-12-25 14:33:37 +00:00
parent 0d078f660a
commit b8c79e0b5f
7 changed files with 80 additions and 123 deletions

View file

@ -21,7 +21,6 @@
#include "eventmanager.h"
#include "bittorrent.h"
#include "json.h"
#include <QDebug>
EventManager::EventManager(QObject *parent, bittorrent *BTSession)
@ -29,12 +28,8 @@ EventManager::EventManager(QObject *parent, bittorrent *BTSession)
{
}
QVariant EventManager::getEventList() const {
QVariantList list;
foreach(QVariantMap event, event_list.values()) {
list << QVariant(event);
}
return QVariant(list);
QList<QVariantMap> EventManager::getEventList() const {
return event_list.values();
}
void EventManager::addedTorrent(QTorrentHandle& h)

View file

@ -40,7 +40,7 @@ class EventManager : public QObject
public:
EventManager(QObject *parent, bittorrent* BTSession);
QVariant getEventList() const;
QList<QVariantMap> getEventList() const;
public slots:
void addedTorrent(QTorrentHandle& h);

View file

@ -160,9 +160,8 @@ void HttpConnection::respondNotFound()
void HttpConnection::respondJson()
{
EventManager* manager = parent->eventManager();
QVariant data = manager->getEventList();
QString string = toJson(data);
EventManager* manager = parent->eventManager();
QString string = json::toJson(manager->getEventList());
generator.setStatusLine(200, "OK");
generator.setContentTypeByExt("js");
generator.setMessage(string);

View file

@ -25,11 +25,11 @@
#include "bittorrent.h"
#include <QTimer>
HttpServer::HttpServer(bittorrent *BTSession, int msec, QObject* parent) : QTcpServer(parent)
HttpServer::HttpServer(bittorrent *_BTSession, int msec, QObject* parent) : QTcpServer(parent)
{
base64 = QByteArray(":").toBase64();
connect(this, SIGNAL(newConnection()), this, SLOT(newHttpConnection()));
HttpServer::BTSession = BTSession;
BTSession = _BTSession;
manager = new EventManager(this, BTSession);
//add torrents
QStringList list = BTSession->getUnfinishedTorrents();
@ -37,11 +37,11 @@ HttpServer::HttpServer(bittorrent *BTSession, int msec, QObject* parent) : QTcpS
QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(h.is_valid()) manager->addedTorrent(h);
}
list = BTSession->getFinishedTorrents();
list = BTSession->getFinishedTorrents();
foreach(QString hash, list) {
QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(h.is_valid()) manager->addedTorrent(h);
}
QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(h.is_valid()) manager->addedTorrent(h);
}
//connect BTSession to manager
connect(BTSession, SIGNAL(addedTorrent(QTorrentHandle&)), manager, SLOT(addedTorrent(QTorrentHandle&)));
connect(BTSession, SIGNAL(deletedTorrent(QString)), manager, SLOT(deletedTorrent(QString)));
@ -99,7 +99,7 @@ bool HttpServer::isAuthorized(QByteArray auth) const
return (auth == base64);
}
EventManager *HttpServer::eventManager() const
EventManager* HttpServer::eventManager() const
{
return manager;
}

View file

@ -1,103 +0,0 @@
/*
* Copyright (C) 2007 by Ishan Arora
* ishanarora@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "json.h"
QString toJson(QVariant v)
{
if (v.isNull())
return "null";
QString result;
switch(v.type())
{
case QVariant::Bool:
case QVariant::Double:
case QVariant::Int:
case QVariant::LongLong:
case QVariant::UInt:
case QVariant::ULongLong:
return v.value<QString>();
case QVariant::String:
{
QString s = v.value<QString>();
result = "\"";
for(int i=0; i<s.size(); i++)
{
QChar ch = s[i];
switch(ch.toAscii())
{
case '\b':
result += "\\b";
break;
case '\f':
result += "\\f";
break;
case '\n':
result += "\\n";
break;
case '\r':
result += "\\r";
break;
case '\t':
result += "\\t";
break;
case '\"':
case '\'':
case '\\':
case '&':
result += '\\';
case '\0':
default:
result += ch;
}
}
result += "\"";
return result;
}
case QVariant::List:
{
result = "[";
QListIterator<QVariant> it(v.value<QVariantList>());
while (it.hasNext())
result += toJson(it.next()) + ",";
if(result.size() > 1)
result.chop(1);
result += "]";
return result;
}
case QVariant::Map:
{
result = "{";
QMapIterator<QString, QVariant> it(v.value<QVariantMap>());
while (it.hasNext()) {
it.next();
if(it.value().isValid())
result += toJson(QVariant(it.key())) + ":" + toJson(it.value()) + ",";
}
if(result.size() > 1)
result.chop(1);
result += "}";
return result;
}
default:
return "undefined";
}
}

View file

@ -24,6 +24,73 @@
#include <QVariant>
QString toJson(QVariant v);
namespace json {
QString toJson(QVariant v) {
if (v.isNull())
return "null";
switch(v.type())
{
case QVariant::Bool:
case QVariant::Double:
case QVariant::Int:
case QVariant::LongLong:
case QVariant::UInt:
case QVariant::ULongLong:
return v.value<QString>();
case QVariant::String:
{
QString s = v.value<QString>();
QString result = "\"";
for(int i=0; i<s.size(); i++)
{
QChar ch = s[i];
switch(ch.toAscii())
{
case '\b':
result += "\\b";
break;
case '\f':
result += "\\f";
break;
case '\n':
result += "\\n";
break;
case '\r':
result += "\\r";
break;
case '\t':
result += "\\t";
break;
case '\"':
case '\'':
case '\\':
case '&':
result += '\\';
case '\0':
default:
result += ch;
}
}
result += "\"";
return result;
}
default:
return "undefined";
}
}
QString toJson(QList<QVariantMap> v) {
QStringList res;
foreach(QVariantMap m, v) {
QStringList vlist;
foreach(QString key, m.keys()) {
vlist << toJson(key)+":"+toJson(m[key]);
}
res << "{"+vlist.join(",")+"}";
}
return "["+res.join(",")+"]";
}
}
#endif

View file

@ -224,7 +224,6 @@ SOURCES += GUI.cpp \
httpconnection.cpp \
httprequestparser.cpp \
httpresponsegenerator.cpp \
json.cpp \
eventmanager.cpp \
SearchTab.cpp \
ico.cpp