From 1f3a021730be98b880d94cabbe21de7e4d8136f5 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Mon, 27 Jan 2020 11:03:21 -0500 Subject: 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 --- src/backend/utils/adt/json.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/backend/utils/adt/json.c') diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c index 4be16b5c201..e73a60ece8a 100644 --- a/src/backend/utils/adt/json.c +++ b/src/backend/utils/adt/json.c @@ -81,7 +81,7 @@ json_in(PG_FUNCTION_ARGS) /* validate it */ lex = makeJsonLexContext(result, false); - pg_parse_json(lex, &nullSemAction); + pg_parse_json_or_ereport(lex, &nullSemAction); /* Internal representation is the same as text, for now */ PG_RETURN_TEXT_P(result); @@ -128,7 +128,7 @@ json_recv(PG_FUNCTION_ARGS) /* Validate it. */ lex = makeJsonLexContextCstringLen(str, nbytes, false); - pg_parse_json(lex, &nullSemAction); + pg_parse_json_or_ereport(lex, &nullSemAction); PG_RETURN_TEXT_P(cstring_to_text_with_len(str, nbytes)); } @@ -1337,12 +1337,15 @@ json_typeof(PG_FUNCTION_ARGS) JsonLexContext *lex; JsonTokenType tok; char *type; + JsonParseErrorType result; json = PG_GETARG_TEXT_PP(0); lex = makeJsonLexContext(json, false); /* Lex exactly one token from the input and check its type. */ - json_lex(lex); + result = json_lex(lex); + if (result != JSON_SUCCESS) + json_ereport_error(result, lex); tok = lex->token_type; switch (tok) { -- cgit v1.2.3