KanoopCommonQt 2.1.1
Kanoop foundational Qt utility library
Loading...
Searching...
No Matches
jsonhelper.h
1/**
2 * JsonHelper
3 *
4 * Some static helper methods for QJson where the C# tryGet() model is
5 * desired.
6 *
7 * Stephen Punak, May 16 2020
8 */
9#ifndef JSONHELPER_H
10#define JSONHELPER_H
11#include <QString>
12#include <QJsonArray>
13#include <QJsonObject>
14#include "kanoopcommon.h"
15
16/**
17 * @brief Static helper methods for reading and writing QJsonObject values safely.
18 *
19 * Provides tryGet-style methods that return false rather than throwing when a key
20 * is absent or the wrong type, plus convenience conversions between Qt and JSON types.
21 */
22class KANOOP_EXPORT JsonHelper
23{
24public:
25 /**
26 * @brief Try to read a string value from a JSON object.
27 * @param object Source JSON object
28 * @param key Key to look up
29 * @param value Output receiving the string
30 * @return true if key exists and holds a string
31 */
32 static bool tryGetString(const QJsonObject& object, const QString& key, QString& value);
33
34 /**
35 * @brief Try to read a nested JSON object from a JSON object.
36 * @param object Source JSON object
37 * @param key Key to look up
38 * @param value Output receiving the nested object
39 * @return true if key exists and holds an object
40 */
41 static bool tryGetObject(const QJsonObject& object, const QString& key, QJsonObject& value);
42
43 /**
44 * @brief Try to read a JSON array from a JSON object.
45 * @param object Source JSON object
46 * @param key Key to look up
47 * @param value Output receiving the array
48 * @return true if key exists and holds an array
49 */
50 static bool tryGetArray(const QJsonObject& object, const QString& key, QJsonArray& value);
51
52 /**
53 * @brief Try to read a 32-bit signed integer from a JSON object.
54 * @param object Source JSON object
55 * @param key Key to look up
56 * @param value Output receiving the integer
57 * @return true if key exists and holds a numeric value
58 */
59 static bool tryGetInt32(const QJsonObject& object, const QString& key, qint32& value);
60
61 /**
62 * @brief Try to read a 32-bit unsigned integer from a JSON object.
63 * @param object Source JSON object
64 * @param key Key to look up
65 * @param value Output receiving the unsigned integer
66 * @return true if key exists and holds a numeric value
67 */
68 static bool tryGetUInt32(const QJsonObject& object, const QString& key, quint32& value);
69
70 /**
71 * @brief Try to read a double from a JSON object.
72 * @param object Source JSON object
73 * @param key Key to look up
74 * @param value Output receiving the double
75 * @return true if key exists and holds a numeric value
76 */
77 static bool tryGetDouble(const QJsonObject& object, const QString& key, qreal& value);
78
79 /**
80 * @brief Try to read a boolean from a JSON object.
81 * @param object Source JSON object
82 * @param key Key to look up
83 * @param value Output receiving the boolean
84 * @return true if key exists and holds a boolean
85 */
86 static bool tryGetBool(const QJsonObject& object, const QString& key, bool& value);
87
88 /**
89 * @brief Format a compact JSON byte array as an indented human-readable string.
90 * @param json Compact JSON bytes
91 * @return Indented JSON string
92 */
93 static QString toIndented(const QByteArray& json);
94
95 /**
96 * @brief Convert a QStringList to a QJsonArray.
97 * @param value String list to convert
98 * @return JSON array containing the strings
99 */
100 static QJsonArray toJsonArray(const QStringList& value);
101
102 /**
103 * @brief Convert a QJsonArray of strings to a QStringList.
104 * @param value JSON array to convert
105 * @return String list extracted from the array
106 */
107 static QStringList toStringList(const QJsonArray& value);
108
109 /**
110 * @brief Convert a QJsonArray of strings to a QStringList (alias for toStringList).
111 * @param value JSON array to convert
112 * @return String list extracted from the array
113 */
114 static QStringList fromJsonArray(const QJsonArray& value) { return toStringList(value); }
115
116 /**
117 * @brief Append all elements from sourceArray into destArray.
118 * @param destArray Destination array to append to
119 * @param sourceArray Source array whose elements are appended
120 */
121 static void appendToArray(QJsonArray& destArray, const QJsonArray& sourceArray);
122
123 /**
124 * @brief Return a UUID as a JSON string, or JSON null if the UUID is null.
125 * @param uuid UUID to serialize
126 * @return JSON string (without braces) or null value
127 */
128 static QJsonValue uuidOrNull(const QUuid& uuid) { return uuid.isNull() ? QJsonValue() : uuid.toString(QUuid::WithoutBraces); }
129
130 /**
131 * @brief Return a string as a JSON string, or JSON null if the string is empty.
132 * @param value String to serialize
133 * @return JSON string or null value
134 */
135 static QJsonValue stringOrNull(const QString& value) { return value.isEmpty() ? QJsonValue() : value; }
136
137 /**
138 * @brief Return a QVariant integer as a JSON integer, or JSON null if the variant is null.
139 * @param value Variant holding an integer
140 * @return JSON integer or null value
141 */
142 static QJsonValue intOrNull(const QVariant& value) { return value.isNull() ? QJsonValue() : value.toInt(); }
143
144 /**
145 * @brief Return a QVariant double as a JSON double, or JSON null if the variant is null.
146 * @param value Variant holding a double
147 * @return JSON double or null value
148 */
149 static QJsonValue doubleOrNull(const QVariant& value) { return value.isNull() ? QJsonValue() : value.toDouble(); }
150
151 /**
152 * @brief Return a QVariant double formatted as a JSON string, or JSON null if the variant is null.
153 * @param value Variant holding a double
154 * @param precision Number of decimal places (default 6)
155 * @return JSON string representation or null value
156 */
157 static QJsonValue doubleStringOrNull(const QVariant& value, int precision = 6);
158
159 /**
160 * @brief Return a QStringList as a JSON array, or JSON null if the list is empty.
161 * @param value String list to serialize
162 * @return JSON array or null value
163 */
164 static QJsonValue arrayOrNull(const QStringList& value);
165
166 /**
167 * @brief Extract an integer QVariant from a JSON value, or an invalid QVariant if null.
168 * @param value JSON value to read
169 * @return QVariant holding the integer, or invalid QVariant
170 */
171 static QVariant intOrNull(const QJsonValue& value) { QVariant result; return value.isNull() ? result : value.toInt(); }
172
173 /**
174 * @brief Extract a double QVariant from a JSON value, or an invalid QVariant if null.
175 * @param value JSON value to read
176 * @return QVariant holding the double, or invalid QVariant
177 */
178 static QVariant doubleOrNull(const QJsonValue& value);
179
180 /**
181 * @brief Parse a double from a JSON string value, or return an invalid QVariant if null.
182 * @param value JSON string value to parse
183 * @param precision Precision hint (default 6)
184 * @return QVariant holding the double, or invalid QVariant
185 */
186 static QVariant doubleStringOrNull(const QJsonValue& value, int precision = 6);
187};
188
189#endif // JSONHELPER_H
JsonHelper.
Definition jsonhelper.h:23
static QJsonValue intOrNull(const QVariant &value)
Return a QVariant integer as a JSON integer, or JSON null if the variant is null.
Definition jsonhelper.h:142
static void appendToArray(QJsonArray &destArray, const QJsonArray &sourceArray)
Append all elements from sourceArray into destArray.
static QVariant intOrNull(const QJsonValue &value)
Extract an integer QVariant from a JSON value, or an invalid QVariant if null.
Definition jsonhelper.h:171
static QVariant doubleOrNull(const QJsonValue &value)
Extract a double QVariant from a JSON value, or an invalid QVariant if null.
static bool tryGetArray(const QJsonObject &object, const QString &key, QJsonArray &value)
Try to read a JSON array from a JSON object.
static bool tryGetString(const QJsonObject &object, const QString &key, QString &value)
Try to read a string value 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 QJsonValue stringOrNull(const QString &value)
Return a string as a JSON string, or JSON null if the string is empty.
Definition jsonhelper.h:135
static QJsonValue doubleOrNull(const QVariant &value)
Return a QVariant double as a JSON double, or JSON null if the variant is null.
Definition jsonhelper.h:149
static QStringList fromJsonArray(const QJsonArray &value)
Convert a QJsonArray of strings to a QStringList (alias for toStringList).
Definition jsonhelper.h:114
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 QVariant doubleStringOrNull(const QJsonValue &value, int precision=6)
Parse a double from a JSON string value, or return an invalid QVariant if null.
static QJsonArray toJsonArray(const QStringList &value)
Convert a QStringList to a QJsonArray.
static bool tryGetObject(const QJsonObject &object, const QString &key, QJsonObject &value)
Try to read a nested JSON object from a JSON object.
static QString toIndented(const QByteArray &json)
Format a compact JSON byte array as an indented human-readable string.
static bool tryGetBool(const QJsonObject &object, const QString &key, bool &value)
Try to read a boolean from a JSON object.
static QJsonValue arrayOrNull(const QStringList &value)
Return a QStringList as a JSON array, or JSON null if the list is empty.
static QJsonValue uuidOrNull(const QUuid &uuid)
Return a UUID as a JSON string, or JSON null if the UUID is null.
Definition jsonhelper.h:128
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 QStringList toStringList(const QJsonArray &value)
Convert a QJsonArray of strings to a QStringList.