Skip to content

Commit 73233b3

Browse files
author
Nikita Glukhov
committed
Extract pushScalarJsonbValue()
1 parent 43399cf commit 73233b3

File tree

4 files changed

+46
-68
lines changed

4 files changed

+46
-68
lines changed

src/backend/utils/adt/jsonb.c

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -333,20 +333,6 @@ jsonb_put_escaped_value(StringInfo out, JsonbValue *scalarVal)
333333
}
334334
}
335335

336-
static JsonbValue *
337-
pushSingleScalarJsonbValue(JsonbParseState **pstate, JsonbValue *jbval)
338-
{
339-
/* single root scalar */
340-
JsonbValue va;
341-
342-
va.type = jbvArray;
343-
va.val.array.rawScalar = true;
344-
va.val.array.nElems = 1;
345-
346-
pushJsonbValue(pstate, WJB_BEGIN_ARRAY, &va);
347-
pushJsonbValue(pstate, WJB_ELEM, jbval);
348-
return pushJsonbValue(pstate, WJB_END_ARRAY, NULL);
349-
}
350336

351337
/*
352338
* For jsonb we always want the de-escaped value - that's what's in token
@@ -396,26 +382,7 @@ jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype)
396382
break;
397383
}
398384

399-
if (_state->parseState == NULL)
400-
{
401-
_state->res = pushSingleScalarJsonbValue(&_state->parseState, &v);
402-
}
403-
else
404-
{
405-
JsonbValue *o = &_state->parseState->contVal;
406-
407-
switch (o->type)
408-
{
409-
case jbvArray:
410-
_state->res = pushJsonbValue(&_state->parseState, WJB_ELEM, &v);
411-
break;
412-
case jbvObject:
413-
_state->res = pushJsonbValue(&_state->parseState, WJB_VALUE, &v);
414-
break;
415-
default:
416-
elog(ERROR, "unexpected parent of nested structure");
417-
}
418-
}
385+
_state->res = pushScalarJsonbValue(&_state->parseState, &v, false);
419386
}
420387

421388
/*
@@ -929,28 +896,8 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
929896
/* work has been done recursively */
930897
return;
931898
}
932-
else if (result->parseState == NULL)
933-
{
934-
result->res = pushSingleScalarJsonbValue(&result->parseState, &jb);
935-
}
936-
else
937-
{
938-
JsonbValue *o = &result->parseState->contVal;
939899

940-
switch (o->type)
941-
{
942-
case jbvArray:
943-
result->res = pushJsonbValue(&result->parseState, WJB_ELEM, &jb);
944-
break;
945-
case jbvObject:
946-
result->res = pushJsonbValue(&result->parseState,
947-
key_scalar ? WJB_KEY : WJB_VALUE,
948-
&jb);
949-
break;
950-
default:
951-
elog(ERROR, "unexpected parent of nested structure");
952-
}
953-
}
900+
result->res = pushScalarJsonbValue(&result->parseState, &jb, key_scalar);
954901
}
955902

956903
/*

src/backend/utils/adt/jsonb_util.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,46 @@ pushJsonbValueScalar(JsonbParseState **pstate, JsonbIteratorToken seq,
654654
return result;
655655
}
656656

657+
static JsonbValue *
658+
pushSingleScalarJsonbValue(JsonbParseState **pstate, JsonbValue *jbval)
659+
{
660+
/* single root scalar */
661+
JsonbValue va;
662+
663+
va.type = jbvArray;
664+
va.val.array.rawScalar = true;
665+
va.val.array.nElems = 1;
666+
667+
pushJsonbValue(pstate, WJB_BEGIN_ARRAY, &va);
668+
pushJsonbValue(pstate, WJB_ELEM, jbval);
669+
return pushJsonbValue(pstate, WJB_END_ARRAY, NULL);
670+
}
671+
672+
static JsonbValue *
673+
pushNestedScalarJsonbValue(JsonbParseState **pstate, JsonbValue *jbval,
674+
bool isKey)
675+
{
676+
switch ((*pstate)->contVal.type)
677+
{
678+
case jbvArray:
679+
return pushJsonbValue(pstate, WJB_ELEM, jbval);
680+
case jbvObject:
681+
return pushJsonbValue(pstate, isKey ? WJB_KEY : WJB_VALUE, jbval);
682+
default:
683+
elog(ERROR, "unexpected parent of nested structure");
684+
return NULL;
685+
}
686+
}
687+
688+
JsonbValue *
689+
pushScalarJsonbValue(JsonbParseState **pstate, JsonbValue *jbval, bool isKey)
690+
{
691+
return *pstate == NULL
692+
? pushSingleScalarJsonbValue(pstate, jbval)
693+
: pushNestedScalarJsonbValue(pstate, jbval, isKey);
694+
695+
}
696+
657697
/*
658698
* pushJsonbValue() worker: Iteration-like forming of Jsonb
659699
*/

src/backend/utils/adt/jsonfuncs.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3957,30 +3957,19 @@ static void
39573957
addJsonbToParseState(JsonbParseState **jbps, Jsonb *jb)
39583958
{
39593959
JsonbIterator *it;
3960-
JsonbValue *o = &(*jbps)->contVal;
39613960
JsonbValue v;
39623961
JsonbIteratorToken type;
39633962

39643963
it = JsonbIteratorInit(&jb->root);
39653964

3966-
Assert(o->type == jbvArray || o->type == jbvObject);
3965+
Assert(*jbps);
39673966

39683967
if (JB_ROOT_IS_SCALAR(jb))
39693968
{
39703969
(void) JsonbIteratorNext(&it, &v, false); /* skip array header */
39713970
(void) JsonbIteratorNext(&it, &v, false); /* fetch scalar value */
39723971

3973-
switch (o->type)
3974-
{
3975-
case jbvArray:
3976-
(void) pushJsonbValue(jbps, WJB_ELEM, &v);
3977-
break;
3978-
case jbvObject:
3979-
(void) pushJsonbValue(jbps, WJB_VALUE, &v);
3980-
break;
3981-
default:
3982-
elog(ERROR, "unexpected parent of nested structure");
3983-
}
3972+
(void) pushScalarJsonbValue(jbps, &v, false);
39843973
}
39853974
else
39863975
{

src/include/utils/jsonb.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ extern JsonbValue *getIthJsonbValueFromContainer(JsonbContainer *sheader,
361361
uint32 i);
362362
extern JsonbValue *pushJsonbValue(JsonbParseState **pstate,
363363
JsonbIteratorToken seq, JsonbValue *jbVal);
364+
extern JsonbValue *pushScalarJsonbValue(JsonbParseState **pstate,
365+
JsonbValue *jbval, bool isKey);
364366
extern JsonbParseState *JsonbParseStateClone(JsonbParseState *state);
365367
extern JsonbIterator *JsonbIteratorInit(JsonbContainer *container);
366368
extern JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val,

0 commit comments

Comments
 (0)