Skip to content

Commit 0c9e5d5

Browse files
committed
Correctly handle NULLs in JSON output.
Error reported by David Wheeler.
1 parent b2ce607 commit 0c9e5d5

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/backend/utils/adt/json.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ static void report_parse_error(JsonParseStack *stack, JsonLexContext *lex);
7979
static void report_invalid_token(JsonLexContext *lex);
8080
static char *extract_mb_char(char *s);
8181
static void composite_to_json(Datum composite, StringInfo result, bool use_line_feeds);
82-
static void array_dim_to_json(StringInfo result, int dim, int ndims,int * dims,
83-
Datum *vals, int * valcount, TYPCATEGORY tcategory,
84-
Oid typoutputfunc, bool use_line_feeds);
82+
static void array_dim_to_json(StringInfo result, int dim, int ndims, int *dims,
83+
Datum *vals, bool *nulls, int *valcount,
84+
TYPCATEGORY tcategory, Oid typoutputfunc,
85+
bool use_line_feeds);
8586
static void array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds);
8687

8788
/* fake type category for JSON so we can distinguish it in datum_to_json */
@@ -682,13 +683,13 @@ extract_mb_char(char *s)
682683
* composite_to_json or array_to_json_internal as appropriate.
683684
*/
684685
static inline void
685-
datum_to_json(Datum val, StringInfo result, TYPCATEGORY tcategory,
686+
datum_to_json(Datum val, bool is_null, StringInfo result, TYPCATEGORY tcategory,
686687
Oid typoutputfunc)
687688
{
688689

689690
char *outputstr;
690691

691-
if (val == (Datum) NULL)
692+
if (is_null)
692693
{
693694
appendStringInfoString(result,"null");
694695
return;
@@ -742,8 +743,8 @@ datum_to_json(Datum val, StringInfo result, TYPCATEGORY tcategory,
742743
*/
743744
static void
744745
array_dim_to_json(StringInfo result, int dim, int ndims,int * dims, Datum *vals,
745-
int * valcount, TYPCATEGORY tcategory, Oid typoutputfunc,
746-
bool use_line_feeds)
746+
bool *nulls, int * valcount, TYPCATEGORY tcategory,
747+
Oid typoutputfunc, bool use_line_feeds)
747748
{
748749

749750
int i;
@@ -762,7 +763,8 @@ array_dim_to_json(StringInfo result, int dim, int ndims,int * dims, Datum *vals,
762763

763764
if (dim + 1 == ndims)
764765
{
765-
datum_to_json(vals[*valcount],result,tcategory,typoutputfunc);
766+
datum_to_json(vals[*valcount], nulls[*valcount], result, tcategory,
767+
typoutputfunc);
766768
(*valcount)++;
767769
}
768770
else
@@ -771,8 +773,8 @@ array_dim_to_json(StringInfo result, int dim, int ndims,int * dims, Datum *vals,
771773
* Do we want line feeds on inner dimensions of arrays?
772774
* For now we'll say no.
773775
*/
774-
array_dim_to_json(result, dim+1, ndims, dims, vals, valcount,
775-
tcategory,typoutputfunc,false);
776+
array_dim_to_json(result, dim+1, ndims, dims, vals, nulls,
777+
valcount, tcategory, typoutputfunc, false);
776778
}
777779
}
778780

@@ -827,7 +829,7 @@ array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
827829
else
828830
tcategory = TypeCategory(element_type);
829831

830-
array_dim_to_json(result, 0, ndim, dim, elements, &count, tcategory,
832+
array_dim_to_json(result, 0, ndim, dim, elements, nulls, &count, tcategory,
831833
typoutputfunc, use_line_feeds);
832834

833835
pfree(elements);
@@ -908,7 +910,7 @@ composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
908910
else
909911
val = origval;
910912

911-
datum_to_json(val, result, tcategory, typoutput);
913+
datum_to_json(val, isnull, result, tcategory, typoutput);
912914

913915
/* Clean up detoasted copy, if any */
914916
if (val != origval)

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