qmjsonobject.cpp
Go to the documentation of this file.
1 //
2 // QtMark JSON Library
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Rodney Forbes <forbesr@ainfosec.com>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 // ============================================================================
23 // Includes
24 // ============================================================================
25 
26 #include <qmjsonobject.h>
27 #include <qmjsonarray.h>
28 
29 // ============================================================================
30 // QMJsonObject Implementation
31 // ============================================================================
32 
34 {
35 }
36 
38 {
39 }
40 
41 QMJsonObject::QMJsonObject(const QHash<QString, QMPointer<QMJsonValue> > &hash)
42 {
43  mHash = hash;
44 }
45 
46 void QMJsonObject::reserve(int32_t alloc)
47 {
48  mHash.reserve(alloc);
49 }
50 
51 int32_t QMJsonObject::capacity(void) const
52 {
53  return mHash.capacity();
54 }
55 
57 {
58  mHash.squeeze();
59 }
60 
62 {
63  auto iter = mHash.begin();
64 
65  while(iter != mHash.end())
66  {
67  if(iter.value().isNull() == true)
68  continue;
69 
70  else if(iter.value()->isObject() == true)
71  iter.value()->toObject()->clear();
72 
73  else if(iter.value()->isArray() == true)
74  iter.value()->toArray()->clear();
75 
76  iter = this->erase(iter);
77  }
78 }
79 
80 int32_t QMJsonObject::count(void) const
81 {
82  return mHash.count();
83 }
84 
85 int32_t QMJsonObject::size(void) const
86 {
87  return mHash.size();
88 }
89 
90 bool QMJsonObject::isEmpty(void) const
91 {
92  return mHash.isEmpty();
93 }
94 
95 bool QMJsonObject::empty(void) const
96 {
97  return mHash.empty();
98 }
99 
100 bool QMJsonObject::contains(const QString &key) const
101 {
102  return mHash.contains(key);
103 }
104 
105 void QMJsonObject::insert(const QString &key, const QMPointer<QMJsonValue> &value, QMJsonReplacementPolicy policy)
106 {
107  if(key.isEmpty() == true)
108  return;
109 
110  auto iter = mHash.find(key);
111 
112  if(iter != mHash.end())
113  {
114  switch(policy)
115  {
117  this->erase(iter);
118  break;
119 
121  return;
122  };
123  }
124 
125  if(value.isNull() == true)
126  {
127  auto newValue = QMPointer<QMJsonValue>(new QMJsonValue);
128 
129  mHash.insert(key, newValue);
130  emit itemAdded(key, newValue);
131  }
132  else
133  {
134  mHash.insert(key, value);
135  emit itemAdded(key, value);
136  }
137 }
138 
139 void QMJsonObject::unite(const QMPointer<QMJsonObject> &object, QMJsonReplacementPolicy replacementPolicy, QMJsonArrayUnitePolicy unitePolicy)
140 {
141  if(object.isNull() == true)
142  return;
143 
144  auto iter1 = object->cbegin();
145 
146  while(iter1 != object->cend())
147  {
148  auto iter2 = mHash.constFind(iter1.key());
149 
150  if(iter2 != mHash.constEnd())
151  {
152  const auto &value1 = iter1.value();
153  const auto &value2 = iter2.value();
154 
155  if(value1.isNull() == true ||
156  value2.isNull() == true)
157  {
158  continue;
159  }
160 
161  else if(value1->isObject() && value2->isObject())
162  value2->toObject()->unite(value1->toObject(), replacementPolicy, unitePolicy);
163 
164  else if(value1->isArray() && value2->isArray())
165  value2->toArray()->unite(value1->toArray(), unitePolicy);
166 
167  else
168  this->insert(iter1.key(), value1, replacementPolicy);
169  }
170  else
171  {
172  this->insert(iter1.key(), iter1.value(), replacementPolicy);
173  }
174 
175  iter1++;
176  }
177 }
178 
179 void QMJsonObject::remove(const QString &key)
180 {
181  auto iter = mHash.find(key);
182 
183  if(iter == mHash.end())
184  return;
185 
186  this->erase(iter);
187 }
188 
189 QMPointer<QMJsonValue> QMJsonObject::take(const QString &key)
190 {
191  auto iter = mHash.find(key);
192 
193  if(iter == mHash.end())
194  return QMPointer<QMJsonValue>(new QMJsonValue);
195 
196  this->erase(iter);
197 
198  return iter.value();
199 }
200 
201 QMPointer<QMJsonValue> QMJsonObject::take(const QString &key, const QMPointer<QMJsonValue> &defaultValue)
202 {
203  auto iter = mHash.find(key);
204 
205  if(iter == mHash.end())
206  return defaultValue;
207 
208  this->erase(iter);
209 
210  return iter.value();
211 }
212 
213 const QString QMJsonObject::key(const QMPointer<QMJsonValue> &value) const
214 {
215  return mHash.key(value);
216 }
217 
218 const QString QMJsonObject::key(const QMPointer<QMJsonValue> &value, const QString &defaultValue) const
219 {
220  return mHash.key(value, defaultValue);
221 }
222 
223 QHash<QString, QMPointer<QMJsonValue> >::iterator QMJsonObject::begin(void)
224 {
225  return mHash.begin();
226 }
227 
228 QHash<QString, QMPointer<QMJsonValue> >::iterator QMJsonObject::end(void)
229 {
230  return mHash.end();
231 }
232 
233 QHash<QString, QMPointer<QMJsonValue> >::const_iterator QMJsonObject::cbegin(void) const
234 {
235  return mHash.cbegin();
236 }
237 
238 QHash<QString, QMPointer<QMJsonValue> >::const_iterator QMJsonObject::cend(void) const
239 {
240  return mHash.cend();
241 }
242 
243 const QHash<QString, QMPointer<QMJsonValue> >::iterator QMJsonObject::erase(const QHash<QString, QMPointer<QMJsonValue> >::iterator &iter)
244 {
245  if(iter == mHash.end())
246  return mHash.end();
247 
248  auto key = iter.key();
249  auto value = iter.value();
250 
251  auto niter = mHash.erase(iter);
252  emit itemRemoved(key, value);
253 
254  return niter;
255 }
256 
257 QHash<QString, QMPointer<QMJsonValue> >::iterator QMJsonObject::find(const QString &key)
258 {
259  return mHash.find(key);
260 }
261 
262 const QHash<QString, QMPointer<QMJsonValue> >::const_iterator QMJsonObject::cfind(const QString &key) const
263 {
264  return mHash.constFind(key);
265 }
266 
267 const QMPointer<QMJsonValue> &QMJsonObject::value(const QString &key) const
268 {
269  auto iter = mHash.constFind(key);
270  static auto defaultValue = QMPointer<QMJsonValue>(new QMJsonValue);
271 
272  if(iter == mHash.constEnd())
273  return defaultValue;
274 
275  return iter.value();
276 }
277 
278 const QMPointer<QMJsonValue> &QMJsonObject::value(const QString &key, const QMPointer<QMJsonValue> &defaultValue) const
279 {
280  auto iter = mHash.constFind(key);
281 
282  if(iter == mHash.constEnd())
283  return defaultValue;
284 
285  return iter.value();
286 }
287 
288 QList<QString> QMJsonObject::keys(void) const
289 {
290  return mHash.keys();
291 }
292 
293 QList<QMPointer<QMJsonValue> > QMJsonObject::values(void) const
294 {
295  return mHash.values();
296 }
297 
298 QHash<QString, QMPointer<QMJsonValue> > QMJsonObject::hash(void) const
299 {
300  return mHash;
301 }
302 
303 bool QMJsonObject::isNull(const QString &key) const
304 {
305  auto iter = mHash.constFind(key);
306 
307  if(iter == mHash.constEnd())
308  return false;
309 
310  return iter.value()->isNull();
311 }
312 
313 bool QMJsonObject::isBool(const QString &key) const
314 {
315  auto iter = mHash.constFind(key);
316 
317  if(iter == mHash.constEnd())
318  return false;
319 
320  return iter.value()->isBool();
321 }
322 
323 bool QMJsonObject::isDouble(const QString &key) const
324 {
325  auto iter = mHash.constFind(key);
326 
327  if(iter == mHash.constEnd())
328  return false;
329 
330  return iter.value()->isDouble();
331 }
332 
333 bool QMJsonObject::isString(const QString &key) const
334 {
335  auto iter = mHash.constFind(key);
336 
337  if(iter == mHash.constEnd())
338  return false;
339 
340  return iter.value()->isString();
341 }
342 
343 bool QMJsonObject::isArray(const QString &key) const
344 {
345  auto iter = mHash.constFind(key);
346 
347  if(iter == mHash.constEnd())
348  return false;
349 
350  return iter.value()->isArray();
351 }
352 
353 bool QMJsonObject::isObject(const QString &key) const
354 {
355  auto iter = mHash.constFind(key);
356 
357  if(iter == mHash.constEnd())
358  return false;
359 
360  return iter.value()->isObject();
361 }
362 
363 bool QMJsonObject::toBool(const QString &key) const
364 {
365  return this->value(key)->toBool();
366 }
367 
368 double QMJsonObject::toDouble(const QString &key) const
369 {
370  return this->value(key)->toDouble();
371 }
372 
373 QString QMJsonObject::toString(const QString &key) const
374 {
375  return this->value(key)->toString();
376 }
377 
378 const QMPointer<QMJsonArray> &QMJsonObject::toArray(const QString &key) const
379 {
380  return this->value(key)->toArray();
381 }
382 
383 const QMPointer<QMJsonObject> &QMJsonObject::toObject(const QString &key) const
384 {
385  return this->value(key)->toObject();
386 }
387 
388 bool QMJsonObject::toBool(const QString &key, bool defaultValue) const
389 {
390  return this->value(key)->toBool(defaultValue);
391 }
392 
393 double QMJsonObject::toDouble(const QString &key, double defaultValue) const
394 {
395  return this->value(key)->toDouble(defaultValue);
396 }
397 
398 const QString &QMJsonObject::toString(const QString &key, const QString &defaultValue) const
399 {
400  return this->value(key)->toString(defaultValue);
401 }
402 
403 const QMPointer<QMJsonArray> &QMJsonObject::toArray(const QString &key, const QMPointer<QMJsonArray> &defaultValue) const
404 {
405  return this->value(key)->toArray(defaultValue);
406 }
407 
408 const QMPointer<QMJsonObject> &QMJsonObject::toObject(const QString &key, const QMPointer<QMJsonObject> &defaultValue) const
409 {
410  return this->value(key)->toObject(defaultValue);
411 }
412 
413 bool QMJsonObject::fromBool(const QString &key, bool value)
414 {
415  return this->value(key)->fromBool(value);
416 }
417 
418 bool QMJsonObject::fromDouble(const QString &key, double value)
419 {
420  return this->value(key)->fromDouble(value);
421 }
422 
423 bool QMJsonObject::fromString(const QString &key, const QString &value)
424 {
425  return this->value(key)->fromString(value);
426 }
427 
428 bool QMJsonObject::fromArray(const QString &key, const QMPointer<QMJsonArray> &value)
429 {
430  return this->value(key)->fromArray(value);
431 }
432 
433 bool QMJsonObject::fromObject(const QString &key, const QMPointer<QMJsonObject> &value)
434 {
435  return this->value(key)->fromObject(value);
436 }
437 
438 bool QMJsonObject::from(const QString &key, const QMPointer<QMJsonValue> &value)
439 {
440  return this->value(key)->from(value);
441 }
442 
443 QDebug operator<<(QDebug dbg, const QMJsonObject &object)
444 {
445  QDebugStateSaver saver(dbg);
446  auto started = false;
447 
448  dbg.nospace() << "QMJsonObject{";
449 
450  for(const auto &key : object.keys())
451  {
452  if(started == true)
453  dbg << ",";
454 
455  dbg << key << ":" << object.value(key);
456  started = true;
457  }
458 
459  return dbg << "}";
460 }
461 
462 QDebug operator<<(QDebug dbg, const QMPointer<QMJsonObject> &value)
463 {
464  if(value.isNull() == true)
465  {
466  QDebugStateSaver saver(dbg);
467 
468  dbg.nospace() << "QMPointer<";
469  dbg << "QMJsonObject" << ">: NULL";
470 
471  return dbg;
472  }
473  else
474  {
475  return dbg << *value;
476  }
477 }
virtual int32_t size(void) const
virtual const QMPointer< QMJsonValue > & value(const QString &key) const
virtual QHash< QString, QMPointer< QMJsonValue > >::iterator begin(void)
virtual bool isDouble(const QString &key) const
virtual const QString key(const QMPointer< QMJsonValue > &value) const
void itemRemoved(const QString &key, const QMPointer< QMJsonValue > &value)
QMJsonReplacementPolicy
Definition: qmjsontype.h:56
virtual bool toBool(const QString &key) const
QMJsonArrayUnitePolicy
Definition: qmjsontype.h:75
virtual QList< QMPointer< QMJsonValue > > values(void) const
QDebug operator<<(QDebug dbg, const QMJsonObject &object)
virtual QHash< QString, QMPointer< QMJsonValue > >::const_iterator cbegin(void) const
virtual void reserve(int32_t alloc)
virtual bool fromDouble(const QString &key, double value)
virtual bool isNull(const QString &key) const
void itemAdded(const QString &key, const QMPointer< QMJsonValue > &value)
virtual const QMPointer< QMJsonObject > & toObject(const QString &key) const
virtual void remove(const QString &key)
virtual const QMPointer< QMJsonArray > & toArray(const QString &key) const
virtual QHash< QString, QMPointer< QMJsonValue > >::iterator find(const QString &key)
virtual QList< QString > keys(void) const
virtual bool isObject(const QString &key) const
virtual void insert(const QString &key, const QMPointer< QMJsonValue > &value, QMJsonReplacementPolicy policy=QMJsonReplacementPolicy_Replace)
virtual void clear(void)
virtual bool isArray(const QString &key) const
virtual bool fromArray(const QString &key, const QMPointer< QMJsonArray > &value)
virtual bool fromBool(const QString &key, bool value)
virtual bool isString(const QString &key) const
virtual bool isBool(const QString &key) const
virtual const QHash< QString, QMPointer< QMJsonValue > >::const_iterator cfind(const QString &key) const
virtual QString toString(const QString &key) const
virtual bool empty(void) const
virtual QMPointer< QMJsonValue > take(const QString &key)
virtual QHash< QString, QMPointer< QMJsonValue > > hash(void) const
virtual void squeeze(void)
virtual bool fromObject(const QString &key, const QMPointer< QMJsonObject > &value)
virtual int32_t count(void) const
virtual bool fromString(const QString &key, const QString &value)
virtual const QHash< QString, QMPointer< QMJsonValue > >::iterator erase(const QHash< QString, QMPointer< QMJsonValue > >::iterator &iter)
virtual bool contains(const QString &key) const
virtual bool from(const QString &key, const QMPointer< QMJsonValue > &value)
virtual bool isEmpty(void) const
virtual int32_t capacity(void) const
virtual QHash< QString, QMPointer< QMJsonValue > >::iterator end(void)
virtual double toDouble(const QString &key) const
virtual QHash< QString, QMPointer< QMJsonValue > >::const_iterator cend(void) const
virtual ~QMJsonObject()
virtual void unite(const QMPointer< QMJsonObject > &object, QMJsonReplacementPolicy replacementPolicy=QMJsonReplacementPolicy_Replace, QMJsonArrayUnitePolicy unitePolicy=QMJsonArrayUnitePolicy_Append)