diff options
author | Stephen Frost <sfrost@snowman.net> | 2014-09-11 21:23:51 -0400 |
---|---|---|
committer | Stephen Frost <sfrost@snowman.net> | 2014-09-11 21:23:51 -0400 |
commit | 95d737ff45a38809130a2c633d9e6bc26c825036 (patch) | |
tree | d3d2c052b1def80edd36fa9751a793eca4a3e0e0 /src/backend/utils/adt/json.c | |
parent | c3c75fcd7a03067d87b830e98e3698b683446762 (diff) | |
download | postgresql-95d737ff45a38809130a2c633d9e6bc26c825036.tar.gz postgresql-95d737ff45a38809130a2c633d9e6bc26c825036.zip |
Add 'ignore_nulls' option to row_to_json
Provide an option to skip NULL values in a row when generating a JSON
object from that row with row_to_json. This can reduce the size of the
JSON object in cases where columns are NULL without really reducing the
information in the JSON object.
This also makes row_to_json into a single function with default values,
rather than having multiple functions. In passing, change array_to_json
to also be a single function with default values (we don't add an
'ignore_nulls' option yet- it's not clear that there is a sensible
use-case there, and it hasn't been asked for in any case).
Pavel Stehule
Diffstat (limited to 'src/backend/utils/adt/json.c')
-rw-r--r-- | src/backend/utils/adt/json.c | 55 |
1 files changed, 15 insertions, 40 deletions
diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c index 494a0285267..19d74014ef5 100644 --- a/src/backend/utils/adt/json.c +++ b/src/backend/utils/adt/json.c @@ -79,7 +79,8 @@ static void report_invalid_token(JsonLexContext *lex); static int report_json_context(JsonLexContext *lex); static char *extract_mb_char(char *s); static void composite_to_json(Datum composite, StringInfo result, - bool use_line_feeds); + bool use_line_feeds, + bool ignore_nulls); static void array_dim_to_json(StringInfo result, int dim, int ndims, int *dims, Datum *vals, bool *nulls, int *valcount, JsonTypeCategory tcategory, Oid outfuncoid, @@ -1362,7 +1363,7 @@ datum_to_json(Datum val, bool is_null, StringInfo result, array_to_json_internal(val, result, false); break; case JSONTYPE_COMPOSITE: - composite_to_json(val, result, false); + composite_to_json(val, result, false, false); break; case JSONTYPE_BOOL: outputstr = DatumGetBool(val) ? "true" : "false"; @@ -1591,7 +1592,8 @@ array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds) * Turn a composite / record into JSON. */ static void -composite_to_json(Datum composite, StringInfo result, bool use_line_feeds) +composite_to_json(Datum composite, StringInfo result, bool use_line_feeds, + bool ignore_nulls) { HeapTupleHeader td; Oid tupType; @@ -1630,6 +1632,12 @@ composite_to_json(Datum composite, StringInfo result, bool use_line_feeds) if (tupdesc->attrs[i]->attisdropped) continue; + val = heap_getattr(tuple, i + 1, tupdesc, &isnull); + + /* Don't serialize NULL field when we don't want it */ + if (isnull && ignore_nulls) + continue; + if (needsep) appendStringInfoString(result, sep); needsep = true; @@ -1638,8 +1646,6 @@ composite_to_json(Datum composite, StringInfo result, bool use_line_feeds) escape_json(result, attname); appendStringInfoChar(result, ':'); - val = heap_getattr(tuple, i + 1, tupdesc, &isnull); - if (isnull) { tcategory = JSONTYPE_NULL; @@ -1688,26 +1694,10 @@ add_json(Datum val, bool is_null, StringInfo result, } /* - * SQL function array_to_json(row) - */ -extern Datum -array_to_json(PG_FUNCTION_ARGS) -{ - Datum array = PG_GETARG_DATUM(0); - StringInfo result; - - result = makeStringInfo(); - - array_to_json_internal(array, result, false); - - PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len)); -} - -/* * SQL function array_to_json(row, prettybool) */ extern Datum -array_to_json_pretty(PG_FUNCTION_ARGS) +array_to_json(PG_FUNCTION_ARGS) { Datum array = PG_GETARG_DATUM(0); bool use_line_feeds = PG_GETARG_BOOL(1); @@ -1721,34 +1711,19 @@ array_to_json_pretty(PG_FUNCTION_ARGS) } /* - * SQL function row_to_json(row) + * SQL function row_to_json(rowval record, pretty bool, ignore_nulls bool) */ extern Datum row_to_json(PG_FUNCTION_ARGS) { Datum array = PG_GETARG_DATUM(0); - StringInfo result; - - result = makeStringInfo(); - - composite_to_json(array, result, false); - - PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len)); -} - -/* - * SQL function row_to_json(row, prettybool) - */ -extern Datum -row_to_json_pretty(PG_FUNCTION_ARGS) -{ - Datum array = PG_GETARG_DATUM(0); bool use_line_feeds = PG_GETARG_BOOL(1); + bool ignore_nulls = PG_GETARG_BOOL(2); StringInfo result; result = makeStringInfo(); - composite_to_json(array, result, use_line_feeds); + composite_to_json(array, result, use_line_feeds, ignore_nulls); PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len)); } |