6.0.0-rc4Source

QJsonValue Class

The QJsonValue class encapsulates a value in JSON. More…

Header#include <QJsonValue>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Types

enumType

Public Functions

QJsonValue(Type type = Null)
QJsonValue(bool b)
QJsonValue(double n)
QJsonValue(int n)
QJsonValue(qint64 v)
QJsonValue(const QString &s)
QJsonValue(QLatin1StringView s)
QJsonValue(const char *s)
QJsonValue(const QJsonArray &a)
QJsonValue(const QJsonObject &o)
QJsonValue(const QJsonValueConstRef &ref)
QJsonValue(const QJsonValue &other)
QJsonValue(QJsonValue &&other)
~QJsonValue()
QJsonValue &operator=(const QJsonValue &other)
QJsonValue &operator=(QJsonValue &&other)
voidswap(QJsonValue &other)
QVarianttoVariant() const
Typetype() const
boolisNull() const
boolisBool() const
boolisDouble() const
boolisString() const
boolisArray() const
boolisObject() const
boolisUndefined() const
booltoBool(bool defaultValue = false) const
inttoInt(int defaultValue = 0) const
qint64toInteger(qint64 defaultValue = 0) const
doubletoDouble(double defaultValue = 0) const
QStringtoString() const
QStringtoString(const QString &defaultValue) const
QJsonArraytoArray() const
QJsonArraytoArray(const QJsonArray &defaultValue) const
QJsonObjecttoObject() const
QJsonObjecttoObject(const QJsonObject &defaultValue) const
QJsonValueoperator[](const QString &key) const
QJsonValueoperator[](QStringView key) const
QJsonValueoperator[](QLatin1StringView key) const
QJsonValueoperator[](K &&key) const
QJsonValueoperator[](qsizetype i) const

Static Public Members

QJsonValuefromVariant(const QVariant &variant)

Detailed Description

A value in JSON can be one of six basic types: a bool, a number, a string, an array, an object, or null. QJsonValue holds one of them, together with a seventh state, Undefined, which is what a lookup that finds nothing returns.

QJsonObject o = doc.object();
QJsonValue v = o.value("count");
if (v.isDouble())
    use(v.toInt());

Numbers are stored as either an exact integer or a double. Integers whose magnitude does not exceed 2^53 stay exact and are written without a decimal point; larger values and fractions are doubles. NaN and infinity cannot be represented in JSON, so they become null.

The accessor functions such as toString() and toInt() return a default value when the value has another type, and never convert between types. Values are copied cheaply: strings, arrays and objects are shared until they are modified.

See also QJsonObject, QJsonArray and QJsonDocument.

Differences from Qt

There are no constructors that take a QVariant, as in Qt 6; use fromVariant(). Overloads taking a C string key are provided for the operator[] functions of QJsonValue, QJsonObject and QJsonDocument, because QLatin1StringView converts implicitly in Nt, which would make a literal key ambiguous. A reference (QJsonValueRef) to an element that no longer exists reads as undefined instead of asserting.

Member Type Documentation

enum QJsonValue::Type

The type of a QJsonValue.

ConstantDescription
QJsonValue::NullA null value
QJsonValue::BoolA boolean
QJsonValue::DoubleA number, either an integer or a double
QJsonValue::StringA string
QJsonValue::ArrayAn array
QJsonValue::ObjectAn object
QJsonValue::UndefinedThe value of a lookup that found nothing

Member Function Documentation

QJsonValue::QJsonValue(Type type = Null)

Creates a QJsonValue of the given type, holding its default: false, 0, an empty string, an empty array or an empty object.

QJsonValue::QJsonValue(bool b)

Creates a value of type Bool holding b.

QJsonValue::QJsonValue(double n)

Creates a value of type Double holding n. NaN and infinity cannot be stored in JSON, so they give a null value.

QJsonValue::QJsonValue(int n)

This is an overloaded function.

Creates a value of type Double holding the integer n.

QJsonValue::QJsonValue(qint64 v)

This is an overloaded function.

Creates a value of type Double holding the integer v. Values up to 2^53 in magnitude are kept exact; larger ones are rounded to the nearest double.

QJsonValue::QJsonValue(const QString &s)

Creates a value of type String holding s.

QJsonValue::QJsonValue(QLatin1StringView s)

This is an overloaded function.

Creates a value of type String holding the Latin-1 text s.

QJsonValue::QJsonValue(const char *s)

This is an overloaded function.

Creates a value of type String holding the UTF-8 text s.

QJsonValue::QJsonValue(const QJsonArray &a)

Creates a value of type Array holding a.

QJsonValue::QJsonValue(const QJsonObject &o)

Creates a value of type Object holding o.

QJsonValue::QJsonValue(const QJsonValueConstRef &ref)

Creates a copy of the value that ref refers to.

QJsonValue::QJsonValue(const QJsonValue &other)

Creates a copy of other.

QJsonValue::QJsonValue(QJsonValue &&other)

Moves other into the new value, which leaves other null.

QJsonValue::~QJsonValue()

Destroys the value.

[static] QJsonValue QJsonValue::fromVariant(const QVariant &variant)

Converts variant to a QJsonValue.

QByteArray, QChar, QDate, QTime and QDateTime do (the dates and times in ISO 8601 form); otherwise Null

Double, kept as an exact integer up to 2^53

See also toVariant(), QJsonArray::fromVariantList() and QJsonObject::fromVariantMap().

bool QJsonValue::isArray() const

Returns true if the value is an array.

bool QJsonValue::isBool() const

Returns true if the value is a bool.

bool QJsonValue::isDouble() const

Returns true if the value is a number; integers count as well.

bool QJsonValue::isNull() const

Returns true if the value is null.

bool QJsonValue::isObject() const

Returns true if the value is an object.

bool QJsonValue::isString() const

Returns true if the value is a string.

bool QJsonValue::isUndefined() const

Returns true if the value is undefined. A lookup of a missing key or index gives an undefined value.

QJsonValue & QJsonValue::operator=(const QJsonValue &other)

Assigns other to this value and returns a reference to it.

QJsonValue & QJsonValue::operator=(QJsonValue &&other)

Moves other into this value, which leaves other null, and returns a reference to this value.

QJsonValue QJsonValue::operator[](const QString &key) const

Returns the value that key maps to if this value is an object, and an undefined value if it is not an object or has no such key.

See also QJsonObject::value().

QJsonValue QJsonValue::operator[](QStringView key) const

This is an overloaded function.

QJsonValue QJsonValue::operator[](QLatin1StringView key) const

This is an overloaded function.

template <typename K, typename = std::enable_if_t<QtPrivate::IsJsonCString<K>>>QJsonValue QJsonValue::operator[](K &&key) const

This is an overloaded function.

Looks key up when it is a C string, such as a string literal.

QJsonValue QJsonValue::operator[](qsizetype i) const

This is an overloaded function.

Returns the element at index i if this value is an array, and an undefined value if it is not an array or i is out of range.

See also QJsonArray::at().

void QJsonValue::swap(QJsonValue &other)

Swaps this value with other.

QJsonArray QJsonValue::toArray() const

Returns the array the value holds, or an empty array if the value is not an array.

QJsonArray QJsonValue::toArray(const QJsonArray &defaultValue) const

This is an overloaded function.

Returns the array the value holds, or defaultValue if the value is not an array.

bool QJsonValue::toBool(bool defaultValue = false) const

Returns the bool the value holds, or defaultValue if the value is not a bool.

double QJsonValue::toDouble(double defaultValue = 0) const

Returns the value as a double, or defaultValue if the value is not a number.

See also toInt() and toInteger().

int QJsonValue::toInt(int defaultValue = 0) const

Returns the value as an int, or defaultValue if the value is not a number, is not a whole number or does not fit in an int.

See also toInteger() and toDouble().

qint64 QJsonValue::toInteger(qint64 defaultValue = 0) const

Returns the value as a qint64, or defaultValue if the value is not a number, is not a whole number or does not fit in a qint64.

See also toInt() and toDouble().

QJsonObject QJsonValue::toObject() const

Returns the object the value holds, or an empty object if the value is not an object.

QJsonObject QJsonValue::toObject(const QJsonObject &defaultValue) const

This is an overloaded function.

Returns the object the value holds, or defaultValue if the value is not an object.

QString QJsonValue::toString() const

Returns the string the value holds, or a null string if the value is not a string.

QString QJsonValue::toString(const QString &defaultValue) const

This is an overloaded function.

Returns the string the value holds, or defaultValue if the value is not a string.

QVariant QJsonValue::toVariant() const

Converts the value to a QVariant.

double

See also fromVariant(), QJsonArray::toVariantList() and QJsonObject::toVariantMap().

Type QJsonValue::type() const

Returns the type of the value.

See also isNull(), isBool(), isDouble(), isString(), isArray(), isObject() and isUndefined().