Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc8a026e

Browse files
committed
Revert95d737f to add 'ignore_nulls'
Per discussion, revert the commit which added 'ignore_nulls' torow_to_json. This capability would be better added as an independentfunction rather than being bolted on to row_to_json. Additionally,the implementation didn't address complex JSON objects, and so wasincomplete anyway.Pointed out by Tom and discussed with Andrew and Robert.
1 parentdef4c28 commitc8a026e

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(rowvalrecord [,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-
CREATEOR REPLACE FUNCTION
902-
row_to_json(rowval record, prettyboolean DEFAULT false, ignore_nullsboolean DEFAULT false)
903-
RETURNS json
904-
LANGUAGE INTERNAL
905-
STRICT STABLE
906-
AS'row_to_json';
907-
908-
CREATEOR REPLACE FUNCTION
909-
array_to_json(arrayval anyarray, prettyboolean 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
staticintreport_json_context(JsonLexContext*lex);
8080
staticchar*extract_mb_char(char*s);
8181
staticvoidcomposite_to_json(Datumcomposite,StringInforesult,
82-
booluse_line_feeds,
83-
boolignore_nulls);
82+
booluse_line_feeds);
8483
staticvoidarray_dim_to_json(StringInforesult,intdim,intndims,int*dims,
8584
Datum*vals,bool*nulls,int*valcount,
8685
JsonTypeCategorytcategory,Oidoutfuncoid,
@@ -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
caseJSONTYPE_COMPOSITE:
1369-
composite_to_json(val,result, false, false);
1368+
composite_to_json(val,result, false);
13701369
break;
13711370
caseJSONTYPE_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
staticvoid
1594-
composite_to_json(Datumcomposite,StringInforesult,booluse_line_feeds,
1595-
boolignore_nulls)
1593+
composite_to_json(Datumcomposite,StringInforesult,booluse_line_feeds)
15961594
{
15971595
HeapTupleHeadertd;
15981596
OidtupType;
@@ -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
externDatum
16991693
array_to_json(PG_FUNCTION_ARGS)
1694+
{
1695+
Datumarray=PG_GETARG_DATUM(0);
1696+
StringInforesult;
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+
externDatum
1709+
array_to_json_pretty(PG_FUNCTION_ARGS)
17001710
{
17011711
Datumarray=PG_GETARG_DATUM(0);
17021712
booluse_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
externDatum
17161726
row_to_json(PG_FUNCTION_ARGS)
1727+
{
1728+
Datumarray=PG_GETARG_DATUM(0);
1729+
StringInforesult;
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+
externDatum
1742+
row_to_json_pretty(PG_FUNCTION_ARGS)
17171743
{
17181744
Datumarray=PG_GETARG_DATUM(0);
17191745
booluse_line_feeds=PG_GETARG_BOOL(1);
1720-
boolignore_nulls=PG_GETARG_BOOL(2);
17211746
StringInforesult;
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-
#defineCATALOG_VERSION_NO201409292
56+
#defineCATALOG_VERSION_NO201409293
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(insertOID=324 (json_sendPGNSPPGUID121000fffftfi1017"114"_null__null__null__null_json_send_null__null__null_ ));
42054205
DESCR("I/O");
4206-
DATA(insertOID=3153 (array_to_jsonPGNSPPGUID121000fffftfs20114"2277 16"_null__null_"{arrayval,pretty}"_null_array_to_json_null__null__null_ ));
4206+
DATA(insertOID=3153 (array_to_jsonPGNSPPGUID121000fffftfs10114"2277"_null__null__null__null_array_to_json_null__null__null_ ));
42074207
DESCR("map array to json");
4208-
DATA(insertOID=3155 (row_to_jsonPGNSPPGUID121000fffftfs30114"2249 16 16"_null__null_"{rowval,pretty,ignore_nulls}"_null_row_to_json_null__null__null_ ));
4208+
DATA(insertOID=3154 (array_to_jsonPGNSPPGUID121000fffftfs20114"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(insertOID=3155 (row_to_jsonPGNSPPGUID121000fffftfs10114"2249"_null__null__null__null_row_to_json_null__null__null_ ));
42094211
DESCR("map row to json");
4212+
DATA(insertOID=3156 (row_to_jsonPGNSPPGUID121000fffftfs20114"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(insertOID=3173 (json_agg_transfnPGNSPPGUID121000ffffffi202281"2281 2283"_null__null__null__null_json_agg_transfn_null__null__null_ ));
42114215
DESCR("json aggregate transition function");
42124216
DATA(insertOID=3174 (json_agg_finalfnPGNSPPGUID121000ffffffi10114"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
externDatumjson_recv(PG_FUNCTION_ARGS);
2424
externDatumjson_send(PG_FUNCTION_ARGS);
2525
externDatumarray_to_json(PG_FUNCTION_ARGS);
26+
externDatumarray_to_json_pretty(PG_FUNCTION_ARGS);
2627
externDatumrow_to_json(PG_FUNCTION_ARGS);
28+
externDatumrow_to_json_pretty(PG_FUNCTION_ARGS);
2729
externDatumto_json(PG_FUNCTION_ARGS);
2830

2931
externDatumjson_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 dfrom generate_series(5,10) x)),false);
105102

106-
WITH xAS (SELECT a,b,cFROM (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 xAS (SELECT a,b,cFROM (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 xAS (SELECT a,b,cFROM (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 xAS (SELECT a,b,cFROM (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)

[8]ページ先頭

©2009-2025 Movatter.jp