public inbox for [email protected]  
help / color / mirror / Atom feed
Overriding default adapters in psycopg
5+ messages / 2 participants
[nested] [flat]

* Overriding default adapters in psycopg
@ 2025-11-16 16:56 Christophe Pettus <[email protected]>
  2025-11-16 17:25 ` Re: Overriding default adapters in psycopg Adrian Klaver <[email protected]>
  2025-11-16 17:30 ` Re: Overriding default adapters in psycopg Adrian Klaver <[email protected]>
  0 siblings, 2 replies; 5+ messages in thread

From: Christophe Pettus @ 2025-11-16 16:56 UTC (permalink / raw)
  To: [email protected]

Hi!

I am attempting to override the default adapters for JSON and JSONB in psycopg (3), but I am not quite sure how to go about it (I'm testing using orjson for serialization and deserialization).  This doesn't appear to work:

> class CyanJSONBLoader(Loader):
>     def load(self, data):
>         return orjson.loads(data)
> 
> psycopg.adapters.register_loader("jsonb", CyanJSONBLoader)
> 
> class CyanJSONBDumper(Dumper):
>     def dump(self, obj):
>         return orjson.dumps(obj)
> 
> psycopg.adapters.register_dumper("jsonb", CyanJSONBDumper)
> 

The calls work, but the dump/load functions are never called.

Thanks!




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Overriding default adapters in psycopg
  2025-11-16 16:56 Overriding default adapters in psycopg Christophe Pettus <[email protected]>
@ 2025-11-16 17:25 ` Adrian Klaver <[email protected]>
  1 sibling, 0 replies; 5+ messages in thread

From: Adrian Klaver @ 2025-11-16 17:25 UTC (permalink / raw)
  To: Christophe Pettus <[email protected]>; [email protected]

On 11/16/25 08:56, Christophe Pettus wrote:
> Hi!
> 
> I am attempting to override the default adapters for JSON and JSONB in psycopg (3), but I am not quite sure how to go about it (I'm testing using orjson for serialization and deserialization).  This doesn't appear to work:
> 
>> class CyanJSONBLoader(Loader):
>>      def load(self, data):
>>          return orjson.loads(data)
>>
>> psycopg.adapters.register_loader("jsonb", CyanJSONBLoader)
>>
>> class CyanJSONBDumper(Dumper):
>>      def dump(self, obj):
>>          return orjson.dumps(obj)
>>
>> psycopg.adapters.register_dumper("jsonb", CyanJSONBDumper)
>>
> 
> The calls work, but the dump/load functions are never called.

I think you might need to use:

https://www.psycopg.org/psycopg3/docs/basic/adapt.html#adapt-json


> 
> Thanks!
> 


-- 
Adrian Klaver
[email protected]





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Overriding default adapters in psycopg
  2025-11-16 16:56 Overriding default adapters in psycopg Christophe Pettus <[email protected]>
@ 2025-11-16 17:30 ` Adrian Klaver <[email protected]>
  2025-11-16 17:43   ` Re: Overriding default adapters in psycopg Christophe Pettus <[email protected]>
  1 sibling, 1 reply; 5+ messages in thread

From: Adrian Klaver @ 2025-11-16 17:30 UTC (permalink / raw)
  To: Christophe Pettus <[email protected]>; [email protected]

On 11/16/25 08:56, Christophe Pettus wrote:
> Hi!
> 
> I am attempting to override the default adapters for JSON and JSONB in psycopg (3), but I am not quite sure how to go about it (I'm testing using orjson for serialization and deserialization).  This doesn't appear to work:
> 

>> class CyanJSONBDumper(Dumper):
>>      def dump(self, obj):
>>          return orjson.dumps(obj)
>>
>> psycopg.adapters.register_dumper("jsonb", CyanJSONBDumper)

Following:

https://www.psycopg.org/psycopg3/docs/api/abc.html#psycopg.abc.Dumper.dump

I believe it should be something like:

psycopg.adapters.register_dumper(<some_python_object>, CyanJSONBDumper)


-- 
Adrian Klaver
[email protected]





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Overriding default adapters in psycopg
  2025-11-16 16:56 Overriding default adapters in psycopg Christophe Pettus <[email protected]>
  2025-11-16 17:30 ` Re: Overriding default adapters in psycopg Adrian Klaver <[email protected]>
@ 2025-11-16 17:43   ` Christophe Pettus <[email protected]>
  2025-11-16 17:56     ` Re: Overriding default adapters in psycopg Adrian Klaver <[email protected]>
  0 siblings, 1 reply; 5+ messages in thread

From: Christophe Pettus @ 2025-11-16 17:43 UTC (permalink / raw)
  To: Adrian Klaver <[email protected]>; +Cc: [email protected]



> On Nov 16, 2025, at 09:30, Adrian Klaver <[email protected]> wrote:
> 
> https://www.psycopg.org/psycopg3/docs/api/abc.html#psycopg.abc.Dumper.dump
> 
> I believe it should be something like:
> 
> psycopg.adapters.register_dumper(<some_python_object>, CyanJSONBDumper)

Thank you!





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Overriding default adapters in psycopg
  2025-11-16 16:56 Overriding default adapters in psycopg Christophe Pettus <[email protected]>
  2025-11-16 17:30 ` Re: Overriding default adapters in psycopg Adrian Klaver <[email protected]>
  2025-11-16 17:43   ` Re: Overriding default adapters in psycopg Christophe Pettus <[email protected]>
@ 2025-11-16 17:56     ` Adrian Klaver <[email protected]>
  0 siblings, 0 replies; 5+ messages in thread

From: Adrian Klaver @ 2025-11-16 17:56 UTC (permalink / raw)
  To: Christophe Pettus <[email protected]>; +Cc: [email protected]

On 11/16/25 09:43, Christophe Pettus wrote:
> 
> 
>> On Nov 16, 2025, at 09:30, Adrian Klaver <[email protected]> wrote:
>>
>> https://www.psycopg.org/psycopg3/docs/api/abc.html#psycopg.abc.Dumper.dump
>>
>> I believe it should be something like:
>>
>> psycopg.adapters.register_dumper(<some_python_object>, CyanJSONBDumper)
> 
> Thank you!

I tried my original suggestion of using:

https://www.psycopg.org/psycopg3/docs/basic/adapt.html#adapt-json

and it seems to work.

Which this issue:

https://github.com/psycopg/psycopg/pull/568

confirms should be the case.


-- 
Adrian Klaver
[email protected]






^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2025-11-16 17:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-11-16 16:56 Overriding default adapters in psycopg Christophe Pettus <[email protected]>
2025-11-16 17:25 ` Adrian Klaver <[email protected]>
2025-11-16 17:30 ` Adrian Klaver <[email protected]>
2025-11-16 17:43   ` Christophe Pettus <[email protected]>
2025-11-16 17:56     ` Adrian Klaver <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox