From af86e70f4b2a2fc21bd02c19a1853aa3422939fe Mon Sep 17 00:00:00 2001 From: Syping Date: Tue, 25 Aug 2020 15:18:23 +0200 Subject: [PATCH] [skip ci] prettify qjson4 output aswell --- qjson4/QJsonDocument.cpp | 367 ++++++++++++++++++++------------------- qjson4/QJsonDocument.h | 74 ++++---- 2 files changed, 224 insertions(+), 217 deletions(-) diff --git a/qjson4/QJsonDocument.cpp b/qjson4/QJsonDocument.cpp index 9d503c3..857f3f6 100644 --- a/qjson4/QJsonDocument.cpp +++ b/qjson4/QJsonDocument.cpp @@ -39,45 +39,45 @@ QJsonDocument::QJsonDocument() : root_(0) { // Name: QJsonDocument //------------------------------------------------------------------------------ QJsonDocument::QJsonDocument(const QJsonObject &object) : root_(0) { - setObject(object); + setObject(object); } //------------------------------------------------------------------------------ // Name: QJsonDocument //------------------------------------------------------------------------------ QJsonDocument::QJsonDocument(const QJsonArray &array) : root_(0) { - setArray(array); + setArray(array); } //------------------------------------------------------------------------------ // Name: QJsonDocument //------------------------------------------------------------------------------ QJsonDocument::QJsonDocument(const QJsonDocument &other) : root_(0) { - if(other.root_) { - root_ = other.root_->clone(); - } + if(other.root_) { + root_ = other.root_->clone(); + } } //------------------------------------------------------------------------------ // Name: ~QJsonDocument //------------------------------------------------------------------------------ QJsonDocument::~QJsonDocument() { - delete root_; + delete root_; } //------------------------------------------------------------------------------ // Name: operator= //------------------------------------------------------------------------------ QJsonDocument &QJsonDocument::operator=(const QJsonDocument &other) { - QJsonDocument(other).swap(*this); - return *this; + QJsonDocument(other).swap(*this); + return *this; } //------------------------------------------------------------------------------ // Name: operator!= //------------------------------------------------------------------------------ bool QJsonDocument::operator!=(const QJsonDocument &other) const { - return !(*this == other); + return !(*this == other); } //------------------------------------------------------------------------------ @@ -85,30 +85,30 @@ bool QJsonDocument::operator!=(const QJsonDocument &other) const { //------------------------------------------------------------------------------ bool QJsonDocument::operator==(const QJsonDocument &other) const { - if(isArray() && other.isArray()) { - return array() == other.array(); - } + if(isArray() && other.isArray()) { + return array() == other.array(); + } - if(isObject() && other.isObject()) { - return object() == other.object(); - } + if(isObject() && other.isObject()) { + return object() == other.object(); + } - if(isEmpty() && other.isEmpty()) { - return true; - } + if(isEmpty() && other.isEmpty()) { + return true; + } - if(isNull() && other.isNull()) { - return true; - } + if(isNull() && other.isNull()) { + return true; + } - return false; + return false; } //------------------------------------------------------------------------------ // Name: isArray //------------------------------------------------------------------------------ bool QJsonDocument::isArray() const { - return root_ && root_->toArray(); + return root_ && root_->toArray(); } //------------------------------------------------------------------------------ @@ -116,56 +116,56 @@ bool QJsonDocument::isArray() const { //------------------------------------------------------------------------------ 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 + // 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_; + return !root_; } //------------------------------------------------------------------------------ // Name: isNull //------------------------------------------------------------------------------ bool QJsonDocument::isNull() const { - return !root_; + return !root_; } //------------------------------------------------------------------------------ // Name: isObject //------------------------------------------------------------------------------ bool QJsonDocument::isObject() const { - return root_ && root_->toObject(); + return root_ && root_->toObject(); } //------------------------------------------------------------------------------ // Name: setArray //------------------------------------------------------------------------------ void QJsonDocument::setArray(const QJsonArray &array) { - setRoot(array); + setRoot(array); } //------------------------------------------------------------------------------ // Name: setObject //------------------------------------------------------------------------------ void QJsonDocument::setObject(const QJsonObject &object) { - setRoot(object); + setRoot(object); } //------------------------------------------------------------------------------ // Name: setRoot //------------------------------------------------------------------------------ void QJsonDocument::setRoot(const QJsonRoot &root) { - delete root_; - root_ = root.clone(); + delete root_; + root_ = root.clone(); } //------------------------------------------------------------------------------ // Name: toBinaryData //------------------------------------------------------------------------------ QByteArray QJsonDocument::toBinaryData() const { - QByteArray r; - // TODO(eteran): implement this - return r; + QByteArray r; + // TODO(eteran): implement this + return r; } //------------------------------------------------------------------------------ @@ -173,100 +173,107 @@ QByteArray QJsonDocument::toBinaryData() const { //------------------------------------------------------------------------------ QString QJsonDocument::escapeString(const QString &s) const { - QString r; + 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; - } - } + 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; + return r; } //------------------------------------------------------------------------------ // Name: toJson //------------------------------------------------------------------------------ -QString QJsonDocument::toJson(const QJsonValue &v, JsonFormat format) const { +QString QJsonDocument::toJson(const QJsonValue &v, JsonFormat format, int intend) const { - QString b; - QTextStream ss(&b, QIODevice::WriteOnly | QIODevice::Text); + QString b; + QTextStream ss(&b, QIODevice::WriteOnly | QIODevice::Text); + bool compact = (format == JsonFormat::Compact); - switch(v.type()) { - case QJsonValue::Null: - ss << "null"; - break; - case QJsonValue::Bool: - ss << (v.toBool() ? "true" : "false"); - break; - case QJsonValue::Double: - { - double d = v.toDouble (); - if (qIsFinite(d)) { - // +2 to format to ensure the expected precision - ss << QByteArray::number(d, 'g', 15 + 2); // ::digits10 is 15 - } else { - ss << "null"; // +INF || -INF || NaN (see RFC4627#section2.4) - } - } - break; - case QJsonValue::String: - ss << '"' << escapeString(v.toString()) << '"'; - break; - case QJsonValue::Array: - { - const QJsonArray a = v.toArray(); - ss << "["; - if(!a.empty()) { - QJsonArray::const_iterator it = a.begin(); - QJsonArray::const_iterator e = a.end(); + switch(v.type()) { + case QJsonValue::Null: + ss << "null"; + break; + case QJsonValue::Bool: + ss << (v.toBool() ? "true" : "false"); + break; + case QJsonValue::Double: + { + double d = v.toDouble (); + if (qIsFinite(d)) { + // +2 to format to ensure the expected precision + ss << QByteArray::number(d, 'g', 15 + 2); // ::digits10 is 15 + } else { + ss << "null"; // +INF || -INF || NaN (see RFC4627#section2.4) + } + } + break; + case QJsonValue::String: + ss << '"' << escapeString(v.toString()) << '"'; + break; + case QJsonValue::Array: + { + const QJsonArray a = v.toArray(); + ss << (compact ? "[" : "[\n"); + if(!a.empty()) { + QJsonArray::const_iterator it = a.begin(); + QJsonArray::const_iterator e = a.end(); - ss << toJson(*it++, format); + if (!compact) ss << QByteArray(4*intend, ' '); + ss << toJson(*it++, format, intend+1); - 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(); + for(;it != e; ++it) { + ss << (compact ? "," : ",\n"); + if (!compact) ss << QByteArray(4*intend, ' '); + ss << toJson(*it, format, intend+1); + } + } + intend--; + ss << (compact ? "]" : QString("\n%1]").arg(QString(4*intend, ' '))); + } + break; + case QJsonValue::Object: + { + const QJsonObject o = v.toObject(); + ss << (compact ? "{" : "{\n"); + if(!o.empty()) { + QJsonObject::const_iterator it = o.begin(); + QJsonObject::const_iterator e = o.end(); - 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; - } + if (!compact) ss << QByteArray(4*intend, ' '); + ss << '"' << escapeString(it.key()) << (compact ? "\":" : "\": ") << toJson(it.value(), format, intend+1); + ++it; + for(;it != e; ++it) { + ss << (compact ? "," : ",\n"); + if (!compact) ss << QByteArray(4*intend, ' '); + ss << '"' << escapeString(it.key()) << (compact ? "\":" : "\": ") << toJson(it.value(), format, intend+1); + } + } + intend--; + ss << (compact ? "}" : QString("\n%1}").arg(QString(4*intend, ' '))); + } + break; + case QJsonValue::Undefined: + Q_ASSERT(0); + break; + } - return b; + return b; } //------------------------------------------------------------------------------ @@ -274,19 +281,19 @@ QString QJsonDocument::toJson(const QJsonValue &v, JsonFormat format) const { //------------------------------------------------------------------------------ QByteArray QJsonDocument::toJson(JsonFormat format) const { - Q_UNUSED(format); + Q_UNUSED(format); - if(isArray()) { - QString s = toJson(array(), format); - return s.toUtf8(); - } + if(isArray()) { + QString s = toJson(array(), format); + return s.toUtf8(); + } - if(isObject()) { - QString s = toJson(object(), format); - return s.toUtf8(); - } + if(isObject()) { + QString s = toJson(object(), format); + return s.toUtf8(); + } - return QByteArray(); + return QByteArray(); } //------------------------------------------------------------------------------ @@ -294,17 +301,17 @@ QByteArray QJsonDocument::toJson(JsonFormat format) const { //------------------------------------------------------------------------------ QVariant QJsonDocument::toVariant() const { - if(!isEmpty()) { - if(QJsonObject *const object = root_->toObject()) { - return object->toVariantMap(); - } + if(!isEmpty()) { + if(QJsonObject *const object = root_->toObject()) { + return object->toVariantMap(); + } - if(QJsonArray *const array = root_->toArray()) { - return array->toVariantList(); - } - } + if(QJsonArray *const array = root_->toArray()) { + return array->toVariantList(); + } + } - return QVariant(); + return QVariant(); } //------------------------------------------------------------------------------ @@ -312,13 +319,13 @@ QVariant QJsonDocument::toVariant() const { //------------------------------------------------------------------------------ QJsonArray QJsonDocument::array() const { - if(!isEmpty()) { - if(QJsonArray *const array = root_->toArray()) { - return *array; - } - } + if(!isEmpty()) { + if(QJsonArray *const array = root_->toArray()) { + return *array; + } + } - return QJsonArray(); + return QJsonArray(); } //------------------------------------------------------------------------------ @@ -326,54 +333,54 @@ QJsonArray QJsonDocument::array() const { //------------------------------------------------------------------------------ QJsonObject QJsonDocument::object() const { - if(!isEmpty()) { - if(QJsonObject *const object = root_->toObject()) { - return *object; - } - } + if(!isEmpty()) { + if(QJsonObject *const object = root_->toObject()) { + return *object; + } + } - return QJsonObject(); + return QJsonObject(); } //------------------------------------------------------------------------------ // Name: rawData //------------------------------------------------------------------------------ const char *QJsonDocument::rawData(int *size) const { - Q_UNUSED(size); - // TODO(eteran): implement this - return 0; + 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); + Q_UNUSED(data); + Q_UNUSED(validation); - QJsonDocument doc; - // TODO(eteran): implement this - return doc; + QJsonDocument doc; + // TODO(eteran): implement this + return doc; } //------------------------------------------------------------------------------ // Name: fromJson //------------------------------------------------------------------------------ QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error) { - QJsonDocument doc; + QJsonDocument doc; - const char *const begin = json.constData(); - const char *const end = begin + json.size(); + const char *const begin = json.constData(); + const char *const end = begin + json.size(); - QJsonParser parser(begin, end); + QJsonParser parser(begin, end); - doc.root_ = parser.parse(); + doc.root_ = parser.parse(); - if(error) { - *error = parser.state(); - } + if(error) { + *error = parser.state(); + } - return doc; + return doc; } //------------------------------------------------------------------------------ @@ -381,10 +388,10 @@ QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *e //------------------------------------------------------------------------------ QJsonDocument QJsonDocument::fromRawData(const char *data, int size, DataValidation validation) { - // data has to be aligned to a 4 byte boundary. - Q_ASSERT(!(reinterpret_cast(data) % 3)); + // data has to be aligned to a 4 byte boundary. + Q_ASSERT(!(reinterpret_cast(data) % 3)); - return fromBinaryData(QByteArray::fromRawData(data, size), validation); + return fromBinaryData(QByteArray::fromRawData(data, size), validation); } //------------------------------------------------------------------------------ @@ -392,26 +399,26 @@ QJsonDocument QJsonDocument::fromRawData(const char *data, int size, DataValidat //------------------------------------------------------------------------------ QJsonDocument QJsonDocument::fromVariant(const QVariant &variant) { - QJsonDocument doc; + 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())); - } + 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; + return doc; } //------------------------------------------------------------------------------ // Name: swap //------------------------------------------------------------------------------ void QJsonDocument::swap(QJsonDocument &other) { - qSwap(root_, other.root_); + qSwap(root_, other.root_); } #endif diff --git a/qjson4/QJsonDocument.h b/qjson4/QJsonDocument.h index 5b1e17a..79a0afd 100644 --- a/qjson4/QJsonDocument.h +++ b/qjson4/QJsonDocument.h @@ -36,66 +36,66 @@ class QJsonRoot; class QJsonDocument { public: - enum DataValidation { - Validate = 0, - BypassValidation = 1 - }; + enum DataValidation { + Validate = 0, + BypassValidation = 1 + }; - enum JsonFormat { - Indented, - Compact - }; + enum JsonFormat { + Indented, + Compact + }; public: - QJsonDocument(); - QJsonDocument(const QJsonObject &object); - QJsonDocument(const QJsonArray &array); - QJsonDocument(const QJsonDocument &other); - ~QJsonDocument(); + QJsonDocument(); + QJsonDocument(const QJsonObject &object); + QJsonDocument(const QJsonArray &array); + QJsonDocument(const QJsonDocument &other); + ~QJsonDocument(); public: - QJsonDocument &operator=(const QJsonDocument &other); + QJsonDocument &operator=(const QJsonDocument &other); public: - bool operator!=(const QJsonDocument &other) const; - bool operator==(const QJsonDocument &other) const; + 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; + 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; + 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; + QJsonArray array() const; + QJsonObject object() const; + const char *rawData(int *size) const; public: - void setArray(const QJsonArray &array); - void setObject(const QJsonObject &object); + 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); + 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; + void setRoot(const QJsonRoot &root); + QString toJson(const QJsonValue &v, JsonFormat format, int intend = 1) const; + QString escapeString(const QString &s) const; private: - void swap(QJsonDocument &other); + void swap(QJsonDocument &other); private: - QJsonRoot *root_; + QJsonRoot *root_; }; #endif