summaryrefslogtreecommitdiffstats
path: root/src/corelib/serialization/qjsonvalue.cpp
blob: e358f18bde726cf597015be8b49d96f7a0a40b5d (plain)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
// Copyright (C) 2020 The Qt Company Ltd.// Copyright (C) 2022 Intel Corporation.// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only#include <qjsonobject.h>#include <qjsonvalue.h>#include <qjsonarray.h>#include <qjsondocument.h>#include <qurl.h>#include <quuid.h>#include <qvariant.h>#include <qstringlist.h>#include <qmap.h>#include <qhash.h>#include <qdebug.h>#include"qdatastream.h"#include"qjsonparser_p.h"#include"qjsonwriter_p.h"#include <private/qnumeric_p.h>#include <private/qcborvalue_p.h>#include <qcborarray.h>#include <qcbormap.h>#include"qjson_p.h" QT_BEGIN_NAMESPACE staticQJsonValue::Type convertFromCborType(QCborValue::Type type) noexcept {switch(type) {caseQCborValue::Null:returnQJsonValue::Null;caseQCborValue::True:caseQCborValue::False:returnQJsonValue::Bool;caseQCborValue::Double:caseQCborValue::Integer:returnQJsonValue::Double;caseQCborValue::String:returnQJsonValue::String;caseQCborValue::Array:returnQJsonValue::Array;caseQCborValue::Map:returnQJsonValue::Object;caseQCborValue::Undefined:default:returnQJsonValue::Undefined;}}/*! \class QJsonValue \inmodule QtCore \ingroup json \ingroup shared \ingroup qtserialization \reentrant \since 5.0 \brief The QJsonValue class encapsulates a value in JSON. \compares equality \compareswith equality QJsonValueConstRef QJsonValueRef \endcompareswith A value in JSON can be one of 6 basic types: JSON is a format to store structured data. It has 6 basic data types: \list \li bool QJsonValue::Bool \li double QJsonValue::Double \li string QJsonValue::String \li array QJsonValue::Array \li object QJsonValue::Object \li null QJsonValue::Null \endlist A value can represent any of the above data types. In addition, QJsonValue has one special flag to represent undefined values. This can be queried with isUndefined(). The type of the value can be queried with type() or accessors like isBool(), isString(), and so on. Likewise, the value can be converted to the type stored in it using the toBool(), toString() and so on. Values are strictly typed internally and contrary to QVariant will not attempt to do any implicit type conversions. This implies that converting to a type that is not stored in the value will return a default constructed return value. \section1 QJsonValueRef QJsonValueRef is a helper class for QJsonArray and QJsonObject. When you get an object of type QJsonValueRef, you can use it as if it were a reference to a QJsonValue. If you assign to it, the assignment will apply to the element in the QJsonArray or QJsonObject from which you got the reference. The following methods return QJsonValueRef: \list \li \l {QJsonArray}::operator[](qsizetype i) \li \l {QJsonObject}::operator[](const QString & key) const \endlist \sa {JSON Support in Qt}, {Saving and Loading a Game}*//*! Creates a QJsonValue of type \a type. The default is to create a Null value. */QJsonValue::QJsonValue(Type type){switch(type) {case Null: value =QCborValue::Null;break;case Bool: value =QCborValue::False;break;case Double: value =QCborValue::Double;break;case String: value =QCborValue::String;break;case Array: value =QCborValue::Array;break;case Object: value =QCborValue::Map;break;case Undefined:break;}}/*! Creates a value of type Bool, with value \a b. */QJsonValue::QJsonValue(bool b):value(b){}staticinline QCborValue doubleValueHelper(double v){ qint64 n =0;// Convert to integer if the number is an integer and changing wouldn't// introduce additional digit precision not present in the double.if(convertDoubleTo<qint64>(v, &n,false/* allow_precision_upgrade */))return n;elsereturn v;}/*! Creates a value of type Double, with value \a v. */QJsonValue::QJsonValue(double v):value(doubleValueHelper(v)){}/*! \overload Creates a value of type Double, with value \a v. */QJsonValue::QJsonValue(int v):value(v){}/*! \overload Creates a value of type Double, with value \a v. NOTE: the integer limits for IEEE 754 double precision data is 2^53 (-9007199254740992 to +9007199254740992). If you pass in values outside this range expect a loss of precision to occur. */QJsonValue::QJsonValue(qint64 v):value(v){}/*! Creates a value of type String, with value \a s. */QJsonValue::QJsonValue(const QString &s):value(s){}/*! \fn QJsonValue::QJsonValue(const char *s) Creates a value of type String with value \a s, assuming UTF-8 encoding of the input. You can disable this constructor by defining \c QT_NO_CAST_FROM_ASCII when you compile your applications. \since 5.3 *//*! Creates a value of type String, with the Latin-1 string viewed by \a s. */QJsonValue::QJsonValue(QLatin1StringView s):value(s){}/*! Creates a value of type Array, with value \a a. */QJsonValue::QJsonValue(const QJsonArray &a):value(QCborArray::fromJsonArray(a)){}/*! \overload \since 6.3 */QJsonValue::QJsonValue(QJsonArray &&a) noexcept :value(QCborArray::fromJsonArray(std::move(a))){}/*! Creates a value of type Object, with value \a o. */QJsonValue::QJsonValue(const QJsonObject &o):value(QCborMap::fromJsonObject(o)){}/*! \overload \since 6.3 */QJsonValue::QJsonValue(QJsonObject &&o) noexcept :value(QCborMap::fromJsonObject(std::move(o))){}/*! Destroys the value. */QJsonValue::~QJsonValue() =default;/*! Creates a copy of \a other. */QJsonValue::QJsonValue(const QJsonValue &other) noexcept =default;/*! Assigns the value stored in \a other to this object. */ QJsonValue &QJsonValue::operator=(const QJsonValue &other) noexcept { QJsonValue copy(other);swap(copy);return*this;}QJsonValue::QJsonValue(QJsonValue &&other) noexcept :value(std::move(other.value)){ other.value =QCborValue(nullptr);}voidQJsonValue::swap(QJsonValue &other) noexcept { value.swap(other.value);}/*! \fn QJsonValue::QJsonValue(QJsonValue &&other) \since 5.10 Move-constructs a QJsonValue from \a other.*//*! \fn QJsonValue &QJsonValue::operator =(QJsonValue &&other) \since 5.10 Move-assigns \a other to this value.*//*! \fn void QJsonValue::swap(QJsonValue &other) \since 5.10 \memberswap{value}*//*! \fn bool QJsonValue::isNull() const Returns \c true if the value is null.*//*! \fn bool QJsonValue::isBool() const Returns \c true if the value contains a boolean. \sa toBool() *//*! \fn bool QJsonValue::isDouble() const Returns \c true if the value contains a double. \sa toDouble() *//*! \fn bool QJsonValue::isString() const Returns \c true if the value contains a string. \sa toString() *//*! \fn bool QJsonValue::isArray() const Returns \c true if the value contains an array. \sa toArray() *//*! \fn bool QJsonValue::isObject() const Returns \c true if the value contains an object. \sa toObject() *//*! \fn bool QJsonValue::isUndefined() const Returns \c true if the value is undefined. This can happen in certain error cases as e.g. accessing a non existing key in a QJsonObject. */#ifndef QT_NO_VARIANT/*! Converts \a variant to a QJsonValue and returns it. The conversion will convert QVariant types as follows: \table \header \li Source type \li Destination type \row \li \list \li QMetaType::Nullptr \endlist \li QJsonValue::Null \row \li \list \li QMetaType::Bool \endlist \li QJsonValue::Bool \row \li \list \li QMetaType::Int \li QMetaType::UInt \li QMetaType::LongLong \li QMetaType::ULongLong \li QMetaType::Float \li QMetaType::Double \endlist \li QJsonValue::Double \row \li \list \li QMetaType::QString \endlist \li QJsonValue::String \row \li \list \li QMetaType::QStringList \li QMetaType::QVariantList \endlist \li QJsonValue::Array \row \li \list \li QMetaType::QVariantMap \li QMetaType::QVariantHash \endlist \li QJsonValue::Object \row \li \list \li QMetaType::QUrl \endlist \li QJsonValue::String. The conversion will use QUrl::toString() with flag QUrl::FullyEncoded, so as to ensure maximum compatibility in parsing the URL \row \li \list \li QMetaType::QUuid \endlist \li QJsonValue::String. Since Qt 5.11, the resulting string will not include braces \row \li \list \li QMetaType::QCborValue \endlist \li Whichever type QCborValue::toJsonValue() returns. \row \li \list \li QMetaType::QCborArray \endlist \li QJsonValue::Array. See QCborValue::toJsonValue() for conversion restrictions. \row \li \list \li QMetaType::QCborMap \endlist \li QJsonValue::Map. See QCborValue::toJsonValue() for conversion restrictions and the "stringification" of map keys. \endtable \section2 Loss of information and other types QVariant can carry more information than is representable in JSON. If the QVariant is not one of the types above, the conversion is not guaranteed and is subject to change in future versions of Qt, as the UUID one did. Code should strive not to use any other types than those listed above. If QVariant::isNull() returns true, a null QJsonValue is returned or inserted into the list or object, regardless of the type carried by QVariant. Note the behavior change in Qt 6.0 affecting QVariant::isNull() also affects this function. A floating point value that is either an infinity or NaN will be converted to a null JSON value. Since Qt 6.0, QJsonValue can store the full precision of any 64-bit signed integer without loss, but in previous versions values outside the range of ±2^53 may lose precision. Unsigned 64-bit values greater than or equal to 2^63 will either lose precision or alias to negative values, so QMetaType::ULongLong should be avoided. For other types not listed above, a conversion to string will be attempted, usually but not always by calling QVariant::toString(). If the conversion fails the value is replaced by a null JSON value. Note that QVariant::toString() is also lossy for the majority of types. For example, if the passed QVariant is representing raw byte array data, it is recommended to pre-encode it to \l {RFC 4686}{Base64} (or another lossless encoding), otherwise a lossy conversion using QString::fromUtf8() will be used. Please note that the conversions via QVariant::toString() are subject to change at any time. Both QVariant and QJsonValue may be extended in the future to support more types, which will result in a change in how this function performs conversions. \sa toVariant(), QCborValue::fromVariant() */ QJsonValue QJsonValue::fromVariant(const QVariant &variant){switch(variant.metaType().id()) {caseQMetaType::Nullptr:returnQJsonValue(Null);caseQMetaType::Bool:returnQJsonValue(variant.toBool());caseQMetaType::Short:caseQMetaType::UShort:caseQMetaType::Int:caseQMetaType::UInt:caseQMetaType::Long:caseQMetaType::LongLong:returnQJsonValue(variant.toLongLong());caseQMetaType::ULong:caseQMetaType::ULongLong:if(variant.toULongLong() <=static_cast<uint64_t>(std::numeric_limits<qint64>::max()))returnQJsonValue(variant.toLongLong());Q_FALLTHROUGH();caseQMetaType::Float16:caseQMetaType::Float:caseQMetaType::Double: {double v = variant.toDouble();returnqt_is_finite(v) ?QJsonValue(v) :QJsonValue();}caseQMetaType::QString:returnQJsonValue(variant.toString());caseQMetaType::QStringList:returnQJsonValue(QJsonArray::fromStringList(variant.toStringList()));caseQMetaType::QVariantList:returnQJsonValue(QJsonArray::fromVariantList(variant.toList()));caseQMetaType::QVariantMap:returnQJsonValue(QJsonObject::fromVariantMap(variant.toMap()));caseQMetaType::QVariantHash:returnQJsonValue(QJsonObject::fromVariantHash(variant.toHash()));#ifndef QT_BOOTSTRAPPEDcaseQMetaType::QUrl:returnQJsonValue(variant.toUrl().toString(QUrl::FullyEncoded));caseQMetaType::QUuid:return variant.toUuid().toString(QUuid::WithoutBraces);caseQMetaType::QJsonValue:return variant.toJsonValue();caseQMetaType::QJsonObject:return variant.toJsonObject();caseQMetaType::QJsonArray:return variant.toJsonArray();caseQMetaType::QJsonDocument: { QJsonDocument doc = variant.toJsonDocument();return doc.isArray() ?QJsonValue(doc.array()) :QJsonValue(doc.object());}caseQMetaType::QCborValue:return qvariant_cast<QCborValue>(variant).toJsonValue();caseQMetaType::QCborArray:return qvariant_cast<QCborArray>(variant).toJsonArray();caseQMetaType::QCborMap:return qvariant_cast<QCborMap>(variant).toJsonObject();#endifdefault:break;} QString string = variant.toString();if(string.isEmpty())returnQJsonValue();returnQJsonValue(string);}/*! Converts the value to a \l {QVariant::}{QVariant()}. The QJsonValue types will be converted as follows: \value Null QMetaType::Nullptr \value Bool QMetaType::Bool \value Double QMetaType::Double or QMetaType::LongLong \value String QString \value Array QVariantList \value Object QVariantMap \value Undefined \l {QVariant::}{QVariant()} \sa fromVariant() */ QVariant QJsonValue::toVariant()const{switch(value.type()) {caseQCborValue::True:return true;caseQCborValue::False:return false;caseQCborValue::Integer:returntoInteger();caseQCborValue::Double:returntoDouble();caseQCborValue::String:returntoString();caseQCborValue::Array:returntoArray().toVariantList();caseQCborValue::Map:returntoObject().toVariantMap();caseQCborValue::Null:returnQVariant::fromValue(nullptr);caseQCborValue::Undefined:default:break;}returnQVariant();}/*! \enum QJsonValue::Type This enum describes the type of the JSON value. \value Null A Null value \value Bool A boolean value. Use toBool() to convert to a bool. \value Double A number value. Use toDouble() to convert to a double, or toInteger() to convert to a qint64. \value String A string. Use toString() to convert to a QString. \value Array An array. Use toArray() to convert to a QJsonArray. \value Object An object. Use toObject() to convert to a QJsonObject. \value Undefined The value is undefined. This is usually returned as an error condition, when trying to read an out of bounds value in an array or a non existent key in an object.*/#endif// !QT_NO_VARIANT/*! \since 6.9 Parses \a json as a UTF-8 encoded JSON value, and creates a QJsonValue from it. Returns a valid QJsonValue if the parsing succeeds. If it fails, the returned value will be \l {QJsonValue::isUndefined} {undefined}, and the optional \a error variable will contain further details about the error. \sa QJsonParseError, isUndefined(), toJson() */ QJsonValue QJsonValue::fromJson(QByteArrayView json, QJsonParseError *error){QJsonPrivate::Parser parser(json.constData(), json.size()); QJsonValue result; result.value = parser.parse(error);return result;}/*!\if defined(qt7) \enum QJsonValue::JsonFormat \since 6.9 This value defines the format of the JSON byte array produced when converting to a QJsonValue using toJson(). \value Indented Defines human readable output as follows: \snippet code/src_corelib_serialization_qjsondocument.cpp 0 \value Compact Defines a compact output as follows: \snippet code/src_corelib_serialization_qjsondocument.cpp 1\else \typealias QJsonValue::JsonFormat \since 6.9 Same as \l QJsonDocument::JsonFormat.\endif*//*! \since 6.9 Converts the QJsonValue to a UTF-8 encoded JSON value in the provided \a format. \sa fromJson(), JsonFormat */ QByteArray QJsonValue::toJson(JsonFormat format)const{ QByteArray json;QJsonPrivate::Writer::valueToJson(value, json,0, (format ==JsonFormat::Compact));return json;}/*! Returns the type of the value. \sa QJsonValue::Type */QJsonValue::Type QJsonValue::type()const{returnconvertFromCborType(value.type());}/*! Converts the value to a bool and returns it. If type() is not bool, the \a defaultValue will be returned. */boolQJsonValue::toBool(bool defaultValue)const{switch(value.type()) {caseQCborValue::True:return true;caseQCborValue::False:return false;default:return defaultValue;}}/*! \since 5.2 Converts the value to an int and returns it. If type() is not Double or the value is not a whole number, the \a defaultValue will be returned. */intQJsonValue::toInt(int defaultValue)const{switch(value.type()) {caseQCborValue::Double: {int dblInt;if(convertDoubleTo<int>(toDouble(), &dblInt))return dblInt;break;}caseQCborValue::Integer: {constauto n = value.toInteger();if(qint64(int(n)) == n)returnint(n);break;}default:break;}return defaultValue;}/*! \since 6.0 Converts the value to an integer and returns it. If type() is not Double or the value is not a whole number representable as qint64, the \a defaultValue will be returned. */ qint64 QJsonValue::toInteger(qint64 defaultValue)const{switch(value.type()) {caseQCborValue::Integer:return value.toInteger();caseQCborValue::Double: { qint64 dblInt;if(convertDoubleTo<qint64>(toDouble(), &dblInt))return dblInt;break;}default:break;}return defaultValue;}/*! Converts the value to a double and returns it. If type() is not Double, the \a defaultValue will be returned. */doubleQJsonValue::toDouble(double defaultValue)const{return value.toDouble(defaultValue);}/*! Converts the value to a QString and returns it. If type() is not String, the \a defaultValue will be returned. \sa toStringView() */ QString QJsonValue::toString(const QString &defaultValue)const{return value.toString(defaultValue);}/*! \since 6.10 Returns the string value stored in this QJsonValue, if it is of the \l{String}{string} type. Otherwise, it returns \a defaultValue. Since QJsonValue stores strings in either US-ASCII, UTF-8 or UTF-16, the returned QAnyStringView may be in any of these encodings. This function does not allocate memory. The return value is valid until the next call to a non-const member function on this object. If this object goes out of scope, the return value is valid until the next call to a non-const member function on the parent JSON object or array. \sa toString()*/ QAnyStringView QJsonValue::toStringView(QAnyStringView defaultValue)const{return value.toStringView(defaultValue);}/*! Converts the value to a QString and returns it. If type() is not String, a null QString will be returned. \sa QString::isNull() */ QString QJsonValue::toString()const{return value.toString();}/*! Converts the value to an array and returns it. If type() is not Array, the \a defaultValue will be returned. */ QJsonArray QJsonValue::toArray(const QJsonArray &defaultValue)const{if(!isArray())return defaultValue; QCborContainerPrivate *dd =nullptr;constauto n =QJsonPrivate::Value::valueHelper(value);constauto container =QJsonPrivate::Value::container(value);Q_ASSERT(n == -1|| container ==nullptr);if(n <0) dd = container;returnQJsonArray(dd);}/*! \overload Converts the value to an array and returns it. If type() is not Array, a \l{QJsonArray::}{QJsonArray()} will be returned. */ QJsonArray QJsonValue::toArray()const{returntoArray(QJsonArray());}/*! Converts the value to an object and returns it. If type() is not Object, the \a defaultValue will be returned. */ QJsonObject QJsonValue::toObject(const QJsonObject &defaultValue)const{if(!isObject())return defaultValue; QCborContainerPrivate *dd =nullptr;constauto container =QJsonPrivate::Value::container(value);constauto n =QJsonPrivate::Value::valueHelper(value);Q_ASSERT(n == -1|| container ==nullptr);if(n <0) dd = container;returnQJsonObject(dd);}/*! \overload Converts the value to an object and returns it. If type() is not Object, the \l {QJsonObject::}{QJsonObject()} will be returned.*/ QJsonObject QJsonValue::toObject()const{returntoObject(QJsonObject());}/*! Returns a QJsonValue representing the value for the key \a key. Equivalent to calling toObject().value(key). The returned QJsonValue is QJsonValue::Undefined if the key does not exist, or if isObject() is false. \since 5.10 \sa QJsonValue, QJsonValue::isUndefined(), QJsonObject */const QJsonValue QJsonValue::operator[](const QString &key)const{return(*this)[QStringView(key)];}/*! \overload \since 5.14*/const QJsonValue QJsonValue::operator[](QStringView key)const{if(!isObject())returnQJsonValue(QJsonValue::Undefined);returntoObject().value(key);}/*! \overload \since 5.10*/const QJsonValue QJsonValue::operator[](QLatin1StringView key)const{if(!isObject())returnQJsonValue(QJsonValue::Undefined);returntoObject().value(key);}/*! Returns a QJsonValue representing the value for index \a i. Equivalent to calling toArray().at(i). The returned QJsonValue is QJsonValue::Undefined, if \a i is out of bounds, or if isArray() is false. \since 5.10 \sa QJsonValue, QJsonValue::isUndefined(), QJsonArray */const QJsonValue QJsonValue::operator[](qsizetype i)const{if(!isArray())returnQJsonValue(QJsonValue::Undefined);returntoArray().at(i);}/*! \fn bool QJsonValue::operator==(const QJsonValue &lhs, const QJsonValue &rhs) Returns \c true if the \a lhs value is equal to \a rhs value, \c false otherwise.*/boolcomparesEqual(const QJsonValue &lhs,const QJsonValue &rhs){if(lhs.value.type() != rhs.value.type()) {if(lhs.isDouble() && rhs.isDouble()) {// One value Cbor integer, one Cbor double, should interact as doubles.return lhs.toDouble() == rhs.toDouble();}return false;}switch(lhs.value.type()) {caseQCborValue::Undefined:caseQCborValue::Null:caseQCborValue::True:caseQCborValue::False:break;caseQCborValue::Double:return lhs.toDouble() == rhs.toDouble();caseQCborValue::Integer:returnQJsonPrivate::Value::valueHelper(lhs.value)==QJsonPrivate::Value::valueHelper(rhs.value);caseQCborValue::String:return lhs.toString() == rhs.toString();caseQCborValue::Array:return lhs.toArray() == rhs.toArray();caseQCborValue::Map:return lhs.toObject() == rhs.toObject();default:return false;}return true;}/*! \fn bool QJsonValue::operator!=(const QJsonValue &lhs, const QJsonValue &rhs) Returns \c true if the \a lhs value is not equal to \a rhs value, \c false otherwise.*//*! \class QJsonValueRef \inmodule QtCore \reentrant \brief The QJsonValueRef class is a helper class for QJsonValue. \internal \ingroup json When you get an object of type QJsonValueRef, if you can assign to it, the assignment will apply to the character in the string from which you got the reference. That is its whole purpose in life. You can use it exactly in the same way as a reference to a QJsonValue. The QJsonValueRef becomes invalid once modifications are made to the string: if you want to keep the character, copy it into a QJsonValue. Most of the QJsonValue member functions also exist in QJsonValueRef. However, they are not explicitly documented here.*/voidQJsonValueRef::detach(){#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) QCborContainerPrivate *d =QJsonPrivate::Value::container(*this); d =QCborContainerPrivate::detach(d, d->elements.size());if(is_object) o->o.reset(d);else a->a.reset(d);#else d =QCborContainerPrivate::detach(d, d->elements.size());#endif}static QJsonValueRef &assignToRef(QJsonValueRef &ref,const QCborValue &value,bool is_object){ QCborContainerPrivate *d =QJsonPrivate::Value::container(ref); qsizetype index =QJsonPrivate::Value::indexHelper(ref);if(is_object && value.isUndefined()) { d->removeAt(index); d->removeAt(index -1);}else{ d->replaceAt(index, value);}return ref;} QJsonValueRef &QJsonValueRef::operator=(const QJsonValue &val){detach();returnassignToRef(*this,QCborValue::fromJsonValue(val), is_object);} QJsonValueRef &QJsonValueRef::operator=(const QJsonValueRef &ref){// ### optimize more?const QCborContainerPrivate *d =QJsonPrivate::Value::container(ref); qsizetype index =QJsonPrivate::Value::indexHelper(ref);if(d ==QJsonPrivate::Value::container(*this) && index ==QJsonPrivate::Value::indexHelper(*this))return*this;// self assignmentdetach();returnassignToRef(*this, d->valueAt(index), is_object);}#ifndef QT_NO_VARIANT QVariant QJsonValueConstRef::toVariant()const{returnconcrete(*this).toVariant();}#endif// !QT_NO_VARIANT QJsonArray QJsonValueConstRef::toArray()const{returnconcrete(*this).toArray();} QJsonObject QJsonValueConstRef::toObject()const{returnconcrete(*this).toObject();}QJsonValue::Type QJsonValueConstRef::concreteType(QJsonValueConstRef self) noexcept {returnconvertFromCborType(QJsonPrivate::Value::elementHelper(self).type);}boolQJsonValueConstRef::concreteBool(QJsonValueConstRef self,bool defaultValue) noexcept {auto&e =QJsonPrivate::Value::elementHelper(self);if(e.type ==QCborValue::False)return false;if(e.type ==QCborValue::True)return true;return defaultValue;} qint64 QJsonValueConstRef::concreteInt(QJsonValueConstRef self, qint64 defaultValue,bool clamp) noexcept {auto&e =QJsonPrivate::Value::elementHelper(self); qint64 v = defaultValue;if(e.type ==QCborValue::Double) {// convertDoubleTo modifies the output even on returning falseif(!convertDoubleTo<qint64>(e.fpvalue(), &v)) v = defaultValue;}else if(e.type ==QCborValue::Integer) { v = e.value;}if(clamp &&qint64(int(v)) != v)return defaultValue;return v;}doubleQJsonValueConstRef::concreteDouble(QJsonValueConstRef self,double defaultValue) noexcept {auto&e =QJsonPrivate::Value::elementHelper(self);if(e.type ==QCborValue::Double)return e.fpvalue();if(e.type ==QCborValue::Integer)return e.value;return defaultValue;} QString QJsonValueConstRef::concreteString(QJsonValueConstRef self,const QString &defaultValue){const QCborContainerPrivate *d =QJsonPrivate::Value::container(self); qsizetype index =QJsonPrivate::Value::indexHelper(self);if(d->elements.at(index).type !=QCborValue::String)return defaultValue;return d->stringAt(index);} QAnyStringView QJsonValueConstRef::concreteStringView(QJsonValueConstRef self, QAnyStringView defaultValue){const QCborContainerPrivate *d =QJsonPrivate::Value::container(self);const qsizetype index =QJsonPrivate::Value::indexHelper(self);if(d->elements.at(index).type !=QCborValue::String)return defaultValue;return d->anyStringViewAt(index);} QJsonValue QJsonValueConstRef::concrete(QJsonValueConstRef self) noexcept {const QCborContainerPrivate *d =QJsonPrivate::Value::container(self); qsizetype index =QJsonPrivate::Value::indexHelper(self);returnQJsonPrivate::Value::fromTrustedCbor(d->valueAt(index));} QAnyStringView QJsonValueConstRef::objectKeyView(QJsonValueConstRef self){Q_ASSERT(self.is_object);const QCborContainerPrivate *d =QJsonPrivate::Value::container(self);const qsizetype index =QJsonPrivate::Value::indexHelper(self);Q_ASSERT(d);Q_ASSERT(index < d->elements.size());return d->anyStringViewAt(index -1);} QString QJsonValueConstRef::objectKey(QJsonValueConstRef self){Q_ASSERT(self.is_object);const QCborContainerPrivate *d =QJsonPrivate::Value::container(self); qsizetype index =QJsonPrivate::Value::indexHelper(self);Q_ASSERT(d);Q_ASSERT(index < d->elements.size());return d->stringAt(index -1);}#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) QVariant QJsonValueRef::toVariant()const{returnQJsonValueConstRef::toVariant();} QJsonArray QJsonValueRef::toArray()const{returnQJsonValueConstRef::toArray();} QJsonObject QJsonValueRef::toObject()const{returnQJsonValueConstRef::toObject();} QJsonValue QJsonValueRef::toValue()const{returnconcrete(*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;returnQJsonValueRef(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) {ifconstexpr(std::is_same_v<decltype(v), QUtf8StringView>)return o[QString::fromUtf8(v)];elsereturn 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;}#endifsize_tqHash(const QJsonValue &value,size_t seed){switch(value.type()) {caseQJsonValue::Null:returnqHash(nullptr, seed);caseQJsonValue::Bool:returnqHash(value.toBool(), seed);caseQJsonValue::Double:returnqHash(value.toDouble(), seed);caseQJsonValue::String:returnqHash(value.toString(), seed);caseQJsonValue::Array:returnqHash(value.toArray(), seed);caseQJsonValue::Object:returnqHash(value.toObject(), seed);caseQJsonValue::Undefined:return seed;}Q_UNREACHABLE_RETURN(0);}#if !defined(QT_NO_DEBUG_STREAM) QDebug operator<<(QDebug dbg,const QJsonValue &o){ QDebugStateSaver saver(dbg);switch(o.value.type()) {caseQCborValue::Undefined: dbg <<"QJsonValue(undefined)";break;caseQCborValue::Null: dbg <<"QJsonValue(null)";break;caseQCborValue::True:caseQCborValue::False: dbg.nospace() <<"QJsonValue(bool, "<< o.toBool() <<')';break;caseQCborValue::Integer: dbg.nospace() <<"QJsonValue(double, "<< o.toInteger() <<')';break;caseQCborValue::Double: dbg.nospace() <<"QJsonValue(double, "<< o.toDouble() <<')';break;caseQCborValue::String: dbg.nospace() <<"QJsonValue(string, "<< o.toString() <<')';break;caseQCborValue::Array: dbg.nospace() <<"QJsonValue(array, "; dbg << o.toArray(); dbg <<')';break;caseQCborValue::Map: dbg.nospace() <<"QJsonValue(object, "; dbg << o.toObject(); dbg <<')';break;default:Q_UNREACHABLE();}return dbg;}#endif#ifndef QT_NO_DATASTREAM QDataStream &operator<<(QDataStream &stream,const QJsonValue &v){ quint8 type = v.type(); stream << type;switch(type) {caseQJsonValue::Undefined:caseQJsonValue::Null:break;caseQJsonValue::Bool: stream << v.toBool();break;caseQJsonValue::Double: stream << v.toDouble();break;caseQJsonValue::String: stream << v.toString();break;caseQJsonValue::Array: stream << v.toArray();break;caseQJsonValue::Object: stream << v.toObject();break;}return stream;} QDataStream &operator>>(QDataStream &stream, QJsonValue &v){ quint8 type; stream >> type;switch(type) {caseQJsonValue::Undefined:caseQJsonValue::Null: v = QJsonValue{QJsonValue::Type(type)};break;caseQJsonValue::Bool: {bool b; stream >> b; v =QJsonValue(b);break;}caseQJsonValue::Double: {double d; stream >> d; v = QJsonValue{d};break;}caseQJsonValue::String: { QString s; stream >> s; v = QJsonValue{s};break;}caseQJsonValue::Array: { QJsonArray a; stream >> a; v = QJsonValue{a};break;}caseQJsonValue::Object: { QJsonObject o; stream >> o; v = QJsonValue{o};break;}default: { stream.setStatus(QDataStream::ReadCorruptData); v = QJsonValue{QJsonValue::Undefined};}}return stream;}#endif QT_END_NAMESPACE 
close