Repository navigation
Expand file tree
/
Copy pathjson_test.cpp
More file actions
396 lines (331 loc) · 12.4 KB
/
Copy pathjson_test.cpp
File metadata and controls
396 lines (331 loc) · 12.4 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Copyright 2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cmath>
#include <limits>
#include <iostream>
#include <gtest/gtest.h>
#include <boost/next_prior.hpp>
#include "json.h"
//
// MSVC 2013 does not support unicode/raw string literals
//
#if __GNUC_PREREQ(4, 7)
TEST(Json, Unicode)
{
auto val = parseJson("\"I \u2665 UTF-8\"");
EXPECT_EQ("I \u2665 UTF-8", val.asString());
val = parseJson("\"I \\u2665 UTF-8\"");
EXPECT_EQ("I \u2665 UTF-8", val.asString());
val = parseJson("\"I \U0001D11E playing in G-clef\"");
EXPECT_EQ("I \U0001D11E playing in G-clef", val.asString());
val = parseJson("\"I \\uD834\\uDD1E playing in G-clef\"");
EXPECT_EQ("I \U0001D11E playing in G-clef", val.asString());
}
#endif
TEST(Json, Parse)
{
auto num = parseJson("12");
EXPECT_TRUE(num.isInt());
EXPECT_EQ(num, 12);
num = parseJson("12e5");
EXPECT_TRUE(num.isDouble());
EXPECT_EQ(num, 12e5);
auto numAs1 = num.asDouble();
EXPECT_EQ(numAs1, 12e5);
EXPECT_EQ(num, 12e5);
EXPECT_EQ(num, 1200000);
auto largeNumber = parseJson("4611686018427387904");
EXPECT_TRUE(largeNumber.isInt());
EXPECT_EQ(largeNumber, 4611686018427387904L);
auto negative = parseJson("-123");
EXPECT_EQ(negative, -123);
auto bfalse = parseJson("false");
auto btrue = parseJson("true");
EXPECT_EQ(bfalse, false);
EXPECT_EQ(btrue, true);
auto null = parseJson("null");
EXPECT_TRUE(null == nullptr);
auto doub1 = parseJson("12.0");
auto doub2 = parseJson("12e2");
EXPECT_EQ(doub1, 12.0);
EXPECT_EQ(doub2, 12e2);
EXPECT_EQ(std::numeric_limits<double>::infinity(),
parseJson("Infinity").asDouble());
EXPECT_EQ(-std::numeric_limits<double>::infinity(),
parseJson("-Infinity").asDouble());
EXPECT_TRUE(std::isnan(parseJson("NaN").asDouble()));
// case matters
EXPECT_THROW(parseJson("infinity"), std::runtime_error);
EXPECT_THROW(parseJson("inf"), std::runtime_error);
EXPECT_THROW(parseJson("nan"), std::runtime_error);
auto array = parseJson(
"[12,false, false , null , [12e4,32, [], 12]]");
EXPECT_EQ(array.size(), 5);
if (array.size() == 5)
{
EXPECT_EQ(boost::prior(array.end())->size(), 4);
}
EXPECT_THROW(parseJson("\n[12,\n\nnotvalidjson"),
std::runtime_error);
EXPECT_THROW(parseJson("12e2e2"),
std::runtime_error);
EXPECT_THROW(parseJson("{\"foo\":12,\"bar\":42} \"something\""),
std::runtime_error);
dynamic value = dynamic::object
("foo", "bar")
("junk", 12)
("another", 32.2)
("a",
{
dynamic::object("a", "b")
("c", "d"),
12.5,
"Yo Dawg",
{ "heh" },
nullptr
}
)
;
// Print then parse and get the same thing, hopefully.
EXPECT_EQ(value, parseJson(toJson(value)));
// Test an object with non-string values.
dynamic something = parseJson(
"{\"old_value\":40,\"changed\":true,\"opened\":false}");
dynamic expected = dynamic::object
("old_value", 40)
("changed", true)
("opened", false);
EXPECT_EQ(something, expected);
}
TEST(Json, ParseTrailingComma)
{
json::serialization_opts on, off;
on.allow_trailing_comma = true;
off.allow_trailing_comma = false;
dynamic arr{ 1, 2 };
EXPECT_EQ(arr, parseJson("[1, 2]", on));
EXPECT_EQ(arr, parseJson("[1, 2,]", on));
EXPECT_EQ(arr, parseJson("[1, 2, ]", on));
EXPECT_EQ(arr, parseJson("[1, 2 , ]", on));
EXPECT_EQ(arr, parseJson("[1, 2 ,]", on));
EXPECT_THROW(parseJson("[1, 2,]", off), std::runtime_error);
dynamic obj = dynamic::object("a", 1);
EXPECT_EQ(obj, parseJson("{\"a\": 1}", on));
EXPECT_EQ(obj, parseJson("{\"a\": 1,}", on));
EXPECT_EQ(obj, parseJson("{\"a\": 1, }", on));
EXPECT_EQ(obj, parseJson("{\"a\": 1 , }", on));
EXPECT_EQ(obj, parseJson("{\"a\": 1 ,}", on));
EXPECT_THROW(parseJson("{\"a\":1,}", off), std::runtime_error);
}
TEST(Json, JavascriptSafe)
{
auto badDouble = (1ll << 63ll) + 1;
dynamic badDyn = badDouble;
EXPECT_EQ(toJson(badDouble), to<std::string>(badDouble));
json::serialization_opts opts;
opts.javascript_safe = true;
EXPECT_ANY_THROW(json::serialize(badDouble, opts));
auto okDouble = 1ll << 63ll;
dynamic okDyn = okDouble;
EXPECT_EQ(toJson(okDouble), to<std::string>(okDouble));
}
#if __GNUC_PREREQ(4, 7)
TEST(Json, Produce)
{
auto value = parseJson(R"( "f\"oo" )");
EXPECT_EQ(toJson(value), R"("f\"oo")");
value = parseJson("\"Control code: \001 \002 \x1f\"");
EXPECT_EQ(toJson(value), R"("Control code: \u0001 \u0002 \u001f")");
// We're not allowed to have non-string keys in json.
EXPECT_THROW(toJson(dynamic::object("abc", "xyz")(42.33, "asd")),
std::runtime_error);
}
TEST(Json, JsonEscape)
{
json::serialization_opts opts;
EXPECT_EQ(
json::serialize("\b\f\n\r\x01\t\\\"/\v\a", opts),
R"("\b\f\n\r\u0001\t\\\"/\u000b\u0007")");
}
#endif
TEST(Json, JsonNonAsciiEncoding)
{
json::serialization_opts opts;
opts.encode_non_ascii = true;
// simple tests
EXPECT_EQ(json::serialize("\x1f", opts), R"("\u001f")");
EXPECT_EQ(json::serialize("\xc2\xa2", opts), R"("\u00a2")");
EXPECT_EQ(json::serialize("\xe2\x82\xac", opts), R"("\u20ac")");
#if __GNUC_PREREQ(4, 7)
// multiple unicode encodings
EXPECT_EQ(
json::serialize("\x1f\xe2\x82\xac", opts),
R"("\u001f\u20ac")");
EXPECT_EQ(
json::serialize("\x1f\xc2\xa2\xe2\x82\xac", opts),
R"("\u001f\u00a2\u20ac")");
EXPECT_EQ(
json::serialize("\xc2\x80\xef\xbf\xbf", opts),
R"("\u0080\uffff")");
EXPECT_EQ(
json::serialize("\xe0\xa0\x80\xdf\xbf", opts),
R"("\u0800\u07ff")");
// first possible sequence of a certain length
EXPECT_EQ(json::serialize("\xc2\x80", opts), R"("\u0080")");
EXPECT_EQ(json::serialize("\xe0\xa0\x80", opts), R"("\u0800")");
// last possible sequence of a certain length
EXPECT_EQ(json::serialize("\xdf\xbf", opts), R"("\u07ff")");
EXPECT_EQ(json::serialize("\xef\xbf\xbf", opts), R"("\uffff")");
// other boundary conditions
EXPECT_EQ(json::serialize("\xed\x9f\xbf", opts), R"("\ud7ff")");
EXPECT_EQ(json::serialize("\xee\x80\x80", opts), R"("\ue000")");
EXPECT_EQ(json::serialize("\xef\xbf\xbd", opts), R"("\ufffd")");
#endif
// incomplete sequences
EXPECT_ANY_THROW(json::serialize("a\xed\x9f", opts));
EXPECT_ANY_THROW(json::serialize("b\xee\x80", opts));
EXPECT_ANY_THROW(json::serialize("c\xef\xbf", opts));
// impossible bytes
EXPECT_ANY_THROW(json::serialize("\xfe", opts));
EXPECT_ANY_THROW(json::serialize("\xff", opts));
// Sample overlong sequences
EXPECT_ANY_THROW(json::serialize("\xc0\xaf", opts));
EXPECT_ANY_THROW(json::serialize("\xe0\x80\xaf", opts));
// Maximum overlong sequences
EXPECT_ANY_THROW(json::serialize("\xc1\xbf", opts));
EXPECT_ANY_THROW(json::serialize("\x30\x9f\xbf", opts));
// illegal code positions
EXPECT_ANY_THROW(json::serialize("\xed\xa0\x80", opts));
EXPECT_ANY_THROW(json::serialize("\xed\xbf\xbf", opts));
// Overlong representation of NUL character
EXPECT_ANY_THROW(json::serialize("\xc0\x80", opts));
EXPECT_ANY_THROW(json::serialize("\xe0\x80\x80", opts));
// Longer than 3 byte encodings
EXPECT_ANY_THROW(json::serialize("\xf4\x8f\xbf\xbf", opts));
EXPECT_ANY_THROW(json::serialize("\xed\xaf\xbf\xed\xbf\xbf", opts));
}
TEST(Json, UTF8Retention)
{
// test retention with valid utf8 strings
std::string input = "\u2665";
std::string jsonInput = toJson(input);
std::string output = parseJson(jsonInput).asString();
std::string jsonOutput = toJson(output);
EXPECT_EQ(input, output);
EXPECT_EQ(jsonInput, jsonOutput);
// test retention with invalid utf8 - note that non-ascii chars are retained
// as is, and no unicode encoding is attempted so no exception is thrown.
EXPECT_EQ(
toJson("a\xe0\xa0\x80z\xc0\x80"),
"\"a\xe0\xa0\x80z\xc0\x80\""
);
}
TEST(Json, UTF8EncodeNonAsciiRetention)
{
json::serialization_opts opts;
opts.encode_non_ascii = true;
// test encode_non_ascii valid utf8 strings
std::string input = "\u2665";
std::string jsonInput = json::serialize(input, opts);
std::string output = parseJson(jsonInput).asString();
std::string jsonOutput = json::serialize(output, opts);
EXPECT_EQ(input, output);
EXPECT_EQ(jsonInput, jsonOutput);
// test encode_non_ascii with invalid utf8 - note that an attempt to encode
// non-ascii to unicode will result is a utf8 validation and throw exceptions.
EXPECT_ANY_THROW(json::serialize("a\xe0\xa0\x80z\xc0\x80", opts));
EXPECT_ANY_THROW(json::serialize("a\xe0\xa0\x80z\xe0\x80\x80", opts));
}
#if __GNUC_PREREQ(4, 7)
TEST(Json, UTF8Validation)
{
json::serialization_opts opts;
opts.validate_utf8 = true;
// test validate_utf8 valid utf8 strings - note that we only validate the
// for utf8 but don't encode non-ascii to unicode so they are retained as is.
EXPECT_EQ(json::serialize("a\xc2\x80z", opts), "\"a\xc2\x80z\"");
EXPECT_EQ(
json::serialize("a\xe0\xa0\x80z", opts),
"\"a\xe0\xa0\x80z\"");
EXPECT_EQ(
json::serialize("a\xe0\xa0\x80m\xc2\x80z", opts),
"\"a\xe0\xa0\x80m\xc2\x80z\"");
// test validate_utf8 with invalid utf8
EXPECT_ANY_THROW(json::serialize("a\xe0\xa0\x80z\xc0\x80", opts));
EXPECT_ANY_THROW(json::serialize("a\xe0\xa0\x80z\xe0\x80\x80", opts));
opts.skip_invalid_utf8 = true;
EXPECT_EQ(json::serialize("a\xe0\xa0\x80z\xc0\x80", opts),
"\"a\xe0\xa0\x80z\ufffd\ufffd\"");
EXPECT_EQ(json::serialize("a\xe0\xa0\x80z\xc0\x80\x80", opts),
"\"a\xe0\xa0\x80z\ufffd\ufffd\ufffd\"");
EXPECT_EQ(json::serialize("z\xc0\x80z\xe0\xa0\x80", opts),
"\"z\ufffd\ufffdz\xe0\xa0\x80\"");
opts.encode_non_ascii = true;
EXPECT_EQ(json::serialize("a\xe0\xa0\x80z\xc0\x80", opts),
"\"a\\u0800z\\ufffd\\ufffd\"");
EXPECT_EQ(json::serialize("a\xe0\xa0\x80z\xc0\x80\x80", opts),
"\"a\\u0800z\\ufffd\\ufffd\\ufffd\"");
EXPECT_EQ(json::serialize("z\xc0\x80z\xe0\xa0\x80", opts),
"\"z\\ufffd\\ufffdz\\u0800\"");
}
#endif
TEST(Json, ParseNonStringKeys)
{
// test string keys
EXPECT_EQ("a", parseJson("{\"a\":[]}").items().begin()->first.asString());
// check that we don't allow non-string keys as this violates the
// strict JSON spec (though it is emitted by the output of
// dynamic with operator <<).
EXPECT_THROW(parseJson("{1:[]}"), std::runtime_error);
// check that we can parse colloquial JSON if the option is set
json::serialization_opts opts;
opts.allow_non_string_keys = true;
auto val = parseJson("{1:[]}", opts);
EXPECT_EQ(1, val.items().begin()->first.asInt());
// test we can still read in strings
auto sval = parseJson("{\"a\":[]}", opts);
EXPECT_EQ("a", sval.items().begin()->first.asString());
// test we can read in doubles
auto dval = parseJson("{1.5:[]}", opts);
EXPECT_EQ(1.5, dval.items().begin()->first.asDouble());
}
TEST(Json, SortKeys)
{
json::serialization_opts opts_on, opts_off;
opts_on.sort_keys = true;
opts_off.sort_keys = false;
dynamic value = dynamic::object
("foo", "bar")
("junk", 12)
("another", 32.2)
("a",
{
dynamic::object("a", "b")
("c", "d"),
12.5,
"Yo Dawg",
{ "heh" },
nullptr
}
)
;
std::string sorted_keys =
R"({"a":[{"a":"b","c":"d"},12.5,"Yo Dawg",["heh"],null],)"
R"("another":32.2,"foo":"bar","junk":12})";
EXPECT_EQ(value, parseJson(json::serialize(value, opts_on)));
EXPECT_EQ(value, parseJson(json::serialize(value, opts_off)));
EXPECT_EQ(sorted_keys, json::serialize(value, opts_on));
}