rdr2view initial
This commit is contained in:
commit
b3a4c3ef5f
198 changed files with 62708 additions and 0 deletions
qjson4
QJsonArrayQJsonArray.cppQJsonArray.hQJsonDocumentQJsonDocument.cppQJsonDocument.hQJsonObjectQJsonObject.cppQJsonObject.hQJsonParseErrorQJsonParseError.cppQJsonParseError.hQJsonParser.cppQJsonParser.hQJsonRootQJsonRoot.hQJsonValueQJsonValue.cppQJsonValue.hQJsonValueRefQJsonValueRef.cppQJsonValueRef.h
1
qjson4/QJsonArray
Normal file
1
qjson4/QJsonArray
Normal file
|
@ -0,0 +1 @@
|
|||
#include "QJsonArray.h"
|
410
qjson4/QJsonArray.cpp
Normal file
410
qjson4/QJsonArray.cpp
Normal file
|
@ -0,0 +1,410 @@
|
|||
/*****************************************************************************
|
||||
* 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 "QJsonArray.h"
|
||||
#include "QJsonValueRef.h"
|
||||
#include "QJsonValue.h"
|
||||
#include <QtCore/QStringList>
|
||||
|
||||
#if QT_VERSION < 0x050000
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonArray
|
||||
// Desc: default constructor
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray::QJsonArray() {
|
||||
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonArray
|
||||
// Desc: copy constructor
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray::QJsonArray(const QJsonArray &other) : values_(other.values_) {
|
||||
|
||||
}
|
||||
|
||||
#if __cplusplus >= 201103L
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonArray
|
||||
// Desc: Creates an array initialized from args initialization list.
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray::QJsonArray(std::initializer_list<QJsonValue> args) {
|
||||
for(const QJsonValue &arg : args) {
|
||||
values_.append(arg);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: ~QJsonArray
|
||||
// Desc: destructor
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray::~QJsonArray() {
|
||||
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: operator=
|
||||
// Desc: assignment operator
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray &QJsonArray::operator=(const QJsonArray &other) {
|
||||
QJsonArray(other).swap(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: operator+=
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray &QJsonArray::operator+=(const QJsonValue &value) {
|
||||
values_.append(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: operator<<
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray &QJsonArray::operator<<(const QJsonValue &value) {
|
||||
values_.append(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: operator+
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray QJsonArray::operator+(const QJsonValue &value) const {
|
||||
QJsonArray arr(*this);
|
||||
arr.append(value);
|
||||
return arr;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: operator!=
|
||||
// Desc: returns true if the compared array IS NOT equal to this
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonArray::operator!=(const QJsonArray &other) const {
|
||||
return values_ != other.values_;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: operator==
|
||||
// Desc: returns true if the compared array IS equal to this
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonArray::operator==(const QJsonArray &other) const {
|
||||
return values_ == other.values_;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: begin
|
||||
// Desc: returns an iterator to the first contained element
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray::const_iterator QJsonArray::begin() const {
|
||||
return values_.begin();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: end
|
||||
// Desc: returns an iterator to one past the last contained element
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray::const_iterator QJsonArray::end() const {
|
||||
return values_.end();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: begin
|
||||
// Desc: returns an iterator to the first contained element
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray::iterator QJsonArray::begin() {
|
||||
return values_.begin();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: end
|
||||
// Desc: returns an iterator to one past the last contained element
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray::iterator QJsonArray::end() {
|
||||
return values_.end();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: constBegin
|
||||
// Desc: returns an iterator to the first contained element
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray::const_iterator QJsonArray::constBegin() const {
|
||||
return begin();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: constEnd
|
||||
// Desc: returns an iterator to one past the last contained element
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray::const_iterator QJsonArray::constEnd() const {
|
||||
return end();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: first
|
||||
// Desc: returns the first element by value
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue QJsonArray::first() const {
|
||||
Q_ASSERT(!empty());
|
||||
return values_.first();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: last
|
||||
// Desc: returns the last element by value
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue QJsonArray::last() const {
|
||||
Q_ASSERT(!empty());
|
||||
return values_.last();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: operator[]
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValueRef QJsonArray::operator[](int i) {
|
||||
return QJsonValueRef(this, i);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: operator[]
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue QJsonArray::operator[](int i) const {
|
||||
return values_[i];
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: at
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue QJsonArray::at(int i) const {
|
||||
return values_.at(i);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: size
|
||||
//------------------------------------------------------------------------------
|
||||
int QJsonArray::size() const {
|
||||
return values_.size();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: count
|
||||
//------------------------------------------------------------------------------
|
||||
int QJsonArray::count() const {
|
||||
return size();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: empty
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonArray::empty() const {
|
||||
return values_.empty();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isEmpty
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonArray::isEmpty() const {
|
||||
return empty();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: pop_back
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonArray::pop_back() {
|
||||
values_.pop_back();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: pop_front
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonArray::pop_front() {
|
||||
values_.pop_front();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: push_back
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonArray::push_back(const QJsonValue &value) {
|
||||
values_.push_back(value);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: push_front
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonArray::push_front(const QJsonValue &value) {
|
||||
values_.push_front(value);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonArray::append(const QJsonValue &value) {
|
||||
values_.append(value);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonArray::contains(const QJsonValue &value) const {
|
||||
return values_.contains(value);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray::iterator QJsonArray::erase(iterator it) {
|
||||
return values_.erase(it);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonArray::insert(int i, const QJsonValue &value) {
|
||||
values_.insert(i, value);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray::iterator QJsonArray::insert(iterator before, const QJsonValue &value) {
|
||||
return values_.insert(before, value);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonArray::prepend(const QJsonValue &value) {
|
||||
values_.prepend(value);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonArray::removeAt(int i) {
|
||||
values_.removeAt(i);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonArray::removeFirst() {
|
||||
values_.removeFirst();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonArray::removeLast() {
|
||||
values_.removeLast();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonArray::replace(int i, const QJsonValue &value) {
|
||||
values_.replace(i, value);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue QJsonArray::takeAt(int i) {
|
||||
return values_.takeAt(i);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toVariantList
|
||||
//------------------------------------------------------------------------------
|
||||
QVariantList QJsonArray::toVariantList() const {
|
||||
QVariantList a;
|
||||
Q_FOREACH(const QJsonValue &v, *this) {
|
||||
a.push_back(v.toVariant());
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray QJsonArray::fromStringList(const QStringList &list) {
|
||||
QJsonArray a;
|
||||
Q_FOREACH(const QString &s, list) {
|
||||
a.push_back(QJsonValue(s));
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray QJsonArray::fromVariantList(const QVariantList &list) {
|
||||
QJsonArray a;
|
||||
Q_FOREACH(const QVariant &v, list) {
|
||||
a.push_back(QJsonValue::fromVariant(v));
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonRoot *QJsonArray::clone() const {
|
||||
return new QJsonArray(*this);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
const QJsonObject *QJsonArray::toObject() const {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject *QJsonArray::toObject() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray *QJsonArray::toArray() {
|
||||
return this;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
const QJsonArray *QJsonArray::toArray() const {
|
||||
return this;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonArray::swap(QJsonArray &other) {
|
||||
qSwap(values_, other.values_);
|
||||
}
|
||||
|
||||
#endif
|
139
qjson4/QJsonArray.h
Normal file
139
qjson4/QJsonArray.h
Normal file
|
@ -0,0 +1,139 @@
|
|||
/*****************************************************************************
|
||||
* 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/>.
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef QJSON_ARRAY_H_
|
||||
#define QJSON_ARRAY_H_
|
||||
|
||||
#include <QtCore/QtGlobal>
|
||||
|
||||
#if QT_VERSION >= 0x050000
|
||||
#include <QtCore/QJsonArray>
|
||||
#else
|
||||
|
||||
#include "QJsonRoot.h"
|
||||
#include <QtCore/QList>
|
||||
#include <QtCore/QVariantList>
|
||||
|
||||
class QJsonValue;
|
||||
class QJsonValueRef;
|
||||
|
||||
class QJsonArray : public QJsonRoot {
|
||||
friend class QJsonDocument;
|
||||
friend class QJsonValue;
|
||||
friend class QJsonValueRef;
|
||||
friend class QJsonParser;
|
||||
public:
|
||||
// TODO(eteran): manually implement the array, for now we use QList
|
||||
// but the real thing has a custom implementation
|
||||
// I guess for the purposes of less interdependancies?
|
||||
// maybe so it's easier to forward declare the iterators?
|
||||
|
||||
typedef QList<QJsonValue>::const_iterator const_iterator;
|
||||
typedef QList<QJsonValue>::iterator iterator;
|
||||
typedef const_iterator ConstIterator;
|
||||
typedef iterator Iterator;
|
||||
typedef QList<QJsonValue>::const_pointer const_pointer;
|
||||
typedef QList<QJsonValue>::const_reference const_reference;
|
||||
typedef QList<QJsonValue>::difference_type difference_type;
|
||||
typedef QList<QJsonValue>::pointer pointer;
|
||||
typedef QList<QJsonValue>::reference reference;
|
||||
typedef QList<QJsonValue>::size_type size_type;
|
||||
typedef QList<QJsonValue>::value_type value_type;
|
||||
|
||||
public:
|
||||
QJsonArray();
|
||||
QJsonArray(const QJsonArray &other);
|
||||
#if __cplusplus >= 201103L
|
||||
QJsonArray(std::initializer_list<QJsonValue> args);
|
||||
#endif
|
||||
~QJsonArray();
|
||||
|
||||
public:
|
||||
QJsonArray &operator=(const QJsonArray &other);
|
||||
|
||||
public:
|
||||
bool operator!=(const QJsonArray &other) const;
|
||||
bool operator==(const QJsonArray &other) const;
|
||||
QJsonArray operator+(const QJsonValue &value) const;
|
||||
QJsonArray &operator+=(const QJsonValue &value);
|
||||
QJsonArray &operator<<(const QJsonValue &value);
|
||||
|
||||
public:
|
||||
const_iterator begin() const;
|
||||
const_iterator end() const;
|
||||
iterator begin();
|
||||
iterator end();
|
||||
const_iterator constBegin() const;
|
||||
const_iterator constEnd() const;
|
||||
|
||||
public:
|
||||
QJsonValueRef operator[](int i);
|
||||
QJsonValue operator[](int i) const;
|
||||
QJsonValue at(int i) const;
|
||||
QJsonValue first() const;
|
||||
QJsonValue last() const;
|
||||
|
||||
public:
|
||||
int size() const;
|
||||
int count() const;
|
||||
bool empty() const;
|
||||
bool isEmpty() const;
|
||||
|
||||
public:
|
||||
void pop_back();
|
||||
void pop_front();
|
||||
void push_back(const QJsonValue &value);
|
||||
void push_front(const QJsonValue &value);
|
||||
|
||||
public:
|
||||
void append(const QJsonValue &value);
|
||||
bool contains(const QJsonValue &value) const;
|
||||
iterator erase(iterator it);
|
||||
void insert(int i, const QJsonValue &value);
|
||||
iterator insert(iterator before, const QJsonValue &value);
|
||||
void prepend(const QJsonValue &value);
|
||||
void removeAt(int i);
|
||||
void removeFirst();
|
||||
void removeLast();
|
||||
void replace(int i, const QJsonValue &value);
|
||||
QJsonValue takeAt(int i);
|
||||
|
||||
public:
|
||||
QVariantList toVariantList() const;
|
||||
|
||||
public:
|
||||
static QJsonArray fromStringList(const QStringList &list);
|
||||
static QJsonArray fromVariantList(const QVariantList &list);
|
||||
|
||||
private:
|
||||
virtual QJsonRoot *clone() const;
|
||||
virtual QJsonArray *toArray();
|
||||
virtual QJsonObject *toObject();
|
||||
virtual const QJsonArray *toArray() const;
|
||||
virtual const QJsonObject *toObject() const;
|
||||
|
||||
private:
|
||||
void swap(QJsonArray &other);
|
||||
|
||||
private:
|
||||
QList<QJsonValue> values_;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
1
qjson4/QJsonDocument
Normal file
1
qjson4/QJsonDocument
Normal file
|
@ -0,0 +1 @@
|
|||
#include "QJsonDocument.h"
|
417
qjson4/QJsonDocument.cpp
Normal file
417
qjson4/QJsonDocument.cpp
Normal file
|
@ -0,0 +1,417 @@
|
|||
/*****************************************************************************
|
||||
* 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) {
|
||||
setObject(object);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonDocument
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonDocument::QJsonDocument(const QJsonArray &array) : root_(0) {
|
||||
setArray(array);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonDocument
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonDocument::QJsonDocument(const QJsonDocument &other) : root_(0) {
|
||||
if(other.root_) {
|
||||
root_ = other.root_->clone();
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: ~QJsonDocument
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonDocument::~QJsonDocument() {
|
||||
delete root_;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: operator=
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonDocument &QJsonDocument::operator=(const QJsonDocument &other) {
|
||||
QJsonDocument(other).swap(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: operator!=
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonDocument::operator!=(const QJsonDocument &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: operator==
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonDocument::operator==(const QJsonDocument &other) const {
|
||||
|
||||
if(isArray() && other.isArray()) {
|
||||
return array() == other.array();
|
||||
}
|
||||
|
||||
if(isObject() && other.isObject()) {
|
||||
return object() == other.object();
|
||||
}
|
||||
|
||||
if(isEmpty() && other.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if(isNull() && other.isNull()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isArray
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonDocument::isArray() const {
|
||||
return root_ && root_->toArray();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isEmpty
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonDocument::isEmpty() const {
|
||||
|
||||
// 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
|
||||
|
||||
return !root_;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isNull
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonDocument::isNull() const {
|
||||
return !root_;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isObject
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonDocument::isObject() const {
|
||||
return root_ && root_->toObject();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: setArray
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonDocument::setArray(const QJsonArray &array) {
|
||||
setRoot(array);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: setObject
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonDocument::setObject(const QJsonObject &object) {
|
||||
setRoot(object);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: setRoot
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonDocument::setRoot(const QJsonRoot &root) {
|
||||
delete root_;
|
||||
root_ = root.clone();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toBinaryData
|
||||
//------------------------------------------------------------------------------
|
||||
QByteArray QJsonDocument::toBinaryData() const {
|
||||
QByteArray r;
|
||||
// TODO(eteran): implement this
|
||||
return r;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: escapeString
|
||||
//------------------------------------------------------------------------------
|
||||
QString QJsonDocument::escapeString(const QString &s) const {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toJson
|
||||
//------------------------------------------------------------------------------
|
||||
QString QJsonDocument::toJson(const QJsonValue &v, JsonFormat format) const {
|
||||
|
||||
QString b;
|
||||
QTextStream ss(&b, QIODevice::WriteOnly | QIODevice::Text);
|
||||
|
||||
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 << "[";
|
||||
if(!a.empty()) {
|
||||
QJsonArray::const_iterator it = a.begin();
|
||||
QJsonArray::const_iterator e = a.end();
|
||||
|
||||
ss << toJson(*it++, format);
|
||||
|
||||
for(;it != e; ++it) {
|
||||
ss << ',';
|
||||
ss << toJson(*it, format);
|
||||
}
|
||||
}
|
||||
ss << "]";
|
||||
}
|
||||
break;
|
||||
case QJsonValue::Object:
|
||||
{
|
||||
const QJsonObject o = v.toObject();
|
||||
ss << "{";
|
||||
if(!o.empty()) {
|
||||
QJsonObject::const_iterator it = o.begin();
|
||||
QJsonObject::const_iterator e = o.end();
|
||||
|
||||
ss << '"' << escapeString(it.key()) << "\": " << toJson(it.value(), format);
|
||||
++it;
|
||||
for(;it != e; ++it) {
|
||||
ss << ',';
|
||||
ss << '"' << escapeString(it.key()) << "\": " << toJson(it.value(), format);
|
||||
}
|
||||
}
|
||||
ss << "}";
|
||||
}
|
||||
break;
|
||||
case QJsonValue::Undefined:
|
||||
Q_ASSERT(0);
|
||||
break;
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toJson
|
||||
//------------------------------------------------------------------------------
|
||||
QByteArray QJsonDocument::toJson(JsonFormat format) const {
|
||||
|
||||
Q_UNUSED(format);
|
||||
|
||||
if(isArray()) {
|
||||
QString s = toJson(array(), format);
|
||||
return s.toUtf8();
|
||||
}
|
||||
|
||||
if(isObject()) {
|
||||
QString s = toJson(object(), format);
|
||||
return s.toUtf8();
|
||||
}
|
||||
|
||||
return QByteArray();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toVariant
|
||||
//------------------------------------------------------------------------------
|
||||
QVariant QJsonDocument::toVariant() const {
|
||||
|
||||
if(!isEmpty()) {
|
||||
if(QJsonObject *const object = root_->toObject()) {
|
||||
return object->toVariantMap();
|
||||
}
|
||||
|
||||
if(QJsonArray *const array = root_->toArray()) {
|
||||
return array->toVariantList();
|
||||
}
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: array
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray QJsonDocument::array() const {
|
||||
|
||||
if(!isEmpty()) {
|
||||
if(QJsonArray *const array = root_->toArray()) {
|
||||
return *array;
|
||||
}
|
||||
}
|
||||
|
||||
return QJsonArray();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: object
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject QJsonDocument::object() const {
|
||||
|
||||
if(!isEmpty()) {
|
||||
if(QJsonObject *const object = root_->toObject()) {
|
||||
return *object;
|
||||
}
|
||||
}
|
||||
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: rawData
|
||||
//------------------------------------------------------------------------------
|
||||
const char *QJsonDocument::rawData(int *size) const {
|
||||
Q_UNUSED(size);
|
||||
// TODO(eteran): implement this
|
||||
return 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: fromBinaryData
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonDocument QJsonDocument::fromBinaryData(const QByteArray &data, DataValidation validation) {
|
||||
Q_UNUSED(data);
|
||||
Q_UNUSED(validation);
|
||||
|
||||
QJsonDocument doc;
|
||||
// TODO(eteran): implement this
|
||||
return doc;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: fromJson
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error) {
|
||||
QJsonDocument doc;
|
||||
|
||||
const char *const begin = json.constData();
|
||||
const char *const end = begin + json.size();
|
||||
|
||||
QJsonParser parser(begin, end);
|
||||
|
||||
doc.root_ = parser.parse();
|
||||
|
||||
if(error) {
|
||||
*error = parser.state();
|
||||
}
|
||||
|
||||
return doc;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: fromRawData
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonDocument QJsonDocument::fromRawData(const char *data, int size, DataValidation validation) {
|
||||
|
||||
// data has to be aligned to a 4 byte boundary.
|
||||
Q_ASSERT(!(reinterpret_cast<quintptr>(data) % 3));
|
||||
|
||||
return fromBinaryData(QByteArray::fromRawData(data, size), validation);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: fromVariant
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonDocument QJsonDocument::fromVariant(const QVariant &variant) {
|
||||
|
||||
QJsonDocument doc;
|
||||
|
||||
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()));
|
||||
}
|
||||
|
||||
return doc;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: swap
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonDocument::swap(QJsonDocument &other) {
|
||||
qSwap(root_, other.root_);
|
||||
}
|
||||
|
||||
#endif
|
103
qjson4/QJsonDocument.h
Normal file
103
qjson4/QJsonDocument.h
Normal file
|
@ -0,0 +1,103 @@
|
|||
/*****************************************************************************
|
||||
* 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/>.
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef QJSON_DOCUMENT_H_
|
||||
#define QJSON_DOCUMENT_H_
|
||||
|
||||
#include <QtCore/QtGlobal>
|
||||
|
||||
#if QT_VERSION >= 0x050000
|
||||
#include <QtCore/QJsonDocument>
|
||||
#else
|
||||
|
||||
class QVariant;
|
||||
class QByteArray;
|
||||
class QTextStream;
|
||||
class QJsonObject;
|
||||
class QJsonValue;
|
||||
class QJsonArray;
|
||||
class QJsonParseError;
|
||||
class QJsonRoot;
|
||||
|
||||
class QJsonDocument {
|
||||
public:
|
||||
enum DataValidation {
|
||||
Validate = 0,
|
||||
BypassValidation = 1
|
||||
};
|
||||
|
||||
enum JsonFormat {
|
||||
Indented,
|
||||
Compact
|
||||
};
|
||||
|
||||
public:
|
||||
QJsonDocument();
|
||||
QJsonDocument(const QJsonObject &object);
|
||||
QJsonDocument(const QJsonArray &array);
|
||||
QJsonDocument(const QJsonDocument &other);
|
||||
~QJsonDocument();
|
||||
|
||||
public:
|
||||
QJsonDocument &operator=(const QJsonDocument &other);
|
||||
|
||||
public:
|
||||
bool operator!=(const QJsonDocument &other) const;
|
||||
bool operator==(const QJsonDocument &other) const;
|
||||
|
||||
public:
|
||||
bool isArray() const;
|
||||
bool isEmpty() const;
|
||||
bool isNull() const;
|
||||
bool isObject() const;
|
||||
|
||||
public:
|
||||
QByteArray toBinaryData() const;
|
||||
QByteArray toJson(JsonFormat format = Indented) const;
|
||||
QVariant toVariant() const;
|
||||
|
||||
public:
|
||||
QJsonArray array() const;
|
||||
QJsonObject object() const;
|
||||
const char *rawData(int *size) const;
|
||||
|
||||
public:
|
||||
void setArray(const QJsonArray &array);
|
||||
void setObject(const QJsonObject &object);
|
||||
|
||||
public:
|
||||
static QJsonDocument fromBinaryData(const QByteArray &data, DataValidation validation = Validate);
|
||||
static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error = 0);
|
||||
static QJsonDocument fromRawData(const char *data, int size, DataValidation validation = Validate);
|
||||
static QJsonDocument fromVariant(const QVariant &variant);
|
||||
|
||||
private:
|
||||
void setRoot(const QJsonRoot &root);
|
||||
QString toJson(const QJsonValue &v, JsonFormat format) const;
|
||||
QString escapeString(const QString &s) const;
|
||||
|
||||
private:
|
||||
void swap(QJsonDocument &other);
|
||||
|
||||
private:
|
||||
QJsonRoot *root_;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
1
qjson4/QJsonObject
Normal file
1
qjson4/QJsonObject
Normal file
|
@ -0,0 +1 @@
|
|||
#include "QJsonObject.h"
|
322
qjson4/QJsonObject.cpp
Normal file
322
qjson4/QJsonObject.cpp
Normal file
|
@ -0,0 +1,322 @@
|
|||
/*****************************************************************************
|
||||
* 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 "QJsonObject.h"
|
||||
|
||||
#if QT_VERSION < 0x050000
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonObject
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject::QJsonObject() {
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonObject
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject::QJsonObject(const QJsonObject &other) : values_(other.values_) {
|
||||
}
|
||||
|
||||
#if __cplusplus >= 201103L
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonObject
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject::QJsonObject(std::initializer_list<QPair<QString, QJsonValue> > args) {
|
||||
for(const QPair<QString, QJsonValue> &arg : args) {
|
||||
values_.insert(arg.first, arg.second);
|
||||
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: ~QJsonObject
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject::~QJsonObject() {
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject &QJsonObject::operator=(const QJsonObject &other) {
|
||||
QJsonObject(other).swap(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject::iterator QJsonObject::begin() {
|
||||
return values_.begin();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject::const_iterator QJsonObject::begin() const {
|
||||
return values_.begin();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject::iterator QJsonObject::end() {
|
||||
return values_.end();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject::const_iterator QJsonObject::end() const {
|
||||
return values_.end();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject::const_iterator QJsonObject::constBegin() const {
|
||||
return begin();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject::const_iterator QJsonObject::constEnd() const {
|
||||
return end();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
int QJsonObject::count() const {
|
||||
return size();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
int QJsonObject::length() const {
|
||||
return size();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
int QJsonObject::size() const {
|
||||
return values_.size();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonObject::empty() const {
|
||||
return values_.empty();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonObject::isEmpty() const {
|
||||
return empty();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject::const_iterator QJsonObject::constFind(const QString &key) const {
|
||||
return values_.find(key);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonObject::contains(const QString &key) const {
|
||||
return values_.contains(key);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject::iterator QJsonObject::find(const QString &key) {
|
||||
return values_.find(key);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject::const_iterator QJsonObject::find(const QString &key) const {
|
||||
return values_.find(key);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject::iterator QJsonObject::erase(iterator it) {
|
||||
return values_.erase(it);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject::iterator QJsonObject::insert(const QString &key, const QJsonValue &value) {
|
||||
return values_.insert(key, value);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QStringList QJsonObject::keys() const {
|
||||
return values_.keys();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonObject::remove(const QString &key) {
|
||||
values_.remove(key);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue QJsonObject::take(const QString &key) {
|
||||
return values_.take(key);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue QJsonObject::value(const QString &key) const {
|
||||
return values_.value(key);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonObject::operator!=(const QJsonObject &other) const {
|
||||
return values_ != other.values_;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonObject::operator==(const QJsonObject &other) const {
|
||||
return values_ != other.values_;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue QJsonObject::operator[](const QString &key) const {
|
||||
return values_[key];
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValueRef QJsonObject::operator[](const QString &key) {
|
||||
return QJsonValueRef(this, key);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QVariantMap QJsonObject::toVariantMap() const {
|
||||
QVariantMap a;
|
||||
for(const_iterator it = begin(); it != end(); ++it) {
|
||||
a.insert(it.key(), it.value().toVariant());
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QVariantHash QJsonObject::toVariantHash() const {
|
||||
QVariantHash a;
|
||||
for(const_iterator it = begin(); it != end(); ++it) {
|
||||
a.insert(it.key(), it.value().toVariant());
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject QJsonObject::fromVariantMap(const QVariantMap &map) {
|
||||
QJsonObject o;
|
||||
for(QVariantMap::const_iterator it = map.begin(); it != map.end(); ++it) {
|
||||
o.insert(it.key(), QJsonValue::fromVariant(it.value()));
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject QJsonObject::fromVariantHash(const QVariantHash &hash) {
|
||||
QJsonObject o;
|
||||
for(QVariantHash::const_iterator it = hash.begin(); it != hash.end(); ++it) {
|
||||
o.insert(it.key(), QJsonValue::fromVariant(it.value()));
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonRoot *QJsonObject::clone() const {
|
||||
return new QJsonObject(*this);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
const QJsonObject *QJsonObject::toObject() const {
|
||||
return this;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject *QJsonObject::toObject() {
|
||||
return this;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray *QJsonObject::toArray() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
//------------------------------------------------------------------------------
|
||||
const QJsonArray *QJsonObject::toArray() const {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: swap
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonObject::swap(QJsonObject &other) {
|
||||
qSwap(values_, other.values_);
|
||||
}
|
||||
|
||||
#endif
|
121
qjson4/QJsonObject.h
Normal file
121
qjson4/QJsonObject.h
Normal file
|
@ -0,0 +1,121 @@
|
|||
/*****************************************************************************
|
||||
* 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/>.
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef QJSON_OBJECT_H_
|
||||
#define QJSON_OBJECT_H_
|
||||
|
||||
#include <QtCore/QtGlobal>
|
||||
|
||||
#if QT_VERSION >= 0x050000
|
||||
#include <QtCore/QJsonObject>
|
||||
#else
|
||||
|
||||
#include "QJsonRoot.h"
|
||||
#include "QJsonValueRef.h"
|
||||
#include "QJsonValue.h"
|
||||
#include <QtCore/QString>
|
||||
#include <QtCore/QStringList>
|
||||
#include <QtCore/QVariantMap>
|
||||
#include <QtCore/QMap>
|
||||
|
||||
class QJsonObject : public QJsonRoot {
|
||||
friend class QJsonDocument;
|
||||
friend class QJsonValue;
|
||||
friend class QJsonValueRef;
|
||||
friend class QJsonParser;
|
||||
public:
|
||||
// TODO(eteran): manually implement the map, for now we use QMap
|
||||
// but the real thing has a custom implementation
|
||||
// I guess for the purposes of less interdependancies?
|
||||
// maybe so it's easier to forward declare the iterators?
|
||||
|
||||
typedef QMap<QString, QJsonValue>::const_iterator const_iterator;
|
||||
typedef QMap<QString, QJsonValue>::iterator iterator;
|
||||
typedef const_iterator ConstIterator;
|
||||
typedef iterator Iterator;
|
||||
typedef QMap<QString, QJsonValue>::key_type key_type;
|
||||
typedef QMap<QString, QJsonValue>::mapped_type mapped_type;
|
||||
typedef QMap<QString, QJsonValue>::size_type size_type;
|
||||
|
||||
public:
|
||||
QJsonObject();
|
||||
#if __cplusplus >= 201103L
|
||||
QJsonObject(std::initializer_list<QPair<QString, QJsonValue> > args);
|
||||
#endif
|
||||
QJsonObject(const QJsonObject &other);
|
||||
~QJsonObject();
|
||||
QJsonObject &operator=(const QJsonObject &other);
|
||||
|
||||
public:
|
||||
iterator begin();
|
||||
const_iterator begin() const;
|
||||
iterator end();
|
||||
const_iterator end() const;
|
||||
const_iterator constBegin() const;
|
||||
const_iterator constEnd() const;
|
||||
|
||||
public:
|
||||
int count() const;
|
||||
int length() const;
|
||||
int size() const;
|
||||
bool empty() const;
|
||||
bool isEmpty() const;
|
||||
|
||||
public:
|
||||
const_iterator constFind(const QString &key) const;
|
||||
bool contains(const QString &key) const;
|
||||
iterator find(const QString &key);
|
||||
const_iterator find(const QString &key) const;
|
||||
|
||||
public:
|
||||
iterator erase(iterator it);
|
||||
iterator insert(const QString &key, const QJsonValue &value);
|
||||
QStringList keys() const;
|
||||
void remove(const QString &key);
|
||||
QJsonValue take(const QString &key);
|
||||
QJsonValue value(const QString &key) const;
|
||||
bool operator!=(const QJsonObject &other) const;
|
||||
bool operator==(const QJsonObject &other) const;
|
||||
QJsonValue operator[](const QString &key) const;
|
||||
QJsonValueRef operator[](const QString &key);
|
||||
|
||||
public:
|
||||
QVariantMap toVariantMap() const;
|
||||
QVariantHash toVariantHash() const;
|
||||
|
||||
public:
|
||||
static QJsonObject fromVariantMap(const QVariantMap &map);
|
||||
static QJsonObject fromVariantHash(const QVariantHash &hash);
|
||||
|
||||
private:
|
||||
virtual QJsonRoot *clone() const;
|
||||
virtual QJsonArray *toArray();
|
||||
virtual QJsonObject *toObject();
|
||||
virtual const QJsonArray *toArray() const;
|
||||
virtual const QJsonObject *toObject() const;
|
||||
|
||||
private:
|
||||
void swap(QJsonObject &other);
|
||||
|
||||
private:
|
||||
QMap<QString, QJsonValue> values_;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
1
qjson4/QJsonParseError
Normal file
1
qjson4/QJsonParseError
Normal file
|
@ -0,0 +1 @@
|
|||
#include "QJsonParseError.h"
|
64
qjson4/QJsonParseError.cpp
Normal file
64
qjson4/QJsonParseError.cpp
Normal file
|
@ -0,0 +1,64 @@
|
|||
/*****************************************************************************
|
||||
* 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 "QJsonParseError.h"
|
||||
|
||||
#if QT_VERSION < 0x050000
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: errorString
|
||||
// Desc: The QJsonParseError class is used to report errors during JSON parsing.
|
||||
//------------------------------------------------------------------------------
|
||||
QString QJsonParseError::errorString() const {
|
||||
switch(error) {
|
||||
case NoError:
|
||||
return "No error occurred";
|
||||
case UnterminatedObject:
|
||||
return "unterminated object";
|
||||
case MissingNameSeparator:
|
||||
return "missing name separator";
|
||||
case UnterminatedArray:
|
||||
return "unterminated array";
|
||||
case MissingValueSeparator:
|
||||
return "missing value separator";
|
||||
case IllegalValue:
|
||||
return "illegal value";
|
||||
case TerminationByNumber:
|
||||
return "invalid termination by number";
|
||||
case IllegalNumber:
|
||||
return "illegal number";
|
||||
case IllegalEscapeSequence:
|
||||
return "illegal escape sequence";
|
||||
case IllegalUTF8String:
|
||||
return "invalid UTF8 string";
|
||||
case UnterminatedString:
|
||||
return "unterminated string";
|
||||
case MissingObject:
|
||||
return "object is missing after a comma";
|
||||
case DeepNesting:
|
||||
return "too deeply nested document";
|
||||
case DocumentTooLarge:
|
||||
return "too large document";
|
||||
case GarbageAtEnd:
|
||||
return "garbage at the end of the document";
|
||||
}
|
||||
|
||||
return QString();
|
||||
}
|
||||
|
||||
#endif
|
60
qjson4/QJsonParseError.h
Normal file
60
qjson4/QJsonParseError.h
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*****************************************************************************
|
||||
* 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/>.
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef QJSON_PARSE_ERROR_H_
|
||||
#define QJSON_PARSE_ERROR_H_
|
||||
|
||||
#include <QtCore/QtGlobal>
|
||||
|
||||
#if QT_VERSION >= 0x050000
|
||||
#include <QtCore/QJsonParseError>
|
||||
#else
|
||||
|
||||
#include <QtCore/QString>
|
||||
|
||||
class QJsonParseError {
|
||||
public:
|
||||
enum ParseError {
|
||||
NoError = 0,
|
||||
UnterminatedObject = 1,
|
||||
MissingNameSeparator = 2,
|
||||
UnterminatedArray = 3,
|
||||
MissingValueSeparator = 4,
|
||||
IllegalValue = 5,
|
||||
TerminationByNumber = 6,
|
||||
IllegalNumber = 7,
|
||||
IllegalEscapeSequence = 8,
|
||||
IllegalUTF8String = 9,
|
||||
UnterminatedString = 10,
|
||||
MissingObject = 11,
|
||||
DeepNesting = 12,
|
||||
DocumentTooLarge = 13,
|
||||
GarbageAtEnd = 14
|
||||
};
|
||||
|
||||
public:
|
||||
QString errorString() const;
|
||||
|
||||
public:
|
||||
ParseError error;
|
||||
int offset;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
455
qjson4/QJsonParser.cpp
Normal file
455
qjson4/QJsonParser.cpp
Normal file
|
@ -0,0 +1,455 @@
|
|||
/*****************************************************************************
|
||||
* 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 "QJsonParser.h"
|
||||
#include "QJsonArray.h"
|
||||
#include "QJsonObject.h"
|
||||
#include "QJsonValue.h"
|
||||
|
||||
|
||||
#if QT_VERSION < 0x050000
|
||||
|
||||
#include <cctype>
|
||||
#include <QScopedPointer>
|
||||
#include <QVector>
|
||||
|
||||
namespace {
|
||||
|
||||
unsigned int to_hex(int ch) {
|
||||
|
||||
static const int hexval[256] = {
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
|
||||
0x0, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
|
||||
};
|
||||
|
||||
if(static_cast<unsigned int>(ch) < 256) {
|
||||
return hexval[static_cast<unsigned int>(ch)];
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonParser
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonParser::QJsonParser(const char *begin, const char *end) : begin_(begin), end_(end), p_(begin) {
|
||||
state_.error = QJsonParseError::NoError;
|
||||
state_.offset = 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: parse
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonRoot *QJsonParser::parse() {
|
||||
if(begin_ == end_) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
QJsonRoot *ret = 0;
|
||||
|
||||
try {
|
||||
const char ch = peek();
|
||||
switch(ch) {
|
||||
case ArrayBegin:
|
||||
ret = getArray();
|
||||
break;
|
||||
case ObjectBegin:
|
||||
ret = getObject();
|
||||
break;
|
||||
default:
|
||||
state_.error = QJsonParseError::IllegalValue;
|
||||
state_.offset = p_ - begin_;
|
||||
break;
|
||||
}
|
||||
} catch(const QJsonParseError &e) {
|
||||
state_ = e;
|
||||
}
|
||||
|
||||
if(ret) {
|
||||
// eat up trailing white space...
|
||||
while(p_ != end_ && std::isspace(*p_)) {
|
||||
++p_;
|
||||
}
|
||||
|
||||
//detect trailing garbage
|
||||
if(p_ != end_) {
|
||||
state_.error = QJsonParseError::GarbageAtEnd;
|
||||
state_.offset = p_ - begin_;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: peek
|
||||
//------------------------------------------------------------------------------
|
||||
char QJsonParser::peek() {
|
||||
// first eat up some whitespace
|
||||
while(p_ != end_ && std::isspace(*p_)) {
|
||||
++p_;
|
||||
}
|
||||
|
||||
return *p_;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: getValue
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue QJsonParser::getValue() {
|
||||
|
||||
switch(peek()) {
|
||||
case ObjectBegin:
|
||||
{
|
||||
QScopedPointer<QJsonObject> obj(getObject());
|
||||
return QJsonValue(*obj);
|
||||
}
|
||||
case ArrayBegin:
|
||||
{
|
||||
QScopedPointer<QJsonArray> arr(getArray());
|
||||
return QJsonValue(*arr);
|
||||
}
|
||||
case Quote:
|
||||
return QJsonValue(getString());
|
||||
case 't':
|
||||
return getTrue();
|
||||
case 'f':
|
||||
return getFalse();
|
||||
case 'n':
|
||||
return getNull();
|
||||
default:
|
||||
return getNumber();
|
||||
}
|
||||
|
||||
throwError(QJsonParseError::MissingObject);
|
||||
return QJsonValue();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: getObject
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject *QJsonParser::getObject() {
|
||||
|
||||
QScopedPointer<QJsonObject> obj(new QJsonObject);
|
||||
|
||||
char tok = peek();
|
||||
if(tok != ObjectBegin) {
|
||||
throwError(QJsonParseError::IllegalValue);
|
||||
}
|
||||
|
||||
++p_;
|
||||
|
||||
// handle empty object
|
||||
tok = peek();
|
||||
if(peek() == ObjectEnd) {
|
||||
++p_;
|
||||
} else {
|
||||
|
||||
do {
|
||||
QPair<QString, QJsonValue> p = getPair();
|
||||
obj->values_.insert(p.first, p.second);
|
||||
|
||||
tok = peek();
|
||||
++p_;
|
||||
|
||||
} while(tok == ValueSeparator);
|
||||
}
|
||||
|
||||
if(tok != ObjectEnd) {
|
||||
throwError(QJsonParseError::UnterminatedObject);
|
||||
}
|
||||
|
||||
return obj.take();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: getArray
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray *QJsonParser::getArray() {
|
||||
|
||||
QScopedPointer<QJsonArray> arr(new QJsonArray);
|
||||
|
||||
char tok = peek();
|
||||
|
||||
if(tok != ArrayBegin) {
|
||||
throwError(QJsonParseError::IllegalValue);
|
||||
}
|
||||
|
||||
++p_;
|
||||
|
||||
// handle empty object
|
||||
tok = peek();
|
||||
if(tok == ArrayEnd) {
|
||||
++p_;
|
||||
} else {
|
||||
do {
|
||||
arr->values_.push_back(getValue());
|
||||
|
||||
tok = peek();
|
||||
++p_;
|
||||
|
||||
} while(tok == ValueSeparator);
|
||||
}
|
||||
|
||||
if(tok != ArrayEnd) {
|
||||
throwError(QJsonParseError::MissingValueSeparator);
|
||||
}
|
||||
|
||||
return arr.take();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: getPair
|
||||
//------------------------------------------------------------------------------
|
||||
QPair<QString, QJsonValue> QJsonParser::getPair() {
|
||||
|
||||
QString key = getString();
|
||||
|
||||
if(peek() != NameSeparator) {
|
||||
throwError(QJsonParseError::MissingNameSeparator);
|
||||
}
|
||||
++p_;
|
||||
|
||||
return qMakePair(key, getValue());
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: getString
|
||||
//------------------------------------------------------------------------------
|
||||
QString QJsonParser::getString() {
|
||||
|
||||
if(peek() != Quote) {
|
||||
throwError(QJsonParseError::IllegalUTF8String);
|
||||
}
|
||||
++p_;
|
||||
|
||||
QByteArray s;
|
||||
|
||||
while(p_ != end_ && *p_ != Quote && *p_ != '\n') {
|
||||
if(*p_ == '\\') {
|
||||
++p_;
|
||||
if(p_ != end_) {
|
||||
switch(*p_) {
|
||||
case '"': s.append('"'); break;
|
||||
case '\\': s.append('\\'); break;
|
||||
case '/': s.append('/'); break;
|
||||
case 'b': s.append('\b'); break;
|
||||
case 'f': s.append('\f'); break;
|
||||
case 'n': s.append('\n'); break;
|
||||
case 'r': s.append('\r'); break;
|
||||
case 't': s.append('\t'); break;
|
||||
case 'u':
|
||||
{
|
||||
|
||||
QString hexChar;
|
||||
|
||||
// convert \uXXXX escape sequences to UTF-8
|
||||
char hex[4];
|
||||
if(p_ == end_) { throwError(QJsonParseError::IllegalEscapeSequence); } hex[0] = *++p_;
|
||||
if(p_ == end_) { throwError(QJsonParseError::IllegalEscapeSequence); } hex[1] = *++p_;
|
||||
if(p_ == end_) { throwError(QJsonParseError::IllegalEscapeSequence); } hex[2] = *++p_;
|
||||
if(p_ == end_) { throwError(QJsonParseError::IllegalEscapeSequence); } hex[3] = *++p_;
|
||||
|
||||
if(!std::isxdigit(hex[0])) throwError(QJsonParseError::IllegalUTF8String);
|
||||
if(!std::isxdigit(hex[1])) throwError(QJsonParseError::IllegalUTF8String);
|
||||
if(!std::isxdigit(hex[2])) throwError(QJsonParseError::IllegalUTF8String);
|
||||
if(!std::isxdigit(hex[3])) throwError(QJsonParseError::IllegalUTF8String);
|
||||
|
||||
quint16 w1 = 0;
|
||||
quint16 w2 = 0;
|
||||
|
||||
w1 |= (to_hex(hex[0]) << 12);
|
||||
w1 |= (to_hex(hex[1]) << 8);
|
||||
w1 |= (to_hex(hex[2]) << 4);
|
||||
w1 |= (to_hex(hex[3]));
|
||||
|
||||
hexChar.append(QChar(w1));
|
||||
|
||||
if((w1 & 0xfc00) == 0xdc00) {
|
||||
throwError(QJsonParseError::IllegalUTF8String);
|
||||
}
|
||||
|
||||
if((w1 & 0xfc00) == 0xd800) {
|
||||
// part of a surrogate pair
|
||||
if(p_ == end_ || *++p_ != '\\') { throwError(QJsonParseError::IllegalEscapeSequence); }
|
||||
if(p_ == end_ || *++p_ != 'u') { throwError(QJsonParseError::IllegalEscapeSequence); }
|
||||
|
||||
// convert \uXXXX escape sequences to UTF-8
|
||||
if(p_ == end_) { throwError(QJsonParseError::IllegalEscapeSequence); } hex[0] = *++p_;
|
||||
if(p_ == end_) { throwError(QJsonParseError::IllegalEscapeSequence); } hex[1] = *++p_;
|
||||
if(p_ == end_) { throwError(QJsonParseError::IllegalEscapeSequence); } hex[2] = *++p_;
|
||||
if(p_ == end_) { throwError(QJsonParseError::IllegalEscapeSequence); } hex[3] = *++p_;
|
||||
|
||||
if(!std::isxdigit(hex[0])) throwError(QJsonParseError::IllegalUTF8String);
|
||||
if(!std::isxdigit(hex[1])) throwError(QJsonParseError::IllegalUTF8String);
|
||||
if(!std::isxdigit(hex[2])) throwError(QJsonParseError::IllegalUTF8String);
|
||||
if(!std::isxdigit(hex[3])) throwError(QJsonParseError::IllegalUTF8String);
|
||||
|
||||
w2 |= (to_hex(hex[0]) << 12);
|
||||
w2 |= (to_hex(hex[1]) << 8);
|
||||
w2 |= (to_hex(hex[2]) << 4);
|
||||
w2 |= (to_hex(hex[3]));
|
||||
|
||||
hexChar.append(QChar(w2));
|
||||
}
|
||||
|
||||
s.append(hexChar.toUtf8());
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
s.append('\\');
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
s.append(*p_);
|
||||
}
|
||||
++p_;
|
||||
}
|
||||
|
||||
if(*p_ != Quote || p_ == end_) {
|
||||
throwError(QJsonParseError::UnterminatedString);
|
||||
}
|
||||
|
||||
++p_;
|
||||
|
||||
return QString::fromUtf8(s, s.size());
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: getNull
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue QJsonParser::getNull() {
|
||||
if(p_ == end_ || *p_++ != 'n') { throwError(QJsonParseError::IllegalValue); }
|
||||
if(p_ == end_ || *p_++ != 'u') { throwError(QJsonParseError::IllegalValue); }
|
||||
if(p_ == end_ || *p_++ != 'l') { throwError(QJsonParseError::IllegalValue); }
|
||||
if(p_ == end_ || *p_++ != 'l') { throwError(QJsonParseError::IllegalValue); }
|
||||
|
||||
return QJsonValue();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: getTrue
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue QJsonParser::getTrue() {
|
||||
if(p_ == end_ || *p_++ != 't') { throwError(QJsonParseError::IllegalValue); }
|
||||
if(p_ == end_ || *p_++ != 'r') { throwError(QJsonParseError::IllegalValue); }
|
||||
if(p_ == end_ || *p_++ != 'u') { throwError(QJsonParseError::IllegalValue); }
|
||||
if(p_ == end_ || *p_++ != 'e') { throwError(QJsonParseError::IllegalValue); }
|
||||
|
||||
return QJsonValue(true);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: getFalse
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue QJsonParser::getFalse() {
|
||||
if(p_ == end_ || *p_++ != 'f') { throwError(QJsonParseError::IllegalValue); }
|
||||
if(p_ == end_ || *p_++ != 'a') { throwError(QJsonParseError::IllegalValue); }
|
||||
if(p_ == end_ || *p_++ != 'l') { throwError(QJsonParseError::IllegalValue); }
|
||||
if(p_ == end_ || *p_++ != 's') { throwError(QJsonParseError::IllegalValue); }
|
||||
if(p_ == end_ || *p_++ != 'e') { throwError(QJsonParseError::IllegalValue); }
|
||||
|
||||
return QJsonValue(false);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: getNumber
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue QJsonParser::getNumber() {
|
||||
// JSON numbers fit the regex: -?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?
|
||||
|
||||
const char *const first = p_;
|
||||
|
||||
// -?
|
||||
if(p_ != end_ && *p_ == '-') {
|
||||
++p_;
|
||||
}
|
||||
|
||||
// (0|[1-9][0-9]*)
|
||||
if(p_ != end_) {
|
||||
if(*p_ >= '1' && *p_ <= '9') {
|
||||
while(p_ != end_ && std::isdigit(*p_)) {
|
||||
++p_;
|
||||
}
|
||||
} else if(*p_ == '0') {
|
||||
++p_;
|
||||
} else {
|
||||
throwError(QJsonParseError::IllegalNumber);
|
||||
}
|
||||
}
|
||||
|
||||
// (\.[0-9]+)?
|
||||
if(p_ != end_ && *p_ == '.') {
|
||||
++p_;
|
||||
if(!std::isdigit(*p_)) {
|
||||
throwError(QJsonParseError::IllegalNumber);
|
||||
}
|
||||
|
||||
while(p_ != end_ && std::isdigit(*p_)) {
|
||||
++p_;
|
||||
}
|
||||
}
|
||||
|
||||
// ([eE][+-]?[0-9]+)?
|
||||
if(p_ != end_ && (*p_ == 'e' || *p_ == 'E')) {
|
||||
++p_;
|
||||
if(p_ != end_ && (*p_ == '+' || *p_ == '-')) {
|
||||
++p_;
|
||||
}
|
||||
if(!std::isdigit(*p_)) {
|
||||
throwError(QJsonParseError::IllegalNumber);
|
||||
}
|
||||
while(p_ != end_ && std::isdigit(*p_)) {
|
||||
++p_;
|
||||
}
|
||||
}
|
||||
|
||||
if(p_ == end_) {
|
||||
throwError(QJsonParseError::TerminationByNumber);
|
||||
}
|
||||
|
||||
return QJsonValue(QByteArray::fromRawData(first, p_ - first).toDouble());
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: state
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonParseError QJsonParser::state() const {
|
||||
return state_;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: throwError
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonParser::throwError(QJsonParseError::ParseError e) {
|
||||
QJsonParseError err;
|
||||
err.error = e;
|
||||
err.offset = p_ - begin_;
|
||||
throw err;
|
||||
}
|
||||
|
||||
#endif
|
81
qjson4/QJsonParser.h
Normal file
81
qjson4/QJsonParser.h
Normal file
|
@ -0,0 +1,81 @@
|
|||
/*****************************************************************************
|
||||
* 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/>.
|
||||
*****************************************************************************/
|
||||
|
||||
// NOTE: this is not part of the "public" Qt API, so using this class directly
|
||||
// is not recomended
|
||||
|
||||
#ifndef QJSON_PARSER_H_
|
||||
#define QJSON_PARSER_H_
|
||||
|
||||
#include <QtCore/QtGlobal>
|
||||
|
||||
#if QT_VERSION < 0x050000
|
||||
|
||||
#include "QJsonParseError.h"
|
||||
#include <QPair>
|
||||
class QJsonRoot;
|
||||
class QJsonObject;
|
||||
class QJsonArray;
|
||||
class QJsonValue;
|
||||
|
||||
class QJsonParser {
|
||||
friend class QJsonDocument;
|
||||
|
||||
public:
|
||||
QJsonParser(const char *begin, const char *end);
|
||||
|
||||
public:
|
||||
QJsonRoot *parse();
|
||||
|
||||
public:
|
||||
QJsonParseError state() const;
|
||||
|
||||
private:
|
||||
static const char ArrayBegin = '[';
|
||||
static const char ArrayEnd = ']';
|
||||
static const char NameSeparator = ':';
|
||||
static const char ValueSeparator = ',';
|
||||
static const char ObjectBegin = '{';
|
||||
static const char ObjectEnd = '}';
|
||||
static const char Quote = '"';
|
||||
|
||||
private:
|
||||
char peek();
|
||||
QJsonObject *getObject();
|
||||
QJsonArray *getArray();
|
||||
QJsonValue getValue();
|
||||
QString getString();
|
||||
QJsonValue getTrue();
|
||||
QJsonValue getFalse();
|
||||
QJsonValue getNull();
|
||||
QJsonValue getNumber();
|
||||
QPair<QString, QJsonValue> getPair();
|
||||
|
||||
private:
|
||||
void throwError(QJsonParseError::ParseError e);
|
||||
|
||||
private:
|
||||
QJsonParseError state_;
|
||||
const char *const begin_;
|
||||
const char *const end_;
|
||||
const char * p_;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
1
qjson4/QJsonRoot
Normal file
1
qjson4/QJsonRoot
Normal file
|
@ -0,0 +1 @@
|
|||
#include "QJsonRoot.h"
|
45
qjson4/QJsonRoot.h
Normal file
45
qjson4/QJsonRoot.h
Normal file
|
@ -0,0 +1,45 @@
|
|||
/*****************************************************************************
|
||||
* 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/>.
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef QJSON_ROOT_H_
|
||||
#define QJSON_ROOT_H_
|
||||
|
||||
#include <QtCore/QtGlobal>
|
||||
|
||||
#if QT_VERSION < 0x050000
|
||||
|
||||
class QJsonObject;
|
||||
class QJsonArray;
|
||||
|
||||
class QJsonRoot {
|
||||
public:
|
||||
virtual ~QJsonRoot() {};
|
||||
|
||||
public:
|
||||
virtual QJsonRoot *clone() const = 0;
|
||||
|
||||
public:
|
||||
virtual QJsonArray *toArray() = 0;
|
||||
virtual QJsonObject *toObject() = 0;
|
||||
virtual const QJsonArray *toArray() const = 0;
|
||||
virtual const QJsonObject *toObject() const = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
1
qjson4/QJsonValue
Normal file
1
qjson4/QJsonValue
Normal file
|
@ -0,0 +1 @@
|
|||
#include "QJsonValue.h"
|
391
qjson4/QJsonValue.cpp
Normal file
391
qjson4/QJsonValue.cpp
Normal file
|
@ -0,0 +1,391 @@
|
|||
/*****************************************************************************
|
||||
* 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 "QJsonValue.h"
|
||||
#include "QJsonArray.h"
|
||||
#include "QJsonObject.h"
|
||||
|
||||
#if QT_VERSION < 0x050000
|
||||
#include <QtCore/QtAlgorithms>
|
||||
#include <QtCore/qmath.h>
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonValue
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue::QJsonValue(Type type) : type_(type) {
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonValue
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue::QJsonValue(bool b) : type_(Bool) {
|
||||
value_.b = b;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonValue
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue::QJsonValue(double n) : type_(Double) {
|
||||
value_.n = n;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonValue
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue::QJsonValue(const QString &s) : type_(String) {
|
||||
value_.s = new QString(s);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonValue
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue::QJsonValue(QLatin1String s) : type_(String) {
|
||||
value_.s = new QString(s);
|
||||
}
|
||||
|
||||
#ifndef QT_NO_CAST_FROM_ASCII
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonValue
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue::QJsonValue(const char *s) : type_(String) {
|
||||
value_.s = new QString(QString::fromUtf8(s));
|
||||
}
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonValue
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue::QJsonValue(const QJsonArray &a) : type_(Array) {
|
||||
value_.r = a.clone();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonValue
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue::QJsonValue(const QJsonObject &o) : type_(Object) {
|
||||
value_.r = o.clone();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonValue
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue::QJsonValue(int n) : type_(Double) {
|
||||
value_.n = n;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonValue
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue::QJsonValue(qint64 n) : type_(Double) {
|
||||
value_.n = n;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: QJsonValue
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue::QJsonValue(const QJsonValue &other) : type_(other.type_) {
|
||||
|
||||
switch(other.type_) {
|
||||
case Bool:
|
||||
value_.b = other.value_.b;
|
||||
break;
|
||||
case Double:
|
||||
value_.n = other.value_.n;
|
||||
break;
|
||||
case String:
|
||||
value_.s = new QString(*other.value_.s);
|
||||
break;
|
||||
case Array:
|
||||
case Object:
|
||||
value_.r = other.value_.r->clone();
|
||||
break;
|
||||
case Undefined:
|
||||
case Null:
|
||||
value_ = other.value_;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: ~QJsonValue
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue::~QJsonValue() {
|
||||
switch(type_) {
|
||||
case Null:
|
||||
case Bool:
|
||||
case Double:
|
||||
case Undefined:
|
||||
break;
|
||||
case String:
|
||||
delete value_.s;
|
||||
break;
|
||||
case Object:
|
||||
case Array:
|
||||
delete value_.r;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: operator=
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue &QJsonValue::operator=(const QJsonValue &other) {
|
||||
QJsonValue(other).swap(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: operator!=
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValue::operator!=(const QJsonValue &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: operator==
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValue::operator==(const QJsonValue &other) const {
|
||||
if(type_ == other.type_) {
|
||||
switch(type_) {
|
||||
case Null:
|
||||
return true;
|
||||
case Bool:
|
||||
return value_.b == other.value_.b;
|
||||
case Double:
|
||||
return value_.n == other.value_.n;
|
||||
case Undefined:
|
||||
return true;
|
||||
case String:
|
||||
return *value_.s == *other.value_.s;
|
||||
case Array:
|
||||
return *(value_.r->toArray()) == *(other.value_.r->toArray());
|
||||
case Object:
|
||||
return *(value_.r->toObject()) == *(other.value_.r->toObject());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isArray
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValue::isArray() const {
|
||||
return type_ == Array;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isBool
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValue::isBool() const {
|
||||
return type_ == Bool;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isDouble
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValue::isDouble() const {
|
||||
return type_ == Double;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isNull
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValue::isNull() const {
|
||||
return type_ == Null;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isObject
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValue::isObject() const {
|
||||
return type_ == Object;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isString
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValue::isString() const {
|
||||
return type_ == String;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isUndefined
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValue::isUndefined() const {
|
||||
return type_ == Undefined;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: type
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue::Type QJsonValue::type() const {
|
||||
return type_;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toArray
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray QJsonValue::toArray(const QJsonArray &defaultValue) const {
|
||||
if(isArray()) {
|
||||
return *(value_.r->toArray());
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toArray
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray QJsonValue::toArray() const {
|
||||
return toArray(QJsonArray());
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toBool
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValue::toBool(bool defaultValue) const {
|
||||
if(isBool()) {
|
||||
return value_.b;
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toDouble
|
||||
//------------------------------------------------------------------------------
|
||||
double QJsonValue::toDouble(double defaultValue) const {
|
||||
if(isDouble()) {
|
||||
return value_.n;
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toInt
|
||||
//------------------------------------------------------------------------------
|
||||
int QJsonValue::toInt(int defaultValue) const {
|
||||
if(isDouble() && qFloor(value_.n) == value_.n) {
|
||||
return value_.n;
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toObject
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject QJsonValue::toObject(const QJsonObject &defaultValue) const {
|
||||
if(isObject()) {
|
||||
return *(value_.r->toObject());
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toObject
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject QJsonValue::toObject() const {
|
||||
return toObject(QJsonObject());
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toString
|
||||
//------------------------------------------------------------------------------
|
||||
QString QJsonValue::toString(const QString &defaultValue) const {
|
||||
|
||||
if(isString()) {
|
||||
return *value_.s;
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toVariant
|
||||
//------------------------------------------------------------------------------
|
||||
QVariant QJsonValue::toVariant() const {
|
||||
switch(type_) {
|
||||
case Null:
|
||||
return QVariant();
|
||||
case Bool:
|
||||
return QVariant::fromValue(value_.b);
|
||||
case Double:
|
||||
return QVariant::fromValue(value_.n);
|
||||
case String:
|
||||
return QVariant::fromValue(*value_.s);
|
||||
case Array:
|
||||
return value_.r->toArray()->toVariantList();
|
||||
case Object:
|
||||
return value_.r->toObject()->toVariantMap();
|
||||
case Undefined:
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: fromVariant
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue QJsonValue::fromVariant(const QVariant &variant) {
|
||||
if(variant.isNull()) {
|
||||
return QJsonValue(Null);
|
||||
}
|
||||
|
||||
switch(variant.type()) {
|
||||
case QVariant::Bool:
|
||||
return QJsonValue(variant.toBool());
|
||||
case QVariant::Int:
|
||||
return QJsonValue(variant.toInt());
|
||||
case QVariant::Double:
|
||||
case QVariant::LongLong:
|
||||
case QVariant::ULongLong:
|
||||
case QVariant::UInt:
|
||||
return QJsonValue(variant.toDouble());
|
||||
case QVariant::String:
|
||||
return QJsonValue(variant.toString());
|
||||
case QVariant::List:
|
||||
return QJsonArray::fromVariantList(variant.toList());
|
||||
case QVariant::StringList:
|
||||
return QJsonArray::fromStringList(variant.toStringList());
|
||||
case QVariant::Map:
|
||||
return QJsonObject::fromVariantMap(variant.toMap());
|
||||
default:
|
||||
const QString s = variant.toString();
|
||||
if(!s.isEmpty()) {
|
||||
return QJsonValue(s);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return QJsonValue(Null);
|
||||
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: swap
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonValue::swap(QJsonValue &other) {
|
||||
qSwap(type_, other.type_);
|
||||
qSwap(value_, other.value_);
|
||||
}
|
||||
|
||||
#endif
|
120
qjson4/QJsonValue.h
Normal file
120
qjson4/QJsonValue.h
Normal file
|
@ -0,0 +1,120 @@
|
|||
/*****************************************************************************
|
||||
* 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/>.
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef QJSON_VALUE_H_
|
||||
#define QJSON_VALUE_H_
|
||||
|
||||
#include <QtCore/QtGlobal>
|
||||
|
||||
#if QT_VERSION >= 0x050000
|
||||
#include <QtCore/QJsonValue>
|
||||
#else
|
||||
|
||||
class QString;
|
||||
|
||||
#include <QtCore/QVariant>
|
||||
|
||||
class QJsonRoot;
|
||||
class QJsonArray;
|
||||
class QJsonObject;
|
||||
|
||||
class QJsonValue {
|
||||
public:
|
||||
enum Type {
|
||||
Null = 0x0,
|
||||
Bool = 0x1,
|
||||
Double = 0x2,
|
||||
String = 0x3,
|
||||
Array = 0x4,
|
||||
Object = 0x5,
|
||||
Undefined = 0x80
|
||||
};
|
||||
|
||||
public:
|
||||
QJsonValue(Type type = Null);
|
||||
QJsonValue(bool b);
|
||||
QJsonValue(double n);
|
||||
QJsonValue(int n);
|
||||
QJsonValue(qint64 n);
|
||||
QJsonValue(const QString &s);
|
||||
QJsonValue(QLatin1String s);
|
||||
#ifndef QT_NO_CAST_FROM_ASCII
|
||||
QJsonValue(const char *s);
|
||||
#endif
|
||||
QJsonValue(const QJsonArray &a);
|
||||
QJsonValue(const QJsonObject &o);
|
||||
QJsonValue(const QJsonValue &other);
|
||||
|
||||
~QJsonValue();
|
||||
|
||||
private:
|
||||
// to protect against incorrect usage due to passing a const char *
|
||||
QJsonValue(const void *);
|
||||
|
||||
public:
|
||||
QJsonValue &operator=(const QJsonValue &other);
|
||||
|
||||
public:
|
||||
bool operator!=(const QJsonValue &other) const;
|
||||
bool operator==(const QJsonValue &other) const;
|
||||
|
||||
public:
|
||||
bool isArray() const;
|
||||
bool isBool() const;
|
||||
bool isDouble() const;
|
||||
bool isNull() const;
|
||||
bool isObject() const;
|
||||
bool isString() const;
|
||||
bool isUndefined() const;
|
||||
|
||||
public:
|
||||
QJsonArray toArray(const QJsonArray &defaultValue) const;
|
||||
QJsonArray toArray() const;
|
||||
bool toBool(bool defaultValue = false) const;
|
||||
double toDouble(double defaultValue = 0) const;
|
||||
int toInt(int defaultValue = 0) const;
|
||||
QJsonObject toObject(const QJsonObject &defaultValue) const;
|
||||
QJsonObject toObject() const;
|
||||
QString toString(const QString &defaultValue = QString()) const;
|
||||
QVariant toVariant() const;
|
||||
|
||||
public:
|
||||
Type type() const;
|
||||
|
||||
public:
|
||||
static QJsonValue fromVariant(const QVariant &variant);
|
||||
|
||||
private:
|
||||
void swap(QJsonValue &other);
|
||||
|
||||
private:
|
||||
Type type_;
|
||||
|
||||
union ValueType {
|
||||
bool b;
|
||||
double n;
|
||||
QString *s;
|
||||
QJsonRoot *r; // OJsonObject or QJsonArray
|
||||
};
|
||||
|
||||
ValueType value_;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
1
qjson4/QJsonValueRef
Normal file
1
qjson4/QJsonValueRef
Normal file
|
@ -0,0 +1 @@
|
|||
#include "QJsonValueRef.h"
|
228
qjson4/QJsonValueRef.cpp
Normal file
228
qjson4/QJsonValueRef.cpp
Normal file
|
@ -0,0 +1,228 @@
|
|||
/*****************************************************************************
|
||||
* 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 "QJsonValueRef.h"
|
||||
|
||||
#if QT_VERSION < 0x050000
|
||||
|
||||
#include "QJsonArray.h"
|
||||
#include "QJsonObject.h"
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValueRef::QJsonValueRef(QJsonArray *array, int idx) : p_(array), index_(idx) {
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValueRef::QJsonValueRef(QJsonObject *object, const QString &key) : p_(object), index_(0), key_(key) {
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValueRef::operator QJsonValue() const {
|
||||
return toValue();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValueRef &QJsonValueRef::operator=(const QJsonValue &val) {
|
||||
|
||||
if(QJsonObject *const o = p_->toObject()) {
|
||||
o->values_[key_] = val;
|
||||
} else if(QJsonArray *const a = p_->toArray()) {
|
||||
a->values_[index_] = val;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name:
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValueRef &QJsonValueRef::operator=(const QJsonValueRef &ref) {
|
||||
|
||||
if(QJsonObject *const o = p_->toObject()) {
|
||||
o->values_[key_] = ref;
|
||||
} else if(QJsonArray *const a = p_->toArray()) {
|
||||
a->values_[index_] = ref;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: type
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue::Type QJsonValueRef::type() const {
|
||||
return toValue().type();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isNull
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValueRef::isNull() const {
|
||||
return toValue().isNull();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isBool
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValueRef::isBool() const {
|
||||
return toValue().isBool();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isDouble
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValueRef::isDouble() const {
|
||||
return toValue().isDouble();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isString
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValueRef::isString() const {
|
||||
return toValue().isString();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isArray
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValueRef::isArray() const {
|
||||
return toValue().isArray();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isObject
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValueRef::isObject() const {
|
||||
return toValue().isObject();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: isUndefined
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValueRef::isUndefined() const {
|
||||
return toValue().isUndefined();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toBool
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValueRef::toBool() const {
|
||||
return toValue().toBool();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toDouble
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
double QJsonValueRef::toDouble() const {
|
||||
return toValue().toDouble();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toInt
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
int QJsonValueRef::toInt(int defaultValue) const {
|
||||
return toValue().toInt(defaultValue);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toString
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
QString QJsonValueRef::toString() const {
|
||||
return toValue().toString();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toArray
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonArray QJsonValueRef::toArray() const {
|
||||
return toValue().toArray();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toObject
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonObject QJsonValueRef::toObject() const {
|
||||
return toValue().toObject();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: operator==
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValueRef::operator==(const QJsonValue &other) const {
|
||||
return toValue() == other;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: operator!=
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
bool QJsonValueRef::operator!=(const QJsonValue &other) const {
|
||||
return toValue() != other;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: toValue
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
QJsonValue QJsonValueRef::toValue() const {
|
||||
if(QJsonObject *const o = p_->toObject()) {
|
||||
return o->values_[key_];
|
||||
} else if(QJsonArray *const a = p_->toArray()) {
|
||||
return a->values_[index_];
|
||||
}
|
||||
|
||||
return QJsonValue();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Name: swap
|
||||
// Desc:
|
||||
//------------------------------------------------------------------------------
|
||||
void QJsonValueRef::swap(QJsonValueRef &other) {
|
||||
qSwap(p_, other.p_);
|
||||
qSwap(key_, other.key_);
|
||||
qSwap(index_, other.index_);
|
||||
}
|
||||
|
||||
#endif
|
79
qjson4/QJsonValueRef.h
Normal file
79
qjson4/QJsonValueRef.h
Normal file
|
@ -0,0 +1,79 @@
|
|||
/*****************************************************************************
|
||||
* 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/>.
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef QJSON_VALUEREF_H_
|
||||
#define QJSON_VALUEREF_H_
|
||||
|
||||
#include <QtCore/QtGlobal>
|
||||
|
||||
#if QT_VERSION >= 0x050000
|
||||
#include <QtCore/QJsonValueRef>
|
||||
#else
|
||||
|
||||
#include "QJsonValue.h"
|
||||
class QJsonRoot;
|
||||
|
||||
class QJsonValueRef {
|
||||
public:
|
||||
QJsonValueRef(QJsonArray *array, int idx);
|
||||
|
||||
// slight variant from official APIs implementation
|
||||
QJsonValueRef(QJsonObject *object, const QString &key);
|
||||
|
||||
public:
|
||||
operator QJsonValue() const;
|
||||
|
||||
public:
|
||||
QJsonValueRef &operator=(const QJsonValue &val);
|
||||
QJsonValueRef &operator=(const QJsonValueRef &val);
|
||||
|
||||
public:
|
||||
QJsonValue::Type type() const;
|
||||
bool isNull() const;
|
||||
bool isBool() const;
|
||||
bool isDouble() const;
|
||||
bool isString() const;
|
||||
bool isArray() const;
|
||||
bool isObject() const;
|
||||
bool isUndefined() const;
|
||||
|
||||
public:
|
||||
bool toBool() const;
|
||||
double toDouble() const;
|
||||
QString toString() const;
|
||||
QJsonArray toArray() const;
|
||||
QJsonObject toObject() const;
|
||||
int toInt(int defaultValue = 0) const;
|
||||
|
||||
public:
|
||||
bool operator==(const QJsonValue &other) const;
|
||||
bool operator!=(const QJsonValue &other) const;
|
||||
|
||||
private:
|
||||
QJsonValue toValue() const;
|
||||
void swap(QJsonValueRef &other);
|
||||
|
||||
private:
|
||||
QJsonRoot *p_;
|
||||
int index_;
|
||||
QString key_;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue