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

Comments

Projects
None yet
2 participants
@haizaar
Copy link

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

This comment has been minimized.

Copy link
Owner

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

This comment has been minimized.

Copy link
Author

haizaar commented Feb 1, 2019

@argaen

This comment has been minimized.

Copy link
Owner

argaen commented Feb 1, 2019

np!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment