mirror of
https://invent.kde.org/network/falkon.git
synced 2024-11-11 09:32:12 +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:
parent
4e8c0adbad
commit
35fb5cecad
|
@ -12,7 +12,7 @@ before_install:
|
||||||
install:
|
install:
|
||||||
- sudo apt-get -qq update
|
- sudo apt-get -qq update
|
||||||
- sudo apt-get -qq install libssl-dev pkg-config libhunspell-dev
|
- 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
|
- 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:
|
script:
|
||||||
|
|
|
@ -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_NO_URL_CAST_FROM_STRING
|
||||||
DEFINES *= QT_USE_QSTRINGBUILDER
|
DEFINES *= QT_USE_QSTRINGBUILDER
|
||||||
|
|
||||||
|
|
|
@ -280,10 +280,10 @@ void Bookmarks::loadBookmarks()
|
||||||
QByteArray data = file.readAll();
|
QByteArray data = file.readAll();
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
bool ok;
|
Json json;
|
||||||
const QVariant res = Json::parse(data, &ok);
|
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() Error parsing bookmarks! Using default bookmarks!";
|
||||||
qWarning() << "Bookmarks::init() Your bookmarks have been backed up in" << backupFile;
|
qWarning() << "Bookmarks::init() Your bookmarks have been backed up in" << backupFile;
|
||||||
|
|
||||||
|
@ -292,9 +292,9 @@ void Bookmarks::loadBookmarks()
|
||||||
QFile::copy(bookmarksFile, backupFile);
|
QFile::copy(bookmarksFile, backupFile);
|
||||||
|
|
||||||
// Load default bookmarks
|
// 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);
|
Q_ASSERT(data.type() == QVariant::Map);
|
||||||
|
|
||||||
loadBookmarksFromMap(data.toMap().value("roots").toMap());
|
loadBookmarksFromMap(data.toMap().value("roots").toMap());
|
||||||
|
@ -327,10 +327,10 @@ void Bookmarks::saveBookmarks()
|
||||||
map.insert("version", Qz::bookmarksVersion);
|
map.insert("version", Qz::bookmarksVersion);
|
||||||
map.insert("roots", bookmarksMap);
|
map.insert("roots", bookmarksMap);
|
||||||
|
|
||||||
bool ok;
|
Json json;
|
||||||
const QByteArray data = Json::serialize(map, &ok);
|
const QString data = json.serialize(map);
|
||||||
|
|
||||||
if (!ok || data.isEmpty()) {
|
if (!json.ok() || data.isEmpty()) {
|
||||||
qWarning() << "Bookmarks::saveBookmarks() Error serializing bookmarks!";
|
qWarning() << "Bookmarks::saveBookmarks() Error serializing bookmarks!";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -341,7 +341,7 @@ void Bookmarks::saveBookmarks()
|
||||||
qWarning() << "Bookmarks::saveBookmarks() Error opening bookmarks file for writing!";
|
qWarning() << "Bookmarks::saveBookmarks() Error opening bookmarks file for writing!";
|
||||||
}
|
}
|
||||||
|
|
||||||
file.write(data);
|
file.write(data.toUtf8());
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -410,7 +410,7 @@ QVariantList Bookmarks::writeBookmarks(BookmarkItem* parent)
|
||||||
|
|
||||||
switch (child->type()) {
|
switch (child->type()) {
|
||||||
case BookmarkItem::Url:
|
case BookmarkItem::Url:
|
||||||
map.insert("url", child->url().toEncoded());
|
map.insert("url", child->urlString());
|
||||||
map.insert("name", child->title());
|
map.insert("name", child->title());
|
||||||
map.insert("description", child->description());
|
map.insert("description", child->description());
|
||||||
map.insert("keyword", child->keyword());
|
map.insert("keyword", child->keyword());
|
||||||
|
|
|
@ -514,10 +514,6 @@ isEqual(QT_MAJOR_VERSION, 5) {
|
||||||
SOURCES += tools/qzregexp.cpp
|
SOURCES += tools/qzregexp.cpp
|
||||||
}
|
}
|
||||||
|
|
||||||
!isEqual(QT_MAJOR_VERSION, 5) {
|
|
||||||
LIBS += -lqjson
|
|
||||||
}
|
|
||||||
|
|
||||||
!mac:unix {
|
!mac:unix {
|
||||||
target.path = $$library_folder
|
target.path = $$library_folder
|
||||||
|
|
||||||
|
|
|
@ -17,51 +17,138 @@
|
||||||
* ============================================================ */
|
* ============================================================ */
|
||||||
#include "json.h"
|
#include "json.h"
|
||||||
|
|
||||||
#if QT_VERSION < 0x050000
|
#include <QScriptEngine>
|
||||||
#include <qjson/parser.h>
|
#include <QScriptValueIterator>
|
||||||
#include <qjson/serializer.h>
|
|
||||||
#else
|
|
||||||
#include <QJsonDocument>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
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
|
delete m_engine;
|
||||||
QJson::Serializer serializer;
|
}
|
||||||
#ifdef QJSON_HAVE_INDENT
|
|
||||||
serializer.setIndentMode(QJson::IndentFull);
|
QVariant Json::parse(const QString &data)
|
||||||
#endif
|
{
|
||||||
const QByteArray data = serializer.serialize(variant);
|
delete m_engine;
|
||||||
|
m_engine = new QScriptEngine();
|
||||||
if (ok) {
|
|
||||||
*ok = !data.isNull();
|
QString jsonData = QString("(%1)").arg(data);
|
||||||
}
|
QScriptValue obj = m_engine->evaluate(jsonData);
|
||||||
|
m_ok = !obj.isError() && obj.isObject();
|
||||||
return data;
|
|
||||||
#else
|
return decodeInner(obj);
|
||||||
QJsonDocument doc = QJsonDocument::fromVariant(variant);
|
}
|
||||||
|
|
||||||
if (ok) {
|
QString Json::serialize(const QVariant &variant)
|
||||||
*ok = !doc.isNull();
|
{
|
||||||
}
|
delete m_engine;
|
||||||
|
m_engine = new QScriptEngine();
|
||||||
return doc.toJson(QJsonDocument::Indented);
|
|
||||||
#endif
|
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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,15 +19,31 @@
|
||||||
#define JSON_H
|
#define JSON_H
|
||||||
|
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
#include <QByteArray>
|
#include <QString>
|
||||||
|
|
||||||
#include "qz_namespace.h"
|
#include "qz_namespace.h"
|
||||||
|
|
||||||
|
class QScriptValue;
|
||||||
|
class QScriptEngine;
|
||||||
|
|
||||||
class QT_QUPZILLA_EXPORT Json
|
class QT_QUPZILLA_EXPORT Json
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static QVariant parse(const QByteArray &data, bool* ok);
|
explicit Json();
|
||||||
static QByteArray serialize(const QVariant &variant, bool* ok);
|
~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
|
#endif // JSON_H
|
||||||
|
|
Loading…
Reference in New Issue
Block a user