public inbox for [email protected]
help / color / mirror / Atom feedFrom: Hans Ginzel <[email protected]>
To: [email protected]
Subject: Re: register_adapter Json with custom JSONEncoder
Date: Mon, 20 Jul 2020 13:18:55 +0200
Message-ID: <[email protected]> (raw)
In-Reply-To: <CA+mi_8bPLhcgA3KgyZ-g+=oSkUBjaB-JoZD5oAKVjPqbm8iiog@mail.gmail.com>
References: <[email protected]>
<CA+mi_8bPLhcgA3KgyZ-g+=oSkUBjaB-JoZD5oAKVjPqbm8iiog@mail.gmail.com>
On Mon, Jul 13, 2020 at 05:15:19PM +0100, Daniele Varrazzo wrote:
>You can use a partial function for instance, of the like of: `lambda
>obj: Json(obj, dumps=mydumps)`.
Thank you, Daniele.
I have (succesfully) tried
from psycopg2.extras import Json
from json import JSONEncoder
from bson import ObjectId
class JSONEncoder(JSONEncoder):
def default(self, o):
if isinstance(o, ObjectId):
return str(o)
return supper().default(self, o)
jsonEncoder = JSONEncoder()
#psycopg2.extensions.register_adapter(dict, Json)
psycopg2.extensions.register_adapter(dict, lambda o: Json(o, dumps=jsonEncoder.encode))
and variants like
import json
psycopg2.extensions.register_adapter(dict, lambda o: Json(o, dumps = lambda oo: json.dumps(oo, default=str)))
or
from bson import json_util
psycopg2.extensions.register_adapter(dict, lambda o: Json(o, dumps=json_util.dumps))
I choosed the first one because of cached Encoder and cleanest way.
Best regards,
Hans
view thread (3+ messages)
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