QtBase  v6.3.1
qjsonvalue.h
Go to the documentation of this file.
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 
49 
50 class QVariant;
51 class QJsonArray;
52 class QJsonObject;
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);
77  QJsonValue(const QString &s);
79 #ifndef QT_NO_CAST_FROM_ASCII
80  QT_ASCII_CAST_WARN inline QJsonValue(const char *s)
82 #endif
83  QJsonValue(const QJsonArray &a);
84  QJsonValue(QJsonArray &&a) noexcept;
85  QJsonValue(const QJsonObject &o);
86  QJsonValue(QJsonObject &&o) noexcept;
87 
89 
90  QJsonValue(const QJsonValue &other);
91  QJsonValue &operator =(const QJsonValue &other);
92 
93  QJsonValue(QJsonValue &&other) noexcept;
94 
95  QJsonValue &operator =(QJsonValue &&other) noexcept
96  {
97  swap(other);
98  return *this;
99  }
100 
101  void swap(QJsonValue &other) noexcept;
102 
103  static QJsonValue fromVariant(const QVariant &variant);
104  QVariant toVariant() const;
105 
106  Type type() const;
107  inline bool isNull() const { return type() == Null; }
108  inline bool isBool() const { return type() == Bool; }
109  inline bool isDouble() const { return type() == Double; }
110  inline bool isString() const { return type() == String; }
111  inline bool isArray() const { return type() == Array; }
112  inline bool isObject() const { return type() == Object; }
113  inline bool isUndefined() const { return type() == Undefined; }
114 
115  bool toBool(bool defaultValue = false) const;
116  int toInt(int defaultValue = 0) const;
117  qint64 toInteger(qint64 defaultValue = 0) const;
118  double toDouble(double defaultValue = 0) const;
119  QString toString() const;
120  QString toString(const QString &defaultValue) const;
121  QJsonArray toArray() const;
122  QJsonArray toArray(const QJsonArray &defaultValue) const;
123  QJsonObject toObject() const;
124  QJsonObject toObject(const QJsonObject &defaultValue) const;
125 
126 #if QT_STRINGVIEW_LEVEL < 2
127  const QJsonValue operator[](const QString &key) const;
128 #endif
129  const QJsonValue operator[](QStringView key) const;
130  const QJsonValue operator[](QLatin1String key) const;
131  const QJsonValue operator[](qsizetype i) const;
132 
133  bool operator==(const QJsonValue &other) const;
134  bool operator!=(const QJsonValue &other) const;
135 
136 private:
137  // avoid implicit conversions from char * to bool
138  QJsonValue(const void *) = delete;
139  friend class QJsonPrivate::Value;
140  friend class QJsonArray;
141  friend class QJsonObject;
142  friend class QCborValue;
143  friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &);
144  friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &);
145 
147 
148  // Assert binary compatibility with pre-5.15 QJsonValue
149  static_assert(sizeof(QExplicitlySharedDataPointer<QCborContainerPrivate>) == sizeof(void *));
150  static_assert(sizeof(QCborValue::Type) == sizeof(QJsonValue::Type));
151 };
152 
153 class Q_CORE_EXPORT QJsonValueRef
154 {
155 public:
157  : a(array), is_object(false), index(static_cast<quint64>(idx)) {}
159  : o(object), is_object(true), index(static_cast<quint64>(idx)) {}
160 
161  QJsonValueRef(const QJsonValueRef &) = default;
162 
163  inline operator QJsonValue() const { return toValue(); }
164  QJsonValueRef &operator = (const QJsonValue &val);
165  QJsonValueRef &operator = (const QJsonValueRef &val);
166 
167  QVariant toVariant() const;
168  inline QJsonValue::Type type() const { return toValue().type(); }
169  inline bool isNull() const { return type() == QJsonValue::Null; }
170  inline bool isBool() const { return type() == QJsonValue::Bool; }
171  inline bool isDouble() const { return type() == QJsonValue::Double; }
172  inline bool isString() const { return type() == QJsonValue::String; }
173  inline bool isArray() const { return type() == QJsonValue::Array; }
174  inline bool isObject() const { return type() == QJsonValue::Object; }
175  inline bool isUndefined() const { return type() == QJsonValue::Undefined; }
176 
177  inline bool toBool(bool defaultValue = false) const { return toValue().toBool(defaultValue); }
178  inline int toInt(int defaultValue = 0) const { return toValue().toInt(defaultValue); }
179  inline qint64 toInteger(qint64 defaultValue = 0) const { return toValue().toInteger(defaultValue); }
180  inline double toDouble(double defaultValue = 0) const { return toValue().toDouble(defaultValue); }
181  inline QString toString(const QString &defaultValue = {}) const { return toValue().toString(defaultValue); }
182  QJsonArray toArray() const;
183  QJsonObject toObject() const;
184 
185  const QJsonValue operator[](QStringView key) const { return toValue()[key]; }
186  const QJsonValue operator[](QLatin1String key) const { return toValue()[key]; }
187  const QJsonValue operator[](qsizetype i) const { return toValue()[i]; }
188 
189  inline bool operator==(const QJsonValue &other) const { return toValue() == other; }
190  inline bool operator!=(const QJsonValue &other) const { return toValue() != other; }
191 
192 private:
193  QJsonValue toValue() const;
194 
195  union {
198  };
199  quint64 is_object : 1;
200  quint64 index : 63;
201 
202  friend class QJsonArray;
203  friend class QJsonObject;
204 };
205 
207 
209 { return QJsonValue(lhs) == QJsonValue(rhs); }
210 inline bool operator!=(const QJsonValueRef &lhs, const QJsonValueRef &rhs)
211 { return !(lhs == rhs); }
212 
213 Q_CORE_EXPORT size_t qHash(const QJsonValue &value, size_t seed = 0);
214 
215 #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
216 Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &);
217 #endif
218 
219 #ifndef QT_NO_DATASTREAM
220 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonValue &);
221 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonValue &);
222 #endif
223 
225 
226 #endif // QJSONVALUE_H
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
#define value
[5]
@ Double
FT_UInt idx
Definition: cffcmap.c:135
Definition: main.cpp:55
The QCborValue class encapsulates a value in CBOR.
Definition: qcborvalue.h:86
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:66
operator>>(QDataStream &ds, qfloat16 &f)
Definition: qfloat16.cpp:344
operator<<(QDataStream &ds, qfloat16 f)
Definition: qfloat16.cpp:327
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:65
template< typename Enum > bool operator!=(Enum lhs, QFlags< Enum > rhs)
template< typename Enum > size_t qHash(QFlags< Enum > flags, size_t seed=0) noexcept
The QJsonArray class encapsulates a JSON array.
Definition: qjsonarray.h:54
The QJsonObject class encapsulates a JSON object.
Definition: qjsonobject.h:56
friend class QJsonValue
Definition: qjsonobject.h:284
The QJsonValue class encapsulates a value in JSON.
Definition: qjsonvalue.h:60
QT_ASCII_CAST_WARN QJsonValue(const char *s)
Definition: qjsonvalue.h:80
bool isBool() const
Definition: qjsonvalue.h:108
bool isString() const
Definition: qjsonvalue.h:110
bool isDouble() const
Definition: qjsonvalue.h:109
bool isNull() const
Definition: qjsonvalue.h:107
bool isArray() const
Definition: qjsonvalue.h:111
bool isObject() const
Definition: qjsonvalue.h:112
bool isUndefined() const
Definition: qjsonvalue.h:113
The QJsonValueRef class is a helper class for QJsonValue.
Definition: qjsonvalue.h:154
bool toBool(bool defaultValue=false) const
Definition: qjsonvalue.h:177
bool isString() const
Definition: qjsonvalue.h:172
bool isDouble() const
Definition: qjsonvalue.h:171
int toInt(int defaultValue=0) const
Definition: qjsonvalue.h:178
QJsonValue::Type type() const
Definition: qjsonvalue.h:168
QJsonObject * o
Definition: qjsonvalue.h:197
const QJsonValue operator[](qsizetype i) const
Definition: qjsonvalue.h:187
qint64 toInteger(qint64 defaultValue=0) const
Definition: qjsonvalue.h:179
QJsonArray * a
Definition: qjsonvalue.h:196
bool isUndefined() const
Definition: qjsonvalue.h:175
QJsonValueRef(QJsonArray *array, qsizetype idx)
Definition: qjsonvalue.h:156
bool isArray() const
Definition: qjsonvalue.h:173
bool isObject() const
Definition: qjsonvalue.h:174
bool operator==(const QJsonValue &other) const
Definition: qjsonvalue.h:189
bool isNull() const
Definition: qjsonvalue.h:169
bool operator!=(const QJsonValue &other) const
Definition: qjsonvalue.h:190
QString toString(const QString &defaultValue={}) const
Definition: qjsonvalue.h:181
QJsonValueRef(const QJsonValueRef &)=default
QJsonValueRef(QJsonObject *object, qsizetype idx)
Definition: qjsonvalue.h:158
const QJsonValue operator[](QStringView key) const
Definition: qjsonvalue.h:185
bool isBool() const
Definition: qjsonvalue.h:170
double toDouble(double defaultValue=0) const
Definition: qjsonvalue.h:180
const QJsonValue operator[](QLatin1String key) const
Definition: qjsonvalue.h:186
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition: qstring.h:84
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QStringView class provides a unified view on UTF-16 strings with a read-only subset of the QStrin...
Definition: qstringview.h:122
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:95
#define true
Definition: ftrandom.c:51
char Bool
Definition: ftraster.c:315
auto it unsigned count const
Definition: hb-iter.hh:848
#define inline
Definition: md4c.c:45
qsizetype fromUtf8(uchar b, OutputPtr &dst, InputPtr &src, InputPtr end)
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION ParsedNumber< double > toDouble(QByteArrayView a) noexcept
EGLOutputLayerEXT EGLint EGLAttrib value
unsigned long long quint64
Definition: qglobal.h:299
ptrdiff_t qsizetype
Definition: qglobal.h:308
long long qint64
Definition: qglobal.h:298
bool operator==(const QJsonValueRef &lhs, const QJsonValueRef &rhs)
Definition: qjsonvalue.h:208
Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &)
Definition: qjsonvalue.cpp:982
bool operator!=(const QJsonValueRef &lhs, const QJsonValueRef &rhs)
Definition: qjsonvalue.h:210
GLenum type
Definition: qopengl.h:270
GLboolean GLboolean GLboolean b
GLsizei const GLfloat * v
[13]
GLuint64 key
GLboolean GLboolean GLboolean GLboolean a
[7]
GLuint index
[2]
GLuint object
[3]
GLfloat n
GLuint GLfloat * val
Definition: qopenglext.h:1513
GLenum array
Definition: qopenglext.h:7028
GLdouble s
[6]
Definition: qopenglext.h:235
#define Q_DECLARE_SHARED(TYPE)
Definition: qtypeinfo.h:197
QT_BEGIN_NAMESPACE bool toBool(const QString &str)
Definition: utils.h:39
QVariant variant
[1]
QSharedPointer< T > other(t)
[5]
this swap(other)
Definition: hb-null.hh:93
Definition: moc.h:48
#define rhs