From 4c60e7baa6f394da4dcc17b335756274fe4db1d5 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Sat, 28 Nov 2026 12:00:45 +0100 Subject: [PATCH 1/2] Add opt-in rfl::Int64AsString processor for 64-bit integers as strings --- docs/content/processors.md | 20 ++++ include/rfl.hpp | 1 + include/rfl/Int64AsString.hpp | 22 +++++ include/rfl/internal/int64_as_string_v.hpp | 32 +++++++ include/rfl/parsing/ParserBasicType.hpp | 59 +++++++++++- tests/json/test_int64_as_string.cpp | 102 +++++++++++++++++++++ 6 files changed, 232 insertions(+), 4 deletions(-) create mode 100644 include/rfl/Int64AsString.hpp create mode 100644 include/rfl/internal/int64_as_string_v.hpp create mode 100644 tests/json/test_int64_as_string.cpp diff --git a/docs/content/processors.md b/docs/content/processors.md index a431bb4e6..865b0557f 100644 --- a/docs/content/processors.md +++ b/docs/content/processors.md @@ -514,6 +514,26 @@ The resulting JSON string looks like this: {"FirstName":"Homer","LastName":"Simpson","Age":45} ``` +## `rfl::Int64AsString` + +Passing `rfl::Int64AsString` serializes and deserializes 64-bit (and wider) integer fields (`int64_t`, `uint64_t`, etc.) as base-10 strings while keeping narrower integers (`int32_t`, `uint32_t`, etc.) as numbers. This lets you share one plain C++ struct across binary/columnar formats (CBOR, MsgPack, Parquet, Arrow) and JavaScript/Protobuf-JSON APIs that lose integer precision above `2^53 - 1`. + +```cpp +struct Trade { + int64_t trade_id; + uint64_t sequence; + int32_t quantity; +}; + +const auto json_string = rfl::json::write( + Trade{.trade_id = 9007199254740993LL, .sequence = 18446744073709551615ULL, .quantity = 5}); +// {"trade_id":"9007199254740993","sequence":"18446744073709551615","quantity":5} + +const auto trade = rfl::json::read(json_string).value(); +``` + +`rfl::json::to_schema()` emits `"type":"string"` for 64-bit integer fields and `"type":"integer"` for narrower integers. + ## Combining several processors You can combine several processors: diff --git a/include/rfl.hpp b/include/rfl.hpp index 432fff2f6..186585ab9 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -30,6 +30,7 @@ #include "rfl/Flatten.hpp" #include "rfl/Generic.hpp" #include "rfl/Hex.hpp" +#include "rfl/Int64AsString.hpp" #include "rfl/Literal.hpp" #include "rfl/NamedTuple.hpp" #include "rfl/NoExtraFields.hpp" diff --git a/include/rfl/Int64AsString.hpp b/include/rfl/Int64AsString.hpp new file mode 100644 index 000000000..9dccb44ec --- /dev/null +++ b/include/rfl/Int64AsString.hpp @@ -0,0 +1,22 @@ +#ifndef RFL_INT64ASSTRING_HPP_ +#define RFL_INT64ASSTRING_HPP_ + +namespace rfl { + +/// This is a 'fake' processor - it doesn't do much by itself, but its +/// inclusion instructs parsers to serialize and deserialize 64-bit (and wider) +/// integer types as base-10 strings instead of format numbers. +/// Useful when sharing one pure C++ struct (plain int64_t / uint64_t) across +/// binary/columnar formats (Arrow, CBOR, MsgPack, Parquet) and JS/Protobuf-JSON +/// APIs that lose integer precision above 2^53 - 1. +struct Int64AsString { + public: + template + static auto process(auto&& _named_tuple) { + return _named_tuple; + } +}; + +} // namespace rfl + +#endif diff --git a/include/rfl/internal/int64_as_string_v.hpp b/include/rfl/internal/int64_as_string_v.hpp new file mode 100644 index 000000000..b30ab6a6d --- /dev/null +++ b/include/rfl/internal/int64_as_string_v.hpp @@ -0,0 +1,32 @@ +#ifndef RFL_INTERNAL_INT64ASSTRING_HPP_ +#define RFL_INTERNAL_INT64ASSTRING_HPP_ + +#include + +#include "../Int64AsString.hpp" +#include "../Processors.hpp" + +namespace rfl::internal { + +template +class int64_as_string; + +template +class int64_as_string : public std::false_type {}; + +template <> +class int64_as_string : public std::true_type {}; + +template +struct int64_as_string> { + static constexpr bool value = + (int64_as_string::value || ... || int64_as_string::value); +}; + +template +constexpr bool int64_as_string_v = + int64_as_string>>::value; + +} // namespace rfl::internal + +#endif diff --git a/include/rfl/parsing/ParserBasicType.hpp b/include/rfl/parsing/ParserBasicType.hpp index f950ff017..736e9716f 100644 --- a/include/rfl/parsing/ParserBasicType.hpp +++ b/include/rfl/parsing/ParserBasicType.hpp @@ -1,7 +1,12 @@ #ifndef RFL_PARSING_PARSER_BASIC_TYPE_HPP_ #define RFL_PARSING_PARSER_BASIC_TYPE_HPP_ +#include +#include +#include #include +#include +#include #include #include "../Result.hpp" @@ -13,6 +18,7 @@ #include "../internal/has_reflection_method_v.hpp" #include "../internal/has_reflection_type_v.hpp" #include "../internal/has_reflector.hpp" +#include "../internal/int64_as_string_v.hpp" #include "../internal/is_basic_type.hpp" #include "../internal/is_description.hpp" #include "../internal/is_literal.hpp" @@ -49,8 +55,33 @@ struct ParserBasicType { * @return A Result containing the parsed value or an error. */ static RFL_ALWAYS_INLINE Result read(const R& _r, - const InputVarType& _var) noexcept { - return _r.template to_basic_type>(_var); + const InputVarType& _var) + noexcept(!internal::int64_as_string_v) { + using U = std::remove_cvref_t; + if constexpr (internal::int64_as_string_v && + std::is_integral_v && !std::is_same_v && + sizeof(U) >= 8) { + return _r.template to_basic_type(_var).and_then( + [](const std::string& _str) -> Result { + if (_str.empty()) { + return error("Could not parse empty string as an integer."); + } + U parsed = 0; + const char* const begin = _str.data(); + const char* const end = _str.data() + _str.size(); + const auto [ptr, ec] = std::from_chars(begin, end, parsed); + if (ec == std::errc::result_out_of_range) { + return error("Integer string '" + _str + + "' is out of range for the target type."); + } + if (ec != std::errc{} || ptr != end) { + return error("Could not parse '" + _str + "' as an integer."); + } + return parsed; + }); + } else { + return _r.template to_basic_type(_var); + } } /** @@ -63,7 +94,22 @@ struct ParserBasicType { */ template static void write(const W& _w, const T& _var, const P& _parent) { - ParentType::add_value(_w, _var, _parent); + using U = std::remove_cvref_t; + if constexpr (internal::int64_as_string_v && + std::is_integral_v && !std::is_same_v && + sizeof(U) >= 8) { + std::array buf{}; + const auto [ptr, ec] = + std::to_chars(buf.data(), buf.data() + buf.size(), _var); + const auto str = + ec == std::errc{} + ? std::string(buf.data(), + static_cast(ptr - buf.data())) + : std::to_string(_var); + ParentType::add_value(_w, str, _parent); + } else { + ParentType::add_value(_w, _var, _parent); + } } /** @@ -75,7 +121,12 @@ struct ParserBasicType { static schema::Type to_schema(std::map*) { using U = std::remove_cvref_t; using Type = schema::Type; - if constexpr (std::is_same()) { + if constexpr (internal::int64_as_string_v && + std::is_integral_v && !std::is_same_v && + sizeof(U) >= 8) { + return Type{Type::String{}}; + + } else if constexpr (std::is_same()) { return Type{Type::Boolean{}}; } else if constexpr (std::is_same()) { diff --git a/tests/json/test_int64_as_string.cpp b/tests/json/test_int64_as_string.cpp new file mode 100644 index 000000000..e1732becc --- /dev/null +++ b/tests/json/test_int64_as_string.cpp @@ -0,0 +1,102 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_int64_as_string { + +struct Trade { + int64_t trade_id; + uint64_t sequence; + int32_t quantity; + double price; +}; + +TEST(json, test_int64_as_string_roundtrip) { + const auto trade = Trade{ + .trade_id = 9007199254740993LL, + .sequence = std::numeric_limits::max(), + .quantity = 42, + .price = 12.5, + }; + + // With rfl::Int64AsString: 64-bit ints become strings, 32-bit/double stay numbers. + write_and_read( + trade, + R"({"trade_id":"9007199254740993","sequence":"18446744073709551615","quantity":42,"price":12.5})"); + + // Default behavior (without processor) is completely unchanged. + write_and_read( + trade, + R"({"trade_id":9007199254740993,"sequence":18446744073709551615,"quantity":42,"price":12.5})"); +} + +TEST(json, test_int64_as_string_nested_optional_vector_validator) { + struct Envelope { + std::optional min_id; + std::vector ids; + rfl::Validator> positive_id; + }; + + const auto env = Envelope{ + .min_id = std::numeric_limits::min(), + .ids = {0ULL, 9007199254740993ULL}, + .positive_id = 9007199254740993LL, + }; + + write_and_read( + env, + R"({"min_id":"-9223372036854775808","ids":["0","9007199254740993"],"positive_id":"9007199254740993"})"); + + // Validator still rejects values failing the numeric constraint. + EXPECT_FALSE((rfl::json::read( + R"({"ids":[],"positive_id":"0"})"))); + + // Works inside rfl::Processors<...> alongside other processors. + using Procs = rfl::Processors; + const auto trade = Trade{ + .trade_id = -9223372036854775807LL, + .sequence = 1ULL, + .quantity = 3, + .price = 1.5, + }; + write_and_read( + trade, + R"({"tradeId":"-9223372036854775807","sequence":"1","quantity":3,"price":1.5})"); +} + +TEST(json, test_int64_as_string_errors_and_schema) { + struct SingleSigned { + int64_t v; + }; + struct SingleUnsigned { + uint64_t v; + }; + + // Bare JSON number is rejected when Int64AsString is active. + EXPECT_FALSE((rfl::json::read(R"({"v":42})"))); + // Syntax and overflow errors. + EXPECT_FALSE((rfl::json::read(R"({"v":""})"))); + EXPECT_FALSE((rfl::json::read(R"({"v":" 42"})"))); + EXPECT_FALSE((rfl::json::read(R"({"v":"42abc"})"))); + EXPECT_FALSE((rfl::json::read( + R"({"v":"9223372036854775808"})"))); + EXPECT_FALSE((rfl::json::read( + R"({"v":"-9223372036854775809"})"))); + EXPECT_FALSE((rfl::json::read( + R"({"v":"18446744073709551616"})"))); + EXPECT_FALSE((rfl::json::read( + R"({"v":"-1"})"))); + + // JSON schema reflects "string" for 64-bit ints and "integer" for int32_t. + const auto schema = rfl::json::to_schema(); + EXPECT_NE(schema.find(R"("trade_id":{"type":"string"})"), std::string::npos); + EXPECT_NE(schema.find(R"("sequence":{"type":"string"})"), std::string::npos); + EXPECT_NE(schema.find(R"("quantity":{"type":"integer"})"), std::string::npos); +} + +} // namespace test_int64_as_string From 9d7fb3d76c8be7a6613caa45e5548fba95da5f87 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Sat, 28 Nov 2026 12:08:50 +0100 Subject: [PATCH 2/2] Emit OpenAPI format: int64/uint64 in JSON Schema for rfl::Int64AsString --- docs/content/processors.md | 2 +- include/rfl/json/schema/Type.hpp | 1 + include/rfl/parsing/ParserBasicType.hpp | 3 ++- include/rfl/parsing/schema/Type.hpp | 4 +++- src/rfl/json/to_schema.cpp | 6 ++++-- tests/json/test_int64_as_string.cpp | 9 ++++++--- 6 files changed, 17 insertions(+), 8 deletions(-) diff --git a/docs/content/processors.md b/docs/content/processors.md index 865b0557f..8a366cf21 100644 --- a/docs/content/processors.md +++ b/docs/content/processors.md @@ -532,7 +532,7 @@ const auto json_string = rfl::json::write( const auto trade = rfl::json::read(json_string).value(); ``` -`rfl::json::to_schema()` emits `"type":"string"` for 64-bit integer fields and `"type":"integer"` for narrower integers. +`rfl::json::to_schema()` emits `{"type":"string","format":"int64"}` / `{"type":"string","format":"uint64"}` for 64-bit integer fields and `{"type":"integer"}` for narrower integers. ## Combining several processors diff --git a/include/rfl/json/schema/Type.hpp b/include/rfl/json/schema/Type.hpp index b032c83b2..a1a7153b2 100644 --- a/include/rfl/json/schema/Type.hpp +++ b/include/rfl/json/schema/Type.hpp @@ -43,6 +43,7 @@ struct Type { struct String { Literal<"string"> type{}; rfl::Flatten annotations{}; + std::optional format{}; rfl::Rename<"minLength", std::optional> minSize{}; rfl::Rename<"maxLength", std::optional> maxSize{}; std::optional contentEncoding{}; diff --git a/include/rfl/parsing/ParserBasicType.hpp b/include/rfl/parsing/ParserBasicType.hpp index 736e9716f..d102e3792 100644 --- a/include/rfl/parsing/ParserBasicType.hpp +++ b/include/rfl/parsing/ParserBasicType.hpp @@ -124,7 +124,8 @@ struct ParserBasicType { if constexpr (internal::int64_as_string_v && std::is_integral_v && !std::is_same_v && sizeof(U) >= 8) { - return Type{Type::String{}}; + return Type{Type::String{ + .format_ = std::is_unsigned_v ? "uint64" : "int64"}}; } else if constexpr (std::is_same()) { return Type{Type::Boolean{}}; diff --git a/include/rfl/parsing/schema/Type.hpp b/include/rfl/parsing/schema/Type.hpp index ea3bc17de..a480b9dcf 100644 --- a/include/rfl/parsing/schema/Type.hpp +++ b/include/rfl/parsing/schema/Type.hpp @@ -37,7 +37,9 @@ struct RFL_API Type { struct Double {}; - struct String {}; + struct String { + std::optional format_{}; + }; struct AnyOf { std::vector types_; diff --git a/src/rfl/json/to_schema.cpp b/src/rfl/json/to_schema.cpp index 3fda88bd2..9c383aa34 100644 --- a/src/rfl/json/to_schema.cpp +++ b/src/rfl/json/to_schema.cpp @@ -215,8 +215,10 @@ schema::Type type_to_json_schema_type(const parsing::schema::Type& _type, std::is_same()) { return schema::Type{.value = schema::Type::Number{}}; - } else if constexpr (std::is_same() || - std::is_same()) { + } else if constexpr (std::is_same()) { + return schema::Type{.value = schema::Type::String{.format = _t.format_}}; + + } else if constexpr (std::is_same()) { return schema::Type{.value = schema::Type::String{}}; } else if constexpr (std::is_same()) { diff --git a/tests/json/test_int64_as_string.cpp b/tests/json/test_int64_as_string.cpp index e1732becc..d96ea4bbc 100644 --- a/tests/json/test_int64_as_string.cpp +++ b/tests/json/test_int64_as_string.cpp @@ -92,10 +92,13 @@ TEST(json, test_int64_as_string_errors_and_schema) { EXPECT_FALSE((rfl::json::read( R"({"v":"-1"})"))); - // JSON schema reflects "string" for 64-bit ints and "integer" for int32_t. + // JSON schema emits OpenAPI/Protobuf-JSON string+format hints for 64-bit + // integers and leaves narrower integers as plain "integer". const auto schema = rfl::json::to_schema(); - EXPECT_NE(schema.find(R"("trade_id":{"type":"string"})"), std::string::npos); - EXPECT_NE(schema.find(R"("sequence":{"type":"string"})"), std::string::npos); + EXPECT_NE(schema.find(R"("trade_id":{"type":"string","format":"int64"})"), + std::string::npos); + EXPECT_NE(schema.find(R"("sequence":{"type":"string","format":"uint64"})"), + std::string::npos); EXPECT_NE(schema.find(R"("quantity":{"type":"integer"})"), std::string::npos); }