getml / reflect-cpp

A C++20 library for fast serialization, deserialization and validation using reflection. Supports JSON, BSON, CBOR, flexbuffers, msgpack, TOML, XML, YAML / msgpack.org[C++20]
https://getml.github.io/reflect-cpp/
MIT License
901 stars 76 forks source link

C++ enums are serialized numerically #3

Closed riidefi closed 9 months ago

riidefi commented 10 months ago

I am not sure if you are familiar with the magic_enum library, but this is the behavior I would expect. Some extension if magic_enum is detected would also be fine.

std::string color_name{"GREEN"};
auto color = magic_enum::enum_cast<Color>(color_name);
if (color.has_value()) {
  // color.value() -> Color::GREEN
}
liuzicheng1987 commented 10 months ago

No, I am not familiar, but it looks very cool. I will certainly take a look.

For these kind of purposes we have Literals as well, but being able to support enums in this way is a very cool thing.

https://github.com/getml/reflect-cpp/blob/main/docs/literals.md

Again, thank you very much for your input. This is a great issue.

liuzicheng1987 commented 9 months ago

So, I have taken a closer look at the library you have mentioned and my enthusiasm for it has somewhat receded.

The problem with it that it uses compiler-specific and non-standard hacks. It is very cool and I am impressed to see that it is possible. However, these kind of hacks are not enterprise-grade code.

reflect-cpp aims to be standard-compliant and enterprise-grade and therefore I wouldn't want to rely on such methods.

Moreover, reflect-cpp already provides a fully standard-compliant alternative, namely rfl::Literal.

The good news is that the C++ reflection standard promises official support for exactly these kind of things. Once that is out, I would be only too happy to include this in reflect-cpp as well, because it is indeed very cool.

However, until that time I don't think there is a way forward, so I am closing an issue. If you, or somebody else, can come up with a standard-compliant way to support this, feel free to reopen the issue and continue the debate. I would only be to happy to be able to support this.

liuzicheng1987 commented 8 months ago

@riidefi , so I have experimented with this a bit more and I have now found a way to do do this in a way that I don't think is too hacky. So this is now supported.