Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jv0hf-0006g6-S2 for psycopg@arkaria.postgresql.org; Mon, 13 Jul 2020 15:49:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jv0he-0006Q4-QN for psycopg@arkaria.postgresql.org; Mon, 13 Jul 2020 15:49:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jv0he-0006Ox-L4 for psycopg@lists.postgresql.org; Mon, 13 Jul 2020 15:49:14 +0000 Received: from postak.karlin.mff.cuni.cz ([195.113.30.11]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jv0hb-0004HN-Q9 for psycopg@lists.postgresql.org; Mon, 13 Jul 2020 15:49:14 +0000 Received: from artax.karlin.mff.cuni.cz (artax.karlin.mff.cuni.cz [195.113.26.195]) by postak.karlin.mff.cuni.cz (Postfix) with ESMTPS id 0EF3B2028B for ; Mon, 13 Jul 2020 17:49:08 +0200 (CEST) Received: by artax.karlin.mff.cuni.cz (Postfix, from userid 1975) id E4168828001; Mon, 13 Jul 2020 17:49:07 +0200 (CEST) Date: Mon, 13 Jul 2020 17:49:07 +0200 From: Hans Ginzel To: psycopg@lists.postgresql.org Subject: register_adapter Json with custom JSONEncoder Message-ID: <20200713154907.GH81733@artax.karlin.mff.cuni.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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