1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

[Json] Removed dependency on QJson

QtScript is now used instead of QJson. QtScript is available on both
Qt 4 & Qt 5 and also as side benefit, is TWICE as fast as QJson!
This commit is contained in:
nowrep 2014-02-10 23:33:12 +01:00
parent 4e8c0adbad
commit 35fb5cecad
6 changed files with 159 additions and 68 deletions

View File

@ -12,7 +12,7 @@ before_install:
install:
- sudo apt-get -qq update
- sudo apt-get -qq install libssl-dev pkg-config libhunspell-dev
- if [[ "$QT" == "qt4" ]]; then sudo apt-get -qq install libqt4-dev libqt4-webkit libqt4-sql-sqlite libqjson-dev; fi
- if [[ "$QT" == "qt4" ]]; then sudo apt-get -qq install libqt4-dev libqt4-webkit libqt4-sql-sqlite; fi
- if [[ "$QT" == "qt5" ]]; then sudo apt-add-repository -y ppa:beineri/opt-qt511; sudo apt-get update -qq; sudo apt-get install -qq qt51base qt51webkit qt51tools qt51script libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev; fi
script:

View File

@ -58,14 +58,6 @@ else {
}
}
# QJson dependency
!mac:unix:isEqual(QT_MAJOR_VERSION, 4) {
!system(test -d /usr/include/qjson): error(ERROR: QJson cannot be found!)
# Ugly hack to check for Indent support in QJson
system(grep setIndentMode /usr/include/qjson/serializer.h > /dev/null): DEFINES *= QJSON_HAVE_INDENT
}
DEFINES *= QT_NO_URL_CAST_FROM_STRING
DEFINES *= QT_USE_QSTRINGBUILDER

View File

@ -280,10 +280,10 @@ void Bookmarks::loadBookmarks()
QByteArray data = file.readAll();
file.close();
bool ok;
const QVariant res = Json::parse(data, &ok);
Json json;
const QVariant res = json.parse(QString::fromUtf8(data));
if (!ok || res.type() != QVariant::Map) {
if (!json.ok() || res.type() != QVariant::Map) {
qWarning() << "Bookmarks::init() Error parsing bookmarks! Using default bookmarks!";
qWarning() << "Bookmarks::init() Your bookmarks have been backed up in" << backupFile;
@ -292,9 +292,9 @@ void Bookmarks::loadBookmarks()
QFile::copy(bookmarksFile, backupFile);
// Load default bookmarks
const QVariant data = Json::parse(QzTools::readAllFileByteContents(":data/bookmarks.json"), &ok);
const QVariant data = json.parse(QzTools::readAllFileContents(":data/bookmarks.json"));
Q_ASSERT(ok);
Q_ASSERT(json.ok());
Q_ASSERT(data.type() == QVariant::Map);
loadBookmarksFromMap(data.toMap().value("roots").toMap());
@ -327,10 +327,10 @@ void Bookmarks::saveBookmarks()
map.insert("version", Qz::bookmarksVersion);
map.insert("roots", bookmarksMap);
bool ok;
const QByteArray data = Json::serialize(map, &ok);
Json json;
const QString data = json.serialize(map);
if (!ok || data.isEmpty()) {
if (!json.ok() || data.isEmpty()) {
qWarning() << "Bookmarks::saveBookmarks() Error serializing bookmarks!";
return;
}
@ -341,7 +341,7 @@ void Bookmarks::saveBookmarks()
qWarning() << "Bookmarks::saveBookmarks() Error opening bookmarks file for writing!";
}
file.write(data);
file.write(data.toUtf8());
file.close();
}
@ -410,7 +410,7 @@ QVariantList Bookmarks::writeBookmarks(BookmarkItem* parent)
switch (child->type()) {
case BookmarkItem::Url:
map.insert("url", child->url().toEncoded());
map.insert("url", child->urlString());
map.insert("name", child->title());
map.insert("description", child->description());
map.insert("keyword", child->keyword());

View File

@ -514,10 +514,6 @@ isEqual(QT_MAJOR_VERSION, 5) {
SOURCES += tools/qzregexp.cpp
}
!isEqual(QT_MAJOR_VERSION, 5) {
LIBS += -lqjson
}
!mac:unix {
target.path = $$library_folder

View File

@ -17,51 +17,138 @@
* ============================================================ */
#include "json.h"
#if QT_VERSION < 0x050000
#include <qjson/parser.h>
#include <qjson/serializer.h>
#else
#include <QJsonDocument>
#endif
#include <QScriptEngine>
#include <QScriptValueIterator>
QVariant Json::parse(const QByteArray &data, bool* ok)
// Class based on http://stackoverflow.com/a/15805783
Json::Json()
: m_engine(0)
, m_ok(true)
{
#if QT_VERSION < 0x050000
QJson::Parser parser;
return parser.parse(data, ok);
#else
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
if (ok) {
*ok = error.error == QJsonParseError::NoError;
}
return doc.toVariant();
#endif
}
QByteArray Json::serialize(const QVariant &variant, bool* ok)
Json::~Json()
{
#if QT_VERSION < 0x050000
QJson::Serializer serializer;
#ifdef QJSON_HAVE_INDENT
serializer.setIndentMode(QJson::IndentFull);
#endif
const QByteArray data = serializer.serialize(variant);
if (ok) {
*ok = !data.isNull();
}
return data;
#else
QJsonDocument doc = QJsonDocument::fromVariant(variant);
if (ok) {
*ok = !doc.isNull();
}
return doc.toJson(QJsonDocument::Indented);
#endif
delete m_engine;
}
QVariant Json::parse(const QString &data)
{
delete m_engine;
m_engine = new QScriptEngine();
QString jsonData = QString("(%1)").arg(data);
QScriptValue obj = m_engine->evaluate(jsonData);
m_ok = !obj.isError() && obj.isObject();
return decodeInner(obj);
}
QString Json::serialize(const QVariant &variant)
{
delete m_engine;
m_engine = new QScriptEngine();
m_engine->evaluate("function toString() { return JSON.stringify(this, null, ' ') }");
QScriptValue toString = m_engine->globalObject().property("toString");
QScriptValue obj = encodeInner(variant.toMap());
QScriptValue result = toString.call(obj);
m_ok = !obj.isError() && obj.isObject();
return result.toString();
}
bool Json::ok() const
{
return m_ok;
}
QMap<QString, QVariant> Json::decodeInner(QScriptValue object)
{
QMap<QString, QVariant> map;
QScriptValueIterator it(object);
while (it.hasNext()) {
it.next();
if (it.value().isArray()) {
map.insert(it.name(), QVariant(decodeInnerToList(it.value())));
}
else if (it.value().isNumber()) {
map.insert(it.name(), QVariant(it.value().toNumber()));
}
else if (it.value().isString()) {
map.insert(it.name(), QVariant(it.value().toString()));
}
else if (it.value().isNull()) {
map.insert(it.name(), QVariant());
}
else if (it.value().isObject()) {
map.insert(it.name(), QVariant(decodeInner(it.value())));
}
}
return map;
}
QList<QVariant> Json::decodeInnerToList(QScriptValue arrayValue)
{
QList<QVariant> list;
QScriptValueIterator it(arrayValue);
while (it.hasNext()) {
it.next();
if (it.name() == QLatin1String("length")) {
continue;
}
if (it.value().isArray()) {
list.append(QVariant(decodeInnerToList(it.value())));
}
else if (it.value().isNumber()) {
list.append(QVariant(it.value().toNumber()));
}
else if (it.value().isString()) {
list.append(QVariant(it.value().toString()));
}
else if (it.value().isNull()) {
list.append(QVariant());
}
else if (it.value().isObject()) {
list.append(QVariant(decodeInner(it.value())));
}
}
return list;
}
QScriptValue Json::encodeInner(const QMap<QString, QVariant> &map)
{
QScriptValue obj = m_engine->newObject();
QMapIterator<QString, QVariant> i(map);
while (i.hasNext()) {
i.next();
if (i.value().type() == QVariant::String) {
obj.setProperty(i.key(), i.value().toString());
}
else if (i.value().type() == QVariant::Int) {
obj.setProperty(i.key(), i.value().toInt());
}
else if (i.value().type() == QVariant::Double) {
obj.setProperty(i.key(), i.value().toDouble());
}
else if (i.value().type() == QVariant::List) {
obj.setProperty(i.key(), qScriptValueFromSequence(m_engine, i.value().toList()));
}
else if (i.value().type() == QVariant::Map) {
obj.setProperty(i.key(), encodeInner(i.value().toMap()));
}
}
return obj;
}

View File

@ -19,15 +19,31 @@
#define JSON_H
#include <QVariant>
#include <QByteArray>
#include <QString>
#include "qz_namespace.h"
class QScriptValue;
class QScriptEngine;
class QT_QUPZILLA_EXPORT Json
{
public:
static QVariant parse(const QByteArray &data, bool* ok);
static QByteArray serialize(const QVariant &variant, bool* ok);
explicit Json();
~Json();
QVariant parse(const QString &data);
QString serialize(const QVariant &variant);
bool ok() const;
private:
QMap<QString, QVariant> decodeInner(QScriptValue object);
QList<QVariant> decodeInnerToList(QScriptValue arrayValue);
QScriptValue encodeInner(const QMap<QString, QVariant> &map);
QScriptEngine* m_engine;
bool m_ok;
};
#endif // JSON_H