Re: JSON for PG 9.2

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: Josh Berkus <josh(at)agliodbs(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: JSON for PG 9.2
Date: 2012-02-01 05:46:55
Message-ID: 4F28D1CF.7040007@dunslane.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 01/31/2012 01:48 PM, Andrew Dunstan wrote:
>
>
> On 01/31/2012 01:32 PM, Merlin Moncure wrote:
>> On Tue, Jan 31, 2012 at 12:15 PM, Josh Berkus<josh(at)agliodbs(dot)com> wrote:
>>> Andrew,
>>>
>>>> based on Abhijit's feeling and some discussion offline, the consensus
>>>> seems to be to remove query_to_json.
>>> If we do that, what would getting complete query results back from a
>>> query look like? It's important to make this as simple for developers
>>> as possible.
>> two options:
>> 1. row_to_json(rowvar)
>> SELECT row_to_json(foo) from foo;
>> SELECT row_to_json(row(a,b,c)) from foo;
>>
>> 2. array_to_json(array_agg()/array())
>> SELECT array_to_json(array(select foo from foo));
>> SELECT array_to_json(array[1,2,3]);
>>
>> #1 I expect will be the more used version -- most json handling client
>> side api (for example node.js drivers) are optimized for row by row
>> processing, but via #2 you can stuff a whole query into single json
>> object if you're so inclined.
>>
>
> You could also write a wrapper something like this:
>
> create function query_to_json(qtext text) returns json language
> plpgsql as
> $$
> begin
> return query execute 'select array_to_json(array(' || qtext ||
> '))';
> end;
> $$;

The array(select...) locution turns out to have less flexibility than
the array_agg(record-ref) locution.

Anyway, for those playing along, I have removed query_to_json, and added
optional pretty printing to array_to_json and row_to_json. WIP can be
seen at <https://bitbucket.org/adunstan/pgdevel>. I still have docs and
output function caching to do, and should post a revised patch within
the next 48 hours.

cheers

andrew

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2012-02-01 06:17:31 feature request - datum_compute_size and datum write_should be public
Previous Message Andrew Dunstan 2012-02-01 04:10:32 Re: [GENERAL] pg_dump -s dumps data?!