public inbox for [email protected]
help / color / mirror / Atom feedFrom: Hans Ginzel <[email protected]>
To: [email protected]
Subject: register_adapter Json with custom JSONEncoder
Date: Mon, 13 Jul 2020 17:49:07 +0200
Message-ID: <[email protected]> (raw)
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
view thread (3+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected]
Subject: Re: register_adapter Json with custom JSONEncoder
In-Reply-To: <[email protected]>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox