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

Commit1c7856d

Browse files
author
Nikita Glukhov
committed
Add jsonb_canonical()
1 parentc85806e commit1c7856d

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
@@ -4307,6 +4307,17 @@ jsonb_pretty(PG_FUNCTION_ARGS)
43074307
PG_RETURN_TEXT_P(cstring_to_text_with_len(str->data,str->len));
43084308
}
43094309

4310+
Datum
4311+
jsonb_canonical(PG_FUNCTION_ARGS)
4312+
{
4313+
Jsonb*jb=PG_GETARG_JSONB(0);
4314+
StringInfostr=makeStringInfo();
4315+
4316+
JsonbToCStringCanonical(str,&jb->root,VARSIZE(jb));
4317+
4318+
PG_RETURN_TEXT_P(cstring_to_text_with_len(str->data,str->len));
4319+
}
4320+
43104321
/*
43114322
* SQL function jsonb_concat (jsonb, jsonb)
43124323
*

‎src/include/catalog/pg_proc.dat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9706,6 +9706,9 @@
97069706
{ oid => '3306', descr => 'Indented text from jsonb',
97079707
proname => 'jsonb_pretty', prorettype => 'text', proargtypes => 'jsonb',
97089708
prosrc => 'jsonb_pretty' },
9709+
{ oid => '2137', descr => 'Canonical text from jsonb',
9710+
proname => 'jsonb_canonical', prorettype => 'text',
9711+
proargtypes => 'jsonb', prosrc => 'jsonb_canonical' },
97099712
{ oid => '3579', descr => 'Insert value into a jsonb',
97109713
proname => 'jsonb_insert', prorettype => 'jsonb',
97119714
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
@@ -373,6 +373,8 @@ extern char *JsonbToCString(StringInfo out, JsonbContainer *in,
373373
intestimated_len);
374374
externchar*JsonbToCStringIndent(StringInfoout,JsonbContainer*in,
375375
intestimated_len);
376+
externchar*JsonbToCStringCanonical(StringInfoout,JsonbContainer*in,
377+
intestimated_len);
376378
externboolJsonbExtractScalar(JsonbContainer*jbc,JsonbValue*res);
377379
externconstchar*JsonbTypeName(JsonbValue*jb);
378380

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp