Skip to content

Commit

Permalink
Ignore "comment" tags (Exceptions)
Browse files Browse the repository at this point in the history
Signed-off-by: Evgeny Shtanov <[email protected]>
  • Loading branch information
eugesh committed Jun 20, 2022
1 parent 47dea33 commit b80c666
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
4 changes: 4 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
**********************************************/

#include <QApplication>
#include <QDebug>
#include <QFile>
#include <QTreeView>
#include <string>
Expand Down Expand Up @@ -46,16 +47,19 @@ int main(int argc, char *argv[])
"phoneNumber":
[
{
"comment": "This is just a comment!",
"type": "home",
"number": "212 555-1234"
},
{
"comment1": "Another comment!",
"type": "fax",
"number": "646 555-4567"
}
]
})";

model->addException({"comment"});
model->loadJson(QByteArray::fromStdString(json));
view->show();

Expand Down
28 changes: 22 additions & 6 deletions qjsonmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
#include <QFont>


inline bool contains(const QStringList& list, const QString &value) {
for (auto val : list)
if (value.contains(val, Qt::CaseInsensitive))
return true;

return false;
}

QJsonTreeItem::QJsonTreeItem(QJsonTreeItem *parent)
{
mParent = parent;
Expand Down Expand Up @@ -96,7 +104,7 @@ QJsonValue::Type QJsonTreeItem::type() const
return mType;
}

QJsonTreeItem* QJsonTreeItem::load(const QJsonValue& value, QJsonTreeItem* parent)
QJsonTreeItem *QJsonTreeItem::load(const QJsonValue& value, const QStringList &exceptions, QJsonTreeItem *parent)
{
QJsonTreeItem *rootItem = new QJsonTreeItem(parent);
rootItem->setKey("root");
Expand All @@ -105,8 +113,11 @@ QJsonTreeItem* QJsonTreeItem::load(const QJsonValue& value, QJsonTreeItem* paren
//Get all QJsonValue childs
const QStringList keys = value.toObject().keys(); // To prevent clazy-range warning
for (const QString &key : keys) {
if (contains(exceptions, key)) {
continue;
}
QJsonValue v = value.toObject().value(key);
QJsonTreeItem *child = load(v, rootItem);
QJsonTreeItem *child = load(v, exceptions, rootItem);
child->setKey(key);
child->setType(v.type());
rootItem->appendChild(child);
Expand All @@ -116,7 +127,7 @@ QJsonTreeItem* QJsonTreeItem::load(const QJsonValue& value, QJsonTreeItem* paren
int index = 0;
const QJsonArray array = value.toArray(); // To prevent clazy-range warning
for (const QJsonValue &v : array) {
QJsonTreeItem *child = load(v, rootItem);
QJsonTreeItem *child = load(v, exceptions, rootItem);
child->setKey(QString::number(index));
child->setType(v.type());
rootItem->appendChild(child);
Expand Down Expand Up @@ -269,11 +280,11 @@ bool QJsonModel::loadJson(const QByteArray &json)
beginResetModel();
delete mRootItem;
if (jdoc.isArray()) {
mRootItem = QJsonTreeItem::load(QJsonValue(jdoc.array()));
mRootItem = QJsonTreeItem::load(QJsonValue(jdoc.array()), mExceptions);
mRootItem->setType(QJsonValue::Array);

} else {
mRootItem = QJsonTreeItem::load(QJsonValue(jdoc.object()));
mRootItem = QJsonTreeItem::load(QJsonValue(jdoc.object()), mExceptions);
mRootItem->setType(QJsonValue::Object);
}
endResetModel();
Expand Down Expand Up @@ -509,7 +520,12 @@ void QJsonModel::valueToJson(QJsonValue jsonValue, QByteArray &json, int indent,
}
}

QJsonValue QJsonModel::genJson(QJsonTreeItem * item) const
void QJsonModel::addException(const QStringList &exceptions)
{
mExceptions = exceptions;
}

QJsonValue QJsonModel::genJson(QJsonTreeItem *item) const
{
auto type = item->type();
int nchild = item->childCount();
Expand Down
6 changes: 5 additions & 1 deletion qjsonmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class QJsonTreeItem
QVariant value() const;
QJsonValue::Type type() const;

static QJsonTreeItem* load(const QJsonValue& value, QJsonTreeItem *parent = nullptr);
static QJsonTreeItem *load(const QJsonValue& value, const QStringList &exceptions = {}, QJsonTreeItem *parent = nullptr);

protected:

Expand Down Expand Up @@ -322,11 +322,15 @@ class QJsonModel : public QAbstractItemModel
void arrayContentToJson(QJsonArray jsonArray, QByteArray &json, int indent, bool compact);
void objectContentToJson(QJsonObject jsonObject, QByteArray &json, int indent, bool compact);
void valueToJson(QJsonValue jsonValue, QByteArray &json, int indent, bool compact);
//! List of tags to skip during JSON parsing
void addException(const QStringList &exceptions);

private:
QJsonValue genJson(QJsonTreeItem *) const;
QJsonTreeItem *mRootItem = nullptr;
QStringList mHeaders;
//! List of exceptions (e.g. comments). Case insensitive, compairs on "contains".
QStringList mExceptions;
};

#endif // QJSONMODEL_H

0 comments on commit b80c666

Please sign in to comment.