Qt 6.x
The Qt SDK
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
52{
53 Q_DISABLE_COPY_MOVE(QJsonDocumentPrivate);
54public:
58 {
59 if (rawData)
60 free(rawData);
61 }
62
64 char *rawData = nullptr;
66
68 {
69 if (rawData) {
70 free(rawData);
71 rawData = nullptr;
72 rawDataSize = 0;
73 }
74 }
75};
76
81 : d(nullptr)
82{
83}
84
89 : d(nullptr)
90{
91 setObject(object);
92}
93
98 : d(nullptr)
99{
101}
102
107 : d(std::make_unique<QJsonDocumentPrivate>(data))
108{
109 Q_ASSERT(d);
111
118
123{
124 if (other.d) {
125 if (!d)
126 d = std::make_unique<QJsonDocumentPrivate>();
127 d->value = other.d->value;
128 } else {
129 d.reset();
130 }
131}
132
134 : d(std::move(other.d))
135{
136}
137
139{
140 qSwap(d, other.d);
141}
142
148{
149 if (this != &other) {
150 if (other.d) {
151 if (!d)
152 d = std::make_unique<QJsonDocumentPrivate>();
153 else
154 d->clearRawData();
155 d->value = other.d->value;
156 } else {
157 d.reset();
158 }
159 }
160 return *this;
161}
162
193{
194 QJsonDocument doc;
195
196 switch (variant.metaType().id()) {
197 case QMetaType::QVariantMap:
199 break;
200 case QMetaType::QVariantHash:
202 break;
203 case QMetaType::QVariantList:
205 break;
206 case QMetaType::QStringList:
207 doc.d = std::make_unique<QJsonDocumentPrivate>();
209 break;
210 default:
211 break;
212 }
213 return doc;
214}
215
225{
226 if (!d)
227 return QVariant();
228
230 if (d->value.isArray())
231 return QJsonArray(container).toVariantList();
232 return QJsonObject(container).toVariantMap();
233}
234
255#if !defined(QT_JSON_READONLY) || defined(Q_QDOC)
257{
258 QByteArray json;
259 if (!d)
260 return json;
261
262 const QCborContainerPrivate *container = QJsonPrivate::Value::container(d->value);
263 if (d->value.isArray())
264 QJsonPrivate::Writer::arrayToJson(container, json, 0, (format == Compact));
265 else
266 QJsonPrivate::Writer::objectToJson(container, json, 0, (format == Compact));
267
268 return json;
269}
270#endif
271
283{
284 QJsonPrivate::Parser parser(json.constData(), json.size());
286 const QCborValue val = parser.parse(error);
287 if (val.isArray() || val.isMap()) {
288 result.d = std::make_unique<QJsonDocumentPrivate>();
289 result.d->value = val;
290 }
291 return result;
292}
293
298{
299 if (!d)
300 return true;
301
302 return false;
303}
304
311{
312 if (!d)
313 return false;
314
315 return d->value.isArray();
316}
317
324{
325 if (!d)
326 return false;
327
328 return d->value.isMap();
329}
330
340{
341 if (isObject()) {
342 if (auto container = QJsonPrivate::Value::container(d->value))
343 return QJsonObject(container);
344 }
345 return QJsonObject();
346}
347
357{
358 if (isArray()) {
359 if (auto container = QJsonPrivate::Value::container(d->value))
360 return QJsonArray(container);
361 }
362 return QJsonArray();
363}
364
371{
372 if (!d)
373 d = std::make_unique<QJsonDocumentPrivate>();
374 else
375 d->clearRawData();
376
377 d->value = QCborValue::fromJsonValue(object);
378}
379
386{
387 if (!d)
388 d = std::make_unique<QJsonDocumentPrivate>();
389 else
390 d->clearRawData();
391
393}
394
408{
409 return (*this)[QStringView(key)];
410}
411
417{
418 if (!isObject())
420
421 return QJsonPrivate::Value::fromTrustedCbor(d->value.toMap().value(key));
422}
423
429{
430 if (!isObject())
432
433 return QJsonPrivate::Value::fromTrustedCbor(d->value.toMap().value(key));
434}
435
449{
450 if (!isArray())
452
453 return QJsonPrivate::Value::fromTrustedCbor(d->value.toArray().at(i));
454}
455
460{
461 if (d && other.d)
462 return d->value == other.d->value;
463 return !d == !other.d;
464}
465
482{
483 return (d == nullptr);
484}
485
486#if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY)
488{
489 QDebugStateSaver saver(dbg);
490 if (!o.d) {
491 dbg << "QJsonDocument()";
492 return dbg;
493 }
494 QByteArray json;
495 const QCborContainerPrivate *container = QJsonPrivate::Value::container(o.d->value);
496 if (o.d->value.isArray())
497 QJsonPrivate::Writer::arrayToJson(container, json, 0, true);
498 else
499 QJsonPrivate::Writer::objectToJson(container, json, 0, true);
500 dbg.nospace() << "QJsonDocument("
501 << json.constData() // print as utf-8 string without extra quotation marks
502 << ')';
503 return dbg;
504}
505#endif
506
507#ifndef QT_NO_DATASTREAM
509{
511 return stream;
512}
513
515{
517 stream >> buffer;
518 QJsonParseError parseError{};
519 doc = QJsonDocument::fromJson(buffer, &parseError);
520 if (parseError.error && !buffer.isEmpty())
522 return stream;
523}
524#endif
525
\inmodule QtCore
Definition qbytearray.h:57
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:474
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
Definition qbytearray.h:122
static QCborArray fromStringList(const QStringList &list)
Returns a QCborArray containing all the strings found in the list list.
\inmodule QtCore\reentrant
Definition qcborvalue.h:50
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:30
\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
bool operator==(const QJsonDocument &other) const
Returns true if the other document is equal to this document.
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:24
int id(int=0) const
Definition qmetatype.h:454
\inmodule QtCore
Definition qstringview.h:76
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\inmodule QtCore
Definition qvariant.h:64
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,...
qSwap(pi, e)
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)
GLuint64 key
GLenum GLuint buffer
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint GLsizei GLsizei GLenum format
GLuint GLfloat * val
GLenum array
GLuint64EXT * result
[6]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
ptrdiff_t qsizetype
Definition qtypes.h:70
unsigned int uint
Definition qtypes.h:29
QObject::connect nullptr
QVariant variant
[1]
QSharedPointer< T > other(t)
[5]
\inmodule QtCore\reentrant