aboutsummaryrefslogtreecommitdiff
path: root/ext/misc/json1.c
diff options
context:
space:
mode:
authordrh <>2022-01-07 17:08:48 +0000
committerdrh <>2022-01-07 17:08:48 +0000
commit338b1fde623427ee8bbdc7921cff8d8714518fda (patch)
tree2ab5525acdfa58bc7560f8291eb36e139ef76cd2 /ext/misc/json1.c
parenta4e4e18494f745b50f69e87de1275bbed5dd75e4 (diff)
downloadsqlite-338b1fde623427ee8bbdc7921cff8d8714518fda.tar.gz
sqlite-338b1fde623427ee8bbdc7921cff8d8714518fda.zip
New json_nextract() function that works like json_extract() except that it
returns NULL instead of raising an error if the first argument is not well-formed JSON. Or if the first argument is not well-formed JSON and the second argument is '$', then return the first argument quoted. The "->" and "->>" operators are converted to use json_nextract(). FossilOrigin-Name: dc00f5286d26524b149de071490320afaa203fec5777b3eb813f07963614861a
Diffstat (limited to 'ext/misc/json1.c')
-rw-r--r--ext/misc/json1.c25
1 files changed, 18 insertions, 7 deletions
diff --git a/ext/misc/json1.c b/ext/misc/json1.c
index 926f0a8b6..2cb9d700b 100644
--- a/ext/misc/json1.c
+++ b/ext/misc/json1.c
@@ -1597,17 +1597,27 @@ static void jsonExtractFunc(
JsonParse *p; /* The parse */
JsonNode *pNode;
const char *zPath;
+ int flags = *(int*)sqlite3_user_data(ctx);
JsonString jx;
if( argc<2 ) return;
- p = jsonParseCached(ctx, argv, ctx);
- if( p==0 ) return;
+ p = jsonParseCached(ctx, argv, (flags & 1)!=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
+ && argc==2
+ && (zPath = (const char*)sqlite3_value_text(argv[1]))!=0
+ && zPath[0]=='$' && zPath[1]==0
+ ){
+ jsonQuoteFunc(ctx, argc, argv);
+ }
+ return;
+ }
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
- && *(int*)sqlite3_user_data(ctx)
- ){
+ if( zPath && zPath[0]!='$' && zPath[0]!=0 && (flags & 2)!=0 ){
/* The -> and ->> operators accept abbreviated PATH arguments:
** NUMBER ==> $[NUMBER]
** LABEL ==> $.LABEL
@@ -2680,8 +2690,9 @@ int sqlite3Json1Init(sqlite3 *db){
{ "json_array_length", 1, 0, jsonArrayLengthFunc },
{ "json_array_length", 2, 0, jsonArrayLengthFunc },
{ "json_extract", -1, 0, jsonExtractFunc },
- { "->", 2, 1, jsonExtractFunc },
- { "->>", 2, 1, jsonExtractFunc },
+ { "json_nextract", -1, 1, jsonExtractFunc },
+ { "->", 2, 3, jsonExtractFunc },
+ { "->>", 2, 3, jsonExtractFunc },
{ "json_insert", -1, 0, jsonSetFunc },
{ "json_ntype", 1, 1, jsonTypeFunc },
{ "json_object", -1, 0, jsonObjectFunc },