Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qjsonvalue.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QJSONVALUE_H
5#define QJSONVALUE_H
6
7#include <QtCore/qcborvalue.h>
8#include <QtCore/qcompare.h>
9#include <QtCore/qglobal.h>
10#include <QtCore/qstring.h>
11#include <QtCore/qshareddata.h>
12
14
15class QVariant;
16class QJsonArray;
17class QJsonObject;
19
20namespace QJsonPrivate {
21class Value;
22}
23
24class Q_CORE_EXPORT QJsonValue
25{
26public:
27 enum Type {
28 Null = 0x0,
29 Bool = 0x1,
30 Double = 0x2,
31 String = 0x3,
32 Array = 0x4,
33 Object = 0x5,
34 Undefined = 0x80
35 };
36
38 QJsonValue(bool b);
39 QJsonValue(double n);
40 QJsonValue(int n);
42 QJsonValue(const QString &s);
44#ifndef QT_NO_CAST_FROM_ASCII
45 QT_ASCII_CAST_WARN inline QJsonValue(const char *s)
46 : QJsonValue(QString::fromUtf8(s)) {}
47#endif
48 QJsonValue(const QJsonArray &a);
49 QJsonValue(QJsonArray &&a) noexcept;
50 QJsonValue(const QJsonObject &o);
51 QJsonValue(QJsonObject &&o) noexcept;
52
54
55 QJsonValue(const QJsonValue &other) noexcept;
56 QJsonValue &operator =(const QJsonValue &other) noexcept;
57
58 QJsonValue(QJsonValue &&other) noexcept;
59
60 QJsonValue &operator =(QJsonValue &&other) noexcept
61 {
62 swap(other);
63 return *this;
64 }
65
66 void swap(QJsonValue &other) noexcept;
67
68 static QJsonValue fromVariant(const QVariant &variant);
69 QVariant toVariant() const;
70
71 Type type() const;
72 inline bool isNull() const { return type() == Null; }
73 inline bool isBool() const { return type() == Bool; }
74 inline bool isDouble() const { return type() == Double; }
75 inline bool isString() const { return type() == String; }
76 inline bool isArray() const { return type() == Array; }
77 inline bool isObject() const { return type() == Object; }
78 inline bool isUndefined() const { return type() == Undefined; }
79
80 bool toBool(bool defaultValue = false) const;
81 int toInt(int defaultValue = 0) const;
82 qint64 toInteger(qint64 defaultValue = 0) const;
83 double toDouble(double defaultValue = 0) const;
84 QString toString() const;
85 QString toString(const QString &defaultValue) const;
86 QJsonArray toArray() const;
87 QJsonArray toArray(const QJsonArray &defaultValue) const;
88 QJsonObject toObject() const;
89 QJsonObject toObject(const QJsonObject &defaultValue) const;
90
91 const QJsonValue operator[](const QString &key) const;
92 const QJsonValue operator[](QStringView key) const;
93 const QJsonValue operator[](QLatin1StringView key) const;
94 const QJsonValue operator[](qsizetype i) const;
95
96#if QT_CORE_REMOVED_SINCE(6, 8)
97 bool operator==(const QJsonValue &other) const;
98 bool operator!=(const QJsonValue &other) const;
99#endif
100
101private:
102 friend Q_CORE_EXPORT bool comparesEqual(const QJsonValue &lhs,
103 const QJsonValue &rhs) noexcept;
105
106 // avoid implicit conversions from char * to bool
107 QJsonValue(const void *) = delete;
109 friend class QJsonArray;
110 friend class QJsonObject;
111 friend class QCborValue;
112 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &);
113 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &);
114
116
117 // Assert binary compatibility with pre-5.15 QJsonValue
118 static_assert(sizeof(QExplicitlySharedDataPointer<QCborContainerPrivate>) == sizeof(void *));
119 static_assert(sizeof(QCborValue::Type) == sizeof(QJsonValue::Type));
120};
121
122Q_DECLARE_SHARED(QJsonValue)
123
125{
126public:
129 inline operator QJsonValue() const { return concrete(*this); }
130
131 Q_CORE_EXPORT QVariant toVariant() const;
132 QJsonValue::Type type() const { return concreteType(*this); }
133 bool isNull() const { return type() == QJsonValue::Null; }
134 bool isBool() const { return type() == QJsonValue::Bool; }
135 bool isDouble() const { return type() == QJsonValue::Double; }
136 bool isString() const { return type() == QJsonValue::String; }
137 bool isArray() const { return type() == QJsonValue::Array; }
138 bool isObject() const { return type() == QJsonValue::Object; }
139 bool isUndefined() const { return type() == QJsonValue::Undefined; }
140
141 bool toBool(bool defaultValue = false) const
142 { return concreteBool(*this, defaultValue); }
143 int toInt(int defaultValue = 0) const
144 { return int(concreteInt(*this, defaultValue, true)); }
145 qint64 toInteger(qint64 defaultValue = 0) const
146 { return concreteInt(*this, defaultValue, false); }
147 double toDouble(double defaultValue = 0) const
148 { return concreteDouble(*this, defaultValue); }
149 QString toString(const QString &defaultValue = {}) const
150 { return concreteString(*this, defaultValue); }
151 Q_CORE_EXPORT QJsonArray toArray() const;
152 Q_CORE_EXPORT QJsonObject toObject() const;
153
154 const QJsonValue operator[](QStringView key) const { return concrete(*this)[key]; }
155 const QJsonValue operator[](QLatin1StringView key) const { return concrete(*this)[key]; }
156 const QJsonValue operator[](qsizetype i) const { return concrete(*this)[i]; }
157
158protected:
159 friend bool comparesEqual(const QJsonValueConstRef &lhs,
160 const QJsonValueConstRef &rhs) noexcept
161 {
162 return comparesEqual(concrete(lhs), concrete(rhs));
163 }
164 friend bool comparesEqual(const QJsonValueConstRef &lhs,
165 const QJsonValue &rhs) noexcept
166 {
167 return comparesEqual(concrete(lhs), rhs);
168 }
171
172 Q_CORE_EXPORT static QJsonValue::Type
173 concreteType(QJsonValueConstRef self) noexcept Q_DECL_PURE_FUNCTION;
174 Q_CORE_EXPORT static bool
175 concreteBool(QJsonValueConstRef self, bool defaultValue) noexcept Q_DECL_PURE_FUNCTION;
176 Q_CORE_EXPORT static qint64
177 concreteInt(QJsonValueConstRef self, qint64 defaultValue, bool clamp) noexcept Q_DECL_PURE_FUNCTION;
178 Q_CORE_EXPORT static double
179 concreteDouble(QJsonValueConstRef self, double defaultValue) noexcept Q_DECL_PURE_FUNCTION;
180 Q_CORE_EXPORT static QString concreteString(QJsonValueConstRef self, const QString &defaultValue);
181 Q_CORE_EXPORT static QJsonValue concrete(QJsonValueConstRef self) noexcept;
182
183 // for iterators
184 Q_CORE_EXPORT static QString objectKey(QJsonValueConstRef self);
185 QString objectKey() const { return objectKey(*this); }
186
187#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
189 : a(array), is_object(false), index(static_cast<quint64>(idx)) {}
191 : o(object), is_object(true), index(static_cast<quint64>(idx)) {}
192
193 void rebind(QJsonValueConstRef other)
194 {
195 Q_ASSERT(is_object == other.is_object);
196 if (is_object)
197 o = other.o;
198 else
199 a = other.a;
200 index = other.index;
201 }
202
203 union {
204 QJsonArray *a;
205 QJsonObject *o;
206 void *d;
207 };
208 quint64 is_object : 1;
209 quint64 index : 63;
210#else
211 constexpr QJsonValueConstRef(QCborContainerPrivate *d, size_t index, bool is_object)
212 : d(d), is_object(is_object), index(index)
213 {}
214
215 // implemented in qjsonarray.h & qjsonobject.h, to get their d
218
220 {
221 d = other.d;
222 index = other.index;
223 }
224
226 size_t is_object : 1;
227 size_t index : std::numeric_limits<size_t>::digits - 1;
228#endif
229
230 friend class QJsonArray;
231 friend class QJsonObject;
233};
234
236QT6_ONLY(QT_WARNING_DISABLE_MSVC(4275)) // non dll-interface class 'QJsonValueConstRef' used as base for dll-interface class 'QJsonValueRef'
237class QT6_ONLY(Q_CORE_EXPORT) QJsonValueRef : public QJsonValueConstRef
238{
239public:
240 QJsonValueRef(const QJsonValueRef &) = default;
241 QT7_ONLY(Q_CORE_EXPORT) QJsonValueRef &operator = (const QJsonValue &val);
242 QT7_ONLY(Q_CORE_EXPORT) QJsonValueRef &operator = (const QJsonValueRef &val);
243
244#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
245 // retained for binary compatibility (due to the Q_CORE_EXPORT) because at
246 // least one compiler emits and exports all inlines in an exported class
247
249 : QJsonValueConstRef(array, idx) {}
251 : QJsonValueConstRef(object, idx) {}
252
253 operator QJsonValue() const { return toValue(); }
254
255 QVariant toVariant() const;
256 inline QJsonValue::Type type() const { return QJsonValueConstRef::type(); }
257 inline bool isNull() const { return type() == QJsonValue::Null; }
258 inline bool isBool() const { return type() == QJsonValue::Bool; }
259 inline bool isDouble() const { return type() == QJsonValue::Double; }
260 inline bool isString() const { return type() == QJsonValue::String; }
261 inline bool isArray() const { return type() == QJsonValue::Array; }
262 inline bool isObject() const { return type() == QJsonValue::Object; }
263 inline bool isUndefined() const { return type() == QJsonValue::Undefined; }
264
265 inline bool toBool(bool defaultValue = false) const { return QJsonValueConstRef::toBool(defaultValue); }
266 inline int toInt(int defaultValue = 0) const { return QJsonValueConstRef::toInt(defaultValue); }
267 inline qint64 toInteger(qint64 defaultValue = 0) const { return QJsonValueConstRef::toInteger(defaultValue); }
268 inline double toDouble(double defaultValue = 0) const { return QJsonValueConstRef::toDouble(defaultValue); }
269 inline QString toString(const QString &defaultValue = {}) const { return QJsonValueConstRef::toString(defaultValue); }
270 QJsonArray toArray() const;
271 QJsonObject toObject() const;
272
273 const QJsonValue operator[](QStringView key) const { return QJsonValueConstRef::operator[](key); }
274 const QJsonValue operator[](QLatin1StringView key) const { return QJsonValueConstRef::operator[](key); }
275 const QJsonValue operator[](qsizetype i) const { return QJsonValueConstRef::operator[](i); }
276
277#if QT_CORE_REMOVED_SINCE(6, 8)
278 inline bool operator==(const QJsonValue &other) const { return comparesEqual(*this, other); }
279 inline bool operator!=(const QJsonValue &other) const { return !comparesEqual(*this, other); }
280#endif
281
282private:
283 friend bool comparesEqual(const QJsonValueRef &lhs,
284 const QJsonValueRef &rhs) noexcept
285 {
286 return comparesEqual(QJsonValue(lhs), QJsonValue(rhs));
287 }
288 friend bool comparesEqual(const QJsonValueRef &lhs,
289 const QJsonValueConstRef &rhs) noexcept
290 {
291 return comparesEqual(QJsonValue(lhs), QJsonValue(rhs));
292 }
295
296 QJsonValue toValue() const;
297#else
298 using QJsonValueConstRef::operator[];
299 Q_CORE_EXPORT QJsonValueRef operator[](QAnyStringView key);
300 Q_CORE_EXPORT QJsonValueRef operator[](qsizetype i);
301
302private:
304#endif // < Qt 7
305
306 QT7_ONLY(Q_CORE_EXPORT) void detach();
307 friend class QJsonArray;
308 friend class QJsonObject;
309};
311
313{
314 return concrete().toJsonValue();
315}
316
317Q_CORE_EXPORT size_t qHash(const QJsonValue &value, size_t seed = 0);
318
319#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
320Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &);
321#endif
322
323#ifndef QT_NO_DATASTREAM
324Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &);
325Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonValue &);
326#endif
327
329
330#endif // QJSONVALUE_H
Definition main.cpp:8
\inmodule QtCore
QCborValue concrete() const noexcept
Definition qcborvalue.h:394
QJsonValue toJsonValue() const
\inmodule QtCore\reentrant
Definition qcborvalue.h:47
QCborArray toArray() const
QJsonValue toJsonValue() const
Converts this QCborValue object to an equivalent representation in JSON and returns it as a QJsonValu...
Type
This enum represents the QCborValue type.
Definition qcborvalue.h:70
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore\reentrant
Definition qjsonarray.h:18
\inmodule QtCore\reentrant
Definition qjsonobject.h:20
constexpr QJsonValueConstRef(QCborContainerPrivate *d, size_t index, bool is_object)
Definition qjsonvalue.h:211
const QJsonValue operator[](QLatin1StringView key) const
Definition qjsonvalue.h:155
bool isBool() const
Definition qjsonvalue.h:134
bool isArray() const
Definition qjsonvalue.h:137
bool isDouble() const
Definition qjsonvalue.h:135
void rebind(QJsonValueConstRef other)
Definition qjsonvalue.h:219
friend bool comparesEqual(const QJsonValueConstRef &lhs, const QJsonValue &rhs) noexcept
Definition qjsonvalue.h:164
bool isString() const
Definition qjsonvalue.h:136
bool isObject() const
Definition qjsonvalue.h:138
QString objectKey() const
Definition qjsonvalue.h:185
QJsonValueConstRef(const QJsonValueConstRef &)=default
qint64 toInteger(qint64 defaultValue=0) const
Definition qjsonvalue.h:145
bool toBool(bool defaultValue=false) const
Definition qjsonvalue.h:141
friend bool comparesEqual(const QJsonValueConstRef &lhs, const QJsonValueConstRef &rhs) noexcept
Definition qjsonvalue.h:159
QJsonValueConstRef & operator=(const QJsonValueConstRef &)=delete
QString toString(const QString &defaultValue={}) const
Definition qjsonvalue.h:149
QJsonValue::Type type() const
Definition qjsonvalue.h:132
const QJsonValue operator[](qsizetype i) const
Definition qjsonvalue.h:156
bool isNull() const
Definition qjsonvalue.h:133
double toDouble(double defaultValue=0) const
Definition qjsonvalue.h:147
int toInt(int defaultValue=0) const
Definition qjsonvalue.h:143
const QJsonValue operator[](QStringView key) const
Definition qjsonvalue.h:154
bool isUndefined() const
Definition qjsonvalue.h:139
\inmodule QtCore \reentrant
\inmodule QtCore\reentrant
Definition qjsonvalue.h:25
QT_ASCII_CAST_WARN QJsonValue(const char *s)
Creates a value of type String with value s, assuming UTF-8 encoding of the input.
Definition qjsonvalue.h:45
bool isBool() const
Returns true if the value contains a boolean.
Definition qjsonvalue.h:73
bool isString() const
Returns true if the value contains a string.
Definition qjsonvalue.h:75
bool isDouble() const
Returns true if the value contains a double.
Definition qjsonvalue.h:74
bool isNull() const
Returns true if the value is null.
Definition qjsonvalue.h:72
~QJsonValue()
Destroys the value.
QJsonValue(const QJsonValue &other) noexcept
Creates a copy of other.
bool isArray() const
Returns true if the value contains an array.
Definition qjsonvalue.h:76
bool isObject() const
Returns true if the value contains an object.
Definition qjsonvalue.h:77
bool isUndefined() const
Returns true if the value is undefined.
Definition qjsonvalue.h:78
Type
This enum describes the type of the JSON value.
Definition qjsonvalue.h:27
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qvariant.h:65
Combined button and popup list for selecting options.
bool isNull(const T &t)
class QT6_ONLY(Q_CORE_EXPORT) QChar
Definition qchar.h:44
#define Q_DECLARE_EQUALITY_COMPARABLE(...)
#define Q_DECL_PURE_FUNCTION
#define QT_WARNING_POP
#define QT_WARNING_DISABLE_MSVC(number)
#define QT_WARNING_PUSH
constexpr bool operator!=(const timespec &t1, const timespec &t2)
bool comparesEqual(const QDir &lhs, const QDir &rhs)
Definition qdir.cpp:1819
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &)
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QJsonValue &)
Q_CORE_EXPORT size_t qHash(const QJsonValue &value, size_t seed=0)
GLboolean GLboolean GLboolean b
GLsizei const GLfloat * v
[13]
GLuint64 key
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint index
[2]
GLuint object
[3]
GLenum type
GLfloat GLfloat clamp
GLfloat n
GLdouble s
[6]
Definition qopenglext.h:235
GLuint GLfloat * val
GLenum array
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define QT_ASCII_CAST_WARN
unsigned long long quint64
Definition qtypes.h:61
ptrdiff_t qsizetype
Definition qtypes.h:165
long long qint64
Definition qtypes.h:60
static QVariant toVariant(const QV4::Value &value, QMetaType typeHint, JSToQVariantConversionBehavior conversionBehavior, V4ObjectSet *visitedObjects)
static int toInt(const QChar &qc, int R)
static double toDouble(Value v)
QDataStream & operator<<(QDataStream &out, const MyClass &myObj)
[4]
QVariant variant
[1]
QSharedPointer< T > other(t)
[5]
this swap(other)
char * toString(const MyType &t)
[31]
Definition moc.h:23
QT_BEGIN_NAMESPACE bool toBool(const QString &str)
Definition utils.h:14