From: drh Date: Fri, 17 Jun 2016 13:01:51 +0000 (+0000) Subject: Add the json_quote() function to the JSON1 extension. X-Git-Tag: version-3.14.0~54^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fheads%2Fjson_quote;p=thirdparty%2Fsqlite.git Add the json_quote() function to the JSON1 extension. FossilOrigin-Name: 2c3714aebf5e40e3728877a235b3c1f93defa33e --- diff --git a/ext/misc/json1.c b/ext/misc/json1.c index 99d299c29c..a9452b0191 100644 --- a/ext/misc/json1.c +++ b/ext/misc/json1.c @@ -1211,6 +1211,25 @@ static void jsonTest1Func( ** Scalar SQL function implementations ****************************************************************************/ +/* +** Implementation of the json_QUOTE(VALUE) function. Return a JSON value +** corresponding to the SQL value input. Mostly this means putting +** double-quotes around strings and returning the unquoted string "null" +** when given a NULL input. +*/ +static void jsonQuoteFunc( + sqlite3_context *ctx, + int argc, + sqlite3_value **argv +){ + JsonString jx; + + jsonInit(&jx, ctx); + jsonAppendValue(&jx, argv[0]); + jsonResult(&jx); + sqlite3_result_subtype(ctx, JSON_SUBTYPE); +} + /* ** Implementation of the json_array(VALUE,...) function. Return a JSON ** array that contains all values given in arguments. Or if any argument @@ -2124,6 +2143,7 @@ int sqlite3Json1Init(sqlite3 *db){ { "json_extract", -1, 0, jsonExtractFunc }, { "json_insert", -1, 0, jsonSetFunc }, { "json_object", -1, 0, jsonObjectFunc }, + { "json_quote", 1, 0, jsonQuoteFunc }, { "json_remove", -1, 0, jsonRemoveFunc }, { "json_replace", -1, 0, jsonReplaceFunc }, { "json_set", -1, 1, jsonSetFunc }, diff --git a/manifest b/manifest index b6450cc841..cfd0f94004 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\sa\smissing\sOP_ColumnsUsed\sopcode\sto\scode\sfor\sexpressions\slike\s"?\sIN\s(SELECT\s...)"\sin\scases\swhere\sexpression\scan\suse\san\sindex\sthat\smay\scontain\sNULL\svalues. -D 2016-06-16T17:14:02.375 +C Add\sthe\sjson_quote()\sfunction\sto\sthe\sJSON1\sextension. +D 2016-06-17T13:01:51.782 F Makefile.in f3f7d2060ce03af4584e711ef3a626ef0b1d6340 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 50149765ef72f4e652b9a0f1f6462c4784bb9423 @@ -211,7 +211,7 @@ F ext/misc/eval.c f971962e92ebb8b0a4e6b62949463ee454d88fa2 F ext/misc/fileio.c d4171c815d6543a9edef8308aab2951413cd8d0f F ext/misc/fuzzer.c 7c64b8197bb77b7d64eff7cac7848870235d4c25 F ext/misc/ieee754.c f190d0cc5182529acb15babd177781be1ac1718c -F ext/misc/json1.c b9c88d5c3b6ecd8c731ffdd7f5b3d902857f8c96 +F ext/misc/json1.c d51a764ba43a49e191bc3536238bfab3def258ca F ext/misc/nextchar.c 35c8b8baacb96d92abbb34a83a997b797075b342 F ext/misc/percentile.c bcbee3c061b884eccb80e21651daaae8e1e43c63 F ext/misc/regexp.c a68d25c659bd2d893cd1215667bbf75ecb9dc7d4 @@ -874,7 +874,7 @@ F test/journal3.test ff8af941f9e06161d3db1b46bb9f965ff0e7f307 F test/jrnlmode.test 7864d59cf7f6e552b9b99ba0f38acd167edc10fa F test/jrnlmode2.test 81610545a4e6ed239ea8fa661891893385e23a1d F test/jrnlmode3.test 556b447a05be0e0963f4311e95ab1632b11c9eaa -F test/json101.test ef42283f0b60d8bacbc2243448e7c84988578e52 +F test/json101.test 865776ed8580703e1684fe4b8ee2e473333bb121 F test/json102.test bf3fe7a706d30936a76a0f7a0375e1e8e73aff5a F test/json103.test c5f6b85e69de05f6b3195f9f9d5ce9cd179099a0 F test/keyword1.test 37ef6bba5d2ed5b07ecdd6810571de2956599dff @@ -1501,7 +1501,10 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 48b555c42de1cbc031fb6c2c93ef170e491c7d76 -R 7c056b92500e212b6fa7144618e1e6e0 -U dan -Z 6cf13efbb72ea4474e244da36e8952f1 +P 0b1579caf06a2c42433b8bc9dc28c9ad381aa07c +R d87f9b42ae99ac8ed32dd707e5951481 +T *branch * json_quote +T *sym-json_quote * +T -sym-trunk * +U drh +Z f05f5d0cd323376080196dc4fc41462d diff --git a/manifest.uuid b/manifest.uuid index 6347d15d5f..8de647c48f 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -0b1579caf06a2c42433b8bc9dc28c9ad381aa07c \ No newline at end of file +2c3714aebf5e40e3728877a235b3c1f93defa33e \ No newline at end of file diff --git a/test/json101.test b/test/json101.test index 9b780a379b..3ee007c1cb 100644 --- a/test/json101.test +++ b/test/json101.test @@ -356,5 +356,34 @@ do_execsql_test json-8.2 { SELECT a=json_extract(b,'$[0]') FROM t8; } {1} +# The json_quote() function transforms an SQL value into a JSON value. +# String values are quoted and interior quotes are escaped. NULL values +# are rendered as the unquoted string "null". +# +do_execsql_test json-9.1 { + SELECT json_quote('abc"xyz'); +} {{"abc\"xyz"}} +do_execsql_test json-9.2 { + SELECT json_quote(3.14159); +} {3.14159} +do_execsql_test json-9.3 { + SELECT json_quote(12345); +} {12345} +do_execsql_test json-9.4 { + SELECT json_quote(null); +} {"null"} +do_catchsql_test json-9.5 { + SELECT json_quote(x'30313233'); +} {1 {JSON cannot hold BLOB values}} +do_catchsql_test json-9.6 { + SELECT json_quote(123,456) +} {1 {wrong number of arguments to function json_quote()}} +do_catchsql_test json-9.7 { + SELECT json_quote() +} {1 {wrong number of arguments to function json_quote()}} + + + + finish_test