[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

@ -179,9 +179,9 @@ QString QJsonDocument::escapeString(const QString &s) const {
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;
@ -199,10 +199,11 @@ QString QJsonDocument::escapeString(const QString &s) const {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// 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:
@ -228,37 +229,43 @@ QString QJsonDocument::toJson(const QJsonValue &v, JsonFormat format) const {
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--;
ss << (compact ? "]" : QString("\n%1]").arg(QString(4*intend, ' ')));
} }
break; break;
case QJsonValue::Object: case QJsonValue::Object:
{ {
const QJsonObject o = v.toObject(); const QJsonObject o = v.toObject();
ss << "{"; ss << (compact ? "{" : "{\n");
if(!o.empty()) { if(!o.empty()) {
QJsonObject::const_iterator it = o.begin(); QJsonObject::const_iterator it = o.begin();
QJsonObject::const_iterator e = o.end(); QJsonObject::const_iterator e = o.end();
ss << '"' << escapeString(it.key()) << "\": " << toJson(it.value(), format); if (!compact) ss << QByteArray(4*intend, ' ');
ss << '"' << escapeString(it.key()) << (compact ? "\":" : "\": ") << toJson(it.value(), format, intend+1);
++it; ++it;
for(;it != e; ++it) { for(;it != e; ++it) {
ss << ','; ss << (compact ? "," : ",\n");
ss << '"' << escapeString(it.key()) << "\": " << toJson(it.value(), format); if (!compact) ss << QByteArray(4*intend, ' ');
ss << '"' << escapeString(it.key()) << (compact ? "\":" : "\": ") << toJson(it.value(), format, intend+1);
} }
} }
ss << "}"; intend--;
ss << (compact ? "}" : QString("\n%1}").arg(QString(4*intend, ' ')));
} }
break; break;
case QJsonValue::Undefined: case QJsonValue::Undefined:

View file

@ -88,7 +88,7 @@ public:
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: