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

EncodeError exception when encoding python dictionary with integer keys #8

Closed
RobiIhs opened this issue Feb 22, 2019 · 1 comment
Closed
Labels

Comments

@RobiIhs
Copy link

RobiIhs commented Feb 22, 2019

the actual updated library 1.2.1 throws exception when encoding { 1 : 'a' }
while the older czjson library 1.0.8 was fixed to allow encode of { 1 : 'a' }

thank you

@danpascu
Copy link
Contributor

This is not a bug. The JSON standard only allows object (dictionary) keys to be strings and cjson clearly tells you that in the error message:

In [2]: cjson.encode({1: 'a'})
---------------------------------------------------------------------------
EncodeError                               Traceback (most recent call last)
<ipython-input-2-6990413e0aea> in <module>()
----> 1 cjson.encode({1: 'a'})

EncodeError: JSON encodable dictionaries must have string/unicode keys

In this regard 1.0.8 was in error and it was fixed.

Other json implementations will automatically convert integers to strings, but that means that encoding and then decoding a dictionary like that will result in a different dictionary:

In [3]: json.loads(json.dumps({1: 'a'}))
Out[3]: {u'1': u'a'}

cjson refuses to make assumptions about your intentions and simply signals the error and expects you to fix it by providing data that can be encoded in JSON without errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants