Skip to content

Commit d1ddafc

Browse files
author
Nikita Glukhov
committed
Preserve json formatting in json_strip_nulls()
1 parent 161c89b commit d1ddafc

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
@@ -62,7 +62,6 @@
6262
#define jsonb_pretty json_pretty
6363
#define jsonb_set json_set
6464
#define jsonb_set_lax json_set_lax
65-
#define jsonb_strip_nulls json_strip_nulls
6665
#define jsonb_to_record json_to_record
6766
#define jsonb_to_recordset json_to_recordset
6867

src/backend/utils/adt/jsonfuncs.c

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

527+
static Datum jsonb_strip_nulls_internal(Jsonb *jb);
528+
527529
#ifndef JSON_C
528530
/*
529531
* pg_parse_json_or_ereport
@@ -4077,7 +4079,9 @@ populate_recordset_object_field_end(void *state, char *fname, bool isnull)
40774079
hashentry->val = _state->saved_scalar;
40784080
}
40794081
}
4082+
#endif
40804083

4084+
#ifdef JSON_C
40814085
/*
40824086
* Semantic actions for json_strip_nulls.
40834087
*
@@ -4180,12 +4184,24 @@ sn_scalar(void *state, char *token, JsonTokenType tokentype)
41804184
Datum
41814185
json_strip_nulls(PG_FUNCTION_ARGS)
41824186
{
4183-
text *json = PG_GETARG_TEXT_PP(0);
4187+
#ifdef JSON_GENERIC
4188+
Json *json = PG_GETARG_JSONB_P(0);
4189+
#else
4190+
text *json = PG_GETARG_TEXT_P(0);
4191+
#endif
41844192
StripnullState *state;
41854193
JsonLexContext *lex;
41864194
JsonSemAction *sem;
41874195

4196+
#ifdef JSON_GENERIC
4197+
if (json->root.ops != &jsontContainerOps)
4198+
return jsonb_strip_nulls_internal(json);
4199+
4200+
lex = makeJsonLexContextCstringLen(json->root.data, json->root.len, GetDatabaseEncoding(), true);
4201+
#else
41884202
lex = makeJsonLexContext(json, true);
4203+
#endif
4204+
41894205
state = palloc0(sizeof(StripnullState));
41904206
sem = palloc0(sizeof(JsonSemAction));
41914207

@@ -4208,15 +4224,21 @@ json_strip_nulls(PG_FUNCTION_ARGS)
42084224
state->strval->len));
42094225

42104226
}
4211-
#endif
4227+
#else
42124228

42134229
/*
42144230
* SQL function jsonb_strip_nulls(jsonb) -> jsonb
42154231
*/
42164232
Datum
42174233
jsonb_strip_nulls(PG_FUNCTION_ARGS)
42184234
{
4219-
Jsonb *jb = PG_GETARG_JSONB_P(0);
4235+
return jsonb_strip_nulls_internal(PG_GETARG_JSONB_P(0));
4236+
}
4237+
#endif
4238+
4239+
static Datum
4240+
jsonb_strip_nulls_internal(Jsonb *jb)
4241+
{
42204242
JsonbIterator *it;
42214243
JsonbParseState *parseState = NULL;
42224244
JsonbValue *res = NULL;

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