]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Add the json_ntype() SQL function. Works like the 1-argument json_type()
authordrh <>
Fri, 7 Jan 2022 16:03:00 +0000 (16:03 +0000)
committerdrh <>
Fri, 7 Jan 2022 16:03:00 +0000 (16:03 +0000)
except that it returns NULL if the argument is not well-formed JSON, rather
than raising an error.

FossilOrigin-Name: ed9956f5ddca68141eded81d5362847db603257329801622c4eb3b0732112f14

ext/misc/json1.c
manifest
manifest.uuid
test/json102.test

index 6f66d4e70cf9efcb5bc4b5e5a7e160ee20513b02..926f0a8b6832b1bf28277bca4b579cc883c38ed8 100644 (file)
@@ -1946,10 +1946,13 @@ jsonSetDone:
 
 /*
 ** json_type(JSON)
+** json_ntype(JSON)
 ** json_type(JSON, PATH)
 **
-** Return the top-level "type" of a JSON string.  Throw an error if
-** either the JSON or PATH inputs are not well-formed.
+** Return the top-level "type" of a JSON string.  json_type() raises an
+** error if either the JSON or PATH inputs are not well-formed.  json_ntype()
+** works like the one-argument version of json_type() except that it
+** returns NULL if the JSON argument is not well-formed.
 */
 static void jsonTypeFunc(
   sqlite3_context *ctx,
@@ -1960,7 +1963,7 @@ static void jsonTypeFunc(
   const char *zPath;
   JsonNode *pNode;
 
-  p = jsonParseCached(ctx, argv, ctx);
+  p = jsonParseCached(ctx, argv, *(int*)sqlite3_user_data(ctx) ? 0 : ctx);
   if( p==0 ) return;
   if( argc==2 ){
     zPath = (const char*)sqlite3_value_text(argv[1]);
@@ -2680,6 +2683,7 @@ int sqlite3Json1Init(sqlite3 *db){
     { "->",                   2, 1,   jsonExtractFunc       },
     { "->>",                  2, 1,   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         },
index 3fa4543e069c30e8fbec2cc736645c2f69d90ed0..7dff65acd8214b5dacdf838c5893d10de464b37e 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Accept\sabbreviated\sJSON\sPaths\son\sthe\sright-hand\sside\sof\sthe\s->\sand\s->>\soperators.
-D 2022-01-07T15:47:12.461
+C Add\sthe\sjson_ntype()\sSQL\sfunction.\s\sWorks\slike\sthe\s1-argument\sjson_type()\nexcept\sthat\sit\sreturns\sNULL\sif\sthe\sargument\sis\snot\swell-formed\sJSON,\srather\nthan\sraising\san\serror.
+D 2022-01-07T16:03:00.989
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -306,7 +306,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 81a11151fbeefd8870c31a9e77576daeacc9ef1c66153950ea2eb084193235c6
+F ext/misc/json1.c 5416dd330d6d4cfade184dd3d123ab720ca99ac72b21bd419b76cdbe69ac68d9
 F ext/misc/memstat.c 3017a0832c645c0f8c773435620d663855f04690172316bd127270d1a7523d4d
 F ext/misc/memtrace.c 7c0d115d2ef716ad0ba632c91e05bd119cb16c1aedf3bec9f06196ead2d5537b
 F ext/misc/memvfs.c 7dffa8cc89c7f2d73da4bd4ccea1bcbd2bd283e3bb4cea398df7c372a197291b
@@ -1147,7 +1147,7 @@ F test/jrnlmode.test 9b5bc01dac22223cb60ec2d5f97acf568d73820794386de5634dcadbea9
 F test/jrnlmode2.test 8759a1d4657c064637f8b079592651530db738419e1d649c6df7048cd724363d
 F test/jrnlmode3.test 556b447a05be0e0963f4311e95ab1632b11c9eaa
 F test/json101.test bb71538005f2d9e18620bdd3b76839a93ca0be61903eb8d751a64e78cf99b8fb
-F test/json102.test 8ee925a282f03f47526ca4e260b8e4af9f4e5b562f38647aa4f32dc84c19a0a3
+F test/json102.test c0c45152f516036dd444b8650dd55a06c455792e35ab1c3180537b77d00f7f10
 F test/json103.test aff6b7a4c17d5a20b487a7bc1a274bfdc63b829413bdfb83bedac42ec7f67e3b
 F test/json104.test 2cb7ff2cca2c8214d3e5260eeb9ce45faec0926f68b3e40c1aaa6ca247284144
 F test/json105.test 45f7d6a9a54c85f8a9589b68d3e7a1f42d02f2359911a8cdbad1f9988f571173
@@ -1937,8 +1937,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 c4e4e3a3fc5da0381ccb7930706e57d7831d87f9c63bafe49ae64117701e1cfe
-R dd2f603ec9049bd111aea3757f3d8f8e
+P d15410900dccbb7159683c29f640fa321b1e019718827334f5fefe89da623008
+R 33189226ed8931e2d3369f37d0903d14
 U drh
-Z 9891da1f488bb126e807d6977835eae4
+Z aa887589cd33ea857ca5f9e682543a02
 # Remove this line to create a well-formed Fossil manifest.
index 6b7aeda36ed6ebbbff5ad82010e6adb4d5630b36..8efe23711e63be8d97aa7f832017d0d8a5707e17 100644 (file)
@@ -1 +1 @@
-d15410900dccbb7159683c29f640fa321b1e019718827334f5fefe89da623008
\ No newline at end of file
+ed9956f5ddca68141eded81d5362847db603257329801622c4eb3b0732112f14
\ No newline at end of file
index 6afe2307b364cea57b13b0090a8674ffd8147ad2..ce1da6993ae0741e4adfb1d5f74d6432ff0b3681 100644 (file)
@@ -176,6 +176,9 @@ do_execsql_test json102-500 {
 do_execsql_test json102-510 {
   SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}');
 } {{object}}
+do_execsql_test json102-511 {
+  SELECT json_ntype('{"a":[2,3.5,true,false,null,"x"]}');
+} {{object}}
 do_execsql_test json102-520 {
   SELECT json_type('{"a":[2,3.5,true,false,null,"x"]}','$');
 } {{object}}
@@ -209,6 +212,12 @@ do_execsql_test json102-610 {
 do_execsql_test json102-620 {
   SELECT json_valid(char(123)||'"x":35');
 } {{0}}
+do_catchsql_test json102-630 {
+  SELECT json_type('["a",');
+} {1 {malformed JSON}}
+do_catchsql_test json102-631 {
+  SELECT json_ntype('["a",');
+} {0 {{}}}
 
 ifcapable vtab {
 do_execsql_test json102-1000 {