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




Reply via email to