-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonLite.cpp
247 lines (207 loc) · 5.57 KB
/
JsonLite.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include <exception>
#include <string>
#include "JsonLite.h"
using namespace JsonLite;
static const char hexMap[] = "0123456789abcdef";
#define HEX_LEN 4
#pragma region PrivateMethods
JsonLiteSerializer::JsonLiteSerializer() : objectList(), buffer()
{
this->root = new JsonElement("", JsonElement::Type::JsonObject, nullptr);
objectList.push_back(this->root);
}
JsonLiteSerializer::~JsonLiteSerializer()
{
for (JsonElement* elem : objectList)
{
delete elem;
}
}
void JsonLiteSerializer::AddTabs(int depth)
{
for (int i = 0; i < depth; ++i)
{
this->buffer += " ";
}
}
JsonElement* JsonLiteSerializer::AddElement(JsonElement * parent, const std::string & name, JsonElement::Type type)
{
if (parent == nullptr)
{
throw std::runtime_error("Invalid parent JSON element.");
}
JsonElement* newElem = new JsonElement(name, type, parent);
parent->children.push_back(newElem);
this->objectList.push_back(newElem);
return newElem;
}
template<class T, typename V>
JsonElement* JsonLiteSerializer::AddValue(JsonElement * parent, const std::string & name, const V& value, JsonElement::Type type)
{
if (parent == nullptr)
{
throw std::runtime_error("Invalid parent JSON element.");
}
if (parent->type == JsonElement::Type::JsonObject)
{
if (name == "")
{
throw std::runtime_error("String value must contain a name.");
}
}
else if (parent->type == JsonElement::Type::JsonArray)
{
if (name != "")
{
throw std::runtime_error("String value must not contain a name.");
}
}
else
{
throw std::runtime_error("Cannot add a string value to this element.");
}
JsonElement* newElem = new JsonValue<T>(name, value, type, parent);
parent->children.push_back(newElem);
this->objectList.push_back(newElem);
return newElem;
}
// Escape all json control characters
void JsonLiteSerializer::SanitizeInput(const std::string& input)
{
for (auto c = input.cbegin(); c != input.cend(); ++c)
{
char ch = *c;
// Encode special control characters in hex
if (ch == '"' || ch == '\\' || ('\x00' <= ch && ch <= '\x1f'))
{
this->buffer += "\\u";
for (int i = 0, j = (HEX_LEN - 1) * 4; i < HEX_LEN; ++i, j -= 4)
{
this->buffer += hexMap[(ch >> j) & 0x0f];
}
}
else {
this->buffer += ch;
}
}
}
std::string JsonLiteSerializer::ToString(bool prettyPrint)
{
this->buffer = "";
ToString(root, 0, prettyPrint);
return this->buffer;
}
#pragma endregion
#pragma region PublicMethods
JsonElement* JsonLiteSerializer::AddArray(JsonElement* parent, const std::string& name)
{
return this->AddElement(parent, name, JsonElement::Type::JsonArray);
}
JsonElement* JsonLiteSerializer::AddObject(JsonElement* parent, const std::string& name)
{
return this->AddElement(parent, name, JsonElement::Type::JsonObject);
}
void JsonLiteSerializer::AddBoolean(JsonElement* parent, const std::string& name, bool value)
{
this->AddValue<bool>(parent, name, value, JsonElement::Type::JsonBool);
}
void JsonLiteSerializer::AddFloat(JsonElement* parent, const std::string& name, float value)
{
this->AddValue<float>(parent, name, value, JsonElement::Type::JsonFloat);
}
void JsonLiteSerializer::AddInteger(JsonElement* parent, const std::string& name, int value)
{
this->AddValue<int>(parent, name, value, JsonElement::Type::JsonInt);
}
void JsonLiteSerializer::AddNull(JsonElement* parent, const std::string& name)
{
this->AddElement(parent, name, JsonElement::Type::JsonNull);
}
void JsonLiteSerializer::AddString(JsonElement* parent, const std::string& name, const std::string& value)
{
this->AddValue<std::string>(parent, name, value, JsonElement::Type::JsonString);
}
void JsonLiteSerializer::Clear()
{
for (JsonElement* elem : objectList)
{
delete elem;
}
objectList.clear();
this->root = new JsonElement("", JsonElement::Type::JsonObject, nullptr);
objectList.push_back(this->root);
}
JsonElement* JsonLiteSerializer::GetParent(JsonElement* elem)
{
return elem->parent;
}
JsonElement* JsonLiteSerializer::GetRoot()
{
return this->root;
}
void JsonLiteSerializer::ToString(JsonElement* elem, int depth, bool prettyPrint)
{
if (prettyPrint)
AddTabs(depth);
// Add element name, if it exists
if (elem->name != "")
{
this->buffer += "\"";
SanitizeInput(elem->name);
this->buffer += "\": ";
}
switch (elem->type)
{
case JsonElement::Type::JsonArray:
this->buffer += '[';
if (prettyPrint) this->buffer += "\n";
break;
case JsonElement::Type::JsonObject:
this->buffer += '{';
if (prettyPrint) this->buffer += "\n";
break;
case JsonElement::Type::JsonString:
this->buffer += "\"";
SanitizeInput(((JsonValue<std::string>*)elem)->value);
this->buffer += "\"";
break;
case JsonElement::Type::JsonInt:
this->buffer += std::to_string(((JsonValue<int>*)elem)->value);
break;
case JsonElement::Type::JsonFloat:
this->buffer += std::to_string(((JsonValue<float>*)elem)->value);
break;
case JsonElement::Type::JsonBool:
this->buffer += ((JsonValue<bool>*)elem)->value == true ? "true" : "false";
break;
case JsonElement::Type::JsonNull:
this->buffer += "null";
break;
default:
throw std::runtime_error("Type not implemented.");
}
// Recurse on all nested elements
for (JsonElement* child : elem->children)
{
ToString(child, depth + 1, prettyPrint);
if (child != elem->children.back())
{
this->buffer += ",";
}
if (prettyPrint)
this->buffer += "\n";
}
// Add closing grouping chars if neccessary
switch (elem->type)
{
case JsonElement::Type::JsonArray:
if (prettyPrint) AddTabs(depth);
this->buffer += ']';
break;
case JsonElement::Type::JsonObject:
if (prettyPrint) AddTabs(depth);
this->buffer += '}';
break;
}
}
#pragma endregion