register_adapter Json with custom JSONEncoder - Mailing list psycopg

From Hans Ginzel
Subject register_adapter Json with custom JSONEncoder
Date
Msg-id 20200713154907.GH81733@artax.karlin.mff.cuni.cz
Whole thread Raw
Responses Re: register_adapter Json with custom JSONEncoder
List psycopg
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 by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: Async notifications in psycopg3
Next
From: Daniele Varrazzo
Date:
Subject: Re: register_adapter Json with custom JSONEncoder