Re: JSON manipulation functions - Mailing list pgsql-hackers

From Joseph Adams
Subject Re: JSON manipulation functions
Date
Msg-id AANLkTik4vcQEkhJvLHzITKmMe--K8TWGU9ixW0agIGtJ@mail.gmail.com
Whole thread Raw
In response to Re: JSON manipulation functions  (Robert Haas <robertmhaas@gmail.com>)
Responses Re: JSON manipulation functions
Re: JSON manipulation functions
List pgsql-hackers
I've started implementing the JSON datatype; the repo is at
http://git.postgresql.org/gitweb?p=json-datatype.git .

On Fri, May 14, 2010 at 1:15 PM, Robert Haas <robertmhaas@gmail.com> wrote:
> On Thu, May 13, 2010 at 9:47 PM, Joseph Adams
> <joeyadams3.14159@gmail.com> wrote:
>> Would it be a bad idea to give an enum and a function the same name
>> (which appears to be allowed by PostgreSQL) ?  If so, json_type(json)
>> could be json_typeof(json) or something instead.
>
> No, I think that's a fine idea.

I tried making a function named json_type that has the same name as
the type json_type.  However, this doesn't work as expected:

SELECT json_type('[1,2,3]');

Instead of calling json_type with '[1,2,3]' casted to JSON, it's
trying to cast '[1,2,3]' to json_type.  Is there a way to override
this behavior, or would I be better off renaming the function?

Note that if the function were renamed, the literal would implicitly be json:

SELECT json_typeof('[1,2,3]'); -- works

I tried this:

CREATE OR REPLACE FUNCTION json_type(json)
RETURNS json_type
AS 'MODULE_PATHNAME','json_get_type'
LANGUAGE C STRICT IMMUTABLE;

CREATE CAST (json AS json_type) WITH FUNCTION json_type(json);

However, json_type('[1,2,3]') still doesn't work (it doesn't infer
that '[1,2,3]' should be casted to json first).  I also tried each of
AS ASSIGNMENT and AS IMPLICIT as well.


pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: [ADMIN] command tag logging
Next
From: Robert Haas
Date:
Subject: Re: JSON manipulation functions