Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git i/src/corelib/json/qjsonvalue.cpp w/src/corelib/json/qjsonvalue.cpp
- index 4b52014db1..e1b317ca79 100644
- --- i/src/corelib/json/qjsonvalue.cpp
- +++ w/src/corelib/json/qjsonvalue.cpp
- @@ -624,6 +624,31 @@ QJsonObject QJsonValue::toObject() const
- return toObject(QJsonObject());
- }
- +QJsonValue QJsonValue::operator [](const QString &key) const
- +{
- + if (!isObject())
- + return QJsonValue(QJsonValue::Undefined);
- +
- + return toObject()[key];
- +}
- +
- +QJsonValue QJsonValue::operator[](int i) const
- +{
- + if (!isArray())
- + return QJsonValue(QJsonValue::Undefined);
- +
- + return toArray()[i];
- +}
- +
- +/*
- +QJsonValueRef QJsonValue::operator [](const QString &key)
- +{
- + //return QJsonObjectRef(this, key);
- +}*/
- +
- +
- +
- +
- /*!
- Returns \c true if the value is equal to \a other.
- */
- @@ -712,20 +737,62 @@ void QJsonValue::detach()
- However, they are not explicitly documented here.
- */
- +struct QJsonValueRefData
- +{
- + QJsonValueRef *ref;
- + QString key;
- + int index;
- +};
- +
- +static const uintptr_t kJsonValueRefPointerTag = 0x1;
- +
- +QJsonValueRef::QJsonValueRef(QJsonValueRef *ref, const QString &key)
- + : a(nullptr), is_object(false), index(0)
- +{
- + p = quintptr(new QJsonValueRefData{ ref, key, -1 }) | kJsonValueRefPointerTag;
- +}
- +
- +QJsonValueRef::QJsonValueRef(QJsonValueRef *ref, int index)
- + : a(nullptr), is_object(false), index(0)
- +{
- + p = quintptr(new QJsonValueRefData{ ref, QString(), index }) | kJsonValueRefPointerTag;
- +}
- +
- +QJsonValueRef::~QJsonValueRef()
- +{
- + if (isReference())
- + delete reinterpret_cast<QJsonValueRefData *>(p & ~kJsonValueRefPointerTag);
- +}
- QJsonValueRef &QJsonValueRef::operator =(const QJsonValue &val)
- {
- - if (is_object)
- + if (isReference()) {
- + QJsonValueRefData *data = reinterpret_cast<QJsonValueRefData *>(p & ~kJsonValueRefPointerTag);
- + qDebug() << __FUNCTION__ << "setting" << data->key << "for" << *data->ref << "to" << val;
- +
- + QJsonValue refVal = data->ref->toValue();
- + if (refVal.isObject()) {
- + QJsonObject object = refVal.toObject();
- + object[data->key] = val;
- + *(data->ref) = object;
- + } else {
- + QJsonArray array = refVal.toArray();
- + array[data->index] = val;
- + *(data->ref) = array;
- + }
- + } else if (is_object) {
- o->setValueAt(index, val);
- - else
- + } else {
- a->replace(index, val);
- -
- + }
- return *this;
- }
- QJsonValueRef &QJsonValueRef::operator =(const QJsonValueRef &ref)
- {
- - if (is_object)
- + if (isReference())
- + qDebug() << __FUNCTION__ << "unimpl";
- + else if (is_object)
- o->setValueAt(index, ref);
- else
- a->replace(index, ref);
- @@ -750,11 +817,39 @@ QJsonObject QJsonValueRef::toObject() const
- QJsonValue QJsonValueRef::toValue() const
- {
- + if (isReference()) {
- + QJsonValueRefData *data = reinterpret_cast<QJsonValueRefData *>(p & ~kJsonValueRefPointerTag);
- + if (data->index >= 0) // FIXME, better mark
- + return data->ref->toValue()[data->index];
- + else
- + return data->ref->toValue()[data->key];
- + }
- +
- if (!is_object)
- return a->at(index);
- return o->valueAt(index);
- }
- +QJsonValueRef QJsonValueRef::operator [](const QString &key)
- +{
- + return QJsonValueRef(this, key);
- +}
- +
- +QJsonValueRef QJsonValueRef::operator [](int i)
- +{
- + return QJsonValueRef(this, i);
- +}
- +
- +bool QJsonValueRef::isReference() const
- +{
- + return p & kJsonValueRefPointerTag;
- +}
- +
- +QJsonValueRef *QJsonValueRef::reference() const
- +{
- +
- +}
- +
- #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
- QDebug operator<<(QDebug dbg, const QJsonValue &o)
- {
- diff --git i/src/corelib/json/qjsonvalue.h w/src/corelib/json/qjsonvalue.h
- index a853acaddd..385f7b4855 100644
- --- i/src/corelib/json/qjsonvalue.h
- +++ w/src/corelib/json/qjsonvalue.h
- @@ -60,6 +60,8 @@ namespace QJsonPrivate {
- class Entry;
- }
- +class QJsonValueRef;
- +
- class Q_CORE_EXPORT QJsonValue
- {
- public:
- @@ -114,6 +116,13 @@ public:
- QJsonObject toObject() const;
- QJsonObject toObject(const QJsonObject &defaultValue) const;
- + QJsonValue operator[] (const QString &key) const;
- + QJsonValue operator[](int i) const;
- +
- + // QJsonValue operator[] (QLatin1String key) const;
- + // QJsonValueRef operator[] (const QString &key);
- + //QJsonValueRef operator[] (QLatin1String key);
- +
- bool operator==(const QJsonValue &other) const;
- bool operator!=(const QJsonValue &other) const;
- @@ -149,6 +158,10 @@ public:
- QJsonValueRef(QJsonObject *object, int idx)
- : o(object), is_object(true), index(idx) {}
- + QJsonValueRef(QJsonValueRef *ref, const QString &key);
- + QJsonValueRef(QJsonValueRef *ref, int index);
- + ~QJsonValueRef();
- +
- inline operator QJsonValue() const { return toValue(); }
- QJsonValueRef &operator = (const QJsonValue &val);
- QJsonValueRef &operator = (const QJsonValueRef &val);
- @@ -170,6 +183,9 @@ public:
- QJsonArray toArray() const;
- QJsonObject toObject() const;
- + QJsonValueRef operator[] (const QString &key);
- + QJsonValueRef operator[](int i);
- +
- // ### Qt 6: Add default values
- inline bool toBool(bool defaultValue) const { return toValue().toBool(defaultValue); }
- inline int toInt(int defaultValue) const { return toValue().toInt(defaultValue); }
- @@ -181,11 +197,15 @@ public:
- private:
- QJsonValue toValue() const;
- + bool isReference() const;
- + QJsonValueRef *reference() const;
- union {
- QJsonArray *a;
- QJsonObject *o;
- + quintptr p;
- };
- +
- uint is_object : 1;
- uint index : 31;
- };
- diff --git i/tests/auto/corelib/json/json.qrc w/tests/auto/corelib/json/json.qrc
- index eb122a1779..8552c815b4 100644
- --- i/tests/auto/corelib/json/json.qrc
- +++ w/tests/auto/corelib/json/json.qrc
- @@ -3,6 +3,7 @@
- <file>bom.json</file>
- <file>test2.json</file>
- <file>test3.json</file>
- + <file>test4.json</file>
- <file>test.json</file>
- <file>test.bjson</file>
- </qresource>
- diff --git i/tests/auto/corelib/json/tst_qtjson.cpp w/tests/auto/corelib/json/tst_qtjson.cpp
- index b215364f0e..1fa85624f0 100644
- --- i/tests/auto/corelib/json/tst_qtjson.cpp
- +++ w/tests/auto/corelib/json/tst_qtjson.cpp
- @@ -145,6 +145,8 @@ private Q_SLOTS:
- void parseErrorOffset_data();
- void parseErrorOffset();
- + void implicitObject();
- +
- private:
- QString testDataDir;
- };
- @@ -2908,5 +2910,61 @@ void tst_QtJson::parseErrorOffset()
- QCOMPARE(error.offset, errorOffset);
- }
- +void tst_QtJson::implicitObject()
- +{
- + QFile file(testDataDir + "/test4.json");
- + file.open(QFile::ReadOnly);
- + QByteArray jsonData = file.readAll();
- + QVERIFY(!jsonData.isEmpty());
- +
- + QJsonDocument doc = QJsonDocument::fromJson(jsonData);
- + QVERIFY(!doc.isNull());
- +
- + QJsonObject docobj = doc.object();
- +
- + QJsonValueRef phoneNumber = docobj["phoneNumber"];
- +
- + qDebug() << docobj["phoneNumber"][1]["number"];
- + /*QJsonArray arr = phoneNumber.toArray();
- + QJsonArray array;
- + for (int i = 0; i < 10; ++i)
- + array.append((double)i);
- + phoneNumber = array;
- + qDebug() << phoneNumber;
- + qDebug() << arr;
- +*/
- + qDebug() << docobj["address"]["apartment"]["floor"];
- + QCOMPARE(docobj["address"]["apartment"]["floor"].toInt(), 4);
- +
- + qDebug() << docobj["address"]["apaasdasdrtment"]["floor"];
- +
- + docobj["address"]["apartment"]["floor"] = 42;
- +
- + qDebug() << docobj;
- +
- + docobj["address"]["asdasd"]["floor"] = 52;
- +
- + qDebug() << docobj;
- +
- + QCOMPARE(docobj["address"]["apartment"]["floor"].toInt(), 42);
- +
- + //qDebug() << doc["address"]["city"];
- +
- +// doc["address"]["city"] = "Oslo";
- +/*
- + QJsonValue addr = doc.object()["address"];
- + qDebug() << addr;
- +
- + QJsonObject obj = addr.toObject();
- + obj["postalCode"] = 123;
- + qDebug() << obj;
- + // doc["address"] = 52;
- +*/
- +
- + //obj["address"] = 42;
- +
- + // qDebug() << doc;
- +}
- +
- QTEST_MAIN(tst_QtJson)
- #include "tst_qtjson.moc"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement