-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJSONhelpers.cpp
112 lines (105 loc) · 3.26 KB
/
JSONhelpers.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
/*
* JSON - JavaScript Object Notation
* My compiler will output logging in that format, which is easy to read and
* there is a lot of software for analyzing it.
*/
std::string JSONifyString(const std::string &str) {
std::string ret = "\"";
for (size_t i = 0; i < str.length(); i++)
if (str[i] == '\n')
ret += "\\n";
else if (str[i] == '\"')
ret += "\\\"";
else if (str[i] == '\'')
ret += "\\'";
else if (str[i] == '\t')
ret += "\\t";
else if (str[i] == '\\')
ret += "\\\\";
else
ret += str[i];
ret += "\"";
return ret;
}
std::string JSONifyVectorOfStrings(const std::vector<std::string> &vector) {
if (vector.empty())
return "[]";
std::string ret = "[";
for (size_t i = 0; i < vector.size(); i++)
if (i == vector.size() - 1)
ret += JSONifyString(vector[i]) + "]";
else
ret += JSONifyString(vector[i]) + ",";
return ret;
}
std::string JSONifyVectorOfDoubles(const std::vector<double> &vector) {
if (vector.empty())
return "[]";
std::string ret = "[";
for (size_t i = 0; i < vector.size(); i++)
if (i == vector.size() - 1)
ret += std::to_string(vector[i]) + "]";
else
ret += std::to_string(vector[i]) + ",";
return ret;
}
std::string JSONifyMapOfStrings(const std::map<std::string, std::string> &map) {
std::vector<std::pair<std::string, std::string>> vector(map.size());
std::copy(map.begin(), map.end(), vector.begin());
std::string ret = "[\n";
for (size_t i = 0; i < vector.size(); i++) {
ret += "{\n\"first\":" + JSONifyString(vector[i].first) +
",\n\"second\":" + JSONifyString(vector[i].second) + "\n}";
if (i != vector.size() - 1)
ret += ",\n";
else
ret += "\n";
}
ret += "]";
return ret;
}
std::string JSONifyMapOfUnsigneds(const std::map<std::string, unsigned> &map) {
std::vector<std::pair<std::string, unsigned>> vector(map.size());
std::copy(map.begin(), map.end(), vector.begin());
std::string ret = "[\n";
for (size_t i = 0; i < vector.size(); i++) {
ret += "{\n\"first\":" + JSONifyString(vector[i].first) +
",\n\"second\":" + std::to_string(vector[i].second) + "\n}";
if (i != vector.size() - 1)
ret += ",\n";
else
ret += "\n";
}
ret += "]";
return ret;
}
std::string JSONifyMapOfDoubles(const std::map<std::string, double> &map) {
std::vector<std::pair<std::string, double>> vector(map.size());
std::copy(map.begin(), map.end(), vector.begin());
std::string ret = "[\n";
for (size_t i = 0; i < vector.size(); i++) {
ret += "{\n\"first\":" + JSONifyString(vector[i].first) +
",\n\"second\":" + std::to_string(vector[i].second) + "\n}";
if (i != vector.size() - 1)
ret += ",\n";
else
ret += "\n";
}
ret += "]";
return ret;
}
std::string JSONifyMapOfInts(const std::map<std::string, int> &map) {
std::vector<std::pair<std::string, int>> vector(map.size());
std::copy(map.begin(), map.end(), vector.begin());
std::string ret = "[\n";
for (size_t i = 0; i < vector.size(); i++) {
ret += "{\n\"first\":" + JSONifyString(vector[i].first) +
",\n\"second\":" + std::to_string(vector[i].second) + "\n}";
if (i != vector.size() - 1)
ret += ",\n";
else
ret += "\n";
}
ret += "]";
return ret;
}