Skip to content

Add rfl::Stringified<T> wrapper for base-10 string-encoded integers - #133

Open
clockblocker wants to merge 1 commit into
mainfrom
feat-stringified-integers
Open

clockblocker wants to merge 1 commit into
mainfrom
feat-stringified-integers

Conversation

@clockblocker

Copy link
Copy Markdown
Owner

Summary

Adds rfl::Stringified<T> (in include/rfl/Stringified.hpp, included from include/rfl.hpp), an opt-in wrapper matching rfl::Hex<T>, rfl::Oct<T>, and rfl::Binary<T> that serializes and deserializes integral types (int64_t, uint64_t, and narrower integers, excluding bool) as base-10 strings.

  • include/rfl/Stringified.hpp:
    • Uses std::to_chars / std::from_chars for fast, locale-independent base-10 formatting and strict parsing (rejects empty strings, whitespace, + signs, floats, trailing characters, negative values for unsigned types, and out-of-range values).
    • Non-throwing Result<Stringified<T>> from_string(std::string_view) and from_reflection(std::string_view) factories (no exceptions thrown on parse failure).
    • Full standard reflect-cpp wrapper API: .value(), .get(), operator*(), operator()(), operator->(), .set(), .str(), operator<=>, and equality with T.
    • Composes with std::optional, std::vector, rfl::DefaultVal, rfl::json::to_schema ({"type":"string"}), and all reflect-cpp formats.
  • include/rfl/internal/HasFromReflectionMethod.hpp: Extends the HasFromReflectionMethod concept to also accept static Result<T> from_reflection(ReflectionType&&) alongside static T from_reflection(ReflectionType&&), so Parser_default reports validation errors via rfl::Result without throwing.
  • Docs & tests: Documented in docs/content/number_systems.md and docs/content/_index.md; added tests/json/test_stringified.cpp.

Motivation

APIs interoperating with JavaScript/TypeScript clients, Protobuf JSON mappings, or Snowflake-style 64-bit IDs routinely encode int64_t/uint64_t fields as decimal JSON strings ("9007199254740993") to avoid IEEE-754 double precision loss above 2^53 - 1. Without a built-in wrapper, users have to store std::string fields with custom regex patterns and manual std::stoll/std::from_chars boilerplate.

Verification

  • Strict standalone header checks (-std=c++20 -Wall -Wextra -Wpedantic -Wshadow -Wconversion -Werror) on include/rfl/Stringified.hpp and include/rfl/internal/HasFromReflectionMethod.hpp: passed.
  • Compile-fail checks verified that rfl::Stringified<bool> and rfl::Stringified<double> are rejected at compile time.
  • tests/json/test_stringified.cpp, tests/json/test_custom_class5.cpp, and tests/json/test_hex.cpp under -fsanitize=undefined -fno-sanitize-recover=all -Wall -Werror: 6/6 passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant