aboutsummaryrefslogtreecommitdiff
path: root/src/json.c
diff options
context:
space:
mode:
authordrh <>2023-12-06 15:35:38 +0000
committerdrh <>2023-12-06 15:35:38 +0000
commit6a8581d828e2c6b940cdf19868c309dde4322ea9 (patch)
tree45e0a641af1e7702af7a47b1b3b335b9d5ef7e1a /src/json.c
parent91ec00c25acd25bd9a6bc2345436370d23723653 (diff)
downloadsqlite-6a8581d828e2c6b940cdf19868c309dde4322ea9.tar.gz
sqlite-6a8581d828e2c6b940cdf19868c309dde4322ea9.zip
The rule for the RHS of the ->> and -> operators when the RHS does not begin
with $ is that it must be (1) all digits, or (2) all alphanumerics, or (3) contained within &#91;..&#93; or else it will become a quoted label. FossilOrigin-Name: 0e059a546ec11fa5c6d007bd65c249ee2422f1facbdb2792c53e0bc0ccc97e14
Diffstat (limited to 'src/json.c')
-rw-r--r--src/json.c22
1 files changed, 20 insertions, 2 deletions
diff --git a/src/json.c b/src/json.c
index 37a0ebfe1..44abe2d16 100644
--- a/src/json.c
+++ b/src/json.c
@@ -3272,6 +3272,20 @@ static void jsonArrayLengthFunc(
jsonParseFree(p);
}
+/* True if the string is all digits */
+static int jsonAllDigits(const char *z, int n){
+ int i;
+ for(i=0; i<n && sqlite3Isdigit(z[i]); i++){}
+ return i==n;
+}
+
+/* True if the string is all alphanumerics and underscores */
+static int jsonAllAlphanum(const char *z, int n){
+ int i;
+ for(i=0; i<n && (sqlite3Isalnum(z[i]) || z[i]=='_'); i++){}
+ return i==n;
+}
+
/*
** json_extract(JSON, PATH, ...)
** "->"(JSON,PATH)
@@ -3329,15 +3343,19 @@ static void jsonExtractFunc(
** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience
*/
jsonStringInit(&jx, ctx);
- if( sqlite3Isdigit(zPath[0]) ){
+ if( jsonAllDigits(zPath, nPath) ){
jsonAppendRawNZ(&jx, "[", 1);
jsonAppendRaw(&jx, zPath, nPath);
jsonAppendRawNZ(&jx, "]", 2);
- }else if( zPath[0]!='[' ){
+ }else if( jsonAllAlphanum(zPath, nPath) ){
jsonAppendRawNZ(&jx, ".", 1);
jsonAppendRaw(&jx, zPath, nPath);
+ }else if( zPath[0]=='[' && nPath>=3 && zPath[nPath-1]==']' ){
+ jsonAppendRaw(&jx, zPath, nPath);
}else{
+ jsonAppendRawNZ(&jx, ".\"", 2);
jsonAppendRaw(&jx, zPath, nPath);
+ jsonAppendRawNZ(&jx, "\"", 1);
}
jsonStringTerminate(&jx);
j = jsonLookupStep(p, 0, jx.zBuf, 0);