Repository navigation
Conversation
hazby2002
marked this pull request as draft
November 11, 2026 02:06
Contributor
|
Nice clean PR — one small edge case I noticed in enum class Colour { Red, Green, Blue };
struct Tag {
int id;
auto operator<=>(const Tag&) const = default;
};
namespace rfl {
template <>
struct Reflector<Tag> {
// Satisfies internal::has_write_reflector<Tag>, but omits ReflType.
static std::string from(const Tag& t) { return std::to_string(t.id); }
};
} // namespace rfl
struct Doc {
std::map<Colour, int> colours;
std::map<Tag, int> tags;
};
// Compiles today: Parser::read only checks has_read_reflector<Tag>, so Tag is
// read as a regular struct.
auto ok = rfl::json::read<Doc>(
R"({"colours":[["Red",1]],"tags":[[{"id":1},2]]})");
auto err = rfl::json::read<Doc, rfl::StringifyMapKeys>( // <-- hard compile error here (in StringMapKey.hpp:29: no type named 'ReflType' in 'rfl::Reflector<Tag>')
R"({"colours":{"Red":1},"tags":[[{"id":1},2]]})");Because } else if constexpr ((internal::has_read_reflector<Key> ||
internal::has_write_reflector<Key>) &&
requires { typename Reflector<Key>::ReflType; }) {
return std::is_same_v<
std::remove_cvref_t<typename Reflector<Key>::ReflType>, std::string>;
} |
hazby2002
marked this pull request as ready for review
November 26, 2026 03:39
This branch has not been deployed
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.
Maps keyed by enums or by custom types with
rfl::Reflector<T>::ReflType = std::stringcurrently fall back to arrays of key/value pairs. Add therfl::StringifyMapKeysprocessor so callers can opt into string-keyed maps without changing existing serialized data by default. For example,std::map<Colour, int>{{Colour::Red, 1}}writes as{"Red":1}with the processor, instead of[["Red",1]].The processor applies to
std::mapandstd::unordered_map, including nested maps. Custom string reflectors take precedence over enum names; key conversion errors are returned throughrfl::Result.EnumNamesOnlyis respected.UnderlyingEnumskeeps ordinary enum maps as arrays, while custom string reflectors still take precedence. Multimaps and non-string reflectors retain their existing behavior. JSON Schema uses the same object representation as string-keyed maps.The implementation covers both string-keyed object/map APIs and typed map APIs. Tests cover representation, default compatibility, nested/empty/unordered maps, move-only values, enum and non-enum reflectors, invalid keys, processor combinations, and schema generation. The YAML regression uses the example from #661.
Closes #661.
Validation on macOS arm64 / AppleClang 21 / C++20: