Convert json_in and jsonb_in to report errors softly.
authorTom Lane <[email protected]>
Sun, 11 Dec 2022 16:28:15 +0000 (11:28 -0500)
committerTom Lane <[email protected]>
Sun, 11 Dec 2022 16:28:15 +0000 (11:28 -0500)
This requires a bit of further infrastructure-extension to allow
trapping errors reported by numeric_in and pg_unicode_to_server,
but otherwise it's pretty straightforward.

In the case of jsonb_in, we are only capturing errors reported
during the initial "parse" phase.  The value-construction phase
(JsonbValueToJsonb) can also throw errors if assorted implementation
limits are exceeded.  We should improve that, but it seems like a
separable project.

Andrew Dunstan and Tom Lane

Discussion: https://postgr.es/m/3bac9841-fe07-713d-fa42-606c225567d6@dunslane.net

17 files changed:
src/backend/utils/adt/json.c
src/backend/utils/adt/jsonb.c
src/backend/utils/adt/jsonfuncs.c
src/backend/utils/fmgr/fmgr.c
src/backend/utils/mb/mbutils.c
src/common/jsonapi.c
src/include/common/jsonapi.h
src/include/fmgr.h
src/include/mb/pg_wchar.h
src/include/utils/jsonfuncs.h
src/test/regress/expected/json.out
src/test/regress/expected/json_encoding.out
src/test/regress/expected/json_encoding_1.out
src/test/regress/expected/jsonb.out
src/test/regress/sql/json.sql
src/test/regress/sql/json_encoding.sql
src/test/regress/sql/jsonb.sql

index fee2ffb55c8b745cb24ce4017ae3feddfd4de5bb..e6896eccfe9a95b8bcdbd1d28beaacea26016a5e 100644 (file)
@@ -81,9 +81,10 @@ json_in(PG_FUNCTION_ARGS)
 
        /* validate it */
        lex = makeJsonLexContext(result, false);
-       pg_parse_json_or_ereport(lex, &nullSemAction);
+       if (!pg_parse_json_or_errsave(lex, &nullSemAction, fcinfo->context))
+               PG_RETURN_NULL();
 
-       /* Internal representation is the same as text, for now */
+       /* Internal representation is the same as text */
        PG_RETURN_TEXT_P(result);
 }
 
@@ -1337,7 +1338,7 @@ json_typeof(PG_FUNCTION_ARGS)
        /* Lex exactly one token from the input and check its type. */
        result = json_lex(lex);
        if (result != JSON_SUCCESS)
-               json_ereport_error(result, lex);
+               json_errsave_error(result, lex, NULL);
        tok = lex->token_type;
        switch (tok)
        {
index a6d8fdb06825ab9f3e138bd5fb1d2a8b94567b21..7c1e5e614451ba83758856880fe8ccc914a58cb2 100644 (file)
@@ -33,6 +33,7 @@ typedef struct JsonbInState
 {
        JsonbParseState *parseState;
        JsonbValue *res;
+       Node       *escontext;
 } JsonbInState;
 
 /* unlike with json categories, we need to treat json and jsonb differently */
@@ -61,8 +62,8 @@ typedef struct JsonbAggState
        Oid                     val_output_func;
 } JsonbAggState;
 
-static inline Datum jsonb_from_cstring(char *json, int len);
-static size_t checkStringLen(size_t len);
+static inline Datum jsonb_from_cstring(char *json, int len, Node *escontext);
+static bool checkStringLen(size_t len, Node *escontext);
 static JsonParseErrorType jsonb_in_object_start(void *pstate);
 static JsonParseErrorType jsonb_in_object_end(void *pstate);
 static JsonParseErrorType jsonb_in_array_start(void *pstate);
@@ -98,7 +99,7 @@ jsonb_in(PG_FUNCTION_ARGS)
 {
        char       *json = PG_GETARG_CSTRING(0);
 
-       return jsonb_from_cstring(json, strlen(json));
+       return jsonb_from_cstring(json, strlen(json), fcinfo->context);
 }
 
 /*
@@ -122,7 +123,7 @@ jsonb_recv(PG_FUNCTION_ARGS)
        else
                elog(ERROR, "unsupported jsonb version number %d", version);
 
-       return jsonb_from_cstring(str, nbytes);
+       return jsonb_from_cstring(str, nbytes, NULL);
 }
 
 /*
@@ -251,9 +252,12 @@ jsonb_typeof(PG_FUNCTION_ARGS)
  * Turns json string into a jsonb Datum.
  *
  * Uses the json parser (with hooks) to construct a jsonb.
+ *
+ * If escontext points to an ErrorSaveContext, errors are reported there
+ * instead of being thrown.
  */
 static inline Datum
-jsonb_from_cstring(char *json, int len)
+jsonb_from_cstring(char *json, int len, Node *escontext)
 {
        JsonLexContext *lex;
        JsonbInState state;
@@ -263,6 +267,7 @@ jsonb_from_cstring(char *json, int len)
        memset(&sem, 0, sizeof(sem));
        lex = makeJsonLexContextCstringLen(json, len, GetDatabaseEncoding(), true);
 
+       state.escontext = escontext;
        sem.semstate = (void *) &state;
 
        sem.object_start = jsonb_in_object_start;
@@ -272,23 +277,24 @@ jsonb_from_cstring(char *json, int len)
        sem.scalar = jsonb_in_scalar;
        sem.object_field_start = jsonb_in_object_field_start;
 
-       pg_parse_json_or_ereport(lex, &sem);
+       if (!pg_parse_json_or_errsave(lex, &sem, escontext))
+               return (Datum) 0;
 
        /* after parsing, the item member has the composed jsonb structure */
        PG_RETURN_POINTER(JsonbValueToJsonb(state.res));
 }
 
-static size_t
-checkStringLen(size_t len)
+static bool
+checkStringLen(size_t len, Node *escontext)
 {
        if (len > JENTRY_OFFLENMASK)
-               ereport(ERROR,
+               ereturn(escontext, false,
                                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                                 errmsg("string too long to represent as jsonb string"),
                                 errdetail("Due to an implementation restriction, jsonb strings cannot exceed %d bytes.",
                                                   JENTRY_OFFLENMASK)));
 
-       return len;
+       return true;
 }
 
 static JsonParseErrorType
@@ -339,7 +345,9 @@ jsonb_in_object_field_start(void *pstate, char *fname, bool isnull)
 
        Assert(fname != NULL);
        v.type = jbvString;
-       v.val.string.len = checkStringLen(strlen(fname));
+       v.val.string.len = strlen(fname);
+       if (!checkStringLen(v.val.string.len, _state->escontext))
+               return JSON_SEM_ACTION_FAILED;
        v.val.string.val = fname;
 
        _state->res = pushJsonbValue(&_state->parseState, WJB_KEY, &v);
@@ -390,7 +398,9 @@ jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype)
                case JSON_TOKEN_STRING:
                        Assert(token != NULL);
                        v.type = jbvString;
-                       v.val.string.len = checkStringLen(strlen(token));
+                       v.val.string.len = strlen(token);
+                       if (!checkStringLen(v.val.string.len, _state->escontext))
+                               return JSON_SEM_ACTION_FAILED;
                        v.val.string.val = token;
                        break;
                case JSON_TOKEN_NUMBER:
@@ -401,10 +411,11 @@ jsonb_in_scalar(void *pstate, char *token, JsonTokenType tokentype)
                         */
                        Assert(token != NULL);
                        v.type = jbvNumeric;
-                       numd = DirectFunctionCall3(numeric_in,
-                                                                          CStringGetDatum(token),
-                                                                          ObjectIdGetDatum(InvalidOid),
-                                                                          Int32GetDatum(-1));
+                       if (!DirectInputFunctionCallSafe(numeric_in, token,
+                                                                                        InvalidOid, -1,
+                                                                                        _state->escontext,
+                                                                                        &numd))
+                               return JSON_SEM_ACTION_FAILED;
                        v.val.numeric = DatumGetNumeric(numd);
                        break;
                case JSON_TOKEN_TRUE:
@@ -738,6 +749,9 @@ jsonb_categorize_type(Oid typoid,
  *
  * If key_scalar is true, the value is stored as a key, so insist
  * it's of an acceptable type, and force it to be a jbvString.
+ *
+ * Note: currently, we assume that result->escontext is NULL and errors
+ * will be thrown.
  */
 static void
 datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
@@ -910,7 +924,8 @@ datum_to_jsonb(Datum val, bool is_null, JsonbInState *result,
                        default:
                                outputstr = OidOutputFunctionCall(outfuncoid, val);
                                jb.type = jbvString;
-                               jb.val.string.len = checkStringLen(strlen(outputstr));
+                               jb.val.string.len = strlen(outputstr);
+                               (void) checkStringLen(jb.val.string.len, NULL);
                                jb.val.string.val = outputstr;
                                break;
                }
@@ -1648,6 +1663,7 @@ jsonb_agg_finalfn(PG_FUNCTION_ARGS)
         * shallow clone is sufficient as we aren't going to change any of the
         * values, just add the final array end marker.
         */
+       memset(&result, 0, sizeof(JsonbInState));
 
        result.parseState = clone_parse_state(arg->res->parseState);
 
@@ -1880,6 +1896,7 @@ jsonb_object_agg_finalfn(PG_FUNCTION_ARGS)
         * going to change any of the values, just add the final object end
         * marker.
         */
+       memset(&result, 0, sizeof(JsonbInState));
 
        result.parseState = clone_parse_state(arg->res->parseState);
 
index d6cad952790ad3c46d252ffcaf0398d3e0953afb..376a9b7ab8215304f58ff977539f1f000d1a545a 100644 (file)
@@ -25,6 +25,7 @@
 #include "lib/stringinfo.h"
 #include "mb/pg_wchar.h"
 #include "miscadmin.h"
+#include "nodes/miscnodes.h"
 #include "utils/array.h"
 #include "utils/builtins.h"
 #include "utils/fmgroids.h"
@@ -490,21 +491,31 @@ static JsonParseErrorType transform_string_values_object_field_start(void *state
 static JsonParseErrorType transform_string_values_array_element_start(void *state, bool isnull);
 static JsonParseErrorType transform_string_values_scalar(void *state, char *token, JsonTokenType tokentype);
 
+
 /*
- * pg_parse_json_or_ereport
+ * pg_parse_json_or_errsave
  *
  * This function is like pg_parse_json, except that it does not return a
  * JsonParseErrorType. Instead, in case of any failure, this function will
+ * save error data into *escontext if that's an ErrorSaveContext, otherwise
  * ereport(ERROR).
+ *
+ * Returns a boolean indicating success or failure (failure will only be
+ * returned when escontext is an ErrorSaveContext).
  */
-void
-pg_parse_json_or_ereport(JsonLexContext *lex, JsonSemAction *sem)
+bool
+pg_parse_json_or_errsave(JsonLexContext *lex, JsonSemAction *sem,
+                                                Node *escontext)
 {
        JsonParseErrorType result;
 
        result = pg_parse_json(lex, sem);
        if (result != JSON_SUCCESS)
-               json_ereport_error(result, lex);
+       {
+               json_errsave_error(result, lex, escontext);
+               return false;
+       }
+       return true;
 }
 
 /*
@@ -608,17 +619,25 @@ jsonb_object_keys(PG_FUNCTION_ARGS)
  * Report a JSON error.
  */
 void
-json_ereport_error(JsonParseErrorType error, JsonLexContext *lex)
+json_errsave_error(JsonParseErrorType error, JsonLexContext *lex,
+                                  Node *escontext)
 {
        if (error == JSON_UNICODE_HIGH_ESCAPE ||
+               error == JSON_UNICODE_UNTRANSLATABLE ||
                error == JSON_UNICODE_CODE_POINT_ZERO)
-               ereport(ERROR,
+               errsave(escontext,
                                (errcode(ERRCODE_UNTRANSLATABLE_CHARACTER),
                                 errmsg("unsupported Unicode escape sequence"),
                                 errdetail_internal("%s", json_errdetail(error, lex)),
                                 report_json_context(lex)));
+       else if (error == JSON_SEM_ACTION_FAILED)
+       {
+               /* semantic action function had better have reported something */
+               if (!SOFT_ERROR_OCCURRED(escontext))
+                       elog(ERROR, "JSON semantic action function did not provide error information");
+       }
        else
-               ereport(ERROR,
+               errsave(escontext,
                                (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
                                 errmsg("invalid input syntax for type %s", "json"),
                                 errdetail_internal("%s", json_errdetail(error, lex)),
@@ -1274,7 +1293,7 @@ get_array_start(void *state)
 
                        error = json_count_array_elements(_state->lex, &nelements);
                        if (error != JSON_SUCCESS)
-                               json_ereport_error(error, _state->lex);
+                               json_errsave_error(error, _state->lex, NULL);
 
                        if (-_state->path_indexes[lex_level] <= nelements)
                                _state->path_indexes[lex_level] += nelements;
index 0d37f69298f731d7963aedb455a2bbca33043f2e..7b28a266ce3b7673903329e280b3e1a063f1f726 100644 (file)
@@ -1614,6 +1614,51 @@ InputFunctionCallSafe(FmgrInfo *flinfo, char *str,
        return true;
 }
 
+/*
+ * Call a directly-named datatype input function, with non-exception
+ * handling of "soft" errors.
+ *
+ * This is like InputFunctionCallSafe, except that it is given a direct
+ * pointer to the C function to call.  We assume that that function is
+ * strict.  Also, the function cannot be one that needs to
+ * look at FmgrInfo, since there won't be any.
+ */
+bool
+DirectInputFunctionCallSafe(PGFunction func, char *str,
+                                                       Oid typioparam, int32 typmod,
+                                                       fmNodePtr escontext,
+                                                       Datum *result)
+{
+       LOCAL_FCINFO(fcinfo, 3);
+
+       if (str == NULL)
+       {
+               *result = (Datum) 0;    /* just return null result */
+               return true;
+       }
+
+       InitFunctionCallInfoData(*fcinfo, NULL, 3, InvalidOid, escontext, NULL);
+
+       fcinfo->args[0].value = CStringGetDatum(str);
+       fcinfo->args[0].isnull = false;
+       fcinfo->args[1].value = ObjectIdGetDatum(typioparam);
+       fcinfo->args[1].isnull = false;
+       fcinfo->args[2].value = Int32GetDatum(typmod);
+       fcinfo->args[2].isnull = false;
+
+       *result = (*func) (fcinfo);
+
+       /* Result value is garbage, and could be null, if an error was reported */
+       if (SOFT_ERROR_OCCURRED(escontext))
+               return false;
+
+       /* Otherwise, shouldn't get null result */
+       if (fcinfo->isnull)
+               elog(ERROR, "input function %p returned NULL", (void *) func);
+
+       return true;
+}
+
 /*
  * Call a previously-looked-up datatype output function.
  *
index 474ab476f5fbe5cdadcaa0bccd2cf7245cd8b330..24f37e3ec9826129ec3d15de2d6c530529d9ec25 100644 (file)
@@ -916,6 +916,63 @@ pg_unicode_to_server(pg_wchar c, unsigned char *s)
                                  BoolGetDatum(false));
 }
 
+/*
+ * Convert a single Unicode code point into a string in the server encoding.
+ *
+ * Same as pg_unicode_to_server(), except that we don't throw errors,
+ * but simply return false on conversion failure.
+ */
+bool
+pg_unicode_to_server_noerror(pg_wchar c, unsigned char *s)
+{
+       unsigned char c_as_utf8[MAX_MULTIBYTE_CHAR_LEN + 1];
+       int                     c_as_utf8_len;
+       int                     converted_len;
+       int                     server_encoding;
+
+       /* Fail if invalid Unicode code point */
+       if (!is_valid_unicode_codepoint(c))
+               return false;
+
+       /* Otherwise, if it's in ASCII range, conversion is trivial */
+       if (c <= 0x7F)
+       {
+               s[0] = (unsigned char) c;
+               s[1] = '\0';
+               return true;
+       }
+
+       /* If the server encoding is UTF-8, we just need to reformat the code */
+       server_encoding = GetDatabaseEncoding();
+       if (server_encoding == PG_UTF8)
+       {
+               unicode_to_utf8(c, s);
+               s[pg_utf_mblen(s)] = '\0';
+               return true;
+       }
+
+       /* For all other cases, we must have a conversion function available */
+       if (Utf8ToServerConvProc == NULL)
+               return false;
+
+       /* Construct UTF-8 source string */
+       unicode_to_utf8(c, c_as_utf8);
+       c_as_utf8_len = pg_utf_mblen(c_as_utf8);
+       c_as_utf8[c_as_utf8_len] = '\0';
+
+       /* Convert, but without throwing error if we can't */
+       converted_len = DatumGetInt32(FunctionCall6(Utf8ToServerConvProc,
+                                                                                               Int32GetDatum(PG_UTF8),
+                                                                                               Int32GetDatum(server_encoding),
+                                                                                               CStringGetDatum((char *) c_as_utf8),
+                                                                                               CStringGetDatum((char *) s),
+                                                                                               Int32GetDatum(c_as_utf8_len),
+                                                                                               BoolGetDatum(true)));
+
+       /* Conversion was successful iff it consumed the whole input */
+       return (converted_len == c_as_utf8_len);
+}
+
 
 /* convert a multibyte string to a wchar */
 int
index 83c286b89bae85b92f53cb691fcf920551b5b4fe..773ae8074ad47cc0cc62fda20a5930afdd60a331 100644 (file)
@@ -791,19 +791,16 @@ json_lex_string(JsonLexContext *lex)
 
                                        /*
                                         * Add the represented character to lex->strval.  In the
-                                        * backend, we can let pg_unicode_to_server() handle any
-                                        * required character set conversion; in frontend, we can
-                                        * only deal with trivial conversions.
-                                        *
-                                        * Note: pg_unicode_to_server() will throw an error for a
-                                        * conversion failure, rather than returning a failure
-                                        * indication.  That seems OK.
+                                        * backend, we can let pg_unicode_to_server_noerror()
+                                        * handle any required character set conversion; in
+                                        * frontend, we can only deal with trivial conversions.
                                         */
 #ifndef FRONTEND
                                        {
                                                char            cbuf[MAX_UNICODE_EQUIVALENT_STRING + 1];
 
-                                               pg_unicode_to_server(ch, (unsigned char *) cbuf);
+                                               if (!pg_unicode_to_server_noerror(ch, (unsigned char *) cbuf))
+                                                       return JSON_UNICODE_UNTRANSLATABLE;
                                                appendStringInfoString(lex->strval, cbuf);
                                        }
 #else
@@ -1167,6 +1164,10 @@ json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
                case JSON_UNICODE_HIGH_ESCAPE:
                        /* note: this case is only reachable in frontend not backend */
                        return _("Unicode escape values cannot be used for code point values above 007F when the encoding is not UTF8.");
+               case JSON_UNICODE_UNTRANSLATABLE:
+                       /* note: this case is only reachable in backend not frontend */
+                       return psprintf(_("Unicode escape value could not be translated to the server's encoding %s."),
+                                                       GetDatabaseEncodingName());
                case JSON_UNICODE_HIGH_SURROGATE:
                        return _("Unicode high surrogate must not follow a high surrogate.");
                case JSON_UNICODE_LOW_SURROGATE:
index 663064e659d2e49b2bee17db6739ef00f7138a93..884c86cae7f48b22fbe336868d36c78624724a24 100644 (file)
@@ -51,6 +51,7 @@ typedef enum JsonParseErrorType
        JSON_UNICODE_CODE_POINT_ZERO,
        JSON_UNICODE_ESCAPE_FORMAT,
        JSON_UNICODE_HIGH_ESCAPE,
+       JSON_UNICODE_UNTRANSLATABLE,
        JSON_UNICODE_HIGH_SURROGATE,
        JSON_UNICODE_LOW_SURROGATE,
        JSON_SEM_ACTION_FAILED          /* error should already be reported */
index b7832d0aa2a622a81347791422a422f43c9e3d97..972afe3aff98690b8a449046e47d157cfe077b5c 100644 (file)
@@ -704,6 +704,10 @@ extern bool InputFunctionCallSafe(FmgrInfo *flinfo, char *str,
                                                                  Oid typioparam, int32 typmod,
                                                                  fmNodePtr escontext,
                                                                  Datum *result);
+extern bool DirectInputFunctionCallSafe(PGFunction func, char *str,
+                                                                               Oid typioparam, int32 typmod,
+                                                                               fmNodePtr escontext,
+                                                                               Datum *result);
 extern Datum OidInputFunctionCall(Oid functionId, char *str,
                                                                  Oid typioparam, int32 typmod);
 extern char *OutputFunctionCall(FmgrInfo *flinfo, Datum val);
index 4f2643742e1ee84878a042f49cc9d6c0af9a39f2..7aa0bc1c59078574dad8d15f0986701de7be9259 100644 (file)
@@ -650,6 +650,7 @@ extern char *pg_any_to_server(const char *s, int len, int encoding);
 extern char *pg_server_to_any(const char *s, int len, int encoding);
 
 extern void pg_unicode_to_server(pg_wchar c, unsigned char *s);
+extern bool pg_unicode_to_server_noerror(pg_wchar c, unsigned char *s);
 
 extern unsigned short BIG5toCNS(unsigned short big5, unsigned char *lc);
 extern unsigned short CNStoBIG5(unsigned short cns, unsigned char lc);
index 865b2ff7c11b99d33fc44dd35d83f30a210caafe..7fad0269f62b09766eb4d085cf614091ac4f2920 100644 (file)
@@ -39,11 +39,16 @@ typedef text *(*JsonTransformStringValuesAction) (void *state, char *elem_value,
 /* build a JsonLexContext from a text datum */
 extern JsonLexContext *makeJsonLexContext(text *json, bool need_escapes);
 
-/* try to parse json, and ereport(ERROR) on failure */
-extern void pg_parse_json_or_ereport(JsonLexContext *lex, JsonSemAction *sem);
+/* try to parse json, and errsave(escontext) on failure */
+extern bool pg_parse_json_or_errsave(JsonLexContext *lex, JsonSemAction *sem,
+                                                                        struct Node *escontext);
 
-/* report an error during json lexing or parsing */
-extern void json_ereport_error(JsonParseErrorType error, JsonLexContext *lex);
+#define pg_parse_json_or_ereport(lex, sem) \
+       (void) pg_parse_json_or_errsave(lex, sem, NULL)
+
+/* save an error during json lexing or parsing */
+extern void json_errsave_error(JsonParseErrorType error, JsonLexContext *lex,
+                                                          struct Node *escontext);
 
 extern uint32 parse_jsonb_index_flags(Jsonb *jb);
 extern void iterate_jsonb_values(Jsonb *jb, uint32 flags, void *state,
index cb181226e9fb9cde624e66e4ab73a56e13a09124..af96ce4180296c3de6dacb4c59dd928346dc94e5 100644 (file)
@@ -320,6 +320,25 @@ LINE 1: SELECT '{
 DETAIL:  Expected JSON value, but found "}".
 CONTEXT:  JSON data, line 4: ...yveryveryveryveryveryveryveryverylongfieldname":}
 -- ERROR missing value for last field
+-- test non-error-throwing input
+select pg_input_is_valid('{"a":true}', 'json');
+ pg_input_is_valid 
+-------------------
+ t
+(1 row)
+
+select pg_input_is_valid('{"a":true', 'json');
+ pg_input_is_valid 
+-------------------
+ f
+(1 row)
+
+select pg_input_error_message('{"a":true', 'json');
+       pg_input_error_message       
+------------------------------------
+ invalid input syntax for type json
+(1 row)
+
 --constructors
 -- array_to_json
 SELECT array_to_json(array(select 1 as a));
index f343f74fe18238e85a7c03c18e7c022b3463af82..083621fb21f120b1e413dc44d3c4f2bb2aeb149b 100644 (file)
@@ -260,3 +260,10 @@ SELECT jsonb '{ "a":  "null \\u0000 escape" }' ->> 'a' as not_an_escape;
  null \u0000 escape
 (1 row)
 
+-- soft error for input-time failure
+select pg_input_error_message('{ "a":  "\ud83d\ude04\ud83d\udc36" }', 'jsonb');
+ pg_input_error_message 
+------------------------
+(1 row)
+
index e2fc131b0fae6c58b5f4e6d0fbf8218d21de8ee1..021d226f8dde63e8e41916facb3482b517c7f863 100644 (file)
@@ -48,7 +48,9 @@ SELECT '"\uaBcD"'::json;              -- OK, uppercase and lower case both OK
 
 -- handling of unicode surrogate pairs
 select json '{ "a":  "\ud83d\ude04\ud83d\udc36" }' -> 'a' as correct_in_utf8;
-ERROR:  conversion between UTF8 and SQL_ASCII is not supported
+ERROR:  unsupported Unicode escape sequence
+DETAIL:  Unicode escape value could not be translated to the server's encoding SQL_ASCII.
+CONTEXT:  JSON data, line 1: { "a":...
 select json '{ "a":  "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
 ERROR:  invalid input syntax for type json
 DETAIL:  Unicode high surrogate must not follow a high surrogate.
@@ -97,7 +99,9 @@ select json '{ "a":  "null \\u0000 escape" }' as not_an_escape;
 (1 row)
 
 select json '{ "a":  "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
-ERROR:  conversion between UTF8 and SQL_ASCII is not supported
+ERROR:  unsupported Unicode escape sequence
+DETAIL:  Unicode escape value could not be translated to the server's encoding SQL_ASCII.
+CONTEXT:  JSON data, line 1: { "a":...
 select json '{ "a":  "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
  correct_everywhere 
 --------------------
@@ -155,14 +159,18 @@ CONTEXT:  JSON data, line 1: ...
 -- use octet_length here so we don't get an odd unicode char in the
 -- output
 SELECT octet_length('"\uaBcD"'::jsonb::text); -- OK, uppercase and lower case both OK
-ERROR:  conversion between UTF8 and SQL_ASCII is not supported
+ERROR:  unsupported Unicode escape sequence
 LINE 1: SELECT octet_length('"\uaBcD"'::jsonb::text);
                             ^
+DETAIL:  Unicode escape value could not be translated to the server's encoding SQL_ASCII.
+CONTEXT:  JSON data, line 1: ...
 -- handling of unicode surrogate pairs
 SELECT octet_length((jsonb '{ "a":  "\ud83d\ude04\ud83d\udc36" }' -> 'a')::text) AS correct_in_utf8;
-ERROR:  conversion between UTF8 and SQL_ASCII is not supported
+ERROR:  unsupported Unicode escape sequence
 LINE 1: SELECT octet_length((jsonb '{ "a":  "\ud83d\ude04\ud83d\udc3...
                                    ^
+DETAIL:  Unicode escape value could not be translated to the server's encoding SQL_ASCII.
+CONTEXT:  JSON data, line 1: { "a":...
 SELECT jsonb '{ "a":  "\ud83d\ud83d" }' -> 'a'; -- 2 high surrogates in a row
 ERROR:  invalid input syntax for type json
 LINE 1: SELECT jsonb '{ "a":  "\ud83d\ud83d" }' -> 'a';
@@ -189,9 +197,11 @@ DETAIL:  Unicode low surrogate must follow a high surrogate.
 CONTEXT:  JSON data, line 1: { "a":...
 -- handling of simple unicode escapes
 SELECT jsonb '{ "a":  "the Copyright \u00a9 sign" }' as correct_in_utf8;
-ERROR:  conversion between UTF8 and SQL_ASCII is not supported
+ERROR:  unsupported Unicode escape sequence
 LINE 1: SELECT jsonb '{ "a":  "the Copyright \u00a9 sign" }' as corr...
                      ^
+DETAIL:  Unicode escape value could not be translated to the server's encoding SQL_ASCII.
+CONTEXT:  JSON data, line 1: { "a":...
 SELECT jsonb '{ "a":  "dollar \u0024 character" }' as correct_everywhere;
      correct_everywhere      
 -----------------------------
@@ -217,9 +227,11 @@ SELECT jsonb '{ "a":  "null \\u0000 escape" }' as not_an_escape;
 (1 row)
 
 SELECT jsonb '{ "a":  "the Copyright \u00a9 sign" }' ->> 'a' as correct_in_utf8;
-ERROR:  conversion between UTF8 and SQL_ASCII is not supported
+ERROR:  unsupported Unicode escape sequence
 LINE 1: SELECT jsonb '{ "a":  "the Copyright \u00a9 sign" }' ->> 'a'...
                      ^
+DETAIL:  Unicode escape value could not be translated to the server's encoding SQL_ASCII.
+CONTEXT:  JSON data, line 1: { "a":...
 SELECT jsonb '{ "a":  "dollar \u0024 character" }' ->> 'a' as correct_everywhere;
  correct_everywhere 
 --------------------
@@ -244,3 +256,10 @@ SELECT jsonb '{ "a":  "null \\u0000 escape" }' ->> 'a' as not_an_escape;
  null \u0000 escape
 (1 row)
 
+-- soft error for input-time failure
+select pg_input_error_message('{ "a":  "\ud83d\ude04\ud83d\udc36" }', 'jsonb');
+       pg_input_error_message        
+-------------------------------------
+ unsupported Unicode escape sequence
+(1 row)
+
index b2b36774823c8bd94e0e8a48f2bf011e284fbac5..be85676b5b93024b0e9f562e9ef0677f63af0d83 100644 (file)
@@ -310,6 +310,31 @@ LINE 1: SELECT '{
 DETAIL:  Expected JSON value, but found "}".
 CONTEXT:  JSON data, line 4: ...yveryveryveryveryveryveryveryverylongfieldname":}
 -- ERROR missing value for last field
+-- test non-error-throwing input
+select pg_input_is_valid('{"a":true}', 'jsonb');
+ pg_input_is_valid 
+-------------------
+ t
+(1 row)
+
+select pg_input_is_valid('{"a":true', 'jsonb');
+ pg_input_is_valid 
+-------------------
+ f
+(1 row)
+
+select pg_input_error_message('{"a":true', 'jsonb');
+       pg_input_error_message       
+------------------------------------
+ invalid input syntax for type json
+(1 row)
+
+select pg_input_error_message('{"a":1e1000000}', 'jsonb');
+     pg_input_error_message     
+--------------------------------
+ value overflows numeric format
+(1 row)
+
 -- make sure jsonb is passed through json generators without being escaped
 SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']);
       array_to_json       
index 589e0cea367b835da5f8beba99233541a32c86f1..21534ed959c9822fd94e9f5b28f5f66e67f37b97 100644 (file)
@@ -81,6 +81,11 @@ SELECT '{
                "averyveryveryveryveryveryveryveryveryverylongfieldname":}'::json;
 -- ERROR missing value for last field
 
+-- test non-error-throwing input
+select pg_input_is_valid('{"a":true}', 'json');
+select pg_input_is_valid('{"a":true', 'json');
+select pg_input_error_message('{"a":true', 'json');
+
 --constructors
 -- array_to_json
 
index d7fac69733de470f9abe8e9e8745dffd246b3dd0..f87b3bd4f31dadaece58ccc5b8dc242e08d0abd5 100644 (file)
@@ -76,3 +76,7 @@ SELECT jsonb '{ "a":  "dollar \u0024 character" }' ->> 'a' as correct_everywhere
 SELECT jsonb '{ "a":  "dollar \\u0024 character" }' ->> 'a' as not_an_escape;
 SELECT jsonb '{ "a":  "null \u0000 escape" }' ->> 'a' as fails;
 SELECT jsonb '{ "a":  "null \\u0000 escape" }' ->> 'a' as not_an_escape;
+
+-- soft error for input-time failure
+
+select pg_input_error_message('{ "a":  "\ud83d\ude04\ud83d\udc36" }', 'jsonb');
index 8d2596626770a003ad8ae6f61aa0bcf27b776dfa..bc44ad1518f633308fd9d04759e94bd55066e7a4 100644 (file)
@@ -86,6 +86,12 @@ SELECT '{
                "averyveryveryveryveryveryveryveryveryverylongfieldname":}'::jsonb;
 -- ERROR missing value for last field
 
+-- test non-error-throwing input
+select pg_input_is_valid('{"a":true}', 'jsonb');
+select pg_input_is_valid('{"a":true', 'jsonb');
+select pg_input_error_message('{"a":true', 'jsonb');
+select pg_input_error_message('{"a":1e1000000}', 'jsonb');
+
 -- make sure jsonb is passed through json generators without being escaped
 SELECT array_to_json(ARRAY [jsonb '{"a":1}', jsonb '{"b":[2,3]}']);