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

Commitea5ff58

Browse files
committed
Be clearer about when jsonapi's need_escapes is needed
Most operations beyond pure json parsing need to set need_escapes totrue to get access to field names and string scalars. Document thisfact more explicitly.Slightly tweaked patch from:Author: Corey Huinker <corey.huinker@gmail.com>Discussion:https://postgr.es/m/CADkLM=c49Vkfg2+A8ubSuEtaGEjuaKZXCA6SrXA8kdwHjx3uxQ@mail.gmail.com
1 parentd3d0983 commitea5ff58

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

‎src/common/jsonapi.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,10 @@ parse_scalar(JsonLexContext *lex, const JsonSemAction *sem)
12971297
returnresult;
12981298
}
12991299

1300-
/* invoke the callback, which may take ownership of val */
1300+
/*
1301+
* invoke the callback, which may take ownership of val. For string
1302+
* values, val is NULL if need_escapes is false.
1303+
*/
13011304
result= (*sfunc) (sem->semstate,val,tok);
13021305

13031306
if (lex->flags&JSONLEX_CTX_OWNS_TOKENS)
@@ -1326,6 +1329,7 @@ parse_object_field(JsonLexContext *lex, const JsonSemAction *sem)
13261329
returnreport_parse_error(JSON_PARSE_STRING,lex);
13271330
if ((ostart!=NULL||oend!=NULL)&&lex->need_escapes)
13281331
{
1332+
/* fname is NULL if need_escapes is false */
13291333
fname=STRDUP(lex->strval->data);
13301334
if (fname==NULL)
13311335
returnJSON_OUT_OF_MEMORY;

‎src/include/common/jsonapi.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ typedef struct JsonLexContext
118118
structjsonapi_StrValType*errormsg;
119119
}JsonLexContext;
120120

121+
/*
122+
* Function types for custom json parsing actions.
123+
*
124+
* fname will be NULL if the context has need_escapes=false, as will token for
125+
* string type values.
126+
*/
121127
typedefJsonParseErrorType (*json_struct_action) (void*state);
122128
typedefJsonParseErrorType (*json_ofield_action) (void*state,char*fname,boolisnull);
123129
typedefJsonParseErrorType (*json_aelem_action) (void*state,boolisnull);
@@ -197,12 +203,17 @@ extern JsonParseErrorType json_count_array_elements(JsonLexContext *lex,
197203
* struct is allocated.
198204
*
199205
* If need_escapes is true, ->strval stores the unescaped lexemes.
206+
*
207+
* Setting need_escapes to true is necessary if the operation needs
208+
* to reference field names or scalar string values. This is true of most
209+
* operations beyond purely checking the json-validity of the source
210+
* document.
211+
*
200212
* Unescaping is expensive, so only request it when necessary.
201213
*
202214
* If need_escapes is true or lex was given as NULL, then the caller is
203215
* responsible for freeing the returned struct, either by calling
204-
* freeJsonLexContext() or (in backend environment) via memory context
205-
* cleanup.
216+
* freeJsonLexContext() or (in backends) via memory context cleanup.
206217
*/
207218
externJsonLexContext*makeJsonLexContextCstringLen(JsonLexContext*lex,
208219
constchar*json,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp