-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathjson_parser.cpp
301 lines (268 loc) · 11 KB
/
json_parser.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
/*******************************************************************\
Module: Example Catch Tests
Author: Diffblue Ltd.
\*******************************************************************/
#include <fstream>
#include <json/json_parser.h>
#include <testing-utils/message.h>
#include <testing-utils/use_catch.h>
#include <util/tempfile.h>
SCENARIO("Loading JSON files")
{
GIVEN("A invalid JSON file and a valid JSON file")
{
temporary_filet valid_json_file("cbmc_unit_json_parser_valid", ".json");
temporary_filet invalid_json_file("cbmc_unit_json_parser_invalid", ".json");
const std::string valid_json_path = valid_json_file();
const std::string invalid_json_path = invalid_json_file();
{
std::ofstream valid_json_out(valid_json_path);
valid_json_out << "{\n"
<< " \"hello\": \"world\"\n"
<< "}\n";
}
{
std::ofstream invalid_json_out(invalid_json_path);
invalid_json_out << "foo\n";
}
WHEN("Loading the invalid JSON file")
{
jsont invalid_json;
const auto invalid_parse_error =
parse_json(invalid_json_path, null_message_handler, invalid_json);
THEN("An error state should be returned")
{
REQUIRE(invalid_parse_error);
REQUIRE(invalid_json.is_null());
AND_WHEN("Loading the valid JSON file")
{
jsont valid_json;
const auto valid_parse_error =
parse_json(valid_json_path, null_message_handler, valid_json);
THEN("The JSON file should be parsed correctly")
{
REQUIRE_FALSE(valid_parse_error);
REQUIRE(valid_json.is_object());
const json_objectt &json_object = to_json_object(valid_json);
REQUIRE(json_object.find("hello") != json_object.end());
REQUIRE(json_object["hello"].value == "world");
}
}
}
}
WHEN("Loading the valid JSON file")
{
jsont valid_json;
const auto valid_parse_error =
parse_json(valid_json_path, null_message_handler, valid_json);
THEN("The JSON file should be parsed correctly")
{
REQUIRE_FALSE(valid_parse_error);
REQUIRE(valid_json.is_object());
const json_objectt &json_object = to_json_object(valid_json);
REQUIRE(json_object.find("hello") != json_object.end());
REQUIRE(json_object["hello"].value == "world");
AND_WHEN("Loading the invalid JSON file")
{
jsont invalid_json;
const auto invalid_parse_error =
parse_json(invalid_json_path, null_message_handler, invalid_json);
THEN("An error state should be returned")
{
REQUIRE(invalid_parse_error);
REQUIRE(invalid_json.is_null());
}
}
}
}
}
GIVEN("A JSON file containing hexadecimal Unicode symbols")
{
temporary_filet unicode_json_file("cbmc_unit_json_parser_unicode", ".json");
const std::string unicode_json_path = unicode_json_file();
{
std::ofstream unicode_json_out(unicode_json_path);
unicode_json_out << "{\n"
<< " \"one\": \"\\u0001\",\n"
<< " \"latin\": \"\\u0042\",\n"
<< " \"grave\": \"\\u00E0\",\n"
<< " \"trema\": \"\\u00FF\",\n"
<< " \"high\": \"\\uFFFF\",\n"
<< " \"several\": \"a\\u0041b\\u2FC3\\uFFFF\"\n"
<< "}\n";
}
WHEN("Loading the JSON file with the Unicode symbols")
{
jsont unicode_json;
const auto unicode_parse_error =
parse_json(unicode_json_path, null_message_handler, unicode_json);
THEN("The JSON file should be parsed correctly")
{
REQUIRE_FALSE(unicode_parse_error);
REQUIRE(unicode_json.is_object());
const json_objectt &json_object = to_json_object(unicode_json);
REQUIRE(json_object.find("one") != json_object.end());
REQUIRE(json_object["one"].value.size() == 1);
REQUIRE(json_object["one"].value == u8"\u0001");
REQUIRE(json_object.find("latin") != json_object.end());
REQUIRE(json_object["latin"].value == "B");
REQUIRE(json_object.find("grave") != json_object.end());
REQUIRE(json_object["grave"].value == "à");
REQUIRE(json_object.find("trema") != json_object.end());
REQUIRE(json_object["trema"].value == "ÿ");
REQUIRE(json_object.find("high") != json_object.end());
REQUIRE(json_object["high"].value == u8"\uFFFF");
REQUIRE(json_object.find("several") != json_object.end());
REQUIRE(json_object["several"].value == u8"aAb\u2FC3\uFFFF");
}
}
}
GIVEN("A JSON file containing escaped characters")
{
temporary_filet unicode_json_file("cbmc_unit_json_parser_escaped", ".json");
const std::string unicode_json_path = unicode_json_file();
{
std::ofstream unicode_json_out(unicode_json_path);
unicode_json_out << "{\n"
<< " \"doublequote\": \"\\\"\",\n"
<< " \"backslash\": \"\\\\\",\n"
<< " \"slash\": \"\\/\",\n"
<< " \"backspace\": \"\\b\",\n"
<< " \"formfeed\": \"\\f\",\n"
<< " \"newline\": \"\\n\",\n"
<< " \"return\": \"\\r\",\n"
<< " \"tab\": \"\\t\",\n"
<< " \"several\": \"\\\"\\\\\\/\\b\\f\\n\\r\\t\"\n"
<< "}\n";
}
WHEN("Loading the JSON file with the escaped characters")
{
jsont unicode_json;
const auto unicode_parse_error =
parse_json(unicode_json_path, null_message_handler, unicode_json);
THEN("The JSON file should be parsed correctly")
{
REQUIRE_FALSE(unicode_parse_error);
REQUIRE(unicode_json.is_object());
const json_objectt &json_object = to_json_object(unicode_json);
REQUIRE(json_object.find("doublequote") != json_object.end());
REQUIRE(json_object["doublequote"].value == "\"");
REQUIRE(json_object.find("backslash") != json_object.end());
REQUIRE(json_object["backslash"].value == "\\");
REQUIRE(json_object.find("slash") != json_object.end());
REQUIRE(json_object["slash"].value == "/");
REQUIRE(json_object.find("backspace") != json_object.end());
REQUIRE(json_object["backspace"].value == "\b");
REQUIRE(json_object.find("formfeed") != json_object.end());
REQUIRE(json_object["formfeed"].value == "\f");
REQUIRE(json_object.find("newline") != json_object.end());
REQUIRE(json_object["newline"].value == "\n");
REQUIRE(json_object.find("return") != json_object.end());
REQUIRE(json_object["return"].value == "\r");
REQUIRE(json_object.find("tab") != json_object.end());
REQUIRE(json_object["tab"].value == "\t");
REQUIRE(json_object.find("several") != json_object.end());
REQUIRE(json_object["several"].value == "\"\\/\b\f\n\r\t");
}
}
}
}
TEST_CASE("json equality", "[core][util][json]")
{
SECTION("null")
{
REQUIRE(jsont::null_json_object == jsont::null_json_object);
}
SECTION("boolean")
{
REQUIRE(jsont::json_boolean(false) == jsont::json_boolean(false));
REQUIRE(jsont::json_boolean(true) == jsont::json_boolean(true));
REQUIRE_FALSE(jsont::json_boolean(true) == jsont::json_boolean(false));
REQUIRE_FALSE(jsont::json_boolean(false) == jsont::null_json_object);
}
SECTION("number")
{
REQUIRE(json_numbert("0") == json_numbert("0"));
REQUIRE(json_numbert("1") == json_numbert("1"));
REQUIRE(json_numbert("-1") == json_numbert("-1"));
REQUIRE(json_numbert("1.578") == json_numbert("1.578"));
REQUIRE_FALSE(json_numbert("0") == json_numbert("1"));
REQUIRE_FALSE(json_numbert("1") == json_numbert("-1"));
REQUIRE_FALSE(json_numbert("-1") == json_numbert("1"));
REQUIRE_FALSE(json_numbert("1.578") == json_numbert("1.5789"));
REQUIRE_FALSE(json_numbert("0") == jsont::json_boolean(false));
REQUIRE_FALSE(jsont::json_boolean(false) == json_numbert("0"));
REQUIRE_FALSE(json_numbert("0") == jsont::null_json_object);
REQUIRE_FALSE(jsont::null_json_object == json_numbert("0"));
}
SECTION("string")
{
REQUIRE(json_stringt("") == json_stringt(""));
REQUIRE(json_stringt("foo") == json_stringt("foo"));
REQUIRE(json_stringt("bar") == json_stringt("bar"));
REQUIRE_FALSE(json_stringt("foo") == json_stringt("bar"));
REQUIRE_FALSE(json_stringt("bar") == json_stringt("baz"));
REQUIRE_FALSE(json_stringt("foo") == json_stringt("food"));
REQUIRE_FALSE(json_stringt("1") == json_numbert("1"));
REQUIRE_FALSE(json_stringt("true") == jsont::json_boolean("true"));
REQUIRE_FALSE(json_stringt("") == jsont::json_boolean("false"));
REQUIRE_FALSE(json_stringt("") == jsont::null_json_object);
}
SECTION("array")
{
REQUIRE(json_arrayt{} == json_arrayt{});
REQUIRE(
json_arrayt{jsont::null_json_object} ==
json_arrayt{jsont::null_json_object});
REQUIRE(
json_arrayt{json_numbert{"9"}, json_numbert{"6"}} ==
json_arrayt{json_numbert{"9"}, json_numbert{"6"}});
REQUIRE(
json_arrayt{
json_stringt{"foo"}, json_stringt{"bar"}, json_stringt{"baz"}} ==
json_arrayt{
json_stringt{"foo"}, json_stringt{"bar"}, json_stringt{"baz"}});
// different lengths
REQUIRE_FALSE(
json_arrayt{json_stringt{"foo"}, json_stringt{"bar"}} ==
json_arrayt{
json_stringt{"foo"}, json_stringt{"bar"}, json_stringt{"baz"}});
// different elements
REQUIRE_FALSE(
json_arrayt{
json_stringt{"foo"}, json_stringt{"bar"}, json_stringt{"foo"}} ==
json_arrayt{
json_stringt{"foo"}, json_stringt{"bar"}, json_stringt{"baz"}});
// different kind
REQUIRE_FALSE(json_arrayt{} == jsont::json_boolean(false));
REQUIRE_FALSE(json_arrayt{} == jsont::null_json_object);
}
SECTION("object")
{
REQUIRE(json_objectt{} == json_objectt{});
REQUIRE(
json_objectt{{"key", json_stringt{"value"}}} ==
json_objectt{{"key", json_stringt{"value"}}});
REQUIRE(
json_objectt{{"key1", json_stringt{"value1"}},
{"key2", json_stringt{"value2"}}} ==
json_objectt{{"key1", json_stringt{"value1"}},
{"key2", json_stringt{"value2"}}});
// Extra property
REQUIRE_FALSE(
json_objectt{{"key1", json_stringt{"value1"}},
{"key2", json_stringt{"value2"}},
{"key3", json_stringt{"value3"}}} ==
json_objectt{{"key1", json_stringt{"value1"}},
{"key2", json_stringt{"value2"}}});
// different field values
REQUIRE_FALSE(
json_objectt{{"key1", json_stringt{"foo"}},
{"key2", json_stringt{"bar"}}} ==
json_objectt{{"key1", json_stringt{"foo"}},
{"key2", json_stringt{"baz"}}});
// different kind
REQUIRE_FALSE(json_objectt{} == json_arrayt{});
REQUIRE_FALSE(json_objectt{} == jsont::null_json_object);
}
}