32 EXPECT_EQ(u8
"I \u2665 UTF-8", val.asString());
33 val =
parseJson(u8
"\"I \U0001D11E playing in G-clef\"");
34 EXPECT_EQ(u8
"I \U0001D11E playing in G-clef", val.asString());
36 val =
parseJson(
"\"I \\uD834\\uDD1E playing in G-clef\"");
37 EXPECT_EQ(u8
"I \U0001D11E playing in G-clef", val.asString());
47 auto numAs1 = num.asDouble();
52 auto largeNumber =
parseJson(
"4611686018427387904");
54 EXPECT_EQ(largeNumber, 4611686018427387904L);
72 std::numeric_limits<double>::infinity(),
75 -std::numeric_limits<double>::infinity(),
87 auto array =
parseJson(
"[12,false, false , null , [12e4,32, [], 12]]");
89 if (array.size() == 5) {
98 parseJson(
"{\"foo\":12,\"bar\":42} \"something\""), std::runtime_error);
110 dynamic::array(
"heh"),
119 parseJson(
"{\"old_value\":40,\"changed\":true,\"opened\":false}");
125 TEST(Json, ParseTrailingComma) {
130 dynamic arr = dynamic::array(1, 2);
152 auto badDouble =
int64_t((1ULL << 63ULL) + 1);
159 auto okDouble =
int64_t(1ULL << 63ULL);
185 R
"("\b\f\n\r\u0001\t\\\"/\u000b\u0007")"); 188 TEST(Json, EscapeCornerCases) { 200 for (
bool ascii : {
true,
false}) {
203 for (
size_t len = 2; len < 32; ++len) {
204 for (
size_t i = 0;
i < len; ++
i) {
205 for (
size_t j = 0; j < len; ++j) {
213 expected.push_back(
'"');
214 for (
size_t pos = 0; pos < len; ++pos) {
217 expected.append(
"\\\\");
218 }
else if (pos == j) {
219 s.append(
"\xe2\x82\xac");
220 expected.append(ascii ?
"\\u20ac" :
"\xe2\x82\xac");
223 expected.push_back(
'x');
226 expected.push_back(
'"');
235 TEST(Json, JsonNonAsciiEncoding) {
249 R
"("\u001f\u00a2\u20ac")"); 252 R
"("\u0080\uffff")"); 255 R
"("\u0800\u07ff")"); 318 folly::toJson(
"a\xe0\xa0\x80z\xc0\x80"),
"\"a\xe0\xa0\x80z\xc0\x80\"");
321 TEST(Json, UTF8EncodeNonAsciiRetention) {
351 "\"a\xe0\xa0\x80m\xc2\x80z\"");
360 u8
"\"a\xe0\xa0\x80z\ufffd\ufffd\"");
363 u8
"\"a\xe0\xa0\x80z\ufffd\ufffd\ufffd\"");
366 u8
"\"z\ufffd\ufffdz\xe0\xa0\x80\"");
371 "\"a\\u0800z\\ufffd\\ufffd\"");
374 "\"a\\u0800z\\ufffd\\ufffd\\ufffd\"");
377 "\"z\\ufffd\\ufffdz\\u0800\"");
380 TEST(Json, ParseNonStringKeys) {
397 auto sval =
parseJson(
"{\"a\":[]}", opts);
398 EXPECT_EQ(
"a", sval.items().begin()->first.asString());
402 EXPECT_EQ(1.5, dval.items().begin()->first.asDouble());
405 TEST(Json, ParseDoubleFallback) {
408 parseJson(
"{\"a\":847605071342477600000000000000}"), std::range_error);
413 parseJson(
"{\"a\":-9223372036854775808}")
419 parseJson(
"{\"a\":9223372036854775807}").items().
begin()->second.asInt());
424 847605071342477600000000000000.0,
425 parseJson(
"{\"a\":847605071342477600000000000000}", opts)
428 ->second.asDouble());
430 847605071342477600000000000000.0,
431 parseJson(
"{\"a\": 847605071342477600000000000000}", opts)
434 ->second.asDouble());
436 847605071342477600000000000000.0,
437 parseJson(
"{\"a\":847605071342477600000000000000 }", opts)
440 ->second.asDouble());
442 847605071342477600000000000000.0,
443 parseJson(
"{\"a\": 847605071342477600000000000000 }", opts)
446 ->second.asDouble());
449 parseJson(
"{\"a\":-9223372036854775808}", opts)
455 parseJson(
"{\"a\":9223372036854775807}", opts)
461 847605071342477612345678900000.0,
462 parseJson(
"{\"a\":847605071342477612345678912345}", opts)
465 ->second.asDouble());
468 R"({"a":-9223372036854775808})"); 471 TEST(Json, ParseNumbersAsStrings) { 485 "46845131213548676854213265486468451312135486768542132",
486 parse(
"46845131213548676854213265486468451312135486768542132"));
488 "-468451312135486768542132654864684513121354867685.5e4",
489 parse(
"-468451312135486768542132654864684513121354867685.5e4"));
528 dynamic::array(
"heh"),
533 R
"({"a":[{"a":"b","c":"d"},12.5,"Yo Dawg",["heh"],null],)" 534 R"("another":32.2,"foo":"bar","junk":12})"; 537 R"({"junk":12,"foo":"bar","another":32.2,)" 538 R"("a":[{"c":"d","a":"b"},12.5,"Yo Dawg",["heh"],null]})"; 551 std::ostringstream oss; 573 dynamic::array(
"heh"),
611 for (
int i = 0;
i < 1000;
i++) {
612 in.append(
"{\"x\":");
615 for (
int i = 0;
i < 1000;
i++) {
622 parseJson(in, opts_high_recursion_limit);
633 EXPECT_EQ(
"{\"a\":\"<foo\\u0040bar%baz?>\"}", serialized);
640 EXPECT_EQ(
"{\"a\":\"<foo@bar%baz\\u003f>\"}", serialized);
647 EXPECT_EQ(
"{\"a\":\"\\u003cfoo\\u0040bar\\u0025baz\\u003f>\"}", serialized);
655 EXPECT_EQ(
"{\"a\":\"a\xe0\xa0\x80z\xc0\x80\"}", serialized);
659 TEST(Json, CharsToUnicodeEscape) {
660 auto testPair = [](std::array<uint64_t, 2> arr,
uint64_t zero,
uint64_t one) {
692 (1UL << 63) | (1UL << 36),
698 (1UL << 63) | (1UL << 36) | (1UL << 33),
704 (1UL << 63) | (1UL << 36) | (1UL << 33),
705 (1UL << (64 - 64)) | (1UL << (93 - 64)));
size_t parse(const char *buf, size_t len)
#define EXPECT_ANY_THROW(statement)
#define EXPECT_THROW(statement, expected_exception)
dynamic parseJson(StringPiece range)
#define EXPECT_EQ(val1, val2)
auto begin(TestAdlIterable &instance)
unsigned int recursion_limit
constexpr auto size(C const &c) -> decltype(c.size())
std::string asString() const
bool allow_trailing_comma
static const char *const value
Function< bool(dynamic const &, dynamic const &) const > sort_keys_by
std::array< uint64_t, 2 > buildExtraAsciiToEscapeBitmap(StringPiece chars)
#define EXPECT_TRUE(condition)
bool parse_numbers_as_strings
#define EXPECT_NE(val1, val2)
bool allow_non_string_keys
std::array< uint64_t, 2 > extra_ascii_to_escape_bitmap
std::string serialize(dynamic const &dyn, serialization_opts const &opts)
std::string toJson(dynamic const &dyn)
PUSHMI_INLINE_VAR constexpr detail::on_fn on
void PrintTo(const ReferenceWrapper< T > &ref,::std::ostream *os)
constexpr detail::First first