Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

please improve the enum conversion documentation (my example gist provided) #1851

Closed
qingfengxia opened this issue Nov 22, 2019 · 4 comments
Closed

Comments

@qingfengxia
Copy link

my first expression is enum class conversion is not supported, but after an time consuming investigation, it is possible without any change, but official document does not provide example.

Here is mime

https://gist.github.com/qingfengxia/92f5fa843ac6bbc951135a32172bfcff

tested with C++17, but should works for C++14/11

also, thank you for this great lib

Qingfeng

@nlohmann
Copy link
Owner

Can you elaborate what is the difference to the documented approach in https://github.com/nlohmann/json#specializing-enum-conversion?

@qingfengxia
Copy link
Author

Maybe add the the end this section

NLOHMANN_JSON_SERIALIZE_ENUM() also works for scoped enum class (C++11), for example.

enum  class DevicePreference {  CPU,    GPU };
NLOHMANN_JSON_SERIALIZE_ENUM(DevicePreference, {
          {DevicePreference::CPU, "CPU"},
          {DevicePreference::GPU, "GPU"},                               })

Usage is same

@nlohmann
Copy link
Owner

How does the existing documentation make you assume it would not work for an enum class?

@qingfengxia
Copy link
Author

A line like "NLOHMANN_JSON_SERIALIZE_ENUM() also works for scoped enum class (C++11)" can assure new users, it works for C++11, if not, it is user's fault. Because, in multiple files project, namespace,etc make it not evident how it should setup. and template programming is hard to spot error.

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

No branches or pull requests

2 participants