Skip to content

Commit 031b22c

Browse files
author
Nikita Glukhov
committed
Preserve json formatting in json_strip_nulls()
1 parent 0a3f747 commit 031b22c

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
#define jsonb_get_element json_get_element

src/backend/utils/adt/jsonfuncs.c

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

532+
static Datum jsonb_strip_nulls_internal(Jsonb *jb);
533+
532534
#ifndef JSON_C
533535
/*
534536
* pg_parse_json_or_ereport
@@ -4217,7 +4219,9 @@ populate_recordset_object_field_end(void *state, char *fname, bool isnull)
42174219
hashentry->val = _state->saved_scalar;
42184220
}
42194221
}
4222+
#endif
42204223

4224+
#ifdef JSON_C
42214225
/*
42224226
* Semantic actions for json_strip_nulls.
42234227
*
@@ -4320,12 +4324,24 @@ sn_scalar(void *state, char *token, JsonTokenType tokentype)
43204324
Datum
43214325
json_strip_nulls(PG_FUNCTION_ARGS)
43224326
{
4323-
text *json = PG_GETARG_TEXT_PP(0);
4327+
#ifdef JSON_GENERIC
4328+
Json *json = PG_GETARG_JSONB_P(0);
4329+
#else
4330+
text *json = PG_GETARG_TEXT_P(0);
4331+
#endif
43244332
StripnullState *state;
43254333
JsonLexContext *lex;
43264334
JsonSemAction *sem;
43274335

4336+
#ifdef JSON_GENERIC
4337+
if (json->root.ops != &jsontContainerOps)
4338+
return jsonb_strip_nulls_internal(json);
4339+
4340+
lex = makeJsonLexContextCstringLen(json->root.data, json->root.len, GetDatabaseEncoding(), true);
4341+
#else
43284342
lex = makeJsonLexContext(json, true);
4343+
#endif
4344+
43294345
state = palloc0(sizeof(StripnullState));
43304346
sem = palloc0(sizeof(JsonSemAction));
43314347

@@ -4348,15 +4364,21 @@ json_strip_nulls(PG_FUNCTION_ARGS)
43484364
state->strval->len));
43494365

43504366
}
4351-
#endif
4367+
#else
43524368

43534369
/*
43544370
* SQL function jsonb_strip_nulls(jsonb) -> jsonb
43554371
*/
43564372
Datum
43574373
jsonb_strip_nulls(PG_FUNCTION_ARGS)
43584374
{
4359-
Jsonb *jb = PG_GETARG_JSONB_P(0);
4375+
return jsonb_strip_nulls_internal(PG_GETARG_JSONB_P(0));
4376+
}
4377+
#endif
4378+
4379+
static Datum
4380+
jsonb_strip_nulls_internal(Jsonb *jb)
4381+
{
43604382
JsonbIterator *it;
43614383
JsonbParseState *parseState = NULL;
43624384
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