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
qjsondocument.cpp
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#include <qjsondocument.h>
5#include <qjsonobject.h>
6#include <qjsonvalue.h>
7#include <qjsonarray.h>
8#include <qstringlist.h>
9#include <qvariant.h>
10#include <qmap.h>
11#include <qhash.h>
12#include <qdebug.h>
13#include <qcbormap.h>
14#include <qcborarray.h>
15#include "qcborvalue_p.h"
16#include "qjsonwriter_p.h"
17#include "qjsonparser_p.h"
18#include "qjson_p.h"
19#include "qdatastream.h"
20
22
54{
55 Q_DISABLE_COPY_MOVE(QJsonDocumentPrivate);
56public:
60 {
61 if (rawData)
62 free(rawData);
63 }
64
66 char *rawData = nullptr;
68
70 {
71 if (rawData) {
72 free(rawData);
73 rawData = nullptr;
74 rawDataSize = 0;
75 }
76 }
77};
78
86
91 : d(nullptr)
92{
93 setObject(object);
94}
95
104
109 : d(std::make_unique<QJsonDocumentPrivate>(data))
110{
112}
113
120
125{
126 if (other.d) {
127 if (!d)
128 d = std::make_unique<QJsonDocumentPrivate>();
129 d->value = other.d->value;
130 } else {
131 d.reset();
132 }
133}
134
136 : d(std::move(other.d))
137{
138}
139
141{
142 qSwap(d, other.d);
143}
144
150{
151 if (this != &other) {
152 if (other.d) {
153 if (!d)
154 d = std::make_unique<QJsonDocumentPrivate>();
155 else
156 d->clearRawData();
157 d->value = other.d->value;
158 } else {
159 d.reset();
160 }
161 }
162 return *this;
163}
164
186#ifndef QT_NO_VARIANT
196{
197 QJsonDocument doc;
198
199 switch (variant.metaType().id()) {
200 case QMetaType::QVariantMap:
202 break;
203 case QMetaType::QVariantHash:
205 break;
206 case QMetaType::QVariantList:
208 break;
209 case QMetaType::QStringList:
210 doc.d = std::make_unique<QJsonDocumentPrivate>();
212 break;
213 default:
214 break;
215 }
216 return doc;
217}
218
228{
229 if (!d)
230 return QVariant();
231
233 if (d->value.isArray())
234 return QJsonArray(container).toVariantList();
235 return QJsonObject(container).toVariantMap();
236}
237#endif // !QT_NO_VARIANT
238
259#if !defined(QT_JSON_READONLY) || defined(Q_QDOC)
261{
262 QByteArray json;
263 if (!d)
264 return json;
265
266 const QCborContainerPrivate *container = QJsonPrivate::Value::container(d->value);
267 if (d->value.isArray())
268 QJsonPrivate::Writer::arrayToJson(container, json, 0, (format == Compact));
269 else
270 QJsonPrivate::Writer::objectToJson(container, json, 0, (format == Compact));
271
272 return json;
273}
274#endif
275
287{
288 QJsonPrivate::Parser parser(json.constData(), json.size());
290 const QCborValue val = parser.parse(error);
291 if (val.isArray() || val.isMap()) {
292 result.d = std::make_unique<QJsonDocumentPrivate>();
293 result.d->value = val;
294 }
295 return result;
296}
297
302{
303 if (!d)
304 return true;
305
306 return false;
307}
308
315{
316 if (!d)
317 return false;
318
319 return d->value.isArray();
320}
321
328{
329 if (!d)
330 return false;
331
332 return d->value.isMap();
333}
334
344{
345 if (isObject()) {
346 if (auto container = QJsonPrivate::Value::container(d->value))
347 return QJsonObject(container);
348 }
349 return QJsonObject();
350}
351
361{
362 if (isArray()) {
363 if (auto container = QJsonPrivate::Value::container(d->value))
364 return QJsonArray(container);
365 }
366 return QJsonArray();
367}
368
375{
376 if (!d)
377 d = std::make_unique<QJsonDocumentPrivate>();
378 else
379 d->clearRawData();
380
381 d->value = QCborValue::fromJsonValue(object);
382}
383
390{
391 if (!d)
392 d = std::make_unique<QJsonDocumentPrivate>();
393 else
394 d->clearRawData();
395
397}
398
412{
413 return (*this)[QStringView(key)];
414}
415
421{
422 if (!isObject())
424
425 return QJsonPrivate::Value::fromTrustedCbor(d->value.toMap().value(key));
426}
427
433{
434 if (!isObject())
436
437 return QJsonPrivate::Value::fromTrustedCbor(d->value.toMap().value(key));
438}
439
453{
454 if (!isArray())
456
457 return QJsonPrivate::Value::fromTrustedCbor(d->value.toArray().at(i));
458}
459
465bool comparesEqual(const QJsonDocument &lhs, const QJsonDocument &rhs) noexcept
466{
467 if (lhs.d && rhs.d)
468 return lhs.d->value == rhs.d->value;
469 return !lhs.d == !rhs.d;
470}
471
488{
489 return (d == nullptr);
490}
491
492#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
494{
495 QDebugStateSaver saver(dbg);
496 if (!o.d) {
497 dbg << "QJsonDocument()";
498 return dbg;
499 }
500 QByteArray json;
501 const QCborContainerPrivate *container = QJsonPrivate::Value::container(o.d->value);
502 if (o.d->value.isArray())
503 QJsonPrivate::Writer::arrayToJson(container, json, 0, true);
504 else
505 QJsonPrivate::Writer::objectToJson(container, json, 0, true);
506 dbg.nospace() << "QJsonDocument("
507 << json.constData() // print as utf-8 string without extra quotation marks
508 << ')';
509 return dbg;
510}
511#endif
512
513#ifndef QT_NO_DATASTREAM
519
521{
523 stream >> buffer;
524 QJsonParseError parseError{};
525 doc = QJsonDocument::fromJson(buffer, &parseError);
526 if (parseError.error && !buffer.isEmpty())
527 stream.setStatus(QDataStream::ReadCorruptData);
528 return stream;
529}
530#endif
531
\inmodule QtCore
Definition qbytearray.h:57
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:494
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:124
static QCborArray fromStringList(const QStringList &list)
Returns a QCborArray containing all the strings found in the list list.
\inmodule QtCore\reentrant
Definition qcborvalue.h:47
static QCborValue fromJsonValue(const QJsonValue &v)
Converts the JSON value contained in v into its corresponding CBOR value and returns it.
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore\reentrant
Definition qjsonarray.h:18
static QJsonArray fromVariantList(const QVariantList &list)
Converts the variant list list to a QJsonArray.
QVariantList toVariantList() const
Converts this object to a QVariantList.
QJsonDocumentPrivate()=default
QJsonDocumentPrivate(QCborValue data)
\inmodule QtCore\reentrant
static QJsonDocument fromVariant(const QVariant &variant)
Creates a QJsonDocument from the QVariant variant.
~QJsonDocument()
Deletes the document.
QVariant toVariant() const
Returns a QVariant representing the Json document.
bool isArray() const
Returns true if the document contains an array.
void swap(QJsonDocument &other) noexcept
bool isNull() const
returns true if this document is null.
const QJsonValue operator[](const QString &key) const
Returns a QJsonValue representing the value for the key key.
QByteArray toJson(JsonFormat format=Indented) const
void setArray(const QJsonArray &array)
Sets array as the main object of this document.
void setObject(const QJsonObject &object)
Sets object as the main object of this document.
QJsonDocument()
Constructs an empty and invalid document.
friend class QJsonValue
QJsonArray array() const
Returns the QJsonArray contained in the document.
QJsonObject object() const
Returns the QJsonObject contained in the document.
bool isObject() const
Returns true if the document contains an object.
bool isEmpty() const
Returns true if the document doesn't contain any data.
QJsonDocument & operator=(const QJsonDocument &other)
Assigns the other document to this QJsonDocument.
static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error=nullptr)
Parses json as a UTF-8 encoded JSON document, and creates a QJsonDocument from it.
\inmodule QtCore\reentrant
Definition qjsonobject.h:20
QVariantMap toVariantMap() const
Converts this object to a QVariantMap.
static QJsonObject fromVariantMap(const QVariantMap &map)
Converts the variant map map to a QJsonObject.
static QJsonObject fromVariantHash(const QVariantHash &map)
Converts the variant hash hash to a QJsonObject.
QCborValue parse(QJsonParseError *error)
static QJsonValue fromTrustedCbor(const QCborValue &v)
Definition qjson_p.h:200
static QCborContainerPrivate * container(const QCborValue &v)
Definition qjson_p.h:175
static void objectToJson(const QCborContainerPrivate *o, QByteArray &json, int indent, bool compact=false)
static void arrayToJson(const QCborContainerPrivate *a, QByteArray &json, int indent, bool compact=false)
\inmodule QtCore\reentrant
Definition qjsonvalue.h:25
int id(int=0) const
Definition qmetatype.h:475
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qvariant.h:65
QList< QVariant > toList() const
Returns the variant as a QVariantList if the variant has userType() \l QMetaType::QVariantList.
QMap< QString, QVariant > toMap() const
Returns the variant as a QVariantMap if the variant has type() \l QMetaType::QVariantMap.
QHash< QString, QVariant > toHash() const
Returns the variant as a QHash<QString, QVariant> if the variant has type() \l QMetaType::QVariantHas...
QMetaType metaType() const
QStringList toStringList() const
Returns the variant as a QStringList if the variant has userType() \l QMetaType::QStringList,...
Combined button and popup list for selecting options.
DBusConnection const char DBusError * error
EGLStreamKHR stream
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
QDataStream & operator>>(QDataStream &stream, QJsonDocument &doc)
QDebug operator<<(QDebug dbg, const QJsonDocument &o)
bool comparesEqual(const QJsonDocument &lhs, const QJsonDocument &rhs) noexcept
GLuint64 key
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum GLuint buffer
GLint GLsizei GLsizei GLenum format
GLuint GLfloat * val
GLenum array
GLuint64EXT * result
[6]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QT_BEGIN_NAMESPACE constexpr void qSwap(T &value1, T &value2) noexcept(std::is_nothrow_swappable_v< T >)
Definition qswap.h:20
ptrdiff_t qsizetype
Definition qtypes.h:165
unsigned int uint
Definition qtypes.h:34
QObject::connect nullptr
QVariant variant
[1]
QSharedPointer< T > other(t)
[5]
\inmodule QtCore\reentrant