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

Commit 1f3a021

Browse files
committed
Adjust pg_parse_json() so that it does not directly ereport().
Instead, it now returns a value indicating either success or the type of error which occurred. The old behavior is still available by calling pg_parse_json_or_ereport(). If the new interface is used, an error can be thrown by passing the return value of pg_parse_json() to json_ereport_error(). pg_parse_json() can still elog() in can't-happen cases, but it seems like that issue is best handled separately. Adjust json_lex() and json_count_array_elements() to return an error code, too. This is all in preparation for making the backend's json parser available to frontend code. Reviewed and/or tested by Mark Dilger and Andrew Dunstan. Discussion: http://postgr.es/m/CA+TgmoYfOXhd27MUDGioVh6QtpD0C1K-f6ObSA10AWiHBAL5bA@mail.gmail.com
1 parent 3e4818e commit 1f3a021

File tree

5 files changed

+342
-283
lines changed

5 files changed

+342
-283
lines changed

src/backend/utils/adt/json.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ json_in(PG_FUNCTION_ARGS)
8181

8282
/* validate it */
8383
lex = makeJsonLexContext(result, false);
84-
pg_parse_json(lex, &nullSemAction);
84+
pg_parse_json_or_ereport(lex, &nullSemAction);
8585

8686
/* Internal representation is the same as text, for now */
8787
PG_RETURN_TEXT_P(result);
@@ -128,7 +128,7 @@ json_recv(PG_FUNCTION_ARGS)
128128

129129
/* Validate it. */
130130
lex = makeJsonLexContextCstringLen(str, nbytes, false);
131-
pg_parse_json(lex, &nullSemAction);
131+
pg_parse_json_or_ereport(lex, &nullSemAction);
132132

133133
PG_RETURN_TEXT_P(cstring_to_text_with_len(str, nbytes));
134134
}
@@ -1337,12 +1337,15 @@ json_typeof(PG_FUNCTION_ARGS)
13371337
JsonLexContext *lex;
13381338
JsonTokenType tok;
13391339
char *type;
1340+
JsonParseErrorType result;
13401341

13411342
json = PG_GETARG_TEXT_PP(0);
13421343
lex = makeJsonLexContext(json, false);
13431344

13441345
/* Lex exactly one token from the input and check its type. */
1345-
json_lex(lex);
1346+
result = json_lex(lex);
1347+
if (result != JSON_SUCCESS)
1348+
json_ereport_error(result, lex);
13461349
tok = lex->token_type;
13471350
switch (tok)
13481351
{

0 commit comments

Comments
 (0)