diff --git a/ext/misc/json1.c b/ext/misc/json1.c index 86a486c21c..26f08e9168 100644 --- a/ext/misc/json1.c +++ b/ext/misc/json1.c @@ -1581,13 +1581,44 @@ static void jsonArrayLengthFunc( sqlite3_result_int64(ctx, n); } +/* +** Bit values for the flags passed into jsonExtractFunc() or +** jsonSetFunc() via the user-data value. +*/ +#define JSON_NULLERR 0x01 /* Return NULL if input is not JSON */ +#define JSON_ABPATH 0x02 /* Allow abbreviated JSON path specs */ +#define JSON_ISSET 0x04 /* json_set(), not json_insert() */ + /* ** json_extract(JSON, PATH, ...) +** json_nextract(JSON, PATH, ...) +** "->"(JSON,PATH) +** "->>"(JSON,PATH) ** -** Return the element described by PATH. Return NULL if there is no -** PATH element. If there are multiple PATHs, then return a JSON array -** with the result from each path. Throw an error if the JSON or any PATH -** is malformed. +** Return the element described by PATH. Return NULL if that PATH element +** is not found. For leaf nodes of the JSON, the value returned is a pure +** SQL value. In other words, quotes have been removed from strings. +** +** If there are multiple PATHs, then the value returned is a JSON array +** with one entry in the array for each PATH term. +** +** Throw an error if any PATH is malformed. +** +** If JSON is not well-formed JSON then: +** +** (1) raise an error if the JSON_NULLERR flag is not set. +** +** (2) Otherwise (if the JSON_NULLERR flags is set and) if there +** is a single PATH argument with the value '$', simply quote +** the JSON input as if by json_quote(). In other words, treat +** the JSON input as a string and convert it into a valid JSON +** string. +** +** (3) Otherwise (if JSON_NULLERR is set and the PATH is not '$') +** return NULL +** +** If the JSON_ABPATH flag is set and there is only a single PATH, then +** allow abbreviated PATH specs that omit the leading "$". */ static void jsonExtractFunc( sqlite3_context *ctx, @@ -1601,11 +1632,11 @@ static void jsonExtractFunc( JsonString jx; if( argc<2 ) return; - p = jsonParseCached(ctx, argv, (flags & 1)!=0 ? 0 : ctx); + p = jsonParseCached(ctx, argv, (flags & JSON_NULLERR)!=0 ? 0 : ctx); if( p==0 ){ /* If the form is "json_nextract(IN,'$')" and IN is not well-formed JSON, ** then return IN as a quoted JSON string. */ - if( (flags & 1)!=0 + if( (flags & JSON_NULLERR)!=0 && argc==2 && (zPath = (const char*)sqlite3_value_text(argv[1]))!=0 && zPath[0]=='$' && zPath[1]==0 @@ -1617,11 +1648,13 @@ static void jsonExtractFunc( if( argc==2 ){ /* With a single PATH argument, the return is the unquoted SQL value */ zPath = (const char*)sqlite3_value_text(argv[1]); - if( zPath && zPath[0]!='$' && zPath[0]!=0 && (flags & 2)!=0 ){ - /* The -> and ->> operators accept abbreviated PATH arguments: - ** NUMBER ==> $[NUMBER] - ** LABEL ==> $.LABEL - ** [NUMBER] ==> $[NUMBER] + if( zPath && zPath[0]!='$' && zPath[0]!=0 && (flags & JSON_ABPATH)!=0 ){ + /* The -> and ->> operators accept abbreviated PATH arguments. This + ** is mostly for compatibility with PostgreSQL, but also for convenience. + ** + ** NUMBER ==> $[NUMBER] // PG compatible + ** LABEL ==> $.LABEL // PG compatible + ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience */ jsonInit(&jx, ctx); if( safe_isdigit(zPath[0]) ){ @@ -1633,7 +1666,7 @@ static void jsonExtractFunc( jsonAppendRaw(&jx, zPath, (int)strlen(zPath)); jsonAppendChar(&jx, 0); } - pNode = jsonLookup(p, jx.zBuf, 0, ctx); + pNode = jx.bErr ? 0 : jsonLookup(p, jx.zBuf, 0, ctx); jsonReset(&jx); }else{ pNode = jsonLookup(p, zPath, 0, ctx); @@ -1896,6 +1929,7 @@ replace_err: jsonParseReset(&x); } + /* ** json_set(JSON, PATH, VALUE, ...) ** @@ -2681,34 +2715,34 @@ int sqlite3Json1Init(sqlite3 *db){ unsigned int i; static const struct { const char *zName; + void (*xFunc)(sqlite3_context*,int,sqlite3_value**); int nArg; int flag; - void (*xFunc)(sqlite3_context*,int,sqlite3_value**); } aFunc[] = { - { "json", 1, 0, jsonRemoveFunc }, - { "json_array", -1, 0, jsonArrayFunc }, - { "json_array_length", 1, 0, jsonArrayLengthFunc }, - { "json_array_length", 2, 0, jsonArrayLengthFunc }, - { "json_extract", -1, 0, jsonExtractFunc }, - { "json_nextract", -1, 1, jsonExtractFunc }, - { "->", 2, 3, jsonExtractFunc }, - { "->>", 2, 2, jsonExtractFunc }, - { "json_insert", -1, 0, jsonSetFunc }, - { "json_ntype", 1, 1, jsonTypeFunc }, - { "json_object", -1, 0, jsonObjectFunc }, - { "json_patch", 2, 0, jsonPatchFunc }, - { "json_quote", 1, 0, jsonQuoteFunc }, - { "json_remove", -1, 0, jsonRemoveFunc }, - { "json_replace", -1, 0, jsonReplaceFunc }, - { "json_set", -1, 1, jsonSetFunc }, - { "json_type", 1, 0, jsonTypeFunc }, - { "json_type", 2, 0, jsonTypeFunc }, - { "json_valid", 1, 0, jsonValidFunc }, + { "json", jsonRemoveFunc, 1, 0 }, + { "json_array", jsonArrayFunc, -1, 0 }, + { "json_array_length", jsonArrayLengthFunc, 1, 0 }, + { "json_array_length", jsonArrayLengthFunc, 2, 0 }, + { "json_extract", jsonExtractFunc, -1, 0 }, + { "json_nextract", jsonExtractFunc, -1, JSON_NULLERR }, + { "->", jsonExtractFunc, 2, JSON_NULLERR|JSON_ABPATH }, + { "->>", jsonExtractFunc, 2, JSON_ABPATH }, + { "json_insert", jsonSetFunc, -1, 0 }, + { "json_object", jsonObjectFunc, -1, 0 }, + { "json_patch", jsonPatchFunc, 2, 0 }, + { "json_quote", jsonQuoteFunc, 1, 0 }, + { "json_remove", jsonRemoveFunc, -1, 0 }, + { "json_replace", jsonReplaceFunc, -1, 0 }, + { "json_set", jsonSetFunc, -1, JSON_ISSET }, + { "json_type", jsonTypeFunc, 1, 0 }, + { "json_ntype", jsonTypeFunc, 1, JSON_NULLERR }, + { "json_type", jsonTypeFunc, 2, 0 }, + { "json_valid", jsonValidFunc, 1, 0 }, #if SQLITE_DEBUG /* DEBUG and TESTING functions */ - { "json_parse", 1, 0, jsonParseFunc }, - { "json_test1", 1, 0, jsonTest1Func }, + { "json_parse", jsonParseFunc, 1, 0 }, + { "json_test1", jsonTest1Func, 1, 0 }, #endif }; static const struct { diff --git a/manifest b/manifest index 0c197632b6..38810b62bc 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Notes\son\sthe\sJSON\senhancement\sproposals. -D 2022-01-07T18:09:56.414 +C Improved\scommenting\sof\schanges\sin\sthe\sjson1.c\sextension. +D 2022-01-08T15:05:53.556 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -307,7 +307,7 @@ F ext/misc/fileio.c 4e7f7cd30de8df4820c552f14af3c9ca451c5ffe1f2e7bef34d598a12ebf F ext/misc/fossildelta.c 1240b2d3e52eab1d50c160c7fe1902a9bd210e052dc209200a750bbf885402d5 F ext/misc/fuzzer.c eae560134f66333e9e1ca4c8ffea75df42056e2ce8456734565dbe1c2a92bf3d F ext/misc/ieee754.c 91a5594071143a4ab79c638fe9f059af1db09932faf2e704c3e29216a7d4f511 -F ext/misc/json1.c bfee5581c6f8443d2b4d73939c1aaad887111b7bb5f3a8c5dbd1572f627de83c +F ext/misc/json1.c a35ebe08f25515e4ab03c4ac7e06dd8a837c461f96df040ef7566a6ee4edbb84 F ext/misc/memstat.c 3017a0832c645c0f8c773435620d663855f04690172316bd127270d1a7523d4d F ext/misc/memtrace.c 7c0d115d2ef716ad0ba632c91e05bd119cb16c1aedf3bec9f06196ead2d5537b F ext/misc/memvfs.c 7dffa8cc89c7f2d73da4bd4ccea1bcbd2bd283e3bb4cea398df7c372a197291b @@ -1938,8 +1938,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 85f8170555ee0d4d28cb7e120a7062e9f64c331a936fdfa29fc0e67224eea7c6 -R f0bbc4fbd1752207d287e4842cece932 +P 18160985ea6b2bbf27de25e0f4f3a1ebcdb079a36af039fc06e37a834e49e772 +R ea25b8b6843a230f65190e794ee89370 U drh -Z d04dbd17b8376af71e0674b428869308 +Z 72ca0ba8118c37c05f41d020549c1f4e # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 3de7da8f44..dc1aa1ea0e 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -18160985ea6b2bbf27de25e0f4f3a1ebcdb079a36af039fc06e37a834e49e772 \ No newline at end of file +4d81425e1bf2cff6fa961d0a7936b5f62d3f8ffe9bffea89c1e8b8ddf8fad6f4 \ No newline at end of file