drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2015-08-12 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
| 13 | ** This SQLite extension implements JSON functions. The interface is |
| 14 | ** modeled after MySQL JSON functions: |
| 15 | ** |
| 16 | ** https://dev.mysql.com/doc/refman/5.7/en/json.html |
| 17 | ** |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 18 | ** For the time being, all JSON is stored as pure text. (We might add |
| 19 | ** a JSONB type in the future which stores a binary encoding of JSON in |
| 20 | ** a BLOB, but there is no support for JSONB in the current implementation.) |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 21 | */ |
| 22 | #include "sqlite3ext.h" |
| 23 | SQLITE_EXTENSION_INIT1 |
| 24 | #include <assert.h> |
| 25 | #include <string.h> |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 26 | #include <ctype.h> |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 27 | #include <stdlib.h> |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 28 | |
| 29 | /* Unsigned integer types */ |
| 30 | typedef sqlite3_uint64 u64; |
| 31 | typedef unsigned int u32; |
| 32 | typedef unsigned char u8; |
| 33 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 34 | /* An instance of this object represents a JSON string |
| 35 | ** under construction. Really, this is a generic string accumulator |
| 36 | ** that can be and is used to create strings other than JSON. |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 37 | */ |
| 38 | typedef struct Json Json; |
| 39 | struct Json { |
| 40 | sqlite3_context *pCtx; /* Function context - put error messages here */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 41 | char *zBuf; /* Append JSON content here */ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 42 | u64 nAlloc; /* Bytes of storage available in zBuf[] */ |
| 43 | u64 nUsed; /* Bytes of zBuf[] currently used */ |
| 44 | u8 bStatic; /* True if zBuf is static space */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 45 | u8 oom; /* True if an OOM has been encountered */ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 46 | char zSpace[100]; /* Initial static space */ |
| 47 | }; |
| 48 | |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 49 | /* JSON type values |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 50 | */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 51 | #define JSON_NULL 0 |
| 52 | #define JSON_TRUE 1 |
| 53 | #define JSON_FALSE 2 |
| 54 | #define JSON_INT 3 |
| 55 | #define JSON_REAL 4 |
| 56 | #define JSON_STRING 5 |
| 57 | #define JSON_ARRAY 6 |
| 58 | #define JSON_OBJECT 7 |
| 59 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 60 | /* |
| 61 | ** Names of the various JSON types: |
| 62 | */ |
| 63 | static const char * const jsonType[] = { |
| 64 | "null", "true", "false", "integer", "real", "text", "array", "object" |
| 65 | }; |
| 66 | |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 67 | /* Bit values for the JsonNode.jnFlag field |
| 68 | */ |
| 69 | #define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */ |
| 70 | #define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */ |
| 71 | #define JNODE_REMOVE 0x04 /* Do not output */ |
| 72 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 73 | |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 74 | /* A single node of parsed JSON |
| 75 | */ |
| 76 | typedef struct JsonNode JsonNode; |
| 77 | struct JsonNode { |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 78 | u8 eType; /* One of the JSON_ type values */ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 79 | u8 jnFlags; /* JNODE flags */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 80 | u32 n; /* Bytes of content, or number of sub-nodes */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 81 | const char *zJContent; /* JSON content */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | /* A completely parsed JSON string |
| 85 | */ |
| 86 | typedef struct JsonParse JsonParse; |
| 87 | struct JsonParse { |
| 88 | u32 nNode; /* Number of slots of aNode[] used */ |
| 89 | u32 nAlloc; /* Number of slots of aNode[] allocated */ |
| 90 | JsonNode *aNode; /* Array of nodes containing the parse */ |
| 91 | const char *zJson; /* Original JSON string */ |
| 92 | u8 oom; /* Set to true if out of memory */ |
| 93 | }; |
| 94 | |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 95 | /* |
| 96 | ** Return the number of consecutive JsonNode slots need to represent |
| 97 | ** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and |
| 98 | ** OBJECT types, the number might be larger. |
| 99 | */ |
| 100 | static u32 jsonSizeof(JsonNode *pNode){ |
| 101 | return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1; |
| 102 | } |
| 103 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 104 | /* Set the Json object to an empty string |
| 105 | */ |
| 106 | static void jsonZero(Json *p){ |
| 107 | p->zBuf = p->zSpace; |
| 108 | p->nAlloc = sizeof(p->zSpace); |
| 109 | p->nUsed = 0; |
| 110 | p->bStatic = 1; |
| 111 | } |
| 112 | |
| 113 | /* Initialize the Json object |
| 114 | */ |
| 115 | static void jsonInit(Json *p, sqlite3_context *pCtx){ |
| 116 | p->pCtx = pCtx; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 117 | p->oom = 0; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 118 | jsonZero(p); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | /* Free all allocated memory and reset the Json object back to its |
| 123 | ** initial state. |
| 124 | */ |
| 125 | static void jsonReset(Json *p){ |
| 126 | if( !p->bStatic ) sqlite3_free(p->zBuf); |
| 127 | jsonZero(p); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /* Report an out-of-memory (OOM) condition |
| 132 | */ |
| 133 | static void jsonOom(Json *p){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 134 | p->oom = 1; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 135 | sqlite3_result_error_nomem(p->pCtx); |
| 136 | jsonReset(p); |
| 137 | } |
| 138 | |
| 139 | /* Enlarge pJson->zBuf so that it can hold at least N more bytes. |
| 140 | ** Return zero on success. Return non-zero on an OOM error |
| 141 | */ |
| 142 | static int jsonGrow(Json *p, u32 N){ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 143 | u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 144 | char *zNew; |
| 145 | if( p->bStatic ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 146 | if( p->oom ) return SQLITE_NOMEM; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 147 | zNew = sqlite3_malloc64(nTotal); |
| 148 | if( zNew==0 ){ |
| 149 | jsonOom(p); |
| 150 | return SQLITE_NOMEM; |
| 151 | } |
| 152 | memcpy(zNew, p->zBuf, p->nUsed); |
| 153 | p->zBuf = zNew; |
| 154 | p->bStatic = 0; |
| 155 | }else{ |
| 156 | zNew = sqlite3_realloc64(p->zBuf, nTotal); |
| 157 | if( zNew==0 ){ |
| 158 | jsonOom(p); |
| 159 | return SQLITE_NOMEM; |
| 160 | } |
| 161 | p->zBuf = zNew; |
| 162 | } |
| 163 | p->nAlloc = nTotal; |
| 164 | return SQLITE_OK; |
| 165 | } |
| 166 | |
| 167 | /* Append N bytes from zIn onto the end of the Json string. |
| 168 | */ |
| 169 | static void jsonAppendRaw(Json *p, const char *zIn, u32 N){ |
| 170 | if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return; |
| 171 | memcpy(p->zBuf+p->nUsed, zIn, N); |
| 172 | p->nUsed += N; |
| 173 | } |
| 174 | |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 175 | /* Append the zero-terminated string zIn |
| 176 | */ |
| 177 | static void jsonAppend(Json *p, const char *zIn){ |
| 178 | jsonAppendRaw(p, zIn, (u32)strlen(zIn)); |
| 179 | } |
| 180 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 181 | /* Append a single character |
| 182 | */ |
| 183 | static void jsonAppendChar(Json *p, char c){ |
| 184 | if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return; |
| 185 | p->zBuf[p->nUsed++] = c; |
| 186 | } |
| 187 | |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 188 | /* Append a comma separator to the output buffer, if the previous |
| 189 | ** character is not '[' or '{'. |
| 190 | */ |
| 191 | static void jsonAppendSeparator(Json *p){ |
| 192 | char c; |
| 193 | if( p->nUsed==0 ) return; |
| 194 | c = p->zBuf[p->nUsed-1]; |
| 195 | if( c!='[' && c!='{' ) jsonAppendChar(p, ','); |
| 196 | } |
| 197 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 198 | /* Append the N-byte string in zIn to the end of the Json string |
| 199 | ** under construction. Enclose the string in "..." and escape |
| 200 | ** any double-quotes or backslash characters contained within the |
| 201 | ** string. |
| 202 | */ |
| 203 | static void jsonAppendString(Json *p, const char *zIn, u32 N){ |
| 204 | u32 i; |
| 205 | if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return; |
| 206 | p->zBuf[p->nUsed++] = '"'; |
| 207 | for(i=0; i<N; i++){ |
| 208 | char c = zIn[i]; |
| 209 | if( c=='"' || c=='\\' ){ |
| 210 | if( (p->nUsed+N+1-i > p->nAlloc) && jsonGrow(p,N+1-i)!=0 ) return; |
| 211 | p->zBuf[p->nUsed++] = '\\'; |
| 212 | } |
| 213 | p->zBuf[p->nUsed++] = c; |
| 214 | } |
| 215 | p->zBuf[p->nUsed++] = '"'; |
| 216 | } |
| 217 | |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 218 | /* Make the JSON in p the result of the SQL function. |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 219 | */ |
| 220 | static void jsonResult(Json *p){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 221 | if( p->oom==0 ){ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 222 | sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed, |
| 223 | p->bStatic ? SQLITE_TRANSIENT : sqlite3_free, |
| 224 | SQLITE_UTF8); |
| 225 | jsonZero(p); |
| 226 | } |
| 227 | assert( p->bStatic ); |
| 228 | } |
| 229 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 230 | /* |
| 231 | ** Convert the JsonNode pNode into a pure JSON string and |
| 232 | ** append to pOut. Subsubstructure is also included. Return |
| 233 | ** the number of JsonNode objects that are encoded. |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 234 | */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 235 | static int jsonRenderNode(JsonNode *pNode, Json *pOut){ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 236 | u32 j = 1; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 237 | switch( pNode->eType ){ |
| 238 | case JSON_NULL: { |
| 239 | jsonAppendRaw(pOut, "null", 4); |
| 240 | break; |
| 241 | } |
| 242 | case JSON_TRUE: { |
| 243 | jsonAppendRaw(pOut, "true", 4); |
| 244 | break; |
| 245 | } |
| 246 | case JSON_FALSE: { |
| 247 | jsonAppendRaw(pOut, "false", 5); |
| 248 | break; |
| 249 | } |
| 250 | case JSON_STRING: { |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 251 | if( pNode->jnFlags & JNODE_RAW ){ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 252 | jsonAppendString(pOut, pNode->zJContent, pNode->n); |
| 253 | break; |
| 254 | } |
| 255 | /* Fall through into the next case */ |
| 256 | } |
| 257 | case JSON_REAL: |
| 258 | case JSON_INT: { |
| 259 | jsonAppendRaw(pOut, pNode->zJContent, pNode->n); |
| 260 | break; |
| 261 | } |
| 262 | case JSON_ARRAY: { |
| 263 | jsonAppendChar(pOut, '['); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 264 | while( j<=pNode->n ){ |
| 265 | if( pNode[j].jnFlags & JNODE_REMOVE ){ |
| 266 | j += jsonSizeof(&pNode[j]); |
| 267 | }else{ |
| 268 | jsonAppendSeparator(pOut); |
| 269 | j += jsonRenderNode(&pNode[j], pOut); |
| 270 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 271 | } |
| 272 | jsonAppendChar(pOut, ']'); |
| 273 | break; |
| 274 | } |
| 275 | case JSON_OBJECT: { |
| 276 | jsonAppendChar(pOut, '{'); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 277 | while( j<=pNode->n ){ |
| 278 | if( pNode[j+1].jnFlags & JNODE_REMOVE ){ |
| 279 | j += 1 + jsonSizeof(&pNode[j+1]); |
| 280 | }else{ |
| 281 | jsonAppendSeparator(pOut); |
| 282 | jsonRenderNode(&pNode[j], pOut); |
| 283 | jsonAppendChar(pOut, ':'); |
| 284 | j += 1 + jsonRenderNode(&pNode[j+1], pOut); |
| 285 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 286 | } |
| 287 | jsonAppendChar(pOut, '}'); |
| 288 | break; |
| 289 | } |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 290 | } |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 291 | return j; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /* |
| 295 | ** Make the JsonNode the return value of the function. |
| 296 | */ |
| 297 | static void jsonReturn(JsonNode *pNode, sqlite3_context *pCtx){ |
| 298 | switch( pNode->eType ){ |
| 299 | case JSON_NULL: { |
| 300 | sqlite3_result_null(pCtx); |
| 301 | break; |
| 302 | } |
| 303 | case JSON_TRUE: { |
| 304 | sqlite3_result_int(pCtx, 1); |
| 305 | break; |
| 306 | } |
| 307 | case JSON_FALSE: { |
| 308 | sqlite3_result_int(pCtx, 0); |
| 309 | break; |
| 310 | } |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 311 | case JSON_REAL: { |
| 312 | double r = strtod(pNode->zJContent, 0); |
| 313 | sqlite3_result_double(pCtx, r); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 314 | break; |
| 315 | } |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 316 | case JSON_INT: { |
| 317 | sqlite3_int64 i = 0; |
| 318 | const char *z = pNode->zJContent; |
| 319 | if( z[0]=='-' ){ z++; } |
| 320 | while( z[0]>='0' && z[0]<='9' ){ i = i*10 + *(z++) - '0'; } |
| 321 | if( pNode->zJContent[0]=='-' ){ i = -i; } |
| 322 | sqlite3_result_int64(pCtx, i); |
| 323 | break; |
| 324 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 325 | case JSON_STRING: { |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 326 | if( pNode->jnFlags & JNODE_RAW ){ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 327 | sqlite3_result_text(pCtx, pNode->zJContent, pNode->n, SQLITE_TRANSIENT); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 328 | }else if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 329 | /* JSON formatted without any backslash-escapes */ |
| 330 | sqlite3_result_text(pCtx, pNode->zJContent+1, pNode->n-2, |
| 331 | SQLITE_TRANSIENT); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 332 | }else{ |
| 333 | /* Translate JSON formatted string into raw text */ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 334 | u32 i; |
| 335 | u32 n = pNode->n; |
| 336 | const char *z = pNode->zJContent; |
| 337 | char *zOut; |
| 338 | u32 j; |
| 339 | zOut = sqlite3_malloc( n+1 ); |
| 340 | if( zOut==0 ){ |
| 341 | sqlite3_result_error_nomem(pCtx); |
| 342 | break; |
| 343 | } |
| 344 | for(i=1, j=0; i<n-1; i++){ |
| 345 | char c = z[i]; |
| 346 | if( c!='\\' && z[i+1] ){ |
| 347 | zOut[j++] = c; |
| 348 | }else{ |
| 349 | c = z[++i]; |
| 350 | if( c=='u' && z[1] ){ |
| 351 | u32 v = 0, k; |
| 352 | z++; |
| 353 | for(k=0; k<4 && z[k]; k++){ |
| 354 | c = z[0]; |
| 355 | if( c>='0' && c<='9' ) v = v*16 + c - '0'; |
| 356 | else if( c>='A' && c<='F' ) v = v*16 + c - 'A' + 10; |
| 357 | else if( c>='a' && c<='f' ) v = v*16 + c - 'a' + 10; |
| 358 | else break; |
| 359 | z++; |
| 360 | } |
| 361 | if( v<=0x7f ){ |
| 362 | zOut[j++] = v; |
| 363 | }else if( v<=0x7ff ){ |
| 364 | zOut[j++] = 0xc0 | (v>>6); |
| 365 | zOut[j++] = 0x80 | (v&0x3f); |
| 366 | }else if( v<=0xffff ){ |
| 367 | zOut[j++] = 0xe0 | (v>>12); |
| 368 | zOut[j++] = 0x80 | ((v>>6)&0x3f); |
| 369 | zOut[j++] = 0x80 | (v&0x3f); |
| 370 | }else if( v<=0x10ffff ){ |
| 371 | zOut[j++] = 0xf0 | (v>>18); |
| 372 | zOut[j++] = 0x80 | ((v>>12)&0x3f); |
| 373 | zOut[j++] = 0x80 | ((v>>6)&0x3f); |
| 374 | zOut[j++] = 0x80 | (v&0x3f); |
| 375 | } |
| 376 | }else{ |
| 377 | if( c=='b' ){ |
| 378 | c = '\b'; |
| 379 | }else if( c=='f' ){ |
| 380 | c = '\f'; |
| 381 | }else if( c=='n' ){ |
| 382 | c = '\n'; |
| 383 | }else if( c=='r' ){ |
| 384 | c = '\r'; |
| 385 | }else if( c=='t' ){ |
| 386 | c = '\t'; |
| 387 | } |
| 388 | zOut[j++] = c; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | zOut[j] = 0; |
| 393 | sqlite3_result_text(pCtx, zOut, j, sqlite3_free); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 394 | } |
| 395 | break; |
| 396 | } |
| 397 | case JSON_ARRAY: |
| 398 | case JSON_OBJECT: { |
| 399 | Json s; |
| 400 | jsonInit(&s, pCtx); |
| 401 | jsonRenderNode(pNode, &s); |
| 402 | jsonResult(&s); |
| 403 | break; |
| 404 | } |
| 405 | } |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 406 | } |
| 407 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 408 | /* |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 409 | ** Create a new JsonNode instance based on the arguments and append that |
| 410 | ** instance to the JsonParse. Return the index in pParse->aNode[] of the |
| 411 | ** new node, or -1 if a memory allocation fails. |
| 412 | */ |
| 413 | static int jsonParseAddNode( |
| 414 | JsonParse *pParse, /* Append the node to this object */ |
| 415 | u32 eType, /* Node type */ |
| 416 | u32 n, /* Content size or sub-node count */ |
| 417 | const char *zContent /* Content */ |
| 418 | ){ |
| 419 | JsonNode *p; |
| 420 | if( pParse->nNode>=pParse->nAlloc ){ |
| 421 | u32 nNew; |
| 422 | JsonNode *pNew; |
| 423 | if( pParse->oom ) return -1; |
| 424 | nNew = pParse->nAlloc*2 + 10; |
| 425 | if( nNew<=pParse->nNode ){ |
| 426 | pParse->oom = 1; |
| 427 | return -1; |
| 428 | } |
| 429 | pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew); |
| 430 | if( pNew==0 ){ |
| 431 | pParse->oom = 1; |
| 432 | return -1; |
| 433 | } |
| 434 | pParse->nAlloc = nNew; |
| 435 | pParse->aNode = pNew; |
| 436 | } |
| 437 | p = &pParse->aNode[pParse->nNode]; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 438 | p->eType = (u8)eType; |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 439 | p->jnFlags = 0; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 440 | p->n = n; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 441 | p->zJContent = zContent; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 442 | return pParse->nNode++; |
| 443 | } |
| 444 | |
| 445 | /* |
| 446 | ** Parse a single JSON value which begins at pParse->zJson[i]. Return the |
| 447 | ** index of the first character past the end of the value parsed. |
| 448 | ** |
| 449 | ** Return negative for a syntax error. Special cases: return -2 if the |
| 450 | ** first non-whitespace character is '}' and return -3 if the first |
| 451 | ** non-whitespace character is ']'. |
| 452 | */ |
| 453 | static int jsonParseValue(JsonParse *pParse, u32 i){ |
| 454 | char c; |
| 455 | u32 j; |
| 456 | u32 iThis; |
| 457 | int x; |
| 458 | while( isspace(pParse->zJson[i]) ){ i++; } |
| 459 | if( (c = pParse->zJson[i])==0 ) return 0; |
| 460 | if( c=='{' ){ |
| 461 | /* Parse object */ |
| 462 | iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0); |
| 463 | if( iThis<0 ) return -1; |
| 464 | for(j=i+1;;j++){ |
| 465 | while( isspace(pParse->zJson[j]) ){ j++; } |
| 466 | x = jsonParseValue(pParse, j); |
| 467 | if( x<0 ){ |
| 468 | if( x==(-2) && pParse->nNode==iThis+1 ) return j+1; |
| 469 | return -1; |
| 470 | } |
| 471 | if( pParse->aNode[pParse->nNode-1].eType!=JSON_STRING ) return -1; |
| 472 | j = x; |
| 473 | while( isspace(pParse->zJson[j]) ){ j++; } |
| 474 | if( pParse->zJson[j]!=':' ) return -1; |
| 475 | j++; |
| 476 | x = jsonParseValue(pParse, j); |
| 477 | if( x<0 ) return -1; |
| 478 | j = x; |
| 479 | while( isspace(pParse->zJson[j]) ){ j++; } |
| 480 | c = pParse->zJson[j]; |
| 481 | if( c==',' ) continue; |
| 482 | if( c!='}' ) return -1; |
| 483 | break; |
| 484 | } |
| 485 | pParse->aNode[iThis].n = pParse->nNode - iThis - 1; |
| 486 | return j+1; |
| 487 | }else if( c=='[' ){ |
| 488 | /* Parse array */ |
| 489 | iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0); |
| 490 | if( iThis<0 ) return -1; |
| 491 | for(j=i+1;;j++){ |
| 492 | while( isspace(pParse->zJson[j]) ){ j++; } |
| 493 | x = jsonParseValue(pParse, j); |
| 494 | if( x<0 ){ |
| 495 | if( x==(-3) && pParse->nNode==iThis+1 ) return j+1; |
| 496 | return -1; |
| 497 | } |
| 498 | j = x; |
| 499 | while( isspace(pParse->zJson[j]) ){ j++; } |
| 500 | c = pParse->zJson[j]; |
| 501 | if( c==',' ) continue; |
| 502 | if( c!=']' ) return -1; |
| 503 | break; |
| 504 | } |
| 505 | pParse->aNode[iThis].n = pParse->nNode - iThis - 1; |
| 506 | return j+1; |
| 507 | }else if( c=='"' ){ |
| 508 | /* Parse string */ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 509 | u8 jnFlags = 0; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 510 | j = i+1; |
| 511 | for(;;){ |
| 512 | c = pParse->zJson[j]; |
| 513 | if( c==0 ) return -1; |
| 514 | if( c=='\\' ){ |
| 515 | c = pParse->zJson[++j]; |
| 516 | if( c==0 ) return -1; |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 517 | jnFlags = JNODE_ESCAPE; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 518 | }else if( c=='"' ){ |
| 519 | break; |
| 520 | } |
| 521 | j++; |
| 522 | } |
| 523 | jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 524 | pParse->aNode[pParse->nNode-1].jnFlags = jnFlags; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 525 | return j+1; |
| 526 | }else if( c=='n' |
| 527 | && strncmp(pParse->zJson+i,"null",4)==0 |
drh | b2cd10e | 2015-08-15 21:29:14 +0000 | [diff] [blame] | 528 | && !isalnum(pParse->zJson[i+4]) ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 529 | jsonParseAddNode(pParse, JSON_NULL, 0, 0); |
| 530 | return i+4; |
| 531 | }else if( c=='t' |
| 532 | && strncmp(pParse->zJson+i,"true",4)==0 |
drh | b2cd10e | 2015-08-15 21:29:14 +0000 | [diff] [blame] | 533 | && !isalnum(pParse->zJson[i+4]) ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 534 | jsonParseAddNode(pParse, JSON_TRUE, 0, 0); |
| 535 | return i+4; |
| 536 | }else if( c=='f' |
| 537 | && strncmp(pParse->zJson+i,"false",5)==0 |
drh | b2cd10e | 2015-08-15 21:29:14 +0000 | [diff] [blame] | 538 | && !isalnum(pParse->zJson[i+5]) ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 539 | jsonParseAddNode(pParse, JSON_FALSE, 0, 0); |
| 540 | return i+5; |
| 541 | }else if( c=='-' || (c>='0' && c<='9') ){ |
| 542 | /* Parse number */ |
| 543 | u8 seenDP = 0; |
| 544 | u8 seenE = 0; |
| 545 | j = i+1; |
| 546 | for(;; j++){ |
| 547 | c = pParse->zJson[j]; |
| 548 | if( c>='0' && c<='9' ) continue; |
| 549 | if( c=='.' ){ |
| 550 | if( pParse->zJson[j-1]=='-' ) return -1; |
| 551 | if( seenDP ) return -1; |
| 552 | seenDP = 1; |
| 553 | continue; |
| 554 | } |
| 555 | if( c=='e' || c=='E' ){ |
| 556 | if( pParse->zJson[j-1]<'0' ) return -1; |
| 557 | if( seenE ) return -1; |
| 558 | seenDP = seenE = 1; |
| 559 | c = pParse->zJson[j+1]; |
| 560 | if( c=='+' || c=='-' ) j++; |
| 561 | continue; |
| 562 | } |
| 563 | break; |
| 564 | } |
| 565 | if( pParse->zJson[j-1]<'0' ) return -1; |
| 566 | jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT, |
| 567 | j - i, &pParse->zJson[i]); |
| 568 | return j; |
| 569 | }else if( c=='}' ){ |
| 570 | return -2; /* End of {...} */ |
| 571 | }else if( c==']' ){ |
| 572 | return -3; /* End of [...] */ |
| 573 | }else{ |
| 574 | return -1; /* Syntax error */ |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | /* |
| 579 | ** Parse a complete JSON string. Return 0 on success or non-zero if there |
| 580 | ** are any errors. If an error occurs, free all memory associated with |
| 581 | ** pParse. |
| 582 | ** |
| 583 | ** pParse is uninitialized when this routine is called. |
| 584 | */ |
| 585 | static int jsonParse(JsonParse *pParse, const char *zJson){ |
| 586 | int i; |
| 587 | if( zJson==0 ) return 1; |
| 588 | memset(pParse, 0, sizeof(*pParse)); |
| 589 | pParse->zJson = zJson; |
| 590 | i = jsonParseValue(pParse, 0); |
| 591 | if( i>0 ){ |
| 592 | while( isspace(zJson[i]) ) i++; |
| 593 | if( zJson[i] ) i = -1; |
| 594 | } |
| 595 | if( i<0 ){ |
| 596 | sqlite3_free(pParse->aNode); |
| 597 | pParse->aNode = 0; |
| 598 | pParse->nNode = 0; |
| 599 | pParse->nAlloc = 0; |
| 600 | return 1; |
| 601 | } |
| 602 | return 0; |
| 603 | } |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 604 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 605 | /* |
| 606 | ** Search along zPath to find the node specified. Return a pointer |
| 607 | ** to that node, or NULL if zPath is malformed or if there is no such |
| 608 | ** node. |
| 609 | */ |
| 610 | static JsonNode *jsonLookup(JsonNode *pRoot, const char *zPath){ |
| 611 | u32 i, j; |
| 612 | if( zPath[0]==0 ) return pRoot; |
| 613 | if( zPath[0]=='.' ){ |
| 614 | if( pRoot->eType!=JSON_OBJECT ) return 0; |
| 615 | zPath++; |
| 616 | for(i=0; isalnum(zPath[i]); i++){} |
| 617 | if( i==0 ) return 0; |
| 618 | j = 1; |
| 619 | while( j<=pRoot->n ){ |
| 620 | if( pRoot[j].n==i+2 |
| 621 | && strncmp(&pRoot[j].zJContent[1],zPath,i)==0 |
| 622 | ){ |
| 623 | return jsonLookup(&pRoot[j+1], &zPath[i]); |
| 624 | } |
| 625 | j++; |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 626 | j += jsonSizeof(&pRoot[j]); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 627 | } |
| 628 | }else if( zPath[0]=='[' && isdigit(zPath[1]) ){ |
| 629 | if( pRoot->eType!=JSON_ARRAY ) return 0; |
| 630 | i = 0; |
| 631 | zPath++; |
| 632 | while( isdigit(zPath[0]) ){ |
| 633 | i = i + zPath[0] - '0'; |
| 634 | zPath++; |
| 635 | } |
| 636 | if( zPath[0]!=']' ) return 0; |
| 637 | zPath++; |
| 638 | j = 1; |
| 639 | while( i>0 && j<=pRoot->n ){ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 640 | j += jsonSizeof(&pRoot[j]); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 641 | i--; |
| 642 | } |
| 643 | if( j<=pRoot->n ){ |
| 644 | return jsonLookup(&pRoot[j], zPath); |
| 645 | } |
| 646 | } |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | /**************************************************************************** |
| 651 | ** SQL functions used for testing and debugging |
| 652 | ****************************************************************************/ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 653 | |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 654 | #ifdef SQLITE_DEBUG |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 655 | /* |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 656 | ** The json_parse(JSON) function returns a string which describes |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 657 | ** a parse of the JSON provided. Or it returns NULL if JSON is not |
| 658 | ** well-formed. |
| 659 | */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 660 | static void jsonParseFunc( |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 661 | sqlite3_context *context, |
| 662 | int argc, |
| 663 | sqlite3_value **argv |
| 664 | ){ |
| 665 | Json s; /* Output string - not real JSON */ |
| 666 | JsonParse x; /* The parse */ |
| 667 | u32 i; |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 668 | char zBuf[100]; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 669 | |
| 670 | assert( argc==1 ); |
| 671 | if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return; |
| 672 | jsonInit(&s, context); |
| 673 | for(i=0; i<x.nNode; i++){ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 674 | sqlite3_snprintf(sizeof(zBuf), zBuf, "node %3u: %7s n=%d\n", |
| 675 | i, jsonType[x.aNode[i].eType], x.aNode[i].n); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 676 | jsonAppend(&s, zBuf); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 677 | if( x.aNode[i].zJContent!=0 ){ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 678 | jsonAppendRaw(&s, " text: ", 10); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 679 | jsonAppendRaw(&s, x.aNode[i].zJContent, x.aNode[i].n); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 680 | jsonAppendRaw(&s, "\n", 1); |
| 681 | } |
| 682 | } |
| 683 | sqlite3_free(x.aNode); |
| 684 | jsonResult(&s); |
| 685 | } |
| 686 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 687 | /* |
| 688 | ** The json_test1(JSON) function parses and rebuilds the JSON string. |
| 689 | */ |
| 690 | static void jsonTest1Func( |
| 691 | sqlite3_context *context, |
| 692 | int argc, |
| 693 | sqlite3_value **argv |
| 694 | ){ |
| 695 | JsonParse x; /* The parse */ |
| 696 | if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return; |
| 697 | jsonReturn(x.aNode, context); |
| 698 | sqlite3_free(x.aNode); |
| 699 | } |
| 700 | |
| 701 | /* |
| 702 | ** The json_nodecount(JSON) function returns the number of nodes in the |
| 703 | ** input JSON string. |
| 704 | */ |
| 705 | static void jsonNodeCountFunc( |
| 706 | sqlite3_context *context, |
| 707 | int argc, |
| 708 | sqlite3_value **argv |
| 709 | ){ |
| 710 | JsonParse x; /* The parse */ |
| 711 | if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return; |
| 712 | sqlite3_result_int64(context, x.nNode); |
| 713 | sqlite3_free(x.aNode); |
| 714 | } |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 715 | #endif /* SQLITE_DEBUG */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 716 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 717 | /**************************************************************************** |
| 718 | ** SQL function implementations |
| 719 | ****************************************************************************/ |
| 720 | |
| 721 | /* |
| 722 | ** Implementation of the json_array(VALUE,...) function. Return a JSON |
| 723 | ** array that contains all values given in arguments. Or if any argument |
| 724 | ** is a BLOB, throw an error. |
| 725 | */ |
| 726 | static void jsonArrayFunc( |
| 727 | sqlite3_context *context, |
| 728 | int argc, |
| 729 | sqlite3_value **argv |
| 730 | ){ |
| 731 | int i; |
| 732 | Json jx; |
| 733 | char cSep = '['; |
| 734 | |
| 735 | jsonInit(&jx, context); |
| 736 | for(i=0; i<argc; i++){ |
| 737 | jsonAppendRaw(&jx, &cSep, 1); |
| 738 | cSep = ','; |
| 739 | switch( sqlite3_value_type(argv[i]) ){ |
| 740 | case SQLITE_NULL: { |
| 741 | jsonAppendRaw(&jx, "null", 4); |
| 742 | break; |
| 743 | } |
| 744 | case SQLITE_INTEGER: |
| 745 | case SQLITE_FLOAT: { |
| 746 | const char *z = (const char*)sqlite3_value_text(argv[i]); |
| 747 | u32 n = (u32)sqlite3_value_bytes(argv[i]); |
| 748 | jsonAppendRaw(&jx, z, n); |
| 749 | break; |
| 750 | } |
| 751 | case SQLITE_TEXT: { |
| 752 | const char *z = (const char*)sqlite3_value_text(argv[i]); |
| 753 | u32 n = (u32)sqlite3_value_bytes(argv[i]); |
| 754 | jsonAppendString(&jx, z, n); |
| 755 | break; |
| 756 | } |
| 757 | default: { |
| 758 | jsonZero(&jx); |
| 759 | sqlite3_result_error(context, "JSON cannot hold BLOB values", -1); |
| 760 | return; |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | jsonAppendRaw(&jx, "]", 1); |
| 765 | jsonResult(&jx); |
| 766 | } |
| 767 | |
| 768 | |
| 769 | /* |
| 770 | ** json_array_length(JSON) |
| 771 | ** json_array_length(JSON, PATH) |
| 772 | ** |
| 773 | ** Return the number of elements in the top-level JSON array. |
| 774 | ** Return 0 if the input is not a well-formed JSON array. |
| 775 | */ |
| 776 | static void jsonArrayLengthFunc( |
| 777 | sqlite3_context *context, |
| 778 | int argc, |
| 779 | sqlite3_value **argv |
| 780 | ){ |
| 781 | JsonParse x; /* The parse */ |
| 782 | sqlite3_int64 n = 0; |
| 783 | u32 i; |
| 784 | const char *zPath; |
| 785 | |
| 786 | if( argc==2 ){ |
| 787 | zPath = (const char*)sqlite3_value_text(argv[1]); |
| 788 | if( zPath==0 ) return; |
| 789 | if( zPath[0]!='$' ) return; |
| 790 | zPath++; |
| 791 | }else{ |
| 792 | zPath = 0; |
| 793 | } |
| 794 | if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0]))==0 ){ |
| 795 | if( x.nNode ){ |
| 796 | JsonNode *pNode = x.aNode; |
| 797 | if( zPath ) pNode = jsonLookup(pNode, zPath); |
| 798 | if( pNode->eType==JSON_ARRAY ){ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 799 | for(i=1; i<=pNode->n; n++){ |
| 800 | i += jsonSizeof(&pNode[i]); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | } |
| 804 | sqlite3_free(x.aNode); |
| 805 | } |
| 806 | sqlite3_result_int64(context, n); |
| 807 | } |
| 808 | |
| 809 | /* |
| 810 | ** json_extract(JSON, PATH) |
| 811 | ** |
| 812 | ** Return the element described by PATH. Return NULL if JSON is not |
| 813 | ** valid JSON or if there is no PATH element or if PATH is malformed. |
| 814 | */ |
| 815 | static void jsonExtractFunc( |
| 816 | sqlite3_context *context, |
| 817 | int argc, |
| 818 | sqlite3_value **argv |
| 819 | ){ |
| 820 | JsonParse x; /* The parse */ |
| 821 | JsonNode *pNode; |
| 822 | const char *zPath; |
| 823 | assert( argc==2 ); |
| 824 | zPath = (const char*)sqlite3_value_text(argv[1]); |
| 825 | if( zPath==0 ) return; |
| 826 | if( zPath[0]!='$' ) return; |
| 827 | zPath++; |
| 828 | if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return; |
| 829 | pNode = jsonLookup(x.aNode, zPath); |
| 830 | if( pNode ){ |
| 831 | jsonReturn(pNode, context); |
| 832 | } |
| 833 | sqlite3_free(x.aNode); |
| 834 | } |
| 835 | |
| 836 | /* |
| 837 | ** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON |
| 838 | ** object that contains all name/value given in arguments. Or if any name |
| 839 | ** is not a string or if any value is a BLOB, throw an error. |
| 840 | */ |
| 841 | static void jsonObjectFunc( |
| 842 | sqlite3_context *context, |
| 843 | int argc, |
| 844 | sqlite3_value **argv |
| 845 | ){ |
| 846 | int i; |
| 847 | Json jx; |
| 848 | char cSep = '{'; |
| 849 | const char *z; |
| 850 | u32 n; |
| 851 | |
| 852 | if( argc&1 ){ |
| 853 | sqlite3_result_error(context, "json_object() requires an even number " |
| 854 | "of arguments", -1); |
| 855 | return; |
| 856 | } |
| 857 | jsonInit(&jx, context); |
| 858 | for(i=0; i<argc; i+=2){ |
| 859 | jsonAppendRaw(&jx, &cSep, 1); |
| 860 | cSep = ','; |
| 861 | if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){ |
| 862 | sqlite3_result_error(context, "json_object() labels must be TEXT", -1); |
| 863 | jsonZero(&jx); |
| 864 | return; |
| 865 | } |
| 866 | z = (const char*)sqlite3_value_text(argv[i]); |
| 867 | n = (u32)sqlite3_value_bytes(argv[i]); |
| 868 | jsonAppendString(&jx, z, n); |
| 869 | jsonAppendRaw(&jx, ":", 1); |
| 870 | switch( sqlite3_value_type(argv[i+1]) ){ |
| 871 | case SQLITE_NULL: { |
| 872 | jsonAppendRaw(&jx, "null", 4); |
| 873 | break; |
| 874 | } |
| 875 | case SQLITE_INTEGER: |
| 876 | case SQLITE_FLOAT: { |
| 877 | z = (const char*)sqlite3_value_text(argv[i+1]); |
| 878 | n = (u32)sqlite3_value_bytes(argv[i+1]); |
| 879 | jsonAppendRaw(&jx, z, n); |
| 880 | break; |
| 881 | } |
| 882 | case SQLITE_TEXT: { |
| 883 | z = (const char*)sqlite3_value_text(argv[i+1]); |
| 884 | n = (u32)sqlite3_value_bytes(argv[i+1]); |
| 885 | jsonAppendString(&jx, z, n); |
| 886 | break; |
| 887 | } |
| 888 | default: { |
| 889 | jsonZero(&jx); |
| 890 | sqlite3_result_error(context, "JSON cannot hold BLOB values", -1); |
| 891 | return; |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | jsonAppendRaw(&jx, "}", 1); |
| 896 | jsonResult(&jx); |
| 897 | } |
| 898 | |
| 899 | |
| 900 | /* |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 901 | ** json_remove(JSON, PATH, ...) |
| 902 | ** |
| 903 | ** Remove the named elements from JSON and return the result. Ill-formed |
| 904 | ** PATH arguments are silently ignored. If JSON is ill-formed, then NULL |
| 905 | ** is returned. |
| 906 | */ |
| 907 | static void jsonRemoveFunc( |
| 908 | sqlite3_context *context, |
| 909 | int argc, |
| 910 | sqlite3_value **argv |
| 911 | ){ |
| 912 | JsonParse x; /* The parse */ |
| 913 | JsonNode *pNode; |
| 914 | const char *zPath; |
| 915 | u32 i; |
| 916 | |
| 917 | if( argc<1 ) return; |
| 918 | if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return; |
| 919 | if( x.nNode ){ |
| 920 | for(i=1; i<argc; i++){ |
| 921 | zPath = (const char*)sqlite3_value_text(argv[i]); |
| 922 | if( zPath==0 ) continue; |
| 923 | if( zPath[0]!='$' ) continue; |
| 924 | pNode = jsonLookup(x.aNode, &zPath[1]); |
| 925 | if( pNode ) pNode->jnFlags |= JNODE_REMOVE; |
| 926 | } |
| 927 | if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){ |
| 928 | jsonReturn(x.aNode, context); |
| 929 | } |
| 930 | } |
| 931 | sqlite3_free(x.aNode); |
| 932 | } |
| 933 | |
| 934 | /* |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 935 | ** json_type(JSON) |
| 936 | ** json_type(JSON, PATH) |
| 937 | ** |
| 938 | ** Return the top-level "type" of a JSON string. Return NULL if the |
| 939 | ** input is not a well-formed JSON string. |
| 940 | */ |
| 941 | static void jsonTypeFunc( |
| 942 | sqlite3_context *context, |
| 943 | int argc, |
| 944 | sqlite3_value **argv |
| 945 | ){ |
| 946 | JsonParse x; /* The parse */ |
| 947 | const char *zPath; |
| 948 | |
| 949 | if( argc==2 ){ |
| 950 | zPath = (const char*)sqlite3_value_text(argv[1]); |
| 951 | if( zPath==0 ) return; |
| 952 | if( zPath[0]!='$' ) return; |
| 953 | zPath++; |
| 954 | }else{ |
| 955 | zPath = 0; |
| 956 | } |
| 957 | if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return; |
| 958 | if( x.nNode ){ |
| 959 | JsonNode *pNode = x.aNode; |
| 960 | if( zPath ) pNode = jsonLookup(pNode, zPath); |
| 961 | sqlite3_result_text(context, jsonType[pNode->eType], -1, SQLITE_STATIC); |
| 962 | } |
| 963 | sqlite3_free(x.aNode); |
| 964 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 965 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 966 | #ifdef _WIN32 |
| 967 | __declspec(dllexport) |
| 968 | #endif |
| 969 | int sqlite3_json_init( |
| 970 | sqlite3 *db, |
| 971 | char **pzErrMsg, |
| 972 | const sqlite3_api_routines *pApi |
| 973 | ){ |
| 974 | int rc = SQLITE_OK; |
| 975 | int i; |
| 976 | static const struct { |
| 977 | const char *zName; |
| 978 | int nArg; |
| 979 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**); |
| 980 | } aFunc[] = { |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 981 | { "json_array", -1, jsonArrayFunc }, |
| 982 | { "json_array_length", 1, jsonArrayLengthFunc }, |
| 983 | { "json_array_length", 2, jsonArrayLengthFunc }, |
| 984 | { "json_extract", 2, jsonExtractFunc }, |
| 985 | { "json_object", -1, jsonObjectFunc }, |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 986 | { "json_remove", -1, jsonRemoveFunc }, |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 987 | { "json_type", 1, jsonTypeFunc }, |
| 988 | { "json_type", 2, jsonTypeFunc }, |
| 989 | |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 990 | #if SQLITE_DEBUG |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 991 | /* DEBUG and TESTING functions */ |
| 992 | { "json_parse", 1, jsonParseFunc }, |
| 993 | { "json_test1", 1, jsonTest1Func }, |
| 994 | { "json_nodecount", 1, jsonNodeCountFunc }, |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame^] | 995 | #endif |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 996 | }; |
| 997 | SQLITE_EXTENSION_INIT2(pApi); |
| 998 | (void)pzErrMsg; /* Unused parameter */ |
| 999 | for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){ |
| 1000 | rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg, |
| 1001 | SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0, |
| 1002 | aFunc[i].xFunc, 0, 0); |
| 1003 | } |
| 1004 | return rc; |
| 1005 | } |