Skip to content

Commit c7372ea

Browse files
author
Nikita Glukhov
committed
Preserve json formatting in json_strip_nulls()
1 parent 2798d1f commit c7372ea

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/backend/utils/adt/json.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
#define jsonb_populate_recordset json_populate_recordset
6060
#define jsonb_pretty json_pretty
6161
#define jsonb_set json_set
62-
#define jsonb_strip_nulls json_strip_nulls
6362
#define jsonb_to_record json_to_record
6463
#define jsonb_to_recordset json_to_recordset
6564

src/backend/utils/adt/jsonfuncs.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,8 @@ static void transform_string_values_array_element_start(void *state, bool isnull
505505
static void transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype);
506506
#endif
507507

508+
static Datum jsonb_strip_nulls_internal(Jsonb *jb);
509+
508510
/*
509511
* SQL function json_object_keys
510512
*
@@ -3854,7 +3856,9 @@ populate_recordset_object_field_end(void *state, char *fname, bool isnull)
38543856
hashentry->val = _state->saved_scalar;
38553857
}
38563858
}
3859+
#endif
38573860

3861+
#ifdef JSON_C
38583862
/*
38593863
* Semantic actions for json_strip_nulls.
38603864
*
@@ -3957,12 +3961,24 @@ sn_scalar(void *state, char *token, JsonTokenType tokentype)
39573961
Datum
39583962
json_strip_nulls(PG_FUNCTION_ARGS)
39593963
{
3960-
text *json = PG_GETARG_TEXT_PP(0);
3964+
#ifdef JSON_GENERIC
3965+
Json *json = PG_GETARG_JSONB(0);
3966+
#else
3967+
text *json = PG_GETARG_TEXT_P(0);
3968+
#endif
39613969
StripnullState *state;
39623970
JsonLexContext *lex;
39633971
JsonSemAction *sem;
39643972

3973+
#ifdef JSON_GENERIC
3974+
if (json->root.ops != &jsontContainerOps)
3975+
return jsonb_strip_nulls_internal(json);
3976+
3977+
lex = makeJsonLexContextCstringLen(json->root.data, json->root.len, true);
3978+
#else
39653979
lex = makeJsonLexContext(json, true);
3980+
#endif
3981+
39663982
state = palloc0(sizeof(StripnullState));
39673983
sem = palloc0(sizeof(JsonSemAction));
39683984

@@ -3985,15 +4001,21 @@ json_strip_nulls(PG_FUNCTION_ARGS)
39854001
state->strval->len));
39864002

39874003
}
3988-
#endif
4004+
#else
39894005

39904006
/*
39914007
* SQL function jsonb_strip_nulls(jsonb) -> jsonb
39924008
*/
39934009
Datum
39944010
jsonb_strip_nulls(PG_FUNCTION_ARGS)
39954011
{
3996-
Jsonb *jb = PG_GETARG_JSONB(0);
4012+
return jsonb_strip_nulls_internal(PG_GETARG_JSONB(0));
4013+
}
4014+
#endif
4015+
4016+
static Datum
4017+
jsonb_strip_nulls_internal(Jsonb *jb)
4018+
{
39974019
JsonbIterator *it;
39984020
JsonbParseState *parseState = NULL;
39994021
JsonbValue *res = NULL;

0 commit comments

Comments
 (0)