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 | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 27 | |
| 28 | /* Unsigned integer types */ |
| 29 | typedef sqlite3_uint64 u64; |
| 30 | typedef unsigned int u32; |
| 31 | typedef unsigned char u8; |
| 32 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 33 | /* An instance of this object represents a JSON string |
| 34 | ** under construction. Really, this is a generic string accumulator |
| 35 | ** that can be and is used to create strings other than JSON. |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 36 | */ |
| 37 | typedef struct Json Json; |
| 38 | struct Json { |
| 39 | sqlite3_context *pCtx; /* Function context - put error messages here */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 40 | char *zBuf; /* Append JSON content here */ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 41 | u64 nAlloc; /* Bytes of storage available in zBuf[] */ |
| 42 | u64 nUsed; /* Bytes of zBuf[] currently used */ |
| 43 | u8 bStatic; /* True if zBuf is static space */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 44 | u8 oom; /* True if an OOM has been encountered */ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 45 | char zSpace[100]; /* Initial static space */ |
| 46 | }; |
| 47 | |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 48 | /* JSON type values |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 49 | */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 50 | #define JSON_NULL 0 |
| 51 | #define JSON_TRUE 1 |
| 52 | #define JSON_FALSE 2 |
| 53 | #define JSON_INT 3 |
| 54 | #define JSON_REAL 4 |
| 55 | #define JSON_STRING 5 |
| 56 | #define JSON_ARRAY 6 |
| 57 | #define JSON_OBJECT 7 |
| 58 | |
| 59 | /* A single node of parsed JSON |
| 60 | */ |
| 61 | typedef struct JsonNode JsonNode; |
| 62 | struct JsonNode { |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 63 | u8 eType; /* One of the JSON_ type values */ |
| 64 | u8 bRaw; /* Content is raw, rather than JSON encoded */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 65 | u32 n; /* Bytes of content, or number of sub-nodes */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 66 | const char *zJContent; /* JSON content */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | /* A completely parsed JSON string |
| 70 | */ |
| 71 | typedef struct JsonParse JsonParse; |
| 72 | struct JsonParse { |
| 73 | u32 nNode; /* Number of slots of aNode[] used */ |
| 74 | u32 nAlloc; /* Number of slots of aNode[] allocated */ |
| 75 | JsonNode *aNode; /* Array of nodes containing the parse */ |
| 76 | const char *zJson; /* Original JSON string */ |
| 77 | u8 oom; /* Set to true if out of memory */ |
| 78 | }; |
| 79 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 80 | /* Set the Json object to an empty string |
| 81 | */ |
| 82 | static void jsonZero(Json *p){ |
| 83 | p->zBuf = p->zSpace; |
| 84 | p->nAlloc = sizeof(p->zSpace); |
| 85 | p->nUsed = 0; |
| 86 | p->bStatic = 1; |
| 87 | } |
| 88 | |
| 89 | /* Initialize the Json object |
| 90 | */ |
| 91 | static void jsonInit(Json *p, sqlite3_context *pCtx){ |
| 92 | p->pCtx = pCtx; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 93 | p->oom = 0; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 94 | jsonZero(p); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /* Free all allocated memory and reset the Json object back to its |
| 99 | ** initial state. |
| 100 | */ |
| 101 | static void jsonReset(Json *p){ |
| 102 | if( !p->bStatic ) sqlite3_free(p->zBuf); |
| 103 | jsonZero(p); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /* Report an out-of-memory (OOM) condition |
| 108 | */ |
| 109 | static void jsonOom(Json *p){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 110 | p->oom = 1; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 111 | sqlite3_result_error_nomem(p->pCtx); |
| 112 | jsonReset(p); |
| 113 | } |
| 114 | |
| 115 | /* Enlarge pJson->zBuf so that it can hold at least N more bytes. |
| 116 | ** Return zero on success. Return non-zero on an OOM error |
| 117 | */ |
| 118 | static int jsonGrow(Json *p, u32 N){ |
| 119 | u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+100; |
| 120 | char *zNew; |
| 121 | if( p->bStatic ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 122 | if( p->oom ) return SQLITE_NOMEM; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 123 | zNew = sqlite3_malloc64(nTotal); |
| 124 | if( zNew==0 ){ |
| 125 | jsonOom(p); |
| 126 | return SQLITE_NOMEM; |
| 127 | } |
| 128 | memcpy(zNew, p->zBuf, p->nUsed); |
| 129 | p->zBuf = zNew; |
| 130 | p->bStatic = 0; |
| 131 | }else{ |
| 132 | zNew = sqlite3_realloc64(p->zBuf, nTotal); |
| 133 | if( zNew==0 ){ |
| 134 | jsonOom(p); |
| 135 | return SQLITE_NOMEM; |
| 136 | } |
| 137 | p->zBuf = zNew; |
| 138 | } |
| 139 | p->nAlloc = nTotal; |
| 140 | return SQLITE_OK; |
| 141 | } |
| 142 | |
| 143 | /* Append N bytes from zIn onto the end of the Json string. |
| 144 | */ |
| 145 | static void jsonAppendRaw(Json *p, const char *zIn, u32 N){ |
| 146 | if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return; |
| 147 | memcpy(p->zBuf+p->nUsed, zIn, N); |
| 148 | p->nUsed += N; |
| 149 | } |
| 150 | |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 151 | /* Append the zero-terminated string zIn |
| 152 | */ |
| 153 | static void jsonAppend(Json *p, const char *zIn){ |
| 154 | jsonAppendRaw(p, zIn, (u32)strlen(zIn)); |
| 155 | } |
| 156 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 157 | /* Append a single character |
| 158 | */ |
| 159 | static void jsonAppendChar(Json *p, char c){ |
| 160 | if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return; |
| 161 | p->zBuf[p->nUsed++] = c; |
| 162 | } |
| 163 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 164 | /* Append the N-byte string in zIn to the end of the Json string |
| 165 | ** under construction. Enclose the string in "..." and escape |
| 166 | ** any double-quotes or backslash characters contained within the |
| 167 | ** string. |
| 168 | */ |
| 169 | static void jsonAppendString(Json *p, const char *zIn, u32 N){ |
| 170 | u32 i; |
| 171 | if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return; |
| 172 | p->zBuf[p->nUsed++] = '"'; |
| 173 | for(i=0; i<N; i++){ |
| 174 | char c = zIn[i]; |
| 175 | if( c=='"' || c=='\\' ){ |
| 176 | if( (p->nUsed+N+1-i > p->nAlloc) && jsonGrow(p,N+1-i)!=0 ) return; |
| 177 | p->zBuf[p->nUsed++] = '\\'; |
| 178 | } |
| 179 | p->zBuf[p->nUsed++] = c; |
| 180 | } |
| 181 | p->zBuf[p->nUsed++] = '"'; |
| 182 | } |
| 183 | |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 184 | /* Make the JSON in p the result of the SQL function. |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 185 | */ |
| 186 | static void jsonResult(Json *p){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 187 | if( p->oom==0 ){ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 188 | sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed, |
| 189 | p->bStatic ? SQLITE_TRANSIENT : sqlite3_free, |
| 190 | SQLITE_UTF8); |
| 191 | jsonZero(p); |
| 192 | } |
| 193 | assert( p->bStatic ); |
| 194 | } |
| 195 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 196 | /* |
| 197 | ** Convert the JsonNode pNode into a pure JSON string and |
| 198 | ** append to pOut. Subsubstructure is also included. Return |
| 199 | ** the number of JsonNode objects that are encoded. |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 200 | */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 201 | static int jsonRenderNode(JsonNode *pNode, Json *pOut){ |
| 202 | u32 j = 0; |
| 203 | switch( pNode->eType ){ |
| 204 | case JSON_NULL: { |
| 205 | jsonAppendRaw(pOut, "null", 4); |
| 206 | break; |
| 207 | } |
| 208 | case JSON_TRUE: { |
| 209 | jsonAppendRaw(pOut, "true", 4); |
| 210 | break; |
| 211 | } |
| 212 | case JSON_FALSE: { |
| 213 | jsonAppendRaw(pOut, "false", 5); |
| 214 | break; |
| 215 | } |
| 216 | case JSON_STRING: { |
| 217 | if( pNode->bRaw ){ |
| 218 | jsonAppendString(pOut, pNode->zJContent, pNode->n); |
| 219 | break; |
| 220 | } |
| 221 | /* Fall through into the next case */ |
| 222 | } |
| 223 | case JSON_REAL: |
| 224 | case JSON_INT: { |
| 225 | jsonAppendRaw(pOut, pNode->zJContent, pNode->n); |
| 226 | break; |
| 227 | } |
| 228 | case JSON_ARRAY: { |
| 229 | jsonAppendChar(pOut, '['); |
| 230 | j = 0; |
| 231 | while( j<pNode->n ){ |
| 232 | if( j>0 ) jsonAppendChar(pOut, ','); |
| 233 | j += jsonRenderNode(&pNode[j+1], pOut); |
| 234 | } |
| 235 | jsonAppendChar(pOut, ']'); |
| 236 | break; |
| 237 | } |
| 238 | case JSON_OBJECT: { |
| 239 | jsonAppendChar(pOut, '{'); |
| 240 | j = 0; |
| 241 | while( j<pNode->n ){ |
| 242 | if( j>0 ) jsonAppendChar(pOut, ','); |
| 243 | j += jsonRenderNode(&pNode[j+1], pOut); |
| 244 | jsonAppendChar(pOut, ':'); |
| 245 | j += jsonRenderNode(&pNode[j+1], pOut); |
| 246 | } |
| 247 | jsonAppendChar(pOut, '}'); |
| 248 | break; |
| 249 | } |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 250 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 251 | return j+1; |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | ** Make the JsonNode the return value of the function. |
| 256 | */ |
| 257 | static void jsonReturn(JsonNode *pNode, sqlite3_context *pCtx){ |
| 258 | switch( pNode->eType ){ |
| 259 | case JSON_NULL: { |
| 260 | sqlite3_result_null(pCtx); |
| 261 | break; |
| 262 | } |
| 263 | case JSON_TRUE: { |
| 264 | sqlite3_result_int(pCtx, 1); |
| 265 | break; |
| 266 | } |
| 267 | case JSON_FALSE: { |
| 268 | sqlite3_result_int(pCtx, 0); |
| 269 | break; |
| 270 | } |
| 271 | |
| 272 | /* FIXME: We really want to do text->numeric conversion on these. |
| 273 | ** Doing so would be easy if these were internal routines, but the |
| 274 | ** necessary interfaces are not exposed for doing it as a loadable |
| 275 | ** extension. */ |
| 276 | case JSON_REAL: |
| 277 | case JSON_INT: { |
| 278 | sqlite3_result_text(pCtx, pNode->zJContent, pNode->n, SQLITE_TRANSIENT); |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | case JSON_STRING: { |
| 283 | if( pNode->bRaw ){ |
| 284 | sqlite3_result_text(pCtx, pNode->zJContent, pNode->n, SQLITE_TRANSIENT); |
| 285 | }else{ |
| 286 | /* Translate JSON formatted string into raw text */ |
| 287 | } |
| 288 | break; |
| 289 | } |
| 290 | case JSON_ARRAY: |
| 291 | case JSON_OBJECT: { |
| 292 | Json s; |
| 293 | jsonInit(&s, pCtx); |
| 294 | jsonRenderNode(pNode, &s); |
| 295 | jsonResult(&s); |
| 296 | break; |
| 297 | } |
| 298 | } |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 299 | } |
| 300 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 301 | /* |
| 302 | ** Implementation of the json_array(VALUE,...) function. Return a JSON |
| 303 | ** array that contains all values given in arguments. Or if any argument |
| 304 | ** is a BLOB, throw an error. |
| 305 | */ |
| 306 | static void jsonArrayFunc( |
| 307 | sqlite3_context *context, |
| 308 | int argc, |
| 309 | sqlite3_value **argv |
| 310 | ){ |
| 311 | int i; |
| 312 | Json jx; |
| 313 | char cSep = '['; |
| 314 | |
| 315 | jsonInit(&jx, context); |
| 316 | for(i=0; i<argc; i++){ |
| 317 | jsonAppendRaw(&jx, &cSep, 1); |
| 318 | cSep = ','; |
| 319 | switch( sqlite3_value_type(argv[i]) ){ |
| 320 | case SQLITE_NULL: { |
| 321 | jsonAppendRaw(&jx, "null", 4); |
| 322 | break; |
| 323 | } |
| 324 | case SQLITE_INTEGER: |
| 325 | case SQLITE_FLOAT: { |
| 326 | const char *z = (const char*)sqlite3_value_text(argv[i]); |
| 327 | u32 n = (u32)sqlite3_value_bytes(argv[i]); |
| 328 | jsonAppendRaw(&jx, z, n); |
| 329 | break; |
| 330 | } |
| 331 | case SQLITE_TEXT: { |
| 332 | const char *z = (const char*)sqlite3_value_text(argv[i]); |
| 333 | u32 n = (u32)sqlite3_value_bytes(argv[i]); |
| 334 | jsonAppendString(&jx, z, n); |
| 335 | break; |
| 336 | } |
| 337 | default: { |
| 338 | jsonZero(&jx); |
| 339 | sqlite3_result_error(context, "JSON cannot hold BLOB values", -1); |
| 340 | return; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | jsonAppendRaw(&jx, "]", 1); |
drh | 2032d60 | 2015-08-12 17:23:34 +0000 | [diff] [blame] | 345 | jsonResult(&jx); |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | ** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON |
| 350 | ** object that contains all name/value given in arguments. Or if any name |
| 351 | ** is not a string or if any value is a BLOB, throw an error. |
| 352 | */ |
| 353 | static void jsonObjectFunc( |
| 354 | sqlite3_context *context, |
| 355 | int argc, |
| 356 | sqlite3_value **argv |
| 357 | ){ |
| 358 | int i; |
| 359 | Json jx; |
| 360 | char cSep = '{'; |
| 361 | const char *z; |
| 362 | u32 n; |
| 363 | |
| 364 | if( argc&1 ){ |
| 365 | sqlite3_result_error(context, "json_object() requires an even number " |
| 366 | "of arguments", -1); |
| 367 | return; |
| 368 | } |
| 369 | jsonInit(&jx, context); |
| 370 | for(i=0; i<argc; i+=2){ |
| 371 | jsonAppendRaw(&jx, &cSep, 1); |
| 372 | cSep = ','; |
| 373 | if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){ |
| 374 | sqlite3_result_error(context, "json_object() labels must be TEXT", -1); |
| 375 | jsonZero(&jx); |
| 376 | return; |
| 377 | } |
| 378 | z = (const char*)sqlite3_value_text(argv[i]); |
| 379 | n = (u32)sqlite3_value_bytes(argv[i]); |
| 380 | jsonAppendString(&jx, z, n); |
| 381 | jsonAppendRaw(&jx, ":", 1); |
| 382 | switch( sqlite3_value_type(argv[i+1]) ){ |
| 383 | case SQLITE_NULL: { |
| 384 | jsonAppendRaw(&jx, "null", 4); |
| 385 | break; |
| 386 | } |
| 387 | case SQLITE_INTEGER: |
| 388 | case SQLITE_FLOAT: { |
| 389 | z = (const char*)sqlite3_value_text(argv[i+1]); |
| 390 | n = (u32)sqlite3_value_bytes(argv[i+1]); |
| 391 | jsonAppendRaw(&jx, z, n); |
| 392 | break; |
| 393 | } |
| 394 | case SQLITE_TEXT: { |
| 395 | z = (const char*)sqlite3_value_text(argv[i+1]); |
| 396 | n = (u32)sqlite3_value_bytes(argv[i+1]); |
| 397 | jsonAppendString(&jx, z, n); |
| 398 | break; |
| 399 | } |
| 400 | default: { |
| 401 | jsonZero(&jx); |
| 402 | sqlite3_result_error(context, "JSON cannot hold BLOB values", -1); |
| 403 | return; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | jsonAppendRaw(&jx, "}", 1); |
| 408 | jsonResult(&jx); |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 409 | } |
| 410 | |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 411 | /* |
| 412 | ** Create a new JsonNode instance based on the arguments and append that |
| 413 | ** instance to the JsonParse. Return the index in pParse->aNode[] of the |
| 414 | ** new node, or -1 if a memory allocation fails. |
| 415 | */ |
| 416 | static int jsonParseAddNode( |
| 417 | JsonParse *pParse, /* Append the node to this object */ |
| 418 | u32 eType, /* Node type */ |
| 419 | u32 n, /* Content size or sub-node count */ |
| 420 | const char *zContent /* Content */ |
| 421 | ){ |
| 422 | JsonNode *p; |
| 423 | if( pParse->nNode>=pParse->nAlloc ){ |
| 424 | u32 nNew; |
| 425 | JsonNode *pNew; |
| 426 | if( pParse->oom ) return -1; |
| 427 | nNew = pParse->nAlloc*2 + 10; |
| 428 | if( nNew<=pParse->nNode ){ |
| 429 | pParse->oom = 1; |
| 430 | return -1; |
| 431 | } |
| 432 | pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew); |
| 433 | if( pNew==0 ){ |
| 434 | pParse->oom = 1; |
| 435 | return -1; |
| 436 | } |
| 437 | pParse->nAlloc = nNew; |
| 438 | pParse->aNode = pNew; |
| 439 | } |
| 440 | p = &pParse->aNode[pParse->nNode]; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 441 | p->eType = (u8)eType; |
| 442 | p->bRaw = 0; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 443 | p->n = n; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 444 | p->zJContent = zContent; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 445 | return pParse->nNode++; |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | ** Parse a single JSON value which begins at pParse->zJson[i]. Return the |
| 450 | ** index of the first character past the end of the value parsed. |
| 451 | ** |
| 452 | ** Return negative for a syntax error. Special cases: return -2 if the |
| 453 | ** first non-whitespace character is '}' and return -3 if the first |
| 454 | ** non-whitespace character is ']'. |
| 455 | */ |
| 456 | static int jsonParseValue(JsonParse *pParse, u32 i){ |
| 457 | char c; |
| 458 | u32 j; |
| 459 | u32 iThis; |
| 460 | int x; |
| 461 | while( isspace(pParse->zJson[i]) ){ i++; } |
| 462 | if( (c = pParse->zJson[i])==0 ) return 0; |
| 463 | if( c=='{' ){ |
| 464 | /* Parse object */ |
| 465 | iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0); |
| 466 | if( iThis<0 ) return -1; |
| 467 | for(j=i+1;;j++){ |
| 468 | while( isspace(pParse->zJson[j]) ){ j++; } |
| 469 | x = jsonParseValue(pParse, j); |
| 470 | if( x<0 ){ |
| 471 | if( x==(-2) && pParse->nNode==iThis+1 ) return j+1; |
| 472 | return -1; |
| 473 | } |
| 474 | if( pParse->aNode[pParse->nNode-1].eType!=JSON_STRING ) return -1; |
| 475 | j = x; |
| 476 | while( isspace(pParse->zJson[j]) ){ j++; } |
| 477 | if( pParse->zJson[j]!=':' ) return -1; |
| 478 | j++; |
| 479 | x = jsonParseValue(pParse, j); |
| 480 | if( x<0 ) return -1; |
| 481 | j = x; |
| 482 | while( isspace(pParse->zJson[j]) ){ j++; } |
| 483 | c = pParse->zJson[j]; |
| 484 | if( c==',' ) continue; |
| 485 | if( c!='}' ) return -1; |
| 486 | break; |
| 487 | } |
| 488 | pParse->aNode[iThis].n = pParse->nNode - iThis - 1; |
| 489 | return j+1; |
| 490 | }else if( c=='[' ){ |
| 491 | /* Parse array */ |
| 492 | iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0); |
| 493 | if( iThis<0 ) return -1; |
| 494 | for(j=i+1;;j++){ |
| 495 | while( isspace(pParse->zJson[j]) ){ j++; } |
| 496 | x = jsonParseValue(pParse, j); |
| 497 | if( x<0 ){ |
| 498 | if( x==(-3) && pParse->nNode==iThis+1 ) return j+1; |
| 499 | return -1; |
| 500 | } |
| 501 | j = x; |
| 502 | while( isspace(pParse->zJson[j]) ){ j++; } |
| 503 | c = pParse->zJson[j]; |
| 504 | if( c==',' ) continue; |
| 505 | if( c!=']' ) return -1; |
| 506 | break; |
| 507 | } |
| 508 | pParse->aNode[iThis].n = pParse->nNode - iThis - 1; |
| 509 | return j+1; |
| 510 | }else if( c=='"' ){ |
| 511 | /* Parse string */ |
| 512 | j = i+1; |
| 513 | for(;;){ |
| 514 | c = pParse->zJson[j]; |
| 515 | if( c==0 ) return -1; |
| 516 | if( c=='\\' ){ |
| 517 | c = pParse->zJson[++j]; |
| 518 | if( c==0 ) return -1; |
| 519 | }else if( c=='"' ){ |
| 520 | break; |
| 521 | } |
| 522 | j++; |
| 523 | } |
| 524 | jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]); |
| 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 | } |
| 604 | |
| 605 | /* |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 606 | ** The json_parse(JSON) function returns a string which describes |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 607 | ** a parse of the JSON provided. Or it returns NULL if JSON is not |
| 608 | ** well-formed. |
| 609 | */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 610 | static void jsonParseFunc( |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 611 | sqlite3_context *context, |
| 612 | int argc, |
| 613 | sqlite3_value **argv |
| 614 | ){ |
| 615 | Json s; /* Output string - not real JSON */ |
| 616 | JsonParse x; /* The parse */ |
| 617 | u32 i; |
| 618 | char zBuf[50]; |
| 619 | static const char *azType[] = { |
| 620 | "NULL", "TRUE", "FALSE", "INT", "REAL", "STRING", "ARRAY", "OBJECT" |
| 621 | }; |
| 622 | |
| 623 | assert( argc==1 ); |
| 624 | if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return; |
| 625 | jsonInit(&s, context); |
| 626 | for(i=0; i<x.nNode; i++){ |
| 627 | sqlite3_snprintf(sizeof(zBuf), zBuf, "node %u:\n", i); |
| 628 | jsonAppend(&s, zBuf); |
| 629 | sqlite3_snprintf(sizeof(zBuf), zBuf, " type: %s\n", |
| 630 | azType[x.aNode[i].eType]); |
| 631 | jsonAppend(&s, zBuf); |
| 632 | if( x.aNode[i].eType>=JSON_INT ){ |
| 633 | sqlite3_snprintf(sizeof(zBuf), zBuf, " n: %u\n", x.aNode[i].n); |
| 634 | jsonAppend(&s, zBuf); |
| 635 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 636 | if( x.aNode[i].zJContent!=0 ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 637 | sqlite3_snprintf(sizeof(zBuf), zBuf, " ofst: %u\n", |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 638 | (u32)(x.aNode[i].zJContent - x.zJson)); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 639 | jsonAppend(&s, zBuf); |
| 640 | jsonAppendRaw(&s, " text: ", 8); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 641 | jsonAppendRaw(&s, x.aNode[i].zJContent, x.aNode[i].n); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 642 | jsonAppendRaw(&s, "\n", 1); |
| 643 | } |
| 644 | } |
| 645 | sqlite3_free(x.aNode); |
| 646 | jsonResult(&s); |
| 647 | } |
| 648 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 649 | /* |
| 650 | ** The json_test1(JSON) function parses and rebuilds the JSON string. |
| 651 | */ |
| 652 | static void jsonTest1Func( |
| 653 | sqlite3_context *context, |
| 654 | int argc, |
| 655 | sqlite3_value **argv |
| 656 | ){ |
| 657 | JsonParse x; /* The parse */ |
| 658 | if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return; |
| 659 | jsonReturn(x.aNode, context); |
| 660 | sqlite3_free(x.aNode); |
| 661 | } |
| 662 | |
| 663 | /* |
| 664 | ** The json_nodecount(JSON) function returns the number of nodes in the |
| 665 | ** input JSON string. |
| 666 | */ |
| 667 | static void jsonNodeCountFunc( |
| 668 | sqlite3_context *context, |
| 669 | int argc, |
| 670 | sqlite3_value **argv |
| 671 | ){ |
| 672 | JsonParse x; /* The parse */ |
| 673 | if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return; |
| 674 | sqlite3_result_int64(context, x.nNode); |
| 675 | sqlite3_free(x.aNode); |
| 676 | } |
| 677 | |
| 678 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 679 | #ifdef _WIN32 |
| 680 | __declspec(dllexport) |
| 681 | #endif |
| 682 | int sqlite3_json_init( |
| 683 | sqlite3 *db, |
| 684 | char **pzErrMsg, |
| 685 | const sqlite3_api_routines *pApi |
| 686 | ){ |
| 687 | int rc = SQLITE_OK; |
| 688 | int i; |
| 689 | static const struct { |
| 690 | const char *zName; |
| 691 | int nArg; |
| 692 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**); |
| 693 | } aFunc[] = { |
drh | 2032d60 | 2015-08-12 17:23:34 +0000 | [diff] [blame] | 694 | { "json_array", -1, jsonArrayFunc }, |
| 695 | { "json_object", -1, jsonObjectFunc }, |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame^] | 696 | { "json_parse", 1, jsonParseFunc }, /* DEBUG */ |
| 697 | { "json_test1", 1, jsonTest1Func }, /* DEBUG */ |
| 698 | { "json_nodecount", 1, jsonNodeCountFunc }, /* DEBUG */ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 699 | }; |
| 700 | SQLITE_EXTENSION_INIT2(pApi); |
| 701 | (void)pzErrMsg; /* Unused parameter */ |
| 702 | for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){ |
| 703 | rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg, |
| 704 | SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0, |
| 705 | aFunc[i].xFunc, 0, 0); |
| 706 | } |
| 707 | return rc; |
| 708 | } |