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
+23
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 },