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

Support custom JSON encoder/decoder #30

Open
binhnq94 opened this issue Feb 26, 2016 · 15 comments
Open

Support custom JSON encoder/decoder #30

binhnq94 opened this issue Feb 26, 2016 · 15 comments

Comments

@binhnq94
Copy link

When i try do

session = yield from get_session(self.request)
session['user'] = user

with user is a document of mongo, my program occur a error:

Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/aiohttp/server.py", line 266, in start
    yield from self.handle_request(message, payload)
  File "/usr/local/lib/python3.4/dist-packages/aiohttp/web.py", line 90, in handle_request
    resp = yield from handler(request)
  File "/usr/local/lib/python3.4/dist-packages/aiohttp_session/__init__.py", line 144, in middleware
    yield from storage.save_session(request, response, session)
  File "/usr/lib/python3.4/asyncio/coroutines.py", line 141, in coro
    res = func(*args, **kw)
  File "/usr/local/lib/python3.4/dist-packages/aiohttp_session/cookie_storage.py", line 52, in save_session
    self._get_session_data(session)
  File "/usr/lib/python3.4/json/__init__.py", line 230, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.4/json/encoder.py", line 192, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.4/json/encoder.py", line 250, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.4/json/encoder.py", line 173, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: ObjectId('56d07ade0686372f8e45b3f1') is not JSON serializable

How do i replace 'json' encoder with 'bson.json_until' encoder?
https://github.com/aio-libs/aiohttp_session/blob/master/aiohttp_session/cookie_storage.py#L51

@asvetlov
Copy link
Member

Unfortunately using custom JSON serializer is not possible in current aiohttp_session version.

Adding encoder and decoder params to SimpleCookieStorage, EncryptedCookieStorage and NaClCookieStorage makes sense.

We are already accept these params in RedisStorage.

I suggest moving self._encoder and self._decoder to AbstractStorage constructor.

The patch is very easy.
I'll accept it without any additional test but documentation update is required (we've missed it even for RedisStorage, shame on me).

Any volunteers?

@asvetlov
Copy link
Member

As workaround I suggest converting ObjectId to string and back manually on every access to session data but really prefer to have encoder/decoder support for every storage class.

The change deserves a new aiohttp_session release.

@binhnq94
Copy link
Author

I am happy and look forward to becoming one comtributor.
in addition, I would like to ask how to commit a contribute code? ( i have seen answer here: http://aiohttp.readthedocs.org/en/stable/contributing.html, and I hope to help you )

i think that "moving self._encoder and self._decoder to AbstractStorage constructor" is a good way.

@asvetlov
Copy link
Member

@TechBK you should create a Pull Request

@binhnq94
Copy link
Author

😃 ok

@binhnq94
Copy link
Author

@asvetlov hey, my friend. check Pull Request.

@itsbjoern
Copy link
Contributor

I just came across this relatively old issue. Unless I'm missing something, is there a reason why @TechBK's change was never merged? If there is a reason I suggest closing this issue otherwise I'd get a PR ready soon. (Especially since his fork looks like he basically completed it)

@asvetlov
Copy link
Member

asvetlov commented Feb 6, 2018

Sorry, but I cannot find any related PR.

@panagiks
Copy link
Contributor

panagiks commented Feb 6, 2018

I think @BFriedrichs refers to #31 .

@asvetlov
Copy link
Member

asvetlov commented Feb 6, 2018

Looks like @TechBK gave up after first review tour.
His PR need more love for polishing before merge.

@itsbjoern
Copy link
Contributor

Correct, I missed that for some reason. I haven’t really looked into how much changed since then but I can’t imagine these changes wouldn’t be still applicable.

@asvetlov
Copy link
Member

asvetlov commented Feb 6, 2018

I suggest creating a new PR from scratch and discussing it.

@Dreamsorcerer Dreamsorcerer changed the title Occur error, if session save a bson.objectid.ObjectId instance. Support custom JSON encoder/decoder Jan 28, 2022
@muazhari
Copy link

muazhari commented Sep 11, 2022

Are there any updates on this issue?

@muazhari
Copy link

muazhari commented Sep 11, 2022

The workaround to this issue is by defining a custom JSONEncoder to json_serialize argument in ClientSession like below:

class BaseClient:

    async def get_client_session(self):
        return aiohttp.ClientSession(base_url=client_config.CLIENT_TRANSACTION_URL,
                                     json_serialize=lambda object: json.dumps(
                                         object,
                                         indent=4,
                                         sort_keys=True,
                                         default=str)
                                     )

@Dreamsorcerer
Copy link
Member

Appears there is still no finished PR, feel free to propose one.

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

No branches or pull requests

6 participants