Re: array_to_json re-encodes ARRAY of json type - Mailing list pgsql-hackers

From Andrew Dunstan
Subject Re: array_to_json re-encodes ARRAY of json type
Date
Msg-id 4F425224.4010904@dunslane.net
Whole thread Raw
In response to array_to_json re-encodes ARRAY of json type  (Itagaki Takahiro <itagaki.takahiro@gmail.com>)
List pgsql-hackers

On 02/20/2012 07:30 AM, Itagaki Takahiro wrote:
> If we pass an ARRAY of json type to array_to_json() function, the
> function seems to
> re-encode the JSON text. But should the following examples be the same result?
> I'm not sure why we don't have a special case for json type in datum_to_json()
> -- do we need to pass-through json types in it?
>
> =# \x
> =# SELECT '["A"]'::json,
>            array_to_json(ARRAY['A']),
>            array_to_json(ARRAY['"A"'::json]);
> -[ RECORD 1 ]-+----------
> json          | ["A"]
> array_to_json | ["A"]
> array_to_json | ["\"A\""]
>


Hmm, maybe. The trouble is that datum_to_json doesn't know what type 
it's getting, only the type category. We could probably fudge it by 
faking a false one for JSON, say with a lower case 'j', which should be 
fairly future-proof, where the category is detected - for efficiency 
reasons we do this for the whole array rather than for each element of 
the array.

There's another case I have on my list to fix too - some numeric output 
such as "NaN" and "Infinity" are not legal JSON numeric values, and need 
to be quoted in order to avoid generating illegal JSON.

cheers

andrew


pgsql-hackers by date:

Previous
From: Robert Haas
Date:
Subject: Re: Displaying accumulated autovacuum cost
Next
From: "ktm@rice.edu"
Date:
Subject: Re: Qual evaluation cost estimates for GIN indexes