#25 Extend json() method in QJsonModel to take a parameter whether or not to format the output compact. Be default it is false to lessen impact on existing use of this method.

This commit is contained in:
Samson Chu 2023-08-09 18:35:25 -04:00
parent cef8105002
commit fae694be15
2 changed files with 4 additions and 4 deletions

View File

@ -399,7 +399,7 @@ Qt::ItemFlags QJsonModel::flags(const QModelIndex &index) const
return QAbstractItemModel::flags(index);
}
QByteArray QJsonModel::json()
QByteArray QJsonModel::json(bool compact)
{
auto jsonValue = genJson(mRootItem);
QByteArray json;
@ -407,9 +407,9 @@ QByteArray QJsonModel::json()
return json;
if (jsonValue.isArray())
arrayToJson(jsonValue.toArray(), json, 0, false);
arrayToJson(jsonValue.toArray(), json, 0, compact);
else
objectToJson(jsonValue.toObject(), json, 0, false);
objectToJson(jsonValue.toObject(), json, 0, compact);
return json;
}

View File

@ -315,7 +315,7 @@ public:
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
QByteArray json();
QByteArray json(bool compact = false);
QByteArray jsonToByte(QJsonValue jsonValue);
void objectToJson(QJsonObject jsonObject, QByteArray &json, int indent, bool compact);
void arrayToJson(QJsonArray jsonArray, QByteArray &json, int indent, bool compact);