Skip to content

Support encoding/decoding dict int keys for JSON - #243

Merged
jcrist merged 1 commit into
mainfrom
json-dict-int-keys
Dec 17, 2022
Merged

Support encoding/decoding dict int keys for JSON#243
jcrist merged 1 commit into
mainfrom
json-dict-int-keys

Conversation

@jcrist

@jcrist jcrist commented Dec 17, 2022

Copy link
Copy Markdown
Member

JSON as a protocol only supports string keys in objects, which means that arbitrary key types can't be used. Previously we only allowed str and Literal[strings...] types for dict keys. This PR relaxes this restriction to also support int-like keys. The following key types are now supported for JSON:

  • str
  • Literal[strings...]
  • Enum
  • int
  • Literal[integers...]
  • IntEnum

along with constraints on these types.

Integer keys are encoded/decoded as integer strings (e.g. 1 -> "1"). There is precedent for this change - the stdlib json library does this automatically for int keys, as does orjson (opt in with OPT_NON_STR_KEYS). Golang's json library and protobuf-json also does this for integer keys. The standard citm_catalog.json benchmark file also has a schema that uses integer-string keys. It makes sense for us to support this use case.

Fixes #241.

JSON as a protocol only supports string keys in objects, which means
that arbitrary key types can't be used. Previously we only allowed `str`
and `Literal[strings...]` types for dict keys. This commit relaxes this
restriction to support the following key types:

- `str`
- `Literal[strings...]`
- `Enum`
- `int`
- `Literal[integers...]`
- `IntEnum`

along with constraints on these types.

Integer keys are encoded/decoded as integer strings (e.g. `1 -> "1"`).
@jcrist
jcrist force-pushed the json-dict-int-keys branch from 158581f to aa2ca47 Compare December 17, 2022 19:13
@jcrist
jcrist merged commit c9b5d6a into main Dec 17, 2022
@jcrist
jcrist deleted the json-dict-int-keys branch December 17, 2022 19:29
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.

Support serializable key in dict (not only str)

1 participant