Skip to content

Commit 7632295

Browse files
author
Nikita Glukhov
committed
Extract pushScalarJsonbValue()
1 parent 8b83804 commit 7632295

File tree

3 files changed

+44
-55
lines changed

3 files changed

+44
-55
lines changed

src/backend/utils/adt/jsonb.c

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -363,20 +363,6 @@ jsonb_put_escaped_value(StringInfo out, JsonbValue *scalarVal)
363363
}
364364
}
365365

366-
static JsonbValue *
367-
pushSingleScalarJsonbValue(JsonbParseState **pstate, JsonbValue *jbval)
368-
{
369-
/* single root scalar */
370-
JsonbValue va;
371-
372-
va.type = jbvArray;
373-
va.val.array.rawScalar = true;
374-
va.val.array.nElems = 1;
375-
376-
pushJsonbValue(pstate, WJB_BEGIN_ARRAY, &va);
377-
pushJsonbValue(pstate, WJB_ELEM, jbval);
378-
return pushJsonbValue(pstate, WJB_END_ARRAY, NULL);
379-
}
380366

381367
/*
382368
* For jsonb we always want the de-escaped value - that's what's in token
@@ -428,26 +414,7 @@ jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype)
428414
break;
429415
}
430416

431-
if (_state->parseState == NULL)
432-
{
433-
_state->res = pushSingleScalarJsonbValue(&_state->parseState, &v);
434-
}
435-
else
436-
{
437-
JsonbValue *o = &_state->parseState->contVal;
438-
439-
switch (o->type)
440-
{
441-
case jbvArray:
442-
_state->res = pushJsonbValue(&_state->parseState, WJB_ELEM, &v);
443-
break;
444-
case jbvObject:
445-
_state->res = pushJsonbValue(&_state->parseState, WJB_VALUE, &v);
446-
break;
447-
default:
448-
elog(ERROR, "unexpected parent of nested structure");
449-
}
450-
}
417+
_state->res = pushScalarJsonbValue(&_state->parseState, &v, false);
451418
}
452419

453420
/*
@@ -917,28 +884,8 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
917884
/* work has been done recursively */
918885
return;
919886
}
920-
else if (result->parseState == NULL)
921-
{
922-
result->res = pushSingleScalarJsonbValue(&result->parseState, &jb);
923-
}
924-
else
925-
{
926-
JsonbValue *o = &result->parseState->contVal;
927887

928-
switch (o->type)
929-
{
930-
case jbvArray:
931-
result->res = pushJsonbValue(&result->parseState, WJB_ELEM, &jb);
932-
break;
933-
case jbvObject:
934-
result->res = pushJsonbValue(&result->parseState,
935-
key_scalar ? WJB_KEY : WJB_VALUE,
936-
&jb);
937-
break;
938-
default:
939-
elog(ERROR, "unexpected parent of nested structure");
940-
}
941-
}
888+
result->res = pushScalarJsonbValue(&result->parseState, &jb, key_scalar);
942889
}
943890

944891
/*

src/backend/utils/adt/jsonb_util.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,46 @@ pushJsonbValueScalar(JsonbParseState **pstate, JsonbIteratorToken seq,
748748
return result;
749749
}
750750

751+
static JsonbValue *
752+
pushSingleScalarJsonbValue(JsonbParseState **pstate, JsonbValue *jbval)
753+
{
754+
/* single root scalar */
755+
JsonbValue va;
756+
757+
va.type = jbvArray;
758+
va.val.array.rawScalar = true;
759+
va.val.array.nElems = 1;
760+
761+
pushJsonbValue(pstate, WJB_BEGIN_ARRAY, &va);
762+
pushJsonbValue(pstate, WJB_ELEM, jbval);
763+
return pushJsonbValue(pstate, WJB_END_ARRAY, NULL);
764+
}
765+
766+
static JsonbValue *
767+
pushNestedScalarJsonbValue(JsonbParseState **pstate, JsonbValue *jbval,
768+
bool isKey)
769+
{
770+
switch ((*pstate)->contVal.type)
771+
{
772+
case jbvArray:
773+
return pushJsonbValue(pstate, WJB_ELEM, jbval);
774+
case jbvObject:
775+
return pushJsonbValue(pstate, isKey ? WJB_KEY : WJB_VALUE, jbval);
776+
default:
777+
elog(ERROR, "unexpected parent of nested structure");
778+
return NULL;
779+
}
780+
}
781+
782+
JsonbValue *
783+
pushScalarJsonbValue(JsonbParseState **pstate, JsonbValue *jbval, bool isKey)
784+
{
785+
return *pstate == NULL
786+
? pushSingleScalarJsonbValue(pstate, jbval)
787+
: pushNestedScalarJsonbValue(pstate, jbval, isKey);
788+
789+
}
790+
751791
/*
752792
* pushJsonbValue() worker: Iteration-like forming of Jsonb
753793
*/

src/include/utils/jsonb.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,8 @@ extern JsonbValue *getIthJsonbValueFromContainer(JsonbContainer *sheader,
387387
uint32 i);
388388
extern JsonbValue *pushJsonbValue(JsonbParseState **pstate,
389389
JsonbIteratorToken seq, JsonbValue *jbval);
390+
extern JsonbValue *pushScalarJsonbValue(JsonbParseState **pstate,
391+
JsonbValue *jbval, bool isKey);
390392
extern JsonbParseState *JsonbParseStateClone(JsonbParseState *state);
391393
extern JsonbIterator *JsonbIteratorInit(JsonbContainer *container);
392394
extern JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val,

0 commit comments

Comments
 (0)