From 821a4c9f8c681faaa45c131ae2c9be455dd3097e Mon Sep 17 00:00:00 2001 From: drh <> Date: Mon, 27 Nov 2023 15:57:11 +0000 Subject: [PATCH] Give the json_valid() function an optional second argument that determines what is meant by "valid". FossilOrigin-Name: a4e19ad43dac81e7655ec03ff69bb99d1d02b0c227034c90fb41415fd4793fe3 --- manifest | 15 +++---- manifest.uuid | 2 +- src/json.c | 111 +++++++++++++++++++++++++++++++++++++--------- test/json101.test | 2 +- 4 files changed, 98 insertions(+), 32 deletions(-) diff --git a/manifest b/manifest index 17d296c97f..188b5f5277 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Convert\sthe\sjson_tree()\sand\sjson_each()\svirtual\stables\sover\sto\suse\sJSONB\shas\ntheir\sinternal\srepresentation. -D 2023-11-27T12:36:29.017 +C Give\sthe\sjson_valid()\sfunction\san\soptional\ssecond\sargument\sthat\sdetermines\nwhat\sis\smeant\sby\s"valid". +D 2023-11-27T15:57:11.976 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -688,7 +688,7 @@ F src/hash.h 3340ab6e1d13e725571d7cee6d3e3135f0779a7d8e76a9ce0a85971fa3953c51 F src/hwtime.h f9c2dfb84dce7acf95ce6d289e46f5f9d3d1afd328e53da8f8e9008e3b3caae6 F src/in-operator.md 10cd8f4bcd225a32518407c2fb2484089112fd71 F src/insert.c 3f0a94082d978bbdd33c38fefea15346c6c6bffb70bc645a71dc0f1f87dd3276 -F src/json.c efd9ec1cb0f0cbee3e43ebaba69469b3d3f44ee623b53b1ce72361da42e05fbe +F src/json.c 6c1de0d7802a15669a78008125b409b67aa23eefcacfc52436d713419d09be9e F src/legacy.c d7874bc885906868cd51e6c2156698f2754f02d9eee1bae2d687323c3ca8e5aa F src/loadext.c 7432c944ff197046d67a1207790a1b13eec4548c85a9457eb0896bb3641dfb36 F src/main.c 1b89f3de98d1b59fec5bac1d66d6ece21f703821b8eaa0d53d9604c35309f6f9 @@ -1325,7 +1325,7 @@ F test/json/json-generator.tcl dc0dd0f393800c98658fc4c47eaa6af29d4e17527380cd286 F test/json/json-q1-b.txt 606818a5fba6d9e418c9f4ea7d8418af026775042dad81439b72447a147a462c F test/json/json-q1.txt 65f9d1cdcc4cffa9823fb73ed936aae5658700cd001fde448f68bfb91c807307 F test/json/json-speed-check.sh b060a9a6c696c0a807d8929400fa11bd7113edc58b0d66b9795f424f8d0db326 x -F test/json101.test 19f9abc77c33151bd7fdb7850fb473cb0154f229b863b740590ad9de807a032d +F test/json101.test 643936557daeb2eeeb839e9d87a682c7d7c6f6aee66045eedd8613cb8471d8d8 F test/json102.test 557a46e16df1aa9bdbc4076a71a45814ea0e7503d6621d87d42a8c04cbc2b0ef F test/json103.test 53df87f83a4e5fa0c0a56eb29ff6c94055c6eb919f33316d62161a8880112dbe F test/json104.test 1b844a70cddcfa2e4cd81a5db0657b2e61e7f00868310f24f56a9ba0114348c1 @@ -2145,9 +2145,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P ab2644aacf4757a51cf62e05cff6711a0a3605d60502a3dd310887df1b993545 b5a5660ca22437640c9bf32c44d92c76a7293dafcbaf4fa6a4c171128d64871d -R 6545b38a1b858ef8dae597ee252577f2 -T +closed b5a5660ca22437640c9bf32c44d92c76a7293dafcbaf4fa6a4c171128d64871d +P ec18caa3f7790b780dde66c1ccbb6eb09d2f1507629cc45955fc1b08380b4017 +R ae458da46f597856d9232f7ad8297973 U drh -Z 9c0c7326e55f087530f0f53e8712d89c +Z 6c8b34340e171672e2ebcad369756a23 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 189e2ccc7a..c418cd9780 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -ec18caa3f7790b780dde66c1ccbb6eb09d2f1507629cc45955fc1b08380b4017 \ No newline at end of file +a4e19ad43dac81e7655ec03ff69bb99d1d02b0c227034c90fb41415fd4793fe3 \ No newline at end of file diff --git a/src/json.c b/src/json.c index 0e3f6fe575..1107a24f4c 100644 --- a/src/json.c +++ b/src/json.c @@ -5241,15 +5241,60 @@ static void jsonTypeFunc( /* ** json_valid(JSON) +** json_valid(JSON, FLAGS) ** -** Return 1 if the argument is one of: +** Check the JSON argument to see if it is well-formed. The FLAGS argument +** encodes the various constraints on what is meant by "well-formed": ** -** * A well-formed canonical JSON string according to RFC-8259 -** (without JSON5 enhancements), or +** 0x01 Canonical RFC-8259 JSON text +** 0x02 JSON text with optional JSON-5 extensions +** 0x04 Superficially appears to be JSONB +** 0x08 Strictly well-formed JSONB ** -** * A BLOB that plausibly could be a JSONB value. +** If the FLAGS argument is omitted, it defaults to 1. Useful values for +** FLAGS include: ** -** Return 0 otherwise. +** 1 Strict canonical JSON text +** 2 JSON text perhaps with JSON-5 extensions +** 4 Superficially appears to be JSONB +** 5 Canonical JSON text or superficial JSONB +** 6 JSON-5 text or superficial JSONB +** 8 Strict JSONB +** 9 Canonical JSON text or strict JSONB +** 10 JSON-5 text or strict JSONB +** +** Other flag combinations are redundant. For example, every canonical +** JSON text is also well-formed JSON-5 text, so FLAG values 2 and 3 +** are the same. Similarly, any input that passes a strict JSONB validation +** will also pass the superficial validation so 12 through 15 are the same +** as 8 through 11 respectively. +** +** This routine runs in linear time to validate text and when doing strict +** JSONB validation. Superficial JSONB validation is constant time, +** assuming the BLOB is already in memory. The performance advantage +** of superficial JSONB validation is why that option is provided. +** Application developers can choose to do fast superficial validation or +** slower strict validation, according to their specific needs. +** +** Only the lower four bits of the FLAGS argument are currently used. +** Higher bits are reserved for future expansion. To facilitate +** compatibility, the current implementation raises an error if any bit +** in FLAGS is set other than the lower four bits. +** +** The original circa 2015 implementation of the JSON routines in +** SQLite only supported canonical RFC-8259 JSON text and the json_valid() +** function only accepted one argument. That is why the default value +** for the FLAGS argument is 1, since FLAGS=1 causes this routine to only +** recognize canonical RFC-8259 JSON text as valid. The extra FLAGS +** argument was added when the JSON routines were extended to support +** JSON5-like extensions and binary JSONB stored in BLOBs. +** +** Return Values: +** +** * Raise an error if FLAGS is outside the range of 1 to 15. +** * Return NULL if the input is NULL +** * Return 1 if the input is well-formed. +** * Return 0 if the input is not well-formed. */ static void jsonValidFunc( sqlite3_context *ctx, @@ -5257,26 +5302,47 @@ static void jsonValidFunc( sqlite3_value **argv ){ JsonParse *p; /* The parse */ - UNUSED_PARAMETER(argc); - if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ + u8 flags = 1; + u8 res = 0; + if( argc==2 ){ + i64 f = sqlite3_value_int64(argv[1]); + if( f<1 || f>15 ){ + sqlite3_result_error(ctx, "FLAGS parameter to json_valid() must be" + " between 1 and 15", -1); + return; + } + flags = f & 0x0f; + } + switch( sqlite3_value_type(argv[0]) ){ + case SQLITE_NULL: { #ifdef SQLITE_LEGACY_JSON_VALID - /* Incorrect legacy behavior was to return FALSE for a NULL input */ - sqlite3_result_int(ctx, 0); + /* Incorrect legacy behavior was to return FALSE for a NULL input */ + sqlite3_result_int(ctx, 0); #endif - return; - } - if( jsonFuncArgMightBeBinary(argv[0]) ){ - sqlite3_result_int(ctx, 1); - return; - } - p = jsonParseCached(ctx, argv[0], 0, 0); - if( p==0 || p->oom ){ - sqlite3_result_error_nomem(ctx); - sqlite3_free(p); - }else{ - sqlite3_result_int(ctx, p->nErr==0 && (p->hasNonstd==0 || p->useMod)); - if( p->nErr ) jsonParseFree(p); + return; + } + case SQLITE_BLOB: { + if( (flags & 0x0c)!=0 && jsonFuncArgMightBeBinary(argv[0]) ){ + /* TO-DO: strict checking if flags & 0x08 */ + res = 1; + } + break; + } + default: { + if( (flags & 0x3)==0 ) break; + p = jsonParseCached(ctx, argv[0], 0, 0); + if( p==0 || p->oom ){ + sqlite3_result_error_nomem(ctx); + sqlite3_free(p); + }else if( p->nErr ){ + jsonParseFree(p); + }else if( (flags & 0x02)!=0 || p->hasNonstd==0 || p->useMod ){ + res = 1; + } + break; + } } + sqlite3_result_int(ctx, res); } /* @@ -6171,6 +6237,7 @@ void sqlite3RegisterJsonFunctions(void){ JFUNCTION(json_type, 1,1,0, 0,0,0, jsonTypeFunc), JFUNCTION(json_type, 2,1,0, 0,0,0, jsonTypeFunc), JFUNCTION(json_valid, 1,1,0, 0,0,0, jsonValidFunc), + JFUNCTION(json_valid, 2,1,0, 0,0,0, jsonValidFunc), #if SQLITE_DEBUG JFUNCTION(json_parse, 1,1,0, 0,0,0, jsonParseFunc), JFUNCTION(json_test1, 1,1,0, 1,0,0, jsonTest1Func), diff --git a/test/json101.test b/test/json101.test index 280d467393..ebaf0f3b34 100644 --- a/test/json101.test +++ b/test/json101.test @@ -324,7 +324,7 @@ do_execsql_test json101-5.2 { SELECT id, json_valid(json), json_type(json), '|' FROM j2 ORDER BY id; } {1 1 object | 2 1 object | 3 1 array |} do_execsql_test json101-5.2b { - SELECT id, json_valid(json), json_type(json), '|' FROM j2b ORDER BY id; + SELECT id, json_valid(json,5), json_type(json), '|' FROM j2b ORDER BY id; } {1 1 object | 2 1 object | 3 1 array |} ifcapable !vtab { -- 2.47.2