Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite74d1fe

Browse files
author
Nikita Glukhov
committed
Add jsonb_canonical()
1 parentaf50a7c commite74d1fe

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

‎src/backend/utils/adt/jsonb.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ typedef enum/* type categories for datum_to_jsonb */
5252
JSONBTYPE_OTHER/* all else */
5353
}JsonbTypeCategory;
5454

55+
typedefenum
56+
{
57+
JsonFormatCanonical,
58+
JsonFormatNormal,
59+
JsonFormatIndented
60+
}JsonFormat;
61+
5562
typedefstructJsonbAggState
5663
{
5764
JsonbInState*res;
@@ -86,7 +93,8 @@ static void datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
8693
boolkey_scalar);
8794
staticvoidadd_jsonb(Datumval,boolis_null,JsonbInState*result,
8895
Oidval_type,boolkey_scalar);
89-
staticchar*JsonbToCStringWorker(StringInfoout,JsonbContainer*in,intestimated_len,boolindent);
96+
staticchar*JsonbToCStringWorker(StringInfoout,JsonbContainer*in,
97+
intestimated_len,JsonFormatformat);
9098
staticvoidadd_indent(StringInfoout,boolindent,intlevel);
9199

92100
/*
@@ -431,7 +439,7 @@ jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype)
431439
char*
432440
JsonbToCString(StringInfoout,JsonbContainer*in,intestimated_len)
433441
{
434-
returnJsonbToCStringWorker(out,in,estimated_len,false);
442+
returnJsonbToCStringWorker(out,in,estimated_len,JsonFormatNormal);
435443
}
436444

437445
/*
@@ -440,14 +448,21 @@ JsonbToCString(StringInfo out, JsonbContainer *in, int estimated_len)
440448
char*
441449
JsonbToCStringIndent(StringInfoout,JsonbContainer*in,intestimated_len)
442450
{
443-
returnJsonbToCStringWorker(out,in,estimated_len, true);
451+
returnJsonbToCStringWorker(out,in,estimated_len,JsonFormatIndented);
452+
}
453+
454+
char*
455+
JsonbToCStringCanonical(StringInfoout,JsonbContainer*in,intestimated_len)
456+
{
457+
returnJsonbToCStringWorker(out,in,estimated_len,JsonFormatCanonical);
444458
}
445459

446460
/*
447461
* common worker for above two functions
448462
*/
449463
staticchar*
450-
JsonbToCStringWorker(StringInfoout,JsonbContainer*in,intestimated_len,boolindent)
464+
JsonbToCStringWorker(StringInfoout,JsonbContainer*in,intestimated_len,
465+
JsonFormatformat)
451466
{
452467
boolfirst= true;
453468
JsonbIterator*it;
@@ -457,7 +472,8 @@ JsonbToCStringWorker(StringInfo out, JsonbContainer *in, int estimated_len, bool
457472
boolredo_switch= false;
458473

459474
/* If we are indenting, don't add a space after a comma */
460-
intispaces=indent ?1 :2;
475+
intispaces=format==JsonFormatNormal ?2 :1;
476+
boolindent=format==JsonFormatIndented;
461477

462478
/*
463479
* Don't indent the very first item. This gets set to the indent flag at

‎src/backend/utils/adt/jsonfuncs.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4208,6 +4208,17 @@ jsonb_pretty(PG_FUNCTION_ARGS)
42084208
PG_RETURN_TEXT_P(cstring_to_text_with_len(str->data,str->len));
42094209
}
42104210

4211+
Datum
4212+
jsonb_canonical(PG_FUNCTION_ARGS)
4213+
{
4214+
Jsonb*jb=PG_GETARG_JSONB(0);
4215+
StringInfostr=makeStringInfo();
4216+
4217+
JsonbToCStringCanonical(str,&jb->root,VARSIZE(jb));
4218+
4219+
PG_RETURN_TEXT_P(cstring_to_text_with_len(str->data,str->len));
4220+
}
4221+
42114222
/*
42124223
* SQL function jsonb_concat (jsonb, jsonb)
42134224
*

‎src/include/catalog/pg_proc.dat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9512,6 +9512,9 @@
95129512
{ oid => '3306', descr => 'Indented text from jsonb',
95139513
proname => 'jsonb_pretty', prorettype => 'text', proargtypes => 'jsonb',
95149514
prosrc => 'jsonb_pretty' },
9515+
{ oid => '4035', descr => 'Canonical text from jsonb',
9516+
proname => 'jsonb_canonical', prorettype => 'text',
9517+
proargtypes => 'jsonb', prosrc => 'jsonb_canonical' },
95159518
{ oid => '3579', descr => 'Insert value into a jsonb',
95169519
proname => 'jsonb_insert', prorettype => 'jsonb',
95179520
proargtypes => 'jsonb _text jsonb bool', prosrc => 'jsonb_insert' },

‎src/include/utils/jsonb.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ extern char *JsonbToCString(StringInfo out, JsonbContainer *in,
372372
intestimated_len);
373373
externchar*JsonbToCStringIndent(StringInfoout,JsonbContainer*in,
374374
intestimated_len);
375+
externchar*JsonbToCStringCanonical(StringInfoout,JsonbContainer*in,
376+
intestimated_len);
375377
externboolJsonbExtractScalar(JsonbContainer*jbc,JsonbValue*res);
376378
externconstchar*JsonbTypeName(JsonbValue*jb);
377379

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp