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
@@ -144,7 +144,9 @@ namespace Void::Json
|
||||
return Value{static_cast<double>(
|
||||
static_cast<std::underlying_type_t<T>>(v))};
|
||||
#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)};
|
||||
} else if constexpr (std::same_as<T, std::string>) {
|
||||
return Value{v};
|
||||
@@ -287,14 +289,27 @@ namespace Void::Json
|
||||
return std::unexpected{
|
||||
ReflectError{"", "expected enum number"}};
|
||||
}
|
||||
return static_cast<T>(
|
||||
static_cast<std::underlying_type_t<T>>(j.as_number()));
|
||||
auto raw = Void::Json::number_as<std::underlying_type_t<T>>(j);
|
||||
if (!raw) {
|
||||
return std::unexpected{
|
||||
ReflectError{"", std::move(raw.error().message)}};
|
||||
}
|
||||
return static_cast<T>(*raw);
|
||||
#endif
|
||||
} else if constexpr (std::is_arithmetic_v<T>) {
|
||||
if (!j.is_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>) {
|
||||
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<double>(StateTraits<T>::version)});
|
||||
"__version", Value{StateTraits<T>::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<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;
|
||||
if (from > to) {
|
||||
return std::unexpected{ReflectError{
|
||||
|
||||
Reference in new issue
Block a user