feat(Data): add exact 64-bit signed/unsigned JSON numbers and checked conversion (M-DATA-02)
This commit is contained in:
1 parent
eacb8a4602
commit
692f5ba5b2
6 files changed
+530
-47
No files matched your search
@@ -14,6 +14,7 @@
|
|||||||
#include <cctype>
|
#include <cctype>
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <cmath>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <format>
|
#include <format>
|
||||||
@@ -404,9 +405,16 @@ namespace Void::Configuration
|
|||||||
} else if (v.is_bool()) {
|
} else if (v.is_bool()) {
|
||||||
cfg.values_.insert_or_assign(prefix, v.as_bool() ? "true"
|
cfg.values_.insert_or_assign(prefix, v.as_bool() ? "true"
|
||||||
: "false");
|
: "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()) {
|
} else if (v.is_number()) {
|
||||||
const double d = v.as_number();
|
const double d = v.as_number();
|
||||||
if (d == static_cast<double>(static_cast<long long>(d))) {
|
if (std::isfinite(d) && !(d == 0.0 && std::signbit(d)) &&
|
||||||
|
std::floor(d) == d && std::abs(d) < 1e15) {
|
||||||
cfg.values_.insert_or_assign(
|
cfg.values_.insert_or_assign(
|
||||||
prefix, std::to_string(static_cast<long long>(d)));
|
prefix, std::to_string(static_cast<long long>(d)));
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+329
-39
@@ -1,10 +1,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <Void/Math/Numeric.hpp>
|
||||||
|
|
||||||
#include <charconv>
|
#include <charconv>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <concepts>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <expected>
|
#include <expected>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <limits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
@@ -23,6 +27,18 @@ namespace Void::Json
|
|||||||
|
|
||||||
class Value;
|
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 {
|
struct ParseError {
|
||||||
std::size_t line{1}, col{1};
|
std::size_t line{1}, col{1};
|
||||||
std::string message;
|
std::string message;
|
||||||
@@ -40,7 +56,9 @@ namespace Void::Json
|
|||||||
using Data =
|
using Data =
|
||||||
std::variant<std::monostate, // null
|
std::variant<std::monostate, // null
|
||||||
bool, // bool
|
bool, // bool
|
||||||
double, // number
|
std::int64_t, // signed integer
|
||||||
|
std::uint64_t, // unsigned integer
|
||||||
|
double, // real
|
||||||
std::string, // string
|
std::string, // string
|
||||||
std::unique_ptr<Array_t>, // array (boxed to avoid
|
std::unique_ptr<Array_t>, // array (boxed to avoid
|
||||||
// incomplete-type
|
// incomplete-type
|
||||||
@@ -74,9 +92,14 @@ namespace Void::Json
|
|||||||
Value() noexcept = default;
|
Value() noexcept = default;
|
||||||
Value(std::nullptr_t) noexcept {}
|
Value(std::nullptr_t) noexcept {}
|
||||||
Value(bool b) noexcept : data_{b} {}
|
Value(bool b) noexcept : data_{b} {}
|
||||||
Value(double n) noexcept : data_{n} {}
|
template <std::signed_integral T>
|
||||||
Value(int n) noexcept : data_{static_cast<double>(n)} {}
|
requires(!std::same_as<std::remove_cv_t<T>, bool>)
|
||||||
Value(long long n) noexcept : data_{static_cast<double>(n)} {}
|
Value(T n) noexcept : data_{static_cast<std::int64_t>(n)} {}
|
||||||
|
template <std::unsigned_integral T>
|
||||||
|
requires(!std::same_as<std::remove_cv_t<T>, bool>)
|
||||||
|
Value(T n) noexcept : data_{static_cast<std::uint64_t>(n)} {}
|
||||||
|
template <std::floating_point T>
|
||||||
|
Value(T n) noexcept : data_{static_cast<double>(n)} {}
|
||||||
Value(std::string s) : data_{std::move(s)} {}
|
Value(std::string s) : data_{std::move(s)} {}
|
||||||
Value(std::string_view s) : data_{std::string{s}} {}
|
Value(std::string_view s) : data_{std::string{s}} {}
|
||||||
Value(const char* 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
|
[[nodiscard]] bool is_null() const noexcept
|
||||||
{
|
{
|
||||||
return data_.index() == 0;
|
return std::holds_alternative<std::monostate>(data_);
|
||||||
}
|
}
|
||||||
[[nodiscard]] bool is_bool() const noexcept
|
[[nodiscard]] bool is_bool() const noexcept
|
||||||
{
|
{
|
||||||
return data_.index() == 1;
|
return std::holds_alternative<bool>(data_);
|
||||||
|
}
|
||||||
|
[[nodiscard]] bool is_int() const noexcept
|
||||||
|
{
|
||||||
|
return std::holds_alternative<std::int64_t>(data_);
|
||||||
|
}
|
||||||
|
[[nodiscard]] bool is_uint() const noexcept
|
||||||
|
{
|
||||||
|
return std::holds_alternative<std::uint64_t>(data_);
|
||||||
|
}
|
||||||
|
[[nodiscard]] bool is_double() const noexcept
|
||||||
|
{
|
||||||
|
return std::holds_alternative<double>(data_);
|
||||||
|
}
|
||||||
|
[[nodiscard]] bool is_integer() const noexcept
|
||||||
|
{
|
||||||
|
return is_int() || is_uint();
|
||||||
}
|
}
|
||||||
[[nodiscard]] bool is_number() const noexcept
|
[[nodiscard]] bool is_number() const noexcept
|
||||||
{
|
{
|
||||||
return data_.index() == 2;
|
return is_int() || is_uint() || is_double();
|
||||||
|
}
|
||||||
|
[[nodiscard]] std::optional<NumberKind> 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
|
[[nodiscard]] bool is_string() const noexcept
|
||||||
{
|
{
|
||||||
return data_.index() == 3;
|
return std::holds_alternative<std::string>(data_);
|
||||||
}
|
}
|
||||||
[[nodiscard]] bool is_array() const noexcept
|
[[nodiscard]] bool is_array() const noexcept
|
||||||
{
|
{
|
||||||
return data_.index() == 4;
|
return std::holds_alternative<std::unique_ptr<Array_t>>(data_);
|
||||||
}
|
}
|
||||||
[[nodiscard]] bool is_object() const noexcept
|
[[nodiscard]] bool is_object() const noexcept
|
||||||
{
|
{
|
||||||
return data_.index() == 5;
|
return std::holds_alternative<std::unique_ptr<Object_t>>(data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── typed access (throws std::bad_variant_access on wrong type)
|
// ── 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<bool>(data_); }
|
[[nodiscard]] bool as_bool() const { return std::get<bool>(data_); }
|
||||||
[[nodiscard]] double as_number() const
|
[[nodiscard]] double as_number() const
|
||||||
{
|
{
|
||||||
|
if (const auto* i = std::get_if<std::int64_t>(&data_)) {
|
||||||
|
return static_cast<double>(*i);
|
||||||
|
}
|
||||||
|
if (const auto* u = std::get_if<std::uint64_t>(&data_)) {
|
||||||
|
return static_cast<double>(*u);
|
||||||
|
}
|
||||||
return std::get<double>(data_);
|
return std::get<double>(data_);
|
||||||
}
|
}
|
||||||
[[nodiscard]] std::int64_t as_int() const
|
[[nodiscard]] std::int64_t as_int() const
|
||||||
{
|
{
|
||||||
return static_cast<std::int64_t>(as_number());
|
if (const auto* i = std::get_if<std::int64_t>(&data_)) {
|
||||||
|
return *i;
|
||||||
|
}
|
||||||
|
if (const auto* u = std::get_if<std::uint64_t>(&data_)) {
|
||||||
|
return static_cast<std::int64_t>(*u);
|
||||||
|
}
|
||||||
|
return static_cast<std::int64_t>(std::get<double>(data_));
|
||||||
}
|
}
|
||||||
|
[[nodiscard]] std::uint64_t as_uint() const
|
||||||
|
{
|
||||||
|
if (const auto* u = std::get_if<std::uint64_t>(&data_)) {
|
||||||
|
return *u;
|
||||||
|
}
|
||||||
|
if (const auto* i = std::get_if<std::int64_t>(&data_)) {
|
||||||
|
return static_cast<std::uint64_t>(*i);
|
||||||
|
}
|
||||||
|
return static_cast<std::uint64_t>(std::get<double>(data_));
|
||||||
|
}
|
||||||
|
[[nodiscard]] double as_double() const { return as_number(); }
|
||||||
[[nodiscard]] const std::string& as_string() const
|
[[nodiscard]] const std::string& as_string() const
|
||||||
{
|
{
|
||||||
return std::get<std::string>(data_);
|
return std::get<std::string>(data_);
|
||||||
@@ -172,6 +241,42 @@ namespace Void::Json
|
|||||||
}
|
}
|
||||||
return as_number();
|
return as_number();
|
||||||
}
|
}
|
||||||
|
[[nodiscard]] std::optional<std::int64_t> get_int() const noexcept
|
||||||
|
{
|
||||||
|
if (const auto* i = std::get_if<std::int64_t>(&data_)) {
|
||||||
|
return *i;
|
||||||
|
}
|
||||||
|
if (const auto* u = std::get_if<std::uint64_t>(&data_)) {
|
||||||
|
auto c = Void::Numeric::checked_cast<std::int64_t>(*u);
|
||||||
|
return c ? std::optional<std::int64_t>{*c} : std::nullopt;
|
||||||
|
}
|
||||||
|
if (const auto* d = std::get_if<double>(&data_)) {
|
||||||
|
auto c = Void::Numeric::checked_cast<std::int64_t>(
|
||||||
|
*d, Void::Numeric::Rounding::exact);
|
||||||
|
return c ? std::optional<std::int64_t>{*c} : std::nullopt;
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
[[nodiscard]] std::optional<std::uint64_t> get_uint() const noexcept
|
||||||
|
{
|
||||||
|
if (const auto* u = std::get_if<std::uint64_t>(&data_)) {
|
||||||
|
return *u;
|
||||||
|
}
|
||||||
|
if (const auto* i = std::get_if<std::int64_t>(&data_)) {
|
||||||
|
auto c = Void::Numeric::checked_cast<std::uint64_t>(*i);
|
||||||
|
return c ? std::optional<std::uint64_t>{*c} : std::nullopt;
|
||||||
|
}
|
||||||
|
if (const auto* d = std::get_if<double>(&data_)) {
|
||||||
|
auto c = Void::Numeric::checked_cast<std::uint64_t>(
|
||||||
|
*d, Void::Numeric::Rounding::exact);
|
||||||
|
return c ? std::optional<std::uint64_t>{*c} : std::nullopt;
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
[[nodiscard]] std::optional<double> get_double() const noexcept
|
||||||
|
{
|
||||||
|
return get_number();
|
||||||
|
}
|
||||||
[[nodiscard]] std::optional<std::string_view>
|
[[nodiscard]] std::optional<std::string_view>
|
||||||
get_string() const noexcept
|
get_string() const noexcept
|
||||||
{
|
{
|
||||||
@@ -357,8 +462,40 @@ namespace Void::Json
|
|||||||
}
|
}
|
||||||
[[nodiscard]] bool empty() const noexcept { return size() == 0; }
|
[[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<std::uint64_t>(a.as_int()) == b.as_uint();
|
||||||
|
}
|
||||||
|
const auto c = Void::Numeric::checked_cast<std::int64_t>(
|
||||||
|
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<std::uint64_t>(
|
||||||
|
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
|
[[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()) {
|
if (data_.index() != o.data_.index()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -368,23 +505,49 @@ namespace Void::Json
|
|||||||
if (is_object()) {
|
if (is_object()) {
|
||||||
return as_object() == o.as_object();
|
return as_object() == o.as_object();
|
||||||
}
|
}
|
||||||
// For monostate, bool, double, string — variant equality works
|
if (is_null()) {
|
||||||
return std::visit(
|
return true;
|
||||||
[&](const auto& lhs) -> bool {
|
}
|
||||||
using T = std::decay_t<decltype(lhs)>;
|
if (is_bool()) {
|
||||||
if constexpr (std::is_same_v<
|
return as_bool() == o.as_bool();
|
||||||
T, std::unique_ptr<Array_t>> ||
|
}
|
||||||
std::is_same_v<
|
if (is_string()) {
|
||||||
T, std::unique_ptr<Object_t>>) {
|
return as_string() == o.as_string();
|
||||||
return false; // unreachable (handled above)
|
}
|
||||||
} else {
|
return false;
|
||||||
return lhs == std::get<T>(o.data_);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
data_);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <Void::Numeric::ArithmeticScalar T>
|
||||||
|
[[nodiscard]] inline std::expected<T, ConversionError>
|
||||||
|
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<T>(v.as_int(), mode);
|
||||||
|
if (!r) return make_conv_err(r.error());
|
||||||
|
return *r;
|
||||||
|
}
|
||||||
|
if (v.is_uint()) {
|
||||||
|
auto r = Void::Numeric::checked_cast<T>(v.as_uint(), mode);
|
||||||
|
if (!r) return make_conv_err(r.error());
|
||||||
|
return *r;
|
||||||
|
}
|
||||||
|
if (v.is_double()) {
|
||||||
|
auto r = Void::Numeric::checked_cast<T>(v.as_double(), mode);
|
||||||
|
if (!r) return make_conv_err(r.error());
|
||||||
|
return *r;
|
||||||
|
}
|
||||||
|
return make_conv_err(Void::Numeric::Error::InvalidInput);
|
||||||
|
}
|
||||||
|
|
||||||
// ── static factories
|
// ── static factories
|
||||||
// ──────────────────────────────────────────────────────────
|
// ──────────────────────────────────────────────────────────
|
||||||
[[nodiscard]] inline Value array(Value::Array_t a = {})
|
[[nodiscard]] inline Value array(Value::Array_t a = {})
|
||||||
@@ -497,43 +660,90 @@ namespace Void::Json
|
|||||||
std::expected<Value, ParseError> parse_number()
|
std::expected<Value, ParseError> parse_number()
|
||||||
{
|
{
|
||||||
const std::size_t start = pos;
|
const std::size_t start = pos;
|
||||||
if (peek() == '-') {
|
const bool neg = (peek() == '-');
|
||||||
|
if (neg) {
|
||||||
advance();
|
advance();
|
||||||
}
|
}
|
||||||
if (peek() == '0') {
|
if (peek() == '0') {
|
||||||
advance();
|
advance();
|
||||||
} else {
|
if (peek() >= '0' && peek() <= '9') {
|
||||||
while (peek() >= '1' && peek() <= '9') {
|
return std::unexpected{
|
||||||
advance();
|
err("leading zero in numeric literal")};
|
||||||
}
|
}
|
||||||
|
} else if (peek() >= '1' && peek() <= '9') {
|
||||||
|
advance();
|
||||||
while (peek() >= '0' && peek() <= '9') {
|
while (peek() >= '0' && peek() <= '9') {
|
||||||
advance();
|
advance();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return std::unexpected{err("expected digit in number")};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool is_real = false;
|
||||||
if (peek() == '.') {
|
if (peek() == '.') {
|
||||||
|
is_real = true;
|
||||||
advance();
|
advance();
|
||||||
|
if (!(peek() >= '0' && peek() <= '9')) {
|
||||||
|
return std::unexpected{
|
||||||
|
err("expected digits after '.'")};
|
||||||
|
}
|
||||||
while (peek() >= '0' && peek() <= '9') {
|
while (peek() >= '0' && peek() <= '9') {
|
||||||
advance();
|
advance();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (peek() == 'e' || peek() == 'E') {
|
if (peek() == 'e' || peek() == 'E') {
|
||||||
|
is_real = true;
|
||||||
advance();
|
advance();
|
||||||
if (peek() == '+' || peek() == '-') {
|
if (peek() == '+' || peek() == '-') {
|
||||||
advance();
|
advance();
|
||||||
}
|
}
|
||||||
|
if (!(peek() >= '0' && peek() <= '9')) {
|
||||||
|
return std::unexpected{
|
||||||
|
err("expected exponent digits")};
|
||||||
|
}
|
||||||
while (peek() >= '0' && peek() <= '9') {
|
while (peek() >= '0' && peek() <= '9') {
|
||||||
advance();
|
advance();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string_view raw = src.substr(start, pos - start);
|
const std::string_view raw = src.substr(start, pos - start);
|
||||||
double val = 0.0;
|
if (raw.size() > Void::Numeric::kMaxNumericTokenBytes) {
|
||||||
const auto [ptr, ec] = std::from_chars(
|
|
||||||
raw.data(), raw.data() + raw.size(), val);
|
|
||||||
if (ec != std::errc{}) {
|
|
||||||
return std::unexpected{
|
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<double>(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<std::int64_t>(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<std::int64_t>(raw)) {
|
||||||
|
return Value{*s};
|
||||||
|
}
|
||||||
|
if (const auto u =
|
||||||
|
Void::Numeric::parse_integer<std::uint64_t>(raw)) {
|
||||||
|
return Value{*u};
|
||||||
|
}
|
||||||
|
return std::unexpected{
|
||||||
|
err("unsigned integer out of range: " +
|
||||||
|
std::string{raw})};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::expected<std::string, ParseError> parse_string_raw()
|
std::expected<std::string, ParseError> parse_string_raw()
|
||||||
@@ -780,6 +990,8 @@ namespace Void::Json
|
|||||||
std::function<void()> on_object_end;
|
std::function<void()> on_object_end;
|
||||||
std::function<void()> on_array_begin;
|
std::function<void()> on_array_begin;
|
||||||
std::function<void()> on_array_end;
|
std::function<void()> on_array_end;
|
||||||
|
std::function<void(std::int64_t)> on_int{};
|
||||||
|
std::function<void(std::uint64_t)> on_uint{};
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace Detail
|
namespace Detail
|
||||||
@@ -846,7 +1058,11 @@ namespace Void::Json
|
|||||||
if (!v) {
|
if (!v) {
|
||||||
return v.error();
|
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());
|
h.on_number(v->as_number());
|
||||||
}
|
}
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
@@ -1016,9 +1232,30 @@ namespace Void::Json
|
|||||||
out += v.as_bool() ? "true" : "false";
|
out += v.as_bool() ? "true" : "false";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (v.is_number()) {
|
if (v.is_int()) {
|
||||||
const double n = v.as_number();
|
char buf[32];
|
||||||
if (std::floor(n) == n && std::abs(n) < 1e15) {
|
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<std::int64_t>(n));
|
out += std::to_string(static_cast<std::int64_t>(n));
|
||||||
} else {
|
} else {
|
||||||
char buf[32];
|
char buf[32];
|
||||||
@@ -1086,6 +1323,49 @@ namespace Void::Json
|
|||||||
|
|
||||||
} // namespace Detail
|
} // namespace Detail
|
||||||
|
|
||||||
|
namespace Detail
|
||||||
|
{
|
||||||
|
inline std::optional<ConversionError>
|
||||||
|
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,
|
[[nodiscard]] inline std::string serialize(const Value& v,
|
||||||
SerializeOptions opts = {})
|
SerializeOptions opts = {})
|
||||||
{
|
{
|
||||||
@@ -1094,4 +1374,14 @@ namespace Void::Json
|
|||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] inline std::expected<std::string, ConversionError>
|
||||||
|
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
|
} // namespace Void::Json
|
||||||
@@ -144,7 +144,9 @@ namespace Void::Json
|
|||||||
return Value{static_cast<double>(
|
return Value{static_cast<double>(
|
||||||
static_cast<std::underlying_type_t<T>>(v))};
|
static_cast<std::underlying_type_t<T>>(v))};
|
||||||
#endif
|
#endif
|
||||||
} else if constexpr (std::is_arithmetic_v<T>) {
|
} else if constexpr (std::integral<T>) {
|
||||||
|
return Value{v};
|
||||||
|
} else if constexpr (std::floating_point<T>) {
|
||||||
return Value{static_cast<double>(v)};
|
return Value{static_cast<double>(v)};
|
||||||
} else if constexpr (std::same_as<T, std::string>) {
|
} else if constexpr (std::same_as<T, std::string>) {
|
||||||
return Value{v};
|
return Value{v};
|
||||||
@@ -287,14 +289,27 @@ namespace Void::Json
|
|||||||
return std::unexpected{
|
return std::unexpected{
|
||||||
ReflectError{"", "expected enum number"}};
|
ReflectError{"", "expected enum number"}};
|
||||||
}
|
}
|
||||||
return static_cast<T>(
|
auto raw = Void::Json::number_as<std::underlying_type_t<T>>(j);
|
||||||
static_cast<std::underlying_type_t<T>>(j.as_number()));
|
if (!raw) {
|
||||||
|
return std::unexpected{
|
||||||
|
ReflectError{"", std::move(raw.error().message)}};
|
||||||
|
}
|
||||||
|
return static_cast<T>(*raw);
|
||||||
#endif
|
#endif
|
||||||
} else if constexpr (std::is_arithmetic_v<T>) {
|
} else if constexpr (std::is_arithmetic_v<T>) {
|
||||||
if (!j.is_number()) {
|
if (!j.is_number()) {
|
||||||
return std::unexpected{ReflectError{"", "expected number"}};
|
return std::unexpected{ReflectError{"", "expected number"}};
|
||||||
}
|
}
|
||||||
return static_cast<T>(j.as_number());
|
const auto mode =
|
||||||
|
(std::floating_point<T> && j.is_double())
|
||||||
|
? Void::Numeric::Rounding::nearest_even
|
||||||
|
: Void::Numeric::Rounding::exact;
|
||||||
|
auto converted = Void::Json::number_as<T>(j, mode);
|
||||||
|
if (!converted) {
|
||||||
|
return std::unexpected{
|
||||||
|
ReflectError{"", std::move(converted.error().message)}};
|
||||||
|
}
|
||||||
|
return *converted;
|
||||||
} else if constexpr (std::same_as<T, std::string>) {
|
} else if constexpr (std::same_as<T, std::string>) {
|
||||||
if (!j.is_string()) {
|
if (!j.is_string()) {
|
||||||
return std::unexpected{ReflectError{"", "expected string"}};
|
return std::unexpected{ReflectError{"", "expected string"}};
|
||||||
@@ -584,7 +599,7 @@ namespace Void::Json
|
|||||||
{
|
{
|
||||||
auto envelope = object();
|
auto envelope = object();
|
||||||
envelope.as_object().emplace_back(
|
envelope.as_object().emplace_back(
|
||||||
"__version", Value{static_cast<double>(StateTraits<T>::version)});
|
"__version", Value{StateTraits<T>::version});
|
||||||
envelope.as_object().emplace_back("data", to_json(v));
|
envelope.as_object().emplace_back("data", to_json(v));
|
||||||
return envelope;
|
return envelope;
|
||||||
}
|
}
|
||||||
@@ -604,7 +619,13 @@ namespace Void::Json
|
|||||||
return std::unexpected{
|
return std::unexpected{
|
||||||
ReflectError{"__version", "malformed versioned envelope"}};
|
ReflectError{"__version", "malformed versioned envelope"}};
|
||||||
}
|
}
|
||||||
const auto from = static_cast<std::uint32_t>(version->as_number());
|
const auto from_expected =
|
||||||
|
Void::Json::number_as<std::uint32_t>(*version);
|
||||||
|
if (!from_expected) {
|
||||||
|
return std::unexpected{
|
||||||
|
ReflectError{"__version", "malformed versioned envelope"}};
|
||||||
|
}
|
||||||
|
const auto from = *from_expected;
|
||||||
constexpr auto to = StateTraits<T>::version;
|
constexpr auto to = StateTraits<T>::version;
|
||||||
if (from > to) {
|
if (from > to) {
|
||||||
return std::unexpected{ReflectError{
|
return std::unexpected{ReflectError{
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
void_add_module(Data
|
void_add_module(Data
|
||||||
INTERNAL_DEPS Core Time
|
INTERNAL_DEPS Core Math Time
|
||||||
)
|
)
|
||||||
@@ -2,6 +2,9 @@
|
|||||||
#include <Void/Testing/Assert.hpp>
|
#include <Void/Testing/Assert.hpp>
|
||||||
#include <Void/Testing/Test.hpp>
|
#include <Void/Testing/Test.hpp>
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <limits>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
using namespace Void::Json;
|
using namespace Void::Json;
|
||||||
@@ -235,3 +238,115 @@ Void::Testing::Test json_copy_deep()
|
|||||||
Value v2 = *v1;
|
Value v2 = *v1;
|
||||||
Void::Testing::expect_eq(v2.size(), std::size_t{3});
|
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<std::int64_t>::min());
|
||||||
|
|
||||||
|
Void::Testing::expect(v->find("max")->number_kind() == NumberKind::signed_integer);
|
||||||
|
Void::Testing::expect_eq(v->find("max")->as_int(), std::numeric_limits<std::int64_t>::max());
|
||||||
|
|
||||||
|
Void::Testing::expect(v->find("umax")->number_kind() == NumberKind::unsigned_integer);
|
||||||
|
Void::Testing::expect_eq(v->find("umax")->as_uint(), std::numeric_limits<std::uint64_t>::max());
|
||||||
|
|
||||||
|
Void::Testing::expect_eq(v->find("p53_p1")->as_int(), static_cast<std::int64_t>(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<double>(std::uint64_t{1} << 53)};
|
||||||
|
Void::Testing::expect(!(exact_i == rounded_d));
|
||||||
|
Void::Testing::expect(Value{std::numeric_limits<std::uint64_t>::max()} !=
|
||||||
|
Value{static_cast<double>(std::numeric_limits<std::uint64_t>::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<double>(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<double>(exact_i, Void::Numeric::Rounding::nearest_even);
|
||||||
|
Void::Testing::expect(lossy_ok.has_value());
|
||||||
|
|
||||||
|
const auto umax_to_i64 = number_as<std::int64_t>(Value{std::numeric_limits<std::uint64_t>::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<std::uint64_t>(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<double>::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<std::int64_t>::min());
|
||||||
|
Void::Testing::expect_eq(seen_u, std::numeric_limits<std::uint64_t>::max());
|
||||||
|
Void::Testing::expect_eq(seen_d, 2.5);
|
||||||
|
}
|
||||||
@@ -2,6 +2,8 @@
|
|||||||
#include <Void/Testing/Assert.hpp>
|
#include <Void/Testing/Assert.hpp>
|
||||||
#include <Void/Testing/Test.hpp>
|
#include <Void/Testing/Test.hpp>
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <limits>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -242,4 +244,51 @@ Void::Testing::Test reflect_member_customization_wins()
|
|||||||
Void::Testing::expect_eq(back->doubled, 10);
|
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<std::int64_t>::min(),
|
||||||
|
.smax = std::numeric_limits<std::int64_t>::max(),
|
||||||
|
.umax = std::numeric_limits<std::uint64_t>::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<WideNumbers>(*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<WideNumbers>(*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<WideNumbers>(*inexact_dbl).has_value());
|
||||||
|
}
|
||||||
|
|
||||||
#endif // VOID_HAS_JSON_REFLECTION
|
#endif // VOID_HAS_JSON_REFLECTION
|
||||||
Reference in new issue
Block a user