[skip ci] prettify qjson4 output aswell

This commit is contained in:
Syping 2020-08-25 15:18:23 +02:00
parent 2bf6bb5630
commit af86e70f4b
2 changed files with 224 additions and 217 deletions

View File

@ -39,45 +39,45 @@ QJsonDocument::QJsonDocument() : root_(0) {
// Name: QJsonDocument // Name: QJsonDocument
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
QJsonDocument::QJsonDocument(const QJsonObject &object) : root_(0) { QJsonDocument::QJsonDocument(const QJsonObject &object) : root_(0) {
setObject(object); setObject(object);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: QJsonDocument // Name: QJsonDocument
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
QJsonDocument::QJsonDocument(const QJsonArray &array) : root_(0) { QJsonDocument::QJsonDocument(const QJsonArray &array) : root_(0) {
setArray(array); setArray(array);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: QJsonDocument // Name: QJsonDocument
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
QJsonDocument::QJsonDocument(const QJsonDocument &other) : root_(0) { QJsonDocument::QJsonDocument(const QJsonDocument &other) : root_(0) {
if(other.root_) { if(other.root_) {
root_ = other.root_->clone(); root_ = other.root_->clone();
} }
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: ~QJsonDocument // Name: ~QJsonDocument
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
QJsonDocument::~QJsonDocument() { QJsonDocument::~QJsonDocument() {
delete root_; delete root_;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: operator= // Name: operator=
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
QJsonDocument &QJsonDocument::operator=(const QJsonDocument &other) { QJsonDocument &QJsonDocument::operator=(const QJsonDocument &other) {
QJsonDocument(other).swap(*this); QJsonDocument(other).swap(*this);
return *this; return *this;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: operator!= // Name: operator!=
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
bool QJsonDocument::operator!=(const QJsonDocument &other) const { 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 { bool QJsonDocument::operator==(const QJsonDocument &other) const {
if(isArray() && other.isArray()) { if(isArray() && other.isArray()) {
return array() == other.array(); return array() == other.array();
} }
if(isObject() && other.isObject()) { if(isObject() && other.isObject()) {
return object() == other.object(); return object() == other.object();
} }
if(isEmpty() && other.isEmpty()) { if(isEmpty() && other.isEmpty()) {
return true; return true;
} }
if(isNull() && other.isNull()) { if(isNull() && other.isNull()) {
return true; return true;
} }
return false; return false;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: isArray // Name: isArray
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
bool QJsonDocument::isArray() const { bool QJsonDocument::isArray() const {
return root_ && root_->toArray(); return root_ && root_->toArray();
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -116,56 +116,56 @@ bool QJsonDocument::isArray() const {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
bool QJsonDocument::isEmpty() const { bool QJsonDocument::isEmpty() const {
// TODO(eteran): figure out the rules here that Qt5 uses // TODO(eteran): figure out the rules here that Qt5 uses
// it *looks* like they define empty as being NULL // it *looks* like they define empty as being NULL
// which is obviously different than this // which is obviously different than this
return !root_; return !root_;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: isNull // Name: isNull
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
bool QJsonDocument::isNull() const { bool QJsonDocument::isNull() const {
return !root_; return !root_;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: isObject // Name: isObject
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
bool QJsonDocument::isObject() const { bool QJsonDocument::isObject() const {
return root_ && root_->toObject(); return root_ && root_->toObject();
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: setArray // Name: setArray
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void QJsonDocument::setArray(const QJsonArray &array) { void QJsonDocument::setArray(const QJsonArray &array) {
setRoot(array); setRoot(array);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: setObject // Name: setObject
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void QJsonDocument::setObject(const QJsonObject &object) { void QJsonDocument::setObject(const QJsonObject &object) {
setRoot(object); setRoot(object);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: setRoot // Name: setRoot
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void QJsonDocument::setRoot(const QJsonRoot &root) { void QJsonDocument::setRoot(const QJsonRoot &root) {
delete root_; delete root_;
root_ = root.clone(); root_ = root.clone();
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: toBinaryData // Name: toBinaryData
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
QByteArray QJsonDocument::toBinaryData() const { QByteArray QJsonDocument::toBinaryData() const {
QByteArray r; QByteArray r;
// TODO(eteran): implement this // TODO(eteran): implement this
return r; return r;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -173,100 +173,107 @@ QByteArray QJsonDocument::toBinaryData() const {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
QString QJsonDocument::escapeString(const QString &s) const { QString QJsonDocument::escapeString(const QString &s) const {
QString r; QString r;
Q_FOREACH(QChar ch, s) { Q_FOREACH(QChar ch, s) {
switch(ch.toLatin1()) { switch(ch.toLatin1()) {
case '\"': r.append("\\\""); break; case '\"': r.append("\\\""); break;
case '\\': r.append("\\\\"); break; case '\\': r.append("\\\\"); break;
#if 0 #if 0
case '/': r.append("\\/"); break; case '/': r.append("\\/"); break;
#endif #endif
case '\b': r.append("\\b"); break; case '\b': r.append("\\b"); break;
case '\f': r.append("\\f"); break; case '\f': r.append("\\f"); break;
case '\n': r.append("\\n"); break; case '\n': r.append("\\n"); break;
case '\r': r.append("\\r"); break; case '\r': r.append("\\r"); break;
case '\t': r.append("\\t"); break; case '\t': r.append("\\t"); break;
default: default:
r += ch; r += ch;
break; break;
} }
} }
return r; return r;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: toJson // Name: toJson
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
QString QJsonDocument::toJson(const QJsonValue &v, JsonFormat format) const { QString QJsonDocument::toJson(const QJsonValue &v, JsonFormat format, int intend) const {
QString b; QString b;
QTextStream ss(&b, QIODevice::WriteOnly | QIODevice::Text); QTextStream ss(&b, QIODevice::WriteOnly | QIODevice::Text);
bool compact = (format == JsonFormat::Compact);
switch(v.type()) { switch(v.type()) {
case QJsonValue::Null: case QJsonValue::Null:
ss << "null"; ss << "null";
break; break;
case QJsonValue::Bool: case QJsonValue::Bool:
ss << (v.toBool() ? "true" : "false"); ss << (v.toBool() ? "true" : "false");
break; break;
case QJsonValue::Double: case QJsonValue::Double:
{ {
double d = v.toDouble (); double d = v.toDouble ();
if (qIsFinite(d)) { if (qIsFinite(d)) {
// +2 to format to ensure the expected precision // +2 to format to ensure the expected precision
ss << QByteArray::number(d, 'g', 15 + 2); // ::digits10 is 15 ss << QByteArray::number(d, 'g', 15 + 2); // ::digits10 is 15
} else { } else {
ss << "null"; // +INF || -INF || NaN (see RFC4627#section2.4) ss << "null"; // +INF || -INF || NaN (see RFC4627#section2.4)
} }
} }
break; break;
case QJsonValue::String: case QJsonValue::String:
ss << '"' << escapeString(v.toString()) << '"'; ss << '"' << escapeString(v.toString()) << '"';
break; break;
case QJsonValue::Array: case QJsonValue::Array:
{ {
const QJsonArray a = v.toArray(); const QJsonArray a = v.toArray();
ss << "["; ss << (compact ? "[" : "[\n");
if(!a.empty()) { if(!a.empty()) {
QJsonArray::const_iterator it = a.begin(); QJsonArray::const_iterator it = a.begin();
QJsonArray::const_iterator e = a.end(); 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) { for(;it != e; ++it) {
ss << ','; ss << (compact ? "," : ",\n");
ss << toJson(*it, format); if (!compact) ss << QByteArray(4*intend, ' ');
} ss << toJson(*it, format, intend+1);
} }
ss << "]"; }
} intend--;
break; ss << (compact ? "]" : QString("\n%1]").arg(QString(4*intend, ' ')));
case QJsonValue::Object: }
{ break;
const QJsonObject o = v.toObject(); case QJsonValue::Object:
ss << "{"; {
if(!o.empty()) { const QJsonObject o = v.toObject();
QJsonObject::const_iterator it = o.begin(); ss << (compact ? "{" : "{\n");
QJsonObject::const_iterator e = o.end(); if(!o.empty()) {
QJsonObject::const_iterator it = o.begin();
QJsonObject::const_iterator e = o.end();
ss << '"' << escapeString(it.key()) << "\": " << toJson(it.value(), format); if (!compact) ss << QByteArray(4*intend, ' ');
++it; ss << '"' << escapeString(it.key()) << (compact ? "\":" : "\": ") << toJson(it.value(), format, intend+1);
for(;it != e; ++it) { ++it;
ss << ','; for(;it != e; ++it) {
ss << '"' << escapeString(it.key()) << "\": " << toJson(it.value(), format); ss << (compact ? "," : ",\n");
} if (!compact) ss << QByteArray(4*intend, ' ');
} ss << '"' << escapeString(it.key()) << (compact ? "\":" : "\": ") << toJson(it.value(), format, intend+1);
ss << "}"; }
} }
break; intend--;
case QJsonValue::Undefined: ss << (compact ? "}" : QString("\n%1}").arg(QString(4*intend, ' ')));
Q_ASSERT(0); }
break; 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 { QByteArray QJsonDocument::toJson(JsonFormat format) const {
Q_UNUSED(format); Q_UNUSED(format);
if(isArray()) { if(isArray()) {
QString s = toJson(array(), format); QString s = toJson(array(), format);
return s.toUtf8(); return s.toUtf8();
} }
if(isObject()) { if(isObject()) {
QString s = toJson(object(), format); QString s = toJson(object(), format);
return s.toUtf8(); return s.toUtf8();
} }
return QByteArray(); return QByteArray();
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -294,17 +301,17 @@ QByteArray QJsonDocument::toJson(JsonFormat format) const {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
QVariant QJsonDocument::toVariant() const { QVariant QJsonDocument::toVariant() const {
if(!isEmpty()) { if(!isEmpty()) {
if(QJsonObject *const object = root_->toObject()) { if(QJsonObject *const object = root_->toObject()) {
return object->toVariantMap(); return object->toVariantMap();
} }
if(QJsonArray *const array = root_->toArray()) { if(QJsonArray *const array = root_->toArray()) {
return array->toVariantList(); return array->toVariantList();
} }
} }
return QVariant(); return QVariant();
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -312,13 +319,13 @@ QVariant QJsonDocument::toVariant() const {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
QJsonArray QJsonDocument::array() const { QJsonArray QJsonDocument::array() const {
if(!isEmpty()) { if(!isEmpty()) {
if(QJsonArray *const array = root_->toArray()) { if(QJsonArray *const array = root_->toArray()) {
return *array; return *array;
} }
} }
return QJsonArray(); return QJsonArray();
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -326,54 +333,54 @@ QJsonArray QJsonDocument::array() const {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
QJsonObject QJsonDocument::object() const { QJsonObject QJsonDocument::object() const {
if(!isEmpty()) { if(!isEmpty()) {
if(QJsonObject *const object = root_->toObject()) { if(QJsonObject *const object = root_->toObject()) {
return *object; return *object;
} }
} }
return QJsonObject(); return QJsonObject();
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: rawData // Name: rawData
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
const char *QJsonDocument::rawData(int *size) const { const char *QJsonDocument::rawData(int *size) const {
Q_UNUSED(size); Q_UNUSED(size);
// TODO(eteran): implement this // TODO(eteran): implement this
return 0; return 0;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: fromBinaryData // Name: fromBinaryData
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
QJsonDocument QJsonDocument::fromBinaryData(const QByteArray &data, DataValidation validation) { QJsonDocument QJsonDocument::fromBinaryData(const QByteArray &data, DataValidation validation) {
Q_UNUSED(data); Q_UNUSED(data);
Q_UNUSED(validation); Q_UNUSED(validation);
QJsonDocument doc; QJsonDocument doc;
// TODO(eteran): implement this // TODO(eteran): implement this
return doc; return doc;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: fromJson // Name: fromJson
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error) { QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error) {
QJsonDocument doc; QJsonDocument doc;
const char *const begin = json.constData(); const char *const begin = json.constData();
const char *const end = begin + json.size(); 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) { if(error) {
*error = parser.state(); *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) { QJsonDocument QJsonDocument::fromRawData(const char *data, int size, DataValidation validation) {
// data has to be aligned to a 4 byte boundary. // data has to be aligned to a 4 byte boundary.
Q_ASSERT(!(reinterpret_cast<quintptr>(data) % 3)); Q_ASSERT(!(reinterpret_cast<quintptr>(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 QJsonDocument::fromVariant(const QVariant &variant) {
QJsonDocument doc; QJsonDocument doc;
if (variant.type() == QVariant::Map) { if (variant.type() == QVariant::Map) {
doc.setObject(QJsonObject::fromVariantMap(variant.toMap())); doc.setObject(QJsonObject::fromVariantMap(variant.toMap()));
} else if (variant.type() == QVariant::Hash) { } else if (variant.type() == QVariant::Hash) {
doc.setObject(QJsonObject::fromVariantHash(variant.toHash())); doc.setObject(QJsonObject::fromVariantHash(variant.toHash()));
} else if (variant.type() == QVariant::List) { } else if (variant.type() == QVariant::List) {
doc.setArray(QJsonArray::fromVariantList(variant.toList())); doc.setArray(QJsonArray::fromVariantList(variant.toList()));
} else if (variant.type() == QVariant::StringList) { } else if (variant.type() == QVariant::StringList) {
doc.setArray(QJsonArray::fromStringList(variant.toStringList())); doc.setArray(QJsonArray::fromStringList(variant.toStringList()));
} }
return doc; return doc;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Name: swap // Name: swap
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
void QJsonDocument::swap(QJsonDocument &other) { void QJsonDocument::swap(QJsonDocument &other) {
qSwap(root_, other.root_); qSwap(root_, other.root_);
} }
#endif #endif

View File

@ -36,66 +36,66 @@ class QJsonRoot;
class QJsonDocument { class QJsonDocument {
public: public:
enum DataValidation { enum DataValidation {
Validate = 0, Validate = 0,
BypassValidation = 1 BypassValidation = 1
}; };
enum JsonFormat { enum JsonFormat {
Indented, Indented,
Compact Compact
}; };
public: public:
QJsonDocument(); QJsonDocument();
QJsonDocument(const QJsonObject &object); QJsonDocument(const QJsonObject &object);
QJsonDocument(const QJsonArray &array); QJsonDocument(const QJsonArray &array);
QJsonDocument(const QJsonDocument &other); QJsonDocument(const QJsonDocument &other);
~QJsonDocument(); ~QJsonDocument();
public: public:
QJsonDocument &operator=(const QJsonDocument &other); QJsonDocument &operator=(const QJsonDocument &other);
public: 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: public:
bool isArray() const; bool isArray() const;
bool isEmpty() const; bool isEmpty() const;
bool isNull() const; bool isNull() const;
bool isObject() const; bool isObject() const;
public: public:
QByteArray toBinaryData() const; QByteArray toBinaryData() const;
QByteArray toJson(JsonFormat format = Indented) const; QByteArray toJson(JsonFormat format = Indented) const;
QVariant toVariant() const; QVariant toVariant() const;
public: public:
QJsonArray array() const; QJsonArray array() const;
QJsonObject object() const; QJsonObject object() const;
const char *rawData(int *size) const; const char *rawData(int *size) const;
public: public:
void setArray(const QJsonArray &array); void setArray(const QJsonArray &array);
void setObject(const QJsonObject &object); void setObject(const QJsonObject &object);
public: public:
static QJsonDocument fromBinaryData(const QByteArray &data, DataValidation validation = Validate); static QJsonDocument fromBinaryData(const QByteArray &data, DataValidation validation = Validate);
static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error = 0); static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error = 0);
static QJsonDocument fromRawData(const char *data, int size, DataValidation validation = Validate); static QJsonDocument fromRawData(const char *data, int size, DataValidation validation = Validate);
static QJsonDocument fromVariant(const QVariant &variant); static QJsonDocument fromVariant(const QVariant &variant);
private: private:
void setRoot(const QJsonRoot &root); void setRoot(const QJsonRoot &root);
QString toJson(const QJsonValue &v, JsonFormat format) const; QString toJson(const QJsonValue &v, JsonFormat format, int intend = 1) const;
QString escapeString(const QString &s) const; QString escapeString(const QString &s) const;
private: private:
void swap(QJsonDocument &other); void swap(QJsonDocument &other);
private: private:
QJsonRoot *root_; QJsonRoot *root_;
}; };
#endif #endif