Skip to content

Commit

Permalink
dridk#25 Extend json() method in QJsonModel to take a parameter wheth…
Browse files Browse the repository at this point in the history
…er or not to format the output compact. Be default it is false to lessen impact on existing use of this method.
  • Loading branch information
Samson Chu committed Aug 9, 2023
1 parent cef8105 commit fae694b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions qjsonmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,17 +399,17 @@ 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;
if (jsonValue.isNull())
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;
}
Expand Down
2 changes: 1 addition & 1 deletion qjsonmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class QJsonModel : public QAbstractItemModel
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);
Expand Down

0 comments on commit fae694b

Please sign in to comment.