[skip ci] prettify qjson4 output aswell
This commit is contained in:
parent
2bf6bb5630
commit
af86e70f4b
2 changed files with 224 additions and 217 deletions
|
@ -179,9 +179,9 @@ QString QJsonDocument::escapeString(const QString &s) const {
|
|||
switch(ch.toLatin1()) {
|
||||
case '\"': r.append("\\\""); break;
|
||||
case '\\': r.append("\\\\"); break;
|
||||
#if 0
|
||||
#if 0
|
||||
case '/': r.append("\\/"); break;
|
||||
#endif
|
||||
#endif
|
||||
case '\b': r.append("\\b"); break;
|
||||
case '\f': r.append("\\f"); break;
|
||||
case '\n': r.append("\\n"); break;
|
||||
|
@ -199,10 +199,11 @@ QString QJsonDocument::escapeString(const QString &s) const {
|
|||
//------------------------------------------------------------------------------
|
||||
// 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);
|
||||
bool compact = (format == JsonFormat::Compact);
|
||||
|
||||
switch(v.type()) {
|
||||
case QJsonValue::Null:
|
||||
|
@ -228,37 +229,43 @@ QString QJsonDocument::toJson(const QJsonValue &v, JsonFormat format) const {
|
|||
case QJsonValue::Array:
|
||||
{
|
||||
const QJsonArray a = v.toArray();
|
||||
ss << "[";
|
||||
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 << (compact ? "," : ",\n");
|
||||
if (!compact) ss << QByteArray(4*intend, ' ');
|
||||
ss << toJson(*it, format, intend+1);
|
||||
}
|
||||
}
|
||||
ss << "]";
|
||||
intend--;
|
||||
ss << (compact ? "]" : QString("\n%1]").arg(QString(4*intend, ' ')));
|
||||
}
|
||||
break;
|
||||
case QJsonValue::Object:
|
||||
{
|
||||
const QJsonObject o = v.toObject();
|
||||
ss << "{";
|
||||
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);
|
||||
if (!compact) ss << QByteArray(4*intend, ' ');
|
||||
ss << '"' << escapeString(it.key()) << (compact ? "\":" : "\": ") << toJson(it.value(), format, intend+1);
|
||||
++it;
|
||||
for(;it != e; ++it) {
|
||||
ss << ',';
|
||||
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 << "}";
|
||||
intend--;
|
||||
ss << (compact ? "}" : QString("\n%1}").arg(QString(4*intend, ' ')));
|
||||
}
|
||||
break;
|
||||
case QJsonValue::Undefined:
|
||||
|
|
|
@ -88,7 +88,7 @@ public:
|
|||
|
||||
private:
|
||||
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;
|
||||
|
||||
private:
|
||||
|
|
Loading…
Reference in a new issue