ijl / orjson

Fast, correct Python JSON library supporting dataclasses, datetimes, and numpy
Apache License 2.0
6.29k stars 215 forks source link

Can orjson produce dictionaries with non-string keys? #531

Closed Tinche closed 1 week ago

Tinche commented 1 week ago

Hi,

this is a question and not a bug so feel free to close. I maintain cattrs, which is a layer above orjson. Cattrs contains a preconfigured converter specifically for orjson. I am considering an optimization and wanted to check first.

When orjson loads a JSON object into a dictionary, is it possible for that dictionary to contain non-string keys? As far as I know, the JSON standard (I guess the ECMA-404) mandates object keys be strings - does orjson ensure this?

I'm asking since if this is true, I can skip validating mapping keys when structuring into dict[str, T], Mapping[str, T], thus speeding up these operations.

ijl commented 1 week ago

The spec is it needs to be a string, and there's no coercion happening in this library, so if it parses without error then dict keys are 100% str.

Tinche commented 1 week ago

Cool, thanks!