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