Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c605bb4

Browse files
author
Nikita Glukhov
committed
Extract pushScalarJsonbValue()
1 parent 57d6dba commit c605bb4

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
@@ -353,20 +353,6 @@ jsonb_put_escaped_value(StringInfo out, JsonbValue *scalarVal)
353353
}
354354
}
355355

356-
static JsonbValue *
357-
pushSingleScalarJsonbValue(JsonbParseState **pstate, JsonbValue *jbval)
358-
{
359-
/* single root scalar */
360-
JsonbValue va;
361-
362-
va.type = jbvArray;
363-
va.val.array.rawScalar = true;
364-
va.val.array.nElems = 1;
365-
366-
pushJsonbValue(pstate, WJB_BEGIN_ARRAY, &va);
367-
pushJsonbValue(pstate, WJB_ELEM, jbval);
368-
return pushJsonbValue(pstate, WJB_END_ARRAY, NULL);
369-
}
370356

371357
/*
372358
* For jsonb we always want the de-escaped value - that's what's in token
@@ -418,26 +404,7 @@ jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype)
418404
break;
419405
}
420406

421-
if (_state->parseState == NULL)
422-
{
423-
_state->res = pushSingleScalarJsonbValue(&_state->parseState, &v);
424-
}
425-
else
426-
{
427-
JsonbValue *o = &_state->parseState->contVal;
428-
429-
switch (o->type)
430-
{
431-
case jbvArray:
432-
_state->res = pushJsonbValue(&_state->parseState, WJB_ELEM, &v);
433-
break;
434-
case jbvObject:
435-
_state->res = pushJsonbValue(&_state->parseState, WJB_VALUE, &v);
436-
break;
437-
default:
438-
elog(ERROR, "unexpected parent of nested structure");
439-
}
440-
}
407+
_state->res = pushScalarJsonbValue(&_state->parseState, &v, false);
441408
}
442409

443410
/*
@@ -906,28 +873,8 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
906873
/* work has been done recursively */
907874
return;
908875
}
909-
else if (result->parseState == NULL)
910-
{
911-
result->res = pushSingleScalarJsonbValue(&result->parseState, &jb);
912-
}
913-
else
914-
{
915-
JsonbValue *o = &result->parseState->contVal;
916876

917-
switch (o->type)
918-
{
919-
case jbvArray:
920-
result->res = pushJsonbValue(&result->parseState, WJB_ELEM, &jb);
921-
break;
922-
case jbvObject:
923-
result->res = pushJsonbValue(&result->parseState,
924-
key_scalar ? WJB_KEY : WJB_VALUE,
925-
&jb);
926-
break;
927-
default:
928-
elog(ERROR, "unexpected parent of nested structure");
929-
}
930-
}
877+
result->res = pushScalarJsonbValue(&result->parseState, &jb, key_scalar);
931878
}
932879

933880
/*

src/backend/utils/adt/jsonb_util.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,46 @@ pushJsonbValueScalar(JsonbParseState **pstate, JsonbIteratorToken seq,
754754
return result;
755755
}
756756

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

src/include/utils/jsonb.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@ extern JsonbValue *getIthJsonbValueFromContainer(JsonbContainer *sheader,
405405
uint32 i);
406406
extern JsonbValue *pushJsonbValue(JsonbParseState **pstate,
407407
JsonbIteratorToken seq, JsonbValue *jbval);
408+
extern JsonbValue *pushScalarJsonbValue(JsonbParseState **pstate,
409+
JsonbValue *jbval, bool isKey);
408410
extern JsonbParseState *JsonbParseStateClone(JsonbParseState *state);
409411
extern JsonbIterator *JsonbIteratorInit(JsonbContainer *container);
410412
extern JsonbIteratorToken JsonbIteratorNext(JsonbIterator **it, JsonbValue *val,

0 commit comments

Comments
 (0)