| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/json/qjsondocument.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
| 5 | ** | - | ||||||||||||
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||
| 7 | ** | - | ||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
| 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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
| 22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
| 25 | ** | - | ||||||||||||
| 26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
| 29 | ** | - | ||||||||||||
| 30 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 31 | ** | - | ||||||||||||
| 32 | ****************************************************************************/ | - | ||||||||||||
| 33 | - | |||||||||||||
| 34 | #include <qjsondocument.h> | - | ||||||||||||
| 35 | #include <qjsonobject.h> | - | ||||||||||||
| 36 | #include <qjsonvalue.h> | - | ||||||||||||
| 37 | #include <qjsonarray.h> | - | ||||||||||||
| 38 | #include <qstringlist.h> | - | ||||||||||||
| 39 | #include <qvariant.h> | - | ||||||||||||
| 40 | #include <qdebug.h> | - | ||||||||||||
| 41 | #include "qjsonwriter_p.h" | - | ||||||||||||
| 42 | #include "qjsonparser_p.h" | - | ||||||||||||
| 43 | #include "qjson_p.h" | - | ||||||||||||
| 44 | - | |||||||||||||
| 45 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 46 | - | |||||||||||||
| 47 | /*! \class QJsonDocument | - | ||||||||||||
| 48 | \inmodule QtCore | - | ||||||||||||
| 49 | \ingroup json | - | ||||||||||||
| 50 | \ingroup shared | - | ||||||||||||
| 51 | \reentrant | - | ||||||||||||
| 52 | \since 5.0 | - | ||||||||||||
| 53 | - | |||||||||||||
| 54 | \brief The QJsonDocument class provides a way to read and write JSON documents. | - | ||||||||||||
| 55 | - | |||||||||||||
| 56 | QJsonDocument is a class that wraps a complete JSON document and can read and | - | ||||||||||||
| 57 | write this document both from a UTF-8 encoded text based representation as well | - | ||||||||||||
| 58 | as Qt's own binary format. | - | ||||||||||||
| 59 | - | |||||||||||||
| 60 | A JSON document can be converted from its text-based representation to a QJsonDocument | - | ||||||||||||
| 61 | using QJsonDocument::fromJson(). toJson() converts it back to text. The parser is very | - | ||||||||||||
| 62 | fast and efficient and converts the JSON to the binary representation used by Qt. | - | ||||||||||||
| 63 | - | |||||||||||||
| 64 | Validity of the parsed document can be queried with !isNull() | - | ||||||||||||
| 65 | - | |||||||||||||
| 66 | A document can be queried as to whether it contains an array or an object using isArray() | - | ||||||||||||
| 67 | and isObject(). The array or object contained in the document can be retrieved using | - | ||||||||||||
| 68 | array() or object() and then read or manipulated. | - | ||||||||||||
| 69 | - | |||||||||||||
| 70 | A document can also be created from a stored binary representation using fromBinaryData() or | - | ||||||||||||
| 71 | fromRawData(). | - | ||||||||||||
| 72 | - | |||||||||||||
| 73 | \sa {JSON Support in Qt}, {JSON Save Game Example} | - | ||||||||||||
| 74 | */ | - | ||||||||||||
| 75 | - | |||||||||||||
| 76 | /*! | - | ||||||||||||
| 77 | * Constructs an empty and invalid document. | - | ||||||||||||
| 78 | */ | - | ||||||||||||
| 79 | QJsonDocument::QJsonDocument() | - | ||||||||||||
| 80 | : d(0) | - | ||||||||||||
| 81 | { | - | ||||||||||||
| 82 | } executed 331 times by 5 tests: end of blockExecuted by:
| 331 | ||||||||||||
| 83 | - | |||||||||||||
| 84 | /*! | - | ||||||||||||
| 85 | * Creates a QJsonDocument from \a object. | - | ||||||||||||
| 86 | */ | - | ||||||||||||
| 87 | QJsonDocument::QJsonDocument(const QJsonObject &object) | - | ||||||||||||
| 88 | : d(0) | - | ||||||||||||
| 89 | { | - | ||||||||||||
| 90 | setObject(object); | - | ||||||||||||
| 91 | } never executed: end of block | 0 | ||||||||||||
| 92 | - | |||||||||||||
| 93 | /*! | - | ||||||||||||
| 94 | * Constructs a QJsonDocument from \a array. | - | ||||||||||||
| 95 | */ | - | ||||||||||||
| 96 | QJsonDocument::QJsonDocument(const QJsonArray &array) | - | ||||||||||||
| 97 | : d(0) | - | ||||||||||||
| 98 | { | - | ||||||||||||
| 99 | setArray(array); | - | ||||||||||||
| 100 | } never executed: end of block | 0 | ||||||||||||
| 101 | - | |||||||||||||
| 102 | /*! | - | ||||||||||||
| 103 | \internal | - | ||||||||||||
| 104 | */ | - | ||||||||||||
| 105 | QJsonDocument::QJsonDocument(QJsonPrivate::Data *data) | - | ||||||||||||
| 106 | : d(data) | - | ||||||||||||
| 107 | { | - | ||||||||||||
| 108 | Q_ASSERT(d); | - | ||||||||||||
| 109 | d->ref.ref(); | - | ||||||||||||
| 110 | } executed 686 times by 113 tests: end of blockExecuted by:
| 686 | ||||||||||||
| 111 | - | |||||||||||||
| 112 | /*! | - | ||||||||||||
| 113 | Deletes the document. | - | ||||||||||||
| 114 | - | |||||||||||||
| 115 | Binary data set with fromRawData is not freed. | - | ||||||||||||
| 116 | */ | - | ||||||||||||
| 117 | QJsonDocument::~QJsonDocument() | - | ||||||||||||
| 118 | { | - | ||||||||||||
| 119 | if (d && !d->ref.deref())
| 7-687 | ||||||||||||
| 120 | delete d; executed 7 times by 3 tests: delete d;Executed by:
| 7 | ||||||||||||
| 121 | } executed 1103 times by 116 tests: end of blockExecuted by:
| 1103 | ||||||||||||
| 122 | - | |||||||||||||
| 123 | /*! | - | ||||||||||||
| 124 | * Creates a copy of the \a other document. | - | ||||||||||||
| 125 | */ | - | ||||||||||||
| 126 | QJsonDocument::QJsonDocument(const QJsonDocument &other) | - | ||||||||||||
| 127 | { | - | ||||||||||||
| 128 | d = other.d; | - | ||||||||||||
| 129 | if (d)
| 0-86 | ||||||||||||
| 130 | d->ref.ref(); never executed: d->ref.ref(); | 0 | ||||||||||||
| 131 | } executed 86 times by 3 tests: end of blockExecuted by:
| 86 | ||||||||||||
| 132 | - | |||||||||||||
| 133 | /*! | - | ||||||||||||
| 134 | * Assigns the \a other document to this QJsonDocument. | - | ||||||||||||
| 135 | * Returns a reference to this object. | - | ||||||||||||
| 136 | */ | - | ||||||||||||
| 137 | QJsonDocument &QJsonDocument::operator =(const QJsonDocument &other) | - | ||||||||||||
| 138 | { | - | ||||||||||||
| 139 | if (d != other.d) {
| 1 | ||||||||||||
| 140 | if (d && !d->ref.deref())
| 0-1 | ||||||||||||
| 141 | delete d; never executed: delete d; | 0 | ||||||||||||
| 142 | d = other.d; | - | ||||||||||||
| 143 | if (d)
| 0-1 | ||||||||||||
| 144 | d->ref.ref(); executed 1 time by 1 test: d->ref.ref();Executed by:
| 1 | ||||||||||||
| 145 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||
| 146 | - | |||||||||||||
| 147 | return *this; executed 2 times by 2 tests: return *this;Executed by:
| 2 | ||||||||||||
| 148 | } | - | ||||||||||||
| 149 | - | |||||||||||||
| 150 | /*! \enum QJsonDocument::DataValidation | - | ||||||||||||
| 151 | - | |||||||||||||
| 152 | This value is used to tell QJsonDocument whether to validate the binary data | - | ||||||||||||
| 153 | when converting to a QJsonDocument using fromBinaryData() or fromRawData(). | - | ||||||||||||
| 154 | - | |||||||||||||
| 155 | \value Validate Validate the data before using it. This is the default. | - | ||||||||||||
| 156 | \value BypassValidation Bypasses data validation. Only use if you received the | - | ||||||||||||
| 157 | data from a trusted place and know it's valid, as using of invalid data can crash | - | ||||||||||||
| 158 | the application. | - | ||||||||||||
| 159 | */ | - | ||||||||||||
| 160 | - | |||||||||||||
| 161 | /*! | - | ||||||||||||
| 162 | Creates a QJsonDocument that uses the first \a size bytes from | - | ||||||||||||
| 163 | \a data. It assumes \a data contains a binary encoded JSON document. | - | ||||||||||||
| 164 | The created document does not take ownership of \a data and the caller | - | ||||||||||||
| 165 | has to guarantee that \a data will not be deleted or modified as long as | - | ||||||||||||
| 166 | any QJsonDocument, QJsonObject or QJsonArray still references the data. | - | ||||||||||||
| 167 | - | |||||||||||||
| 168 | \a data has to be aligned to a 4 byte boundary. | - | ||||||||||||
| 169 | - | |||||||||||||
| 170 | \a validation decides whether the data is checked for validity before being used. | - | ||||||||||||
| 171 | By default the data is validated. If the \a data is not valid, the method returns | - | ||||||||||||
| 172 | a null document. | - | ||||||||||||
| 173 | - | |||||||||||||
| 174 | Returns a QJsonDocument representing the data. | - | ||||||||||||
| 175 | - | |||||||||||||
| 176 | \sa rawData(), fromBinaryData(), isNull(), DataValidation | - | ||||||||||||
| 177 | */ | - | ||||||||||||
| 178 | QJsonDocument QJsonDocument::fromRawData(const char *data, int size, DataValidation validation) | - | ||||||||||||
| 179 | { | - | ||||||||||||
| 180 | if (quintptr(data) & 3) {
| 0 | ||||||||||||
| 181 | qWarning() <<"QJsonDocument::fromRawData: data has to have 4 byte alignment"; | - | ||||||||||||
| 182 | return QJsonDocument(); never executed: return QJsonDocument(); | 0 | ||||||||||||
| 183 | } | - | ||||||||||||
| 184 | - | |||||||||||||
| 185 | if (size < (int)(sizeof(QJsonPrivate::Header) + sizeof(QJsonPrivate::Base)))
| 0 | ||||||||||||
| 186 | return QJsonDocument(); never executed: return QJsonDocument(); | 0 | ||||||||||||
| 187 | - | |||||||||||||
| 188 | QJsonPrivate::Data *d = new QJsonPrivate::Data((char *)data, size); | - | ||||||||||||
| 189 | d->ownsData = false; | - | ||||||||||||
| 190 | - | |||||||||||||
| 191 | if (validation != BypassValidation && !d->valid()) {
| 0 | ||||||||||||
| 192 | delete d; | - | ||||||||||||
| 193 | return QJsonDocument(); never executed: return QJsonDocument(); | 0 | ||||||||||||
| 194 | } | - | ||||||||||||
| 195 | - | |||||||||||||
| 196 | return QJsonDocument(d); never executed: return QJsonDocument(d); | 0 | ||||||||||||
| 197 | } | - | ||||||||||||
| 198 | - | |||||||||||||
| 199 | /*! | - | ||||||||||||
| 200 | Returns the raw binary representation of the data | - | ||||||||||||
| 201 | \a size will contain the size of the returned data. | - | ||||||||||||
| 202 | - | |||||||||||||
| 203 | This method is useful to e.g. stream the JSON document | - | ||||||||||||
| 204 | in it's binary form to a file. | - | ||||||||||||
| 205 | */ | - | ||||||||||||
| 206 | const char *QJsonDocument::rawData(int *size) const | - | ||||||||||||
| 207 | { | - | ||||||||||||
| 208 | if (!d) {
| 0 | ||||||||||||
| 209 | *size = 0; | - | ||||||||||||
| 210 | return 0; never executed: return 0; | 0 | ||||||||||||
| 211 | } | - | ||||||||||||
| 212 | *size = d->alloc; | - | ||||||||||||
| 213 | return d->rawData; never executed: return d->rawData; | 0 | ||||||||||||
| 214 | } | - | ||||||||||||
| 215 | - | |||||||||||||
| 216 | /*! | - | ||||||||||||
| 217 | Creates a QJsonDocument from \a data. | - | ||||||||||||
| 218 | - | |||||||||||||
| 219 | \a validation decides whether the data is checked for validity before being used. | - | ||||||||||||
| 220 | By default the data is validated. If the \a data is not valid, the method returns | - | ||||||||||||
| 221 | a null document. | - | ||||||||||||
| 222 | - | |||||||||||||
| 223 | \sa toBinaryData(), fromRawData(), isNull(), DataValidation | - | ||||||||||||
| 224 | */ | - | ||||||||||||
| 225 | QJsonDocument QJsonDocument::fromBinaryData(const QByteArray &data, DataValidation validation) | - | ||||||||||||
| 226 | { | - | ||||||||||||
| 227 | if (data.size() < (int)(sizeof(QJsonPrivate::Header) + sizeof(QJsonPrivate::Base)))
| 0-679 | ||||||||||||
| 228 | return QJsonDocument(); never executed: return QJsonDocument(); | 0 | ||||||||||||
| 229 | - | |||||||||||||
| 230 | QJsonPrivate::Header h; | - | ||||||||||||
| 231 | memcpy(&h, data.constData(), sizeof(QJsonPrivate::Header)); | - | ||||||||||||
| 232 | QJsonPrivate::Base root; | - | ||||||||||||
| 233 | memcpy(&root, data.constData() + sizeof(QJsonPrivate::Header), sizeof(QJsonPrivate::Base)); | - | ||||||||||||
| 234 | - | |||||||||||||
| 235 | // do basic checks here, so we don't try to allocate more memory than we can. | - | ||||||||||||
| 236 | if (h.tag != QJsonDocument::BinaryFormatTag || h.version != 1u ||
| 0-679 | ||||||||||||
| 237 | sizeof(QJsonPrivate::Header) + root.size > (uint)data.size())
| 0-679 | ||||||||||||
| 238 | return QJsonDocument(); never executed: return QJsonDocument(); | 0 | ||||||||||||
| 239 | - | |||||||||||||
| 240 | const uint size = sizeof(QJsonPrivate::Header) + root.size; | - | ||||||||||||
| 241 | char *raw = (char *)malloc(size); | - | ||||||||||||
| 242 | if (!raw)
| 0-679 | ||||||||||||
| 243 | return QJsonDocument(); never executed: return QJsonDocument(); | 0 | ||||||||||||
| 244 | - | |||||||||||||
| 245 | memcpy(raw, data.constData(), size); | - | ||||||||||||
| 246 | QJsonPrivate::Data *d = new QJsonPrivate::Data(raw, size); | - | ||||||||||||
| 247 | - | |||||||||||||
| 248 | if (validation != BypassValidation && !d->valid()) {
| 0-679 | ||||||||||||
| 249 | delete d; | - | ||||||||||||
| 250 | return QJsonDocument(); never executed: return QJsonDocument(); | 0 | ||||||||||||
| 251 | } | - | ||||||||||||
| 252 | - | |||||||||||||
| 253 | return QJsonDocument(d); executed 679 times by 110 tests: return QJsonDocument(d);Executed by:
| 679 | ||||||||||||
| 254 | } | - | ||||||||||||
| 255 | - | |||||||||||||
| 256 | /*! | - | ||||||||||||
| 257 | Creates a QJsonDocument from the QVariant \a variant. | - | ||||||||||||
| 258 | - | |||||||||||||
| 259 | If the \a variant contains any other type than a QVariantMap, | - | ||||||||||||
| 260 | QVariantList or QStringList, the returned document is invalid. | - | ||||||||||||
| 261 | - | |||||||||||||
| 262 | \sa toVariant() | - | ||||||||||||
| 263 | */ | - | ||||||||||||
| 264 | QJsonDocument QJsonDocument::fromVariant(const QVariant &variant) | - | ||||||||||||
| 265 | { | - | ||||||||||||
| 266 | QJsonDocument doc; | - | ||||||||||||
| 267 | if (variant.type() == QVariant::Map) {
| 0 | ||||||||||||
| 268 | doc.setObject(QJsonObject::fromVariantMap(variant.toMap())); | - | ||||||||||||
| 269 | } else if (variant.type() == QVariant::List) { never executed: end of block
| 0 | ||||||||||||
| 270 | doc.setArray(QJsonArray::fromVariantList(variant.toList())); | - | ||||||||||||
| 271 | } else if (variant.type() == QVariant::StringList) { never executed: end of block
| 0 | ||||||||||||
| 272 | doc.setArray(QJsonArray::fromStringList(variant.toStringList())); | - | ||||||||||||
| 273 | } never executed: end of block | 0 | ||||||||||||
| 274 | return doc; never executed: return doc; | 0 | ||||||||||||
| 275 | } | - | ||||||||||||
| 276 | - | |||||||||||||
| 277 | /*! | - | ||||||||||||
| 278 | Returns a QVariant representing the Json document. | - | ||||||||||||
| 279 | - | |||||||||||||
| 280 | The returned variant will be a QVariantList if the document is | - | ||||||||||||
| 281 | a QJsonArray and a QVariantMap if the document is a QJsonObject. | - | ||||||||||||
| 282 | - | |||||||||||||
| 283 | \sa fromVariant(), QJsonValue::toVariant() | - | ||||||||||||
| 284 | */ | - | ||||||||||||
| 285 | QVariant QJsonDocument::toVariant() const | - | ||||||||||||
| 286 | { | - | ||||||||||||
| 287 | if (!d)
| 0 | ||||||||||||
| 288 | return QVariant(); never executed: return QVariant(); | 0 | ||||||||||||
| 289 | - | |||||||||||||
| 290 | if (d->header->root()->isArray())
| 0 | ||||||||||||
| 291 | return QJsonArray(d, static_cast<QJsonPrivate::Array *>(d->header->root())).toVariantList(); never executed: return QJsonArray(d, static_cast<QJsonPrivate::Array *>(d->header->root())).toVariantList(); | 0 | ||||||||||||
| 292 | else | - | ||||||||||||
| 293 | return QJsonObject(d, static_cast<QJsonPrivate::Object *>(d->header->root())).toVariantMap(); never executed: return QJsonObject(d, static_cast<QJsonPrivate::Object *>(d->header->root())).toVariantMap(); | 0 | ||||||||||||
| 294 | } | - | ||||||||||||
| 295 | - | |||||||||||||
| 296 | /*! | - | ||||||||||||
| 297 | Converts the QJsonDocument to a UTF-8 encoded JSON document. | - | ||||||||||||
| 298 | - | |||||||||||||
| 299 | \sa fromJson() | - | ||||||||||||
| 300 | */ | - | ||||||||||||
| 301 | #ifndef QT_JSON_READONLY | - | ||||||||||||
| 302 | QByteArray QJsonDocument::toJson() const | - | ||||||||||||
| 303 | { | - | ||||||||||||
| 304 | return toJson(Indented); never executed: return toJson(Indented); | 0 | ||||||||||||
| 305 | } | - | ||||||||||||
| 306 | #endif | - | ||||||||||||
| 307 | - | |||||||||||||
| 308 | /*! | - | ||||||||||||
| 309 | \enum QJsonDocument::JsonFormat | - | ||||||||||||
| 310 | - | |||||||||||||
| 311 | This value defines the format of the JSON byte array produced | - | ||||||||||||
| 312 | when converting to a QJsonDocument using toJson(). | - | ||||||||||||
| 313 | - | |||||||||||||
| 314 | \value Indented Defines human readable output as follows: | - | ||||||||||||
| 315 | \code | - | ||||||||||||
| 316 | { | - | ||||||||||||
| 317 | "Array": [ | - | ||||||||||||
| 318 | true, | - | ||||||||||||
| 319 | 999, | - | ||||||||||||
| 320 | "string" | - | ||||||||||||
| 321 | ], | - | ||||||||||||
| 322 | "Key": "Value", | - | ||||||||||||
| 323 | "null": null | - | ||||||||||||
| 324 | } | - | ||||||||||||
| 325 | \endcode | - | ||||||||||||
| 326 | - | |||||||||||||
| 327 | \value Compact Defines a compact output as follows: | - | ||||||||||||
| 328 | \code | - | ||||||||||||
| 329 | {"Array":[true,999,"string"],"Key":"Value","null":null} | - | ||||||||||||
| 330 | \endcode | - | ||||||||||||
| 331 | */ | - | ||||||||||||
| 332 | - | |||||||||||||
| 333 | /*! | - | ||||||||||||
| 334 | Converts the QJsonDocument to a UTF-8 encoded JSON document in the provided \a format. | - | ||||||||||||
| 335 | - | |||||||||||||
| 336 | \sa fromJson(), JsonFormat | - | ||||||||||||
| 337 | */ | - | ||||||||||||
| 338 | #ifndef QT_JSON_READONLY | - | ||||||||||||
| 339 | QByteArray QJsonDocument::toJson(JsonFormat format) const | - | ||||||||||||
| 340 | { | - | ||||||||||||
| 341 | if (!d)
| 0 | ||||||||||||
| 342 | return QByteArray(); never executed: return QByteArray(); | 0 | ||||||||||||
| 343 | - | |||||||||||||
| 344 | QByteArray json; | - | ||||||||||||
| 345 | - | |||||||||||||
| 346 | if (d->header->root()->isArray())
| 0 | ||||||||||||
| 347 | QJsonPrivate::Writer::arrayToJson(static_cast<QJsonPrivate::Array *>(d->header->root()), json, 0, (format == Compact)); never executed: QJsonPrivate::Writer::arrayToJson(static_cast<QJsonPrivate::Array *>(d->header->root()), json, 0, (format == Compact)); | 0 | ||||||||||||
| 348 | else | - | ||||||||||||
| 349 | QJsonPrivate::Writer::objectToJson(static_cast<QJsonPrivate::Object *>(d->header->root()), json, 0, (format == Compact)); never executed: QJsonPrivate::Writer::objectToJson(static_cast<QJsonPrivate::Object *>(d->header->root()), json, 0, (format == Compact)); | 0 | ||||||||||||
| 350 | - | |||||||||||||
| 351 | return json; never executed: return json; | 0 | ||||||||||||
| 352 | } | - | ||||||||||||
| 353 | #endif | - | ||||||||||||
| 354 | - | |||||||||||||
| 355 | /*! | - | ||||||||||||
| 356 | Parses \a json as a UTF-8 encoded JSON document, and creates a QJsonDocument | - | ||||||||||||
| 357 | from it. | - | ||||||||||||
| 358 | - | |||||||||||||
| 359 | Returns a valid (non-null) QJsonDocument if the parsing succeeds. If it fails, | - | ||||||||||||
| 360 | the returned document will be null, and the optional \a error variable will contain | - | ||||||||||||
| 361 | further details about the error. | - | ||||||||||||
| 362 | - | |||||||||||||
| 363 | \sa toJson(), QJsonParseError, isNull() | - | ||||||||||||
| 364 | */ | - | ||||||||||||
| 365 | QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error) | - | ||||||||||||
| 366 | { | - | ||||||||||||
| 367 | QJsonPrivate::Parser parser(json.constData(), json.length()); | - | ||||||||||||
| 368 | return parser.parse(error); executed 10 times by 4 tests: return parser.parse(error);Executed by:
| 10 | ||||||||||||
| 369 | } | - | ||||||||||||
| 370 | - | |||||||||||||
| 371 | /*! | - | ||||||||||||
| 372 | Returns \c true if the document doesn't contain any data. | - | ||||||||||||
| 373 | */ | - | ||||||||||||
| 374 | bool QJsonDocument::isEmpty() const | - | ||||||||||||
| 375 | { | - | ||||||||||||
| 376 | if (!d)
| 0 | ||||||||||||
| 377 | return true; never executed: return true; | 0 | ||||||||||||
| 378 | - | |||||||||||||
| 379 | return false; never executed: return false; | 0 | ||||||||||||
| 380 | } | - | ||||||||||||
| 381 | - | |||||||||||||
| 382 | /*! | - | ||||||||||||
| 383 | Returns a binary representation of the document. | - | ||||||||||||
| 384 | - | |||||||||||||
| 385 | The binary representation is also the native format used internally in Qt, | - | ||||||||||||
| 386 | and is very efficient and fast to convert to and from. | - | ||||||||||||
| 387 | - | |||||||||||||
| 388 | The binary format can be stored on disk and interchanged with other applications | - | ||||||||||||
| 389 | or computers. fromBinaryData() can be used to convert it back into a | - | ||||||||||||
| 390 | JSON document. | - | ||||||||||||
| 391 | - | |||||||||||||
| 392 | \sa fromBinaryData() | - | ||||||||||||
| 393 | */ | - | ||||||||||||
| 394 | QByteArray QJsonDocument::toBinaryData() const | - | ||||||||||||
| 395 | { | - | ||||||||||||
| 396 | if (!d || !d->rawData)
| 0 | ||||||||||||
| 397 | return QByteArray(); never executed: return QByteArray(); | 0 | ||||||||||||
| 398 | - | |||||||||||||
| 399 | return QByteArray(d->rawData, d->header->root()->size + sizeof(QJsonPrivate::Header)); never executed: return QByteArray(d->rawData, d->header->root()->size + sizeof(QJsonPrivate::Header)); | 0 | ||||||||||||
| 400 | } | - | ||||||||||||
| 401 | - | |||||||||||||
| 402 | /*! | - | ||||||||||||
| 403 | Returns \c true if the document contains an array. | - | ||||||||||||
| 404 | - | |||||||||||||
| 405 | \sa array(), isObject() | - | ||||||||||||
| 406 | */ | - | ||||||||||||
| 407 | bool QJsonDocument::isArray() const | - | ||||||||||||
| 408 | { | - | ||||||||||||
| 409 | if (!d)
| 0-2 | ||||||||||||
| 410 | return false; never executed: return false; | 0 | ||||||||||||
| 411 | - | |||||||||||||
| 412 | QJsonPrivate::Header *h = (QJsonPrivate::Header *)d->rawData; | - | ||||||||||||
| 413 | return h->root()->isArray(); executed 2 times by 2 tests: return h->root()->isArray();Executed by:
| 2 | ||||||||||||
| 414 | } | - | ||||||||||||
| 415 | - | |||||||||||||
| 416 | /*! | - | ||||||||||||
| 417 | Returns \c true if the document contains an object. | - | ||||||||||||
| 418 | - | |||||||||||||
| 419 | \sa object(), isArray() | - | ||||||||||||
| 420 | */ | - | ||||||||||||
| 421 | bool QJsonDocument::isObject() const | - | ||||||||||||
| 422 | { | - | ||||||||||||
| 423 | if (!d)
| 0-1 | ||||||||||||
| 424 | return false; never executed: return false; | 0 | ||||||||||||
| 425 | - | |||||||||||||
| 426 | QJsonPrivate::Header *h = (QJsonPrivate::Header *)d->rawData; | - | ||||||||||||
| 427 | return h->root()->isObject(); executed 1 time by 1 test: return h->root()->isObject();Executed by:
| 1 | ||||||||||||
| 428 | } | - | ||||||||||||
| 429 | - | |||||||||||||
| 430 | /*! | - | ||||||||||||
| 431 | Returns the QJsonObject contained in the document. | - | ||||||||||||
| 432 | - | |||||||||||||
| 433 | Returns an empty object if the document contains an | - | ||||||||||||
| 434 | array. | - | ||||||||||||
| 435 | - | |||||||||||||
| 436 | \sa isObject(), array(), setObject() | - | ||||||||||||
| 437 | */ | - | ||||||||||||
| 438 | QJsonObject QJsonDocument::object() const | - | ||||||||||||
| 439 | { | - | ||||||||||||
| 440 | if (d) {
| 0-685 | ||||||||||||
| 441 | QJsonPrivate::Base *b = d->header->root(); | - | ||||||||||||
| 442 | if (b->isObject())
| 0-685 | ||||||||||||
| 443 | return QJsonObject(d, static_cast<QJsonPrivate::Object *>(b)); executed 685 times by 112 tests: return QJsonObject(d, static_cast<QJsonPrivate::Object *>(b));Executed by:
| 685 | ||||||||||||
| 444 | } never executed: end of block | 0 | ||||||||||||
| 445 | return QJsonObject(); never executed: return QJsonObject(); | 0 | ||||||||||||
| 446 | } | - | ||||||||||||
| 447 | - | |||||||||||||
| 448 | /*! | - | ||||||||||||
| 449 | Returns the QJsonArray contained in the document. | - | ||||||||||||
| 450 | - | |||||||||||||
| 451 | Returns an empty array if the document contains an | - | ||||||||||||
| 452 | object. | - | ||||||||||||
| 453 | - | |||||||||||||
| 454 | \sa isArray(), object(), setArray() | - | ||||||||||||
| 455 | */ | - | ||||||||||||
| 456 | QJsonArray QJsonDocument::array() const | - | ||||||||||||
| 457 | { | - | ||||||||||||
| 458 | if (d) {
| 0-1 | ||||||||||||
| 459 | QJsonPrivate::Base *b = d->header->root(); | - | ||||||||||||
| 460 | if (b->isArray())
| 0-1 | ||||||||||||
| 461 | return QJsonArray(d, static_cast<QJsonPrivate::Array *>(b)); executed 1 time by 1 test: return QJsonArray(d, static_cast<QJsonPrivate::Array *>(b));Executed by:
| 1 | ||||||||||||
| 462 | } never executed: end of block | 0 | ||||||||||||
| 463 | return QJsonArray(); never executed: return QJsonArray(); | 0 | ||||||||||||
| 464 | } | - | ||||||||||||
| 465 | - | |||||||||||||
| 466 | /*! | - | ||||||||||||
| 467 | Sets \a object as the main object of this document. | - | ||||||||||||
| 468 | - | |||||||||||||
| 469 | \sa setArray(), object() | - | ||||||||||||
| 470 | */ | - | ||||||||||||
| 471 | void QJsonDocument::setObject(const QJsonObject &object) | - | ||||||||||||
| 472 | { | - | ||||||||||||
| 473 | if (d && !d->ref.deref())
| 0 | ||||||||||||
| 474 | delete d; never executed: delete d; | 0 | ||||||||||||
| 475 | - | |||||||||||||
| 476 | d = object.d; | - | ||||||||||||
| 477 | - | |||||||||||||
| 478 | if (!d) {
| 0 | ||||||||||||
| 479 | d = new QJsonPrivate::Data(0, QJsonValue::Object); | - | ||||||||||||
| 480 | } else if (d->compactionCounter || object.o != d->header->root()) { never executed: end of block
| 0 | ||||||||||||
| 481 | QJsonObject o(object); | - | ||||||||||||
| 482 | if (d->compactionCounter)
| 0 | ||||||||||||
| 483 | o.compact(); never executed: o.compact(); | 0 | ||||||||||||
| 484 | else | - | ||||||||||||
| 485 | o.detach2(); never executed: o.detach2(); | 0 | ||||||||||||
| 486 | d = o.d; | - | ||||||||||||
| 487 | d->ref.ref(); | - | ||||||||||||
| 488 | return; never executed: return; | 0 | ||||||||||||
| 489 | } | - | ||||||||||||
| 490 | d->ref.ref(); | - | ||||||||||||
| 491 | } never executed: end of block | 0 | ||||||||||||
| 492 | - | |||||||||||||
| 493 | /*! | - | ||||||||||||
| 494 | Sets \a array as the main object of this document. | - | ||||||||||||
| 495 | - | |||||||||||||
| 496 | \sa setObject(), array() | - | ||||||||||||
| 497 | */ | - | ||||||||||||
| 498 | void QJsonDocument::setArray(const QJsonArray &array) | - | ||||||||||||
| 499 | { | - | ||||||||||||
| 500 | if (d && !d->ref.deref())
| 0 | ||||||||||||
| 501 | delete d; never executed: delete d; | 0 | ||||||||||||
| 502 | - | |||||||||||||
| 503 | d = array.d; | - | ||||||||||||
| 504 | - | |||||||||||||
| 505 | if (!d) {
| 0 | ||||||||||||
| 506 | d = new QJsonPrivate::Data(0, QJsonValue::Array); | - | ||||||||||||
| 507 | } else if (d->compactionCounter || array.a != d->header->root()) { never executed: end of block
| 0 | ||||||||||||
| 508 | QJsonArray a(array); | - | ||||||||||||
| 509 | if (d->compactionCounter)
| 0 | ||||||||||||
| 510 | a.compact(); never executed: a.compact(); | 0 | ||||||||||||
| 511 | else | - | ||||||||||||
| 512 | a.detach2(); never executed: a.detach2(); | 0 | ||||||||||||
| 513 | d = a.d; | - | ||||||||||||
| 514 | d->ref.ref(); | - | ||||||||||||
| 515 | return; never executed: return; | 0 | ||||||||||||
| 516 | } | - | ||||||||||||
| 517 | d->ref.ref(); | - | ||||||||||||
| 518 | } never executed: end of block | 0 | ||||||||||||
| 519 | - | |||||||||||||
| 520 | /*! | - | ||||||||||||
| 521 | Returns \c true if the \a other document is equal to this document. | - | ||||||||||||
| 522 | */ | - | ||||||||||||
| 523 | bool QJsonDocument::operator==(const QJsonDocument &other) const | - | ||||||||||||
| 524 | { | - | ||||||||||||
| 525 | if (d == other.d)
| 0-8 | ||||||||||||
| 526 | return true; executed 8 times by 1 test: return true;Executed by:
| 8 | ||||||||||||
| 527 | - | |||||||||||||
| 528 | if (!d || !other.d)
| 0 | ||||||||||||
| 529 | return false; never executed: return false; | 0 | ||||||||||||
| 530 | - | |||||||||||||
| 531 | if (d->header->root()->isArray() != other.d->header->root()->isArray())
| 0 | ||||||||||||
| 532 | return false; never executed: return false; | 0 | ||||||||||||
| 533 | - | |||||||||||||
| 534 | if (d->header->root()->isObject())
| 0 | ||||||||||||
| 535 | return QJsonObject(d, static_cast<QJsonPrivate::Object *>(d->header->root())) never executed: return QJsonObject(d, static_cast<QJsonPrivate::Object *>(d->header->root())) == QJsonObject(other.d, static_cast<QJsonPrivate::Object *>(other.d->header->root())); | 0 | ||||||||||||
| 536 | == QJsonObject(other.d, static_cast<QJsonPrivate::Object *>(other.d->header->root())); never executed: return QJsonObject(d, static_cast<QJsonPrivate::Object *>(d->header->root())) == QJsonObject(other.d, static_cast<QJsonPrivate::Object *>(other.d->header->root())); | 0 | ||||||||||||
| 537 | else | - | ||||||||||||
| 538 | return QJsonArray(d, static_cast<QJsonPrivate::Array *>(d->header->root())) never executed: return QJsonArray(d, static_cast<QJsonPrivate::Array *>(d->header->root())) == QJsonArray(other.d, static_cast<QJsonPrivate::Array *>(other.d->header->root())); | 0 | ||||||||||||
| 539 | == QJsonArray(other.d, static_cast<QJsonPrivate::Array *>(other.d->header->root())); never executed: return QJsonArray(d, static_cast<QJsonPrivate::Array *>(d->header->root())) == QJsonArray(other.d, static_cast<QJsonPrivate::Array *>(other.d->header->root())); | 0 | ||||||||||||
| 540 | } | - | ||||||||||||
| 541 | - | |||||||||||||
| 542 | /*! | - | ||||||||||||
| 543 | \fn bool QJsonDocument::operator!=(const QJsonDocument &other) const | - | ||||||||||||
| 544 | - | |||||||||||||
| 545 | returns \c true if \a other is not equal to this document | - | ||||||||||||
| 546 | */ | - | ||||||||||||
| 547 | - | |||||||||||||
| 548 | /*! | - | ||||||||||||
| 549 | returns \c true if this document is null. | - | ||||||||||||
| 550 | - | |||||||||||||
| 551 | Null documents are documents created through the default constructor. | - | ||||||||||||
| 552 | - | |||||||||||||
| 553 | Documents created from UTF-8 encoded text or the binary format are | - | ||||||||||||
| 554 | validated during parsing. If validation fails, the returned document | - | ||||||||||||
| 555 | will also be null. | - | ||||||||||||
| 556 | */ | - | ||||||||||||
| 557 | bool QJsonDocument::isNull() const | - | ||||||||||||
| 558 | { | - | ||||||||||||
| 559 | return (d == 0); executed 661 times by 113 tests: return (d == 0);Executed by:
| 661 | ||||||||||||
| 560 | } | - | ||||||||||||
| 561 | - | |||||||||||||
| 562 | #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) | - | ||||||||||||
| 563 | QDebug operator<<(QDebug dbg, const QJsonDocument &o) | - | ||||||||||||
| 564 | { | - | ||||||||||||
| 565 | QDebugStateSaver saver(dbg); | - | ||||||||||||
| 566 | if (!o.d) {
| 0-1 | ||||||||||||
| 567 | dbg << "QJsonDocument()"; | - | ||||||||||||
| 568 | return dbg; executed 1 time by 1 test: return dbg;Executed by:
| 1 | ||||||||||||
| 569 | } | - | ||||||||||||
| 570 | QByteArray json; | - | ||||||||||||
| 571 | if (o.d->header->root()->isArray())
| 0 | ||||||||||||
| 572 | QJsonPrivate::Writer::arrayToJson(static_cast<QJsonPrivate::Array *>(o.d->header->root()), json, 0, true); never executed: QJsonPrivate::Writer::arrayToJson(static_cast<QJsonPrivate::Array *>(o.d->header->root()), json, 0, true); | 0 | ||||||||||||
| 573 | else | - | ||||||||||||
| 574 | QJsonPrivate::Writer::objectToJson(static_cast<QJsonPrivate::Object *>(o.d->header->root()), json, 0, true); never executed: QJsonPrivate::Writer::objectToJson(static_cast<QJsonPrivate::Object *>(o.d->header->root()), json, 0, true); | 0 | ||||||||||||
| 575 | dbg.nospace() << "QJsonDocument(" | - | ||||||||||||
| 576 | << json.constData() // print as utf-8 string without extra quotation marks | - | ||||||||||||
| 577 | << ')'; | - | ||||||||||||
| 578 | return dbg; never executed: return dbg; | 0 | ||||||||||||
| 579 | } | - | ||||||||||||
| 580 | #endif | - | ||||||||||||
| 581 | - | |||||||||||||
| 582 | QT_END_NAMESPACE | - | ||||||||||||
| Source code | Switch to Preprocessed file |