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

JsonSerializer converts dictionaries keys to strings #441

Closed
haizaar opened this issue Jan 31, 2019 · 3 comments
Closed

JsonSerializer converts dictionaries keys to strings #441

haizaar opened this issue Jan 31, 2019 · 3 comments

Comments

@haizaar
Copy link

@haizaar haizaar commented Jan 31, 2019

Good day. aiocache==0.10.1 on Python 3.7.

Consider the following snippet:

>>> cache = aiocache.SimpleMemoryCache(serializer=aiocache.serializers.JsonSerializer())
>>> asyncio.run(cache.set("key", {123: 'asdf'}))
True
>>> asyncio.run(cache.get("key"))
{'123': 'asdf'}

While I put {123: 'asdf'} in the cache, I got {'123': 'asdf'} back - they `1231 key became a string.

Works fine with Pickle serializer.

@argaen
Copy link
Member

@argaen argaen commented Jan 31, 2019

numeric keys are not valid JSONs https://stackoverflow.com/questions/8758715/using-number-as-index-json

>>> import json
>>> value = {123: 'asdf'}
>>> json.dumps(value)
'{"123": "asdf"}'

If you want to save objects like that and persist the types you'll have to use a serializer like the Pickle one or build a custom one

PS: closing because its not an issue with the library, feel free to keep discussing if you have further doubts though :)

@argaen argaen closed this Jan 31, 2019
@haizaar
Copy link
Author

@haizaar haizaar commented Feb 1, 2019

@argaen
Copy link
Member

@argaen argaen commented Feb 1, 2019

np!

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

Successfully merging a pull request may close this issue.

None yet
2 participants