@@ -79,7 +79,8 @@ static void report_invalid_token(JsonLexContext *lex);
79
79
static int report_json_context (JsonLexContext * lex );
80
80
static char * extract_mb_char (char * s );
81
81
static void composite_to_json (Datum composite , StringInfo result ,
82
- bool use_line_feeds );
82
+ bool use_line_feeds ,
83
+ bool ignore_nulls );
83
84
static void array_dim_to_json (StringInfo result , int dim , int ndims , int * dims ,
84
85
Datum * vals , bool * nulls , int * valcount ,
85
86
JsonTypeCategory tcategory , Oid outfuncoid ,
@@ -1362,7 +1363,7 @@ datum_to_json(Datum val, bool is_null, StringInfo result,
1362
1363
array_to_json_internal (val , result , false);
1363
1364
break ;
1364
1365
case JSONTYPE_COMPOSITE :
1365
- composite_to_json (val , result , false);
1366
+ composite_to_json (val , result , false, false );
1366
1367
break ;
1367
1368
case JSONTYPE_BOOL :
1368
1369
outputstr = DatumGetBool (val ) ? "true" : "false" ;
@@ -1591,7 +1592,8 @@ array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
1591
1592
* Turn a composite / record into JSON.
1592
1593
*/
1593
1594
static 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 )
1595
1597
{
1596
1598
HeapTupleHeader td ;
1597
1599
Oid tupType ;
@@ -1630,6 +1632,12 @@ composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
1630
1632
if (tupdesc -> attrs [i ]-> attisdropped )
1631
1633
continue ;
1632
1634
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
+
1633
1641
if (needsep )
1634
1642
appendStringInfoString (result , sep );
1635
1643
needsep = true;
@@ -1638,8 +1646,6 @@ composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
1638
1646
escape_json (result , attname );
1639
1647
appendStringInfoChar (result , ':' );
1640
1648
1641
- val = heap_getattr (tuple , i + 1 , tupdesc , & isnull );
1642
-
1643
1649
if (isnull )
1644
1650
{
1645
1651
tcategory = JSONTYPE_NULL ;
@@ -1687,27 +1693,11 @@ add_json(Datum val, bool is_null, StringInfo result,
1687
1693
datum_to_json (val , is_null , result , tcategory , outfuncoid , key_scalar );
1688
1694
}
1689
1695
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
-
1706
1696
/*
1707
1697
* SQL function array_to_json(row, prettybool)
1708
1698
*/
1709
1699
extern Datum
1710
- array_to_json_pretty (PG_FUNCTION_ARGS )
1700
+ array_to_json (PG_FUNCTION_ARGS )
1711
1701
{
1712
1702
Datum array = PG_GETARG_DATUM (0 );
1713
1703
bool use_line_feeds = PG_GETARG_BOOL (1 );
@@ -1721,34 +1711,19 @@ array_to_json_pretty(PG_FUNCTION_ARGS)
1721
1711
}
1722
1712
1723
1713
/*
1724
- * SQL function row_to_json(row )
1714
+ * SQL function row_to_json(rowval record, pretty bool, ignore_nulls bool )
1725
1715
*/
1726
1716
extern Datum
1727
1717
row_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 )
1744
1718
{
1745
1719
Datum array = PG_GETARG_DATUM (0 );
1746
1720
bool use_line_feeds = PG_GETARG_BOOL (1 );
1721
+ bool ignore_nulls = PG_GETARG_BOOL (2 );
1747
1722
StringInfo result ;
1748
1723
1749
1724
result = makeStringInfo ();
1750
1725
1751
- composite_to_json (array , result , use_line_feeds );
1726
+ composite_to_json (array , result , use_line_feeds , ignore_nulls );
1752
1727
1753
1728
PG_RETURN_TEXT_P (cstring_to_text_with_len (result -> data , result -> len ));
1754
1729
}
0 commit comments