-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.cpp
293 lines (253 loc) · 10.6 KB
/
json.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
#include <functional>
#include <iostream>
#include <map>
#include <ostream>
#include <variant>
#include <pcc/pcc.hpp>
// Forward declate json structures
struct json_null;
struct json_boolean;
struct json_number;
struct json_string;
struct json_array;
struct json_object;
// Define json value type as a variant of possible json values
using json_value = std::variant<json_null, json_boolean, json_number,
json_string, json_array, json_object>;
struct json_null final {};
struct json_boolean final {
bool value;
};
struct json_number final {
double value;
};
struct json_string final {
std::string value;
};
struct json_object;
struct json_array final {
std::vector<json_value> value;
};
struct json_object final {
std::map<std::string, json_value> value;
};
// Deine output operators for json values just ot print results
std::ostream &operator<<(std::ostream &os, const json_null &) {
os << "null";
return os;
}
std::ostream &operator<<(std::ostream &os, const json_boolean &b) {
os << (b.value ? "true" : "false");
return os;
}
std::ostream &operator<<(std::ostream &os, const json_number &n) {
os << n.value;
return os;
}
std::ostream &operator<<(std::ostream &os, const json_string &s) {
os << "\"" << s.value << "\"";
return os;
}
std::ostream &operator<<(std::ostream &os, const json_value &v);
std::ostream &operator<<(std::ostream &os, const json_array &a) {
os << "[";
for (size_t i = 0; i < a.value.size(); ++i) {
os << a.value[i];
if (i != a.value.size() - 1) {
os << ", ";
}
}
os << "]";
return os;
}
std::ostream &operator<<(std::ostream &os, const json_object &o) {
os << "{";
size_t i = 0;
for (const auto &[k, v] : o.value) {
os << "\"" << k << "\": " << v;
if (i != o.value.size() - 1) {
os << ", ";
}
++i;
}
os << "}";
return os;
}
std::ostream &operator<<(std::ostream &os, const json_value &v) {
std::visit([&os](const auto &v) { os << v; }, v);
return os;
}
/// @brief Parse a string with escape characters
auto parse_string() {
return pcc::escaped(pcc::alphanumeric1(), '\\', pcc::one_of("\"n\\"));
}
/// @brief Parse boolean values
auto boolean() {
// constant is a parser that always returns the same value
// if the parser is successful. For example, constant(true, tag("true"))
// will return true if the input starts with "true" and false otherwise.
auto true_parser = pcc::constant(true, pcc::tag("true"));
auto false_parser = pcc::constant(false, pcc::tag("false"));
// alternative is a parser that tries the first parser and if it fails
// tries the second parser, then the third and so on. If all parsers fail
// the result is a failure. Parsers should have the same return type.
return pcc::alternative(std::move(true_parser), std::move(false_parser));
}
/// @brief Parse null values
auto null() { return pcc::constant(nullptr, pcc::tag("null")); }
/// @brief Parse a string
auto string() {
// tag is a parser that returns the input if it starts with the given string
// and fails otherwise. For example, tag("hello") will return "hello" if the
// input starts with "hello" and fail otherwise. Then operator << is used to
// apply next parser and return the result of the second parser. In this
// case the second parser is parse_string. Then operator >> is used to apply
// the third parser and return the result of the second parser. In this case
// the third parser is pcc::tag("\""). The result of whole expression is the
// string between quotes.
return pcc::tag("\"") << parse_string() >> pcc::tag("\"");
}
auto json() {
// map is a parser that applies a function to the result of the parser.
// So if the parser is successful the function is applied to the result,
// and transform the result to another type. For example, map([](auto n) ->
// hex { return to_hex(n); }, pcc::arithmetic<int>()) will return a hex
// number type if the parser is successful. Here we just transform the
// result to json_value for each parser.
auto null_parser =
pcc::map([](auto) -> json_value { return json_null{}; }, null());
auto boolean_parser = pcc::map(
[](auto b) -> json_value { return json_boolean{b}; }, boolean());
auto num_parser =
pcc::map([](auto n) -> json_value { return json_number{n}; },
pcc::arithmetic<double>());
auto string_parser = pcc::map(
[](const auto &s) -> json_value {
return json_string{std::string(s.begin(), s.end())};
},
string());
// Make a parser to parse json values. This parser is recursive because
// JSON values can be arrays or objects that contain other JSON values.
// It takes two parsers as arguments, one for parsing arrays and one for
// parsing objects. They are passed as arguments to the lambda because
// they are undefined at the time of the definition of the lambda.
auto json_value_parser = [null_parser, boolean_parser, num_parser,
string_parser](auto json_array_parser,
auto json_object_parser) -> auto {
// We again apply alternative to try to parse different types of JSON
// values. We also apply spaces0 to ignore spaces between values.
// We use lazy to allow recursive calls to the parser, otherwice we
// will have a stack overflow. So each recursive parser should be
// wrapped in a lambda and called with lazy.
auto element =
pcc::spaces0() << pcc::alternative(
pcc::lazy([json_object_parser] {
return json_object_parser(json_object_parser);
}),
pcc::lazy([json_array_parser, json_object_parser] {
return json_array_parser(json_array_parser,
json_object_parser);
}),
null_parser, boolean_parser, num_parser, string_parser) >>
pcc::spaces0();
return element;
};
// Make a parser to parse JSON arrays. This parser is recursive because
// JSON arrays can contain other JSON values. It takes two parsers as
// arguments, one for parsing arrays and one for parsing objects. They
// are passed as arguments to the lambda because they are undefined at
// the time of the definition of the lambda.
auto json_array_parser =
[null_parser, boolean_parser, num_parser, string_parser,
json_value_parser](auto self, auto json_object_parser)
-> pcc::parser<std::function<pcc::parse_result<json_value, char>(
std::string_view)>> {
// Element is a json value, json_array_parser and json_object_parser
// are forwarded to the json_value_parser. We use lazy to allow
// recursion.
auto element = pcc::lazy([json_value_parser, self, json_object_parser] {
return json_value_parser(self, json_object_parser);
});
// Elements is a list of elements separated by commas. We use
// separated_list to parse a list of elements separated by commas.
// It returns std::vector<T> where T is the type of the element parser.
auto elements = pcc::separated_list(element, pcc::tag(","));
// Array parser is a parser that parses elements between square
// brackets.
auto array_parser = pcc::tag("[") << elements >> pcc::tag("]");
// We map the result of the parser to a json_array.
return pcc::map(
[](const auto &v) -> json_value { return json_array{v}; },
array_parser);
};
// Make a parser to parse JSON objects. This parser is recursive again,
// same statements as above are valid here.
auto json_object_parser = [null_parser, boolean_parser, num_parser,
string_parser, json_array_parser,
json_value_parser](auto self)
-> pcc::parser<std::function<pcc::parse_result<json_value, char>(
std::string_view)>> {
// JSON objects are key-value pairs. Key is a string followed by a
// colon.
auto key =
pcc::spaces0() << string() >> pcc::spaces0() >> pcc::tag(":");
// Value is a JSON value. We use lazy to allow recursion.
auto value = pcc::lazy([json_value_parser, json_array_parser, self] {
return json_value_parser(json_array_parser, self);
});
// Element is a key-value pair. We use product to make a product type
// of applied parsers, it is a std::tuple<T1, T2> where T1 is the type
// of the first parser and T2 is the type of the second parser.
auto element =
pcc::product<std::tuple<std::string, json_value>>(key, value);
// Elements is a list of elements separated by commas.
auto elements = pcc::separated_list(element, pcc::tag(","));
// Object parser is a parser that parses elements between figure
// brackets.
auto object_parser = pcc::tag("{") << elements >> pcc::tag("}");
// We map the result of the parser to a json_object.
return pcc::map(
[](const auto &v) -> json_value {
std::map<std::string, json_value> value;
for (const auto &[k, v] : v) {
value[k] = v;
}
return json_object{std::move(value)};
},
object_parser);
};
// The root parser is either a JSON object, a JSON array or null.
return pcc::spaces0() << pcc::alternative(
pcc::lazy([json_object_parser] {
return json_object_parser(json_object_parser);
}),
pcc::lazy([json_array_parser, json_object_parser] {
return json_array_parser(json_array_parser,
json_object_parser);
}),
null_parser) >>
pcc::spaces0();
}
int main() {
// Create a JSON parser.
auto json_parser = json();
// JSON string to parse
constexpr char json_str[] = R"(
{
"key1": "value1",
"key2": 2,
"key3": [ 1, 2 , 3 ] ,
"key4": {
"key5": "value5" ,
"key6": 6
}
})";
// Parse the JSON string
auto result = json_parser(json_str);
// If the parser is successful print the result, otherwise print the error.
if (result.is_success()) {
std::cout << result.value_unsafe().value << std::endl;
} else {
std::cout << "Failure: " << result.error_unsafe() << std::endl;
}
}