Skip to content

Commit fba12c8

Browse files
committed
Simplify addJsonbToParseState()
This function no longer needs to walk non-scalar structures passed to it, following commit 54547bd.
1 parent 54547bd commit fba12c8

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

src/backend/utils/adt/jsonfuncs.c

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3225,8 +3225,9 @@ jsonb_strip_nulls(PG_FUNCTION_ARGS)
32253225
* If the parse state container is an object, the jsonb is pushed as
32263226
* a value, not a key.
32273227
*
3228-
* This needs to be done using an iterator because pushJsonbValue doesn't
3229-
* like getting jbvBinary values, so we can't just push jb as a whole.
3228+
* If the new value is a root scalar, extract the value using an iterator, and
3229+
* just add that. Otherwise, add the value as the type appropriate for
3230+
* the container.
32303231
*/
32313232
static void
32323233
addJsonbToParseState(JsonbParseState **jbps, Jsonb *jb)
@@ -3236,36 +3237,26 @@ addJsonbToParseState(JsonbParseState **jbps, Jsonb *jb)
32363237
int type;
32373238
JsonbValue v;
32383239

3239-
it = JsonbIteratorInit(&jb->root);
3240-
32413240
Assert(o->type == jbvArray || o->type == jbvObject);
32423241

32433242
if (JB_ROOT_IS_SCALAR(jb))
32443243
{
3244+
it = JsonbIteratorInit(&jb->root);
3245+
32453246
(void) JsonbIteratorNext(&it, &v, false); /* skip array header */
32463247
(void) JsonbIteratorNext(&it, &v, false); /* fetch scalar value */
32473248

3248-
switch (o->type)
3249-
{
3250-
case jbvArray:
3251-
(void) pushJsonbValue(jbps, WJB_ELEM, &v);
3252-
break;
3253-
case jbvObject:
3254-
(void) pushJsonbValue(jbps, WJB_VALUE, &v);
3255-
break;
3256-
default:
3257-
elog(ERROR, "unexpected parent of nested structure");
3258-
}
3249+
if (o->type == jbvArray)
3250+
(void) pushJsonbValue(jbps, WJB_ELEM, &v);
3251+
else
3252+
(void) pushJsonbValue(jbps, WJB_VALUE, &v);
32593253
}
32603254
else
32613255
{
3262-
while ((type = JsonbIteratorNext(&it, &v, false)) != WJB_DONE)
3263-
{
3264-
if (type == WJB_KEY || type == WJB_VALUE || type == WJB_ELEM)
3265-
(void) pushJsonbValue(jbps, type, &v);
3266-
else
3267-
(void) pushJsonbValue(jbps, type, NULL);
3268-
}
3256+
if (o->type == jbvArray)
3257+
(void) pushJsonbValue(jbps, WJB_ELEM, &jb->root);
3258+
else
3259+
(void) pushJsonbValue(jbps, WJB_VALUE, &jb->root);
32693260
}
32703261

32713262
}

0 commit comments

Comments
 (0)