qmjsonarray.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 <qmjsonarray.h>
27 #include <qmjsonobject.h>
28 
29 // ============================================================================
30 // QMJsonArray Implementation
31 // ============================================================================
32 
34 {
35 }
36 
37 QMJsonArray::QMJsonArray(const QList<QMPointer<QMJsonValue> > &list)
38 {
39  mList = list;
40 }
41 
43 {
44 }
45 
46 void QMJsonArray::reserve(int32_t alloc)
47 {
48  mList.reserve(alloc);
49 }
50 
52 {
53  for(const auto &value : mList)
54  {
55  if(value->isObject() == true)
56  {
57  value->toObject()->clear();
58  continue;
59  }
60 
61  if(value->isArray() == true)
62  {
63  value->toArray()->clear();
64  continue;
65  }
66  }
67 
68  while(mList.count() > 0)
69  this->removeFirst();
70 }
71 
72 int32_t QMJsonArray::count(void) const
73 {
74  return mList.count();
75 }
76 
77 int32_t QMJsonArray::length(void) const
78 {
79  return mList.length();
80 }
81 
82 int32_t QMJsonArray::size(void) const
83 {
84  return mList.size();
85 }
86 
87 bool QMJsonArray::isEmpty(void) const
88 {
89  return mList.isEmpty();
90 }
91 
92 bool QMJsonArray::empty(void) const
93 {
94  return mList.empty();
95 }
96 
97 bool QMJsonArray::contains(const QMPointer<QMJsonValue> &value) const
98 {
99  return mList.contains(value);
100 }
101 
102 int32_t QMJsonArray::indexOf(const QMPointer<QMJsonValue> &value) const
103 {
104  return mList.indexOf(value);
105 }
106 
107 int32_t QMJsonArray::lastIndexOf(const QMPointer<QMJsonValue> &value, int32_t from) const
108 {
109  return mList.lastIndexOf(value, from);
110 }
111 
112 bool QMJsonArray::endsWith(const QMPointer<QMJsonValue> &value) const
113 {
114  return mList.endsWith(value);
115 }
116 
117 bool QMJsonArray::startsWith(const QMPointer<QMJsonValue> &value) const
118 {
119  return mList.startsWith(value);
120 }
121 
122 void QMJsonArray::prepend(const QMPointer<QMJsonValue> &value)
123 {
124  this->insert(0, value);
125 }
126 
127 void QMJsonArray::append(const QMPointer<QMJsonValue> &value)
128 {
129  this->insert(mList.count(), value);
130 }
131 
132 void QMJsonArray::insert(int32_t index, const QMPointer<QMJsonValue> &value)
133 {
134  if(index < 0) index = 0;
135  if(index > mList.count()) index = mList.count();
136 
137  if(value.isNull() == true)
138  {
139  auto newValue = QMPointer<QMJsonValue>(new QMJsonValue);
140 
141  mList.insert(index, newValue);
142  emit itemAdded(index, newValue);
143  }
144  else
145  {
146  mList.insert(index, value);
147  emit itemAdded(index, value);
148  }
149 }
150 
151 void QMJsonArray::unite(const QMPointer<QMJsonArray> &array, QMJsonArrayUnitePolicy policy)
152 {
153  if(array.isNull() == true)
154  return;
155 
156  for(const auto &value : array->values())
157  {
158  switch(policy)
159  {
161  this->prepend(value);
162  break;
163 
165  this->append(value);
166  break;
167  }
168  }
169 }
170 
171 void QMJsonArray::removeAll(const QMPointer<QMJsonValue> &value)
172 {
173  while(mList.contains(value) == true)
174  this->removeAt(mList.indexOf(value));
175 }
176 
177 void QMJsonArray::removeOne(const QMPointer<QMJsonValue> &value)
178 {
179  this->removeAt(mList.indexOf(value));
180 }
181 
182 void QMJsonArray::removeAt(int32_t index)
183 {
184  if(index < 0 || index >= mList.count())
185  return;
186 
187  emit itemRemoved(index, mList.takeAt(index));
188 }
189 
191 {
192  this->removeAt(0);
193 }
194 
196 {
197  this->removeAt(mList.count() - 1);
198 }
199 
200 QMPointer<QMJsonValue> QMJsonArray::takeFirst(void)
201 {
202  return this->takeAt(0);
203 }
204 
205 QMPointer<QMJsonValue> QMJsonArray::takeLast(void)
206 {
207  return this->takeAt(mList.count() - 1);
208 }
209 
210 QMPointer<QMJsonValue> QMJsonArray::takeAt(int32_t index)
211 {
212  if(index < 0 || index >= mList.count())
213  return QMPointer<QMJsonValue>(new QMJsonValue);
214 
215  auto value = mList.takeAt(index);
216 
217  emit itemRemoved(index, value);
218  return value;
219 }
220 
221 QMPointer<QMJsonValue> QMJsonArray::takeAt(int32_t index, const QMPointer<QMJsonValue> &defaultValue)
222 {
223  if(index < 0 || index >= mList.count())
224  return defaultValue;
225 
226  auto value = mList.takeAt(index);
227 
228  emit itemRemoved(index, value);
229  return value;
230 }
231 
232 const QMPointer<QMJsonValue> &QMJsonArray::front(void) const
233 {
234  static auto defaultValue = QMPointer<QMJsonValue>(new QMJsonValue);
235 
236  if(mList.isEmpty() == true)
237  return defaultValue;
238 
239  return mList.front();
240 }
241 
242 const QMPointer<QMJsonValue> &QMJsonArray::back(void) const
243 {
244  static auto defaultValue = QMPointer<QMJsonValue>(new QMJsonValue);
245 
246  if(mList.isEmpty() == true)
247  return defaultValue;
248 
249  return mList.back();
250 }
251 
252 const QMPointer<QMJsonValue> &QMJsonArray::value(int32_t index) const
253 {
254  static auto defaultValue = QMPointer<QMJsonValue>(new QMJsonValue);
255 
256  if(index < 0 || index >= mList.count())
257  return defaultValue;
258 
259  return mList.at(index);
260 }
261 
262 const QMPointer<QMJsonValue> &QMJsonArray::value(int32_t index, const QMPointer<QMJsonValue> &defaultValue) const
263 {
264  if(index < 0 || index >= mList.count())
265  return defaultValue;
266 
267  return mList.at(index);
268 }
269 
270 QList<QMPointer<QMJsonValue> > QMJsonArray::values(void) const
271 {
272  return mList;
273 }
274 
275 QList<QMPointer<QMJsonValue> > QMJsonArray::mid(int32_t pos, int32_t length) const
276 {
277  if(mList.count() == 0)
278  return QList<QMPointer<QMJsonValue> >();
279 
280  if(pos >= mList.count())
281  return QList<QMPointer<QMJsonValue> >();
282 
283  if(pos < 0) pos = 0;
284 
285  return mList.mid(pos, length);
286 }
287 
288 void QMJsonArray::move(int32_t from, int32_t to)
289 {
290  if(mList.count() == 0)
291  return;
292 
293  if(to < 0 || to >= mList.count()) return;
294  if(from < 0 || from >= mList.count()) return;
295  if(to == from) return;
296 
297  mList.move(from, to);
298 }
299 
300 void QMJsonArray::replace(int32_t index, const QMPointer<QMJsonValue> &value)
301 {
302  if(index < 0)
303  return;
304 
305  if(index >= mList.count())
306  return;
307 
308  auto removedValue = mList.at(index);
309 
310  if(value.isNull() == true)
311  mList.replace(index, QMPointer<QMJsonValue>(new QMJsonValue));
312  else
313  mList.replace(index, value);
314 
315  emit itemRemoved(index, removedValue);
316  emit itemAdded(index, value);
317 }
318 
319 bool QMJsonArray::isNull(int32_t index) const
320 {
321  if(index < 0 || index >= mList.count())
322  return false;
323 
324  return mList.at(index)->isNull();
325 }
326 
327 bool QMJsonArray::isBool(int32_t index) const
328 {
329  if(index < 0 || index >= mList.count())
330  return false;
331 
332  return mList.at(index)->isBool();
333 }
334 
335 bool QMJsonArray::isDouble(int32_t index) const
336 {
337  if(index < 0 || index >= mList.count())
338  return false;
339 
340  return mList.at(index)->isDouble();
341 }
342 
343 bool QMJsonArray::isString(int32_t index) const
344 {
345  if(index < 0 || index >= mList.count())
346  return false;
347 
348  return mList.at(index)->isString();
349 }
350 
351 bool QMJsonArray::isArray(int32_t index) const
352 {
353  if(index < 0 || index >= mList.count())
354  return false;
355 
356  return mList.at(index)->isArray();
357 }
358 
359 bool QMJsonArray::isObject(int32_t index) const
360 {
361  if(index < 0 || index >= mList.count())
362  return false;
363 
364  return mList.at(index)->isObject();
365 }
366 
367 bool QMJsonArray::toBool(int32_t index) const
368 {
369  return this->value(index)->toBool();
370 }
371 
372 double QMJsonArray::toDouble(int32_t index) const
373 {
374  return this->value(index)->toDouble();
375 }
376 
377 QString QMJsonArray::toString(int32_t index) const
378 {
379  return this->value(index)->toString();
380 }
381 
382 const QMPointer<QMJsonArray> &QMJsonArray::toArray(int32_t index) const
383 {
384  return this->value(index)->toArray();
385 }
386 
387 const QMPointer<QMJsonObject> &QMJsonArray::toObject(int32_t index) const
388 {
389  return this->value(index)->toObject();
390 }
391 
392 bool QMJsonArray::toBool(int32_t index, bool defaultValue) const
393 {
394  return this->value(index)->toBool(defaultValue);
395 }
396 
397 double QMJsonArray::toDouble(int32_t index, double defaultValue) const
398 {
399  return this->value(index)->toDouble(defaultValue);
400 }
401 
402 const QString &QMJsonArray::toString(int32_t index, const QString &defaultValue) const
403 {
404  return this->value(index)->toString(defaultValue);
405 }
406 
407 const QMPointer<QMJsonArray> &QMJsonArray::toArray(int32_t index, const QMPointer<QMJsonArray> &defaultValue) const
408 {
409  return this->value(index)->toArray(defaultValue);
410 }
411 
412 const QMPointer<QMJsonObject> &QMJsonArray::toObject(int32_t index, const QMPointer<QMJsonObject> &defaultValue) const
413 {
414  return this->value(index)->toObject(defaultValue);
415 }
416 
417 bool QMJsonArray::fromBool(int32_t index, bool value)
418 {
419  return this->value(index)->fromBool(value);
420 }
421 
422 bool QMJsonArray::fromDouble(int32_t index, double value)
423 {
424  return this->value(index)->fromDouble(value);
425 }
426 
427 bool QMJsonArray::fromString(int32_t index, const QString &value)
428 {
429  return this->value(index)->fromString(value);
430 }
431 
432 bool QMJsonArray::fromArray(int32_t index, const QMPointer<QMJsonArray> &value)
433 {
434  return this->value(index)->fromArray(value);
435 }
436 
437 bool QMJsonArray::fromObject(int32_t index, const QMPointer<QMJsonObject> &value)
438 {
439  return this->value(index)->fromObject(value);
440 }
441 
442 bool QMJsonArray::from(int32_t index, const QMPointer<QMJsonValue> &value)
443 {
444  return this->value(index)->from(value);
445 }
446 
447 QDebug operator<<(QDebug dbg, const QMJsonArray &array)
448 {
449  QDebugStateSaver saver(dbg);
450  auto started = false;
451 
452  dbg.nospace() << "QMJsonArray[";
453 
454  for(const auto &value : array.values())
455  {
456  if(started == true)
457  dbg << ",";
458 
459  dbg << value;
460  started = true;
461  }
462 
463  return dbg << "]";
464 }
465 
466 QDebug operator<<(QDebug dbg, const QMPointer<QMJsonArray> &value)
467 {
468  if(value.isNull() == true)
469  {
470  QDebugStateSaver saver(dbg);
471 
472  dbg.nospace() << "QMPointer<";
473  dbg << "QMJsonArray" << ">: NULL";
474 
475  return dbg;
476  }
477  else
478  {
479  return dbg << *value;
480  }
481 }
virtual void append(const QMPointer< QMJsonValue > &value)
virtual bool isObject(int32_t index) const
virtual QList< QMPointer< QMJsonValue > > mid(int32_t pos, int32_t length=-1) const
virtual void clear(void)
Definition: qmjsonarray.cpp:51
virtual double toDouble(int32_t index) const
virtual QMPointer< QMJsonValue > takeAt(int32_t index)
virtual int32_t length(void) const
Definition: qmjsonarray.cpp:77
virtual bool startsWith(const QMPointer< QMJsonValue > &value) const
virtual QMPointer< QMJsonValue > takeLast(void)
virtual void replace(int32_t index, const QMPointer< QMJsonValue > &value)
virtual void removeOne(const QMPointer< QMJsonValue > &value)
void itemRemoved(int32_t index, const QMPointer< QMJsonValue > &value)
QMJsonArrayUnitePolicy
Definition: qmjsontype.h:75
virtual int32_t size(void) const
Definition: qmjsonarray.cpp:82
virtual QString toString(int32_t index) const
virtual bool contains(const QMPointer< QMJsonValue > &value) const
Definition: qmjsonarray.cpp:97
virtual void prepend(const QMPointer< QMJsonValue > &value)
virtual void move(int32_t from, int32_t to)
QDebug operator<<(QDebug dbg, const QMJsonArray &array)
virtual void reserve(int32_t alloc)
Definition: qmjsonarray.cpp:46
virtual bool isEmpty(void) const
Definition: qmjsonarray.cpp:87
virtual bool endsWith(const QMPointer< QMJsonValue > &value) const
virtual bool toBool(int32_t index) const
virtual bool fromBool(int32_t index, bool value)
virtual const QMPointer< QMJsonValue > & value(int32_t index) const
virtual const QMPointer< QMJsonValue > & front(void) const
virtual bool from(int32_t index, const QMPointer< QMJsonValue > &value)
virtual bool fromArray(int32_t index, const QMPointer< QMJsonArray > &value)
virtual QMPointer< QMJsonValue > takeFirst(void)
virtual bool isArray(int32_t index) const
virtual QList< QMPointer< QMJsonValue > > values(void) const
virtual bool fromDouble(int32_t index, double value)
virtual void insert(int32_t index, const QMPointer< QMJsonValue > &value)
virtual void removeLast(void)
virtual void removeFirst(void)
virtual bool fromString(int32_t index, const QString &value)
virtual int32_t count(void) const
Definition: qmjsonarray.cpp:72
virtual int32_t lastIndexOf(const QMPointer< QMJsonValue > &value, int32_t from=-1) const
virtual void removeAt(int32_t index)
virtual ~QMJsonArray()
Definition: qmjsonarray.cpp:42
virtual bool isString(int32_t index) const
virtual int32_t indexOf(const QMPointer< QMJsonValue > &value) const
virtual bool isNull(int32_t index) const
void itemAdded(int32_t index, const QMPointer< QMJsonValue > &value)
virtual const QMPointer< QMJsonArray > & toArray(int32_t index) const
virtual bool empty(void) const
Definition: qmjsonarray.cpp:92
virtual void unite(const QMPointer< QMJsonArray > &array, QMJsonArrayUnitePolicy policy=QMJsonArrayUnitePolicy_Append)
virtual const QMPointer< QMJsonValue > & back(void) const
virtual bool fromObject(int32_t index, const QMPointer< QMJsonObject > &value)
virtual bool isDouble(int32_t index) const
virtual bool isBool(int32_t index) const
virtual void removeAll(const QMPointer< QMJsonValue > &value)
virtual const QMPointer< QMJsonObject > & toObject(int32_t index) const