Signals | Public Member Functions | Static Public Member Functions | Friends
QMJsonValue Class Reference

#include <qmjsonvalue.h>

Inheritance diagram for QMJsonValue:

Signals

void valueChanged (void)
 

Public Member Functions

 QMJsonValue ()
 
 QMJsonValue (bool value)
 
 QMJsonValue (double value)
 
 QMJsonValue (const QString &value)
 
 QMJsonValue (const QMPointer< QMJsonValue > &value)
 
 QMJsonValue (const QMPointer< QMJsonArray > &value)
 
 QMJsonValue (const QMPointer< QMJsonObject > &value)
 
 QMJsonValue (QMJsonValue *value)
 
 QMJsonValue (QMJsonArray *value)
 
 QMJsonValue (QMJsonObject *value)
 
template<class T >
 QMJsonValue (const T &value)
 
 QMJsonValue (const char *value)
 
 QMJsonValue (float value)
 
 QMJsonValue (char value)
 
 QMJsonValue (int8_t value)
 
 QMJsonValue (uint8_t value)
 
 QMJsonValue (int16_t value)
 
 QMJsonValue (uint16_t value)
 
 QMJsonValue (int32_t value)
 
 QMJsonValue (uint32_t value)
 
 QMJsonValue (int64_t value)
 
 QMJsonValue (uint64_t value)
 
virtual ~QMJsonValue ()
 
virtual bool isNull (void) const
 
virtual bool isBool (void) const
 
virtual bool isDouble (void) const
 
virtual bool isString (void) const
 
virtual bool isArray (void) const
 
virtual bool isObject (void) const
 
template<class T >
bool is (void) const
 
virtual bool toBool (void) const
 
virtual double toDouble (void) const
 
virtual QString toString (void) const
 
virtual const QMPointer< QMJsonArray > & toArray (void) const
 
virtual const QMPointer< QMJsonObject > & toObject (void) const
 
virtual bool toBool (bool defaultValue) const
 
virtual double toDouble (double defaultValue) const
 
virtual const QString & toString (const QString &defaultValue) const
 
virtual const QMPointer< QMJsonArray > & toArray (const QMPointer< QMJsonArray > &defaultValue) const
 
virtual const QMPointer< QMJsonObject > & toObject (const QMPointer< QMJsonObject > &defaultValue) const
 
template<class T >
const T & to (const T &defaultValue) const
 
virtual bool fromBool (bool value)
 
virtual bool fromDouble (double value)
 
virtual bool fromString (const QString &value)
 
virtual bool fromArray (const QMPointer< QMJsonArray > &value)
 
virtual bool fromObject (const QMPointer< QMJsonObject > &value)
 
virtual bool from (const QMPointer< QMJsonValue > &value)
 
template<class T >
bool from (const T &value)
 
virtual const QMPointer< QMJsonValue > & value (int32_t index) const
 
virtual const QMPointer< QMJsonValue > & value (const QString &key) const
 
virtual QVariant toVariant (void)
 
virtual QMJsonValueType type (void) const
 
virtual const QString & typeString (void) const
 
virtual QString toJson (QMJsonFormat format=QMJsonFormat_Pretty, QMJsonSort sort=QMJsonSort_None) const
 
virtual bool toJsonFile (const QString &filename, QMJsonFormat format=QMJsonFormat_Pretty, QMJsonSort sort=QMJsonSort_None) const
 

Static Public Member Functions

static QMPointer< QMJsonValuefromVariant (const QVariant &value)
 
static QMPointer< QMJsonValuefromJson (const QString &json)
 
static QMPointer< QMJsonValuefromJsonFile (const QString &filename)
 
static void registerFromComplexJson (const QString &qmjsontype, FromComplexJsonFunc func)
 

Friends

template<class T >
class QMJsonType
 
QDebug QM_JSON_EXPORT operator<< (QDebug dbg, const QMJsonValue &value)
 

Detailed Description

Definition at line 48 of file qmjsonvalue.h.

Constructor & Destructor Documentation

QMJsonValue::QMJsonValue ( )

Creates a JSON "null" value.

Definition at line 49 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( bool  value)
explicit

Creates a JSON boolean value.

Definition at line 54 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( double  value)
explicit

Creates a JSON number value.

Note
QtMark JSON stores all numbers internally as a double.

Definition at line 62 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( const QString &  value)
explicit

Creates a JSON string value.

Note
QtMark JSON stores all strings internally as QStrings.

Definition at line 70 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( const QMPointer< QMJsonValue > &  value)
explicit

Creates a new JSON value from an existing JSON value.

Both JSON values share the same internal pointer, and thus modifying one of the values, modifies them all.

Note
If the user provides a NULL QMJsonValue, a default, "null" JSON value will be created in it's place.

Definition at line 78 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( const QMPointer< QMJsonArray > &  value)
explicit

Creates a JSON array, wrapped in a JSON value.

Note
If the user provides a NULL QMJsonArray, a default, JSON array will be created in it's place.

Definition at line 92 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( const QMPointer< QMJsonObject > &  value)
explicit

Creates a JSON object, wrapped in a JSON value.

Note
If the user provides a NULL QMJsonObject, a default, JSON object will be created in it's place.

Definition at line 109 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( QMJsonValue value)
explicit

Creates a new JSON value from an existing JSON value.

Both JSON values share the same internal pointer, and thus modifying one of the values, modifies them all.

Note
If the user provides a NULL QMJsonValue, a default, "null" JSON value will be created in it's place.

Definition at line 126 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( QMJsonArray value)
explicit

Creates a JSON array, wrapped in a JSON value.

Note
If the user provides a NULL QMJsonArray, a default, JSON array will be created in it's place.
This function wraps the QMJsonArray in a QMPointer, since the QtMark JSON Library only supported managed pointers. For this reason, user's of this API should be aware that the pointer will become managed once used. The most common use for this function is when creating new JSON arrays as shown below
auto value = QMPointer<QMJsonValue>(new QMJsonArray);

Definition at line 140 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( QMJsonObject value)
explicit

Creates a JSON object, wrapped in a JSON value.

Note
If the user provides a NULL QMJsonObject, a default, JSON object will be created in it's place.
This function wraps the QMJsonObject in a QMPointer, since the QtMark JSON Library only supported managed pointers. For this reason, user's of this API should be aware that the pointer will become managed once used. The most common use for this function is when creating new JSON objects as shown below
auto value = QMPointer<QMJsonValue>(new QMJsonObject);

Definition at line 158 of file qmjsonvalue.cpp.

template<class T >
QMJsonValue::QMJsonValue ( const T &  value)
explicit

Creates a JSON value with type T.

The QtMark JSON Library supports wraping almost anything in a JSON value. If you use this function, the compiler will likely require that you provide a QDebug overload for this type (if one does not already exist).

If you wish to use toJson and fromJson with a non-standard type, you need to provide the Complex JSON template overloads. For an example of how to do this, see the qmjsontype_qsize.cpp file.

This library also supported nested complex types. To see an example of how this works, see the qmjsontype_qrect.cpp file, as the QRect implementation relys on QSize and QPoint.

auto value = QMPointer<QMJsonValue>(QSize(4, 8));

Definition at line 176 of file qmjsonvalue.h.

QMJsonValue::QMJsonValue ( const char *  value)
explicit

Creates a JSON string value.

Note
QtMark JSON stores all strings internally as QStrings.

Definition at line 176 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( float  value)
explicit

Creates a JSON number value.

Note
QtMark JSON stores all numbers internally as a double.

Definition at line 184 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( char  value)
explicit

Creates a JSON number value.

Note
QtMark JSON stores all numbers internally as a double.

Definition at line 192 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( int8_t  value)
explicit

Creates a JSON number value.

Note
QtMark JSON stores all numbers internally as a double.

Definition at line 200 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( uint8_t  value)
explicit

Creates a JSON number value.

Note
QtMark JSON stores all numbers internally as a double.

Definition at line 208 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( int16_t  value)
explicit

Creates a JSON number value.

Note
QtMark JSON stores all numbers internally as a double.

Definition at line 216 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( uint16_t  value)
explicit

Creates a JSON number value.

Note
QtMark JSON stores all numbers internally as a double.

Definition at line 224 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( int32_t  value)
explicit

Creates a JSON number value.

Note
QtMark JSON stores all numbers internally as a double.

Definition at line 232 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( uint32_t  value)
explicit

Creates a JSON number value.

Note
QtMark JSON stores all numbers internally as a double.

Definition at line 240 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( int64_t  value)
explicit

Creates a JSON number value.

Note
QtMark JSON stores all numbers internally as a double.

Definition at line 248 of file qmjsonvalue.cpp.

QMJsonValue::QMJsonValue ( uint64_t  value)
explicit

Creates a JSON number value.

Note
QtMark JSON stores all numbers internally as a double.

Definition at line 256 of file qmjsonvalue.cpp.

QMJsonValue::~QMJsonValue ( )
virtual

Definition at line 302 of file qmjsonvalue.cpp.

Member Function Documentation

bool QMJsonValue::isNull ( void  ) const
virtual

Returns true if the wrapped JSON value is a "null".

Note
Internally, "null" QMJsonValues have no internal type pointer.

Definition at line 306 of file qmjsonvalue.cpp.

bool QMJsonValue::isBool ( void  ) const
virtual

Returns true if the wrapped JSON value is a boolean

Definition at line 311 of file qmjsonvalue.cpp.

bool QMJsonValue::isDouble ( void  ) const
virtual

Returns true if the wrapped JSON value is a double.

Note
All numbers are stored as doubles, including integer types.

Definition at line 316 of file qmjsonvalue.cpp.

bool QMJsonValue::isString ( void  ) const
virtual

Returns true if the wrapped JSON value is a string.

Note
QtMark JSON stores all strings internally as QStrings.

Definition at line 321 of file qmjsonvalue.cpp.

bool QMJsonValue::isArray ( void  ) const
virtual

Returns true if the wrapped JSON value is a JSON array

Definition at line 326 of file qmjsonvalue.cpp.

bool QMJsonValue::isObject ( void  ) const
virtual

Returns true if the wrapped JSON value is a JSON object

Definition at line 331 of file qmjsonvalue.cpp.

template<class T >
bool QMJsonValue::is ( void  ) const

Returns true if the wrapped JSON value is of type T. This function is ment to be used with custom, complex types as show below:

auto value = QMPointer<QMJsonValue>(new QMJsonValue(QSize()));
qDebug() << value->is<QSize>(); // true

Definition at line 185 of file qmjsonvalue.h.

bool QMJsonValue::toBool ( void  ) const
virtual

Unwraps the JSON value and returns the interanl type. Depending on what type the JSON value is, will dictate what is returned:

  • null: false
  • bool: true / falue (native)
  • double: false if the value is 0, true otherwise
  • string: true if the value is "true", false otherwise
  • else: false

Definition at line 336 of file qmjsonvalue.cpp.

double QMJsonValue::toDouble ( void  ) const
virtual

Unwraps the JSON value and returns the interanl type. Depending on what type the JSON value is, will dictate what is returned:

  • null: 0
  • bool: false = 0, true = 1
  • double: double (native)
  • string: string converted to double, 0 if conversion fails.
  • else: 0

Definition at line 367 of file qmjsonvalue.cpp.

QString QMJsonValue::toString ( void  ) const
virtual

Unwraps the JSON value and returns the interanl type. Depending on what type the JSON value is, will dictate what is returned:

  • null: "null"
  • bool: "true" if true, "false" if false
  • double: double converted to string
  • string: string (native)
  • else: 0

Definition at line 395 of file qmjsonvalue.cpp.

const QMPointer< QMJsonArray > & QMJsonValue::toArray ( void  ) const
virtual

Unwraps the JSON value and returns a QMJsonArray. If the internal type is not a QMJsonArray, an empty QMJsonArray is returned.

Definition at line 423 of file qmjsonvalue.cpp.

const QMPointer< QMJsonObject > & QMJsonValue::toObject ( void  ) const
virtual

Unwraps the JSON value and returns a QMJsonObject. If the internal type is not a QMJsonObject, an empty QMJsonObject is returned.

Definition at line 434 of file qmjsonvalue.cpp.

bool QMJsonValue::toBool ( bool  defaultValue) const
virtual

Unwraps the JSON value and returns the interanl type. If the interal type is not a boolean, the default value is returned instead.

Definition at line 445 of file qmjsonvalue.cpp.

double QMJsonValue::toDouble ( double  defaultValue) const
virtual

Unwraps the JSON value and returns the interanl type. If the interal type is not a double, the default value is returned instead.

Definition at line 455 of file qmjsonvalue.cpp.

const QString & QMJsonValue::toString ( const QString &  defaultValue) const
virtual

Unwraps the JSON value and returns the interanl type. If the interal type is not a string, the default value is returned instead.

Definition at line 465 of file qmjsonvalue.cpp.

const QMPointer< QMJsonArray > & QMJsonValue::toArray ( const QMPointer< QMJsonArray > &  defaultValue) const
virtual

Unwraps the JSON value and returns the interanl type. If the interal type is not an array, the default value is returned instead.

Definition at line 475 of file qmjsonvalue.cpp.

const QMPointer< QMJsonObject > & QMJsonValue::toObject ( const QMPointer< QMJsonObject > &  defaultValue) const
virtual

Unwraps the JSON value and returns the interanl type. If the interal type is not an object, the default value is returned instead.

Definition at line 485 of file qmjsonvalue.cpp.

template<class T >
const T & QMJsonValue::to ( const T &  defaultValue) const

Unwraps the JSON value and returns the interanl type. If the interal type is not of type T, the default value is returned instead. This function is ment to be used with custom, complex types as show below:

auto value1 = QMPointer<QMJsonValue>(new QMJsonValue(false));
auto value2 = QMPointer<QMJsonValue>(new QMJsonValue(QSize(4, 8)));
qDebug() << value1->to<QSize>(15, 16); // QSize(15, 16)
qDebug() << value2->to<QSize>(15, 16); // QSize(4, 8)

Definition at line 191 of file qmjsonvalue.h.

bool QMJsonValue::fromBool ( bool  value)
virtual

Unwraps the JSON value and sets the interanl type. Depending on what type the JSON value is, will dictate what the internal type is set to:

  • null: (ignored, returns false)
  • bool: true / falue (native)
  • double: 0 if false, 1 if true
  • string: "false" if false, "true" if true
  • else: (ignored, returns false)

Definition at line 495 of file qmjsonvalue.cpp.

bool QMJsonValue::fromDouble ( double  value)
virtual

Unwraps the JSON value and sets the interanl type. Depending on what type the JSON value is, will dictate what the internal type is set to:

  • null: (ignored, returns false)
  • bool: false if 0, true otherwise
  • double: double (native)
  • string: double converted to string
  • else: (ignored, returns false)

Definition at line 526 of file qmjsonvalue.cpp.

bool QMJsonValue::fromString ( const QString &  value)
virtual

Unwraps the JSON value and sets the interanl type. Depending on what type the JSON value is, will dictate what the internal type is set to:

  • null: (ignored, returns false)
  • bool: true if "true", false otherwise
  • double: string converted to double. 0 if conversion fails.
  • string: string (native)
  • else: (ignored, returns false)

Definition at line 554 of file qmjsonvalue.cpp.

bool QMJsonValue::fromArray ( const QMPointer< QMJsonArray > &  value)
virtual

Unwraps the JSON value and sets the internal type to a QMJsonArray. If the internal type is not a QMJsonArray, the request is ignored

Definition at line 590 of file qmjsonvalue.cpp.

bool QMJsonValue::fromObject ( const QMPointer< QMJsonObject > &  value)
virtual

Unwraps the JSON value and sets the internal type to a QMJsonObject. If the internal type is not a QMJsonObject, the request is ignored

Definition at line 614 of file qmjsonvalue.cpp.

bool QMJsonValue::from ( const QMPointer< QMJsonValue > &  value)
virtual

Unwraps the JSON value and sets the interanl type. Depending on what type the JSON value is, will dictate what the internal type is set to:

Definition at line 638 of file qmjsonvalue.cpp.

template<class T >
bool QMJsonValue::from ( const T &  value)

Unwraps the JSON value and sets the interanl type. If the interal type is not of type T, the request is ignored. This function is ment to be used with custom, complex types as show below:

auto value1 = QMPointer<QMJsonValue>(new QMJsonValue(false));
auto value2 = QMPointer<QMJsonValue>(new QMJsonValue(QSize(4, 8)));
qDebug() << value1->from<QSize>(15, 16); // false
qDebug() << value2->from<QSize>(15, 16); // true

Definition at line 202 of file qmjsonvalue.h.

const QMPointer< QMJsonValue > & QMJsonValue::value ( int32_t  index) const
virtual

A convenience function. The following two statements are equvilant:

qDebug() << value->value(index); // QMJsonValue()
qDebug() << value->toArray()->value(index); // QMJsonValue()

Definition at line 673 of file qmjsonvalue.cpp.

const QMPointer< QMJsonValue > & QMJsonValue::value ( const QString &  key) const
virtual

A convenience function. The following two statements are equvilant:

qDebug() << value->value(key); // QMJsonValue()
qDebug() << value->toObject()->value(key); // QMJsonValue()

Definition at line 678 of file qmjsonvalue.cpp.

QVariant QMJsonValue::toVariant ( void  )
virtual

Converts the JSON value to a Variant. QMJsonArrays and QMJsonObjects are converted into QVariantLists and QVariantHashs, and wrapped in a QVariant.

Definition at line 683 of file qmjsonvalue.cpp.

QMPointer< QMJsonValue > QMJsonValue::fromVariant ( const QVariant &  value)
static

Converts a QVariant into a QMJsonValue. QVariants can take on a lot of different types. Types that are not supported by this library return QVariant's default constructor.

Definition at line 727 of file qmjsonvalue.cpp.

QMJsonValueType QMJsonValue::type ( void  ) const
virtual

Returns the internal type for this QMJsonValue.

Definition at line 831 of file qmjsonvalue.cpp.

const QString & QMJsonValue::typeString ( void  ) const
virtual

Returns a string representation of the internal type for this QMJsonValue.

Definition at line 836 of file qmjsonvalue.cpp.

QString QMJsonValue::toJson ( QMJsonFormat  format = QMJsonFormat_Pretty,
QMJsonSort  sort = QMJsonSort_None 
) const
virtual

Returns a string representation of this QMJsonValue. If QMJsonFormat_Optimized is provided, the returned string does not contain added whitespace or newlines. This function provides better performance when sort is set to QMJsonSort_None, however sorting is needed if you wish to compare the output of two JSON values.

Non-standard JSON types (i.e. complex types) are converted into their JSON equivalents using the toComplexJson and fromComplexJson template overloads. To see an exmaple of how this is done, see the template overloads withing the GUI portion of this library. Specificaly the overloads for QSize and QRect which demonstration coverting a complex type into native JSON (i.e. QSize) and converting a complex type into other complex types (i.e. QRect)

See also
registerFromComplexJson

Definition at line 855 of file qmjsonvalue.cpp.

bool QMJsonValue::toJsonFile ( const QString &  filename,
QMJsonFormat  format = QMJsonFormat_Pretty,
QMJsonSort  sort = QMJsonSort_None 
) const
virtual

Converts the QMJsonValue to a string and saves the results to filename. If the save fails, this function returns false, otherwise true.

See also
toJson

Definition at line 896 of file qmjsonvalue.cpp.

QMPointer< QMJsonValue > QMJsonValue::fromJson ( const QString &  json)
static

Converts the provided sting into the QMJsonValue equivlant.

If the provided string contains a JSON object that has a key named 'qmjsontype', QMJsonValue will attempt to convert the JSON object to it's complex type using the fromComplexJson function that was registered with registerFromComplexJson. If no fromComplexJson exists for the type defined by 'qmjsontype', or has not been registered, the JSON object will be treated as a standard JSON object.

See also
registerFromComplexJson

Definition at line 910 of file qmjsonvalue.cpp.

QMPointer< QMJsonValue > QMJsonValue::fromJsonFile ( const QString &  filename)
static

Read in the contents of filename, and converts the resulting sting into the QMJsonValue equivlant.

See also
fromJson

Definition at line 1078 of file qmjsonvalue.cpp.

void QMJsonValue::registerFromComplexJson ( const QString &  qmjsontype,
FromComplexJsonFunc  func 
)
static

Registers a fromComplexJson function with the QMJsonValue class. When fromJson or fromJsonFile are called, fromComplexJson that are registered will be used to convert JSON objects that contain 'qmjsontype' into their complex equivlants.

See also
main.cpp

Definition at line 964 of file qmjsonvalue.cpp.

void QMJsonValue::valueChanged ( void  )
signal

Friends And Related Function Documentation

template<class T >
friend class QMJsonType
friend

Definition at line 52 of file qmjsonvalue.h.

QDebug QM_JSON_EXPORT operator<< ( QDebug  dbg,
const QMJsonValue value 
)
friend

Definition at line 1096 of file qmjsonvalue.cpp.


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