From 692f5ba5b2f9d998acc36bb61765f8fdf156bcbb Mon Sep 17 00:00:00 2001 From: faumaray Date: Tue, 24 Nov 2026 03:25:17 +0300 Subject: [PATCH] feat(Data): add exact 64-bit signed/unsigned JSON numbers and checked conversion (M-DATA-02) --- include/Void/Configuration/Config.hpp | 10 +- include/Void/Data/Json.hpp | 368 +++++++++++++++++++++++--- include/Void/Data/Reflect.hpp | 33 ++- src/Data/module.cmake | 2 +- tests/standalone/Data/Json.cpp | 115 ++++++++ tests/standalone/Data/Reflect.cpp | 49 ++++ 6 files changed, 530 insertions(+), 47 deletions(-) diff --git a/include/Void/Configuration/Config.hpp b/include/Void/Configuration/Config.hpp index 4a30a6d..dcab4cf 100644 --- a/include/Void/Configuration/Config.hpp +++ b/include/Void/Configuration/Config.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -404,9 +405,16 @@ namespace Void::Configuration } else if (v.is_bool()) { cfg.values_.insert_or_assign(prefix, v.as_bool() ? "true" : "false"); + } else if (v.is_int()) { + cfg.values_.insert_or_assign( + prefix, std::to_string(v.as_int())); + } else if (v.is_uint()) { + cfg.values_.insert_or_assign( + prefix, std::to_string(v.as_uint())); } else if (v.is_number()) { const double d = v.as_number(); - if (d == static_cast(static_cast(d))) { + if (std::isfinite(d) && !(d == 0.0 && std::signbit(d)) && + std::floor(d) == d && std::abs(d) < 1e15) { cfg.values_.insert_or_assign( prefix, std::to_string(static_cast(d))); } else { diff --git a/include/Void/Data/Json.hpp b/include/Void/Data/Json.hpp index dbf12f3..ed8062d 100644 --- a/include/Void/Data/Json.hpp +++ b/include/Void/Data/Json.hpp @@ -1,10 +1,14 @@ #pragma once +#include + #include #include +#include #include #include #include +#include #include #include #include @@ -23,6 +27,18 @@ namespace Void::Json class Value; + enum class NumberKind : std::uint8_t { + signed_integer, + unsigned_integer, + real + }; + + struct ConversionError { + Void::Numeric::Error numeric_error{Void::Numeric::Error::InvalidInput}; + std::string path; + std::string message; + }; + struct ParseError { std::size_t line{1}, col{1}; std::string message; @@ -40,7 +56,9 @@ namespace Void::Json using Data = std::variant, // array (boxed to avoid // incomplete-type @@ -74,9 +92,14 @@ namespace Void::Json Value() noexcept = default; Value(std::nullptr_t) noexcept {} Value(bool b) noexcept : data_{b} {} - Value(double n) noexcept : data_{n} {} - Value(int n) noexcept : data_{static_cast(n)} {} - Value(long long n) noexcept : data_{static_cast(n)} {} + template + requires(!std::same_as, bool>) + Value(T n) noexcept : data_{static_cast(n)} {} + template + requires(!std::same_as, bool>) + Value(T n) noexcept : data_{static_cast(n)} {} + template + Value(T n) noexcept : data_{static_cast(n)} {} Value(std::string s) : data_{std::move(s)} {} Value(std::string_view s) : data_{std::string{s}} {} Value(const char* s) : data_{std::string{s}} {} @@ -101,27 +124,50 @@ namespace Void::Json // ──────────────────────────────────────────────────────────── [[nodiscard]] bool is_null() const noexcept { - return data_.index() == 0; + return std::holds_alternative(data_); } [[nodiscard]] bool is_bool() const noexcept { - return data_.index() == 1; + return std::holds_alternative(data_); + } + [[nodiscard]] bool is_int() const noexcept + { + return std::holds_alternative(data_); + } + [[nodiscard]] bool is_uint() const noexcept + { + return std::holds_alternative(data_); + } + [[nodiscard]] bool is_double() const noexcept + { + return std::holds_alternative(data_); + } + [[nodiscard]] bool is_integer() const noexcept + { + return is_int() || is_uint(); } [[nodiscard]] bool is_number() const noexcept { - return data_.index() == 2; + return is_int() || is_uint() || is_double(); + } + [[nodiscard]] std::optional number_kind() const noexcept + { + if (is_int()) return NumberKind::signed_integer; + if (is_uint()) return NumberKind::unsigned_integer; + if (is_double()) return NumberKind::real; + return std::nullopt; } [[nodiscard]] bool is_string() const noexcept { - return data_.index() == 3; + return std::holds_alternative(data_); } [[nodiscard]] bool is_array() const noexcept { - return data_.index() == 4; + return std::holds_alternative>(data_); } [[nodiscard]] bool is_object() const noexcept { - return data_.index() == 5; + return std::holds_alternative>(data_); } // ── typed access (throws std::bad_variant_access on wrong type) @@ -129,12 +175,35 @@ namespace Void::Json [[nodiscard]] bool as_bool() const { return std::get(data_); } [[nodiscard]] double as_number() const { + if (const auto* i = std::get_if(&data_)) { + return static_cast(*i); + } + if (const auto* u = std::get_if(&data_)) { + return static_cast(*u); + } return std::get(data_); } [[nodiscard]] std::int64_t as_int() const { - return static_cast(as_number()); + if (const auto* i = std::get_if(&data_)) { + return *i; + } + if (const auto* u = std::get_if(&data_)) { + return static_cast(*u); + } + return static_cast(std::get(data_)); } + [[nodiscard]] std::uint64_t as_uint() const + { + if (const auto* u = std::get_if(&data_)) { + return *u; + } + if (const auto* i = std::get_if(&data_)) { + return static_cast(*i); + } + return static_cast(std::get(data_)); + } + [[nodiscard]] double as_double() const { return as_number(); } [[nodiscard]] const std::string& as_string() const { return std::get(data_); @@ -172,6 +241,42 @@ namespace Void::Json } return as_number(); } + [[nodiscard]] std::optional get_int() const noexcept + { + if (const auto* i = std::get_if(&data_)) { + return *i; + } + if (const auto* u = std::get_if(&data_)) { + auto c = Void::Numeric::checked_cast(*u); + return c ? std::optional{*c} : std::nullopt; + } + if (const auto* d = std::get_if(&data_)) { + auto c = Void::Numeric::checked_cast( + *d, Void::Numeric::Rounding::exact); + return c ? std::optional{*c} : std::nullopt; + } + return {}; + } + [[nodiscard]] std::optional get_uint() const noexcept + { + if (const auto* u = std::get_if(&data_)) { + return *u; + } + if (const auto* i = std::get_if(&data_)) { + auto c = Void::Numeric::checked_cast(*i); + return c ? std::optional{*c} : std::nullopt; + } + if (const auto* d = std::get_if(&data_)) { + auto c = Void::Numeric::checked_cast( + *d, Void::Numeric::Rounding::exact); + return c ? std::optional{*c} : std::nullopt; + } + return {}; + } + [[nodiscard]] std::optional get_double() const noexcept + { + return get_number(); + } [[nodiscard]] std::optional get_string() const noexcept { @@ -357,8 +462,40 @@ namespace Void::Json } [[nodiscard]] bool empty() const noexcept { return size() == 0; } + private: + + static bool numeric_equals(const Value& a, const Value& b) noexcept + { + if (a.is_int()) { + if (b.is_int()) return a.as_int() == b.as_int(); + if (b.is_uint()) { + return a.as_int() >= 0 && + static_cast(a.as_int()) == b.as_uint(); + } + const auto c = Void::Numeric::checked_cast( + b.as_double(), Void::Numeric::Rounding::exact); + return c.has_value() && *c == a.as_int(); + } + if (a.is_uint()) { + if (b.is_uint()) return a.as_uint() == b.as_uint(); + if (b.is_int()) return numeric_equals(b, a); + const auto c = Void::Numeric::checked_cast( + b.as_double(), Void::Numeric::Rounding::exact); + return c.has_value() && *c == a.as_uint(); + } + if (b.is_double()) { + return a.as_double() == b.as_double(); + } + return numeric_equals(b, a); + } + + public: + [[nodiscard]] bool operator==(const Value& o) const noexcept { + if (is_number() && o.is_number()) { + return numeric_equals(*this, o); + } if (data_.index() != o.data_.index()) { return false; } @@ -368,23 +505,49 @@ namespace Void::Json if (is_object()) { return as_object() == o.as_object(); } - // For monostate, bool, double, string — variant equality works - return std::visit( - [&](const auto& lhs) -> bool { - using T = std::decay_t; - if constexpr (std::is_same_v< - T, std::unique_ptr> || - std::is_same_v< - T, std::unique_ptr>) { - return false; // unreachable (handled above) - } else { - return lhs == std::get(o.data_); - } - }, - data_); + if (is_null()) { + return true; + } + if (is_bool()) { + return as_bool() == o.as_bool(); + } + if (is_string()) { + return as_string() == o.as_string(); + } + return false; } }; + template + [[nodiscard]] inline std::expected + number_as(const Value& v, + Void::Numeric::Rounding mode = Void::Numeric::Rounding::exact, + std::string_view path = {}) + { + auto make_conv_err = [&](Void::Numeric::Error e) { + return std::unexpected{ConversionError{ + e, + std::string{path}, + "Json::number_as: " + std::string{Void::Numeric::to_string(e)}}}; + }; + if (v.is_int()) { + auto r = Void::Numeric::checked_cast(v.as_int(), mode); + if (!r) return make_conv_err(r.error()); + return *r; + } + if (v.is_uint()) { + auto r = Void::Numeric::checked_cast(v.as_uint(), mode); + if (!r) return make_conv_err(r.error()); + return *r; + } + if (v.is_double()) { + auto r = Void::Numeric::checked_cast(v.as_double(), mode); + if (!r) return make_conv_err(r.error()); + return *r; + } + return make_conv_err(Void::Numeric::Error::InvalidInput); + } + // ── static factories // ────────────────────────────────────────────────────────── [[nodiscard]] inline Value array(Value::Array_t a = {}) @@ -497,43 +660,90 @@ namespace Void::Json std::expected parse_number() { const std::size_t start = pos; - if (peek() == '-') { + const bool neg = (peek() == '-'); + if (neg) { advance(); } if (peek() == '0') { advance(); - } else { - while (peek() >= '1' && peek() <= '9') { - advance(); + if (peek() >= '0' && peek() <= '9') { + return std::unexpected{ + err("leading zero in numeric literal")}; } + } else if (peek() >= '1' && peek() <= '9') { + advance(); while (peek() >= '0' && peek() <= '9') { advance(); } + } else { + return std::unexpected{err("expected digit in number")}; } + + bool is_real = false; if (peek() == '.') { + is_real = true; advance(); + if (!(peek() >= '0' && peek() <= '9')) { + return std::unexpected{ + err("expected digits after '.'")}; + } while (peek() >= '0' && peek() <= '9') { advance(); } } if (peek() == 'e' || peek() == 'E') { + is_real = true; advance(); if (peek() == '+' || peek() == '-') { advance(); } + if (!(peek() >= '0' && peek() <= '9')) { + return std::unexpected{ + err("expected exponent digits")}; + } while (peek() >= '0' && peek() <= '9') { advance(); } } + const std::string_view raw = src.substr(start, pos - start); - double val = 0.0; - const auto [ptr, ec] = std::from_chars( - raw.data(), raw.data() + raw.size(), val); - if (ec != std::errc{}) { + if (raw.size() > Void::Numeric::kMaxNumericTokenBytes) { return std::unexpected{ - err("invalid number: " + std::string{raw})}; + err("numeric literal exceeds 256 bytes")}; } - return Value{val}; + if (is_real) { + const auto r = Void::Numeric::parse_real(raw); + if (!r) { + return std::unexpected{ + err("invalid or out-of-range real: " + + std::string{raw})}; + } + return Value{*r}; + } + if (neg) { + if (raw == "-0") { + return Value{std::int64_t{0}}; + } + const auto r = + Void::Numeric::parse_integer(raw); + if (!r) { + return std::unexpected{ + err("signed integer out of range: " + + std::string{raw})}; + } + return Value{*r}; + } + if (const auto s = + Void::Numeric::parse_integer(raw)) { + return Value{*s}; + } + if (const auto u = + Void::Numeric::parse_integer(raw)) { + return Value{*u}; + } + return std::unexpected{ + err("unsigned integer out of range: " + + std::string{raw})}; } std::expected parse_string_raw() @@ -780,6 +990,8 @@ namespace Void::Json std::function on_object_end; std::function on_array_begin; std::function on_array_end; + std::function on_int{}; + std::function on_uint{}; }; namespace Detail @@ -846,7 +1058,11 @@ namespace Void::Json if (!v) { return v.error(); } - if (h.on_number) { + if (v->is_int() && h.on_int) { + h.on_int(v->as_int()); + } else if (v->is_uint() && h.on_uint) { + h.on_uint(v->as_uint()); + } else if (h.on_number) { h.on_number(v->as_number()); } return std::nullopt; @@ -1016,9 +1232,30 @@ namespace Void::Json out += v.as_bool() ? "true" : "false"; return; } - if (v.is_number()) { - const double n = v.as_number(); - if (std::floor(n) == n && std::abs(n) < 1e15) { + if (v.is_int()) { + char buf[32]; + const auto [ptr, ec] = + std::to_chars(buf, buf + sizeof(buf), v.as_int()); + if (ec == std::errc{}) { + out.append(buf, ptr); + } + return; + } + if (v.is_uint()) { + char buf[32]; + const auto [ptr, ec] = + std::to_chars(buf, buf + sizeof(buf), v.as_uint()); + if (ec == std::errc{}) { + out.append(buf, ptr); + } + return; + } + if (v.is_double()) { + const double n = v.as_double(); + if (n == 0.0 && std::signbit(n)) { + out += "-0.0"; + } else if (std::isfinite(n) && std::floor(n) == n && + std::abs(n) < 1e15) { out += std::to_string(static_cast(n)); } else { char buf[32]; @@ -1086,6 +1323,49 @@ namespace Void::Json } // namespace Detail + namespace Detail + { + inline std::optional + validate_finite(const Value& v, std::string& path) + { + if (v.is_double() && !std::isfinite(v.as_double())) { + return ConversionError{ + Void::Numeric::Error::NonFinite, + path, + "Json::try_serialize: non-finite floating value"}; + } + if (v.is_array()) { + const auto& arr = v.as_array(); + const std::size_t saved = path.size(); + for (std::size_t i = 0; i < arr.size(); ++i) { + path.resize(saved); + path += '['; + path += std::to_string(i); + path += ']'; + if (auto e = validate_finite(arr[i], path)) { + return e; + } + } + path.resize(saved); + } else if (v.is_object()) { + const auto& obj = v.as_object(); + const std::size_t saved = path.size(); + for (const auto& [k, val] : obj) { + path.resize(saved); + if (!path.empty()) { + path += '.'; + } + path += k; + if (auto e = validate_finite(val, path)) { + return e; + } + } + path.resize(saved); + } + return std::nullopt; + } + } // namespace Detail + [[nodiscard]] inline std::string serialize(const Value& v, SerializeOptions opts = {}) { @@ -1094,4 +1374,14 @@ namespace Void::Json return out; } + [[nodiscard]] inline std::expected + try_serialize(const Value& v, SerializeOptions opts = {}) + { + std::string path; + if (auto err = Detail::validate_finite(v, path)) { + return std::unexpected{std::move(*err)}; + } + return serialize(v, opts); + } + } // namespace Void::Json diff --git a/include/Void/Data/Reflect.hpp b/include/Void/Data/Reflect.hpp index 6c31fcf..668266c 100644 --- a/include/Void/Data/Reflect.hpp +++ b/include/Void/Data/Reflect.hpp @@ -144,7 +144,9 @@ namespace Void::Json return Value{static_cast( static_cast>(v))}; #endif - } else if constexpr (std::is_arithmetic_v) { + } else if constexpr (std::integral) { + return Value{v}; + } else if constexpr (std::floating_point) { return Value{static_cast(v)}; } else if constexpr (std::same_as) { return Value{v}; @@ -287,14 +289,27 @@ namespace Void::Json return std::unexpected{ ReflectError{"", "expected enum number"}}; } - return static_cast( - static_cast>(j.as_number())); + auto raw = Void::Json::number_as>(j); + if (!raw) { + return std::unexpected{ + ReflectError{"", std::move(raw.error().message)}}; + } + return static_cast(*raw); #endif } else if constexpr (std::is_arithmetic_v) { if (!j.is_number()) { return std::unexpected{ReflectError{"", "expected number"}}; } - return static_cast(j.as_number()); + const auto mode = + (std::floating_point && j.is_double()) + ? Void::Numeric::Rounding::nearest_even + : Void::Numeric::Rounding::exact; + auto converted = Void::Json::number_as(j, mode); + if (!converted) { + return std::unexpected{ + ReflectError{"", std::move(converted.error().message)}}; + } + return *converted; } else if constexpr (std::same_as) { if (!j.is_string()) { return std::unexpected{ReflectError{"", "expected string"}}; @@ -584,7 +599,7 @@ namespace Void::Json { auto envelope = object(); envelope.as_object().emplace_back( - "__version", Value{static_cast(StateTraits::version)}); + "__version", Value{StateTraits::version}); envelope.as_object().emplace_back("data", to_json(v)); return envelope; } @@ -604,7 +619,13 @@ namespace Void::Json return std::unexpected{ ReflectError{"__version", "malformed versioned envelope"}}; } - const auto from = static_cast(version->as_number()); + const auto from_expected = + Void::Json::number_as(*version); + if (!from_expected) { + return std::unexpected{ + ReflectError{"__version", "malformed versioned envelope"}}; + } + const auto from = *from_expected; constexpr auto to = StateTraits::version; if (from > to) { return std::unexpected{ReflectError{ diff --git a/src/Data/module.cmake b/src/Data/module.cmake index 1f3f9a3..9582de6 100644 --- a/src/Data/module.cmake +++ b/src/Data/module.cmake @@ -1,3 +1,3 @@ void_add_module(Data - INTERNAL_DEPS Core Time + INTERNAL_DEPS Core Math Time ) diff --git a/tests/standalone/Data/Json.cpp b/tests/standalone/Data/Json.cpp index 249d16d..d4966db 100644 --- a/tests/standalone/Data/Json.cpp +++ b/tests/standalone/Data/Json.cpp @@ -2,6 +2,9 @@ #include #include +#include +#include +#include #include using namespace Void::Json; @@ -235,3 +238,115 @@ Void::Testing::Test json_copy_deep() Value v2 = *v1; Void::Testing::expect_eq(v2.size(), std::size_t{3}); } + +// ── exact 64-bit integers & checked conversions (M-DATA-02) ─────────────────── + +Void::Testing::Test json_exact_64_bit_integers_and_negative_zero_round_trip() +{ + constexpr std::uint64_t p53 = std::uint64_t{1} << 53; + const std::string doc = + R"({"min":-9223372036854775808,"max":9223372036854775807,)" + R"("umax":18446744073709551615,"p53_m1":)" + + std::to_string(p53 - 1ULL) + R"(,"p53":)" + std::to_string(p53) + + R"(,"p53_p1":)" + std::to_string(p53 + 1ULL) + + R"(,"neg_zero_int":-0,"neg_zero_real":-0.0})"; + + const auto v = parse(doc); + Void::Testing::require(v.has_value()); + + Void::Testing::expect(v->find("min")->number_kind() == NumberKind::signed_integer); + Void::Testing::expect_eq(v->find("min")->as_int(), std::numeric_limits::min()); + + Void::Testing::expect(v->find("max")->number_kind() == NumberKind::signed_integer); + Void::Testing::expect_eq(v->find("max")->as_int(), std::numeric_limits::max()); + + Void::Testing::expect(v->find("umax")->number_kind() == NumberKind::unsigned_integer); + Void::Testing::expect_eq(v->find("umax")->as_uint(), std::numeric_limits::max()); + + Void::Testing::expect_eq(v->find("p53_p1")->as_int(), static_cast(p53 + 1ULL)); + + Void::Testing::expect(v->find("neg_zero_int")->is_int()); + Void::Testing::expect_eq(v->find("neg_zero_int")->as_int(), std::int64_t{0}); + + const auto* nzr = v->find("neg_zero_real"); + Void::Testing::expect(nzr->is_double()); + Void::Testing::expect(std::signbit(nzr->as_double())); + + const auto dumped = serialize(*v); + Void::Testing::expect(dumped.find("-9223372036854775808") != std::string::npos); + Void::Testing::expect(dumped.find("18446744073709551615") != std::string::npos); + Void::Testing::expect(dumped.find("\"neg_zero_real\":-0.0") != std::string::npos); + + const auto reparsed = parse(dumped); + Void::Testing::require(reparsed.has_value()); + Void::Testing::expect(*reparsed == *v); +} + +Void::Testing::Test json_rejects_oversized_integers_and_bad_number_grammar() +{ + Void::Testing::expect(!parse("-9223372036854775809").has_value()); + Void::Testing::expect(!parse("18446744073709551616").has_value()); + Void::Testing::expect(!parse("01").has_value()); + Void::Testing::expect(!parse("-").has_value()); + Void::Testing::expect(!parse("1.").has_value()); + Void::Testing::expect(!parse("1e").has_value()); + + const std::string huge(257, '9'); + Void::Testing::expect(!parse(huge).has_value()); +} + +Void::Testing::Test json_exact_equality_and_number_as_checked_conversion() +{ + constexpr std::uint64_t p53_p1 = (std::uint64_t{1} << 53) + 1ULL; + const Value exact_i{p53_p1}; + const Value rounded_d{static_cast(std::uint64_t{1} << 53)}; + Void::Testing::expect(!(exact_i == rounded_d)); + Void::Testing::expect(Value{std::numeric_limits::max()} != + Value{static_cast(std::numeric_limits::max())}); + Void::Testing::expect(Value{std::int64_t{42}} == Value{std::uint64_t{42}}); + Void::Testing::expect(Value{std::int64_t{42}} == Value{42.0}); + + // number_as defaults to Rounding::exact. + const auto exact_fail = number_as(exact_i, Void::Numeric::Rounding::exact, "p53_p1"); + Void::Testing::expect(!exact_fail.has_value()); + Void::Testing::expect(exact_fail.error().numeric_error == Void::Numeric::Error::Inexact); + Void::Testing::expect_eq(exact_fail.error().path, std::string{"p53_p1"}); + + const auto lossy_ok = number_as(exact_i, Void::Numeric::Rounding::nearest_even); + Void::Testing::expect(lossy_ok.has_value()); + + const auto umax_to_i64 = number_as(Value{std::numeric_limits::max()}); + Void::Testing::expect(!umax_to_i64.has_value()); + Void::Testing::expect(umax_to_i64.error().numeric_error == Void::Numeric::Error::Overflow); + + const auto neg_to_u64 = number_as(Value{std::int64_t{-1}}); + Void::Testing::expect(!neg_to_u64.has_value()); + Void::Testing::expect(neg_to_u64.error().numeric_error == Void::Numeric::Error::Underflow); + + // try_serialize rejects non-finite floats with path diagnostics. + Value::Object_t obj; + obj.emplace_back("ok", Value{1}); + obj.emplace_back("bad", Value{std::numeric_limits::quiet_NaN()}); + const auto ser_err = try_serialize(Value{std::move(obj)}); + Void::Testing::expect(!ser_err.has_value()); + Void::Testing::expect(ser_err.error().numeric_error == Void::Numeric::Error::NonFinite); + Void::Testing::expect_eq(ser_err.error().path, std::string{"bad"}); +} + +Void::Testing::Test json_sax_typed_integer_callbacks_preserve_64_bit_endpoints() +{ + std::int64_t seen_i{0}; + std::uint64_t seen_u{0}; + double seen_d{0.0}; + SaxHandler h; + h.on_int = [&](std::int64_t v) { seen_i = v; }; + h.on_uint = [&](std::uint64_t v) { seen_u = v; }; + h.on_number = [&](double v) { seen_d = v; }; + + const auto err = parse_sax( + R"([-9223372036854775808,18446744073709551615,2.5])", h); + Void::Testing::expect(!err.has_value()); + Void::Testing::expect_eq(seen_i, std::numeric_limits::min()); + Void::Testing::expect_eq(seen_u, std::numeric_limits::max()); + Void::Testing::expect_eq(seen_d, 2.5); +} diff --git a/tests/standalone/Data/Reflect.cpp b/tests/standalone/Data/Reflect.cpp index 84c8152..62919a8 100644 --- a/tests/standalone/Data/Reflect.cpp +++ b/tests/standalone/Data/Reflect.cpp @@ -2,6 +2,8 @@ #include #include +#include +#include #include #include #include @@ -242,4 +244,51 @@ Void::Testing::Test reflect_member_customization_wins() Void::Testing::expect_eq(back->doubled, 10); } +namespace +{ + struct WideNumbers + { + std::int64_t smin = 0; + std::int64_t smax = 0; + std::uint64_t umax = 0; + std::uint64_t p53_p1 = 0; + float ratio = 0.0F; + double dval = 0.0; + }; +} // namespace + +Void::Testing::Test reflect_exact_64_bit_integers_and_rejects_inexact() +{ + constexpr std::uint64_t p53_p1 = (std::uint64_t{1} << 53) + 1ULL; + const WideNumbers orig{ + .smin = std::numeric_limits::min(), + .smax = std::numeric_limits::max(), + .umax = std::numeric_limits::max(), + .p53_p1 = p53_p1, + .ratio = 0.1F, + .dval = 1.25}; + + const auto json_str = Void::Json::serialize(Void::Json::to_json(orig)); + const auto parsed = Void::Json::parse(json_str); + Void::Testing::require(parsed.has_value()); + + const auto round = Void::Json::from_json(*parsed); + Void::Testing::require(round.has_value()); + Void::Testing::expect_eq(round->smin, orig.smin); + Void::Testing::expect_eq(round->smax, orig.smax); + Void::Testing::expect_eq(round->umax, orig.umax); + Void::Testing::expect_eq(round->p53_p1, orig.p53_p1); + Void::Testing::expect_eq(round->dval, 1.25); + + // Reject fractional value into integer field & inexact 2^53+1 into double field. + const auto frac_err = Void::Json::parse(R"({"smin": 1.5})"); + Void::Testing::require(frac_err.has_value()); + Void::Testing::expect(!Void::Json::from_json(*frac_err).has_value()); + + const auto inexact_dbl = Void::Json::parse( + std::string{R"({"dval": )"} + std::to_string(p53_p1) + "}"); + Void::Testing::require(inexact_dbl.has_value()); + Void::Testing::expect(!Void::Json::from_json(*inexact_dbl).has_value()); +} + #endif // VOID_HAS_JSON_REFLECTION