aboutsummaryrefslogtreecommitdiff
path: root/src/json.c
diff options
context:
space:
mode:
authordrh <>2022-04-04 15:15:45 +0000
committerdrh <>2022-04-04 15:15:45 +0000
commit0de10ac11cb9f5e0e1bed4c56c20b118a20dae3a (patch)
tree654b475da3b38b7a69622cc60440f1e5ed15b9b5 /src/json.c
parentb07fb4f1c27609c40b8d1d417f9c8db3e5f7f402 (diff)
downloadsqlite-0de10ac11cb9f5e0e1bed4c56c20b118a20dae3a.tar.gz
sqlite-0de10ac11cb9f5e0e1bed4c56c20b118a20dae3a.zip
When constructing the JSON Path for the "fullpath" column of the
json_tree() and json_each() table-valued functions, be sure to quote object labels where necessary. FossilOrigin-Name: 0fbbe7881cadf0b3c211653c7a0797e0a90c7c24da78ecc8a27140c05f89f2ed
Diffstat (limited to 'src/json.c')
-rw-r--r--src/json.c35
1 files changed, 29 insertions, 6 deletions
diff --git a/src/json.c b/src/json.c
index 81111fff1..3636d3665 100644
--- a/src/json.c
+++ b/src/json.c
@@ -2275,6 +2275,33 @@ static int jsonEachNext(sqlite3_vtab_cursor *cur){
return SQLITE_OK;
}
+/* Append an object label to the JSON Path being constructed
+** in pStr.
+*/
+static void jsonAppendObjectPathElement(
+ JsonString *pStr,
+ JsonNode *pNode
+){
+ int jj, nn;
+ const char *z;
+ assert( pNode->eType==JSON_STRING );
+ assert( pNode->jnFlags & JNODE_LABEL );
+ assert( pNode->eU==1 );
+ z = pNode->u.zJContent;
+ nn = pNode->n;
+ assert( nn>=2 );
+ assert( z[0]=='"' );
+ assert( z[nn-1]=='"' );
+ if( nn>2 && sqlite3Isalpha(z[1]) ){
+ for(jj=2; jj<nn-1 && sqlite3Isalnum(z[jj]); jj++){}
+ if( jj==nn-1 ){
+ z++;
+ nn -= 2;
+ }
+ }
+ jsonPrintf(nn+2, pStr, ".%.*s", nn, z);
+}
+
/* Append the name of the path for element i to pStr
*/
static void jsonEachComputePath(
@@ -2299,10 +2326,7 @@ static void jsonEachComputePath(
}else{
assert( pUp->eType==JSON_OBJECT );
if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
- assert( pNode->eType==JSON_STRING );
- assert( pNode->jnFlags & JNODE_LABEL );
- assert( pNode->eU==1 );
- jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
+ jsonAppendObjectPathElement(pStr, pNode);
}
}
@@ -2373,8 +2397,7 @@ static int jsonEachColumn(
if( p->eType==JSON_ARRAY ){
jsonPrintf(30, &x, "[%d]", p->iRowid);
}else if( p->eType==JSON_OBJECT ){
- assert( pThis->eU==1 );
- jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
+ jsonAppendObjectPathElement(&x, pThis);
}
}
jsonResult(&x);