qmjsontype_qstring.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_qstring.h>
27 
28 // ============================================================================
29 // QMJsonType<QString> Implementation
30 // ============================================================================
31 
32 template <>
33 QMPointer<QMJsonValue> QM_JSON_EXPORT QMJsonType<QString>::fromJson(const QString &json, int32_t &index)
34 {
35  auto result = QString();
36 
37  index++;
38 
39  while(1)
40  {
41  QMJsonValue::verifyIndex(json, index);
42 
43  switch(json.at(index).toLatin1())
44  {
45  case '\\':
46  {
47  index++;
48  QMJsonValue::verifyIndex(json, index);
49 
50  switch(json.at(index).toLatin1())
51  {
52  case '"': result += '"'; break;
53  case '\\': result += '\\'; break;
54  case '/': result += '/'; break;
55  case 'b': result += '\b'; break;
56  case 'f': result += '\f'; break;
57  case 'n': result += '\n'; break;
58  case 'r': result += '\r'; break;
59  case 't': result += '\t'; break;
60 
61  // TODO: Need to add support for \u [number]
62 
63  default:
64  break;
65  };
66 
67  index++;
68  break;
69  }
70 
71  case '"':
72  index++;
73  return QMPointer<QMJsonValue>(new QMJsonValue(result));
74 
75  default:
76  result += json.at(index++);
77  break;
78  };
79  }
80 
81  return QMPointer<QMJsonValue>(new QMJsonValue());
82 }
83 
84 template <>
86 {
87  (void)tab;
88  (void)sort;
89 
90  auto result = QString();
91  const auto &str = this->get();
92 
93  result += '"';
94  for(int i = 0; i < str.length(); i++)
95  {
96  auto c = str.at(i).toLatin1();
97 
98  switch(c)
99  {
100  case '\\':
101  result += "\\\\";
102  break;
103  case '"':
104  result += "\\\"";
105  break;
106 
107  case '/':
108  result += "\\/";
109  break;
110 
111  case '\b':
112  result += "\\b";
113  break;
114 
115  case '\f':
116  result += "\\f";
117  break;
118 
119  case '\n':
120  result += "\\n";
121  break;
122 
123  case '\r':
124  result += "\\r";
125  break;
126 
127  case '\t':
128  result += "\\t";
129  break;
130 
131  default:
132  result += c;
133  break;
134  }
135 
136  }
137  result += '"';
138 
139  return result;
140 }
141 
142 template <>
144 {
145  return true;
146 }
virtual QString toJson(int32_t tab, QMJsonSort sort)
Definition: qmjsontype.h:189
#define QM_JSON_EXPORT
Definition: qmjsontype.h:36
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