Add the json_check() function, which returns its argument if the argument

is well-formed JSON or which throws an error otherwise.

FossilOrigin-Name: 64abb65d4df11e5b3bcc4afc8e7c18e907c6080a
This commit is contained in:
drh 2015-08-28 03:48:04 +00:00
parent ecb5fedb3f
commit f6ec8d4f4c
4 changed files with 38 additions and 8 deletions

View File

@ -1049,6 +1049,28 @@ static void jsonArrayLengthFunc(
if( !x.oom ) sqlite3_result_int64(ctx, n);
}
/*
** json_check(JSON)
**
** Check the JSON argument to verify that it is well-formed. Return a
** compacted version of the argument (with white-space removed) if the
** argument is well-formed. Through an error if the argument is not
** correctly formatted JSON.
*/
static void jsonCheckFunc(
sqlite3_context *ctx,
int argc,
sqlite3_value **argv
){
JsonParse x; /* The parse */
if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ){
sqlite3_result_error(ctx, "malformed JSON", -1);
return;
}
jsonReturn(x.aNode, ctx, argv);
jsonParseReset(&x);
}
/*
** json_extract(JSON, PATH)
**
@ -1780,6 +1802,7 @@ int sqlite3_json_init(
{ "json_array", -1, 0, jsonArrayFunc },
{ "json_array_length", 1, 0, jsonArrayLengthFunc },
{ "json_array_length", 2, 0, jsonArrayLengthFunc },
{ "json_check", 1, 0, jsonCheckFunc },
{ "json_extract", 2, 0, jsonExtractFunc },
{ "json_insert", -1, 0, jsonSetFunc },
{ "json_object", -1, 0, jsonObjectFunc },

View File

@ -1,5 +1,5 @@
C Enhance\sthe\sjson_insert(),\sjson_replace(),\sand\sjson_set()\sfunctions\swith\sthe\nability\sto\sadd\sJSON\sinstead\sof\stext\sif\sthe\sargument\sis\stext\sand\sif\sthe\sPATH\nbegins\swith\s'$$'\sinstead\sof\sjust\s'$'.
D 2015-08-28T03:33:50.087
C Add\sthe\sjson_check()\sfunction,\swhich\sreturns\sits\sargument\sif\sthe\sargument\nis\swell-formed\sJSON\sor\swhich\sthrows\san\serror\sotherwise.
D 2015-08-28T03:48:04.807
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in e2218eb228374422969de7b1680eda6864affcef
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@ -192,7 +192,7 @@ F ext/misc/eval.c f971962e92ebb8b0a4e6b62949463ee454d88fa2
F ext/misc/fileio.c d4171c815d6543a9edef8308aab2951413cd8d0f
F ext/misc/fuzzer.c 4c84635c71c26cfa7c2e5848cf49fe2d2cfcd767
F ext/misc/ieee754.c b0362167289170627659e84173f5d2e8fee8566e
F ext/misc/json1.c e0aeaa8b2f374d06ca19081afbd70b271731b130
F ext/misc/json1.c 0b2bf3f2c3786fd6873b5f9a89023912c28ac889
F ext/misc/nextchar.c 35c8b8baacb96d92abbb34a83a997b797075b342
F ext/misc/percentile.c bcbee3c061b884eccb80e21651daaae8e1e43c63
F ext/misc/regexp.c af92cdaa5058fcec1451e49becc7ba44dba023dc
@ -810,7 +810,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 5dfb181790c123123c8e7981d4d3c941b6cc8af4
F test/json101.test 420fe3917c9a6a0fb7f967f1f641054509acd0e2
F test/keyword1.test 37ef6bba5d2ed5b07ecdd6810571de2956599dff
F test/lastinsert.test 42e948fd6442f07d60acbd15d33fb86473e0ef63
F test/laststmtchanges.test ae613f53819206b3222771828d024154d51db200
@ -1380,7 +1380,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 66f92a16866e5825363636b9cc4b8f9b29d9e84d
R 8075e39a7f0130f58ac8ab5ea1825d8f
P 44f103d8862abc2d5613bac04dc2ea8e625b1f40
R 204205d2daf18b22608467b1b10a8a77
U drh
Z 9db1096145751bb3222f3dab3f132c33
Z 4f534e8322ceee4fbcec765d09d17add

View File

@ -1 +1 @@
44f103d8862abc2d5613bac04dc2ea8e625b1f40
64abb65d4df11e5b3bcc4afc8e7c18e907c6080a

View File

@ -63,4 +63,11 @@ do_execsql_test json1-3.4 {
SELECT json_type(json_set('{"a":1,"b":2}','$$.b','{"x":3,"y":4}'),'$.b');
} {object}
do_execsql_test json1-4.1 {
SELECT json_check(' [ 1, 2] ');
} {[1,2]}
do_catchsql_test json1-4.2 {
SELECT json_check(' [ 1, 2 ');
} {1 {malformed JSON}}
finish_test