json/qjsonobject.cpp

Switch to Source codePreprocessed file
LineSource CodeCoverage
1 -
2 -
3QJsonObject::QJsonObject() -
4 : d(0), o(0) -
5{ -
6}
-
7 -
8 -
9 -
10 -
11QJsonObject::QJsonObject(QJsonPrivate::Data *data, QJsonPrivate::Object *object) -
12 : d(data), o(object) -
13{ -
14 qt_noop(); -
15 qt_noop(); -
16 d->ref.ref(); -
17}
-
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 -
62 -
63 QJsonObject object; -
64 for (QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it)
-
65 object.insert(it.key(), QJsonValue::fromVariant(it.value()));
-
66 return object;
-
67} -
68 -
69 -
70 -
71 -
72 -
73 -
74QVariantMap QJsonObject::toVariantMap() const -
75{ -
76 QVariantMap map; -
77 if (o) {
-
78 for (uint i = 0; i < o->length; ++i) {
-
79 QJsonPrivate::Entry *e = o->entryAt(i); -
80 map.insert(e->key(), QJsonValue(d, o, e->value).toVariant()); -
81 }
-
82 }
-
83 return map;
-
84} -
85 -
86 -
87 -
88 -
89QStringList QJsonObject::keys() const -
90{ -
91 if (!d)
-
92 return QStringList();
-
93 -
94 QStringList keys; -
95 -
96 for (uint i = 0; i < o->length; ++i) {
-
97 QJsonPrivate::Entry *e = o->entryAt(i); -
98 keys.append(e->key()); -
99 }
-
100 -
101 return keys;
-
102} -
103 -
104 -
105 -
106 -
107int QJsonObject::size() const -
108{ -
109 if (!d)
-
110 return 0;
-
111 -
112 return o->length;
-
113} -
114 -
115 -
116 -
117 -
118 -
119 -
120bool QJsonObject::isEmpty() const -
121{ -
122 if (!d)
-
123 return true;
-
124 -
125 return !o->length;
-
126} -
127QJsonValue QJsonObject::value(const QString &key) const -
128{ -
129 if (!d)
-
130 return QJsonValue();
-
131 -
132 bool keyExists; -
133 int i = o->indexOf(key, &keyExists); -
134 if (!keyExists)
-
135 return QJsonValue(QJsonValue::Undefined);
-
136 return QJsonValue(d, o, o->entryAt(i)->value);
-
137} -
138QJsonValue QJsonObject::operator [](const QString &key) const -
139{ -
140 return value(key);
-
141} -
142QJsonValueRef QJsonObject::operator [](const QString &key) -
143{ -
144 -
145 bool keyExists = false; -
146 int index = o ? o->indexOf(key, &keyExists) : -1;
-
147 if (!keyExists) {
-
148 iterator i = insert(key, QJsonValue()); -
149 index = i.i; -
150 }
-
151 return QJsonValueRef(this, index);
-
152} -
153QJsonObject::iterator QJsonObject::insert(const QString &key, const QJsonValue &value) -
154{ -
155 if (value.t == QJsonValue::Undefined) {
-
156 remove(key); -
157 return end();
-
158 } -
159 -
160 bool latinOrIntValue; -
161 int valueSize = QJsonPrivate::Value::requiredStorage(value, &latinOrIntValue); -
162 -
163 bool latinKey = QJsonPrivate::useCompressed(key); -
164 int valueOffset = sizeof(QJsonPrivate::Entry) + QJsonPrivate::qStringSize(key, latinKey); -
165 int requiredSize = valueOffset + valueSize; -
166 -
167 detach(requiredSize + sizeof(QJsonPrivate::offset)); -
168 -
169 if (!o->length)
-
170 o->tableOffset = sizeof(QJsonPrivate::Object);
-
171 -
172 bool keyExists = false; -
173 int pos = o->indexOf(key, &keyExists); -
174 if (keyExists)
-
175 ++d->compactionCounter;
-
176 -
177 o->reserveSpace(requiredSize, pos, 1, keyExists); -
178 -
179 QJsonPrivate::Entry *e = o->entryAt(pos); -
180 e->value.type = value.t; -
181 e->value.latinKey = latinKey; -
182 e->value.latinOrIntValue = latinOrIntValue; -
183 e->value.value = QJsonPrivate::Value::valueToStore(value, (char *)e - (char *)o + valueOffset); -
184 QJsonPrivate::copyString((char *)(e + 1), key, latinKey); -
185 if (valueSize)
-
186 QJsonPrivate::Value::copyData(value, (char *)e + valueOffset, latinOrIntValue);
-
187 -
188 return iterator(this, pos);
-
189} -
190 -
191 -
192 -
193 -
194 -
195 -
196void QJsonObject::remove(const QString &key) -
197{ -
198 if (!d)
-
199 return;
-
200 -
201 bool keyExists; -
202 int index = o->indexOf(key, &keyExists); -
203 if (!keyExists)
-
204 return;
-
205 -
206 detach(); -
207 o->removeItems(index, 1); -
208 ++d->compactionCounter; -
209 if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u)
-
210 compact();
-
211}
-
212QJsonValue QJsonObject::take(const QString &key) -
213{ -
214 if (!o)
-
215 return QJsonValue(QJsonValue::Undefined);
-
216 -
217 bool keyExists; -
218 int index = o->indexOf(key, &keyExists); -
219 if (!keyExists)
-
220 return QJsonValue(QJsonValue::Undefined);
-
221 -
222 QJsonValue v(d, o, o->entryAt(index)->value); -
223 detach(); -
224 o->removeItems(index, 1); -
225 ++d->compactionCounter; -
226 if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u)
-
227 compact();
-
228 -
229 return v;
-
230} -
231 -
232 -
233 -
234 -
235 -
236 -
237bool QJsonObject::contains(const QString &key) const -
238{ -
239 if (!o)
-
240 return false;
-
241 -
242 bool keyExists; -
243 o->indexOf(key, &keyExists); -
244 return keyExists;
-
245} -
246 -
247 -
248 -
249 -
250bool QJsonObject::operator==(const QJsonObject &other) const -
251{ -
252 if (o == other.o)
-
253 return true;
-
254 -
255 if (!o)
-
256 return !other.o->length;
-
257 if (!other.o)
-
258 return !o->length;
-
259 if (o->length != other.o->length)
-
260 return false;
-
261 -
262 for (uint i = 0; i < o->length; ++i) {
-
263 QJsonPrivate::Entry *e = o->entryAt(i); -
264 QJsonValue v(d, o, e->value); -
265 if (other.value(e->key()) != v)
-
266 return false;
-
267 }
-
268 -
269 return true;
-
270} -
271 -
272 -
273 -
274 -
275bool QJsonObject::operator!=(const QJsonObject &other) const -
276{ -
277 return !(*this == other);
-
278} -
279QJsonObject::iterator QJsonObject::erase(QJsonObject::iterator it) -
280{ -
281 qt_noop(); -
282 if (it.o != this || it.i < 0 || it.i >= (int)o->length)
-
283 return iterator(this, o->length);
-
284 -
285 int index = it.i; -
286 -
287 o->removeItems(index, 1); -
288 ++d->compactionCounter; -
289 if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u)
-
290 compact();
-
291 -
292 -
293 return it;
-
294} -
295QJsonObject::iterator QJsonObject::find(const QString &key) -
296{ -
297 bool keyExists = false; -
298 int index = o ? o->indexOf(key, &keyExists) : 0;
-
299 if (!keyExists)
-
300 return end();
-
301 detach(); -
302 return iterator(this, index);
-
303} -
304QJsonObject::const_iterator QJsonObject::constFind(const QString &key) const -
305{ -
306 bool keyExists = false; -
307 int index = o ? o->indexOf(key, &keyExists) : 0;
-
308 if (!keyExists)
-
309 return end();
-
310 return const_iterator(this, index);
-
311} -
312void QJsonObject::detach(uint reserve) -
313{ -
314 if (!d) {
-
315 d = new QJsonPrivate::Data(reserve, QJsonValue::Object); -
316 o = static_cast<QJsonPrivate::Object *>(d->header->root()); -
317 d->ref.ref(); -
318 return;
-
319 } -
320 if (reserve == 0 && d->ref.load() == 1)
-
321 return;
-
322 -
323 QJsonPrivate::Data *x = d->clone(o, reserve); -
324 x->ref.ref(); -
325 if (!d->ref.deref())
-
326 delete d;
-
327 d = x; -
328 o = static_cast<QJsonPrivate::Object *>(d->header->root()); -
329}
-
330 -
331 -
332 -
333 -
334void QJsonObject::compact() -
335{ -
336 if (!d || !d->compactionCounter)
-
337 return;
-
338 -
339 detach(); -
340 d->compact(); -
341 o = static_cast<QJsonPrivate::Object *>(d->header->root()); -
342}
-
343 -
344 -
345 -
346 -
347QString QJsonObject::keyAt(int i) const -
348{ -
349 qt_noop(); -
350 -
351 QJsonPrivate::Entry *e = o->entryAt(i); -
352 return e->key();
-
353} -
354 -
355 -
356 -
357 -
358QJsonValue QJsonObject::valueAt(int i) const -
359{ -
360 if (!o || i < 0 || i >= (int)o->length)
-
361 return QJsonValue(QJsonValue::Undefined);
-
362 -
363 QJsonPrivate::Entry *e = o->entryAt(i); -
364 return QJsonValue(d, o, e->value);
-
365} -
366 -
367 -
368 -
369 -
370void QJsonObject::setValueAt(int i, const QJsonValue &val) -
371{ -
372 qt_noop(); -
373 -
374 QJsonPrivate::Entry *e = o->entryAt(i); -
375 insert(e->key(), val); -
376}
-
377 -
378 -
379QDebug operator<<(QDebug dbg, const QJsonObject &o) -
380{ -
381 if (!o.o) {
-
382 dbg << "QJsonObject()"; -
383 return dbg;
-
384 } -
385 QByteArray json; -
386 QJsonPrivate::Writer::objectToJson(o.o, json, 0, true); -
387 dbg.nospace() << "QJsonObject(" -
388 << json.constData() -
389 << ")"; -
390 return dbg.space();
-
391} -
392 -
393 -
394 -
395 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial