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

breaking: Encode/decode all enums by value. #211

Merged
merged 2 commits into from Nov 28, 2022
Merged

breaking: Encode/decode all enums by value. #211

merged 2 commits into from Nov 28, 2022

Conversation

jcrist
Copy link
Owner

@jcrist jcrist commented Nov 28, 2022

Previously integer enums encoded/decoded by their integer values,
while all other enums encoded/decoded by their string names. This PR
changes things so that all enums encode/decode by their values (string
and integer values only supported for now).

This is a breaking change

Before this PR:

import enum
import msgspec

class Example(enum.Enum):
    A = "x"

msgspec.json.encode(Example.A)
# > b'"A"'

After this PR:

import enum
import msgspec

class Example(enum.Enum):
    A = "x"

msgspec.json.encode(Example.A)
# > b'"x"'

Many other tools in the python ecosystem support encoding enums as their
values by default (orjson, pydantic, cattrs, json), but no tool
AFAICT supports encoding as their names by default. The purpose of this
change is to better match the rest of the Python ecosystem.

Fixes #209.

Previously integer enums encoded/decoded by their integer *values*,
while all other enums encoded/decoded by their string *names*. This PR
changes things so that all enums encode/decode by their values (string
and integer values only supported for now).

This is a *breaking change*.

Many other tools in the python ecosystem support encoding enums as their
values by default (`orjson`, `pydantic`, `cattrs`, `json`), but no tool
AFAICT supports encoding as their names by default. The purpose of this
change is to better match the rest of the Python ecosystem.
@jcrist jcrist merged commit 5658970 into main Nov 28, 2022
@jcrist jcrist deleted the enum-by-value branch November 28, 2022 04:07
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.

Issues with enum serialization
1 participant