Correctly handle NULLs in JSON output.

Error reported by David Wheeler.
This commit is contained in:
Andrew Dunstan 2012-02-23 23:44:16 -05:00
parent b2ce60703a
commit 0c9e5d5e0d
1 changed files with 14 additions and 12 deletions

View File

@ -79,9 +79,10 @@ static void report_parse_error(JsonParseStack *stack, JsonLexContext *lex);
static void report_invalid_token(JsonLexContext *lex); static void report_invalid_token(JsonLexContext *lex);
static char *extract_mb_char(char *s); static char *extract_mb_char(char *s);
static void composite_to_json(Datum composite, StringInfo result, bool use_line_feeds); static void composite_to_json(Datum composite, StringInfo result, bool use_line_feeds);
static void array_dim_to_json(StringInfo result, int dim, int ndims,int * dims, static void array_dim_to_json(StringInfo result, int dim, int ndims, int *dims,
Datum *vals, int * valcount, TYPCATEGORY tcategory, Datum *vals, bool *nulls, int *valcount,
Oid typoutputfunc, bool use_line_feeds); TYPCATEGORY tcategory, Oid typoutputfunc,
bool use_line_feeds);
static void array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds); static void array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds);
/* fake type category for JSON so we can distinguish it in datum_to_json */ /* fake type category for JSON so we can distinguish it in datum_to_json */
@ -682,13 +683,13 @@ extract_mb_char(char *s)
* composite_to_json or array_to_json_internal as appropriate. * composite_to_json or array_to_json_internal as appropriate.
*/ */
static inline void static inline void
datum_to_json(Datum val, StringInfo result, TYPCATEGORY tcategory, datum_to_json(Datum val, bool is_null, StringInfo result, TYPCATEGORY tcategory,
Oid typoutputfunc) Oid typoutputfunc)
{ {
char *outputstr; char *outputstr;
if (val == (Datum) NULL) if (is_null)
{ {
appendStringInfoString(result,"null"); appendStringInfoString(result,"null");
return; return;
@ -742,8 +743,8 @@ datum_to_json(Datum val, StringInfo result, TYPCATEGORY tcategory,
*/ */
static void static void
array_dim_to_json(StringInfo result, int dim, int ndims,int * dims, Datum *vals, array_dim_to_json(StringInfo result, int dim, int ndims,int * dims, Datum *vals,
int * valcount, TYPCATEGORY tcategory, Oid typoutputfunc, bool *nulls, int * valcount, TYPCATEGORY tcategory,
bool use_line_feeds) Oid typoutputfunc, bool use_line_feeds)
{ {
int i; int i;
@ -762,7 +763,8 @@ array_dim_to_json(StringInfo result, int dim, int ndims,int * dims, Datum *vals,
if (dim + 1 == ndims) if (dim + 1 == ndims)
{ {
datum_to_json(vals[*valcount],result,tcategory,typoutputfunc); datum_to_json(vals[*valcount], nulls[*valcount], result, tcategory,
typoutputfunc);
(*valcount)++; (*valcount)++;
} }
else else
@ -771,8 +773,8 @@ array_dim_to_json(StringInfo result, int dim, int ndims,int * dims, Datum *vals,
* Do we want line feeds on inner dimensions of arrays? * Do we want line feeds on inner dimensions of arrays?
* For now we'll say no. * For now we'll say no.
*/ */
array_dim_to_json(result, dim+1, ndims, dims, vals, valcount, array_dim_to_json(result, dim+1, ndims, dims, vals, nulls,
tcategory,typoutputfunc,false); valcount, tcategory, typoutputfunc, false);
} }
} }
@ -827,7 +829,7 @@ array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)
else else
tcategory = TypeCategory(element_type); tcategory = TypeCategory(element_type);
array_dim_to_json(result, 0, ndim, dim, elements, &count, tcategory, array_dim_to_json(result, 0, ndim, dim, elements, nulls, &count, tcategory,
typoutputfunc, use_line_feeds); typoutputfunc, use_line_feeds);
pfree(elements); pfree(elements);
@ -908,7 +910,7 @@ composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
else else
val = origval; val = origval;
datum_to_json(val, result, tcategory, typoutput); datum_to_json(val, isnull, result, tcategory, typoutput);
/* Clean up detoasted copy, if any */ /* Clean up detoasted copy, if any */
if (val != origval) if (val != origval)