1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QJSONVALUE_H
41#define QJSONVALUE_H
42
43#include <QtCore/qglobal.h>
44#include <QtCore/qstring.h>
45
46QT_BEGIN_NAMESPACE
47
48class QDebug;
49class QVariant;
50class QJsonArray;
51class QJsonObject;
52
53namespace QJsonPrivate {
54 class Data;
55 class Base;
56 class Object;
57 class Header;
58 class Array;
59 class Value;
60 class Entry;
61}
62
63class Q_CORE_EXPORT QJsonValue
64{
65public:
66 enum Type {
67 Null = 0x0,
68 Bool = 0x1,
69 Double = 0x2,
70 String = 0x3,
71 Array = 0x4,
72 Object = 0x5,
73 Undefined = 0x80
74 };
75
76 QJsonValue(Type = Null);
77 QJsonValue(bool b);
78 QJsonValue(double n);
79 QJsonValue(int n);
80 QJsonValue(qint64 n);
81 QJsonValue(const QString &s);
82 QJsonValue(QLatin1String s);
83#ifndef QT_NO_CAST_FROM_ASCII
84 inline QT_ASCII_CAST_WARN QJsonValue(const char *s)
85 : d(nullptr), t(String) { stringDataFromQStringHelper(QString::fromUtf8(s)); }
86#endif
87 QJsonValue(const QJsonArray &a);
88 QJsonValue(const QJsonObject &o);
89
90 ~QJsonValue();
91
92 QJsonValue(const QJsonValue &other);
93 QJsonValue &operator =(const QJsonValue &other);
94
95 QJsonValue(QJsonValue &&other) noexcept
96 : ui(other.ui),
97 d(other.d),
98 t(other.t)
99 {
100 other.ui = 0;
101 other.d = nullptr;
102 other.t = Null;
103 }
104
105 QJsonValue &operator =(QJsonValue &&other) noexcept
106 {
107 swap(other);
108 return *this;
109 }
110
111 void swap(QJsonValue &other) noexcept
112 {
113 qSwap(ui, other.ui);
114 qSwap(d, other.d);
115 qSwap(t, other.t);
116 }
117
118 static QJsonValue fromVariant(const QVariant &variant);
119 QVariant toVariant() const;
120
121 Type type() const;
122 inline bool isNull() const { return type() == Null; }
123 inline bool isBool() const { return type() == Bool; }
124 inline bool isDouble() const { return type() == Double; }
125 inline bool isString() const { return type() == String; }
126 inline bool isArray() const { return type() == Array; }
127 inline bool isObject() const { return type() == Object; }
128 inline bool isUndefined() const { return type() == Undefined; }
129
130 bool toBool(bool defaultValue = false) const;
131 int toInt(int defaultValue = 0) const;
132 double toDouble(double defaultValue = 0) const;
133 QString toString() const;
134 QString toString(const QString &defaultValue) const;
135 QJsonArray toArray() const;
136 QJsonArray toArray(const QJsonArray &defaultValue) const;
137 QJsonObject toObject() const;
138 QJsonObject toObject(const QJsonObject &defaultValue) const;
139
140#if QT_STRINGVIEW_LEVEL < 2
141 const QJsonValue operator[](const QString &key) const;
142#endif
143 const QJsonValue operator[](QStringView key) const;
144 const QJsonValue operator[](QLatin1String key) const;
145 const QJsonValue operator[](int i) const;
146
147 bool operator==(const QJsonValue &other) const;
148 bool operator!=(const QJsonValue &other) const;
149
150private:
151 // avoid implicit conversions from char * to bool
152 inline QJsonValue(const void *) {}
153 friend class QJsonPrivate::Value;
154 friend class QJsonArray;
155 friend class QJsonObject;
156 friend class QCborValue;
157 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &);
158 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &);
159
160 QJsonValue(QJsonPrivate::Data *d, QJsonPrivate::Base *b, const QJsonPrivate::Value& v);
161 void stringDataFromQStringHelper(const QString &string);
162
163 void detach();
164
165 union {
166 quint64 ui;
167 bool b;
168 double dbl;
169 QStringData *stringData;
170 QJsonPrivate::Base *base;
171 };
172 QJsonPrivate::Data *d; // needed for Objects and Arrays
173 Type t;
174};
175
176class Q_CORE_EXPORT QJsonValueRef
177{
178public:
179 QJsonValueRef(QJsonArray *array, int idx)
180 : a(array), is_object(false), index(idx) {}
181 QJsonValueRef(QJsonObject *object, int idx)
182 : o(object), is_object(true), index(idx) {}
183
184 inline operator QJsonValue() const { return toValue(); }
185 QJsonValueRef &operator = (const QJsonValue &val);
186 QJsonValueRef &operator = (const QJsonValueRef &val);
187
188 QVariant toVariant() const;
189 inline QJsonValue::Type type() const { return toValue().type(); }
190 inline bool isNull() const { return type() == QJsonValue::Null; }
191 inline bool isBool() const { return type() == QJsonValue::Bool; }
192 inline bool isDouble() const { return type() == QJsonValue::Double; }
193 inline bool isString() const { return type() == QJsonValue::String; }
194 inline bool isArray() const { return type() == QJsonValue::Array; }
195 inline bool isObject() const { return type() == QJsonValue::Object; }
196 inline bool isUndefined() const { return type() == QJsonValue::Undefined; }
197
198 inline bool toBool() const { return toValue().toBool(); }
199 inline int toInt() const { return toValue().toInt(); }
200 inline double toDouble() const { return toValue().toDouble(); }
201 inline QString toString() const { return toValue().toString(); }
202 QJsonArray toArray() const;
203 QJsonObject toObject() const;
204
205 // ### Qt 6: Add default values
206 inline bool toBool(bool defaultValue) const { return toValue().toBool(defaultValue); }
207 inline int toInt(int defaultValue) const { return toValue().toInt(defaultValue); }
208 inline double toDouble(double defaultValue) const { return toValue().toDouble(defaultValue); }
209 inline QString toString(const QString &defaultValue) const { return toValue().toString(defaultValue); }
210
211 inline bool operator==(const QJsonValue &other) const { return toValue() == other; }
212 inline bool operator!=(const QJsonValue &other) const { return toValue() != other; }
213
214private:
215 QJsonValue toValue() const;
216
217 union {
218 QJsonArray *a;
219 QJsonObject *o;
220 };
221 uint is_object : 1;
222 uint index : 31;
223};
224
225// ### Qt 6: Get rid of these fake pointer classes
226class QJsonValuePtr
227{
228 QJsonValue value;
229public:
230 explicit QJsonValuePtr(const QJsonValue& val)
231 : value(val) {}
232
233 QJsonValue& operator*() { return value; }
234 QJsonValue* operator->() { return &value; }
235};
236
237class QJsonValueRefPtr
238{
239 QJsonValueRef valueRef;
240public:
241 QJsonValueRefPtr(QJsonArray *array, int idx)
242 : valueRef(array, idx) {}
243 QJsonValueRefPtr(QJsonObject *object, int idx)
244 : valueRef(object, idx) {}
245
246 QJsonValueRef& operator*() { return valueRef; }
247 QJsonValueRef* operator->() { return &valueRef; }
248};
249
250Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QJsonValue)
251
252Q_CORE_EXPORT uint qHash(const QJsonValue &value, uint seed = 0);
253
254#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
255Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &);
256#endif
257
258#ifndef QT_NO_DATASTREAM
259Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &);
260Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonValue &);
261#endif
262
263QT_END_NAMESPACE
264
265#endif // QJSONVALUE_H
266