Support encoding/decoding dict int keys for JSON - #243
Merged
Conversation
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
force-pushed
the
json-dict-int-keys
branch
from
December 17, 2022 19:13
158581f to
aa2ca47
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
JSON as a protocol only supports string keys in objects, which means that arbitrary key types can't be used. Previously we only allowed
strandLiteral[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:strLiteral[strings...]EnumintLiteral[integers...]IntEnumalong with constraints on these types.
Integer keys are encoded/decoded as integer strings (e.g.
1 -> "1"). There is precedent for this change - the stdlibjsonlibrary does this automatically forintkeys, as doesorjson(opt in withOPT_NON_STR_KEYS). Golang'sjsonlibrary and protobuf-json also does this for integer keys. The standardcitm_catalog.jsonbenchmark file also has a schema that uses integer-string keys. It makes sense for us to support this use case.Fixes #241.