Data: keep exact JSON number lexemes and accept out-of-range numbers
StrictJson now preserves numbers that cannot be represented exactly as int64 or double in a Number alternative holding the exact lexeme, integrality, int64 fit and double range state. Grammar-valid numbers such as 2^63, 1e309 and 1e-400 parse instead of failing, decimals such as 1.0000000000000001 no longer collapse to double 1.0, and serialization round-trips the lexeme. Expose as_number/is_integral/is_finite_number and keep allowDecimals for consumers that reject decimals. Closes #60
This commit is contained in:
1 parent
e6745ac8cb
commit
9cb86f8f97
2 files changed
+396
-17
No files matched your search
@@ -39,10 +39,105 @@ Void::Testing::Test strict_json_preserves_signed_64_bit_integers()
|
||||
Void::Testing::expect_eq(
|
||||
std::get<std::int64_t>(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<Void::Json::Strict::Value::Object>(&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::parse("9223372036854775808").has_value());
|
||||
!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("-9223372036854775809").has_value());
|
||||
!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<double>(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<std::string_view, 4> 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()
|
||||
@@ -108,11 +203,12 @@ Void::Testing::Test strict_json_parses_lsp_decimals_and_exponents()
|
||||
|
||||
Void::Testing::Test strict_json_rejects_malformed_numbers()
|
||||
{
|
||||
constexpr std::array<std::string_view, 20> kMalformed{
|
||||
"1.", "-0.", ".5", "-.5", "1.e5",
|
||||
"1e", "1e+", "1E-", "1.5e", "01",
|
||||
"01.5", "-01.5", "+1", "+1.5", "1e309",
|
||||
"-1e309", "1e-400", "NaN", "Infinity", "-Infinity",
|
||||
// Out-of-range numbers (1e309, 1e-400) are grammar-valid and accepted;
|
||||
// see strict_json_accepts_out_of_double_range_numbers.
|
||||
constexpr std::array<std::string_view, 17> 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());
|
||||
|
||||
Reference in new issue
Block a user