qjsonobject.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/corelib/json/qjsonobject.cpp
Switch to Source codePreprocessed file
LineSourceCount
1-
2-
3QJsonObject::QJsonObject()-
4 : d(0), o(0)-
5{-
6}-
7QJsonObject::QJsonObject(QJsonPrivate::Data *data, QJsonPrivate::Object *object)-
8 : d(data), o(object)-
9{-
10 ((!(d)) ? qt_assert("d",__FILE__,131137) : qt_noop());-
11 ((!(o)) ? qt_assert("o",__FILE__,132138) : qt_noop());-
12 d->ref.ref();-
13}-
14void QJsonObject::initialize()-
15{-
16 d = 0;-
17 o = 0;-
18}-
19-
20-
21-
22-
23QJsonObject::~QJsonObject()-
24{-
25 if (d && !d->ref.deref())-
26 delete d;-
27}-
28-
29-
30-
31-
32-
33-
34-
35QJsonObject::QJsonObject(const QJsonObject &other)-
36{-
37 d = other.d;-
38 o = other.o;-
39 if (d)-
40 d->ref.ref();-
41}-
42-
43-
44-
45-
46QJsonObject &QJsonObject::operator =(const QJsonObject &other)-
47{-
48 if (d != other.d) {-
49 if (d && !d->ref.deref())-
50 delete d;-
51 d = other.d;-
52 if (d)-
53 d->ref.ref();-
54 }-
55 o = other.o;-
56-
57 return *this;-
58}-
59QJsonObject QJsonObject::fromVariantMap(const QVariantMap &map)-
60{-
61 QJsonObject object;-
62 if (map.isEmpty())-
63 return object;-
64-
65 object.detach2(1024);-
66-
67 QVector<QJsonPrivate::offset> offsets;-
68 QJsonPrivate::offset currentOffset;-
69 currentOffset = sizeof(QJsonPrivate::Base);-
70-
71-
72-
73 for (QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it) {-
74 QString key = it.key();-
75 QJsonValue val = QJsonValue::fromVariant(it.value());-
76-
77 bool latinOrIntValue;-
78 int valueSize = QJsonPrivate::Value::requiredStorage(val, &latinOrIntValue);-
79-
80 bool latinKey = QJsonPrivate::useCompressed(key);-
81 int valueOffset = sizeof(QJsonPrivate::Entry) + QJsonPrivate::qStringSize(key, latinKey);-
82 int requiredSize = valueOffset + valueSize;-
83-
84 if (!object.detach2(requiredSize + sizeof(QJsonPrivate::offset)))-
85 return QJsonObject();-
86-
87 QJsonPrivate::Entry *e = reinterpret_cast<QJsonPrivate::Entry *>(reinterpret_cast<char *>(object.o) + currentOffset);-
88 e->value.type = val.t;-
89 e->value.latinKey = latinKey;-
90 e->value.latinOrIntValue = latinOrIntValue;-
91 e->value.value = QJsonPrivate::Value::valueToStore(val, (char *)e - (char *)object.o + valueOffset);-
92 QJsonPrivate::copyString((char *)(e + 1), key, latinKey);-
93 if (valueSize)-
94 QJsonPrivate::Value::copyData(val, (char *)e + valueOffset, latinOrIntValue);-
95-
96 offsets << currentOffset;-
97 currentOffset += requiredSize;-
98 object.o->size = currentOffset;-
99 }-
100-
101-
102 object.o->tableOffset = currentOffset;-
103 if (!object.detach2(sizeof(QJsonPrivate::offset)*offsets.size()))-
104 return QJsonObject();-
105 memcpy(object.o->table(), offsets.constData(), offsets.size()*sizeof(uint));-
106 object.o->length = offsets.size();-
107 object.o->size = currentOffset + sizeof(QJsonPrivate::offset)*offsets.size();-
108-
109 return object;-
110}-
111QVariantMap QJsonObject::toVariantMap() const-
112{-
113 QVariantMap map;-
114 if (o) {-
115 for (uint i = 0; i < o->length; ++i) {-
116 QJsonPrivate::Entry *e = o->entryAt(i);-
117 map.insert(e->key(), QJsonValue(d, o, e->value).toVariant());-
118 }-
119 }-
120 return map;-
121}-
122QJsonObject QJsonObject::fromVariantHash(const QVariantHash &hash)-
123{-
124-
125-
126 QJsonObject object;-
127 for (QVariantHash::const_iterator it = hash.constBegin(); it != hash.constEnd(); ++it)-
128 object.insert(it.key(), QJsonValue::fromVariant(it.value()));-
129 return object;-
130}-
131QVariantHash QJsonObject::toVariantHash() const-
132{-
133 QVariantHash hash;-
134 if (o
oDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
135 hash.reserve(o->length);-
136 for (uint i = 0; i < o->length
i < o->lengthDescription
TRUEnever evaluated
FALSEnever evaluated
; ++i) {
0
137 QJsonPrivate::Entry *e = o->entryAt(i);-
138 hash.insert(e->key(), QJsonValue(d, o, e->value).toVariant());-
139 }
never executed: end of block
0
140 }
never executed: end of block
0
141 return
never executed: return hash;
hash;
never executed: return hash;
0
142}-
143-
144-
145-
146-
147-
148-
149QStringList QJsonObject::keys() const-
150{-
151 QStringList keys;-
152 if (o) {-
153 keys.reserve(o->length);-
154 for (uint i = 0; i < o->length; ++i) {-
155 QJsonPrivate::Entry *e = o->entryAt(i);-
156 keys.append(e->key());-
157 }-
158 }-
159 return keys;-
160}-
161-
162-
163-
164-
165int QJsonObject::size() const-
166{-
167 if (!d)-
168 return 0;-
169-
170 return o->length;-
171}-
172-
173-
174-
175-
176-
177-
178bool QJsonObject::isEmpty() const-
179{-
180 if (!d)-
181 return true;-
182-
183 return !o->length;-
184}-
185QJsonValue QJsonObject::value(const QString &key) const-
186{-
187 if (!d)-
188 return QJsonValue(QJsonValue::Undefined);-
189-
190 bool keyExists;-
191 int i = o->indexOf(key, &keyExists);-
192 if (!keyExists)-
193 return QJsonValue(QJsonValue::Undefined);-
194 return QJsonValue(d, o, o->entryAt(i)->value);-
195}-
196-
197-
198-
199-
200-
201QJsonValue QJsonObject::value(QLatin1String key) const-
202{-
203 if (!d
!dDescription
TRUEnever evaluated
FALSEevaluated 86427 times by 115 tests
Evaluated by:
  • tst_NetworkSelfTest
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractPrintDialog
  • tst_QAbstractScrollArea
  • tst_QAccessibility
  • tst_QApplication
  • tst_QBrush
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QColorDialog
  • tst_QColumnView
  • tst_QComboBox
  • tst_QCommandLinkButton
  • tst_QCompleter
  • tst_QDataStream
  • tst_QDateTimeEdit
  • tst_QDialog
  • tst_QDoubleSpinBox
  • tst_QFactoryLoader
  • tst_QFileDialog2
  • tst_QFileIconProvider
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QFocusEvent
  • ...
)
0-86427
204 return
never executed: return QJsonValue(QJsonValue::Undefined);
QJsonValue(QJsonValue::Undefined);
never executed: return QJsonValue(QJsonValue::Undefined);
0
205-
206 bool keyExists;-
207 int i = o->indexOf(key, &keyExists);-
208 if (!keyExists
!keyExistsDescription
TRUEevaluated 147 times by 2 tests
Evaluated by:
  • tst_QFactoryLoader
  • tst_QOpenGlConfig
FALSEevaluated 86280 times by 115 tests
Evaluated by:
  • tst_NetworkSelfTest
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractPrintDialog
  • tst_QAbstractScrollArea
  • tst_QAccessibility
  • tst_QApplication
  • tst_QBrush
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QColorDialog
  • tst_QColumnView
  • tst_QComboBox
  • tst_QCommandLinkButton
  • tst_QCompleter
  • tst_QDataStream
  • tst_QDateTimeEdit
  • tst_QDialog
  • tst_QDoubleSpinBox
  • tst_QFactoryLoader
  • tst_QFileDialog2
  • tst_QFileIconProvider
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QFocusEvent
  • ...
)
147-86280
209 return
executed 147 times by 2 tests: return QJsonValue(QJsonValue::Undefined);
Executed by:
  • tst_QFactoryLoader
  • tst_QOpenGlConfig
QJsonValue(QJsonValue::Undefined);
executed 147 times by 2 tests: return QJsonValue(QJsonValue::Undefined);
Executed by:
  • tst_QFactoryLoader
  • tst_QOpenGlConfig
147
210 return
executed 86280 times by 115 tests: return QJsonValue(d, o, o->entryAt(i)->value);
Executed by:
  • tst_NetworkSelfTest
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractPrintDialog
  • tst_QAbstractScrollArea
  • tst_QAccessibility
  • tst_QApplication
  • tst_QBrush
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QColorDialog
  • tst_QColumnView
  • tst_QComboBox
  • tst_QCommandLinkButton
  • tst_QCompleter
  • tst_QDataStream
  • tst_QDateTimeEdit
  • tst_QDialog
  • tst_QDoubleSpinBox
  • tst_QFactoryLoader
  • tst_QFileDialog2
  • tst_QFileIconProvider
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QFocusEvent
  • ...
QJsonValue(d, o, o->entryAt(i)->value);
executed 86280 times by 115 tests: return QJsonValue(d, o, o->entryAt(i)->value);
Executed by:
  • tst_NetworkSelfTest
  • tst_QAbstractItemView
  • tst_QAbstractNetworkCache
  • tst_QAbstractPrintDialog
  • tst_QAbstractScrollArea
  • tst_QAccessibility
  • tst_QApplication
  • tst_QBrush
  • tst_QButtonGroup
  • tst_QCalendarWidget
  • tst_QColorDialog
  • tst_QColumnView
  • tst_QComboBox
  • tst_QCommandLinkButton
  • tst_QCompleter
  • tst_QDataStream
  • tst_QDateTimeEdit
  • tst_QDialog
  • tst_QDoubleSpinBox
  • tst_QFactoryLoader
  • tst_QFileDialog2
  • tst_QFileIconProvider
  • tst_QFileSystemModel
  • tst_QFiledialog
  • tst_QFocusEvent
  • ...
86280
211}-
212QJsonValue QJsonObject::operator [](const QString &key) const-
213{-
214 return value(key);-
215}-
216QJsonValueRef QJsonObject::operator [](const QString &key)-
217{-
218-
219 bool keyExists = false;-
220 int index = o ? o->indexOf(key, &keyExists) : -1;-
221 if (!keyExists) {-
222 iterator i = insert(key, QJsonValue());-
223 index = i.i;-
224 }-
225 return QJsonValueRef(this, index);-
226}-
227-
228-
229-
230-
231-
232QJsonValueRef QJsonObject::operator [](QLatin1String key)-
233{-
234-
235 return
never executed: return operator[](QString(key));
operator[](QString(key));
never executed: return operator[](QString(key));
0
236}-
237QJsonObject::iterator QJsonObject::insert(const QString &key, const QJsonValue &value)-
238{-
239 if (value.t == QJsonValue::Undefined) {-
240 remove(key);-
241 return end();-
242 }-
243 QJsonValue val = value;-
244-
245 bool latinOrIntValue;-
246 int valueSize = QJsonPrivate::Value::requiredStorage(val, &latinOrIntValue);-
247-
248 bool latinKey = QJsonPrivate::useCompressed(key);-
249 int valueOffset = sizeof(QJsonPrivate::Entry) + QJsonPrivate::qStringSize(key, latinKey);-
250 int requiredSize = valueOffset + valueSize;-
251-
252 if (!detach2(requiredSize + sizeof(QJsonPrivate::offset)))-
253 return iterator();-
254-
255 if (!o->length)-
256 o->tableOffset = sizeof(QJsonPrivate::Object);-
257-
258 bool keyExists = false;-
259 int pos = o->indexOf(key, &keyExists);-
260 if (keyExists)-
261 ++d->compactionCounter;-
262-
263 uint off = o->reserveSpace(requiredSize, pos, 1, keyExists);-
264 if (!off)-
265 return end();-
266-
267 QJsonPrivate::Entry *e = o->entryAt(pos);-
268 e->value.type = val.t;-
269 e->value.latinKey = latinKey;-
270 e->value.latinOrIntValue = latinOrIntValue;-
271 e->value.value = QJsonPrivate::Value::valueToStore(val, (char *)e - (char *)o + valueOffset);-
272 QJsonPrivate::copyString((char *)(e + 1), key, latinKey);-
273 if (valueSize)-
274 QJsonPrivate::Value::copyData(val, (char *)e + valueOffset, latinOrIntValue);-
275-
276 if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u)-
277 compact();-
278-
279 return iterator(this, pos);-
280}-
281-
282-
283-
284-
285-
286-
287void QJsonObject::remove(const QString &key)-
288{-
289 if (!d)-
290 return;-
291-
292 bool keyExists;-
293 int index = o->indexOf(key, &keyExists);-
294 if (!keyExists)-
295 return;-
296-
297 detach2();-
298 o->removeItems(index, 1);-
299 ++d->compactionCounter;-
300 if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u)-
301 compact();-
302}-
303QJsonValue QJsonObject::take(const QString &key)-
304{-
305 if (!o)-
306 return QJsonValue(QJsonValue::Undefined);-
307-
308 bool keyExists;-
309 int index = o->indexOf(key, &keyExists);-
310 if (!keyExists)-
311 return QJsonValue(QJsonValue::Undefined);-
312-
313 QJsonValue v(d, o, o->entryAt(index)->value);-
314 detach2();-
315 o->removeItems(index, 1);-
316 ++d->compactionCounter;-
317 if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u)-
318 compact();-
319-
320 return v;-
321}-
322-
323-
324-
325-
326-
327-
328bool QJsonObject::contains(const QString &key) const-
329{-
330 if (!o)-
331 return false;-
332-
333 bool keyExists;-
334 o->indexOf(key, &keyExists);-
335 return keyExists;-
336}-
337-
338-
339-
340-
341-
342bool QJsonObject::contains(QLatin1String key) const-
343{-
344 if (!o
!oDescription
TRUEnever evaluated
FALSEevaluated 12 times by 1 test
Evaluated by:
  • tst_QOpenGlConfig
)
0-12
345 return
never executed: return false;
false;
never executed: return false;
0
346-
347 bool keyExists;-
348 o->indexOf(key, &keyExists);-
349 return
executed 12 times by 1 test: return keyExists;
Executed by:
  • tst_QOpenGlConfig
keyExists;
executed 12 times by 1 test: return keyExists;
Executed by:
  • tst_QOpenGlConfig
12
350}-
351-
352-
353-
354-
355bool QJsonObject::operator==(const QJsonObject &other) const-
356{-
357 if (o == other.o)-
358 return true;-
359-
360 if (!o)-
361 return !other.o->length;-
362 if (!other.o)-
363 return !o->length;-
364 if (o->length != other.o->length)-
365 return false;-
366-
367 for (uint i = 0; i < o->length; ++i) {-
368 QJsonPrivate::Entry *e = o->entryAt(i);-
369 QJsonValue v(d, o, e->value);-
370 if (other.value(e->key()) != v)-
371 return false;-
372 }-
373-
374 return true;-
375}-
376-
377-
378-
379-
380bool QJsonObject::operator!=(const QJsonObject &other) const-
381{-
382 return !(*this == other);-
383}-
384QJsonObject::iterator QJsonObject::erase(QJsonObject::iterator it)-
385{-
386 ((!(d && d->ref.load() == 1)) ? qt_assert("d && d->ref.load() == 1",__FILE__,573627) : qt_noop());-
387 if (it.o != this || it.i < 0 || it.i >= (int)o->length)-
388 return iterator(this, o->length);-
389-
390 int index = it.i;-
391-
392 o->removeItems(index, 1);-
393 ++d->compactionCounter;-
394 if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u)-
395 compact();-
396-
397-
398 return it;-
399}-
400QJsonObject::iterator QJsonObject::find(const QString &key)-
401{-
402 bool keyExists = false;-
403 int index = o ? o->indexOf(key, &keyExists) : 0;-
404 if (!keyExists)-
405 return end();-
406 detach2();-
407 return iterator(this, index);-
408}-
409-
410-
411-
412-
413-
414QJsonObject::iterator QJsonObject::find(QLatin1String key)-
415{-
416 bool keyExists = false;-
417 int index = o
oDescription
TRUEnever evaluated
FALSEnever evaluated
? o->indexOf(key, &keyExists) : 0;
0
418 if (!keyExists
!keyExistsDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
419 return
never executed: return end();
end();
never executed: return end();
0
420 detach2();-
421 return
never executed: return iterator(this, index);
iterator(this, index);
never executed: return iterator(this, index);
0
422}-
423QJsonObject::const_iterator QJsonObject::constFind(const QString &key) const-
424{-
425 bool keyExists = false;-
426 int index = o ? o->indexOf(key, &keyExists) : 0;-
427 if (!keyExists)-
428 return end();-
429 return const_iterator(this, index);-
430}-
431-
432-
433-
434-
435-
436QJsonObject::const_iterator QJsonObject::constFind(QLatin1String key) const-
437{-
438 bool keyExists = false;-
439 int index = o
oDescription
TRUEnever evaluated
FALSEnever evaluated
? o->indexOf(key, &keyExists) : 0;
0
440 if (!keyExists
!keyExistsDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
441 return
never executed: return end();
end();
never executed: return end();
0
442 return
never executed: return const_iterator(this, index);
const_iterator(this, index);
never executed: return const_iterator(this, index);
0
443}-
444void QJsonObject::detach(uint reserve)-
445{-
446 (void)reserve;-
447 ((!(!reserve)) ? qt_assert("!reserve",__FILE__,11091196) : qt_noop());-
448 detach2(reserve);-
449}-
450-
451bool QJsonObject::detach2(uint reserve)-
452{-
453 if (!d) {-
454 if (reserve >= QJsonPrivate::Value::MaxSize) {-
455 QMessageLogger(__FILE__, 11171204, __PRETTY_FUNCTION__).warning("QJson: Document too large to store in data structure");-
456 return false;-
457 }-
458 d = new QJsonPrivate::Data(reserve, QJsonValue::Object);-
459 o = static_cast<QJsonPrivate::Object *>(d->header->root());-
460 d->ref.ref();-
461 return true;-
462 }-
463 if (reserve == 0 && d->ref.load() == 1)-
464 return true;-
465-
466 QJsonPrivate::Data *x = d->clone(o, reserve);-
467 if (!x)-
468 return false;-
469 x->ref.ref();-
470 if (!d->ref.deref())-
471 delete d;-
472 d = x;-
473 o = static_cast<QJsonPrivate::Object *>(d->header->root());-
474 return true;-
475}-
476-
477-
478-
479-
480void QJsonObject::compact()-
481{-
482 if (!d || !d->compactionCounter)-
483 return;-
484-
485 detach2();-
486 d->compact();-
487 o = static_cast<QJsonPrivate::Object *>(d->header->root());-
488}-
489-
490-
491-
492-
493QString QJsonObject::keyAt(int i) const-
494{-
495 ((!(o && i >= 0 && i < (int)o->length)) ? qt_assert("o && i >= 0 && i < (int)o->length",__FILE__,11571244) : qt_noop());-
496-
497 QJsonPrivate::Entry *e = o->entryAt(i);-
498 return e->key();-
499}-
500-
501-
502-
503-
504QJsonValue QJsonObject::valueAt(int i) const-
505{-
506 if (!o || i < 0 || i >= (int)o->length)-
507 return QJsonValue(QJsonValue::Undefined);-
508-
509 QJsonPrivate::Entry *e = o->entryAt(i);-
510 return QJsonValue(d, o, e->value);-
511}-
512-
513-
514-
515-
516void QJsonObject::setValueAt(int i, const QJsonValue &val)-
517{-
518 ((!(o && i >= 0 && i < (int)o->length)) ? qt_assert("o && i >= 0 && i < (int)o->length",__FILE__,11801267) : qt_noop());-
519-
520 QJsonPrivate::Entry *e = o->entryAt(i);-
521 insert(e->key(), val);-
522}-
523-
524-
525QDebug operator<<(QDebug dbg, const QJsonObject &o)-
526{-
527 QDebugStateSaver saver(dbg);-
528 if (!o.o) {-
529 dbg << "QJsonObject()";-
530 return dbg;-
531 }-
532 QByteArray json;-
533 QJsonPrivate::Writer::objectToJson(o.o, json, 0, true);-
534 dbg.nospace() << "QJsonObject("-
535 << json.constData()-
536 << ")";-
537 return dbg;-
538}-
539-
540-
541-
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9