Skip to content

Commit 93889e9

Browse files
author
Nikita Glukhov
committed
Extract pushScalarJsonbValue()
1 parent 7a9b2c1 commit 93889e9

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
@@ -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
@@ -696,6 +696,46 @@ pushJsonbValueScalar(JsonbParseState **pstate, JsonbIteratorToken seq,
696696
return result;
697697
}
698698

699+
static JsonbValue *
700+
pushSingleScalarJsonbValue(JsonbParseState **pstate, JsonbValue *jbval)
701+
{
702+
/* single root scalar */
703+
JsonbValue va;
704+
705+
va.type = jbvArray;
706+
va.val.array.rawScalar = true;
707+
va.val.array.nElems = 1;
708+
709+
pushJsonbValue(pstate, WJB_BEGIN_ARRAY, &va);
710+
pushJsonbValue(pstate, WJB_ELEM, jbval);
711+
return pushJsonbValue(pstate, WJB_END_ARRAY, NULL);
712+
}
713+
714+
static JsonbValue *
715+
pushNestedScalarJsonbValue(JsonbParseState **pstate, JsonbValue *jbval,
716+
bool isKey)
717+
{
718+
switch ((*pstate)->contVal.type)
719+
{
720+
case jbvArray:
721+
return pushJsonbValue(pstate, WJB_ELEM, jbval);
722+
case jbvObject:
723+
return pushJsonbValue(pstate, isKey ? WJB_KEY : WJB_VALUE, jbval);
724+
default:
725+
elog(ERROR, "unexpected parent of nested structure");
726+
return NULL;
727+
}
728+
}
729+
730+
JsonbValue *
731+
pushScalarJsonbValue(JsonbParseState **pstate, JsonbValue *jbval, bool isKey)
732+
{
733+
return *pstate == NULL
734+
? pushSingleScalarJsonbValue(pstate, jbval)
735+
: pushNestedScalarJsonbValue(pstate, jbval, isKey);
736+
737+
}
738+
699739
/*
700740
* pushJsonbValue() worker: Iteration-like forming of Jsonb
701741
*/

src/backend/utils/adt/jsonfuncs.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4164,31 +4164,20 @@ static void
41644164
addJsonbToParseState(JsonbParseState **jbps, Jsonb *jb)
41654165
{
41664166
JsonbIterator *it;
4167-
JsonbValue *o = &(*jbps)->contVal;
41684167
JsonbValue v;
41694168
JsonbIteratorToken type;
41704169

41714170
it = JsonbIteratorInit(&jb->root);
41724171

4173-
Assert(o->type == jbvArray || o->type == jbvObject);
4172+
Assert(*jbps);
41744173

41754174
if (JB_ROOT_IS_SCALAR(jb))
41764175
{
41774176
(void) JsonbIteratorNext(&it, &v, false); /* skip array header */
41784177
Assert(v.type == jbvArray);
41794178
(void) JsonbIteratorNext(&it, &v, false); /* fetch scalar value */
41804179

4181-
switch (o->type)
4182-
{
4183-
case jbvArray:
4184-
(void) pushJsonbValue(jbps, WJB_ELEM, &v);
4185-
break;
4186-
case jbvObject:
4187-
(void) pushJsonbValue(jbps, WJB_VALUE, &v);
4188-
break;
4189-
default:
4190-
elog(ERROR, "unexpected parent of nested structure");
4191-
}
4180+
(void) pushScalarJsonbValue(jbps, &v, false);
41924181
}
41934182
else
41944183
{

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)
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