diff --git a/src/corelib/serialization/qjsonvalue.cpp b/src/corelib/serialization/qjsonvalue.cpp index 37ead9648eb..6175c022703 100644 --- a/src/corelib/serialization/qjsonvalue.cpp +++ b/src/corelib/serialization/qjsonvalue.cpp @@ -1072,6 +1072,44 @@ QJsonValue QJsonValueRef::toValue() const { return concrete(*this); } +#else +QJsonValueRef QJsonValueRef::operator[](qsizetype key) +{ + if (d->elements.at(index).type != QCborValue::Array) + d->replaceAt(index, QCborValue::Array); + + auto &e = d->elements[index]; + e.container = QCborContainerPrivate::grow(e.container, key); // detaches + e.flags |= QtCbor::Element::IsContainer; + + return QJsonValueRef(e.container, key, false); +} + +QJsonValueRef QJsonValueRef::operator[](QAnyStringView key) +{ + // must go through QJsonObject because some of the machinery is non-static + // member or file-static in qjsonobject.cpp + QJsonObject o = QJsonPrivate::Value::fromTrustedCbor(d->valueAt(index)).toObject(); + QJsonValueRef ret = key.visit([&](auto v) { + if constexpr (std::is_same_v) + return o[QString::fromUtf8(v)]; + else + return o[v]; + }); + + // ### did the QJsonObject::operator[] above detach? + QCborContainerPrivate *x = o.o.take(); + Q_ASSERT(x->ref.loadRelaxed() == 1); + + auto &e = d->elements[index]; + if (e.flags & QtCbor::Element::IsContainer && e.container != x) + o.o.reset(e.container); // might not an object! + + e.flags |= QtCbor::Element::IsContainer; + e.container = x; + + return ret; +} #endif size_t qHash(const QJsonValue &value, size_t seed) diff --git a/src/corelib/serialization/qjsonvalue.h b/src/corelib/serialization/qjsonvalue.h index cbf555507ba..4d6caf265a3 100644 --- a/src/corelib/serialization/qjsonvalue.h +++ b/src/corelib/serialization/qjsonvalue.h @@ -229,6 +229,10 @@ protected: quint64 is_object : 1; quint64 index : 63; #else + constexpr QJsonValueConstRef(QCborContainerPrivate *d, size_t index, bool is_object) + : d(d), is_object(is_object), index(index) + {} + // implemented in qjsonarray.h & qjsonobject.h, to get their d QJsonValueConstRef(QJsonArray *array, qsizetype idx); QJsonValueConstRef(QJsonObject *object, qsizetype idx); @@ -296,6 +300,9 @@ private: QJsonValue toValue() const; #else using QJsonValueConstRef::operator[]; + Q_CORE_EXPORT QJsonValueRef operator[](QAnyStringView key); + Q_CORE_EXPORT QJsonValueRef operator[](qsizetype i); + private: using QJsonValueConstRef::QJsonValueConstRef; #endif // < Qt 7