Re: JSON type caster

From: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
To: Tobias Oberstein <tobias(dot)oberstein(at)gmail(dot)com>
Cc: psycopg(at)postgresql(dot)org
Subject: Re: JSON type caster
Date: 2012-09-19 15:43:54
Message-ID: CA+mi_8Zz0q1_-+zW-GfY3CbcT_cikwc9+UAhxDkkSmrF7z3emQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

On Wed, Sep 19, 2012 at 2:42 PM, Tobias Oberstein
<tobias(dot)oberstein(at)gmail(dot)com> wrote:
>> Uhm... but what about a new function? To be called
>>
>> psycopg2.extras.register_default_json(globally=True, loads=loads)
>>
>> or
>> conn = psycopg2.connect(...)
>> psycopg2.extras.register_default_json(conn, loads=loads)
>>
>
> +1 for that:
>
> it provides a simple to use interface for the use case "PG >=9.2 and global
> custom json"

Done (this function plus the conn_or_curs argument optional if not
required, instead of requiring an explicit None)

>> class Json(object):
>> def __init__(self, adapted, dumps=None):
>> self.adapted = adapted
>> self._dumps = dumps is None and json.dumps or dumps

> +1 for that also ..adding dump argument and dropping kwargs forwarding,
> because customizing via kwargs on a per Json-instance basis is probably the
> less likely case compared to "I want to use one custom loads/dumps
> everywhere".

Done this too.

> Mmh. There is still some asymmetry: loads is configured
> globally or per connection, dumps is configured "per argument instance"
> (Json instance) ..

Yes, this is a little bit harder to kill. Json is peculiar on not
having a specific class to deal with, so the use of the adapter is
more explicit than with other data type. Modulo this issue, adapters
are only global whereas typecasters can be per connection or per
cursor too. I think it could be possible and desirable to register
adapters on more ristrected scopes.

-- Daniele

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message Jonathan S. Katz 2012-09-19 16:59:47 Re: Range Type Support
Previous Message Tobias Oberstein 2012-09-19 13:42:33 Re: JSON type caster