Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Abandon the JSONB format for now. (We may return to it in the future.) Add a function to render a JSON parse. |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | json |
Files: | files | file ages | folders |
SHA1: |
9703c0aa18ae43375af876474b818e50 |
User & Date: | drh 2015-08-17 11:28:03.070 |
Context
2015-08-17
| ||
15:17 | Initial implementation for json_array_length(), json_extract(), and json_type(). (check-in: 3998320451 user: drh tags: json) | |
11:28 | Abandon the JSONB format for now. (We may return to it in the future.) Add a function to render a JSON parse. (check-in: 9703c0aa18 user: drh tags: json) | |
2015-08-15
| ||
21:29 | Fix off-by-one error when parsing primitive JSON types "true", "false", and "null". (check-in: 42c15c1e36 user: drh tags: json) | |
Changes
Changes to ext/misc/json.c.
︙ | ︙ | |||
11 12 13 14 15 16 17 | ****************************************************************************** ** ** This SQLite extension implements JSON functions. The interface is ** modeled after MySQL JSON functions: ** ** https://dev.mysql.com/doc/refman/5.7/en/json.html ** | | < | < < < < < < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | | > | | 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | ****************************************************************************** ** ** This SQLite extension implements JSON functions. The interface is ** modeled after MySQL JSON functions: ** ** https://dev.mysql.com/doc/refman/5.7/en/json.html ** ** For the time being, all JSON is stored as pure text. (We might add ** a JSONB type in the future which stores a binary encoding of JSON in ** a BLOB, but there is no support for JSONB in the current implementation.) */ #include "sqlite3ext.h" SQLITE_EXTENSION_INIT1 #include <assert.h> #include <string.h> #include <ctype.h> /* Unsigned integer types */ typedef sqlite3_uint64 u64; typedef unsigned int u32; typedef unsigned char u8; /* An instance of this object represents a JSON string ** under construction. Really, this is a generic string accumulator ** that can be and is used to create strings other than JSON. */ typedef struct Json Json; struct Json { sqlite3_context *pCtx; /* Function context - put error messages here */ char *zBuf; /* Append JSON content here */ u64 nAlloc; /* Bytes of storage available in zBuf[] */ u64 nUsed; /* Bytes of zBuf[] currently used */ u8 bStatic; /* True if zBuf is static space */ u8 oom; /* True if an OOM has been encountered */ char zSpace[100]; /* Initial static space */ }; |
︙ | ︙ | |||
109 110 111 112 113 114 115 | #define JSON_ARRAY 6 #define JSON_OBJECT 7 /* A single node of parsed JSON */ typedef struct JsonNode JsonNode; struct JsonNode { | | > | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #define JSON_ARRAY 6 #define JSON_OBJECT 7 /* A single node of parsed JSON */ typedef struct JsonNode JsonNode; struct JsonNode { u8 eType; /* One of the JSON_ type values */ u8 bRaw; /* Content is raw, rather than JSON encoded */ u32 n; /* Bytes of content, or number of sub-nodes */ const char *zJContent; /* JSON content */ }; /* A completely parsed JSON string */ typedef struct JsonParse JsonParse; struct JsonParse { u32 nNode; /* Number of slots of aNode[] used */ u32 nAlloc; /* Number of slots of aNode[] allocated */ JsonNode *aNode; /* Array of nodes containing the parse */ const char *zJson; /* Original JSON string */ u8 oom; /* Set to true if out of memory */ }; /* Set the Json object to an empty string */ static void jsonZero(Json *p){ p->zBuf = p->zSpace; p->nAlloc = sizeof(p->zSpace); p->nUsed = 0; p->bStatic = 1; |
︙ | ︙ | |||
258 259 260 261 262 263 264 265 266 267 268 269 270 271 | } /* Append the zero-terminated string zIn */ static void jsonAppend(Json *p, const char *zIn){ jsonAppendRaw(p, zIn, (u32)strlen(zIn)); } /* Append the N-byte string in zIn to the end of the Json string ** under construction. Enclose the string in "..." and escape ** any double-quotes or backslash characters contained within the ** string. */ static void jsonAppendString(Json *p, const char *zIn, u32 N){ | > > > > > > > | 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | } /* Append the zero-terminated string zIn */ static void jsonAppend(Json *p, const char *zIn){ jsonAppendRaw(p, zIn, (u32)strlen(zIn)); } /* Append a single character */ static void jsonAppendChar(Json *p, char c){ if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return; p->zBuf[p->nUsed++] = c; } /* Append the N-byte string in zIn to the end of the Json string ** under construction. Enclose the string in "..." and escape ** any double-quotes or backslash characters contained within the ** string. */ static void jsonAppendString(Json *p, const char *zIn, u32 N){ |
︙ | ︙ | |||
279 280 281 282 283 284 285 | p->zBuf[p->nUsed++] = '\\'; } p->zBuf[p->nUsed++] = c; } p->zBuf[p->nUsed++] = '"'; } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > | | > | > > > | > > > > > > | > > > > > > > > | | > > > > > > > > > > > > > > > > > | 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | p->zBuf[p->nUsed++] = '\\'; } p->zBuf[p->nUsed++] = c; } p->zBuf[p->nUsed++] = '"'; } /* Make the JSON in p the result of the SQL function. */ static void jsonResult(Json *p){ if( p->oom==0 ){ sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed, p->bStatic ? SQLITE_TRANSIENT : sqlite3_free, SQLITE_UTF8); jsonZero(p); } assert( p->bStatic ); } /* ** Convert the JsonNode pNode into a pure JSON string and ** append to pOut. Subsubstructure is also included. Return ** the number of JsonNode objects that are encoded. */ static int jsonRenderNode(JsonNode *pNode, Json *pOut){ u32 j = 0; switch( pNode->eType ){ case JSON_NULL: { jsonAppendRaw(pOut, "null", 4); break; } case JSON_TRUE: { jsonAppendRaw(pOut, "true", 4); break; } case JSON_FALSE: { jsonAppendRaw(pOut, "false", 5); break; } case JSON_STRING: { if( pNode->bRaw ){ jsonAppendString(pOut, pNode->zJContent, pNode->n); break; } /* Fall through into the next case */ } case JSON_REAL: case JSON_INT: { jsonAppendRaw(pOut, pNode->zJContent, pNode->n); break; } case JSON_ARRAY: { jsonAppendChar(pOut, '['); j = 0; while( j<pNode->n ){ if( j>0 ) jsonAppendChar(pOut, ','); j += jsonRenderNode(&pNode[j+1], pOut); } jsonAppendChar(pOut, ']'); break; } case JSON_OBJECT: { jsonAppendChar(pOut, '{'); j = 0; while( j<pNode->n ){ if( j>0 ) jsonAppendChar(pOut, ','); j += jsonRenderNode(&pNode[j+1], pOut); jsonAppendChar(pOut, ':'); j += jsonRenderNode(&pNode[j+1], pOut); } jsonAppendChar(pOut, '}'); break; } } return j+1; } /* ** Make the JsonNode the return value of the function. */ static void jsonReturn(JsonNode *pNode, sqlite3_context *pCtx){ switch( pNode->eType ){ case JSON_NULL: { sqlite3_result_null(pCtx); break; } case JSON_TRUE: { sqlite3_result_int(pCtx, 1); break; } case JSON_FALSE: { sqlite3_result_int(pCtx, 0); break; } /* FIXME: We really want to do text->numeric conversion on these. ** Doing so would be easy if these were internal routines, but the ** necessary interfaces are not exposed for doing it as a loadable ** extension. */ case JSON_REAL: case JSON_INT: { sqlite3_result_text(pCtx, pNode->zJContent, pNode->n, SQLITE_TRANSIENT); break; } case JSON_STRING: { if( pNode->bRaw ){ sqlite3_result_text(pCtx, pNode->zJContent, pNode->n, SQLITE_TRANSIENT); }else{ /* Translate JSON formatted string into raw text */ } break; } case JSON_ARRAY: case JSON_OBJECT: { Json s; jsonInit(&s, pCtx); jsonRenderNode(pNode, &s); jsonResult(&s); break; } } } /* ** Implementation of the json_array(VALUE,...) function. Return a JSON ** array that contains all values given in arguments. Or if any argument ** is a BLOB, throw an error. */ |
︙ | ︙ | |||
434 435 436 437 438 439 440 | } } } jsonAppendRaw(&jx, "]", 1); jsonResult(&jx); } | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | } } } jsonAppendRaw(&jx, "]", 1); jsonResult(&jx); } /* ** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON ** object that contains all name/value given in arguments. Or if any name ** is not a string or if any value is a BLOB, throw an error. */ static void jsonObjectFunc( sqlite3_context *context, |
︙ | ︙ | |||
576 577 578 579 580 581 582 | pParse->oom = 1; return -1; } pParse->nAlloc = nNew; pParse->aNode = pNew; } p = &pParse->aNode[pParse->nNode]; | | > | | 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | pParse->oom = 1; return -1; } pParse->nAlloc = nNew; pParse->aNode = pNew; } p = &pParse->aNode[pParse->nNode]; p->eType = (u8)eType; p->bRaw = 0; p->n = n; p->zJContent = zContent; return pParse->nNode++; } /* ** Parse a single JSON value which begins at pParse->zJson[i]. Return the ** index of the first character past the end of the value parsed. ** |
︙ | ︙ | |||
740 741 742 743 744 745 746 | pParse->nAlloc = 0; return 1; } return 0; } /* | | | | 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 | pParse->nAlloc = 0; return 1; } return 0; } /* ** The json_parse(JSON) function returns a string which describes ** a parse of the JSON provided. Or it returns NULL if JSON is not ** well-formed. */ static void jsonParseFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ Json s; /* Output string - not real JSON */ JsonParse x; /* The parse */ u32 i; |
︙ | ︙ | |||
770 771 772 773 774 775 776 | sqlite3_snprintf(sizeof(zBuf), zBuf, " type: %s\n", azType[x.aNode[i].eType]); jsonAppend(&s, zBuf); if( x.aNode[i].eType>=JSON_INT ){ sqlite3_snprintf(sizeof(zBuf), zBuf, " n: %u\n", x.aNode[i].n); jsonAppend(&s, zBuf); } | | | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > < | > > | 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 | sqlite3_snprintf(sizeof(zBuf), zBuf, " type: %s\n", azType[x.aNode[i].eType]); jsonAppend(&s, zBuf); if( x.aNode[i].eType>=JSON_INT ){ sqlite3_snprintf(sizeof(zBuf), zBuf, " n: %u\n", x.aNode[i].n); jsonAppend(&s, zBuf); } if( x.aNode[i].zJContent!=0 ){ sqlite3_snprintf(sizeof(zBuf), zBuf, " ofst: %u\n", (u32)(x.aNode[i].zJContent - x.zJson)); jsonAppend(&s, zBuf); jsonAppendRaw(&s, " text: ", 8); jsonAppendRaw(&s, x.aNode[i].zJContent, x.aNode[i].n); jsonAppendRaw(&s, "\n", 1); } } sqlite3_free(x.aNode); jsonResult(&s); } /* ** The json_test1(JSON) function parses and rebuilds the JSON string. */ static void jsonTest1Func( sqlite3_context *context, int argc, sqlite3_value **argv ){ JsonParse x; /* The parse */ if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return; jsonReturn(x.aNode, context); sqlite3_free(x.aNode); } /* ** The json_nodecount(JSON) function returns the number of nodes in the ** input JSON string. */ static void jsonNodeCountFunc( sqlite3_context *context, int argc, sqlite3_value **argv ){ JsonParse x; /* The parse */ if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return; sqlite3_result_int64(context, x.nNode); sqlite3_free(x.aNode); } #ifdef _WIN32 __declspec(dllexport) #endif int sqlite3_json_init( sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi ){ int rc = SQLITE_OK; int i; static const struct { const char *zName; int nArg; void (*xFunc)(sqlite3_context*,int,sqlite3_value**); } aFunc[] = { { "json_array", -1, jsonArrayFunc }, { "json_object", -1, jsonObjectFunc }, { "json_parse", 1, jsonParseFunc }, /* DEBUG */ { "json_test1", 1, jsonTest1Func }, /* DEBUG */ { "json_nodecount", 1, jsonNodeCountFunc }, /* DEBUG */ }; SQLITE_EXTENSION_INIT2(pApi); (void)pzErrMsg; /* Unused parameter */ for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){ rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg, SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0, aFunc[i].xFunc, 0, 0); } return rc; } |