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 1jv17h-0007rh-2c for psycopg@arkaria.postgresql.org; Mon, 13 Jul 2020 16:16:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jv17A-0007ni-BP for psycopg@arkaria.postgresql.org; Mon, 13 Jul 2020 16:15:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jv17A-0007na-3O for psycopg@lists.postgresql.org; Mon, 13 Jul 2020 16:15:36 +0000 Received: from mail-lf1-x136.google.com ([2a00:1450:4864:20::136]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jv177-0003lq-4R for psycopg@lists.postgresql.org; Mon, 13 Jul 2020 16:15:35 +0000 Received: by mail-lf1-x136.google.com with SMTP id s16so9351642lfp.12 for ; Mon, 13 Jul 2020 09:15:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=V2/9kD+m83Cnn0olYBnLsoou5QNySC4iDQZ5sV0l52k=; b=hketyj3030fo/4yOYEHtkMJBzo2pOuedjXO+7hEAv1u49qLQnG9X+OX83NP7xZakXQ eYQ79hZ5Dm+a4hpc6oTSasxFfKUfnikdGHNzfbGFDLTpio2iVxxDfnbg7NkDyiU7ovKn 6Aj9qY4bTryshKFUk22F0kVqPCQmvEcw5mui2h0Wo8wpC58MkDGAc/Lg7sYt2o4Duzk5 wMP6rSJ4iP/+i6ElH3G2/L4SW3aWiSHiQtO1CO7Mvt7YoXICDj80iV9wcyIxVDLnOgiJ gBrzv3y4kNHy2Pooqksxn1HM7h3HRPuhz743QnJkOzFSU+ONUUr/gqOtgvXCgEf7MxGY kDrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=V2/9kD+m83Cnn0olYBnLsoou5QNySC4iDQZ5sV0l52k=; b=oU5R/UJZ2g79fAjTaroVEWO6mFgoWsZYKyXMmQBsatogMa5U4Lpk3lrU2+cWm/ZN2A VWUHt7O+764hL53ZU4WiPNH1Aq8zBvGAg9FLpy7VE/4y6angZfIfnd/2X9qDNemKFqKY /Uj+gKiRkkpcoH7hei7A2EHzC/D0RUwE4lxQyVDciy/6s3g4j9Vt4owIgxVBuhvTvnVo LSX8m8EM0PDhorTUwzRlf8Os+olikyVv1qi+3gsUeDUsLE4J811TOJSw/cHP9PsGDzkr baRBTArh6Xjxv1nWlPs1wykAehYFMoKprLgtsdWE8QzNV6Ob8AfytnAazjJgKgBnXybJ l34A== X-Gm-Message-State: AOAM532P4N2qdh8aaW1/WXSHc2K+i7FYZvWhpGARqsnrKXuSrjNU+833 qollZEjA18xb7DwwPQUSMpo7BzM7Y1te0T9uIpM= X-Google-Smtp-Source: ABdhPJxBlXCkF7EoE4EV0dEzxehTi/GPy1GdtLZ5yjm3KJVK2ITOpE53u2X0bWWGYCUiPcjEsn2Gnx62q81KHlHi2yY= X-Received: by 2002:a19:c603:: with SMTP id w3mr10658lff.28.1594656931294; Mon, 13 Jul 2020 09:15:31 -0700 (PDT) MIME-Version: 1.0 References: <20200713154907.GH81733@artax.karlin.mff.cuni.cz> In-Reply-To: <20200713154907.GH81733@artax.karlin.mff.cuni.cz> From: Daniele Varrazzo Date: Mon, 13 Jul 2020 17:15:19 +0100 Message-ID: Subject: Re: register_adapter Json with custom JSONEncoder To: Hans Ginzel Cc: psycopg@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello, On Mon, 13 Jul 2020 at 16:49, Hans Ginzel wrote: [...] > But I need to use custom JSONEncoder because of bson.ObectId type > # https://stackoverflow.com/a/16586277/2556118 This doc page has more examples: https://www.psycopg.org/docs/extras.html#json-adaptation > There is a parameter dumps in Json.__init__(self, adapted, dumps=None), > but how to set it when used with register_adapter(), You can use a partial function for instance, of the like of: `lambda obj: Json(obj, dumps=mydumps)`. > https://www.psycopg.org/docs/extensions.html#psycopg2.extensions.register_adapter? > > Should I write myself the whole Json class? No, if you really want you can subclass the one we provide and only override `_dumps()`: the one psycopg2 provides consists in pretty much: class Json(object): def __init__(self, adapted, dumps=None): self.adapted = adapted self._dumps = dumps or json.dumps def dumps(self, obj): return self._dumps(obj) so you can either create your instances using a wrapper in register_adapter, or subclass the _dumps() method of the class: as you can see it is equivalent. I guess the first method is better as you don't need to care about the implementation of the `Json` class. -- Daniele