Skip to content

Commit 5f4e1b2

Browse files
author
Nikita Glukhov
committed
Minor refactoring of datum_to_jsonb()
1 parent a87ec40 commit 5f4e1b2

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

src/backend/utils/adt/jsonb.c

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,6 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
700700
char *outputstr;
701701
bool numeric_error;
702702
JsonbValue jb;
703-
bool scalar_jsonb = false;
704703

705704
check_stack_depth();
706705

@@ -730,10 +729,10 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
730729
{
731730
case JSONBTYPE_ARRAY:
732731
array_to_jsonb_internal(val, result);
733-
break;
732+
return;
734733
case JSONBTYPE_COMPOSITE:
735734
composite_to_jsonb(val, result);
736-
break;
735+
return;
737736
case JSONBTYPE_BOOL:
738737
if (key_scalar)
739738
{
@@ -873,9 +872,8 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
873872
sem.object_field_start = jsonb_in_object_field_start;
874873

875874
pg_parse_json(lex, &sem);
876-
877875
}
878-
break;
876+
return;
879877
#ifdef JSON_GENERIC
880878
case JSONBTYPE_JSON:
881879
#endif
@@ -897,35 +895,33 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
897895
(void) JsonbIteratorNext(&it, &jb, true);
898896
Assert(jb.type == jbvArray);
899897
(void) JsonbIteratorNext(&it, &jb, true);
900-
scalar_jsonb = true;
898+
break;
901899
}
902-
else
903-
{
904-
JsonbIteratorToken type;
905900

906-
907-
if (result->parseState)
908-
{
901+
if (result->parseState)
902+
result->res =
909903
pushScalarJsonbValue(&result->parseState,
910904
JsonToJsonValue(jsonb, &jb),
911905
false, false);
912-
return;
913-
}
906+
else
907+
{
908+
JsonbIteratorToken type;
914909

915910
while ((type = JsonbIteratorNext(&it, &jb, false))
916911
!= WJB_DONE)
917912
{
918-
if (type == WJB_END_ARRAY || type == WJB_END_OBJECT ||
919-
type == WJB_BEGIN_ARRAY || type == WJB_BEGIN_OBJECT)
920-
result->res = pushJsonbValue(&result->parseState,
921-
type, NULL);
922-
else
923-
result->res = pushJsonbValue(&result->parseState,
924-
type, &jb);
913+
JsonbValue *jv =
914+
type == WJB_END_ARRAY ||
915+
type == WJB_END_OBJECT ||
916+
type == WJB_BEGIN_ARRAY ||
917+
type == WJB_BEGIN_OBJECT ? NULL : &jb;
918+
919+
result->res = pushJsonbValue(&result->parseState,
920+
type, jv);
925921
}
926922
}
927923
}
928-
break;
924+
return;
929925
default:
930926
outputstr = OidOutputFunctionCall(outfuncoid, val);
931927
jb.type = jbvString;
@@ -936,13 +932,6 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
936932
}
937933

938934
/* Now insert jb into result, unless we did it recursively */
939-
if (!is_null && !scalar_jsonb &&
940-
tcategory >= JSONBTYPE_JSON && tcategory <= JSONBTYPE_JSONCAST)
941-
{
942-
/* work has been done recursively */
943-
return;
944-
}
945-
946935
result->res = pushScalarJsonbValue(&result->parseState, &jb, key_scalar,
947936
true);
948937
}

0 commit comments

Comments
 (0)