-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonSerialaizer.cpp
63 lines (52 loc) · 1.24 KB
/
JsonSerialaizer.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
#include <iostream>
#include <string>
class Serializer {
public:
Serializer() = default;
virtual void BeginArray() = 0;
virtual void AddArrayItem(const std::string &element) = 0;
virtual void EndArray() = 0;
virtual ~Serializer() {
}
};
class JsonSerializer : public Serializer {
public:
size_t bracket_counter;
size_t element_counter;
bool afterbracket = false;
std::string ans;
void BeginArray() override {
if (element_counter > 0 | afterbracket) {
ans += ",";
}
ans += "[";
bracket_counter++;
element_counter = 0;
afterbracket = false;
}
void AddArrayItem(const std::string &element) override {
if (element_counter > 0 || afterbracket) {
ans += ",";
}
ans = ans + "\"" + element + "\"";
element_counter++;
}
void EndArray() override {
bracket_counter -= 1;
ans += "]";
if (bracket_counter == 0) {
std::cout << ans;
ans = "";
} else {
afterbracket = true;
}
}
};
int main() {
Serializer *k = new JsonSerializer();
k->BeginArray();
k->EndArray();
k->BeginArray();
k->EndArray();
return 0;
}