![]() |
Qt 6.x
The Qt SDK
|
\inmodule QtCore\reentrant More...
#include <qjsonvalue.h>
Public Types | |
enum | Type { Null = 0x0 , Bool = 0x1 , Double = 0x2 , String = 0x3 , Array = 0x4 , Object = 0x5 , Undefined = 0x80 } |
This enum describes the type of the JSON value. More... | |
Public Member Functions | |
QJsonValue (Type=Null) | |
Creates a QJsonValue of type type. | |
QJsonValue (bool b) | |
Creates a value of type Bool, with value b. | |
QJsonValue (double n) | |
Creates a value of type Double, with value v. | |
QJsonValue (int n) | |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Creates a value of type Double, with value v. | |
QJsonValue (qint64 v) | |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Creates a value of type Double, with value v. | |
QJsonValue (const QString &s) | |
Creates a value of type String, with value s. | |
QJsonValue (QLatin1StringView s) | |
Creates a value of type String, with the Latin-1 string viewed by s. | |
QT_ASCII_CAST_WARN | QJsonValue (const char *s) |
Creates a value of type String with value s, assuming UTF-8 encoding of the input. | |
QJsonValue (const QJsonArray &a) | |
Creates a value of type Array, with value a. | |
QJsonValue (QJsonArray &&a) noexcept | |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
QJsonValue (const QJsonObject &o) | |
Creates a value of type Object, with value o. | |
QJsonValue (QJsonObject &&o) noexcept | |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
~QJsonValue () | |
Destroys the value. | |
QJsonValue (const QJsonValue &other) noexcept | |
Creates a copy of other. | |
QJsonValue & | operator= (const QJsonValue &other) noexcept |
Assigns the value stored in other to this object. | |
QJsonValue (QJsonValue &&other) noexcept | |
QJsonValue & | operator= (QJsonValue &&other) noexcept |
void | swap (QJsonValue &other) noexcept |
QVariant | toVariant () const |
Converts the value to a \l {QVariant::}{QVariant()}. | |
Type | type () const |
Returns the type of the value. | |
bool | isNull () const |
Returns true if the value is null. | |
bool | isBool () const |
Returns true if the value contains a boolean. | |
bool | isDouble () const |
Returns true if the value contains a double. | |
bool | isString () const |
Returns true if the value contains a string. | |
bool | isArray () const |
Returns true if the value contains an array. | |
bool | isObject () const |
Returns true if the value contains an object. | |
bool | isUndefined () const |
Returns true if the value is undefined. | |
bool | toBool (bool defaultValue=false) const |
Converts the value to a bool and returns it. | |
int | toInt (int defaultValue=0) const |
qint64 | toInteger (qint64 defaultValue=0) const |
double | toDouble (double defaultValue=0) const |
Converts the value to a double and returns it. | |
QString | toString () const |
Converts the value to a QString and returns it. | |
QString | toString (const QString &defaultValue) const |
Converts the value to a QString and returns it. | |
QJsonArray | toArray () const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Converts the value to an array and returns it. | |
QJsonArray | toArray (const QJsonArray &defaultValue) const |
Converts the value to an array and returns it. | |
QJsonObject | toObject () const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Converts the value to an object and returns it. | |
QJsonObject | toObject (const QJsonObject &defaultValue) const |
Converts the value to an object and returns it. | |
const QJsonValue | operator[] (const QString &key) const |
Returns a QJsonValue representing the value for the key key. | |
const QJsonValue | operator[] (QStringView key) const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
const QJsonValue | operator[] (QLatin1StringView key) const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. | |
const QJsonValue | operator[] (qsizetype i) const |
Returns a QJsonValue representing the value for index i. | |
bool | operator== (const QJsonValue &other) const |
Returns true if the value is equal to other. | |
bool | operator!= (const QJsonValue &other) const |
Returns true if the value is not equal to other. | |
Static Public Member Functions | |
static QJsonValue | fromVariant (const QVariant &variant) |
Converts variant to a QJsonValue and returns it. | |
Friends | |
class | QJsonPrivate::Value |
class | QJsonArray |
class | QJsonObject |
class | QCborValue |
Q_CORE_EXPORT QDebug | operator<< (QDebug, const QJsonValue &) |
Q_CORE_EXPORT QDataStream & | operator<< (QDataStream &, const QJsonValue &) |
\inmodule QtCore
\reentrant
The QJsonValue class encapsulates a value in JSON.
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
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.
Definition at line 23 of file qjsonvalue.h.
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.
Enumerator | |
---|---|
Null | |
Bool | |
Double | |
String | |
Array | |
Object | |
Undefined |
Definition at line 26 of file qjsonvalue.h.
Creates a QJsonValue of type type.
The default is to create a Null value.
Definition at line 107 of file qjsonvalue.cpp.
References QCborValue::Array, Array, Bool, QCborValue::Double, Double, QCborValue::False, QCborValue::Map, QCborValue::Null, Null, Object, QCborValue::String, String, and Undefined.
QJsonValue::QJsonValue | ( | bool | b | ) |
Creates a value of type Bool, with value b.
Definition at line 136 of file qjsonvalue.cpp.
QJsonValue::QJsonValue | ( | double | n | ) |
Creates a value of type Double, with value v.
Definition at line 155 of file qjsonvalue.cpp.
QJsonValue::QJsonValue | ( | int | n | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Creates a value of type Double, with value v.
Definition at line 164 of file qjsonvalue.cpp.
QJsonValue::QJsonValue | ( | qint64 | v | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Creates a value of type Double, with value 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.
Definition at line 175 of file qjsonvalue.cpp.
QJsonValue::QJsonValue | ( | const QString & | s | ) |
Creates a value of type String, with value s.
Definition at line 183 of file qjsonvalue.cpp.
QJsonValue::QJsonValue | ( | QLatin1StringView | s | ) |
Creates a value of type String, with the Latin-1 string viewed by s.
Definition at line 203 of file qjsonvalue.cpp.
|
inline |
Creates a value of type String with value s, assuming UTF-8 encoding of the input.
You can disable this constructor by defining QT_NO_CAST_FROM_ASCII
when you compile your applications.
Definition at line 44 of file qjsonvalue.h.
QJsonValue::QJsonValue | ( | const QJsonArray & | a | ) |
Creates a value of type Array, with value a.
Definition at line 211 of file qjsonvalue.cpp.
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 220 of file qjsonvalue.cpp.
QJsonValue::QJsonValue | ( | const QJsonObject & | o | ) |
Creates a value of type Object, with value o.
Definition at line 228 of file qjsonvalue.cpp.
|
noexcept |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 237 of file qjsonvalue.cpp.
|
default |
Destroys the value.
|
defaultnoexcept |
Creates a copy of other.
|
noexcept |
Move-constructs a QJsonValue from other.
Definition at line 263 of file qjsonvalue.cpp.
References other().
|
static |
Converts variant to a QJsonValue and returns it.
The conversion will convert QVariant types as follows:
\table \header
\row
Definition at line 470 of file qjsonvalue.cpp.
References QJsonDocument::array(), QJsonArray::fromStringList(), QJsonObject::fromVariantHash(), QJsonArray::fromVariantList(), QJsonObject::fromVariantMap(), QUrl::FullyEncoded, QMetaType::id(), QJsonDocument::isArray(), QVariant::metaType(), Null, QJsonDocument::object(), Q_FALLTHROUGH, qt_is_finite(), QVariant::toBool(), QVariant::toDouble(), QVariant::toHash(), QVariant::toJsonArray(), QVariant::toJsonDocument(), QVariant::toJsonObject(), QVariant::toJsonValue(), QVariant::toList(), QVariant::toLongLong(), QVariant::toMap(), QVariant::toString(), QUrl::toString(), QUuid::toString(), QVariant::toStringList(), QVariant::toULongLong(), QVariant::toUrl(), QVariant::toUuid(), variant, and QUuid::WithoutBraces.
Referenced by QJsonObject::fromVariantHash().
|
inline |
Returns true
if the value contains an array.
Definition at line 75 of file qjsonvalue.h.
Referenced by matches(), operator[](), QMapboxCommon::parseGeoLocation(), QGeoRouteParserOsrmV5Private::parseReply(), readGpuFeatures(), readInputFile(), and toArray().
|
inline |
Returns true
if the value contains a boolean.
Definition at line 72 of file qjsonvalue.h.
Referenced by readInputFile().
|
inline |
Returns true
if the value contains a double.
Definition at line 73 of file qjsonvalue.h.
References Double.
Referenced by TileProvider::onNetworkReplyFinished(), operator==(), and QGeoRouteParserOsrmV5Private::parseReply().
|
inline |
Returns true
if the value is null.
Definition at line 71 of file qjsonvalue.h.
References Null.
Referenced by exportFeature().
|
inline |
Returns true
if the value contains an object.
Definition at line 76 of file qjsonvalue.h.
Referenced by operator[](), operator[](), QMapboxCommon::parseGeoLocation(), QGeoRouteParserOsrmV5Private::parseReply(), readInputFile(), readInputFileDirectory(), and toObject().
|
inline |
Returns true
if the value contains a string.
Definition at line 74 of file qjsonvalue.h.
Referenced by matches(), TileProvider::onNetworkReplyFinished(), readInputFile(), and readInputFileDirectory().
|
inline |
Returns true
if the value is undefined.
This can happen in certain error cases as e.g. accessing a non existing key in a QJsonObject.
Definition at line 77 of file qjsonvalue.h.
References Undefined.
Referenced by readInputFile(), and readInputFileDirectory().
bool QJsonValue::operator!= | ( | const QJsonValue & | other | ) | const |
Returns true
if the value is not equal to other.
Definition at line 862 of file qjsonvalue.cpp.
References other().
|
noexcept |
Assigns the value stored in other to this object.
Definition at line 256 of file qjsonvalue.cpp.
References copy(), other(), and swap().
|
inlinenoexcept |
Move-assigns other to this value.
Definition at line 59 of file qjsonvalue.h.
References other(), and swap().
bool QJsonValue::operator== | ( | const QJsonValue & | other | ) | const |
Returns true
if the value is equal to other.
Definition at line 826 of file qjsonvalue.cpp.
References QCborValue::Array, QCborValue::Double, QCborValue::False, QCborValue::Integer, isDouble(), QCborValue::Map, QCborValue::Null, other(), QCborValue::String, toArray(), toDouble(), toObject(), toString(), QCborValue::True, QCborValue::Undefined, and QJsonPrivate::Value::valueHelper().
const QJsonValue QJsonValue::operator[] | ( | const QString & | key | ) | const |
Returns a QJsonValue representing the value for the key key.
Equivalent to calling toObject().value(key).
The returned QJsonValue is QJsonValue::Undefined if the key does not exist, or if isObject() is false.
Definition at line 774 of file qjsonvalue.cpp.
const QJsonValue QJsonValue::operator[] | ( | QLatin1StringView | key | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 795 of file qjsonvalue.cpp.
References isObject(), toObject(), Undefined, and QJsonObject::value().
const QJsonValue QJsonValue::operator[] | ( | qsizetype | i | ) | const |
Returns a QJsonValue representing the value for index i.
Equivalent to calling toArray().at(i).
The returned QJsonValue is QJsonValue::Undefined, if i is out of bounds, or if isArray() is false.
Definition at line 815 of file qjsonvalue.cpp.
References QJsonArray::at(), i, isArray(), toArray(), and Undefined.
const QJsonValue QJsonValue::operator[] | ( | QStringView | key | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 783 of file qjsonvalue.cpp.
References isObject(), toObject(), Undefined, and QJsonObject::value().
|
noexcept |
Swaps the value other with this. This operation is very fast and never fails.
Definition at line 269 of file qjsonvalue.cpp.
References other().
QJsonArray QJsonValue::toArray | ( | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Converts the value to an array and returns it.
If type() is not Array, a \l{QJsonArray::}{QJsonArray()} will be returned.
Definition at line 727 of file qjsonvalue.cpp.
References QJsonArray, and toArray().
Referenced by QSSGQmlUtilities::applyEdit(), constructRoute(), QDefaultOutputMapping::load(), matches(), operator==(), operator[](), QT_BEGIN_NAMESPACE::parseDocument(), QMapboxCommon::parseGeoLocation(), QGeoRouteParserOsrmV4Private::parseReply(), QGeoRouteParserOsrmV5Private::parseStep(), readGpuFeatures(), readInputFile(), toArray(), QJsonValueConstRef::toArray(), and toVariant().
QJsonArray QJsonValue::toArray | ( | const QJsonArray & | defaultValue | ) | const |
Converts the value to an array and returns it.
If type() is not Array, the defaultValue will be returned.
Definition at line 707 of file qjsonvalue.cpp.
References QJsonPrivate::Value::container(), isArray(), Q_ASSERT, QJsonArray, and QJsonPrivate::Value::valueHelper().
bool QJsonValue::toBool | ( | bool | defaultValue = false | ) | const |
Converts the value to a bool and returns it.
If type() is not bool, the defaultValue will be returned.
Definition at line 606 of file qjsonvalue.cpp.
References QCborValue::False, and QCborValue::True.
Referenced by readInputFile().
double QJsonValue::toDouble | ( | double | defaultValue = 0 | ) | const |
Converts the value to a double and returns it.
If type() is not Double, the defaultValue will be returned.
Definition at line 675 of file qjsonvalue.cpp.
Referenced by constructRoute(), instructionDepart(), TileProvider::onNetworkReplyFinished(), operator==(), parseCoordinate(), QMapboxCommon::parseGeoLocation(), QT_BEGIN_NAMESPACE::parseLocation(), QGeoRouteParserOsrmV5Private::parseStep(), toInt(), toInteger(), and toVariant().
int QJsonValue::toInt | ( | int | defaultValue = 0 | ) | const |
If type() is not Double or the value is not a whole number, the defaultValue will be returned.
Definition at line 625 of file qjsonvalue.cpp.
References QCborValue::Double, QCborValue::Integer, and toDouble().
Referenced by instructionRotary(), instructionRoundabout(), and V4CommandHandler::requestSequenceNr().
If type() is not Double or the value is not a whole number representable as qint64, the defaultValue will be returned.
Definition at line 653 of file qjsonvalue.cpp.
References QCborValue::Double, QCborValue::Integer, and toDouble().
Referenced by toVariant().
QJsonObject QJsonValue::toObject | ( | ) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Converts the value to an object and returns it.
If type() is not Object, the \l {QJsonObject::}{QJsonObject()} will be returned.
Definition at line 757 of file qjsonvalue.cpp.
References QJsonObject, and toObject().
Referenced by GeoRouteJsonParserEsri::GeoRouteJsonParserEsri(), QDefaultOutputMapping::load(), QGtk3Json::load(), QKmsScreenConfig::loadConfig(), QGeoTiledMappingManagerEngineNokia::loadCopyrightsDescriptorsFromJson(), operator==(), operator[](), operator[](), CategoryParser::parse(), parseAddressObject(), parseCategories(), parseContactDetails(), QT_BEGIN_NAMESPACE::parseDocument(), QT_BEGIN_NAMESPACE::parseLocation(), QT_BEGIN_NAMESPACE::parsePlaceResult(), QGeoRouteParserOsrmV5Private::parseReply(), QGeoRouteParserOsrmV4Private::parseReply(), parseReview(), QGeoRouteParserOsrmV5Private::parseStep(), readInputFile(), readInputFileDirectory(), QOffscreenIntegration::setConfiguration(), toObject(), QJsonValueConstRef::toObject(), and toVariant().
QJsonObject QJsonValue::toObject | ( | const QJsonObject & | defaultValue | ) | const |
Converts the value to an object and returns it.
If type() is not Object, the defaultValue will be returned.
Definition at line 737 of file qjsonvalue.cpp.
References QJsonPrivate::Value::container(), isObject(), Q_ASSERT, QJsonObject, and QJsonPrivate::Value::valueHelper().
QString QJsonValue::toString | ( | ) | const |
Converts the value to a QString and returns it.
If type() is not String, a null QString will be returned.
Definition at line 697 of file qjsonvalue.cpp.
Referenced by QSSGQmlUtilities::addResource(), constructRoute(), UnknownV4CommandHandler::handleRequest(), instructionDirection(), instructionRotary(), instructionText(), instructionUseLane(), matches(), TileProvider::onNetworkReplyFinished(), operator==(), parseBoundingBox(), QMapboxCommon::parseGeoLocation(), QT_BEGIN_NAMESPACE::parseLocation(), QT_BEGIN_NAMESPACE::parsePlaceResult(), QGeoRouteParserOsrmV4Private::parseReply(), QGeoRouteParserOsrmV5Private::parseStep(), readInputFile(), readInputFileDirectory(), QV4DebugClient::response(), toVariant(), and QGeoRouteParserOsrmV5ExtensionMapbox::updateSegment().
Converts the value to a QString and returns it.
If type() is not String, the defaultValue will be returned.
Definition at line 685 of file qjsonvalue.cpp.
QVariant QJsonValue::toVariant | ( | ) | const |
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()}
Definition at line 548 of file qjsonvalue.cpp.
References QCborValue::Array, QCborValue::Double, QCborValue::False, QVariant::fromValue(), QCborValue::Integer, QCborValue::Map, QCborValue::Null, QCborValue::String, toArray(), toDouble(), toInteger(), toObject(), toString(), QJsonArray::toVariantList(), QJsonObject::toVariantMap(), QCborValue::True, and QCborValue::Undefined.
Referenced by QGeoRouteParserOsrmV5Private::parseStep(), and QJsonValueConstRef::toVariant().
QJsonValue::Type QJsonValue::type | ( | ) | const |
Returns the type of the value.
Definition at line 596 of file qjsonvalue.cpp.
References convertFromCborType().
Referenced by matches().
|
friend |
Definition at line 1146 of file qjsonvalue.cpp.
|
friend |
Definition at line 1105 of file qjsonvalue.cpp.
|
friend |
Definition at line 104 of file qjsonvalue.h.
|
friend |
Definition at line 102 of file qjsonvalue.h.
|
friend |
Definition at line 103 of file qjsonvalue.h.
Referenced by toObject(), and toObject().
|
friend |
Definition at line 101 of file qjsonvalue.h.