qmjsontype_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 <qmjsontype_qmjsonarray.h>
27 
28 // ============================================================================
29 // QMJsonType<QMPointer<QMJsonArray> > Implementation
30 // ============================================================================
31 
32 template <>
33 QMPointer<QMJsonValue> QM_JSON_EXPORT QMJsonType<QMPointer<QMJsonArray> >::fromJson(const QString &json, int32_t &index)
34 {
35  auto array = QMPointer<QMJsonArray>(new QMJsonArray);
36 
37  index++;
38 
39  do
40  {
41  QMJsonValue::skipSpaces(json, index);
42  QMJsonValue::verifyIndex(json, index);
43 
44  switch(json.at(index).toLatin1())
45  {
46  case ']':
47  index++;
48  return QMPointer<QMJsonValue>(new QMJsonValue(array));
49 
50  case ',':
51  index++;
52  continue;
53 
54  default:
55  array->append(QMJsonValue::fromJson(json, index));
56  }
57  }
58  while(1);
59 
60  return QMPointer<QMJsonValue>(new QMJsonValue());
61 }
62 
63 template <>
65 {
66  auto json = QString();
67  const auto &array = this->get();
68 
69  if(array.isNull() == true)
70  return "[]";
71 
72  if(array->count() == 0)
73  return "[]";
74 
75  if(tab == (int32_t)QMJsonFormat_Optimized)
76  {
77  json += '[';
78 
79  for(const auto &value : array->values())
80  {
81  json += value->toJson((QMJsonFormat)tab, sort);
82  json += ',';
83  }
84 
85  return json.replace(json.length() - 1, 1, ']');
86  }
87  else
88  {
89  json += '[';
90 
91  tab += 4;
92  auto space = QString(tab, ' ');
93  for(const auto &value : array->values())
94  {
95  json += "\r\n";
96  json += space;
97  json += value->toJson((QMJsonFormat)tab, sort);
98  json += ',';
99  }
100  tab -= 4;
101 
102  json.remove(json.length() - 1, 1);
103  json += "\r\n";
104  json += QString(tab, ' ');
105  json += ']';
106 
107  return json;
108  }
109 }
110 
111 template <>
113 {
114  return true;
115 }
virtual QString toJson(int32_t tab, QMJsonSort sort)
Definition: qmjsontype.h:189
static QMPointer< QMJsonValue > fromJson(const QString &json)
#define QM_JSON_EXPORT
Definition: qmjsontype.h:36
QMJsonFormat
Definition: qmjsontype.h:62
static QMPointer< QMJsonValue > fromJson(const QString &json, int32_t &index)
Definition: qmjsontype.h:198
virtual bool isBaseType(void)
Definition: qmjsontype.h:213
QMJsonSort
Definition: qmjsontype.h:68