CopperSpice API  1.7.2
QJsonValue Class Reference

Encapsulates a value in JSON. More...

Public Types

enum  Type
 

Public Methods

 QJsonValue (bool b)
 
 QJsonValue (const QJsonValue &other)
 
 QJsonValue (double n)
 
 QJsonValue (int n)
 
 QJsonValue (qint64 n)
 
 QJsonValue (QJsonArray array)
 
 QJsonValue (QJsonObject object)
 
 QJsonValue (QString str)
 
 QJsonValue (Type type=Null)
 
 ~QJsonValue ()
 
bool isArray () const
 
bool isBool () const
 
bool isDouble () const
 
bool isNull () const
 
bool isObject () const
 
bool isString () const
 
bool isUndefined () const
 
bool operator!= (const QJsonValue &other) const
 
QJsonValue & operator= (const QJsonValue &other)
 
bool operator== (const QJsonValue &other) const
 
QJsonArray toArray () const
 
QJsonArray toArray (const QJsonArray &defaultValue) const
 
bool toBool (bool defaultValue=false) const
 
double toDouble (double defaultValue=0) const
 
int toInt (int defaultValue=0) const
 
QJsonObject toObject () const
 
QJsonObject toObject (const QJsonObject &defaultValue) const
 
QString toString (const QString &defaultValue=QString ()) const
 
QVariant toVariant () const
 
Type type () const
 

Static Public Methods

static QJsonValue fromVariant (const QVariant &variant)
 

Detailed Description

The QJsonValue class encapsulates the value type available in a JSON format. A value can only be one of the data types shown below.

  • bool
  • double
  • string
  • array
  • object
  • null

There is one extra QJsonValue which is used to represent undefined values.

The data type of a value can be queried using type() or with the methods like isBool(), isDouble(), isString(), isArray(), and isObject(). The method isUndefined() is used to query if the data type is Type::Undefined.

The value stored in a QJsonValue can be retrieved by using toBool(), toDouble(), toInt(), toString(), toArray(), and toObject().

See also
JSON Support

Member Enumeration Documentation

This enum describes the type of the JSON value.

ConstantValueDescription
QJsonValue::Null0x0
QJsonValue::Bool0x1 toBool() to convert to a bool
QJsonValue::Double0x2 toDouble() to convert to a double
QJsonValue::String0x3 toString() to convert to a QString
QJsonValue::Array0x4 toArray() to convert to a QJsonArray
QJsonValue::Object0x5 toObject() to convert to a QJsonObject
QJsonValue::Undefined0x80 Usually the result of an error when trying to read an out of bounds value in an array or a non existent key in an object

Constructor & Destructor Documentation

QJsonValue::QJsonValue ( Type  type = Null)

Creates a QJsonValue of type type. The default is to create a null value.

QJsonValue::QJsonValue ( bool  b)

Creates a value of type bool with value b.

QJsonValue::QJsonValue ( double  n)

Creates a value of type double with value n.

QJsonValue::QJsonValue ( int  n)

Creates a value of type double with value n.

QJsonValue::QJsonValue ( qint64  n)

Creates a value of type double, with value n.

Note
The integer limits for IEEE 754 double precision data is 2^53 (-9007199254740992 to +9007199254740992). If you pass in a value outside this range expect a loss of precision to occur.
QJsonValue::QJsonValue ( QString  str)

Creates a value of type string with value str.

QJsonValue::QJsonValue ( QJsonArray  array)

Creates a value of type array with value array.

QJsonValue::QJsonValue ( QJsonObject  object)

Creates a value of type object with value object.

QJsonValue::QJsonValue ( const QJsonValue &  other)

Creates a copy of other.

QJsonValue::~QJsonValue ( )

Destroys the value.

Method Documentation

QJsonValue QJsonValue::fromVariant ( const QVariant variant)
static

Converts variant to a QJsonValue and returns the converted value. For all other QVariant types which are not shown in the table below, a conversion to a QString will be attempted. If the returned string is empty a null QJsonValue will be returned.

Source TypeDestination Type
QVariant::Bool QJsonValue::Bool
QVariant::Int
QVariant::UInt
QVariant::LongLong
QVariant::ULongLong
QVariant::Double
QVariant::Float
QJsonValue::Double
QVariant::String QJsonValue::String
QVariant::StringList
QVariant::List
QJsonValue::Array
QVariant::Hash
QVariant::Map
QJsonValue::Object
See also
toVariant()
bool QJsonValue::isArray ( ) const
inline

Returns true if the value contains an array.

See also
toArray()
bool QJsonValue::isBool ( ) const
inline

Returns true if the value contains a boolean.

See also
toBool()
bool QJsonValue::isDouble ( ) const
inline

Returns true if the value contains a double.

See also
toDouble()
bool QJsonValue::isNull ( ) const
inline

Returns true if the value is null.

bool QJsonValue::isObject ( ) const
inline

Returns true if the value contains an object.

See also
toObject()
bool QJsonValue::isString ( ) const
inline

Returns true if the value contains a string.

See also
toString()
bool QJsonValue::isUndefined ( ) const
inline

Returns true if the value is undefined. This can happen in certain error cases, for example accessing a non existing key in a QJsonObject.

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

Returns true if the value is not equal to other.

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

Copy assigns from other and returns a reference to this object.

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

Returns true if the value is equal to other.

QJsonArray QJsonValue::toArray ( ) const

Returns the value as a QJsonArray. If the type() is not an array an empty array will be returned.

QJsonArray QJsonValue::toArray ( const QJsonArray defaultValue) const

Returns the value as a QJsonArray. If the type() is not an array the defaultValue will be returned.

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

Returns the value as a bool. If the type() is not a bool the defaultValue will be returned.

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

Returns the value as a double. If the type() is not a double the defaultValue will be returned.

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

Returns the value as an int. If the type() is not a double the defaultValue will be returned.

QJsonObject QJsonValue::toObject ( ) const

Returns the value as an object. If the type() is not an object an empty object will be returned.

QJsonObject QJsonValue::toObject ( const QJsonObject defaultValue) const

Returns the value as an object. If the type() is not an object the defaultValue will be returned.

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

Returns the value as a string. If the type() is not a string the defaultValue will be returned.

QVariant QJsonValue::toVariant ( ) const

Converts the given value to a QVariant. The QJsonValue will be converted to a QVariant of the corresponding type.

JSON Data TypeDescription
QJsonValue::Null Empty QVariant
QJsonValue::Undefined Empty QVariant
QJsonValue::Bool QVariant::Bool
QJsonValue::Double QVariant::Double
QJsonValue::String QString
QJsonValue::Array QVariantList
QJsonValue::Object QVariantMap
See also
fromVariant()
Type QJsonValue::type ( ) const

Returns the type of the value.

See also
QJsonValue::Type