rdr2view/qjson4/QJsonDocument.cpp

425 lines
14 KiB
C++
Raw Permalink Normal View History

2019-11-10 00:12:44 +01:00
/*****************************************************************************
* gta5view Grand Theft Auto V Profile Viewer
* Copyright (C) 2016 Syping
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include "QJsonDocument.h"
#include "QJsonObject.h"
#include "QJsonArray.h"
#include "QJsonParser.h"
#include <QtCore/QStringList>
#include <QtCore/QByteArray>
#include <QtCore/QTextStream>
#include <QtCore/QTextCodec>
#include <QtCore/QtCore>
#if QT_VERSION < 0x050000
//------------------------------------------------------------------------------
// Name: QJsonDocument
//------------------------------------------------------------------------------
QJsonDocument::QJsonDocument() : root_(0) {
}
//------------------------------------------------------------------------------
// Name: QJsonDocument
//------------------------------------------------------------------------------
QJsonDocument::QJsonDocument(const QJsonObject &object) : root_(0) {
2020-08-25 20:47:50 +02:00
setObject(object);
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: QJsonDocument
//------------------------------------------------------------------------------
QJsonDocument::QJsonDocument(const QJsonArray &array) : root_(0) {
2020-08-25 20:47:50 +02:00
setArray(array);
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: QJsonDocument
//------------------------------------------------------------------------------
QJsonDocument::QJsonDocument(const QJsonDocument &other) : root_(0) {
2020-08-25 20:47:50 +02:00
if(other.root_) {
root_ = other.root_->clone();
}
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: ~QJsonDocument
//------------------------------------------------------------------------------
QJsonDocument::~QJsonDocument() {
2020-08-25 20:47:50 +02:00
delete root_;
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: operator=
//------------------------------------------------------------------------------
QJsonDocument &QJsonDocument::operator=(const QJsonDocument &other) {
2020-08-25 20:47:50 +02:00
QJsonDocument(other).swap(*this);
return *this;
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: operator!=
//------------------------------------------------------------------------------
bool QJsonDocument::operator!=(const QJsonDocument &other) const {
2020-08-25 20:47:50 +02:00
return !(*this == other);
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: operator==
//------------------------------------------------------------------------------
bool QJsonDocument::operator==(const QJsonDocument &other) const {
2020-08-25 20:47:50 +02:00
if(isArray() && other.isArray()) {
return array() == other.array();
}
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
if(isObject() && other.isObject()) {
return object() == other.object();
}
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
if(isEmpty() && other.isEmpty()) {
return true;
}
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
if(isNull() && other.isNull()) {
return true;
}
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
return false;
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: isArray
//------------------------------------------------------------------------------
bool QJsonDocument::isArray() const {
2020-08-25 20:47:50 +02:00
return root_ && root_->toArray();
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: isEmpty
//------------------------------------------------------------------------------
bool QJsonDocument::isEmpty() const {
2020-08-25 20:47:50 +02:00
// TODO(eteran): figure out the rules here that Qt5 uses
// it *looks* like they define empty as being NULL
// which is obviously different than this
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
return !root_;
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: isNull
//------------------------------------------------------------------------------
bool QJsonDocument::isNull() const {
2020-08-25 20:47:50 +02:00
return !root_;
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: isObject
//------------------------------------------------------------------------------
bool QJsonDocument::isObject() const {
2020-08-25 20:47:50 +02:00
return root_ && root_->toObject();
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: setArray
//------------------------------------------------------------------------------
void QJsonDocument::setArray(const QJsonArray &array) {
2020-08-25 20:47:50 +02:00
setRoot(array);
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: setObject
//------------------------------------------------------------------------------
void QJsonDocument::setObject(const QJsonObject &object) {
2020-08-25 20:47:50 +02:00
setRoot(object);
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: setRoot
//------------------------------------------------------------------------------
void QJsonDocument::setRoot(const QJsonRoot &root) {
2020-08-25 20:47:50 +02:00
delete root_;
root_ = root.clone();
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: toBinaryData
//------------------------------------------------------------------------------
QByteArray QJsonDocument::toBinaryData() const {
2020-08-25 20:47:50 +02:00
QByteArray r;
// TODO(eteran): implement this
return r;
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: escapeString
//------------------------------------------------------------------------------
QString QJsonDocument::escapeString(const QString &s) const {
2020-08-25 20:47:50 +02:00
QString r;
Q_FOREACH(QChar ch, s) {
switch(ch.toLatin1()) {
case '\"': r.append("\\\""); break;
case '\\': r.append("\\\\"); break;
#if 0
case '/': r.append("\\/"); break;
#endif
case '\b': r.append("\\b"); break;
case '\f': r.append("\\f"); break;
case '\n': r.append("\\n"); break;
case '\r': r.append("\\r"); break;
case '\t': r.append("\\t"); break;
default:
r += ch;
break;
}
}
return r;
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: toJson
//------------------------------------------------------------------------------
2020-08-25 20:47:50 +02:00
QString QJsonDocument::toJson(const QJsonValue &v, JsonFormat format, int indent) const {
QString b;
QTextStream ss(&b, QIODevice::WriteOnly | QIODevice::Text);
bool compact = (format == JsonFormat::Compact);
switch(v.type()) {
case QJsonValue::Null:
ss << "null";
break;
case QJsonValue::Bool:
ss << (v.toBool() ? "true" : "false");
break;
case QJsonValue::Double:
{
double d = v.toDouble ();
if (qIsFinite(d)) {
// +2 to format to ensure the expected precision
ss << QByteArray::number(d, 'g', 15 + 2); // ::digits10 is 15
} else {
ss << "null"; // +INF || -INF || NaN (see RFC4627#section2.4)
}
}
break;
case QJsonValue::String:
ss << '"' << escapeString(v.toString()) << '"';
break;
case QJsonValue::Array:
{
const QJsonArray a = v.toArray();
ss << (compact ? "[" : "[\n");
if(!a.empty()) {
QJsonArray::const_iterator it = a.begin();
QJsonArray::const_iterator e = a.end();
if (!compact) ss << QByteArray(4*indent, ' ');
ss << toJson(*it++, format, indent+1);
for(;it != e; ++it) {
ss << (compact ? "," : ",\n");
if (!compact) ss << QByteArray(4*indent, ' ');
ss << toJson(*it, format, indent+1);
}
}
indent--;
ss << (compact ? "]" : QString("\n%1]").arg(QString(4*indent, ' ')));
}
break;
case QJsonValue::Object:
{
const QJsonObject o = v.toObject();
ss << (compact ? "{" : "{\n");
if(!o.empty()) {
QJsonObject::const_iterator it = o.begin();
QJsonObject::const_iterator e = o.end();
if (!compact) ss << QByteArray(4*indent, ' ');
ss << '"' << escapeString(it.key()) << (compact ? "\":" : "\": ") << toJson(it.value(), format, indent+1);
++it;
for(;it != e; ++it) {
ss << (compact ? "," : ",\n");
if (!compact) ss << QByteArray(4*indent, ' ');
ss << '"' << escapeString(it.key()) << (compact ? "\":" : "\": ") << toJson(it.value(), format, indent+1);
}
}
indent--;
ss << (compact ? "}" : QString("\n%1}").arg(QString(4*indent, ' ')));
}
break;
case QJsonValue::Undefined:
Q_ASSERT(0);
break;
}
return b;
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: toJson
//------------------------------------------------------------------------------
QByteArray QJsonDocument::toJson(JsonFormat format) const {
2020-08-25 20:47:50 +02:00
Q_UNUSED(format);
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
if(isArray()) {
QString s = toJson(array(), format);
return s.toUtf8();
}
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
if(isObject()) {
QString s = toJson(object(), format);
return s.toUtf8();
}
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
return QByteArray();
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: toVariant
//------------------------------------------------------------------------------
QVariant QJsonDocument::toVariant() const {
2020-08-25 20:47:50 +02:00
if(!isEmpty()) {
if(QJsonObject *const object = root_->toObject()) {
return object->toVariantMap();
}
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
if(QJsonArray *const array = root_->toArray()) {
return array->toVariantList();
}
}
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
return QVariant();
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: array
//------------------------------------------------------------------------------
QJsonArray QJsonDocument::array() const {
2020-08-25 20:47:50 +02:00
if(!isEmpty()) {
if(QJsonArray *const array = root_->toArray()) {
return *array;
}
}
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
return QJsonArray();
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: object
//------------------------------------------------------------------------------
QJsonObject QJsonDocument::object() const {
2020-08-25 20:47:50 +02:00
if(!isEmpty()) {
if(QJsonObject *const object = root_->toObject()) {
return *object;
}
}
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
return QJsonObject();
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: rawData
//------------------------------------------------------------------------------
const char *QJsonDocument::rawData(int *size) const {
2020-08-25 20:47:50 +02:00
Q_UNUSED(size);
// TODO(eteran): implement this
return 0;
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: fromBinaryData
//------------------------------------------------------------------------------
QJsonDocument QJsonDocument::fromBinaryData(const QByteArray &data, DataValidation validation) {
2020-08-25 20:47:50 +02:00
Q_UNUSED(data);
Q_UNUSED(validation);
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
QJsonDocument doc;
// TODO(eteran): implement this
return doc;
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: fromJson
//------------------------------------------------------------------------------
QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error) {
2020-08-25 20:47:50 +02:00
QJsonDocument doc;
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
const char *const begin = json.constData();
const char *const end = begin + json.size();
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
QJsonParser parser(begin, end);
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
doc.root_ = parser.parse();
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
if(error) {
*error = parser.state();
}
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
return doc;
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: fromRawData
//------------------------------------------------------------------------------
QJsonDocument QJsonDocument::fromRawData(const char *data, int size, DataValidation validation) {
2020-08-25 20:47:50 +02:00
// data has to be aligned to a 4 byte boundary.
Q_ASSERT(!(reinterpret_cast<quintptr>(data) % 3));
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
return fromBinaryData(QByteArray::fromRawData(data, size), validation);
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: fromVariant
//------------------------------------------------------------------------------
QJsonDocument QJsonDocument::fromVariant(const QVariant &variant) {
2020-08-25 20:47:50 +02:00
QJsonDocument doc;
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
if (variant.type() == QVariant::Map) {
doc.setObject(QJsonObject::fromVariantMap(variant.toMap()));
} else if (variant.type() == QVariant::Hash) {
doc.setObject(QJsonObject::fromVariantHash(variant.toHash()));
} else if (variant.type() == QVariant::List) {
doc.setArray(QJsonArray::fromVariantList(variant.toList()));
} else if (variant.type() == QVariant::StringList) {
doc.setArray(QJsonArray::fromStringList(variant.toStringList()));
}
2019-11-10 00:12:44 +01:00
2020-08-25 20:47:50 +02:00
return doc;
2019-11-10 00:12:44 +01:00
}
//------------------------------------------------------------------------------
// Name: swap
//------------------------------------------------------------------------------
void QJsonDocument::swap(QJsonDocument &other) {
2020-08-25 20:47:50 +02:00
qSwap(root_, other.root_);
2019-11-10 00:12:44 +01:00
}
#endif