01.02.2017 17:41, Anastasia Lubennikova:
> Now the simplest way to extract booleans and numbers from json/jsonb
> is to cast it
> to text and then cast to the appropriate type:
>
> postgres=# select 'true'::jsonb::text::bool;
> bool
> ------
> t
> postgres=# select '1.0'::jsonb::text::numeric;
> numeric
> ---------
> 1.0
>
>
> This patch implements direct casts from jsonb numeric (jbvNumeric) to
> numeric, int4 and float8,
> and from jsonb bool (jbvBool) to bool.
>
> postgres=# select 'true'::jsonb::bool;
> bool
> ------
> t
>
> postgres=# select '1.0'::jsonb::numeric;
> numeric
> ---------
> 1.0
>
>
> Waiting for your feedback.
> If you find it useful, I can also add support of json and other types,
> such as smallint and bigint.
>
>
I totally forgot about this thread, but it is a small but useful feature.
Maybe it's time to dust it off.
This patch was made by request of one of our clients,
and though they already use custom build, I think it would be better to
have these casts in core.
The patch is attached.
There are no tests yet, since I don't really sure what set of types do
we want to support.
Now the patch provides jsonb to numeric, int4, float8 and bool.
Also I have some doubts about castcontext. But 'explisit' seems to be
the correct one here.
--
Anastasia Lubennikova
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company