Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
QJsonValue Class Reference

\inmodule QtCore\reentrant More...

#include <qjsonvalue.h>

Collaboration diagram for QJsonValue:

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...
enum class  JsonFormat { Indented , Compact }

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.
QJsonValueoperator= (const QJsonValue &other) noexcept
 Assigns the value stored in other to this object.
 QJsonValue (QJsonValue &&other) noexcept
QJsonValueoperator= (QJsonValue &&other) noexcept
void swap (QJsonValue &other) noexcept
QVariant toVariant () const
 Converts the value to a \l {QVariant::}{QVariant()}.
QByteArray toJson (JsonFormat format=JsonFormat::Indented) const
 \typealias QJsonValue::JsonFormat
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.
QAnyStringView toStringView (QAnyStringView defaultValue={}) const
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.

Static Public Member Functions

static QJsonValue fromVariant (const QVariant &variant)
 Converts variant to a QJsonValue and returns it.
static QJsonValue fromJson (QByteArrayView json, QJsonParseError *error=nullptr)

Friends

class QJsonPrivate::Value
class QJsonArray
class QJsonObject
class QCborValue
Q_CORE_EXPORT bool comparesEqual (const QJsonValue &lhs, const QJsonValue &rhs)
Q_CORE_EXPORT QDebug operator<< (QDebug dbg, const QJsonValue &o)
Q_CORE_EXPORT QDataStreamoperator<< (QDataStream &stream, const QJsonValue &v)

Detailed Description

\inmodule QtCore

\reentrant

Since
5.0

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

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 29 of file qjsonvalue.h.

Member Enumeration Documentation

◆ JsonFormat

enum class QJsonValue::JsonFormat
strong
Enumerator
Indented 
Compact 

Definition at line 45 of file qjsonvalue.h.

◆ 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 32 of file qjsonvalue.h.

Constructor & Destructor Documentation

◆ QJsonValue() [1/14]

QJsonValue::QJsonValue ( Type type = Null)

Creates a QJsonValue of type type.

The default is to create a Null value.

Definition at line 114 of file qjsonvalue.cpp.

◆ QJsonValue() [2/14]

QJsonValue::QJsonValue ( bool b)

Creates a value of type Bool, with value b.

Definition at line 143 of file qjsonvalue.cpp.

◆ QJsonValue() [3/14]

QJsonValue::QJsonValue ( double n)

Creates a value of type Double, with value v.

Definition at line 162 of file qjsonvalue.cpp.

◆ QJsonValue() [4/14]

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 171 of file qjsonvalue.cpp.

◆ QJsonValue() [5/14]

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.

This is stored internally as a 64-bit integer, so retains its full precision, as long as it is retrieved with \l toInteger(). However, retrieving its value with \l toDouble() will lose precision unless the value lies between ±2^53.

See also
toInteger(), toDouble()

Definition at line 187 of file qjsonvalue.cpp.

◆ QJsonValue() [6/14]

QJsonValue::QJsonValue ( const QString & s)

Creates a value of type String, with value s.

Definition at line 195 of file qjsonvalue.cpp.

◆ QJsonValue() [7/14]

QJsonValue::QJsonValue ( QLatin1StringView s)

Creates a value of type String, with the Latin-1 string viewed by s.

Definition at line 215 of file qjsonvalue.cpp.

◆ QJsonValue() [8/14]

QT_ASCII_CAST_WARN QJsonValue::QJsonValue ( const char * s)
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.

Since
5.3

Definition at line 59 of file qjsonvalue.h.

◆ QJsonValue() [9/14]

QJsonValue::QJsonValue ( const QJsonArray & a)

Creates a value of type Array, with value a.

Definition at line 223 of file qjsonvalue.cpp.

◆ QJsonValue() [10/14]

QJsonValue::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.

Since
6.3

Definition at line 232 of file qjsonvalue.cpp.

◆ QJsonValue() [11/14]

QJsonValue::QJsonValue ( const QJsonObject & o)

Creates a value of type Object, with value o.

Definition at line 240 of file qjsonvalue.cpp.

◆ QJsonValue() [12/14]

QJsonValue::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.

Since
6.3

Definition at line 249 of file qjsonvalue.cpp.

◆ ~QJsonValue()

QJsonValue::~QJsonValue ( )
default

Destroys the value.

◆ QJsonValue() [13/14]

QJsonValue::QJsonValue ( const QJsonValue & other)
defaultnoexcept

Creates a copy of other.

◆ QJsonValue() [14/14]

QJsonValue::QJsonValue ( QJsonValue && other)
noexcept
Since
5.10

Move-constructs a QJsonValue from other.

Definition at line 275 of file qjsonvalue.cpp.

Member Function Documentation

◆ fromJson()

QJsonValue QJsonValue::fromJson ( QByteArrayView json,
QJsonParseError * error = nullptr )
static
Since
6.9 Parses 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 error variable will contain further details about the error.

See also
QJsonParseError, isUndefined(), toJson()

Definition at line 619 of file qjsonvalue.cpp.

◆ fromVariant()

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

Converts variant to a QJsonValue and returns it.

The conversion will convert QVariant types as follows:

\table \header

\row

Definition at line 482 of file qjsonvalue.cpp.

◆ isArray()

bool QJsonValue::isArray ( ) const
inline

Returns true if the value contains an array.

See also
toArray()

Definition at line 94 of file qjsonvalue.h.

◆ isBool()

bool QJsonValue::isBool ( ) const
inline

Returns true if the value contains a boolean.

See also
toBool()

Definition at line 91 of file qjsonvalue.h.

◆ isDouble()

bool QJsonValue::isDouble ( ) const
inline

Returns true if the value contains a double.

See also
toDouble()

Definition at line 92 of file qjsonvalue.h.

◆ isNull()

bool QJsonValue::isNull ( ) const
inline

Returns true if the value is null.

Definition at line 90 of file qjsonvalue.h.

◆ isObject()

bool QJsonValue::isObject ( ) const
inline

Returns true if the value contains an object.

See also
toObject()

Definition at line 95 of file qjsonvalue.h.

◆ isString()

bool QJsonValue::isString ( ) const
inline

Returns true if the value contains a string.

See also
toString()

Definition at line 93 of file qjsonvalue.h.

◆ isUndefined()

bool QJsonValue::isUndefined ( ) const
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 96 of file qjsonvalue.h.

◆ operator=() [1/2]

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

Assigns the value stored in other to this object.

Definition at line 268 of file qjsonvalue.cpp.

◆ operator=() [2/2]

QJsonValue & QJsonValue::operator= ( QJsonValue && other)
inlinenoexcept
Since
5.10

Move-assigns other to this value.

Definition at line 74 of file qjsonvalue.h.

◆ operator[]() [1/4]

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.

Since
5.10
See also
QJsonValue, QJsonValue::isUndefined(), QJsonObject

Definition at line 868 of file qjsonvalue.cpp.

◆ operator[]() [2/4]

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.

Since
5.10

Definition at line 889 of file qjsonvalue.cpp.

◆ operator[]() [3/4]

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.

Since
5.10
See also
QJsonValue, QJsonValue::isUndefined(), QJsonArray

Definition at line 909 of file qjsonvalue.cpp.

◆ operator[]() [4/4]

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.

Since
5.14

Definition at line 877 of file qjsonvalue.cpp.

◆ swap()

void QJsonValue::swap ( QJsonValue & other)
noexcept
Since
5.10 \memberswap{value}

Definition at line 281 of file qjsonvalue.cpp.

◆ toArray() [1/2]

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 821 of file qjsonvalue.cpp.

◆ toArray() [2/2]

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 801 of file qjsonvalue.cpp.

◆ toBool()

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 678 of file qjsonvalue.cpp.

◆ toDouble()

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 747 of file qjsonvalue.cpp.

◆ toInt()

int QJsonValue::toInt ( int defaultValue = 0) const
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 defaultValue will be returned.

Definition at line 697 of file qjsonvalue.cpp.

◆ toInteger()

qint64 QJsonValue::toInteger ( qint64 defaultValue = 0) const
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 defaultValue will be returned.

Definition at line 725 of file qjsonvalue.cpp.

◆ toJson()

QByteArray QJsonValue::toJson ( JsonFormat format = JsonFormat::Indented) const

\typealias QJsonValue::JsonFormat

Since
6.9

Same as \l QJsonDocument::JsonFormat.

Since
6.9 Converts the QJsonValue to a UTF-8 encoded JSON value in the provided format.
See also
fromJson(), JsonFormat

Definition at line 654 of file qjsonvalue.cpp.

◆ toObject() [1/2]

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 851 of file qjsonvalue.cpp.

◆ toObject() [2/2]

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 831 of file qjsonvalue.cpp.

◆ toString() [1/2]

QString QJsonValue::toString ( ) const

Converts the value to a QString and returns it.

If type() is not String, a null QString will be returned.

See also
QString::isNull()

Definition at line 791 of file qjsonvalue.cpp.

◆ toString() [2/2]

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

Converts the value to a QString and returns it.

If type() is not String, the defaultValue will be returned.

See also
toStringView()

Definition at line 759 of file qjsonvalue.cpp.

◆ toStringView()

QAnyStringView QJsonValue::toStringView ( QAnyStringView defaultValue = {}) const
Since
6.10

Returns the string value stored in this QJsonValue, if it is of the \l{String}{string} type. Otherwise, it returns 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.

See also
toString()

Definition at line 779 of file qjsonvalue.cpp.

◆ toVariant()

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()}

See also
fromVariant()

Definition at line 563 of file qjsonvalue.cpp.

◆ type()

QJsonValue::Type QJsonValue::type ( ) const

Returns the type of the value.

See also
QJsonValue::Type

Definition at line 668 of file qjsonvalue.cpp.

◆ comparesEqual

Q_CORE_EXPORT bool comparesEqual ( const QJsonValue & lhs,
const QJsonValue & rhs )
friend

Definition at line 922 of file qjsonvalue.cpp.

◆ operator<< [1/2]

Q_CORE_EXPORT QDataStream & operator<< ( QDataStream & stream,
const QJsonValue & v )
friend

Definition at line 1261 of file qjsonvalue.cpp.

◆ operator<< [2/2]

Q_CORE_EXPORT QDebug operator<< ( QDebug dbg,
const QJsonValue & o )
friend

Definition at line 1220 of file qjsonvalue.cpp.

◆ QCborValue

friend class QCborValue
friend

Definition at line 130 of file qjsonvalue.h.

◆ QJsonArray

friend class QJsonArray
friend

Definition at line 128 of file qjsonvalue.h.

◆ QJsonObject

friend class QJsonObject
friend

Definition at line 129 of file qjsonvalue.h.

◆ QJsonPrivate::Value

friend class QJsonPrivate::Value
friend

Definition at line 127 of file qjsonvalue.h.


The documentation for this class was generated from the following files: