Skip to content

Add opt-in string map keys for enums and custom reflectors - #775

Open
hazby2002 wants to merge 2 commits into
munich-data-engineering:mainfrom
hazby2002:feat/stringify-map-keys
Open

hazby2002 wants to merge 2 commits into
munich-data-engineering:mainfrom
hazby2002:feat/stringify-map-keys

Conversation

@hazby2002

Copy link
Copy Markdown
Contributor

Maps keyed by enums or by custom types with rfl::Reflector<T>::ReflType = std::string currently fall back to arrays of key/value pairs. Add the rfl::StringifyMapKeys processor 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::map and std::unordered_map, including nested maps. Custom string reflectors take precedence over enum names; key conversion errors are returned through rfl::Result. EnumNamesOnly is respected. UnderlyingEnums keeps 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:

  • All 506 default tests and all 66 YAML tests pass.
  • 58/59 CBOR tests pass, including the new test. The sole failure is an existing exact-error-message assertion with jsoncons 1.10.0; the identical failure reproduces on unmodified upstream code.
  • All 11 new CTest tests pass. The 8 new JSON/Generic tests and a separate Boost.Serialization typed-map test also pass under ASan/UBSan.
  • All three new headers compile independently with warnings treated as errors.

@hazby2002
hazby2002 marked this pull request as draft November 11, 2026 02:06
@clockblocker

clockblocker commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Nice clean PR — one small edge case I noticed in StringMapKey::enabled:

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 rfl::internal::has_write_reflector<T> only checks Reflector<T>::from(item) and does not require typename Reflector<T>::ReflType to exist, StringMapKey::enabled hits a hard error instead of evaluating to false when ReflType is absent:

} 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
hazby2002 marked this pull request as ready for review November 26, 2026 03:39

This branch has not been deployed

No deployments
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.

Deserialising std::map Enum Keys

2 participants