-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsojson.cpp
344 lines (289 loc) · 8.85 KB
/
jsojson.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <string>
#include <vector>
#include <memory>
#include <iostream>
#include "jsojson.h"
namespace JsoJson {
struct value;
enum class Type {
NONE,
INT,
DOUBLE,
STRING,
BOOL,
NUL,
OBJECT,
ARRAY,
};
typedef std::pair<std::string, std::shared_ptr<value>> key_value;
typedef std::vector<key_value> object;
typedef std::vector<std::shared_ptr<value>> array;
struct value {
virtual Type type() const { return Type::NONE; }
virtual ~value() {};
};
struct valueInt : value {
long int v;
Type type() const override { return Type::INT; }
valueInt(const long int v) : v(v) {}
};
struct valueDouble : value {
double v;
Type type() const override { return Type::DOUBLE; }
valueDouble(const double v) : v(v) {}
};
struct valueString : value {
std::string v;
Type type() const override { return Type::STRING; }
valueString(const char* v) : v(v) {}
};
struct valueBool : value {
bool v;
Type type() const override { return Type::BOOL; }
valueBool(const bool v) : v(v) {}
};
struct valueNull : value {
Type type() const override { return Type::NUL; }
valueNull() {}
};
struct valueObject : value {
object v;
Type type() const override { return Type::OBJECT; }
valueObject() : v() {}
};
struct valueArray : value {
array v;
Type type() const override { return Type::ARRAY; }
valueArray() : v() {}
};
std::shared_ptr<value> CreatePremitiveValue(const JsoJsonPremitiveValue* pv) {
switch (pv->type) {
case JSO_JSON_INT: return std::make_shared<valueInt>(pv->v.i);
case JSO_JSON_DOUBLE: return std::make_shared<valueDouble>(pv->v.d);
case JSO_JSON_STRING: return std::make_shared<valueString>(pv->v.s);
case JSO_JSON_BOOL: return std::make_shared<valueBool>(pv->v.b ? true : false);
case JSO_JSON_NULL: return std::make_shared<valueNull>();
default: return nullptr;
}
}
template <class T> auto cast(const std::shared_ptr<value> v) -> std::shared_ptr<T> {
return std::dynamic_pointer_cast<T>(v);
}
struct FormatOptions {
const char* space_after_comma;
const char* space_key_value;
const char* delimiter;
unsigned int indent_spaces;
};
static void GetString(std::shared_ptr<value> v, FormatOptions* f, unsigned int indent_level = 0, bool enable_indent = true)
{
std::string indent = "";
for (int i = 0; i < f->indent_spaces * indent_level; ++i) {
indent += " ";
}
if (enable_indent) {
std::cout << indent;
}
std::string indent_oa = indent; // for each object or array element
for (int i = 0; i < f->indent_spaces; ++i) {
indent_oa += " ";
}
if (v->type() == Type::BOOL) {
const auto& b = cast<valueBool>(v);
std::cout << (b->v ? "true" : "false");
} else if (v->type() == Type::NUL) {
std::cout << "null";
} else if (v->type() == Type::INT) {
const auto& i = cast<valueInt>(v);
std::cout << i->v;
} else if (v->type() == Type::DOUBLE) {
const auto& d = cast<valueDouble>(v);
std::cout << d->v;
} else if (v->type() == Type::STRING) {
const auto& s = cast<valueString>(v);
std::cout << '"' << s->v << '"';
} else if (v->type() == Type::OBJECT) {
bool first = true;
const auto& o = cast<valueObject>(v);
std::cout << '{' << f->delimiter;
if (o->v.size() > 0) {
std::cout << indent_oa;
}
for (const auto& kv : o->v) {
if (!first) {
std::cout << "," << f->space_after_comma << f->delimiter;
std::cout << indent_oa;
}
first = false;
std::cout << "\"" << kv.first << "\":" << f->space_key_value;
GetString(kv.second, f, indent_level + 1, false);
}
if (o->v.size() > 0) {
std::cout << f->delimiter << indent << '}';
} else {
std::cout << indent << '}';
}
if (indent_level == 0) {
std::cout << f->delimiter;
}
} else if (v->type() == Type::ARRAY) {
bool first = true;
const auto& a = cast<valueArray>(v);
std::cout << '[' << f->delimiter;
if (a->v.size() > 0) {
std::cout << indent_oa;
}
for (const auto& e : a->v) {
if (!first) {
std::cout << "," << f->space_after_comma << f->delimiter;
std::cout << indent_oa;
}
first = false;
GetString(e, f, indent_level + 1, false);
}
if (a->v.size() > 0) {
std::cout << f->delimiter << indent << ']';
} else {
std::cout << indent << ']';
}
if (indent_level == 0) {
std::cout << f->delimiter;
}
}
}
} // namespace JsoJson
struct JsoJsonHandle {
std::shared_ptr<JsoJson::value> head;
std::vector<std::shared_ptr<JsoJson::value>> stack; // use std::stack?
JsoJsonHandle() : head(std::make_shared<JsoJson::value>()), stack(1, head) {}
};
struct JsoJsonHandle* JsoJsonCreate()
{
return new JsoJsonHandle();
}
void JsoJsonDestroy(struct JsoJsonHandle* h)
{
delete h;
}
JsoJsonBool JsoJsonAddValue(struct JsoJsonHandle* h, const struct JsoJsonPremitiveValue* pv)
{
JsoJsonBool ret = JSO_JSON_TRUE;
auto v = JsoJson::CreatePremitiveValue(pv);
auto& latest = h->stack.back();
if (latest->type() == JsoJson::Type::NONE) {
// JSON which consists of only one premitive value
h->head = latest = v;
} else if (latest->type() == JsoJson::Type::OBJECT) {
// push v to stack which is poped when JsoJsonAddKey called
h->stack.emplace_back(v);
} else if (latest->type() == JsoJson::Type::ARRAY) {
// push v to latest array value directry
auto array = JsoJson::cast<JsoJson::valueArray>(latest);
if (array) {
array->v.emplace_back(v);
}
} else {
ret = JSO_JSON_FALSE;
}
return ret;
}
JsoJsonBool JsoJsonAddKey(struct JsoJsonHandle* h, const char* k)
{
JsoJsonBool ret = JSO_JSON_TRUE;
auto v = h->stack.back(); h->stack.pop_back();
auto parent = h->stack.back();
if (parent->type() == JsoJson::Type::OBJECT) {
auto o = JsoJson::cast<JsoJson::valueObject>(parent);
if (o) {
o->v.emplace_back(std::make_pair(std::string(k), v));
}
} else {
ret = JSO_JSON_FALSE;
}
return ret;
}
JsoJsonBool JsoJsonAddKey2(struct JsoJsonHandle* h, const char* k)
{
std::string key(k);
std::string::size_type pos = key.find(':');
if (pos != std::string::npos) {
key = key.substr(0, pos);
}
pos = key.find(' ');
if (pos != std::string::npos) {
key = key.substr(0, pos);
}
return JsoJsonAddKey(h, key.c_str());
}
JsoJsonBool JsoJsonEnterArray(struct JsoJsonHandle* h)
{
JsoJsonBool ret = JSO_JSON_TRUE;
auto v = std::make_shared<JsoJson::valueArray>();
auto& latest = h->stack.back();
if (latest->type() == JsoJson::Type::NONE) {
h->head = latest = v;
} else if (latest->type() == JsoJson::Type::OBJECT) {
h->stack.emplace_back(v);
} else if (latest->type() == JsoJson::Type::ARRAY) {
auto a = JsoJson::cast<JsoJson::valueArray>(h->stack.back());
h->stack.emplace_back(v);
if (a) {
a->v.emplace_back(v);
}
} else {
ret = JSO_JSON_FALSE;
}
return ret;
}
JsoJsonBool JsoJsonLeaveArray(struct JsoJsonHandle* h)
{
int l = h->stack.size();
if (l > 2 && h->stack[l-2]->type() != JsoJson::Type::OBJECT) {
h->stack.pop_back();
}
return JSO_JSON_TRUE;
}
JsoJsonBool JsoJsonEnterObject(struct JsoJsonHandle* h)
{
auto v = std::make_shared<JsoJson::valueObject>();
auto& latest = h->stack.back();
if (latest->type() == JsoJson::Type::NONE) {
h->head = latest = v;
} else if (latest->type() == JsoJson::Type::OBJECT) {
h->stack.emplace_back(v);
} else if (latest->type() == JsoJson::Type::ARRAY) {
auto a = JsoJson::cast<JsoJson::valueArray>(h->stack.back());
h->stack.emplace_back(v);
if (a) {
a->v.emplace_back(v);
}
}
return JSO_JSON_TRUE;
}
JsoJsonBool JsoJsonLeaveObject(struct JsoJsonHandle* h)
{
int l = h->stack.size();
if (l > 2 && h->stack[l-2]->type() != JsoJson::Type::OBJECT) {
h->stack.pop_back();
}
return JSO_JSON_TRUE;
}
const char * JsoJsonGetJsonString(struct JsoJsonHandle* h, int pretty_format)
{
// TODO
JsoJson::FormatOptions f;
if (pretty_format) {
f.space_after_comma = "";
f.space_key_value = " ";
f.delimiter = "\n";
f.indent_spaces = 2;
} else {
f.space_after_comma = " ";
f.space_key_value = " ";
f.delimiter = "";
f.indent_spaces = 0;
}
JsoJson::GetString(h->head, &f, 0);
std::cout << std::endl;
return "";
}