Skip site navigation (1) Skip section navigation (2)

Re: JSON in 9.2 - Could we have just one to_json() function instead of two separate versions ?

From: Hannu Krosing <hannu(at)2ndQuadrant(dot)com>
To: David Johnston <polobo(at)yahoo(dot)com>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Merlin Moncure <mmoncure(at)gmail(dot)com>, Joey Adams <joeyadams3(dot)14159(at)gmail(dot)com>, PavelStehule <pavel(dot)stehule(at)gmail(dot)com>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: JSON in 9.2 - Could we have just one to_json() function instead of two separate versions ?
Date: 2012-05-02 08:52:11
Message-ID: 1335948731.3106.402.camel@hvost (view raw or flat)
Thread:
Lists: pgsql-hackers
On Tue, 2012-05-01 at 21:22 -0400, David Johnston wrote:
> On May 1, 2012, at 20:41, Hannu Krosing <hannu(at)2ndQuadrant(dot)com> wrote:
> > 
> > Most people don't work in strongly-typed environment, and thus would
> > work around such restriction if they need a simple JSON value at the
> > other end of the interchange.
> > 
> > 
> >> My personal take it is have it fail since any arbitrary decision to cast 
> >> to JSON Text

For arrays and records the json text and jason value are exactly the
same. it is just that json representations of simple types are
officially not JSON texts.

>  is going to make someone unhappy and supposedly they can 
> >> modify their query so that the result generates whatever format they desire.
> > 
> > Do you actually have such an experience or is it just a wild guess ?
> > 
> > 
> 
> So even given the semantic differences between an object and a scalar 
> I am better understanding where interpreting JSON as JSON Value makes 
> sense.  However, if I convert a record or array to JSON I expect to get 
> a JSON Text even if the there is only a single column or value in the input.  

Of course you will, and you will get a Json Text even for empty object
or array. 

array[1] and 1 and {'one':1} are all different and will stay such.

> I guess my take is that record -> JSON text while anything else is JSON 
> value.  Whether it is worth maiming the special case for record is 
> worthwhile I really do not know but the semantic difference does exist; 
> and record output is a significant aspect of PostgreSQL output.

I have never suggested that we special-case an 1-element record or list
and start returning only the contained value for these.

> I get the ease-of-use aspect but also recognize that sometimes being slightly 
> harder to use is worthwhile if you eliminate ambiguities or limit the 
> possibility to make mistakes.

There are no ambiguities in what is returnded for record or array.

But not being able to return JSON values via cast to json for some types
or not using such casts will make extending the json support for types
by user much much harder. And nonstandard. 

Using simple cast to json is very PostgreSQL-ish way to give support of
json to any type



-- 
-------
Hannu Krosing
PostgreSQL Unlimited Scalability and Performance Consultant
2ndQuadrant Nordic
PG Admin Book: http://www.2ndQuadrant.com/books/


In response to

pgsql-hackers by date

Next:From: Hannu KrosingDate: 2012-05-02 09:29:29
Subject: Re: JSON in 9.2 - Could we have just one to_json() function instead of two separate versions ?
Previous:From: Peter EisentrautDate: 2012-05-02 07:23:29
Subject: Re: proposal: additional error fields

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group