@@ -79,7 +79,8 @@ static void report_invalid_token(JsonLexContext *lex);
7979static int report_json_context (JsonLexContext * lex );
8080static char * extract_mb_char (char * s );
8181static void composite_to_json (Datum composite , StringInfo result ,
82- bool use_line_feeds );
82+ bool use_line_feeds ,
83+ bool ignore_nulls );
8384static void array_dim_to_json (StringInfo result , int dim , int ndims , int * dims ,
8485 Datum * vals , bool * nulls , int * valcount ,
8586 JsonTypeCategory tcategory , Oid outfuncoid ,
@@ -1362,7 +1363,7 @@ datum_to_json(Datum val, bool is_null, StringInfo result,
13621363 array_to_json_internal (val , result , false);
13631364 break ;
13641365 case JSONTYPE_COMPOSITE :
1365- composite_to_json (val , result , false);
1366+ composite_to_json (val , result , false, false );
13661367 break ;
13671368 case JSONTYPE_BOOL :
13681369 outputstr = DatumGetBool (val ) ? "true" : "false" ;
@@ -1591,7 +1592,8 @@ array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
15911592 * Turn a composite / record into JSON.
15921593 */
15931594static void
1594- composite_to_json (Datum composite , StringInfo result , bool use_line_feeds )
1595+ composite_to_json (Datum composite , StringInfo result , bool use_line_feeds ,
1596+ bool ignore_nulls )
15951597{
15961598 HeapTupleHeader td ;
15971599 Oid tupType ;
@@ -1630,6 +1632,12 @@ composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
16301632 if (tupdesc -> attrs [i ]-> attisdropped )
16311633 continue ;
16321634
1635+ val = heap_getattr (tuple , i + 1 , tupdesc , & isnull );
1636+
1637+ /* Don't serialize NULL field when we don't want it */
1638+ if (isnull && ignore_nulls )
1639+ continue ;
1640+
16331641 if (needsep )
16341642 appendStringInfoString (result , sep );
16351643 needsep = true;
@@ -1638,8 +1646,6 @@ composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
16381646 escape_json (result , attname );
16391647 appendStringInfoChar (result , ':' );
16401648
1641- val = heap_getattr (tuple , i + 1 , tupdesc , & isnull );
1642-
16431649 if (isnull )
16441650 {
16451651 tcategory = JSONTYPE_NULL ;
@@ -1687,27 +1693,11 @@ add_json(Datum val, bool is_null, StringInfo result,
16871693 datum_to_json (val , is_null , result , tcategory , outfuncoid , key_scalar );
16881694}
16891695
1690- /*
1691- * SQL function array_to_json(row)
1692- */
1693- extern Datum
1694- array_to_json (PG_FUNCTION_ARGS )
1695- {
1696- Datum array = PG_GETARG_DATUM (0 );
1697- StringInfo result ;
1698-
1699- result = makeStringInfo ();
1700-
1701- array_to_json_internal (array , result , false);
1702-
1703- PG_RETURN_TEXT_P (cstring_to_text_with_len (result -> data , result -> len ));
1704- }
1705-
17061696/*
17071697 * SQL function array_to_json(row, prettybool)
17081698 */
17091699extern Datum
1710- array_to_json_pretty (PG_FUNCTION_ARGS )
1700+ array_to_json (PG_FUNCTION_ARGS )
17111701{
17121702 Datum array = PG_GETARG_DATUM (0 );
17131703 bool use_line_feeds = PG_GETARG_BOOL (1 );
@@ -1721,34 +1711,19 @@ array_to_json_pretty(PG_FUNCTION_ARGS)
17211711}
17221712
17231713/*
1724- * SQL function row_to_json(row )
1714+ * SQL function row_to_json(rowval record, pretty bool, ignore_nulls bool )
17251715 */
17261716extern Datum
17271717row_to_json (PG_FUNCTION_ARGS )
1728- {
1729- Datum array = PG_GETARG_DATUM (0 );
1730- StringInfo result ;
1731-
1732- result = makeStringInfo ();
1733-
1734- composite_to_json (array , result , false);
1735-
1736- PG_RETURN_TEXT_P (cstring_to_text_with_len (result -> data , result -> len ));
1737- }
1738-
1739- /*
1740- * SQL function row_to_json(row, prettybool)
1741- */
1742- extern Datum
1743- row_to_json_pretty (PG_FUNCTION_ARGS )
17441718{
17451719 Datum array = PG_GETARG_DATUM (0 );
17461720 bool use_line_feeds = PG_GETARG_BOOL (1 );
1721+ bool ignore_nulls = PG_GETARG_BOOL (2 );
17471722 StringInfo result ;
17481723
17491724 result = makeStringInfo ();
17501725
1751- composite_to_json (array , result , use_line_feeds );
1726+ composite_to_json (array , result , use_line_feeds , ignore_nulls );
17521727
17531728 PG_RETURN_TEXT_P (cstring_to_text_with_len (result -> data , result -> len ));
17541729}
0 commit comments