diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2022-12-11 10:39:05 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2022-12-11 10:39:05 -0500 |
commit | 50428a301d5ad46316cac2192f2ca8d91898aa3c (patch) | |
tree | af6ebd0e69eed53dfdd8f87eb7d36e228d274d5f /src/include/common/jsonapi.h | |
parent | d02ef65bce6575e10ac0b4e7b8552ff67687c944 (diff) | |
download | postgresql-50428a301d5ad46316cac2192f2ca8d91898aa3c.tar.gz postgresql-50428a301d5ad46316cac2192f2ca8d91898aa3c.zip |
Change JsonSemAction to allow non-throw error reporting.
Formerly, semantic action functions for the JSON parser returned void,
so that there was no way for them to affect the parser's behavior.
That means in particular that they can't force an error exit except by
longjmp'ing. That won't do in the context of our project to make input
functions return errors softly. Hence, change them to return the same
JsonParseErrorType enum value as the parser itself uses. If an action
function returns anything besides JSON_SUCCESS, the parse is abandoned
and that error code is returned.
Action functions can thus easily return the same error conditions that
the parser already knows about. As an escape hatch for expansion, also
invent a code JSON_SEM_ACTION_FAILED that the core parser does not know
the exact meaning of. When returning this code, an action function
must use some out-of-band mechanism for reporting the error details.
This commit simply makes the API change and causes all the existing
action functions to return JSON_SUCCESS, so that there is no actual
change in behavior here. This is long enough and boring enough that
it seemed best to commit it separately from the changes that make
real use of the new mechanism.
In passing, remove a duplicate assignment of
transform_string_values_scalar.
Discussion: https://postgr.es/m/1436686.1670701118@sss.pgh.pa.us
Diffstat (limited to 'src/include/common/jsonapi.h')
-rw-r--r-- | src/include/common/jsonapi.h | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/src/include/common/jsonapi.h b/src/include/common/jsonapi.h index 8d31630e5ce..663064e659d 100644 --- a/src/include/common/jsonapi.h +++ b/src/include/common/jsonapi.h @@ -16,7 +16,7 @@ #include "lib/stringinfo.h" -typedef enum +typedef enum JsonTokenType { JSON_TOKEN_INVALID, JSON_TOKEN_STRING, @@ -33,7 +33,7 @@ typedef enum JSON_TOKEN_END } JsonTokenType; -typedef enum +typedef enum JsonParseErrorType { JSON_SUCCESS, JSON_ESCAPING_INVALID, @@ -52,7 +52,8 @@ typedef enum JSON_UNICODE_ESCAPE_FORMAT, JSON_UNICODE_HIGH_ESCAPE, JSON_UNICODE_HIGH_SURROGATE, - JSON_UNICODE_LOW_SURROGATE + JSON_UNICODE_LOW_SURROGATE, + JSON_SEM_ACTION_FAILED /* error should already be reported */ } JsonParseErrorType; @@ -84,14 +85,15 @@ typedef struct JsonLexContext StringInfo strval; } JsonLexContext; -typedef void (*json_struct_action) (void *state); -typedef void (*json_ofield_action) (void *state, char *fname, bool isnull); -typedef void (*json_aelem_action) (void *state, bool isnull); -typedef void (*json_scalar_action) (void *state, char *token, JsonTokenType tokentype); +typedef JsonParseErrorType (*json_struct_action) (void *state); +typedef JsonParseErrorType (*json_ofield_action) (void *state, char *fname, bool isnull); +typedef JsonParseErrorType (*json_aelem_action) (void *state, bool isnull); +typedef JsonParseErrorType (*json_scalar_action) (void *state, char *token, JsonTokenType tokentype); /* * Semantic Action structure for use in parsing json. + * * Any of these actions can be NULL, in which case nothing is done at that * point, Likewise, semstate can be NULL. Using an all-NULL structure amounts * to doing a pure parse with no side-effects, and is therefore exactly @@ -100,6 +102,11 @@ typedef void (*json_scalar_action) (void *state, char *token, JsonTokenType toke * The 'fname' and 'token' strings passed to these actions are palloc'd. * They are not free'd or used further by the parser, so the action function * is free to do what it wishes with them. + * + * All action functions return JsonParseErrorType. If the result isn't + * JSON_SUCCESS, the parse is abandoned and that error code is returned. + * If it is JSON_SEM_ACTION_FAILED, the action function is responsible + * for having reported the error in some appropriate way. */ typedef struct JsonSemAction { |