Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ea5ff58

Browse files
committed
Be clearer about when jsonapi's need_escapes is needed
Most operations beyond pure json parsing need to set need_escapes to true to get access to field names and string scalars. Document this fact 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 parent d3d0983 commit ea5ff58

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
return result;
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
return report_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
return JSON_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
struct jsonapi_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
typedef JsonParseErrorType (*json_struct_action) (void *state);
122128
typedef JsonParseErrorType (*json_ofield_action) (void *state, char *fname, bool isnull);
123129
typedef JsonParseErrorType (*json_aelem_action) (void *state, bool isnull);
@@ -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
extern JsonLexContext *makeJsonLexContextCstringLen(JsonLexContext *lex,
208219
const char *json,

0 commit comments

Comments
 (0)