KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
JsonHelper Class Reference

JsonHelper. More...

#include <jsonhelper.h>

Static Public Member Functions

static bool tryGetString (const QJsonObject &object, const QString &key, QString &value)
 Try to read a string value from a JSON object.
 
static bool tryGetObject (const QJsonObject &object, const QString &key, QJsonObject &value)
 Try to read a nested JSON object from a JSON object.
 
static bool tryGetArray (const QJsonObject &object, const QString &key, QJsonArray &value)
 Try to read a JSON array from a JSON object.
 
static bool tryGetInt32 (const QJsonObject &object, const QString &key, qint32 &value)
 Try to read a 32-bit signed integer from a JSON object.
 
static bool tryGetUInt32 (const QJsonObject &object, const QString &key, quint32 &value)
 Try to read a 32-bit unsigned integer from a JSON object.
 
static bool tryGetDouble (const QJsonObject &object, const QString &key, qreal &value)
 Try to read a double from a JSON object.
 
static bool tryGetBool (const QJsonObject &object, const QString &key, bool &value)
 Try to read a boolean from a JSON object.
 
static QString toIndented (const QByteArray &json)
 Format a compact JSON byte array as an indented human-readable string.
 
static QJsonArray toJsonArray (const QStringList &value)
 Convert a QStringList to a QJsonArray.
 
static QStringList toStringList (const QJsonArray &value)
 Convert a QJsonArray of strings to a QStringList.
 
static QStringList fromJsonArray (const QJsonArray &value)
 Convert a QJsonArray of strings to a QStringList (alias for toStringList).
 
static void appendToArray (QJsonArray &destArray, const QJsonArray &sourceArray)
 Append all elements from sourceArray into destArray.
 
static QJsonValue uuidOrNull (const QUuid &uuid)
 Return a UUID as a JSON string, or JSON null if the UUID is null.
 
static QJsonValue stringOrNull (const QString &value)
 Return a string as a JSON string, or JSON null if the string is empty.
 
static QJsonValue intOrNull (const QVariant &value)
 Return a QVariant integer as a JSON integer, or JSON null if the variant is null.
 
static QJsonValue doubleOrNull (const QVariant &value)
 Return a QVariant double as a JSON double, or JSON null if the variant is null.
 
static QJsonValue doubleStringOrNull (const QVariant &value, int precision=6)
 Return a QVariant double formatted as a JSON string, or JSON null if the variant is null.
 
static QJsonValue arrayOrNull (const QStringList &value)
 Return a QStringList as a JSON array, or JSON null if the list is empty.
 
static QVariant intOrNull (const QJsonValue &value)
 Extract an integer QVariant from a JSON value, or an invalid QVariant if null.
 
static QVariant doubleOrNull (const QJsonValue &value)
 Extract a double QVariant from a JSON value, or an invalid QVariant if null.
 
static QVariant doubleStringOrNull (const QJsonValue &value, int precision=6)
 Parse a double from a JSON string value, or return an invalid QVariant if null.
 

Detailed Description

JsonHelper.

Some static helper methods for QJson where the C# tryGet() model is desired.

Stephen Punak, May 16 2020

Static helper methods for reading and writing QJsonObject values safely.

Provides tryGet-style methods that return false rather than throwing when a key is absent or the wrong type, plus convenience conversions between Qt and JSON types.

Definition at line 22 of file jsonhelper.h.

Member Function Documentation

◆ appendToArray()

static void JsonHelper::appendToArray ( QJsonArray &  destArray,
const QJsonArray &  sourceArray 
)
static

Append all elements from sourceArray into destArray.

Parameters
destArrayDestination array to append to
sourceArraySource array whose elements are appended

◆ arrayOrNull()

static QJsonValue JsonHelper::arrayOrNull ( const QStringList &  value)
static

Return a QStringList as a JSON array, or JSON null if the list is empty.

Parameters
valueString list to serialize
Returns
JSON array or null value

◆ doubleOrNull() [1/2]

static QVariant JsonHelper::doubleOrNull ( const QJsonValue &  value)
static

Extract a double QVariant from a JSON value, or an invalid QVariant if null.

Parameters
valueJSON value to read
Returns
QVariant holding the double, or invalid QVariant

◆ doubleOrNull() [2/2]

static QJsonValue JsonHelper::doubleOrNull ( const QVariant &  value)
inlinestatic

Return a QVariant double as a JSON double, or JSON null if the variant is null.

Parameters
valueVariant holding a double
Returns
JSON double or null value

Definition at line 149 of file jsonhelper.h.

◆ doubleStringOrNull() [1/2]

static QVariant JsonHelper::doubleStringOrNull ( const QJsonValue &  value,
int  precision = 6 
)
static

Parse a double from a JSON string value, or return an invalid QVariant if null.

Parameters
valueJSON string value to parse
precisionPrecision hint (default 6)
Returns
QVariant holding the double, or invalid QVariant

◆ doubleStringOrNull() [2/2]

static QJsonValue JsonHelper::doubleStringOrNull ( const QVariant &  value,
int  precision = 6 
)
static

Return a QVariant double formatted as a JSON string, or JSON null if the variant is null.

Parameters
valueVariant holding a double
precisionNumber of decimal places (default 6)
Returns
JSON string representation or null value

◆ fromJsonArray()

static QStringList JsonHelper::fromJsonArray ( const QJsonArray &  value)
inlinestatic

Convert a QJsonArray of strings to a QStringList (alias for toStringList).

Parameters
valueJSON array to convert
Returns
String list extracted from the array

Definition at line 114 of file jsonhelper.h.

◆ intOrNull() [1/2]

static QVariant JsonHelper::intOrNull ( const QJsonValue &  value)
inlinestatic

Extract an integer QVariant from a JSON value, or an invalid QVariant if null.

Parameters
valueJSON value to read
Returns
QVariant holding the integer, or invalid QVariant

Definition at line 171 of file jsonhelper.h.

◆ intOrNull() [2/2]

static QJsonValue JsonHelper::intOrNull ( const QVariant &  value)
inlinestatic

Return a QVariant integer as a JSON integer, or JSON null if the variant is null.

Parameters
valueVariant holding an integer
Returns
JSON integer or null value

Definition at line 142 of file jsonhelper.h.

◆ stringOrNull()

static QJsonValue JsonHelper::stringOrNull ( const QString &  value)
inlinestatic

Return a string as a JSON string, or JSON null if the string is empty.

Parameters
valueString to serialize
Returns
JSON string or null value

Definition at line 135 of file jsonhelper.h.

◆ toIndented()

static QString JsonHelper::toIndented ( const QByteArray &  json)
static

Format a compact JSON byte array as an indented human-readable string.

Parameters
jsonCompact JSON bytes
Returns
Indented JSON string

◆ toJsonArray()

static QJsonArray JsonHelper::toJsonArray ( const QStringList &  value)
static

Convert a QStringList to a QJsonArray.

Parameters
valueString list to convert
Returns
JSON array containing the strings

◆ toStringList()

static QStringList JsonHelper::toStringList ( const QJsonArray &  value)
static

Convert a QJsonArray of strings to a QStringList.

Parameters
valueJSON array to convert
Returns
String list extracted from the array

◆ tryGetArray()

static bool JsonHelper::tryGetArray ( const QJsonObject &  object,
const QString &  key,
QJsonArray &  value 
)
static

Try to read a JSON array from a JSON object.

Parameters
objectSource JSON object
keyKey to look up
valueOutput receiving the array
Returns
true if key exists and holds an array

◆ tryGetBool()

static bool JsonHelper::tryGetBool ( const QJsonObject &  object,
const QString &  key,
bool &  value 
)
static

Try to read a boolean from a JSON object.

Parameters
objectSource JSON object
keyKey to look up
valueOutput receiving the boolean
Returns
true if key exists and holds a boolean

◆ tryGetDouble()

static bool JsonHelper::tryGetDouble ( const QJsonObject &  object,
const QString &  key,
qreal &  value 
)
static

Try to read a double from a JSON object.

Parameters
objectSource JSON object
keyKey to look up
valueOutput receiving the double
Returns
true if key exists and holds a numeric value

◆ tryGetInt32()

static bool JsonHelper::tryGetInt32 ( const QJsonObject &  object,
const QString &  key,
qint32 &  value 
)
static

Try to read a 32-bit signed integer from a JSON object.

Parameters
objectSource JSON object
keyKey to look up
valueOutput receiving the integer
Returns
true if key exists and holds a numeric value

◆ tryGetObject()

static bool JsonHelper::tryGetObject ( const QJsonObject &  object,
const QString &  key,
QJsonObject &  value 
)
static

Try to read a nested JSON object from a JSON object.

Parameters
objectSource JSON object
keyKey to look up
valueOutput receiving the nested object
Returns
true if key exists and holds an object

◆ tryGetString()

static bool JsonHelper::tryGetString ( const QJsonObject &  object,
const QString &  key,
QString &  value 
)
static

Try to read a string value from a JSON object.

Parameters
objectSource JSON object
keyKey to look up
valueOutput receiving the string
Returns
true if key exists and holds a string

◆ tryGetUInt32()

static bool JsonHelper::tryGetUInt32 ( const QJsonObject &  object,
const QString &  key,
quint32 &  value 
)
static

Try to read a 32-bit unsigned integer from a JSON object.

Parameters
objectSource JSON object
keyKey to look up
valueOutput receiving the unsigned integer
Returns
true if key exists and holds a numeric value

◆ uuidOrNull()

static QJsonValue JsonHelper::uuidOrNull ( const QUuid &  uuid)
inlinestatic

Return a UUID as a JSON string, or JSON null if the UUID is null.

Parameters
uuidUUID to serialize
Returns
JSON string (without braces) or null value

Definition at line 128 of file jsonhelper.h.


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