Skip to content

Conversation

@mosquito
Copy link

@mosquito mosquito commented Mar 14, 2018

JSONB column is not automatically decoded.

Workaround for me is:

async def set_json_charset(connection): await connection.set_type_codec( 'jsonb', encoder=lambda x: x, decoder=ujson.loads, schema='pg_catalog' ) await Pool("postgresql://127.0.0.1/template0", init=set_json_charset)
@elprans
Copy link
Member

elprans commented Mar 14, 2018

Not decoding JSON data automatically is by design.

@elprans elprans closed this Mar 14, 2018
@mosquito
Copy link
Author

IMHO is't so strange. Jsonb fields serializes automatically while inserts but select returns string.

@elprans
Copy link
Member

elprans commented Mar 14, 2018

Jsonb fields serializes automatically

It doesn't:

import asyncio import asyncpg import json async def test(): conn = await asyncpg.connect() try: await conn.execute('CREATE TABLE tab(a jsonb);') await conn.execute('INSERT INTO tab(a) VALUES($1)', {'foo': 'bar'}) print(json.dumps(dict(await conn.fetchrow('SELECT * FROM tab')))) finally: await conn.execute('DROP TABLE tab') await conn.close() if __name__ == '__main__': loop = asyncio.get_event_loop() loop.run_until_complete(test())
 File "asyncpg/protocol/codecs/json.pyx", line 13, in asyncpg.protocol.protocol.jsonb_encode as_pg_string_and_size(settings, obj, &str, &size) File "asyncpg/protocol/codecs/text.pyx", line 12, in asyncpg.protocol.protocol.as_pg_string_and_size raise TypeError('expected str, got {}'.format(type(obj).__name__)) TypeError: expected str, got dict 
@mosquito
Copy link
Author

mosquito commented Mar 14, 2018

hmmm... ok. My mistake. asyncpgsa serializes it. Thank you.

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

Labels

None yet

2 participants