Skip to content

Commit c8a026e

Browse files
committed
Revert 95d737f to add 'ignore_nulls'
Per discussion, revert the commit which added 'ignore_nulls' to row_to_json. This capability would be better added as an independent function rather than being bolted on to row_to_json. Additionally, the implementation didn't address complex JSON objects, and so was incomplete anyway. Pointed out by Tom and discussed with Andrew and Robert.
1 parent def4c28 commit c8a026e

File tree

8 files changed

+51
-118
lines changed

8 files changed

+51
-118
lines changed

doc/src/sgml/func.sgml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10309,13 +10309,11 @@ table2-mapping
1030910309
</row>
1031010310
<row>
1031110311
<entry>
10312-
<literal>row_to_json(rowval record [, pretty bool [, ignore_nulls bool] ])</literal>
10312+
<literal>row_to_json(record [, pretty_bool])</literal>
1031310313
</entry>
1031410314
<entry>
1031510315
Returns the row as a JSON object. Line feeds will be added between
10316-
level-1 elements if <parameter>pretty_bool</parameter> is true. Elements
10317-
with NULL values will be skipped when <parameter>ignore_nulls</parameter>
10318-
is true.
10316+
level-1 elements if <parameter>pretty_bool</parameter> is true.
1031910317
</entry>
1032010318
<entry><literal>row_to_json(row(1,'foo'))</literal></entry>
1032110319
<entry><literal>{"f1":1,"f2":"foo"}</literal></entry>

src/backend/catalog/system_views.sql

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -897,17 +897,3 @@ RETURNS interval
897897
LANGUAGE INTERNAL
898898
STRICT IMMUTABLE
899899
AS 'make_interval';
900-
901-
CREATE OR REPLACE FUNCTION
902-
row_to_json(rowval record, pretty boolean DEFAULT false, ignore_nulls boolean DEFAULT false)
903-
RETURNS json
904-
LANGUAGE INTERNAL
905-
STRICT STABLE
906-
AS 'row_to_json';
907-
908-
CREATE OR REPLACE FUNCTION
909-
array_to_json(arrayval anyarray, pretty boolean DEFAULT false)
910-
RETURNS json
911-
LANGUAGE INTERNAL
912-
STRICT STABLE
913-
AS 'array_to_json';

src/backend/utils/adt/json.c

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ static void report_invalid_token(JsonLexContext *lex);
7979
static int report_json_context(JsonLexContext *lex);
8080
static char *extract_mb_char(char *s);
8181
static void composite_to_json(Datum composite, StringInfo result,
82-
bool use_line_feeds,
83-
bool ignore_nulls);
82+
bool use_line_feeds);
8483
static void array_dim_to_json(StringInfo result, int dim, int ndims, int *dims,
8584
Datum *vals, bool *nulls, int *valcount,
8685
JsonTypeCategory tcategory, Oid outfuncoid,
@@ -1366,7 +1365,7 @@ datum_to_json(Datum val, bool is_null, StringInfo result,
13661365
array_to_json_internal(val, result, false);
13671366
break;
13681367
case JSONTYPE_COMPOSITE:
1369-
composite_to_json(val, result, false, false);
1368+
composite_to_json(val, result, false);
13701369
break;
13711370
case JSONTYPE_BOOL:
13721371
outputstr = DatumGetBool(val) ? "true" : "false";
@@ -1591,8 +1590,7 @@ array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
15911590
* Turn a composite / record into JSON.
15921591
*/
15931592
static void
1594-
composite_to_json(Datum composite, StringInfo result, bool use_line_feeds,
1595-
bool ignore_nulls)
1593+
composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
15961594
{
15971595
HeapTupleHeader td;
15981596
Oid tupType;
@@ -1631,12 +1629,6 @@ composite_to_json(Datum composite, StringInfo result, bool use_line_feeds,
16311629
if (tupdesc->attrs[i]->attisdropped)
16321630
continue;
16331631

1634-
val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
1635-
1636-
/* Don't serialize NULL field when we don't want it */
1637-
if (isnull && ignore_nulls)
1638-
continue;
1639-
16401632
if (needsep)
16411633
appendStringInfoString(result, sep);
16421634
needsep = true;
@@ -1645,6 +1637,8 @@ composite_to_json(Datum composite, StringInfo result, bool use_line_feeds,
16451637
escape_json(result, attname);
16461638
appendStringInfoChar(result, ':');
16471639

1640+
val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
1641+
16481642
if (isnull)
16491643
{
16501644
tcategory = JSONTYPE_NULL;
@@ -1693,10 +1687,26 @@ add_json(Datum val, bool is_null, StringInfo result,
16931687
}
16941688

16951689
/*
1696-
* SQL function array_to_json(row, prettybool)
1690+
* SQL function array_to_json(row)
16971691
*/
16981692
extern Datum
16991693
array_to_json(PG_FUNCTION_ARGS)
1694+
{
1695+
Datum array = PG_GETARG_DATUM(0);
1696+
StringInfo result;
1697+
1698+
result = makeStringInfo();
1699+
1700+
array_to_json_internal(array, result, false);
1701+
1702+
PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1703+
}
1704+
1705+
/*
1706+
* SQL function array_to_json(row, prettybool)
1707+
*/
1708+
extern Datum
1709+
array_to_json_pretty(PG_FUNCTION_ARGS)
17001710
{
17011711
Datum array = PG_GETARG_DATUM(0);
17021712
bool use_line_feeds = PG_GETARG_BOOL(1);
@@ -1710,19 +1720,34 @@ array_to_json(PG_FUNCTION_ARGS)
17101720
}
17111721

17121722
/*
1713-
* SQL function row_to_json(rowval record, pretty bool, ignore_nulls bool)
1723+
* SQL function row_to_json(row)
17141724
*/
17151725
extern Datum
17161726
row_to_json(PG_FUNCTION_ARGS)
1727+
{
1728+
Datum array = PG_GETARG_DATUM(0);
1729+
StringInfo result;
1730+
1731+
result = makeStringInfo();
1732+
1733+
composite_to_json(array, result, false);
1734+
1735+
PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
1736+
}
1737+
1738+
/*
1739+
* SQL function row_to_json(row, prettybool)
1740+
*/
1741+
extern Datum
1742+
row_to_json_pretty(PG_FUNCTION_ARGS)
17171743
{
17181744
Datum array = PG_GETARG_DATUM(0);
17191745
bool use_line_feeds = PG_GETARG_BOOL(1);
1720-
bool ignore_nulls = PG_GETARG_BOOL(2);
17211746
StringInfo result;
17221747

17231748
result = makeStringInfo();
17241749

1725-
composite_to_json(array, result, use_line_feeds, ignore_nulls);
1750+
composite_to_json(array, result, use_line_feeds);
17261751

17271752
PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
17281753
}

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201409292
56+
#define CATALOG_VERSION_NO 201409293
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4203,10 +4203,14 @@ DATA(insert OID = 323 ( json_recv PGNSP PGUID 12 1 0 0 0 f f f f t f i 1 0
42034203
DESCR("I/O");
42044204
DATA(insert OID = 324 ( json_send PGNSP PGUID 12 1 0 0 0 f f f f t f i 1 0 17 "114" _null_ _null_ _null_ _null_ json_send _null_ _null_ _null_ ));
42054205
DESCR("I/O");
4206-
DATA(insert OID = 3153 ( array_to_json PGNSP PGUID 12 1 0 0 0 f f f f t f s 2 0 114 "2277 16" _null_ _null_ "{arrayval,pretty}" _null_ array_to_json _null_ _null_ _null_ ));
4206+
DATA(insert OID = 3153 ( array_to_json PGNSP PGUID 12 1 0 0 0 f f f f t f s 1 0 114 "2277" _null_ _null_ _null_ _null_ array_to_json _null_ _null_ _null_ ));
42074207
DESCR("map array to json");
4208-
DATA(insert OID = 3155 ( row_to_json PGNSP PGUID 12 1 0 0 0 f f f f t f s 3 0 114 "2249 16 16" _null_ _null_ "{rowval,pretty,ignore_nulls}" _null_ row_to_json _null_ _null_ _null_ ));
4208+
DATA(insert OID = 3154 ( array_to_json PGNSP PGUID 12 1 0 0 0 f f f f t f s 2 0 114 "2277 16" _null_ _null_ _null_ _null_ array_to_json_pretty _null_ _null_ _null_ ));
4209+
DESCR("map array to json with optional pretty printing");
4210+
DATA(insert OID = 3155 ( row_to_json PGNSP PGUID 12 1 0 0 0 f f f f t f s 1 0 114 "2249" _null_ _null_ _null_ _null_ row_to_json _null_ _null_ _null_ ));
42094211
DESCR("map row to json");
4212+
DATA(insert OID = 3156 ( row_to_json PGNSP PGUID 12 1 0 0 0 f f f f t f s 2 0 114 "2249 16" _null_ _null_ _null_ _null_ row_to_json_pretty _null_ _null_ _null_ ));
4213+
DESCR("map row to json with optional pretty printing");
42104214
DATA(insert OID = 3173 ( json_agg_transfn PGNSP PGUID 12 1 0 0 0 f f f f f f i 2 0 2281 "2281 2283" _null_ _null_ _null_ _null_ json_agg_transfn _null_ _null_ _null_ ));
42114215
DESCR("json aggregate transition function");
42124216
DATA(insert OID = 3174 ( json_agg_finalfn PGNSP PGUID 12 1 0 0 0 f f f f f f i 1 0 114 "2281" _null_ _null_ _null_ _null_ json_agg_finalfn _null_ _null_ _null_ ));

src/include/utils/json.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ extern Datum json_out(PG_FUNCTION_ARGS);
2323
extern Datum json_recv(PG_FUNCTION_ARGS);
2424
extern Datum json_send(PG_FUNCTION_ARGS);
2525
extern Datum array_to_json(PG_FUNCTION_ARGS);
26+
extern Datum array_to_json_pretty(PG_FUNCTION_ARGS);
2627
extern Datum row_to_json(PG_FUNCTION_ARGS);
28+
extern Datum row_to_json_pretty(PG_FUNCTION_ARGS);
2729
extern Datum to_json(PG_FUNCTION_ARGS);
2830

2931
extern Datum json_agg_transfn(PG_FUNCTION_ARGS);

src/test/regress/expected/json.out

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -397,70 +397,12 @@ FROM rows q;
397397
"y":"txt3"}
398398
(3 rows)
399399

400-
SELECT row_to_json(q,pretty := true)
401-
FROM rows q;
402-
row_to_json
403-
--------------
404-
{"x":1, +
405-
"y":"txt1"}
406-
{"x":2, +
407-
"y":"txt2"}
408-
{"x":3, +
409-
"y":"txt3"}
410-
(3 rows)
411-
412400
SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false);
413401
row_to_json
414402
-----------------------
415403
{"f1":[5,6,7,8,9,10]}
416404
(1 row)
417405

418-
WITH x AS (SELECT a,b,c FROM (VALUES(10,20,30),
419-
(10,NULL, NULL),
420-
(NULL, NULL, NULL)) g(a,b,c))
421-
SELECT row_to_json(x, false, false) FROM x;
422-
row_to_json
423-
------------------------------
424-
{"a":10,"b":20,"c":30}
425-
{"a":10,"b":null,"c":null}
426-
{"a":null,"b":null,"c":null}
427-
(3 rows)
428-
429-
WITH x AS (SELECT a,b,c FROM (VALUES(10,20,30),
430-
(10,NULL, NULL),
431-
(NULL, NULL, NULL)) g(a,b,c))
432-
SELECT row_to_json(x, false, true) FROM x;
433-
row_to_json
434-
------------------------
435-
{"a":10,"b":20,"c":30}
436-
{"a":10}
437-
{}
438-
(3 rows)
439-
440-
WITH x AS (SELECT a,b,c FROM (VALUES(10,20,30),
441-
(10,NULL, NULL),
442-
(NULL, NULL, NULL)) g(a,b,c))
443-
SELECT row_to_json(x, ignore_nulls := true) FROM x;
444-
row_to_json
445-
------------------------
446-
{"a":10,"b":20,"c":30}
447-
{"a":10}
448-
{}
449-
(3 rows)
450-
451-
WITH x AS (SELECT a,b,c FROM (VALUES(10,20,30),
452-
(10,NULL, NULL),
453-
(NULL, NULL, NULL)) g(a,b,c))
454-
SELECT row_to_json(x, ignore_nulls := true, pretty := true) FROM x;
455-
row_to_json
456-
-------------
457-
{"a":10, +
458-
"b":20, +
459-
"c":30}
460-
{"a":10}
461-
{}
462-
(3 rows)
463-
464406
-- to_json, timestamps
465407
select to_json(timestamp '2014-05-28 12:22:35.614298');
466408
to_json

src/test/regress/sql/json.sql

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,32 +98,8 @@ FROM generate_series(1,3) AS x;
9898
SELECT row_to_json(q,true)
9999
FROM rows q;
100100

101-
SELECT row_to_json(q,pretty := true)
102-
FROM rows q;
103-
104101
SELECT row_to_json(row((select array_agg(x) as d from generate_series(5,10) x)),false);
105102

106-
WITH x AS (SELECT a,b,c FROM (VALUES(10,20,30),
107-
(10,NULL, NULL),
108-
(NULL, NULL, NULL)) g(a,b,c))
109-
SELECT row_to_json(x, false, false) FROM x;
110-
111-
WITH x AS (SELECT a,b,c FROM (VALUES(10,20,30),
112-
(10,NULL, NULL),
113-
(NULL, NULL, NULL)) g(a,b,c))
114-
SELECT row_to_json(x, false, true) FROM x;
115-
116-
WITH x AS (SELECT a,b,c FROM (VALUES(10,20,30),
117-
(10,NULL, NULL),
118-
(NULL, NULL, NULL)) g(a,b,c))
119-
SELECT row_to_json(x, ignore_nulls := true) FROM x;
120-
121-
WITH x AS (SELECT a,b,c FROM (VALUES(10,20,30),
122-
(10,NULL, NULL),
123-
(NULL, NULL, NULL)) g(a,b,c))
124-
SELECT row_to_json(x, ignore_nulls := true, pretty := true) FROM x;
125-
126-
127103
-- to_json, timestamps
128104

129105
select to_json(timestamp '2014-05-28 12:22:35.614298');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy