[HACKERS] Cast jsonb to numeric, int, float, bool - Mailing list pgsql-hackers

From Anastasia Lubennikova
Subject [HACKERS] Cast jsonb to numeric, int, float, bool
Date
Msg-id 0154d35a-24ae-f063-5273-9ffcdf1c7f2e@postgrespro.ru
Whole thread Raw
Responses Re: [HACKERS] Cast jsonb to numeric, int, float, bool  (Nikita Glukhov <n.gluhov@postgrespro.ru>)
Re: [HACKERS] Cast jsonb to numeric, int, float, bool  (Anastasia Lubennikova <a.lubennikova@postgrespro.ru>)
List pgsql-hackers
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.

-- 
Anastasia Lubennikova
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Attachment

pgsql-hackers by date:

Previous
From: Rafia Sabih
Date:
Subject: Re: [HACKERS] WIP: [[Parallel] Shared] Hash
Next
From: Vik Fearing
Date:
Subject: Re: [HACKERS] new autovacuum criterion for visible pages