-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[RFC] Enum JSON string converter #9881
Comments
A string-based converter seems like a great idea. I'm recalling that this was mentioned somewhere before, but can't find it. enum Foo
B
C
end
Foo::C.to_json # => "1" When reading that serialized value from a program with an additional enum member, it returns a different result: enum Foo
B
C
end
Foo.from_json("1") # => B Using the names instead of values would be more reliable. Renaming an existing member is far less likely then adding, removing or reordering members. Deserializing a renamed or removed member would also properly fail, not simply return a different member. |
To not forget: The same applies to YAML serialization. |
Enums sure are handy for DSLs and protocols, especially with the recent discriminator logic in serialization.
Currently, if an enum is present on a model including
JSON::Serializable
it serializes to an integer.I propose
Enum::StringConverter
JSON converter module.Usage:
An alternative would be allowing
String
typed enums, like the followingThe text was updated successfully, but these errors were encountered: