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 | #include <QtCore/qshareddata.h> |
46 | #include <QtCore/qcborvalue.h> |
47 | |
48 | QT_BEGIN_NAMESPACE |
49 | |
50 | class QVariant; |
51 | class QJsonArray; |
52 | class QJsonObject; |
53 | class QCborContainerPrivate; |
54 | |
55 | namespace QJsonPrivate { |
56 | class Value; |
57 | } |
58 | |
59 | class Q_CORE_EXPORT QJsonValue |
60 | { |
61 | public: |
62 | enum Type { |
63 | Null = 0x0, |
64 | Bool = 0x1, |
65 | Double = 0x2, |
66 | String = 0x3, |
67 | Array = 0x4, |
68 | Object = 0x5, |
69 | Undefined = 0x80 |
70 | }; |
71 | |
72 | QJsonValue(Type = Null); |
73 | QJsonValue(bool b); |
74 | QJsonValue(double n); |
75 | QJsonValue(int n); |
76 | QJsonValue(qint64 v); |
77 | QJsonValue(const QString &s); |
78 | QJsonValue(QLatin1String s); |
79 | #ifndef QT_NO_CAST_FROM_ASCII |
80 | QT_ASCII_CAST_WARN inline QJsonValue(const char *s) |
81 | : QJsonValue(QString::fromUtf8(s)) {} |
82 | #endif |
83 | QJsonValue(const QJsonArray &a); |
84 | QJsonValue(const QJsonObject &o); |
85 | |
86 | ~QJsonValue(); |
87 | |
88 | QJsonValue(const QJsonValue &other); |
89 | QJsonValue &operator =(const QJsonValue &other); |
90 | |
91 | QJsonValue(QJsonValue &&other) noexcept; |
92 | |
93 | QJsonValue &operator =(QJsonValue &&other) noexcept |
94 | { |
95 | swap(other); |
96 | return *this; |
97 | } |
98 | |
99 | void swap(QJsonValue &other) noexcept; |
100 | |
101 | static QJsonValue fromVariant(const QVariant &variant); |
102 | QVariant toVariant() const; |
103 | |
104 | Type type() const; |
105 | inline bool isNull() const { return type() == Null; } |
106 | inline bool isBool() const { return type() == Bool; } |
107 | inline bool isDouble() const { return type() == Double; } |
108 | inline bool isString() const { return type() == String; } |
109 | inline bool isArray() const { return type() == Array; } |
110 | inline bool isObject() const { return type() == Object; } |
111 | inline bool isUndefined() const { return type() == Undefined; } |
112 | |
113 | bool toBool(bool defaultValue = false) const; |
114 | int toInt(int defaultValue = 0) const; |
115 | qint64 toInteger(qint64 defaultValue = 0) const; |
116 | double toDouble(double defaultValue = 0) const; |
117 | QString toString() const; |
118 | QString toString(const QString &defaultValue) const; |
119 | QJsonArray toArray() const; |
120 | QJsonArray toArray(const QJsonArray &defaultValue) const; |
121 | QJsonObject toObject() const; |
122 | QJsonObject toObject(const QJsonObject &defaultValue) const; |
123 | |
124 | #if QT_STRINGVIEW_LEVEL < 2 |
125 | const QJsonValue operator[](const QString &key) const; |
126 | #endif |
127 | const QJsonValue operator[](QStringView key) const; |
128 | const QJsonValue operator[](QLatin1String key) const; |
129 | const QJsonValue operator[](qsizetype i) const; |
130 | |
131 | bool operator==(const QJsonValue &other) const; |
132 | bool operator!=(const QJsonValue &other) const; |
133 | |
134 | private: |
135 | // avoid implicit conversions from char * to bool |
136 | QJsonValue(const void *) = delete; |
137 | friend class QJsonPrivate::Value; |
138 | friend class QJsonArray; |
139 | friend class QJsonObject; |
140 | friend class QCborValue; |
141 | friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &); |
142 | friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &); |
143 | |
144 | QCborValue value; |
145 | |
146 | // Assert binary compatibility with pre-5.15 QJsonValue |
147 | static_assert(sizeof(QExplicitlySharedDataPointer<QCborContainerPrivate>) == sizeof(void *)); |
148 | static_assert(sizeof(QCborValue::Type) == sizeof(QJsonValue::Type)); |
149 | }; |
150 | |
151 | class Q_CORE_EXPORT QJsonValueRef |
152 | { |
153 | public: |
154 | QJsonValueRef(QJsonArray *array, qsizetype idx) |
155 | : a(array), is_object(false), index(static_cast<quint64>(idx)) {} |
156 | QJsonValueRef(QJsonObject *object, qsizetype idx) |
157 | : o(object), is_object(true), index(static_cast<quint64>(idx)) {} |
158 | |
159 | QJsonValueRef(const QJsonValueRef &) = default; |
160 | |
161 | inline operator QJsonValue() const { return toValue(); } |
162 | QJsonValueRef &operator = (const QJsonValue &val); |
163 | QJsonValueRef &operator = (const QJsonValueRef &val); |
164 | |
165 | QVariant toVariant() const; |
166 | inline QJsonValue::Type type() const { return toValue().type(); } |
167 | inline bool isNull() const { return type() == QJsonValue::Null; } |
168 | inline bool isBool() const { return type() == QJsonValue::Bool; } |
169 | inline bool isDouble() const { return type() == QJsonValue::Double; } |
170 | inline bool isString() const { return type() == QJsonValue::String; } |
171 | inline bool isArray() const { return type() == QJsonValue::Array; } |
172 | inline bool isObject() const { return type() == QJsonValue::Object; } |
173 | inline bool isUndefined() const { return type() == QJsonValue::Undefined; } |
174 | |
175 | inline bool toBool(bool defaultValue = false) const { return toValue().toBool(defaultValue); } |
176 | inline int toInt(int defaultValue = 0) const { return toValue().toInt(defaultValue); } |
177 | inline qint64 toInteger(qint64 defaultValue = 0) const { return toValue().toInteger(defaultValue); } |
178 | inline double toDouble(double defaultValue = 0) const { return toValue().toDouble(defaultValue); } |
179 | inline QString toString(const QString &defaultValue = {}) const { return toValue().toString(defaultValue); } |
180 | QJsonArray toArray() const; |
181 | QJsonObject toObject() const; |
182 | |
183 | inline bool operator==(const QJsonValue &other) const { return toValue() == other; } |
184 | inline bool operator!=(const QJsonValue &other) const { return toValue() != other; } |
185 | |
186 | private: |
187 | QJsonValue toValue() const; |
188 | |
189 | union { |
190 | QJsonArray *a; |
191 | QJsonObject *o; |
192 | }; |
193 | quint64 is_object : 1; |
194 | quint64 index : 63; |
195 | |
196 | friend class QJsonArray; |
197 | friend class QJsonObject; |
198 | }; |
199 | |
200 | Q_DECLARE_SHARED(QJsonValue) |
201 | |
202 | Q_CORE_EXPORT size_t qHash(const QJsonValue &value, size_t seed = 0); |
203 | |
204 | #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) |
205 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &); |
206 | #endif |
207 | |
208 | #ifndef QT_NO_DATASTREAM |
209 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &); |
210 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonValue &); |
211 | #endif |
212 | |
213 | QT_END_NAMESPACE |
214 | |
215 | #endif // QJSONVALUE_H |
216 | |