Commit 502a3832 authored by Andrew Dunstan's avatar Andrew Dunstan

Correctly handle array pseudotypes in to_json and to_jsonb

Columns with array pseudotypes have not been identified as arrays, so
they have been rendered as strings in the json and jsonb conversion
routines. This change allows them to be rendered as json arrays, making
it possible to deal correctly with the anyarray columns in pg_stats.
parent 4c728f38
...@@ -1397,9 +1397,10 @@ json_categorize_type(Oid typoid, ...@@ -1397,9 +1397,10 @@ json_categorize_type(Oid typoid,
default: default:
/* Check for arrays and composites */ /* Check for arrays and composites */
if (OidIsValid(get_element_type(typoid))) if (OidIsValid(get_element_type(typoid)) || typoid == ANYARRAYOID
|| typoid == RECORDARRAYOID)
*tcategory = JSONTYPE_ARRAY; *tcategory = JSONTYPE_ARRAY;
else if (type_is_rowtype(typoid)) else if (type_is_rowtype(typoid)) /* includes RECORDOID */
*tcategory = JSONTYPE_COMPOSITE; *tcategory = JSONTYPE_COMPOSITE;
else else
{ {
......
...@@ -644,9 +644,10 @@ jsonb_categorize_type(Oid typoid, ...@@ -644,9 +644,10 @@ jsonb_categorize_type(Oid typoid,
default: default:
/* Check for arrays and composites */ /* Check for arrays and composites */
if (OidIsValid(get_element_type(typoid))) if (OidIsValid(get_element_type(typoid)) || typoid == ANYARRAYOID
|| typoid == RECORDARRAYOID)
*tcategory = JSONBTYPE_ARRAY; *tcategory = JSONBTYPE_ARRAY;
else if (type_is_rowtype(typoid)) else if (type_is_rowtype(typoid)) /* includes RECORDOID */
*tcategory = JSONBTYPE_COMPOSITE; *tcategory = JSONBTYPE_COMPOSITE;
else else
{ {
......
...@@ -383,6 +383,15 @@ SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)), ...@@ -383,6 +383,15 @@ SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),
{"f1":[5,6,7,8,9,10]} {"f1":[5,6,7,8,9,10]}
(1 row) (1 row)
-- anyarray column
select to_json(histogram_bounds) histogram_bounds
from pg_stats
where attname = 'tmplname' and tablename = 'pg_pltemplate';
histogram_bounds
---------------------------------------------------------------------------------------
["plperl","plperlu","plpgsql","plpython2u","plpython3u","plpythonu","pltcl","pltclu"]
(1 row)
-- to_json, timestamps -- to_json, timestamps
select to_json(timestamp '2014-05-28 12:22:35.614298'); select to_json(timestamp '2014-05-28 12:22:35.614298');
to_json to_json
......
...@@ -279,6 +279,15 @@ SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']); ...@@ -279,6 +279,15 @@ SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']);
[{"a": 1},{"b": [2, 3]}] [{"a": 1},{"b": [2, 3]}]
(1 row) (1 row)
-- anyarray column
select to_jsonb(histogram_bounds) histogram_bounds
from pg_stats
where attname = 'tmplname' and tablename = 'pg_pltemplate';
histogram_bounds
----------------------------------------------------------------------------------------------
["plperl", "plperlu", "plpgsql", "plpython2u", "plpython3u", "plpythonu", "pltcl", "pltclu"]
(1 row)
-- to_jsonb, timestamps -- to_jsonb, timestamps
select to_jsonb(timestamp '2014-05-28 12:22:35.614298'); select to_jsonb(timestamp '2014-05-28 12:22:35.614298');
to_jsonb to_jsonb
......
...@@ -102,6 +102,12 @@ FROM rows q; ...@@ -102,6 +102,12 @@ FROM rows q;
SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false); SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false);
-- anyarray column
select to_json(histogram_bounds) histogram_bounds
from pg_stats
where attname = 'tmplname' and tablename = 'pg_pltemplate';
-- to_json, timestamps -- to_json, timestamps
select to_json(timestamp '2014-05-28 12:22:35.614298'); select to_json(timestamp '2014-05-28 12:22:35.614298');
......
...@@ -62,6 +62,12 @@ SELECT ' '::jsonb; -- ERROR, no value ...@@ -62,6 +62,12 @@ SELECT ' '::jsonb; -- ERROR, no value
-- make sure jsonb is passed through json generators without being escaped -- make sure jsonb is passed through json generators without being escaped
SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']); SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']);
-- anyarray column
select to_jsonb(histogram_bounds) histogram_bounds
from pg_stats
where attname = 'tmplname' and tablename = 'pg_pltemplate';
-- to_jsonb, timestamps -- to_jsonb, timestamps
select to_jsonb(timestamp '2014-05-28 12:22:35.614298'); select to_jsonb(timestamp '2014-05-28 12:22:35.614298');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment