Repository navigation
Add rfl::Stringified<T> wrapper for base-10 string-encoded integers - #133
Open
clockblocker wants to merge 1 commit into
Open
clockblocker wants to merge 1 commit into
clockblocker wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
rfl::Stringified<T>(ininclude/rfl/Stringified.hpp, included frominclude/rfl.hpp), an opt-in wrapper matchingrfl::Hex<T>,rfl::Oct<T>, andrfl::Binary<T>that serializes and deserializes integral types (int64_t,uint64_t, and narrower integers, excludingbool) as base-10 strings.include/rfl/Stringified.hpp:std::to_chars/std::from_charsfor 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).Result<Stringified<T>> from_string(std::string_view)andfrom_reflection(std::string_view)factories (no exceptions thrown on parse failure).reflect-cppwrapper API:.value(),.get(),operator*(),operator()(),operator->(),.set(),.str(),operator<=>, and equality withT.std::optional,std::vector,rfl::DefaultVal,rfl::json::to_schema({"type":"string"}), and allreflect-cppformats.include/rfl/internal/HasFromReflectionMethod.hpp: Extends theHasFromReflectionMethodconcept to also acceptstatic Result<T> from_reflection(ReflectionType&&)alongsidestatic T from_reflection(ReflectionType&&), soParser_defaultreports validation errors viarfl::Resultwithout throwing.docs/content/number_systems.mdanddocs/content/_index.md; addedtests/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_tfields as decimal JSON strings ("9007199254740993") to avoid IEEE-754 double precision loss above2^53 - 1. Without a built-in wrapper, users have to storestd::stringfields with custom regex patterns and manualstd::stoll/std::from_charsboilerplate.Verification
-std=c++20 -Wall -Wextra -Wpedantic -Wshadow -Wconversion -Werror) oninclude/rfl/Stringified.hppandinclude/rfl/internal/HasFromReflectionMethod.hpp: passed.rfl::Stringified<bool>andrfl::Stringified<double>are rejected at compile time.tests/json/test_stringified.cpp,tests/json/test_custom_class5.cpp, andtests/json/test_hex.cppunder-fsanitize=undefined -fno-sanitize-recover=all -Wall -Werror: 6/6 passed.