Skip to content

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

Merged
jcrist merged 2 commits into
mainfrom
enum-by-value
Nov 28, 2022
Merged

breaking: Encode/decode all enums by value.#211
jcrist merged 2 commits into
mainfrom
enum-by-value

Conversation

@jcrist

@jcrist jcrist commented Nov 28, 2022

Copy link
Copy Markdown
Member

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