#include #include #include #include #include #include #include #include #include #include Void::Testing::Test strict_json_preserves_signed_64_bit_integers() { const auto result = Void::Json::Strict::parse( R"({"minimum":-9223372036854775808,"maximum":9223372036854775807,"zero":0,"negZero":-0,"answer":42})"); Void::Testing::require(result.has_value()); const auto* object = std::get_if(&result->value); Void::Testing::require(object != nullptr); Void::Testing::require( std::holds_alternative(object->at("minimum").value)); Void::Testing::expect_eq( std::get(object->at("minimum").value), INT64_MIN); Void::Testing::require( std::holds_alternative(object->at("maximum").value)); Void::Testing::expect_eq( std::get(object->at("maximum").value), INT64_MAX); Void::Testing::require( std::holds_alternative(object->at("zero").value)); Void::Testing::expect_eq( std::get(object->at("zero").value), std::int64_t{0}); Void::Testing::require( std::holds_alternative(object->at("negZero").value)); Void::Testing::expect_eq( std::get(object->at("negZero").value), std::int64_t{0}); Void::Testing::require( std::holds_alternative(object->at("answer").value)); Void::Testing::expect_eq( std::get(object->at("answer").value), std::int64_t{42}); } Void::Testing::Test strict_json_keeps_out_of_int64_integers_as_exact_numbers() { const auto positive = Void::Json::Strict::parse("9223372036854775808"); Void::Testing::require(positive.has_value()); const auto* number = Void::Json::Strict::as_number(*positive); Void::Testing::require(number != nullptr); Void::Testing::expect_eq(number->lexeme, std::string{"9223372036854775808"}); Void::Testing::expect(number->isInteger); Void::Testing::expect(!number->fitsInt64); Void::Testing::expect(number->range == Void::Json::Strict::Number::Range::in_range); Void::Testing::expect(Void::Json::Strict::is_integral(*positive)); const auto negative = Void::Json::Strict::parse("-9223372036854775809"); Void::Testing::require(negative.has_value()); const auto* negative_number = Void::Json::Strict::as_number(*negative); Void::Testing::require(negative_number != nullptr); Void::Testing::expect(negative_number->isInteger); Void::Testing::expect(!negative_number->fitsInt64); } Void::Testing::Test strict_json_accepts_out_of_double_range_numbers() { const auto tiny = Void::Json::Strict::parse(R"({"n":1e-400})"); Void::Testing::require(tiny.has_value()); const auto* tiny_object = std::get_if(&tiny->value); Void::Testing::require(tiny_object != nullptr); const auto* tiny_number = Void::Json::Strict::as_number(tiny_object->at("n")); Void::Testing::require(tiny_number != nullptr); Void::Testing::expect_eq(tiny_number->lexeme, std::string{"1e-400"}); Void::Testing::expect(!tiny_number->isInteger); Void::Testing::expect(tiny_number->range == Void::Json::Strict::Number::Range::underflow); Void::Testing::expect( !Void::Json::Strict::is_finite_number(tiny_object->at("n"))); for (const std::string_view input : {"1e309", "-1e309"}) { const auto parsed = Void::Json::Strict::parse(input); Void::Testing::require(parsed.has_value()); const auto* huge = Void::Json::Strict::as_number(*parsed); Void::Testing::require(huge != nullptr); Void::Testing::expect_eq(huge->lexeme, std::string{input}); Void::Testing::expect(huge->range == Void::Json::Strict::Number::Range::overflow); } // allowDecimals still rejects decimal lexemes, in range or not. constexpr Void::Json::Strict::ParseOptions noDecimals{ .allowDecimals = false, }; Void::Testing::expect( !Void::Json::Strict::parse("1e309", noDecimals).has_value()); Void::Testing::expect( !Void::Json::Strict::parse("1e-400", noDecimals).has_value()); } Void::Testing::Test strict_json_decimal_lexemes_keep_exactness() { // A rounded fractional id must not become the integral id 1. const auto rounded = Void::Json::Strict::parse("1.0000000000000001"); Void::Testing::require(rounded.has_value()); const auto* number = Void::Json::Strict::as_number(*rounded); Void::Testing::require(number != nullptr); Void::Testing::expect_eq(number->lexeme, std::string{"1.0000000000000001"}); Void::Testing::expect(!number->isInteger); Void::Testing::expect(!Void::Json::Strict::is_integral(*rounded)); // Exactly representable decimals stay doubles and are still integral // only when their value is integral. const auto exact = Void::Json::Strict::parse("1.0"); Void::Testing::require(exact.has_value()); Void::Testing::expect( std::holds_alternative(exact->value)); Void::Testing::expect(Void::Json::Strict::is_integral(*exact)); const auto exponent = Void::Json::Strict::parse("1e2"); Void::Testing::require(exponent.has_value()); Void::Testing::expect(Void::Json::Strict::is_integral(*exponent)); const auto fractional = Void::Json::Strict::parse("0.5"); Void::Testing::require(fractional.has_value()); Void::Testing::expect(!Void::Json::Strict::is_integral(*fractional)); } Void::Testing::Test strict_json_exact_numbers_round_trip_through_serialize() { constexpr std::array kInputs{ "9223372036854775808", "1e309", "1e-400", "1.0000000000000001", }; for (const std::string_view input : kInputs) { const auto parsed = Void::Json::Strict::parse(input); Void::Testing::require(parsed.has_value()); const auto serialized = Void::Json::Strict::serialize(*parsed); Void::Testing::require(serialized.has_value()); Void::Testing::expect_eq(*serialized, std::string{input}); } } Void::Testing::Test strict_json_rejects_duplicates_floats_when_disabled_and_deep_inputs() { Void::Testing::expect( !Void::Json::Strict::parse(R"({"key":1,"key":2})").has_value()); constexpr Void::Json::Strict::ParseOptions noDecimals{ .allowDecimals = false, }; Void::Testing::expect( !Void::Json::Strict::parse(R"({"value":1.5})", noDecimals).has_value()); Void::Testing::expect( !Void::Json::Strict::parse("1e5", noDecimals).has_value()); Void::Testing::expect( !Void::Json::Strict::parse("[[[0]]]", 1U).has_value()); } Void::Testing::Test strict_json_parses_lsp_decimals_and_exponents() { constexpr std::string_view kLspMessage = R"({"jsonrpc":"2.0","id":1,"result":{"red":0.5,"green":0.25,"blue":1.0,"alpha":1.0,"offset":-0.125,"tiny":1e-3,"scaled":2.5E+2,"negZero":-0.0}})"; const auto parsed = Void::Json::Strict::parse(kLspMessage); Void::Testing::require(parsed.has_value()); const auto* root = std::get_if(&parsed->value); Void::Testing::require(root != nullptr); Void::Testing::expect_eq( std::get(root->at("jsonrpc").value), std::string{"2.0"}); Void::Testing::require( std::holds_alternative(root->at("id").value)); Void::Testing::expect_eq( std::get(root->at("id").value), std::int64_t{1}); const auto* color = std::get_if(&root->at("result").value); Void::Testing::require(color != nullptr); auto expect_decimal = [&](const std::string_view key, const double expected) { const auto it = color->find(key); Void::Testing::require(it != color->end()); Void::Testing::require( std::holds_alternative(it->second.value)); Void::Testing::expect_eq(std::get(it->second.value), expected); }; expect_decimal("red", 0.5); expect_decimal("green", 0.25); expect_decimal("blue", 1.0); expect_decimal("alpha", 1.0); expect_decimal("offset", -0.125); expect_decimal("tiny", 1e-3); expect_decimal("scaled", 250.0); const auto negZeroIt = color->find("negZero"); Void::Testing::require(negZeroIt != color->end()); Void::Testing::require( std::holds_alternative(negZeroIt->second.value)); const double negZero = std::get(negZeroIt->second.value); Void::Testing::expect_eq(negZero, 0.0); Void::Testing::expect(std::signbit(negZero)); } Void::Testing::Test strict_json_rejects_malformed_numbers() { // Out-of-range numbers (1e309, 1e-400) are grammar-valid and accepted; // see strict_json_accepts_out_of_double_range_numbers. constexpr std::array kMalformed{ "1.", "-0.", ".5", "-.5", "1.e5", "1e", "1e+", "1E-", "1.5e", "01", "01.5", "-01.5", "+1", "+1.5", "NaN", "Infinity", "-Infinity", }; for (const std::string_view input : kMalformed) { Void::Testing::expect(!Void::Json::Strict::parse(input).has_value()); } } Void::Testing::Test strict_json_value_constructors_select_expected_alternatives() { using Void::Json::Strict::Value; const Value nilVal{nullptr}; const Value boolVal{true}; const Value intVal{42}; const Value i64Val{std::numeric_limits::max()}; const Value floatVal{1.5F}; const Value doubleVal{2.25}; const Value strVal{std::string{"hello"}}; Void::Testing::expect(std::holds_alternative(nilVal.value)); Void::Testing::expect(std::holds_alternative(boolVal.value)); Void::Testing::expect(std::holds_alternative(intVal.value)); Void::Testing::expect_eq(std::get(intVal.value), std::int64_t{42}); Void::Testing::expect(std::holds_alternative(i64Val.value)); Void::Testing::expect(std::holds_alternative(floatVal.value)); Void::Testing::expect_eq(std::get(floatVal.value), 1.5); Void::Testing::expect(std::holds_alternative(doubleVal.value)); Void::Testing::expect_eq(std::get(doubleVal.value), 2.25); Void::Testing::expect(std::holds_alternative(strVal.value)); } Void::Testing::Test strict_json_decodes_valid_unicode_pairs() { const auto result = Void::Json::Strict::parse(R"("\uD83D\uDE03")"); Void::Testing::require(result.has_value()); Void::Testing::expect_eq(std::get(result->value), std::string{"\xF0\x9F\x98\x83"}); Void::Testing::expect( !Void::Json::Strict::parse(R"("\uD83D")").has_value()); } Void::Testing::Test strict_json_validates_utf8_in_parse_and_serialize() { // Valid 1, 2, 3, and 4-byte scalars including boundaries U+D7FF, U+E000, U+10FFFF const std::string valid_json = std::string{R"({"ascii":"hello","latin":")"} + "\xC3\xA9" + R"(","euro":")" + "\xE2\x82\xAC" + R"(","emoji":")" + "\xF0\x9F\x98\x80" + R"(","hi_bmp":")" + "\xED\x9F\xBF" + R"(","priv":")" + "\xEE\x80\x80" + R"(","max_cp":")" + "\xF4\x8F\xBF\xBF" + R"("})"; const auto parsed = Void::Json::Strict::parse(valid_json); Void::Testing::require(parsed.has_value()); const auto serialized = Void::Json::Strict::serialize(*parsed); Void::Testing::require(serialized.has_value()); const auto reparsed = Void::Json::Strict::parse(*serialized); Void::Testing::require(reparsed.has_value()); // Invalid UTF-8 sequences in string values and object keys const std::array invalid_payloads{ std::string{"\"\xFF\""}, // invalid lead 0xFF std::string{"\"\x80\""}, // stray continuation byte std::string{"\"\xBF\""}, // stray continuation byte std::string{"\"\xC0\xAF\""}, // overlong 2-byte '/' std::string{"\"\xC1\x81\""}, // overlong 2-byte 'A' std::string{"\"\xE0\x9F\x80\""}, // overlong 3-byte std::string{"\"\xED\xA0\x80\""}, // UTF-8 encoded U+D800 std::string{"\"\xED\xBF\xBF\""}, // UTF-8 encoded U+DFFF std::string{"\"\xF4\x90\x80\x80\""}, // > U+10FFFF (U+110000) std::string{"{\"\xFF\":\"val\"}"}, // invalid UTF-8 in object key }; for (const auto& payload : invalid_payloads) { const auto res = Void::Json::Strict::parse(payload); Void::Testing::expect(!res.has_value()); Void::Testing::expect(!Void::Json::Strict::is_valid_utf8(payload)); } // Truncated multi-byte sequences inside strings Void::Testing::expect( !Void::Json::Strict::parse(std::string{"\"\xC3\""}).has_value()); Void::Testing::expect( !Void::Json::Strict::parse(std::string{"\"\xF0\x9F\x98\""}) .has_value()); // Serialization must also reject corrupt UTF-8 in Values or keys const Void::Json::Strict::Value bad_val{std::string{"ok\xED\xA0\x80"}}; Void::Testing::expect( !Void::Json::Strict::serialize(bad_val).has_value()); Void::Json::Strict::Value::Object bad_key_obj; bad_key_obj.emplace(std::string{"bad\xFFkey"}, Void::Json::Strict::Value{1}); Void::Testing::expect( !Void::Json::Strict::serialize( Void::Json::Strict::Value{std::move(bad_key_obj)}) .has_value()); // Control char escaping & round-trip in serialize() const Void::Json::Strict::Value ctrl{std::string{"a\nb\tc\x01\"\\"}}; const auto escaped = Void::Json::Strict::serialize(ctrl); Void::Testing::require(escaped.has_value()); const auto ctrl_back = Void::Json::Strict::parse(*escaped); Void::Testing::require(ctrl_back.has_value()); Void::Testing::expect_eq(std::get(ctrl_back->value), std::string{"a\nb\tc\x01\"\\"}); }