register_adapter Json with custom JSONEncoder

Поиск
Список
Период
Сортировка
Искать
От
Hans Ginzel
Тема
register_adapter Json with custom JSONEncoder
Дата
Msg-id
20200713154907.GH81733@artax.karlin.mff.cuni.cz
Список
Дерево обсуждения
register_adapter Json with custom JSONEncoder Hans Ginzel <hans@matfyz.cz>
Re: register_adapter Json with custom JSONEncoder Daniele Varrazzo <daniele.varrazzo@gmail.com>
Re: register_adapter Json with custom JSONEncoder Hans Ginzel <hans@matfyz.cz>
Hello,

how can I register an adapter with custom JSONEncoder, please.

On Stack Overflow, https://stackoverflow.com/a/55939024/2556118
I have found to use Json class
psycopg2.extensions.register_adapter(dict, psycopg2.extras.Json).

But I need to use custom JSONEncoder because of bson.ObectId type
# https://stackoverflow.com/a/16586277/2556118
import json
from bson import ObjectId
class JSONEncoder(json.JSONEncoder):
     def default(self, o):
         if isinstance(o, ObjectId):
             return str(o)
         return supper().JSONEncoder.default(self, o)

There is a parameter dumps in Json.__init__(self, adapted, dumps=None),
but how to set it when used with register_adapter(),
https://www.psycopg.org/docs/extensions.html#psycopg2.extensions.register_adapter?

Should I write myself the whole Json class?

Thank you in advance,
Hans




В списке psycopg по дате отправления
От: Adrian Klaver
Дата:
От: Daniele Varrazzo
Дата:
FAQ