Re: Should the JSON datatype be a specialization of text?

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Joseph Adams <joeyadams3(dot)14159(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Should the JSON datatype be a specialization of text?
Date: 2010-06-17 16:31:55
Message-ID: AANLkTinNcPQFmp8No0_WZazNeUTwvPbvPUbI-i4KVUhN@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Thu, Jun 17, 2010 at 11:25 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Robert Haas <robertmhaas(at)gmail(dot)com> writes:
>> On Thu, Jun 17, 2010 at 2:29 AM, Joseph Adams
>> <joeyadams3(dot)14159(at)gmail(dot)com> wrote:
>>>        * No surprises when casting between JSON and TEXT.  If approach B is
>>> used, '"string"'::json would be '"string"', but '"string"'::json::text
>>> would be 'string'.
>
>> As far as I'm concerned, that's a non-starter.  It should be legal to
>> cast text to json, but what it should do is validate that the string
>> is already legal JSON, not quote it as a string.
>
> I'm not really convinced about that.  It seems clear to me that there
> are two behaviors that we'd like:
>
> 1. Take a string that is legal JSON, and make it into a JSON object.
>
> 2. Take an arbitrary string (or a number, a bool, etc) and make it a
> literal value within a JSON object.
>
> We can make one of these behaviors be invoked by a cast, and the other
> by an explicit function call --- the question is which is which.

Up to this point I agree.

> I'm
> inclined to think that associating #2 with casts might be better,
> because clearly casting numerics or bools to JSON ought to act like #2.
> If we do it as you suggest then casting text to JSON behaves differently
> from casting anything else to JSON.

I think this is going to turn into a thicket of semantic ambiguity.
There are also two things you might want on output - (1) take a JSON
object and export it as a string; (2) take a JSON object and extract
from it some natively typed thing. So what happens, for example, when
someone writes:

json 'true'

Do they get a JSON boolean or a JSON text? i.e. true or 'true'?

Joseph's proposal also involved foo::text::json::text <> foo::text,
which seems pretty ugly to me.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise Postgres Company

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2010-06-17 16:43:13 Re: streaming replication breaks horribly if master crashes
Previous Message Joshua D. Drake 2010-06-17 15:31:21 Re: ANNOUNCE list (was Re: [HACKERS] New PGXN Extension site)