-
Notifications
You must be signed in to change notification settings - Fork 37
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
Dicts with non-json keys (e.g. Enums) could be dumped more nicely. #140
Comments
I have a similar scenario (mixed value types only for experimentation).
De-serialization of the serialized form does work as expected:
Admittedly, the serialized form of a dict with Enum keys does look a little odd. But it seems they need to make sure the keys are valid types for Python's JSON. Search the code for "if not any(issubclass(type(key), json_key) for json_key in JSON_KEYS)". I think you should rephrase the issue's title into "A dict with Enum keys serialized representation does not look the way I like". This way, you can argue the beauty of the representation (along the lines "readability counts", compatibility with external systems, use of de-serialization libs other than JSON). But for as long as you want to dump into JSON format, you need to make sure that the keys are of acceptable types. |
Why is that? Enums are serialised to strings when they are not dict keys. |
I think swissguest answered it nicely. 💐 Python allows more types as keys in dicts than json allows keys in objects. As you know, anything that is hashable in Python can be used as a key in a dict, whereas json allows only strings, integers, floats, booleans or null.
I'm not against making an exception for enumerations though, as I agree that your expected outcome would be nicer to read. However, it would encompass also featuring a new argument to the jsons.load(dumped, cls=SomeClass, key_cls=SomeEnum) |
Example
Expected Output
Actual Output
The enum keys are hashed and the hashes are used in place of the actual keys whilst an additional "-keys" map is added with the hashes back to the original values.
The text was updated successfully, but these errors were encountered: