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-18 16:44:09
Message-ID: CA+mi_8bRAZP107BF5hrTMy4VaK3BGsApcU=r-T2ksrQvc0KpCg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

On Tue, Sep 18, 2012 at 4:57 PM, Tobias Oberstein
<tobias(dot)oberstein(at)gmail(dot)com> wrote:

> Personally, I am mainly calling stored procedures, i.e.
>
> select proname, prorettype, proargtypes from pg_proc where proname =
> 'create_employee';
>
> create_employee,114,114
>
> => a SP which takes 1 JSON arg, and has return type JSON.
>
> The return type casting is taken care of by the (new) SQL=>Py JSON caster.
>
> The argument type isn't handled automatically.
>
> Given the stuff above (Json() thin wrapper), I still need to determine
> the input arg type from pg_proc ... there isn't any "magic" I can turn
> on with Psycopg, right?

Right, this would be the shortcoming. Unfortunately there is no Python
class representing a json, and the mapping object -> adapter is by
python class only. I assume your create_employee function cannot take
*any* json: it may be expecting e.g. a dict and not a list, and maybe
some specific attributes while ignoring others. For this case it's
probably easy to create a Python object that knows how to adapt to
json syntax. But in the generic case a Json can be a python string, or
a list, or a dict, or None, each of which have a more reasonable
Postgres representation.

If anybody comes with a better solution than the Json wrapper I'd be
happy to know.

BTW, because of the zope problem discussed today, I'd be happy to have
a release soon, so if nobody steps ahead in the next few hours, I'll
try and implement the json support tonight and have it for psycopg
2.4.6.

-- Daniele

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message Jonathan S. Katz 2012-09-18 16:57:36 Range Type Support
Previous Message Tobias Oberstein 2012-09-18 15:57:35 Re: JSON type caster