Snapshot of upstream SQLite 3.42.0
[sqlcipher.git] / src / json.c
blob8735634c8476dcc1436399b271c13a4dd3859c7b
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.
11 ******************************************************************************
13 ** This SQLite JSON functions.
15 ** This file began as an extension in ext/misc/json1.c in 2015. That
16 ** extension proved so useful that it has now been moved into the core.
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.
21 ** This implementation parses JSON text at 250 MB/s, so it is hard to see
22 ** how JSONB might improve on that.)
24 #ifndef SQLITE_OMIT_JSON
25 #include "sqliteInt.h"
28 ** Growing our own isspace() routine this way is twice as fast as
29 ** the library isspace() function, resulting in a 7% overall performance
30 ** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
32 static const char jsonIsSpace[] = {
33 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
35 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
38 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
39 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50 #define fast_isspace(x) (jsonIsSpace[(unsigned char)x])
52 #if !defined(SQLITE_DEBUG) && !defined(SQLITE_COVERAGE_TEST)
53 # define VVA(X)
54 #else
55 # define VVA(X) X
56 #endif
58 /* Objects */
59 typedef struct JsonString JsonString;
60 typedef struct JsonNode JsonNode;
61 typedef struct JsonParse JsonParse;
63 /* An instance of this object represents a JSON string
64 ** under construction. Really, this is a generic string accumulator
65 ** that can be and is used to create strings other than JSON.
67 struct JsonString {
68 sqlite3_context *pCtx; /* Function context - put error messages here */
69 char *zBuf; /* Append JSON content here */
70 u64 nAlloc; /* Bytes of storage available in zBuf[] */
71 u64 nUsed; /* Bytes of zBuf[] currently used */
72 u8 bStatic; /* True if zBuf is static space */
73 u8 bErr; /* True if an error has been encountered */
74 char zSpace[100]; /* Initial static space */
77 /* JSON type values
79 #define JSON_NULL 0
80 #define JSON_TRUE 1
81 #define JSON_FALSE 2
82 #define JSON_INT 3
83 #define JSON_REAL 4
84 #define JSON_STRING 5
85 #define JSON_ARRAY 6
86 #define JSON_OBJECT 7
88 /* The "subtype" set for JSON values */
89 #define JSON_SUBTYPE 74 /* Ascii for "J" */
92 ** Names of the various JSON types:
94 static const char * const jsonType[] = {
95 "null", "true", "false", "integer", "real", "text", "array", "object"
98 /* Bit values for the JsonNode.jnFlag field
100 #define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
101 #define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
102 #define JNODE_REMOVE 0x04 /* Do not output */
103 #define JNODE_REPLACE 0x08 /* Replace with JsonNode.u.iReplace */
104 #define JNODE_PATCH 0x10 /* Patch with JsonNode.u.pPatch */
105 #define JNODE_APPEND 0x20 /* More ARRAY/OBJECT entries at u.iAppend */
106 #define JNODE_LABEL 0x40 /* Is a label of an object */
107 #define JNODE_JSON5 0x80 /* Node contains JSON5 enhancements */
110 /* A single node of parsed JSON
112 struct JsonNode {
113 u8 eType; /* One of the JSON_ type values */
114 u8 jnFlags; /* JNODE flags */
115 u8 eU; /* Which union element to use */
116 u32 n; /* Bytes of content, or number of sub-nodes */
117 union {
118 const char *zJContent; /* 1: Content for INT, REAL, and STRING */
119 u32 iAppend; /* 2: More terms for ARRAY and OBJECT */
120 u32 iKey; /* 3: Key for ARRAY objects in json_tree() */
121 u32 iReplace; /* 4: Replacement content for JNODE_REPLACE */
122 JsonNode *pPatch; /* 5: Node chain of patch for JNODE_PATCH */
123 } u;
126 /* A completely parsed JSON string
128 struct JsonParse {
129 u32 nNode; /* Number of slots of aNode[] used */
130 u32 nAlloc; /* Number of slots of aNode[] allocated */
131 JsonNode *aNode; /* Array of nodes containing the parse */
132 const char *zJson; /* Original JSON string */
133 u32 *aUp; /* Index of parent of each node */
134 u16 iDepth; /* Nesting depth */
135 u8 nErr; /* Number of errors seen */
136 u8 oom; /* Set to true if out of memory */
137 u8 hasNonstd; /* True if input uses non-standard features like JSON5 */
138 int nJson; /* Length of the zJson string in bytes */
139 u32 iErr; /* Error location in zJson[] */
140 u32 iHold; /* Replace cache line with the lowest iHold value */
144 ** Maximum nesting depth of JSON for this implementation.
146 ** This limit is needed to avoid a stack overflow in the recursive
147 ** descent parser. A depth of 1000 is far deeper than any sane JSON
148 ** should go. Historical note: This limit was 2000 prior to version 3.42.0
150 #define JSON_MAX_DEPTH 1000
152 /**************************************************************************
153 ** Utility routines for dealing with JsonString objects
154 **************************************************************************/
156 /* Set the JsonString object to an empty string
158 static void jsonZero(JsonString *p){
159 p->zBuf = p->zSpace;
160 p->nAlloc = sizeof(p->zSpace);
161 p->nUsed = 0;
162 p->bStatic = 1;
165 /* Initialize the JsonString object
167 static void jsonInit(JsonString *p, sqlite3_context *pCtx){
168 p->pCtx = pCtx;
169 p->bErr = 0;
170 jsonZero(p);
174 /* Free all allocated memory and reset the JsonString object back to its
175 ** initial state.
177 static void jsonReset(JsonString *p){
178 if( !p->bStatic ) sqlite3_free(p->zBuf);
179 jsonZero(p);
183 /* Report an out-of-memory (OOM) condition
185 static void jsonOom(JsonString *p){
186 p->bErr = 1;
187 sqlite3_result_error_nomem(p->pCtx);
188 jsonReset(p);
191 /* Enlarge pJson->zBuf so that it can hold at least N more bytes.
192 ** Return zero on success. Return non-zero on an OOM error
194 static int jsonGrow(JsonString *p, u32 N){
195 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
196 char *zNew;
197 if( p->bStatic ){
198 if( p->bErr ) return 1;
199 zNew = sqlite3_malloc64(nTotal);
200 if( zNew==0 ){
201 jsonOom(p);
202 return SQLITE_NOMEM;
204 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
205 p->zBuf = zNew;
206 p->bStatic = 0;
207 }else{
208 zNew = sqlite3_realloc64(p->zBuf, nTotal);
209 if( zNew==0 ){
210 jsonOom(p);
211 return SQLITE_NOMEM;
213 p->zBuf = zNew;
215 p->nAlloc = nTotal;
216 return SQLITE_OK;
219 /* Append N bytes from zIn onto the end of the JsonString string.
221 static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
222 if( N==0 ) return;
223 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
224 memcpy(p->zBuf+p->nUsed, zIn, N);
225 p->nUsed += N;
228 /* Append formatted text (not to exceed N bytes) to the JsonString.
230 static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
231 va_list ap;
232 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
233 va_start(ap, zFormat);
234 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
235 va_end(ap);
236 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
239 /* Append a single character
241 static void jsonAppendChar(JsonString *p, char c){
242 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
243 p->zBuf[p->nUsed++] = c;
246 /* Append a comma separator to the output buffer, if the previous
247 ** character is not '[' or '{'.
249 static void jsonAppendSeparator(JsonString *p){
250 char c;
251 if( p->nUsed==0 ) return;
252 c = p->zBuf[p->nUsed-1];
253 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
256 /* Append the N-byte string in zIn to the end of the JsonString string
257 ** under construction. Enclose the string in "..." and escape
258 ** any double-quotes or backslash characters contained within the
259 ** string.
261 static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
262 u32 i;
263 if( zIn==0 || ((N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0) ) return;
264 p->zBuf[p->nUsed++] = '"';
265 for(i=0; i<N; i++){
266 unsigned char c = ((unsigned const char*)zIn)[i];
267 if( c=='"' || c=='\\' ){
268 json_simple_escape:
269 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
270 p->zBuf[p->nUsed++] = '\\';
271 }else if( c<=0x1f ){
272 static const char aSpecial[] = {
273 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
274 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
276 assert( sizeof(aSpecial)==32 );
277 assert( aSpecial['\b']=='b' );
278 assert( aSpecial['\f']=='f' );
279 assert( aSpecial['\n']=='n' );
280 assert( aSpecial['\r']=='r' );
281 assert( aSpecial['\t']=='t' );
282 if( aSpecial[c] ){
283 c = aSpecial[c];
284 goto json_simple_escape;
286 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
287 p->zBuf[p->nUsed++] = '\\';
288 p->zBuf[p->nUsed++] = 'u';
289 p->zBuf[p->nUsed++] = '0';
290 p->zBuf[p->nUsed++] = '0';
291 p->zBuf[p->nUsed++] = '0' + (c>>4);
292 c = "0123456789abcdef"[c&0xf];
294 p->zBuf[p->nUsed++] = c;
296 p->zBuf[p->nUsed++] = '"';
297 assert( p->nUsed<p->nAlloc );
301 ** The zIn[0..N] string is a JSON5 string literal. Append to p a translation
302 ** of the string literal that standard JSON and that omits all JSON5
303 ** features.
305 static void jsonAppendNormalizedString(JsonString *p, const char *zIn, u32 N){
306 u32 i;
307 jsonAppendChar(p, '"');
308 zIn++;
309 N -= 2;
310 while( N>0 ){
311 for(i=0; i<N && zIn[i]!='\\'; i++){}
312 if( i>0 ){
313 jsonAppendRaw(p, zIn, i);
314 zIn += i;
315 N -= i;
316 if( N==0 ) break;
318 assert( zIn[0]=='\\' );
319 switch( (u8)zIn[1] ){
320 case '\'':
321 jsonAppendChar(p, '\'');
322 break;
323 case 'v':
324 jsonAppendRaw(p, "\\u0009", 6);
325 break;
326 case 'x':
327 jsonAppendRaw(p, "\\u00", 4);
328 jsonAppendRaw(p, &zIn[2], 2);
329 zIn += 2;
330 N -= 2;
331 break;
332 case '0':
333 jsonAppendRaw(p, "\\u0000", 6);
334 break;
335 case '\r':
336 if( zIn[2]=='\n' ){
337 zIn++;
338 N--;
340 break;
341 case '\n':
342 break;
343 case 0xe2:
344 assert( N>=4 );
345 assert( 0x80==(u8)zIn[2] );
346 assert( 0xa8==(u8)zIn[3] || 0xa9==(u8)zIn[3] );
347 zIn += 2;
348 N -= 2;
349 break;
350 default:
351 jsonAppendRaw(p, zIn, 2);
352 break;
354 zIn += 2;
355 N -= 2;
357 jsonAppendChar(p, '"');
361 ** The zIn[0..N] string is a JSON5 integer literal. Append to p a translation
362 ** of the string literal that standard JSON and that omits all JSON5
363 ** features.
365 static void jsonAppendNormalizedInt(JsonString *p, const char *zIn, u32 N){
366 if( zIn[0]=='+' ){
367 zIn++;
368 N--;
369 }else if( zIn[0]=='-' ){
370 jsonAppendChar(p, '-');
371 zIn++;
372 N--;
374 if( zIn[0]=='0' && (zIn[1]=='x' || zIn[1]=='X') ){
375 sqlite3_int64 i = 0;
376 int rc = sqlite3DecOrHexToI64(zIn, &i);
377 if( rc<=1 ){
378 jsonPrintf(100,p,"%lld",i);
379 }else{
380 assert( rc==2 );
381 jsonAppendRaw(p, "9.0e999", 7);
383 return;
385 jsonAppendRaw(p, zIn, N);
389 ** The zIn[0..N] string is a JSON5 real literal. Append to p a translation
390 ** of the string literal that standard JSON and that omits all JSON5
391 ** features.
393 static void jsonAppendNormalizedReal(JsonString *p, const char *zIn, u32 N){
394 u32 i;
395 if( zIn[0]=='+' ){
396 zIn++;
397 N--;
398 }else if( zIn[0]=='-' ){
399 jsonAppendChar(p, '-');
400 zIn++;
401 N--;
403 if( zIn[0]=='.' ){
404 jsonAppendChar(p, '0');
406 for(i=0; i<N; i++){
407 if( zIn[i]=='.' && (i+1==N || !sqlite3Isdigit(zIn[i+1])) ){
408 i++;
409 jsonAppendRaw(p, zIn, i);
410 zIn += i;
411 N -= i;
412 jsonAppendChar(p, '0');
413 break;
416 if( N>0 ){
417 jsonAppendRaw(p, zIn, N);
424 ** Append a function parameter value to the JSON string under
425 ** construction.
427 static void jsonAppendValue(
428 JsonString *p, /* Append to this JSON string */
429 sqlite3_value *pValue /* Value to append */
431 switch( sqlite3_value_type(pValue) ){
432 case SQLITE_NULL: {
433 jsonAppendRaw(p, "null", 4);
434 break;
436 case SQLITE_FLOAT: {
437 jsonPrintf(100, p, "%!0.15g", sqlite3_value_double(pValue));
438 break;
440 case SQLITE_INTEGER: {
441 const char *z = (const char*)sqlite3_value_text(pValue);
442 u32 n = (u32)sqlite3_value_bytes(pValue);
443 jsonAppendRaw(p, z, n);
444 break;
446 case SQLITE_TEXT: {
447 const char *z = (const char*)sqlite3_value_text(pValue);
448 u32 n = (u32)sqlite3_value_bytes(pValue);
449 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
450 jsonAppendRaw(p, z, n);
451 }else{
452 jsonAppendString(p, z, n);
454 break;
456 default: {
457 if( p->bErr==0 ){
458 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
459 p->bErr = 2;
460 jsonReset(p);
462 break;
468 /* Make the JSON in p the result of the SQL function.
470 static void jsonResult(JsonString *p){
471 if( p->bErr==0 ){
472 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
473 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
474 SQLITE_UTF8);
475 jsonZero(p);
477 assert( p->bStatic );
480 /**************************************************************************
481 ** Utility routines for dealing with JsonNode and JsonParse objects
482 **************************************************************************/
485 ** Return the number of consecutive JsonNode slots need to represent
486 ** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
487 ** OBJECT types, the number might be larger.
489 ** Appended elements are not counted. The value returned is the number
490 ** by which the JsonNode counter should increment in order to go to the
491 ** next peer value.
493 static u32 jsonNodeSize(JsonNode *pNode){
494 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
498 ** Reclaim all memory allocated by a JsonParse object. But do not
499 ** delete the JsonParse object itself.
501 static void jsonParseReset(JsonParse *pParse){
502 sqlite3_free(pParse->aNode);
503 pParse->aNode = 0;
504 pParse->nNode = 0;
505 pParse->nAlloc = 0;
506 sqlite3_free(pParse->aUp);
507 pParse->aUp = 0;
511 ** Free a JsonParse object that was obtained from sqlite3_malloc().
513 static void jsonParseFree(JsonParse *pParse){
514 jsonParseReset(pParse);
515 sqlite3_free(pParse);
519 ** Convert the JsonNode pNode into a pure JSON string and
520 ** append to pOut. Subsubstructure is also included. Return
521 ** the number of JsonNode objects that are encoded.
523 static void jsonRenderNode(
524 JsonNode *pNode, /* The node to render */
525 JsonString *pOut, /* Write JSON here */
526 sqlite3_value **aReplace /* Replacement values */
528 assert( pNode!=0 );
529 if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
530 if( (pNode->jnFlags & JNODE_REPLACE)!=0 && ALWAYS(aReplace!=0) ){
531 assert( pNode->eU==4 );
532 jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
533 return;
535 assert( pNode->eU==5 );
536 pNode = pNode->u.pPatch;
538 switch( pNode->eType ){
539 default: {
540 assert( pNode->eType==JSON_NULL );
541 jsonAppendRaw(pOut, "null", 4);
542 break;
544 case JSON_TRUE: {
545 jsonAppendRaw(pOut, "true", 4);
546 break;
548 case JSON_FALSE: {
549 jsonAppendRaw(pOut, "false", 5);
550 break;
552 case JSON_STRING: {
553 assert( pNode->eU==1 );
554 if( pNode->jnFlags & JNODE_RAW ){
555 if( pNode->jnFlags & JNODE_LABEL ){
556 jsonAppendChar(pOut, '"');
557 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
558 jsonAppendChar(pOut, '"');
559 }else{
560 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
562 }else if( pNode->jnFlags & JNODE_JSON5 ){
563 jsonAppendNormalizedString(pOut, pNode->u.zJContent, pNode->n);
564 }else{
565 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
567 break;
569 case JSON_REAL: {
570 assert( pNode->eU==1 );
571 if( pNode->jnFlags & JNODE_JSON5 ){
572 jsonAppendNormalizedReal(pOut, pNode->u.zJContent, pNode->n);
573 }else{
574 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
576 break;
578 case JSON_INT: {
579 assert( pNode->eU==1 );
580 if( pNode->jnFlags & JNODE_JSON5 ){
581 jsonAppendNormalizedInt(pOut, pNode->u.zJContent, pNode->n);
582 }else{
583 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
585 break;
587 case JSON_ARRAY: {
588 u32 j = 1;
589 jsonAppendChar(pOut, '[');
590 for(;;){
591 while( j<=pNode->n ){
592 if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
593 jsonAppendSeparator(pOut);
594 jsonRenderNode(&pNode[j], pOut, aReplace);
596 j += jsonNodeSize(&pNode[j]);
598 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
599 assert( pNode->eU==2 );
600 pNode = &pNode[pNode->u.iAppend];
601 j = 1;
603 jsonAppendChar(pOut, ']');
604 break;
606 case JSON_OBJECT: {
607 u32 j = 1;
608 jsonAppendChar(pOut, '{');
609 for(;;){
610 while( j<=pNode->n ){
611 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
612 jsonAppendSeparator(pOut);
613 jsonRenderNode(&pNode[j], pOut, aReplace);
614 jsonAppendChar(pOut, ':');
615 jsonRenderNode(&pNode[j+1], pOut, aReplace);
617 j += 1 + jsonNodeSize(&pNode[j+1]);
619 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
620 assert( pNode->eU==2 );
621 pNode = &pNode[pNode->u.iAppend];
622 j = 1;
624 jsonAppendChar(pOut, '}');
625 break;
631 ** Return a JsonNode and all its descendents as a JSON string.
633 static void jsonReturnJson(
634 JsonNode *pNode, /* Node to return */
635 sqlite3_context *pCtx, /* Return value for this function */
636 sqlite3_value **aReplace /* Array of replacement values */
638 JsonString s;
639 jsonInit(&s, pCtx);
640 jsonRenderNode(pNode, &s, aReplace);
641 jsonResult(&s);
642 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
646 ** Translate a single byte of Hex into an integer.
647 ** This routine only works if h really is a valid hexadecimal
648 ** character: 0..9a..fA..F
650 static u8 jsonHexToInt(int h){
651 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
652 #ifdef SQLITE_EBCDIC
653 h += 9*(1&~(h>>4));
654 #else
655 h += 9*(1&(h>>6));
656 #endif
657 return (u8)(h & 0xf);
661 ** Convert a 4-byte hex string into an integer
663 static u32 jsonHexToInt4(const char *z){
664 u32 v;
665 assert( sqlite3Isxdigit(z[0]) );
666 assert( sqlite3Isxdigit(z[1]) );
667 assert( sqlite3Isxdigit(z[2]) );
668 assert( sqlite3Isxdigit(z[3]) );
669 v = (jsonHexToInt(z[0])<<12)
670 + (jsonHexToInt(z[1])<<8)
671 + (jsonHexToInt(z[2])<<4)
672 + jsonHexToInt(z[3]);
673 return v;
677 ** Make the JsonNode the return value of the function.
679 static void jsonReturn(
680 JsonNode *pNode, /* Node to return */
681 sqlite3_context *pCtx, /* Return value for this function */
682 sqlite3_value **aReplace /* Array of replacement values */
684 switch( pNode->eType ){
685 default: {
686 assert( pNode->eType==JSON_NULL );
687 sqlite3_result_null(pCtx);
688 break;
690 case JSON_TRUE: {
691 sqlite3_result_int(pCtx, 1);
692 break;
694 case JSON_FALSE: {
695 sqlite3_result_int(pCtx, 0);
696 break;
698 case JSON_INT: {
699 sqlite3_int64 i = 0;
700 int rc;
701 int bNeg = 0;
702 const char *z;
705 assert( pNode->eU==1 );
706 z = pNode->u.zJContent;
707 if( z[0]=='-' ){ z++; bNeg = 1; }
708 else if( z[0]=='+' ){ z++; }
709 rc = sqlite3DecOrHexToI64(z, &i);
710 if( rc<=1 ){
711 sqlite3_result_int64(pCtx, bNeg ? -i : i);
712 }else if( rc==3 && bNeg ){
713 sqlite3_result_int64(pCtx, SMALLEST_INT64);
714 }else{
715 goto to_double;
717 break;
719 case JSON_REAL: {
720 double r;
721 const char *z;
722 assert( pNode->eU==1 );
723 to_double:
724 z = pNode->u.zJContent;
725 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
726 sqlite3_result_double(pCtx, r);
727 break;
729 case JSON_STRING: {
730 if( pNode->jnFlags & JNODE_RAW ){
731 assert( pNode->eU==1 );
732 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
733 SQLITE_TRANSIENT);
734 }else if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
735 /* JSON formatted without any backslash-escapes */
736 assert( pNode->eU==1 );
737 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
738 SQLITE_TRANSIENT);
739 }else{
740 /* Translate JSON formatted string into raw text */
741 u32 i;
742 u32 n = pNode->n;
743 const char *z;
744 char *zOut;
745 u32 j;
746 u32 nOut = n;
747 assert( pNode->eU==1 );
748 z = pNode->u.zJContent;
749 zOut = sqlite3_malloc( nOut+1 );
750 if( zOut==0 ){
751 sqlite3_result_error_nomem(pCtx);
752 break;
754 for(i=1, j=0; i<n-1; i++){
755 char c = z[i];
756 if( c=='\\' ){
757 c = z[++i];
758 if( c=='u' ){
759 u32 v = jsonHexToInt4(z+i+1);
760 i += 4;
761 if( v==0 ) break;
762 if( v<=0x7f ){
763 zOut[j++] = (char)v;
764 }else if( v<=0x7ff ){
765 zOut[j++] = (char)(0xc0 | (v>>6));
766 zOut[j++] = 0x80 | (v&0x3f);
767 }else{
768 u32 vlo;
769 if( (v&0xfc00)==0xd800
770 && i<n-6
771 && z[i+1]=='\\'
772 && z[i+2]=='u'
773 && ((vlo = jsonHexToInt4(z+i+3))&0xfc00)==0xdc00
775 /* We have a surrogate pair */
776 v = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
777 i += 6;
778 zOut[j++] = 0xf0 | (v>>18);
779 zOut[j++] = 0x80 | ((v>>12)&0x3f);
780 zOut[j++] = 0x80 | ((v>>6)&0x3f);
781 zOut[j++] = 0x80 | (v&0x3f);
782 }else{
783 zOut[j++] = 0xe0 | (v>>12);
784 zOut[j++] = 0x80 | ((v>>6)&0x3f);
785 zOut[j++] = 0x80 | (v&0x3f);
788 continue;
789 }else if( c=='b' ){
790 c = '\b';
791 }else if( c=='f' ){
792 c = '\f';
793 }else if( c=='n' ){
794 c = '\n';
795 }else if( c=='r' ){
796 c = '\r';
797 }else if( c=='t' ){
798 c = '\t';
799 }else if( c=='v' ){
800 c = '\v';
801 }else if( c=='\'' || c=='"' || c=='/' || c=='\\' ){
802 /* pass through unchanged */
803 }else if( c=='0' ){
804 c = 0;
805 }else if( c=='x' ){
806 c = (jsonHexToInt(z[i+1])<<4) | jsonHexToInt(z[i+2]);
807 i += 2;
808 }else if( c=='\r' && z[i+1]=='\n' ){
809 i++;
810 continue;
811 }else if( 0xe2==(u8)c ){
812 assert( 0x80==(u8)z[i+1] );
813 assert( 0xa8==(u8)z[i+2] || 0xa9==(u8)z[i+2] );
814 i += 2;
815 continue;
816 }else{
817 continue;
819 } /* end if( c=='\\' ) */
820 zOut[j++] = c;
821 } /* end for() */
822 zOut[j] = 0;
823 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
825 break;
827 case JSON_ARRAY:
828 case JSON_OBJECT: {
829 jsonReturnJson(pNode, pCtx, aReplace);
830 break;
835 /* Forward reference */
836 static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
839 ** A macro to hint to the compiler that a function should not be
840 ** inlined.
842 #if defined(__GNUC__)
843 # define JSON_NOINLINE __attribute__((noinline))
844 #elif defined(_MSC_VER) && _MSC_VER>=1310
845 # define JSON_NOINLINE __declspec(noinline)
846 #else
847 # define JSON_NOINLINE
848 #endif
851 static JSON_NOINLINE int jsonParseAddNodeExpand(
852 JsonParse *pParse, /* Append the node to this object */
853 u32 eType, /* Node type */
854 u32 n, /* Content size or sub-node count */
855 const char *zContent /* Content */
857 u32 nNew;
858 JsonNode *pNew;
859 assert( pParse->nNode>=pParse->nAlloc );
860 if( pParse->oom ) return -1;
861 nNew = pParse->nAlloc*2 + 10;
862 pNew = sqlite3_realloc64(pParse->aNode, sizeof(JsonNode)*nNew);
863 if( pNew==0 ){
864 pParse->oom = 1;
865 return -1;
867 pParse->nAlloc = nNew;
868 pParse->aNode = pNew;
869 assert( pParse->nNode<pParse->nAlloc );
870 return jsonParseAddNode(pParse, eType, n, zContent);
874 ** Create a new JsonNode instance based on the arguments and append that
875 ** instance to the JsonParse. Return the index in pParse->aNode[] of the
876 ** new node, or -1 if a memory allocation fails.
878 static int jsonParseAddNode(
879 JsonParse *pParse, /* Append the node to this object */
880 u32 eType, /* Node type */
881 u32 n, /* Content size or sub-node count */
882 const char *zContent /* Content */
884 JsonNode *p;
885 if( pParse->aNode==0 || pParse->nNode>=pParse->nAlloc ){
886 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
888 p = &pParse->aNode[pParse->nNode];
889 p->eType = (u8)(eType & 0xff);
890 p->jnFlags = (u8)(eType >> 8);
891 VVA( p->eU = zContent ? 1 : 0 );
892 p->n = n;
893 p->u.zJContent = zContent;
894 return pParse->nNode++;
898 ** Return true if z[] begins with 2 (or more) hexadecimal digits
900 static int jsonIs2Hex(const char *z){
901 return sqlite3Isxdigit(z[0]) && sqlite3Isxdigit(z[1]);
905 ** Return true if z[] begins with 4 (or more) hexadecimal digits
907 static int jsonIs4Hex(const char *z){
908 return jsonIs2Hex(z) && jsonIs2Hex(&z[2]);
912 ** Return the number of bytes of JSON5 whitespace at the beginning of
913 ** the input string z[].
915 ** JSON5 whitespace consists of any of the following characters:
917 ** Unicode UTF-8 Name
918 ** U+0009 09 horizontal tab
919 ** U+000a 0a line feed
920 ** U+000b 0b vertical tab
921 ** U+000c 0c form feed
922 ** U+000d 0d carriage return
923 ** U+0020 20 space
924 ** U+00a0 c2 a0 non-breaking space
925 ** U+1680 e1 9a 80 ogham space mark
926 ** U+2000 e2 80 80 en quad
927 ** U+2001 e2 80 81 em quad
928 ** U+2002 e2 80 82 en space
929 ** U+2003 e2 80 83 em space
930 ** U+2004 e2 80 84 three-per-em space
931 ** U+2005 e2 80 85 four-per-em space
932 ** U+2006 e2 80 86 six-per-em space
933 ** U+2007 e2 80 87 figure space
934 ** U+2008 e2 80 88 punctuation space
935 ** U+2009 e2 80 89 thin space
936 ** U+200a e2 80 8a hair space
937 ** U+2028 e2 80 a8 line separator
938 ** U+2029 e2 80 a9 paragraph separator
939 ** U+202f e2 80 af narrow no-break space (NNBSP)
940 ** U+205f e2 81 9f medium mathematical space (MMSP)
941 ** U+3000 e3 80 80 ideographical space
942 ** U+FEFF ef bb bf byte order mark
944 ** In addition, comments between '/', '*' and '*', '/' and
945 ** from '/', '/' to end-of-line are also considered to be whitespace.
947 static int json5Whitespace(const char *zIn){
948 int n = 0;
949 const u8 *z = (u8*)zIn;
950 while( 1 /*exit by "goto whitespace_done"*/ ){
951 switch( z[n] ){
952 case 0x09:
953 case 0x0a:
954 case 0x0b:
955 case 0x0c:
956 case 0x0d:
957 case 0x20: {
958 n++;
959 break;
961 case '/': {
962 if( z[n+1]=='*' && z[n+2]!=0 ){
963 int j;
964 for(j=n+3; z[j]!='/' || z[j-1]!='*'; j++){
965 if( z[j]==0 ) goto whitespace_done;
967 n = j+1;
968 break;
969 }else if( z[n+1]=='/' ){
970 int j;
971 char c;
972 for(j=n+2; (c = z[j])!=0; j++){
973 if( c=='\n' || c=='\r' ) break;
974 if( 0xe2==(u8)c && 0x80==(u8)z[j+1]
975 && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2])
977 j += 2;
978 break;
981 n = j;
982 if( z[n] ) n++;
983 break;
985 goto whitespace_done;
987 case 0xc2: {
988 if( z[n+1]==0xa0 ){
989 n += 2;
990 break;
992 goto whitespace_done;
994 case 0xe1: {
995 if( z[n+1]==0x9a && z[n+2]==0x80 ){
996 n += 3;
997 break;
999 goto whitespace_done;
1001 case 0xe2: {
1002 if( z[n+1]==0x80 ){
1003 u8 c = z[n+2];
1004 if( c<0x80 ) goto whitespace_done;
1005 if( c<=0x8a || c==0xa8 || c==0xa9 || c==0xaf ){
1006 n += 3;
1007 break;
1009 }else if( z[n+1]==0x81 && z[n+2]==0x9f ){
1010 n += 3;
1011 break;
1013 goto whitespace_done;
1015 case 0xe3: {
1016 if( z[n+1]==0x80 && z[n+2]==0x80 ){
1017 n += 3;
1018 break;
1020 goto whitespace_done;
1022 case 0xef: {
1023 if( z[n+1]==0xbb && z[n+2]==0xbf ){
1024 n += 3;
1025 break;
1027 goto whitespace_done;
1029 default: {
1030 goto whitespace_done;
1034 whitespace_done:
1035 return n;
1039 ** Extra floating-point literals to allow in JSON.
1041 static const struct NanInfName {
1042 char c1;
1043 char c2;
1044 char n;
1045 char eType;
1046 char nRepl;
1047 char *zMatch;
1048 char *zRepl;
1049 } aNanInfName[] = {
1050 { 'i', 'I', 3, JSON_REAL, 7, "inf", "9.0e999" },
1051 { 'i', 'I', 8, JSON_REAL, 7, "infinity", "9.0e999" },
1052 { 'n', 'N', 3, JSON_NULL, 4, "NaN", "null" },
1053 { 'q', 'Q', 4, JSON_NULL, 4, "QNaN", "null" },
1054 { 's', 'S', 4, JSON_NULL, 4, "SNaN", "null" },
1058 ** Parse a single JSON value which begins at pParse->zJson[i]. Return the
1059 ** index of the first character past the end of the value parsed.
1061 ** Special return values:
1063 ** 0 End if input
1064 ** -1 Syntax error
1065 ** -2 '}' seen
1066 ** -3 ']' seen
1067 ** -4 ',' seen
1068 ** -5 ':' seen
1070 static int jsonParseValue(JsonParse *pParse, u32 i){
1071 char c;
1072 u32 j;
1073 int iThis;
1074 int x;
1075 JsonNode *pNode;
1076 const char *z = pParse->zJson;
1077 json_parse_restart:
1078 switch( (u8)z[i] ){
1079 case '{': {
1080 /* Parse object */
1081 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
1082 if( iThis<0 ) return -1;
1083 if( ++pParse->iDepth > JSON_MAX_DEPTH ){
1084 pParse->iErr = i;
1085 return -1;
1087 for(j=i+1;;j++){
1088 u32 nNode = pParse->nNode;
1089 x = jsonParseValue(pParse, j);
1090 if( x<=0 ){
1091 if( x==(-2) ){
1092 j = pParse->iErr;
1093 if( pParse->nNode!=(u32)iThis+1 ) pParse->hasNonstd = 1;
1094 break;
1096 j += json5Whitespace(&z[j]);
1097 if( sqlite3JsonId1(z[j])
1098 || (z[j]=='\\' && z[j+1]=='u' && jsonIs4Hex(&z[j+2]))
1100 int k = j+1;
1101 while( (sqlite3JsonId2(z[k]) && json5Whitespace(&z[k])==0)
1102 || (z[k]=='\\' && z[k+1]=='u' && jsonIs4Hex(&z[k+2]))
1104 k++;
1106 jsonParseAddNode(pParse, JSON_STRING | (JNODE_RAW<<8), k-j, &z[j]);
1107 pParse->hasNonstd = 1;
1108 x = k;
1109 }else{
1110 if( x!=-1 ) pParse->iErr = j;
1111 return -1;
1114 if( pParse->oom ) return -1;
1115 pNode = &pParse->aNode[nNode];
1116 if( pNode->eType!=JSON_STRING ){
1117 pParse->iErr = j;
1118 return -1;
1120 pNode->jnFlags |= JNODE_LABEL;
1121 j = x;
1122 if( z[j]==':' ){
1123 j++;
1124 }else{
1125 if( fast_isspace(z[j]) ){
1126 do{ j++; }while( fast_isspace(z[j]) );
1127 if( z[j]==':' ){
1128 j++;
1129 goto parse_object_value;
1132 x = jsonParseValue(pParse, j);
1133 if( x!=(-5) ){
1134 if( x!=(-1) ) pParse->iErr = j;
1135 return -1;
1137 j = pParse->iErr+1;
1139 parse_object_value:
1140 x = jsonParseValue(pParse, j);
1141 if( x<=0 ){
1142 if( x!=(-1) ) pParse->iErr = j;
1143 return -1;
1145 j = x;
1146 if( z[j]==',' ){
1147 continue;
1148 }else if( z[j]=='}' ){
1149 break;
1150 }else{
1151 if( fast_isspace(z[j]) ){
1152 do{ j++; }while( fast_isspace(z[j]) );
1153 if( z[j]==',' ){
1154 continue;
1155 }else if( z[j]=='}' ){
1156 break;
1159 x = jsonParseValue(pParse, j);
1160 if( x==(-4) ){
1161 j = pParse->iErr;
1162 continue;
1164 if( x==(-2) ){
1165 j = pParse->iErr;
1166 break;
1169 pParse->iErr = j;
1170 return -1;
1172 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
1173 pParse->iDepth--;
1174 return j+1;
1176 case '[': {
1177 /* Parse array */
1178 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
1179 if( iThis<0 ) return -1;
1180 if( ++pParse->iDepth > JSON_MAX_DEPTH ){
1181 pParse->iErr = i;
1182 return -1;
1184 memset(&pParse->aNode[iThis].u, 0, sizeof(pParse->aNode[iThis].u));
1185 for(j=i+1;;j++){
1186 x = jsonParseValue(pParse, j);
1187 if( x<=0 ){
1188 if( x==(-3) ){
1189 j = pParse->iErr;
1190 if( pParse->nNode!=(u32)iThis+1 ) pParse->hasNonstd = 1;
1191 break;
1193 if( x!=(-1) ) pParse->iErr = j;
1194 return -1;
1196 j = x;
1197 if( z[j]==',' ){
1198 continue;
1199 }else if( z[j]==']' ){
1200 break;
1201 }else{
1202 if( fast_isspace(z[j]) ){
1203 do{ j++; }while( fast_isspace(z[j]) );
1204 if( z[j]==',' ){
1205 continue;
1206 }else if( z[j]==']' ){
1207 break;
1210 x = jsonParseValue(pParse, j);
1211 if( x==(-4) ){
1212 j = pParse->iErr;
1213 continue;
1215 if( x==(-3) ){
1216 j = pParse->iErr;
1217 break;
1220 pParse->iErr = j;
1221 return -1;
1223 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
1224 pParse->iDepth--;
1225 return j+1;
1227 case '\'': {
1228 u8 jnFlags;
1229 char cDelim;
1230 pParse->hasNonstd = 1;
1231 jnFlags = JNODE_JSON5;
1232 goto parse_string;
1233 case '"':
1234 /* Parse string */
1235 jnFlags = 0;
1236 parse_string:
1237 cDelim = z[i];
1238 j = i+1;
1239 for(;;){
1240 c = z[j];
1241 if( (c & ~0x1f)==0 ){
1242 /* Control characters are not allowed in strings */
1243 pParse->iErr = j;
1244 return -1;
1246 if( c=='\\' ){
1247 c = z[++j];
1248 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
1249 || c=='n' || c=='r' || c=='t'
1250 || (c=='u' && jsonIs4Hex(&z[j+1])) ){
1251 jnFlags |= JNODE_ESCAPE;
1252 }else if( c=='\'' || c=='0' || c=='v' || c=='\n'
1253 || (0xe2==(u8)c && 0x80==(u8)z[j+1]
1254 && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2]))
1255 || (c=='x' && jsonIs2Hex(&z[j+1])) ){
1256 jnFlags |= (JNODE_ESCAPE|JNODE_JSON5);
1257 pParse->hasNonstd = 1;
1258 }else if( c=='\r' ){
1259 if( z[j+1]=='\n' ) j++;
1260 jnFlags |= (JNODE_ESCAPE|JNODE_JSON5);
1261 pParse->hasNonstd = 1;
1262 }else{
1263 pParse->iErr = j;
1264 return -1;
1266 }else if( c==cDelim ){
1267 break;
1269 j++;
1271 jsonParseAddNode(pParse, JSON_STRING | (jnFlags<<8), j+1-i, &z[i]);
1272 return j+1;
1274 case 't': {
1275 if( strncmp(z+i,"true",4)==0 && !sqlite3Isalnum(z[i+4]) ){
1276 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
1277 return i+4;
1279 pParse->iErr = i;
1280 return -1;
1282 case 'f': {
1283 if( strncmp(z+i,"false",5)==0 && !sqlite3Isalnum(z[i+5]) ){
1284 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
1285 return i+5;
1287 pParse->iErr = i;
1288 return -1;
1290 case '+': {
1291 u8 seenDP, seenE, jnFlags;
1292 pParse->hasNonstd = 1;
1293 jnFlags = JNODE_JSON5;
1294 goto parse_number;
1295 case '.':
1296 if( sqlite3Isdigit(z[i+1]) ){
1297 pParse->hasNonstd = 1;
1298 jnFlags = JNODE_JSON5;
1299 seenE = 0;
1300 seenDP = JSON_REAL;
1301 goto parse_number_2;
1303 pParse->iErr = i;
1304 return -1;
1305 case '-':
1306 case '0':
1307 case '1':
1308 case '2':
1309 case '3':
1310 case '4':
1311 case '5':
1312 case '6':
1313 case '7':
1314 case '8':
1315 case '9':
1316 /* Parse number */
1317 jnFlags = 0;
1318 parse_number:
1319 seenDP = JSON_INT;
1320 seenE = 0;
1321 assert( '-' < '0' );
1322 assert( '+' < '0' );
1323 assert( '.' < '0' );
1324 c = z[i];
1326 if( c<='0' ){
1327 if( c=='0' ){
1328 if( (z[i+1]=='x' || z[i+1]=='X') && sqlite3Isxdigit(z[i+2]) ){
1329 assert( seenDP==JSON_INT );
1330 pParse->hasNonstd = 1;
1331 jnFlags |= JNODE_JSON5;
1332 for(j=i+3; sqlite3Isxdigit(z[j]); j++){}
1333 goto parse_number_finish;
1334 }else if( sqlite3Isdigit(z[i+1]) ){
1335 pParse->iErr = i+1;
1336 return -1;
1338 }else{
1339 if( !sqlite3Isdigit(z[i+1]) ){
1340 /* JSON5 allows for "+Infinity" and "-Infinity" using exactly
1341 ** that case. SQLite also allows these in any case and it allows
1342 ** "+inf" and "-inf". */
1343 if( (z[i+1]=='I' || z[i+1]=='i')
1344 && sqlite3StrNICmp(&z[i+1], "inf",3)==0
1346 pParse->hasNonstd = 1;
1347 if( z[i]=='-' ){
1348 jsonParseAddNode(pParse, JSON_REAL, 8, "-9.0e999");
1349 }else{
1350 jsonParseAddNode(pParse, JSON_REAL, 7, "9.0e999");
1352 return i + (sqlite3StrNICmp(&z[i+4],"inity",5)==0 ? 9 : 4);
1354 if( z[i+1]=='.' ){
1355 pParse->hasNonstd = 1;
1356 jnFlags |= JNODE_JSON5;
1357 goto parse_number_2;
1359 pParse->iErr = i;
1360 return -1;
1362 if( z[i+1]=='0' ){
1363 if( sqlite3Isdigit(z[i+2]) ){
1364 pParse->iErr = i+1;
1365 return -1;
1366 }else if( (z[i+2]=='x' || z[i+2]=='X') && sqlite3Isxdigit(z[i+3]) ){
1367 pParse->hasNonstd = 1;
1368 jnFlags |= JNODE_JSON5;
1369 for(j=i+4; sqlite3Isxdigit(z[j]); j++){}
1370 goto parse_number_finish;
1375 parse_number_2:
1376 for(j=i+1;; j++){
1377 c = z[j];
1378 if( sqlite3Isdigit(c) ) continue;
1379 if( c=='.' ){
1380 if( seenDP==JSON_REAL ){
1381 pParse->iErr = j;
1382 return -1;
1384 seenDP = JSON_REAL;
1385 continue;
1387 if( c=='e' || c=='E' ){
1388 if( z[j-1]<'0' ){
1389 if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){
1390 pParse->hasNonstd = 1;
1391 jnFlags |= JNODE_JSON5;
1392 }else{
1393 pParse->iErr = j;
1394 return -1;
1397 if( seenE ){
1398 pParse->iErr = j;
1399 return -1;
1401 seenDP = JSON_REAL;
1402 seenE = 1;
1403 c = z[j+1];
1404 if( c=='+' || c=='-' ){
1405 j++;
1406 c = z[j+1];
1408 if( c<'0' || c>'9' ){
1409 pParse->iErr = j;
1410 return -1;
1412 continue;
1414 break;
1416 if( z[j-1]<'0' ){
1417 if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){
1418 pParse->hasNonstd = 1;
1419 jnFlags |= JNODE_JSON5;
1420 }else{
1421 pParse->iErr = j;
1422 return -1;
1425 parse_number_finish:
1426 jsonParseAddNode(pParse, seenDP | (jnFlags<<8), j - i, &z[i]);
1427 return j;
1429 case '}': {
1430 pParse->iErr = i;
1431 return -2; /* End of {...} */
1433 case ']': {
1434 pParse->iErr = i;
1435 return -3; /* End of [...] */
1437 case ',': {
1438 pParse->iErr = i;
1439 return -4; /* List separator */
1441 case ':': {
1442 pParse->iErr = i;
1443 return -5; /* Object label/value separator */
1445 case 0: {
1446 return 0; /* End of file */
1448 case 0x09:
1449 case 0x0a:
1450 case 0x0d:
1451 case 0x20: {
1453 i++;
1454 }while( fast_isspace(z[i]) );
1455 goto json_parse_restart;
1457 case 0x0b:
1458 case 0x0c:
1459 case '/':
1460 case 0xc2:
1461 case 0xe1:
1462 case 0xe2:
1463 case 0xe3:
1464 case 0xef: {
1465 j = json5Whitespace(&z[i]);
1466 if( j>0 ){
1467 i += j;
1468 pParse->hasNonstd = 1;
1469 goto json_parse_restart;
1471 pParse->iErr = i;
1472 return -1;
1474 case 'n': {
1475 if( strncmp(z+i,"null",4)==0 && !sqlite3Isalnum(z[i+4]) ){
1476 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
1477 return i+4;
1479 /* fall-through into the default case that checks for NaN */
1481 default: {
1482 u32 k;
1483 int nn;
1484 c = z[i];
1485 for(k=0; k<sizeof(aNanInfName)/sizeof(aNanInfName[0]); k++){
1486 if( c!=aNanInfName[k].c1 && c!=aNanInfName[k].c2 ) continue;
1487 nn = aNanInfName[k].n;
1488 if( sqlite3StrNICmp(&z[i], aNanInfName[k].zMatch, nn)!=0 ){
1489 continue;
1491 if( sqlite3Isalnum(z[i+nn]) ) continue;
1492 jsonParseAddNode(pParse, aNanInfName[k].eType,
1493 aNanInfName[k].nRepl, aNanInfName[k].zRepl);
1494 pParse->hasNonstd = 1;
1495 return i + nn;
1497 pParse->iErr = i;
1498 return -1; /* Syntax error */
1500 } /* End switch(z[i]) */
1504 ** Parse a complete JSON string. Return 0 on success or non-zero if there
1505 ** are any errors. If an error occurs, free all memory associated with
1506 ** pParse.
1508 ** pParse is uninitialized when this routine is called.
1510 static int jsonParse(
1511 JsonParse *pParse, /* Initialize and fill this JsonParse object */
1512 sqlite3_context *pCtx, /* Report errors here */
1513 const char *zJson /* Input JSON text to be parsed */
1515 int i;
1516 memset(pParse, 0, sizeof(*pParse));
1517 if( zJson==0 ) return 1;
1518 pParse->zJson = zJson;
1519 i = jsonParseValue(pParse, 0);
1520 if( pParse->oom ) i = -1;
1521 if( i>0 ){
1522 assert( pParse->iDepth==0 );
1523 while( fast_isspace(zJson[i]) ) i++;
1524 if( zJson[i] ){
1525 i += json5Whitespace(&zJson[i]);
1526 if( zJson[i] ){
1527 jsonParseReset(pParse);
1528 return 1;
1530 pParse->hasNonstd = 1;
1533 if( i<=0 ){
1534 if( pCtx!=0 ){
1535 if( pParse->oom ){
1536 sqlite3_result_error_nomem(pCtx);
1537 }else{
1538 sqlite3_result_error(pCtx, "malformed JSON", -1);
1541 jsonParseReset(pParse);
1542 return 1;
1544 return 0;
1547 /* Mark node i of pParse as being a child of iParent. Call recursively
1548 ** to fill in all the descendants of node i.
1550 static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
1551 JsonNode *pNode = &pParse->aNode[i];
1552 u32 j;
1553 pParse->aUp[i] = iParent;
1554 switch( pNode->eType ){
1555 case JSON_ARRAY: {
1556 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
1557 jsonParseFillInParentage(pParse, i+j, i);
1559 break;
1561 case JSON_OBJECT: {
1562 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
1563 pParse->aUp[i+j] = i;
1564 jsonParseFillInParentage(pParse, i+j+1, i);
1566 break;
1568 default: {
1569 break;
1575 ** Compute the parentage of all nodes in a completed parse.
1577 static int jsonParseFindParents(JsonParse *pParse){
1578 u32 *aUp;
1579 assert( pParse->aUp==0 );
1580 aUp = pParse->aUp = sqlite3_malloc64( sizeof(u32)*pParse->nNode );
1581 if( aUp==0 ){
1582 pParse->oom = 1;
1583 return SQLITE_NOMEM;
1585 jsonParseFillInParentage(pParse, 0, 0);
1586 return SQLITE_OK;
1590 ** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
1592 #define JSON_CACHE_ID (-429938) /* First cache entry */
1593 #define JSON_CACHE_SZ 4 /* Max number of cache entries */
1596 ** Obtain a complete parse of the JSON found in the first argument
1597 ** of the argv array. Use the sqlite3_get_auxdata() cache for this
1598 ** parse if it is available. If the cache is not available or if it
1599 ** is no longer valid, parse the JSON again and return the new parse,
1600 ** and also register the new parse so that it will be available for
1601 ** future sqlite3_get_auxdata() calls.
1603 ** If an error occurs and pErrCtx!=0 then report the error on pErrCtx
1604 ** and return NULL.
1606 ** If an error occurs and pErrCtx==0 then return the Parse object with
1607 ** JsonParse.nErr non-zero. If the caller invokes this routine with
1608 ** pErrCtx==0 and it gets back a JsonParse with nErr!=0, then the caller
1609 ** is responsible for invoking jsonParseFree() on the returned value.
1610 ** But the caller may invoke jsonParseFree() *only* if pParse->nErr!=0.
1612 static JsonParse *jsonParseCached(
1613 sqlite3_context *pCtx,
1614 sqlite3_value **argv,
1615 sqlite3_context *pErrCtx
1617 const char *zJson = (const char*)sqlite3_value_text(argv[0]);
1618 int nJson = sqlite3_value_bytes(argv[0]);
1619 JsonParse *p;
1620 JsonParse *pMatch = 0;
1621 int iKey;
1622 int iMinKey = 0;
1623 u32 iMinHold = 0xffffffff;
1624 u32 iMaxHold = 0;
1625 if( zJson==0 ) return 0;
1626 for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
1627 p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey);
1628 if( p==0 ){
1629 iMinKey = iKey;
1630 break;
1632 if( pMatch==0
1633 && p->nJson==nJson
1634 && memcmp(p->zJson,zJson,nJson)==0
1636 p->nErr = 0;
1637 pMatch = p;
1638 }else if( p->iHold<iMinHold ){
1639 iMinHold = p->iHold;
1640 iMinKey = iKey;
1642 if( p->iHold>iMaxHold ){
1643 iMaxHold = p->iHold;
1646 if( pMatch ){
1647 pMatch->nErr = 0;
1648 pMatch->iHold = iMaxHold+1;
1649 return pMatch;
1651 p = sqlite3_malloc64( sizeof(*p) + nJson + 1 );
1652 if( p==0 ){
1653 sqlite3_result_error_nomem(pCtx);
1654 return 0;
1656 memset(p, 0, sizeof(*p));
1657 p->zJson = (char*)&p[1];
1658 memcpy((char*)p->zJson, zJson, nJson+1);
1659 if( jsonParse(p, pErrCtx, p->zJson) ){
1660 if( pErrCtx==0 ){
1661 p->nErr = 1;
1662 return p;
1664 sqlite3_free(p);
1665 return 0;
1667 p->nJson = nJson;
1668 p->iHold = iMaxHold+1;
1669 sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, p,
1670 (void(*)(void*))jsonParseFree);
1671 return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey);
1675 ** Compare the OBJECT label at pNode against zKey,nKey. Return true on
1676 ** a match.
1678 static int jsonLabelCompare(const JsonNode *pNode, const char *zKey, u32 nKey){
1679 assert( pNode->eU==1 );
1680 if( pNode->jnFlags & JNODE_RAW ){
1681 if( pNode->n!=nKey ) return 0;
1682 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
1683 }else{
1684 if( pNode->n!=nKey+2 ) return 0;
1685 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
1688 static int jsonSameLabel(const JsonNode *p1, const JsonNode *p2){
1689 if( p1->jnFlags & JNODE_RAW ){
1690 return jsonLabelCompare(p2, p1->u.zJContent, p1->n);
1691 }else if( p2->jnFlags & JNODE_RAW ){
1692 return jsonLabelCompare(p1, p2->u.zJContent, p2->n);
1693 }else{
1694 return p1->n==p2->n && strncmp(p1->u.zJContent,p2->u.zJContent,p1->n)==0;
1698 /* forward declaration */
1699 static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
1702 ** Search along zPath to find the node specified. Return a pointer
1703 ** to that node, or NULL if zPath is malformed or if there is no such
1704 ** node.
1706 ** If pApnd!=0, then try to append new nodes to complete zPath if it is
1707 ** possible to do so and if no existing node corresponds to zPath. If
1708 ** new nodes are appended *pApnd is set to 1.
1710 static JsonNode *jsonLookupStep(
1711 JsonParse *pParse, /* The JSON to search */
1712 u32 iRoot, /* Begin the search at this node */
1713 const char *zPath, /* The path to search */
1714 int *pApnd, /* Append nodes to complete path if not NULL */
1715 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
1717 u32 i, j, nKey;
1718 const char *zKey;
1719 JsonNode *pRoot = &pParse->aNode[iRoot];
1720 if( zPath[0]==0 ) return pRoot;
1721 if( pRoot->jnFlags & JNODE_REPLACE ) return 0;
1722 if( zPath[0]=='.' ){
1723 if( pRoot->eType!=JSON_OBJECT ) return 0;
1724 zPath++;
1725 if( zPath[0]=='"' ){
1726 zKey = zPath + 1;
1727 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
1728 nKey = i-1;
1729 if( zPath[i] ){
1730 i++;
1731 }else{
1732 *pzErr = zPath;
1733 return 0;
1735 testcase( nKey==0 );
1736 }else{
1737 zKey = zPath;
1738 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
1739 nKey = i;
1740 if( nKey==0 ){
1741 *pzErr = zPath;
1742 return 0;
1745 j = 1;
1746 for(;;){
1747 while( j<=pRoot->n ){
1748 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
1749 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
1751 j++;
1752 j += jsonNodeSize(&pRoot[j]);
1754 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
1755 assert( pRoot->eU==2 );
1756 iRoot += pRoot->u.iAppend;
1757 pRoot = &pParse->aNode[iRoot];
1758 j = 1;
1760 if( pApnd ){
1761 u32 iStart, iLabel;
1762 JsonNode *pNode;
1763 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
1764 iLabel = jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
1765 zPath += i;
1766 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
1767 if( pParse->oom ) return 0;
1768 if( pNode ){
1769 pRoot = &pParse->aNode[iRoot];
1770 assert( pRoot->eU==0 );
1771 pRoot->u.iAppend = iStart - iRoot;
1772 pRoot->jnFlags |= JNODE_APPEND;
1773 VVA( pRoot->eU = 2 );
1774 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
1776 return pNode;
1778 }else if( zPath[0]=='[' ){
1779 i = 0;
1780 j = 1;
1781 while( sqlite3Isdigit(zPath[j]) ){
1782 i = i*10 + zPath[j] - '0';
1783 j++;
1785 if( j<2 || zPath[j]!=']' ){
1786 if( zPath[1]=='#' ){
1787 JsonNode *pBase = pRoot;
1788 int iBase = iRoot;
1789 if( pRoot->eType!=JSON_ARRAY ) return 0;
1790 for(;;){
1791 while( j<=pBase->n ){
1792 if( (pBase[j].jnFlags & JNODE_REMOVE)==0 ) i++;
1793 j += jsonNodeSize(&pBase[j]);
1795 if( (pBase->jnFlags & JNODE_APPEND)==0 ) break;
1796 assert( pBase->eU==2 );
1797 iBase += pBase->u.iAppend;
1798 pBase = &pParse->aNode[iBase];
1799 j = 1;
1801 j = 2;
1802 if( zPath[2]=='-' && sqlite3Isdigit(zPath[3]) ){
1803 unsigned int x = 0;
1804 j = 3;
1806 x = x*10 + zPath[j] - '0';
1807 j++;
1808 }while( sqlite3Isdigit(zPath[j]) );
1809 if( x>i ) return 0;
1810 i -= x;
1812 if( zPath[j]!=']' ){
1813 *pzErr = zPath;
1814 return 0;
1816 }else{
1817 *pzErr = zPath;
1818 return 0;
1821 if( pRoot->eType!=JSON_ARRAY ) return 0;
1822 zPath += j + 1;
1823 j = 1;
1824 for(;;){
1825 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
1826 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
1827 j += jsonNodeSize(&pRoot[j]);
1829 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
1830 assert( pRoot->eU==2 );
1831 iRoot += pRoot->u.iAppend;
1832 pRoot = &pParse->aNode[iRoot];
1833 j = 1;
1835 if( j<=pRoot->n ){
1836 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
1838 if( i==0 && pApnd ){
1839 u32 iStart;
1840 JsonNode *pNode;
1841 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
1842 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
1843 if( pParse->oom ) return 0;
1844 if( pNode ){
1845 pRoot = &pParse->aNode[iRoot];
1846 assert( pRoot->eU==0 );
1847 pRoot->u.iAppend = iStart - iRoot;
1848 pRoot->jnFlags |= JNODE_APPEND;
1849 VVA( pRoot->eU = 2 );
1851 return pNode;
1853 }else{
1854 *pzErr = zPath;
1856 return 0;
1860 ** Append content to pParse that will complete zPath. Return a pointer
1861 ** to the inserted node, or return NULL if the append fails.
1863 static JsonNode *jsonLookupAppend(
1864 JsonParse *pParse, /* Append content to the JSON parse */
1865 const char *zPath, /* Description of content to append */
1866 int *pApnd, /* Set this flag to 1 */
1867 const char **pzErr /* Make this point to any syntax error */
1869 *pApnd = 1;
1870 if( zPath[0]==0 ){
1871 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
1872 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
1874 if( zPath[0]=='.' ){
1875 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
1876 }else if( strncmp(zPath,"[0]",3)==0 ){
1877 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
1878 }else{
1879 return 0;
1881 if( pParse->oom ) return 0;
1882 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
1886 ** Return the text of a syntax error message on a JSON path. Space is
1887 ** obtained from sqlite3_malloc().
1889 static char *jsonPathSyntaxError(const char *zErr){
1890 return sqlite3_mprintf("JSON path error near '%q'", zErr);
1894 ** Do a node lookup using zPath. Return a pointer to the node on success.
1895 ** Return NULL if not found or if there is an error.
1897 ** On an error, write an error message into pCtx and increment the
1898 ** pParse->nErr counter.
1900 ** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
1901 ** nodes are appended.
1903 static JsonNode *jsonLookup(
1904 JsonParse *pParse, /* The JSON to search */
1905 const char *zPath, /* The path to search */
1906 int *pApnd, /* Append nodes to complete path if not NULL */
1907 sqlite3_context *pCtx /* Report errors here, if not NULL */
1909 const char *zErr = 0;
1910 JsonNode *pNode = 0;
1911 char *zMsg;
1913 if( zPath==0 ) return 0;
1914 if( zPath[0]!='$' ){
1915 zErr = zPath;
1916 goto lookup_err;
1918 zPath++;
1919 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
1920 if( zErr==0 ) return pNode;
1922 lookup_err:
1923 pParse->nErr++;
1924 assert( zErr!=0 && pCtx!=0 );
1925 zMsg = jsonPathSyntaxError(zErr);
1926 if( zMsg ){
1927 sqlite3_result_error(pCtx, zMsg, -1);
1928 sqlite3_free(zMsg);
1929 }else{
1930 sqlite3_result_error_nomem(pCtx);
1932 return 0;
1937 ** Report the wrong number of arguments for json_insert(), json_replace()
1938 ** or json_set().
1940 static void jsonWrongNumArgs(
1941 sqlite3_context *pCtx,
1942 const char *zFuncName
1944 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
1945 zFuncName);
1946 sqlite3_result_error(pCtx, zMsg, -1);
1947 sqlite3_free(zMsg);
1951 ** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
1953 static void jsonRemoveAllNulls(JsonNode *pNode){
1954 int i, n;
1955 assert( pNode->eType==JSON_OBJECT );
1956 n = pNode->n;
1957 for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
1958 switch( pNode[i].eType ){
1959 case JSON_NULL:
1960 pNode[i].jnFlags |= JNODE_REMOVE;
1961 break;
1962 case JSON_OBJECT:
1963 jsonRemoveAllNulls(&pNode[i]);
1964 break;
1970 /****************************************************************************
1971 ** SQL functions used for testing and debugging
1972 ****************************************************************************/
1974 #ifdef SQLITE_DEBUG
1976 ** The json_parse(JSON) function returns a string which describes
1977 ** a parse of the JSON provided. Or it returns NULL if JSON is not
1978 ** well-formed.
1980 static void jsonParseFunc(
1981 sqlite3_context *ctx,
1982 int argc,
1983 sqlite3_value **argv
1985 JsonString s; /* Output string - not real JSON */
1986 JsonParse x; /* The parse */
1987 u32 i;
1989 assert( argc==1 );
1990 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
1991 jsonParseFindParents(&x);
1992 jsonInit(&s, ctx);
1993 for(i=0; i<x.nNode; i++){
1994 const char *zType;
1995 if( x.aNode[i].jnFlags & JNODE_LABEL ){
1996 assert( x.aNode[i].eType==JSON_STRING );
1997 zType = "label";
1998 }else{
1999 zType = jsonType[x.aNode[i].eType];
2001 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
2002 i, zType, x.aNode[i].n, x.aUp[i]);
2003 assert( x.aNode[i].eU==0 || x.aNode[i].eU==1 );
2004 if( x.aNode[i].u.zJContent!=0 ){
2005 assert( x.aNode[i].eU==1 );
2006 jsonAppendRaw(&s, " ", 1);
2007 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
2008 }else{
2009 assert( x.aNode[i].eU==0 );
2011 jsonAppendRaw(&s, "\n", 1);
2013 jsonParseReset(&x);
2014 jsonResult(&s);
2018 ** The json_test1(JSON) function return true (1) if the input is JSON
2019 ** text generated by another json function. It returns (0) if the input
2020 ** is not known to be JSON.
2022 static void jsonTest1Func(
2023 sqlite3_context *ctx,
2024 int argc,
2025 sqlite3_value **argv
2027 UNUSED_PARAMETER(argc);
2028 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
2030 #endif /* SQLITE_DEBUG */
2032 /****************************************************************************
2033 ** Scalar SQL function implementations
2034 ****************************************************************************/
2037 ** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
2038 ** corresponding to the SQL value input. Mostly this means putting
2039 ** double-quotes around strings and returning the unquoted string "null"
2040 ** when given a NULL input.
2042 static void jsonQuoteFunc(
2043 sqlite3_context *ctx,
2044 int argc,
2045 sqlite3_value **argv
2047 JsonString jx;
2048 UNUSED_PARAMETER(argc);
2050 jsonInit(&jx, ctx);
2051 jsonAppendValue(&jx, argv[0]);
2052 jsonResult(&jx);
2053 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2057 ** Implementation of the json_array(VALUE,...) function. Return a JSON
2058 ** array that contains all values given in arguments. Or if any argument
2059 ** is a BLOB, throw an error.
2061 static void jsonArrayFunc(
2062 sqlite3_context *ctx,
2063 int argc,
2064 sqlite3_value **argv
2066 int i;
2067 JsonString jx;
2069 jsonInit(&jx, ctx);
2070 jsonAppendChar(&jx, '[');
2071 for(i=0; i<argc; i++){
2072 jsonAppendSeparator(&jx);
2073 jsonAppendValue(&jx, argv[i]);
2075 jsonAppendChar(&jx, ']');
2076 jsonResult(&jx);
2077 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2082 ** json_array_length(JSON)
2083 ** json_array_length(JSON, PATH)
2085 ** Return the number of elements in the top-level JSON array.
2086 ** Return 0 if the input is not a well-formed JSON array.
2088 static void jsonArrayLengthFunc(
2089 sqlite3_context *ctx,
2090 int argc,
2091 sqlite3_value **argv
2093 JsonParse *p; /* The parse */
2094 sqlite3_int64 n = 0;
2095 u32 i;
2096 JsonNode *pNode;
2098 p = jsonParseCached(ctx, argv, ctx);
2099 if( p==0 ) return;
2100 assert( p->nNode );
2101 if( argc==2 ){
2102 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
2103 pNode = jsonLookup(p, zPath, 0, ctx);
2104 }else{
2105 pNode = p->aNode;
2107 if( pNode==0 ){
2108 return;
2110 if( pNode->eType==JSON_ARRAY ){
2111 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
2112 for(i=1; i<=pNode->n; n++){
2113 i += jsonNodeSize(&pNode[i]);
2116 sqlite3_result_int64(ctx, n);
2120 ** Bit values for the flags passed into jsonExtractFunc() or
2121 ** jsonSetFunc() via the user-data value.
2123 #define JSON_JSON 0x01 /* Result is always JSON */
2124 #define JSON_SQL 0x02 /* Result is always SQL */
2125 #define JSON_ABPATH 0x03 /* Allow abbreviated JSON path specs */
2126 #define JSON_ISSET 0x04 /* json_set(), not json_insert() */
2129 ** json_extract(JSON, PATH, ...)
2130 ** "->"(JSON,PATH)
2131 ** "->>"(JSON,PATH)
2133 ** Return the element described by PATH. Return NULL if that PATH element
2134 ** is not found.
2136 ** If JSON_JSON is set or if more that one PATH argument is supplied then
2137 ** always return a JSON representation of the result. If JSON_SQL is set,
2138 ** then always return an SQL representation of the result. If neither flag
2139 ** is present and argc==2, then return JSON for objects and arrays and SQL
2140 ** for all other values.
2142 ** When multiple PATH arguments are supplied, the result is a JSON array
2143 ** containing the result of each PATH.
2145 ** Abbreviated JSON path expressions are allows if JSON_ABPATH, for
2146 ** compatibility with PG.
2148 static void jsonExtractFunc(
2149 sqlite3_context *ctx,
2150 int argc,
2151 sqlite3_value **argv
2153 JsonParse *p; /* The parse */
2154 JsonNode *pNode;
2155 const char *zPath;
2156 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
2157 JsonString jx;
2159 if( argc<2 ) return;
2160 p = jsonParseCached(ctx, argv, ctx);
2161 if( p==0 ) return;
2162 if( argc==2 ){
2163 /* With a single PATH argument */
2164 zPath = (const char*)sqlite3_value_text(argv[1]);
2165 if( zPath==0 ) return;
2166 if( flags & JSON_ABPATH ){
2167 if( zPath[0]!='$' || (zPath[1]!='.' && zPath[1]!='[' && zPath[1]!=0) ){
2168 /* The -> and ->> operators accept abbreviated PATH arguments. This
2169 ** is mostly for compatibility with PostgreSQL, but also for
2170 ** convenience.
2172 ** NUMBER ==> $[NUMBER] // PG compatible
2173 ** LABEL ==> $.LABEL // PG compatible
2174 ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience
2176 jsonInit(&jx, ctx);
2177 if( sqlite3Isdigit(zPath[0]) ){
2178 jsonAppendRaw(&jx, "$[", 2);
2179 jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
2180 jsonAppendRaw(&jx, "]", 2);
2181 }else{
2182 jsonAppendRaw(&jx, "$.", 1 + (zPath[0]!='['));
2183 jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
2184 jsonAppendChar(&jx, 0);
2186 pNode = jx.bErr ? 0 : jsonLookup(p, jx.zBuf, 0, ctx);
2187 jsonReset(&jx);
2188 }else{
2189 pNode = jsonLookup(p, zPath, 0, ctx);
2191 if( pNode ){
2192 if( flags & JSON_JSON ){
2193 jsonReturnJson(pNode, ctx, 0);
2194 }else{
2195 jsonReturn(pNode, ctx, 0);
2196 sqlite3_result_subtype(ctx, 0);
2199 }else{
2200 pNode = jsonLookup(p, zPath, 0, ctx);
2201 if( p->nErr==0 && pNode ) jsonReturn(pNode, ctx, 0);
2203 }else{
2204 /* Two or more PATH arguments results in a JSON array with each
2205 ** element of the array being the value selected by one of the PATHs */
2206 int i;
2207 jsonInit(&jx, ctx);
2208 jsonAppendChar(&jx, '[');
2209 for(i=1; i<argc; i++){
2210 zPath = (const char*)sqlite3_value_text(argv[i]);
2211 pNode = jsonLookup(p, zPath, 0, ctx);
2212 if( p->nErr ) break;
2213 jsonAppendSeparator(&jx);
2214 if( pNode ){
2215 jsonRenderNode(pNode, &jx, 0);
2216 }else{
2217 jsonAppendRaw(&jx, "null", 4);
2220 if( i==argc ){
2221 jsonAppendChar(&jx, ']');
2222 jsonResult(&jx);
2223 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2225 jsonReset(&jx);
2229 /* This is the RFC 7396 MergePatch algorithm.
2231 static JsonNode *jsonMergePatch(
2232 JsonParse *pParse, /* The JSON parser that contains the TARGET */
2233 u32 iTarget, /* Node of the TARGET in pParse */
2234 JsonNode *pPatch /* The PATCH */
2236 u32 i, j;
2237 u32 iRoot;
2238 JsonNode *pTarget;
2239 if( pPatch->eType!=JSON_OBJECT ){
2240 return pPatch;
2242 assert( iTarget<pParse->nNode );
2243 pTarget = &pParse->aNode[iTarget];
2244 assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
2245 if( pTarget->eType!=JSON_OBJECT ){
2246 jsonRemoveAllNulls(pPatch);
2247 return pPatch;
2249 iRoot = iTarget;
2250 for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
2251 u32 nKey;
2252 const char *zKey;
2253 assert( pPatch[i].eType==JSON_STRING );
2254 assert( pPatch[i].jnFlags & JNODE_LABEL );
2255 assert( pPatch[i].eU==1 );
2256 nKey = pPatch[i].n;
2257 zKey = pPatch[i].u.zJContent;
2258 for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
2259 assert( pTarget[j].eType==JSON_STRING );
2260 assert( pTarget[j].jnFlags & JNODE_LABEL );
2261 if( jsonSameLabel(&pPatch[i], &pTarget[j]) ){
2262 if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
2263 if( pPatch[i+1].eType==JSON_NULL ){
2264 pTarget[j+1].jnFlags |= JNODE_REMOVE;
2265 }else{
2266 JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
2267 if( pNew==0 ) return 0;
2268 pTarget = &pParse->aNode[iTarget];
2269 if( pNew!=&pTarget[j+1] ){
2270 assert( pTarget[j+1].eU==0
2271 || pTarget[j+1].eU==1
2272 || pTarget[j+1].eU==2 );
2273 testcase( pTarget[j+1].eU==1 );
2274 testcase( pTarget[j+1].eU==2 );
2275 VVA( pTarget[j+1].eU = 5 );
2276 pTarget[j+1].u.pPatch = pNew;
2277 pTarget[j+1].jnFlags |= JNODE_PATCH;
2280 break;
2283 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
2284 int iStart, iPatch;
2285 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
2286 jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
2287 iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
2288 if( pParse->oom ) return 0;
2289 jsonRemoveAllNulls(pPatch);
2290 pTarget = &pParse->aNode[iTarget];
2291 assert( pParse->aNode[iRoot].eU==0 || pParse->aNode[iRoot].eU==2 );
2292 testcase( pParse->aNode[iRoot].eU==2 );
2293 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
2294 VVA( pParse->aNode[iRoot].eU = 2 );
2295 pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
2296 iRoot = iStart;
2297 assert( pParse->aNode[iPatch].eU==0 );
2298 VVA( pParse->aNode[iPatch].eU = 5 );
2299 pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
2300 pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
2303 return pTarget;
2307 ** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
2308 ** object that is the result of running the RFC 7396 MergePatch() algorithm
2309 ** on the two arguments.
2311 static void jsonPatchFunc(
2312 sqlite3_context *ctx,
2313 int argc,
2314 sqlite3_value **argv
2316 JsonParse x; /* The JSON that is being patched */
2317 JsonParse y; /* The patch */
2318 JsonNode *pResult; /* The result of the merge */
2320 UNUSED_PARAMETER(argc);
2321 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
2322 if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
2323 jsonParseReset(&x);
2324 return;
2326 pResult = jsonMergePatch(&x, 0, y.aNode);
2327 assert( pResult!=0 || x.oom );
2328 if( pResult ){
2329 jsonReturnJson(pResult, ctx, 0);
2330 }else{
2331 sqlite3_result_error_nomem(ctx);
2333 jsonParseReset(&x);
2334 jsonParseReset(&y);
2339 ** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
2340 ** object that contains all name/value given in arguments. Or if any name
2341 ** is not a string or if any value is a BLOB, throw an error.
2343 static void jsonObjectFunc(
2344 sqlite3_context *ctx,
2345 int argc,
2346 sqlite3_value **argv
2348 int i;
2349 JsonString jx;
2350 const char *z;
2351 u32 n;
2353 if( argc&1 ){
2354 sqlite3_result_error(ctx, "json_object() requires an even number "
2355 "of arguments", -1);
2356 return;
2358 jsonInit(&jx, ctx);
2359 jsonAppendChar(&jx, '{');
2360 for(i=0; i<argc; i+=2){
2361 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
2362 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
2363 jsonReset(&jx);
2364 return;
2366 jsonAppendSeparator(&jx);
2367 z = (const char*)sqlite3_value_text(argv[i]);
2368 n = (u32)sqlite3_value_bytes(argv[i]);
2369 jsonAppendString(&jx, z, n);
2370 jsonAppendChar(&jx, ':');
2371 jsonAppendValue(&jx, argv[i+1]);
2373 jsonAppendChar(&jx, '}');
2374 jsonResult(&jx);
2375 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2380 ** json_remove(JSON, PATH, ...)
2382 ** Remove the named elements from JSON and return the result. malformed
2383 ** JSON or PATH arguments result in an error.
2385 static void jsonRemoveFunc(
2386 sqlite3_context *ctx,
2387 int argc,
2388 sqlite3_value **argv
2390 JsonParse x; /* The parse */
2391 JsonNode *pNode;
2392 const char *zPath;
2393 u32 i;
2395 if( argc<1 ) return;
2396 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
2397 assert( x.nNode );
2398 for(i=1; i<(u32)argc; i++){
2399 zPath = (const char*)sqlite3_value_text(argv[i]);
2400 if( zPath==0 ) goto remove_done;
2401 pNode = jsonLookup(&x, zPath, 0, ctx);
2402 if( x.nErr ) goto remove_done;
2403 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
2405 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
2406 jsonReturnJson(x.aNode, ctx, 0);
2408 remove_done:
2409 jsonParseReset(&x);
2413 ** json_replace(JSON, PATH, VALUE, ...)
2415 ** Replace the value at PATH with VALUE. If PATH does not already exist,
2416 ** this routine is a no-op. If JSON or PATH is malformed, throw an error.
2418 static void jsonReplaceFunc(
2419 sqlite3_context *ctx,
2420 int argc,
2421 sqlite3_value **argv
2423 JsonParse x; /* The parse */
2424 JsonNode *pNode;
2425 const char *zPath;
2426 u32 i;
2428 if( argc<1 ) return;
2429 if( (argc&1)==0 ) {
2430 jsonWrongNumArgs(ctx, "replace");
2431 return;
2433 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
2434 assert( x.nNode );
2435 for(i=1; i<(u32)argc; i+=2){
2436 zPath = (const char*)sqlite3_value_text(argv[i]);
2437 pNode = jsonLookup(&x, zPath, 0, ctx);
2438 if( x.nErr ) goto replace_err;
2439 if( pNode ){
2440 assert( pNode->eU==0 || pNode->eU==1 || pNode->eU==4 );
2441 testcase( pNode->eU!=0 && pNode->eU!=1 );
2442 pNode->jnFlags |= (u8)JNODE_REPLACE;
2443 VVA( pNode->eU = 4 );
2444 pNode->u.iReplace = i + 1;
2447 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
2448 assert( x.aNode[0].eU==4 );
2449 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
2450 }else{
2451 jsonReturnJson(x.aNode, ctx, argv);
2453 replace_err:
2454 jsonParseReset(&x);
2459 ** json_set(JSON, PATH, VALUE, ...)
2461 ** Set the value at PATH to VALUE. Create the PATH if it does not already
2462 ** exist. Overwrite existing values that do exist.
2463 ** If JSON or PATH is malformed, throw an error.
2465 ** json_insert(JSON, PATH, VALUE, ...)
2467 ** Create PATH and initialize it to VALUE. If PATH already exists, this
2468 ** routine is a no-op. If JSON or PATH is malformed, throw an error.
2470 static void jsonSetFunc(
2471 sqlite3_context *ctx,
2472 int argc,
2473 sqlite3_value **argv
2475 JsonParse x; /* The parse */
2476 JsonNode *pNode;
2477 const char *zPath;
2478 u32 i;
2479 int bApnd;
2480 int bIsSet = sqlite3_user_data(ctx)!=0;
2482 if( argc<1 ) return;
2483 if( (argc&1)==0 ) {
2484 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
2485 return;
2487 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
2488 assert( x.nNode );
2489 for(i=1; i<(u32)argc; i+=2){
2490 zPath = (const char*)sqlite3_value_text(argv[i]);
2491 bApnd = 0;
2492 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
2493 if( x.oom ){
2494 sqlite3_result_error_nomem(ctx);
2495 goto jsonSetDone;
2496 }else if( x.nErr ){
2497 goto jsonSetDone;
2498 }else if( pNode && (bApnd || bIsSet) ){
2499 testcase( pNode->eU!=0 && pNode->eU!=1 );
2500 assert( pNode->eU!=3 && pNode->eU!=5 );
2501 VVA( pNode->eU = 4 );
2502 pNode->jnFlags |= (u8)JNODE_REPLACE;
2503 pNode->u.iReplace = i + 1;
2506 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
2507 assert( x.aNode[0].eU==4 );
2508 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
2509 }else{
2510 jsonReturnJson(x.aNode, ctx, argv);
2512 jsonSetDone:
2513 jsonParseReset(&x);
2517 ** json_type(JSON)
2518 ** json_type(JSON, PATH)
2520 ** Return the top-level "type" of a JSON string. json_type() raises an
2521 ** error if either the JSON or PATH inputs are not well-formed.
2523 static void jsonTypeFunc(
2524 sqlite3_context *ctx,
2525 int argc,
2526 sqlite3_value **argv
2528 JsonParse *p; /* The parse */
2529 const char *zPath;
2530 JsonNode *pNode;
2532 p = jsonParseCached(ctx, argv, ctx);
2533 if( p==0 ) return;
2534 if( argc==2 ){
2535 zPath = (const char*)sqlite3_value_text(argv[1]);
2536 pNode = jsonLookup(p, zPath, 0, ctx);
2537 }else{
2538 pNode = p->aNode;
2540 if( pNode ){
2541 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
2546 ** json_valid(JSON)
2548 ** Return 1 if JSON is a well-formed canonical JSON string according
2549 ** to RFC-7159. Return 0 otherwise.
2551 static void jsonValidFunc(
2552 sqlite3_context *ctx,
2553 int argc,
2554 sqlite3_value **argv
2556 JsonParse *p; /* The parse */
2557 UNUSED_PARAMETER(argc);
2558 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
2559 p = jsonParseCached(ctx, argv, 0);
2560 if( p==0 || p->oom ){
2561 sqlite3_result_error_nomem(ctx);
2562 sqlite3_free(p);
2563 }else{
2564 sqlite3_result_int(ctx, p->nErr==0 && p->hasNonstd==0);
2565 if( p->nErr ) jsonParseFree(p);
2570 ** json_error_position(JSON)
2572 ** If the argument is not an interpretable JSON string, then return the 1-based
2573 ** character position at which the parser first recognized that the input
2574 ** was in error. The left-most character is 1. If the string is valid
2575 ** JSON, then return 0.
2577 ** Note that json_valid() is only true for strictly conforming canonical JSON.
2578 ** But this routine returns zero if the input contains extension. Thus:
2580 ** (1) If the input X is strictly conforming canonical JSON:
2582 ** json_valid(X) returns true
2583 ** json_error_position(X) returns 0
2585 ** (2) If the input X is JSON but it includes extension (such as JSON5) that
2586 ** are not part of RFC-8259:
2588 ** json_valid(X) returns false
2589 ** json_error_position(X) return 0
2591 ** (3) If the input X cannot be interpreted as JSON even taking extensions
2592 ** into account:
2594 ** json_valid(X) return false
2595 ** json_error_position(X) returns 1 or more
2597 static void jsonErrorFunc(
2598 sqlite3_context *ctx,
2599 int argc,
2600 sqlite3_value **argv
2602 JsonParse *p; /* The parse */
2603 UNUSED_PARAMETER(argc);
2604 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
2605 p = jsonParseCached(ctx, argv, 0);
2606 if( p==0 || p->oom ){
2607 sqlite3_result_error_nomem(ctx);
2608 sqlite3_free(p);
2609 }else if( p->nErr==0 ){
2610 sqlite3_result_int(ctx, 0);
2611 }else{
2612 int n = 1;
2613 u32 i;
2614 const char *z = p->zJson;
2615 for(i=0; i<p->iErr && ALWAYS(z[i]); i++){
2616 if( (z[i]&0xc0)!=0x80 ) n++;
2618 sqlite3_result_int(ctx, n);
2619 jsonParseFree(p);
2624 /****************************************************************************
2625 ** Aggregate SQL function implementations
2626 ****************************************************************************/
2628 ** json_group_array(VALUE)
2630 ** Return a JSON array composed of all values in the aggregate.
2632 static void jsonArrayStep(
2633 sqlite3_context *ctx,
2634 int argc,
2635 sqlite3_value **argv
2637 JsonString *pStr;
2638 UNUSED_PARAMETER(argc);
2639 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
2640 if( pStr ){
2641 if( pStr->zBuf==0 ){
2642 jsonInit(pStr, ctx);
2643 jsonAppendChar(pStr, '[');
2644 }else if( pStr->nUsed>1 ){
2645 jsonAppendChar(pStr, ',');
2647 pStr->pCtx = ctx;
2648 jsonAppendValue(pStr, argv[0]);
2651 static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
2652 JsonString *pStr;
2653 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
2654 if( pStr ){
2655 pStr->pCtx = ctx;
2656 jsonAppendChar(pStr, ']');
2657 if( pStr->bErr ){
2658 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
2659 assert( pStr->bStatic );
2660 }else if( isFinal ){
2661 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
2662 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
2663 pStr->bStatic = 1;
2664 }else{
2665 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
2666 pStr->nUsed--;
2668 }else{
2669 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
2671 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2673 static void jsonArrayValue(sqlite3_context *ctx){
2674 jsonArrayCompute(ctx, 0);
2676 static void jsonArrayFinal(sqlite3_context *ctx){
2677 jsonArrayCompute(ctx, 1);
2680 #ifndef SQLITE_OMIT_WINDOWFUNC
2682 ** This method works for both json_group_array() and json_group_object().
2683 ** It works by removing the first element of the group by searching forward
2684 ** to the first comma (",") that is not within a string and deleting all
2685 ** text through that comma.
2687 static void jsonGroupInverse(
2688 sqlite3_context *ctx,
2689 int argc,
2690 sqlite3_value **argv
2692 unsigned int i;
2693 int inStr = 0;
2694 int nNest = 0;
2695 char *z;
2696 char c;
2697 JsonString *pStr;
2698 UNUSED_PARAMETER(argc);
2699 UNUSED_PARAMETER(argv);
2700 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
2701 #ifdef NEVER
2702 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
2703 ** always have been called to initalize it */
2704 if( NEVER(!pStr) ) return;
2705 #endif
2706 z = pStr->zBuf;
2707 for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){
2708 if( c=='"' ){
2709 inStr = !inStr;
2710 }else if( c=='\\' ){
2711 i++;
2712 }else if( !inStr ){
2713 if( c=='{' || c=='[' ) nNest++;
2714 if( c=='}' || c==']' ) nNest--;
2717 if( i<pStr->nUsed ){
2718 pStr->nUsed -= i;
2719 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
2720 z[pStr->nUsed] = 0;
2721 }else{
2722 pStr->nUsed = 1;
2725 #else
2726 # define jsonGroupInverse 0
2727 #endif
2731 ** json_group_obj(NAME,VALUE)
2733 ** Return a JSON object composed of all names and values in the aggregate.
2735 static void jsonObjectStep(
2736 sqlite3_context *ctx,
2737 int argc,
2738 sqlite3_value **argv
2740 JsonString *pStr;
2741 const char *z;
2742 u32 n;
2743 UNUSED_PARAMETER(argc);
2744 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
2745 if( pStr ){
2746 if( pStr->zBuf==0 ){
2747 jsonInit(pStr, ctx);
2748 jsonAppendChar(pStr, '{');
2749 }else if( pStr->nUsed>1 ){
2750 jsonAppendChar(pStr, ',');
2752 pStr->pCtx = ctx;
2753 z = (const char*)sqlite3_value_text(argv[0]);
2754 n = (u32)sqlite3_value_bytes(argv[0]);
2755 jsonAppendString(pStr, z, n);
2756 jsonAppendChar(pStr, ':');
2757 jsonAppendValue(pStr, argv[1]);
2760 static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
2761 JsonString *pStr;
2762 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
2763 if( pStr ){
2764 jsonAppendChar(pStr, '}');
2765 if( pStr->bErr ){
2766 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
2767 assert( pStr->bStatic );
2768 }else if( isFinal ){
2769 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
2770 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
2771 pStr->bStatic = 1;
2772 }else{
2773 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
2774 pStr->nUsed--;
2776 }else{
2777 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
2779 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2781 static void jsonObjectValue(sqlite3_context *ctx){
2782 jsonObjectCompute(ctx, 0);
2784 static void jsonObjectFinal(sqlite3_context *ctx){
2785 jsonObjectCompute(ctx, 1);
2790 #ifndef SQLITE_OMIT_VIRTUALTABLE
2791 /****************************************************************************
2792 ** The json_each virtual table
2793 ****************************************************************************/
2794 typedef struct JsonEachCursor JsonEachCursor;
2795 struct JsonEachCursor {
2796 sqlite3_vtab_cursor base; /* Base class - must be first */
2797 u32 iRowid; /* The rowid */
2798 u32 iBegin; /* The first node of the scan */
2799 u32 i; /* Index in sParse.aNode[] of current row */
2800 u32 iEnd; /* EOF when i equals or exceeds this value */
2801 u8 eType; /* Type of top-level element */
2802 u8 bRecursive; /* True for json_tree(). False for json_each() */
2803 char *zJson; /* Input JSON */
2804 char *zRoot; /* Path by which to filter zJson */
2805 JsonParse sParse; /* Parse of the input JSON */
2808 /* Constructor for the json_each virtual table */
2809 static int jsonEachConnect(
2810 sqlite3 *db,
2811 void *pAux,
2812 int argc, const char *const*argv,
2813 sqlite3_vtab **ppVtab,
2814 char **pzErr
2816 sqlite3_vtab *pNew;
2817 int rc;
2819 /* Column numbers */
2820 #define JEACH_KEY 0
2821 #define JEACH_VALUE 1
2822 #define JEACH_TYPE 2
2823 #define JEACH_ATOM 3
2824 #define JEACH_ID 4
2825 #define JEACH_PARENT 5
2826 #define JEACH_FULLKEY 6
2827 #define JEACH_PATH 7
2828 /* The xBestIndex method assumes that the JSON and ROOT columns are
2829 ** the last two columns in the table. Should this ever changes, be
2830 ** sure to update the xBestIndex method. */
2831 #define JEACH_JSON 8
2832 #define JEACH_ROOT 9
2834 UNUSED_PARAMETER(pzErr);
2835 UNUSED_PARAMETER(argv);
2836 UNUSED_PARAMETER(argc);
2837 UNUSED_PARAMETER(pAux);
2838 rc = sqlite3_declare_vtab(db,
2839 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
2840 "json HIDDEN,root HIDDEN)");
2841 if( rc==SQLITE_OK ){
2842 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
2843 if( pNew==0 ) return SQLITE_NOMEM;
2844 memset(pNew, 0, sizeof(*pNew));
2845 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
2847 return rc;
2850 /* destructor for json_each virtual table */
2851 static int jsonEachDisconnect(sqlite3_vtab *pVtab){
2852 sqlite3_free(pVtab);
2853 return SQLITE_OK;
2856 /* constructor for a JsonEachCursor object for json_each(). */
2857 static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2858 JsonEachCursor *pCur;
2860 UNUSED_PARAMETER(p);
2861 pCur = sqlite3_malloc( sizeof(*pCur) );
2862 if( pCur==0 ) return SQLITE_NOMEM;
2863 memset(pCur, 0, sizeof(*pCur));
2864 *ppCursor = &pCur->base;
2865 return SQLITE_OK;
2868 /* constructor for a JsonEachCursor object for json_tree(). */
2869 static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2870 int rc = jsonEachOpenEach(p, ppCursor);
2871 if( rc==SQLITE_OK ){
2872 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
2873 pCur->bRecursive = 1;
2875 return rc;
2878 /* Reset a JsonEachCursor back to its original state. Free any memory
2879 ** held. */
2880 static void jsonEachCursorReset(JsonEachCursor *p){
2881 sqlite3_free(p->zJson);
2882 sqlite3_free(p->zRoot);
2883 jsonParseReset(&p->sParse);
2884 p->iRowid = 0;
2885 p->i = 0;
2886 p->iEnd = 0;
2887 p->eType = 0;
2888 p->zJson = 0;
2889 p->zRoot = 0;
2892 /* Destructor for a jsonEachCursor object */
2893 static int jsonEachClose(sqlite3_vtab_cursor *cur){
2894 JsonEachCursor *p = (JsonEachCursor*)cur;
2895 jsonEachCursorReset(p);
2896 sqlite3_free(cur);
2897 return SQLITE_OK;
2900 /* Return TRUE if the jsonEachCursor object has been advanced off the end
2901 ** of the JSON object */
2902 static int jsonEachEof(sqlite3_vtab_cursor *cur){
2903 JsonEachCursor *p = (JsonEachCursor*)cur;
2904 return p->i >= p->iEnd;
2907 /* Advance the cursor to the next element for json_tree() */
2908 static int jsonEachNext(sqlite3_vtab_cursor *cur){
2909 JsonEachCursor *p = (JsonEachCursor*)cur;
2910 if( p->bRecursive ){
2911 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
2912 p->i++;
2913 p->iRowid++;
2914 if( p->i<p->iEnd ){
2915 u32 iUp = p->sParse.aUp[p->i];
2916 JsonNode *pUp = &p->sParse.aNode[iUp];
2917 p->eType = pUp->eType;
2918 if( pUp->eType==JSON_ARRAY ){
2919 assert( pUp->eU==0 || pUp->eU==3 );
2920 testcase( pUp->eU==3 );
2921 VVA( pUp->eU = 3 );
2922 if( iUp==p->i-1 ){
2923 pUp->u.iKey = 0;
2924 }else{
2925 pUp->u.iKey++;
2929 }else{
2930 switch( p->eType ){
2931 case JSON_ARRAY: {
2932 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
2933 p->iRowid++;
2934 break;
2936 case JSON_OBJECT: {
2937 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
2938 p->iRowid++;
2939 break;
2941 default: {
2942 p->i = p->iEnd;
2943 break;
2947 return SQLITE_OK;
2950 /* Append an object label to the JSON Path being constructed
2951 ** in pStr.
2953 static void jsonAppendObjectPathElement(
2954 JsonString *pStr,
2955 JsonNode *pNode
2957 int jj, nn;
2958 const char *z;
2959 assert( pNode->eType==JSON_STRING );
2960 assert( pNode->jnFlags & JNODE_LABEL );
2961 assert( pNode->eU==1 );
2962 z = pNode->u.zJContent;
2963 nn = pNode->n;
2964 if( (pNode->jnFlags & JNODE_RAW)==0 ){
2965 assert( nn>=2 );
2966 assert( z[0]=='"' || z[0]=='\'' );
2967 assert( z[nn-1]=='"' || z[0]=='\'' );
2968 if( nn>2 && sqlite3Isalpha(z[1]) ){
2969 for(jj=2; jj<nn-1 && sqlite3Isalnum(z[jj]); jj++){}
2970 if( jj==nn-1 ){
2971 z++;
2972 nn -= 2;
2976 jsonPrintf(nn+2, pStr, ".%.*s", nn, z);
2979 /* Append the name of the path for element i to pStr
2981 static void jsonEachComputePath(
2982 JsonEachCursor *p, /* The cursor */
2983 JsonString *pStr, /* Write the path here */
2984 u32 i /* Path to this element */
2986 JsonNode *pNode, *pUp;
2987 u32 iUp;
2988 if( i==0 ){
2989 jsonAppendChar(pStr, '$');
2990 return;
2992 iUp = p->sParse.aUp[i];
2993 jsonEachComputePath(p, pStr, iUp);
2994 pNode = &p->sParse.aNode[i];
2995 pUp = &p->sParse.aNode[iUp];
2996 if( pUp->eType==JSON_ARRAY ){
2997 assert( pUp->eU==3 || (pUp->eU==0 && pUp->u.iKey==0) );
2998 testcase( pUp->eU==0 );
2999 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
3000 }else{
3001 assert( pUp->eType==JSON_OBJECT );
3002 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
3003 jsonAppendObjectPathElement(pStr, pNode);
3007 /* Return the value of a column */
3008 static int jsonEachColumn(
3009 sqlite3_vtab_cursor *cur, /* The cursor */
3010 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
3011 int i /* Which column to return */
3013 JsonEachCursor *p = (JsonEachCursor*)cur;
3014 JsonNode *pThis = &p->sParse.aNode[p->i];
3015 switch( i ){
3016 case JEACH_KEY: {
3017 if( p->i==0 ) break;
3018 if( p->eType==JSON_OBJECT ){
3019 jsonReturn(pThis, ctx, 0);
3020 }else if( p->eType==JSON_ARRAY ){
3021 u32 iKey;
3022 if( p->bRecursive ){
3023 if( p->iRowid==0 ) break;
3024 assert( p->sParse.aNode[p->sParse.aUp[p->i]].eU==3 );
3025 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
3026 }else{
3027 iKey = p->iRowid;
3029 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
3031 break;
3033 case JEACH_VALUE: {
3034 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
3035 jsonReturn(pThis, ctx, 0);
3036 break;
3038 case JEACH_TYPE: {
3039 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
3040 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
3041 break;
3043 case JEACH_ATOM: {
3044 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
3045 if( pThis->eType>=JSON_ARRAY ) break;
3046 jsonReturn(pThis, ctx, 0);
3047 break;
3049 case JEACH_ID: {
3050 sqlite3_result_int64(ctx,
3051 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
3052 break;
3054 case JEACH_PARENT: {
3055 if( p->i>p->iBegin && p->bRecursive ){
3056 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
3058 break;
3060 case JEACH_FULLKEY: {
3061 JsonString x;
3062 jsonInit(&x, ctx);
3063 if( p->bRecursive ){
3064 jsonEachComputePath(p, &x, p->i);
3065 }else{
3066 if( p->zRoot ){
3067 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
3068 }else{
3069 jsonAppendChar(&x, '$');
3071 if( p->eType==JSON_ARRAY ){
3072 jsonPrintf(30, &x, "[%d]", p->iRowid);
3073 }else if( p->eType==JSON_OBJECT ){
3074 jsonAppendObjectPathElement(&x, pThis);
3077 jsonResult(&x);
3078 break;
3080 case JEACH_PATH: {
3081 if( p->bRecursive ){
3082 JsonString x;
3083 jsonInit(&x, ctx);
3084 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
3085 jsonResult(&x);
3086 break;
3088 /* For json_each() path and root are the same so fall through
3089 ** into the root case */
3090 /* no break */ deliberate_fall_through
3092 default: {
3093 const char *zRoot = p->zRoot;
3094 if( zRoot==0 ) zRoot = "$";
3095 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
3096 break;
3098 case JEACH_JSON: {
3099 assert( i==JEACH_JSON );
3100 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
3101 break;
3104 return SQLITE_OK;
3107 /* Return the current rowid value */
3108 static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3109 JsonEachCursor *p = (JsonEachCursor*)cur;
3110 *pRowid = p->iRowid;
3111 return SQLITE_OK;
3114 /* The query strategy is to look for an equality constraint on the json
3115 ** column. Without such a constraint, the table cannot operate. idxNum is
3116 ** 1 if the constraint is found, 3 if the constraint and zRoot are found,
3117 ** and 0 otherwise.
3119 static int jsonEachBestIndex(
3120 sqlite3_vtab *tab,
3121 sqlite3_index_info *pIdxInfo
3123 int i; /* Loop counter or computed array index */
3124 int aIdx[2]; /* Index of constraints for JSON and ROOT */
3125 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
3126 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
3127 const struct sqlite3_index_constraint *pConstraint;
3129 /* This implementation assumes that JSON and ROOT are the last two
3130 ** columns in the table */
3131 assert( JEACH_ROOT == JEACH_JSON+1 );
3132 UNUSED_PARAMETER(tab);
3133 aIdx[0] = aIdx[1] = -1;
3134 pConstraint = pIdxInfo->aConstraint;
3135 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3136 int iCol;
3137 int iMask;
3138 if( pConstraint->iColumn < JEACH_JSON ) continue;
3139 iCol = pConstraint->iColumn - JEACH_JSON;
3140 assert( iCol==0 || iCol==1 );
3141 testcase( iCol==0 );
3142 iMask = 1 << iCol;
3143 if( pConstraint->usable==0 ){
3144 unusableMask |= iMask;
3145 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
3146 aIdx[iCol] = i;
3147 idxMask |= iMask;
3150 if( pIdxInfo->nOrderBy>0
3151 && pIdxInfo->aOrderBy[0].iColumn<0
3152 && pIdxInfo->aOrderBy[0].desc==0
3154 pIdxInfo->orderByConsumed = 1;
3157 if( (unusableMask & ~idxMask)!=0 ){
3158 /* If there are any unusable constraints on JSON or ROOT, then reject
3159 ** this entire plan */
3160 return SQLITE_CONSTRAINT;
3162 if( aIdx[0]<0 ){
3163 /* No JSON input. Leave estimatedCost at the huge value that it was
3164 ** initialized to to discourage the query planner from selecting this
3165 ** plan. */
3166 pIdxInfo->idxNum = 0;
3167 }else{
3168 pIdxInfo->estimatedCost = 1.0;
3169 i = aIdx[0];
3170 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
3171 pIdxInfo->aConstraintUsage[i].omit = 1;
3172 if( aIdx[1]<0 ){
3173 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
3174 }else{
3175 i = aIdx[1];
3176 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
3177 pIdxInfo->aConstraintUsage[i].omit = 1;
3178 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
3181 return SQLITE_OK;
3184 /* Start a search on a new JSON string */
3185 static int jsonEachFilter(
3186 sqlite3_vtab_cursor *cur,
3187 int idxNum, const char *idxStr,
3188 int argc, sqlite3_value **argv
3190 JsonEachCursor *p = (JsonEachCursor*)cur;
3191 const char *z;
3192 const char *zRoot = 0;
3193 sqlite3_int64 n;
3195 UNUSED_PARAMETER(idxStr);
3196 UNUSED_PARAMETER(argc);
3197 jsonEachCursorReset(p);
3198 if( idxNum==0 ) return SQLITE_OK;
3199 z = (const char*)sqlite3_value_text(argv[0]);
3200 if( z==0 ) return SQLITE_OK;
3201 n = sqlite3_value_bytes(argv[0]);
3202 p->zJson = sqlite3_malloc64( n+1 );
3203 if( p->zJson==0 ) return SQLITE_NOMEM;
3204 memcpy(p->zJson, z, (size_t)n+1);
3205 if( jsonParse(&p->sParse, 0, p->zJson) ){
3206 int rc = SQLITE_NOMEM;
3207 if( p->sParse.oom==0 ){
3208 sqlite3_free(cur->pVtab->zErrMsg);
3209 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
3210 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
3212 jsonEachCursorReset(p);
3213 return rc;
3214 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
3215 jsonEachCursorReset(p);
3216 return SQLITE_NOMEM;
3217 }else{
3218 JsonNode *pNode = 0;
3219 if( idxNum==3 ){
3220 const char *zErr = 0;
3221 zRoot = (const char*)sqlite3_value_text(argv[1]);
3222 if( zRoot==0 ) return SQLITE_OK;
3223 n = sqlite3_value_bytes(argv[1]);
3224 p->zRoot = sqlite3_malloc64( n+1 );
3225 if( p->zRoot==0 ) return SQLITE_NOMEM;
3226 memcpy(p->zRoot, zRoot, (size_t)n+1);
3227 if( zRoot[0]!='$' ){
3228 zErr = zRoot;
3229 }else{
3230 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
3232 if( zErr ){
3233 sqlite3_free(cur->pVtab->zErrMsg);
3234 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
3235 jsonEachCursorReset(p);
3236 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
3237 }else if( pNode==0 ){
3238 return SQLITE_OK;
3240 }else{
3241 pNode = p->sParse.aNode;
3243 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
3244 p->eType = pNode->eType;
3245 if( p->eType>=JSON_ARRAY ){
3246 assert( pNode->eU==0 );
3247 VVA( pNode->eU = 3 );
3248 pNode->u.iKey = 0;
3249 p->iEnd = p->i + pNode->n + 1;
3250 if( p->bRecursive ){
3251 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
3252 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
3253 p->i--;
3255 }else{
3256 p->i++;
3258 }else{
3259 p->iEnd = p->i+1;
3262 return SQLITE_OK;
3265 /* The methods of the json_each virtual table */
3266 static sqlite3_module jsonEachModule = {
3267 0, /* iVersion */
3268 0, /* xCreate */
3269 jsonEachConnect, /* xConnect */
3270 jsonEachBestIndex, /* xBestIndex */
3271 jsonEachDisconnect, /* xDisconnect */
3272 0, /* xDestroy */
3273 jsonEachOpenEach, /* xOpen - open a cursor */
3274 jsonEachClose, /* xClose - close a cursor */
3275 jsonEachFilter, /* xFilter - configure scan constraints */
3276 jsonEachNext, /* xNext - advance a cursor */
3277 jsonEachEof, /* xEof - check for end of scan */
3278 jsonEachColumn, /* xColumn - read data */
3279 jsonEachRowid, /* xRowid - read data */
3280 0, /* xUpdate */
3281 0, /* xBegin */
3282 0, /* xSync */
3283 0, /* xCommit */
3284 0, /* xRollback */
3285 0, /* xFindMethod */
3286 0, /* xRename */
3287 0, /* xSavepoint */
3288 0, /* xRelease */
3289 0, /* xRollbackTo */
3290 0 /* xShadowName */
3293 /* The methods of the json_tree virtual table. */
3294 static sqlite3_module jsonTreeModule = {
3295 0, /* iVersion */
3296 0, /* xCreate */
3297 jsonEachConnect, /* xConnect */
3298 jsonEachBestIndex, /* xBestIndex */
3299 jsonEachDisconnect, /* xDisconnect */
3300 0, /* xDestroy */
3301 jsonEachOpenTree, /* xOpen - open a cursor */
3302 jsonEachClose, /* xClose - close a cursor */
3303 jsonEachFilter, /* xFilter - configure scan constraints */
3304 jsonEachNext, /* xNext - advance a cursor */
3305 jsonEachEof, /* xEof - check for end of scan */
3306 jsonEachColumn, /* xColumn - read data */
3307 jsonEachRowid, /* xRowid - read data */
3308 0, /* xUpdate */
3309 0, /* xBegin */
3310 0, /* xSync */
3311 0, /* xCommit */
3312 0, /* xRollback */
3313 0, /* xFindMethod */
3314 0, /* xRename */
3315 0, /* xSavepoint */
3316 0, /* xRelease */
3317 0, /* xRollbackTo */
3318 0 /* xShadowName */
3320 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3321 #endif /* !defined(SQLITE_OMIT_JSON) */
3324 ** Register JSON functions.
3326 void sqlite3RegisterJsonFunctions(void){
3327 #ifndef SQLITE_OMIT_JSON
3328 static FuncDef aJsonFunc[] = {
3329 JFUNCTION(json, 1, 0, jsonRemoveFunc),
3330 JFUNCTION(json_array, -1, 0, jsonArrayFunc),
3331 JFUNCTION(json_array_length, 1, 0, jsonArrayLengthFunc),
3332 JFUNCTION(json_array_length, 2, 0, jsonArrayLengthFunc),
3333 JFUNCTION(json_error_position,1, 0, jsonErrorFunc),
3334 JFUNCTION(json_extract, -1, 0, jsonExtractFunc),
3335 JFUNCTION(->, 2, JSON_JSON, jsonExtractFunc),
3336 JFUNCTION(->>, 2, JSON_SQL, jsonExtractFunc),
3337 JFUNCTION(json_insert, -1, 0, jsonSetFunc),
3338 JFUNCTION(json_object, -1, 0, jsonObjectFunc),
3339 JFUNCTION(json_patch, 2, 0, jsonPatchFunc),
3340 JFUNCTION(json_quote, 1, 0, jsonQuoteFunc),
3341 JFUNCTION(json_remove, -1, 0, jsonRemoveFunc),
3342 JFUNCTION(json_replace, -1, 0, jsonReplaceFunc),
3343 JFUNCTION(json_set, -1, JSON_ISSET, jsonSetFunc),
3344 JFUNCTION(json_type, 1, 0, jsonTypeFunc),
3345 JFUNCTION(json_type, 2, 0, jsonTypeFunc),
3346 JFUNCTION(json_valid, 1, 0, jsonValidFunc),
3347 #if SQLITE_DEBUG
3348 JFUNCTION(json_parse, 1, 0, jsonParseFunc),
3349 JFUNCTION(json_test1, 1, 0, jsonTest1Func),
3350 #endif
3351 WAGGREGATE(json_group_array, 1, 0, 0,
3352 jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,
3353 SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC),
3354 WAGGREGATE(json_group_object, 2, 0, 0,
3355 jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,
3356 SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC)
3358 sqlite3InsertBuiltinFuncs(aJsonFunc, ArraySize(aJsonFunc));
3359 #endif
3362 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
3364 ** Register the JSON table-valued functions
3366 int sqlite3JsonTableFunctions(sqlite3 *db){
3367 int rc = SQLITE_OK;
3368 static const struct {
3369 const char *zName;
3370 sqlite3_module *pModule;
3371 } aMod[] = {
3372 { "json_each", &jsonEachModule },
3373 { "json_tree", &jsonTreeModule },
3375 unsigned int i;
3376 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
3377 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
3379 return rc;
3381 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) */