4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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)
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.
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 */
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 */
109 /* A single node of parsed JSON
112 u8 eType
; /* One of the JSON_ type values */
113 u8 jnFlags
; /* JNODE flags */
114 u8 eU
; /* Which union element to use */
115 u32 n
; /* Bytes of content, or number of sub-nodes */
117 const char *zJContent
; /* 1: Content for INT, REAL, and STRING */
118 u32 iAppend
; /* 2: More terms for ARRAY and OBJECT */
119 u32 iKey
; /* 3: Key for ARRAY objects in json_tree() */
120 u32 iReplace
; /* 4: Replacement content for JNODE_REPLACE */
121 JsonNode
*pPatch
; /* 5: Node chain of patch for JNODE_PATCH */
125 /* A completely parsed JSON string
128 u32 nNode
; /* Number of slots of aNode[] used */
129 u32 nAlloc
; /* Number of slots of aNode[] allocated */
130 JsonNode
*aNode
; /* Array of nodes containing the parse */
131 const char *zJson
; /* Original JSON string */
132 u32
*aUp
; /* Index of parent of each node */
133 u8 oom
; /* Set to true if out of memory */
134 u8 nErr
; /* Number of errors seen */
135 u16 iDepth
; /* Nesting depth */
136 int nJson
; /* Length of the zJson string in bytes */
137 u32 iHold
; /* Replace cache line with the lowest iHold value */
141 ** Maximum nesting depth of JSON for this implementation.
143 ** This limit is needed to avoid a stack overflow in the recursive
144 ** descent parser. A depth of 2000 is far deeper than any sane JSON
147 #define JSON_MAX_DEPTH 2000
149 /**************************************************************************
150 ** Utility routines for dealing with JsonString objects
151 **************************************************************************/
153 /* Set the JsonString object to an empty string
155 static void jsonZero(JsonString
*p
){
157 p
->nAlloc
= sizeof(p
->zSpace
);
162 /* Initialize the JsonString object
164 static void jsonInit(JsonString
*p
, sqlite3_context
*pCtx
){
171 /* Free all allocated memory and reset the JsonString object back to its
174 static void jsonReset(JsonString
*p
){
175 if( !p
->bStatic
) sqlite3_free(p
->zBuf
);
180 /* Report an out-of-memory (OOM) condition
182 static void jsonOom(JsonString
*p
){
184 sqlite3_result_error_nomem(p
->pCtx
);
188 /* Enlarge pJson->zBuf so that it can hold at least N more bytes.
189 ** Return zero on success. Return non-zero on an OOM error
191 static int jsonGrow(JsonString
*p
, u32 N
){
192 u64 nTotal
= N
<p
->nAlloc
? p
->nAlloc
*2 : p
->nAlloc
+N
+10;
195 if( p
->bErr
) return 1;
196 zNew
= sqlite3_malloc64(nTotal
);
201 memcpy(zNew
, p
->zBuf
, (size_t)p
->nUsed
);
205 zNew
= sqlite3_realloc64(p
->zBuf
, nTotal
);
216 /* Append N bytes from zIn onto the end of the JsonString string.
218 static void jsonAppendRaw(JsonString
*p
, const char *zIn
, u32 N
){
220 if( (N
+p
->nUsed
>= p
->nAlloc
) && jsonGrow(p
,N
)!=0 ) return;
221 memcpy(p
->zBuf
+p
->nUsed
, zIn
, N
);
225 /* Append formatted text (not to exceed N bytes) to the JsonString.
227 static void jsonPrintf(int N
, JsonString
*p
, const char *zFormat
, ...){
229 if( (p
->nUsed
+ N
>= p
->nAlloc
) && jsonGrow(p
, N
) ) return;
230 va_start(ap
, zFormat
);
231 sqlite3_vsnprintf(N
, p
->zBuf
+p
->nUsed
, zFormat
, ap
);
233 p
->nUsed
+= (int)strlen(p
->zBuf
+p
->nUsed
);
236 /* Append a single character
238 static void jsonAppendChar(JsonString
*p
, char c
){
239 if( p
->nUsed
>=p
->nAlloc
&& jsonGrow(p
,1)!=0 ) return;
240 p
->zBuf
[p
->nUsed
++] = c
;
243 /* Append a comma separator to the output buffer, if the previous
244 ** character is not '[' or '{'.
246 static void jsonAppendSeparator(JsonString
*p
){
248 if( p
->nUsed
==0 ) return;
249 c
= p
->zBuf
[p
->nUsed
-1];
250 if( c
!='[' && c
!='{' ) jsonAppendChar(p
, ',');
253 /* Append the N-byte string in zIn to the end of the JsonString string
254 ** under construction. Enclose the string in "..." and escape
255 ** any double-quotes or backslash characters contained within the
258 static void jsonAppendString(JsonString
*p
, const char *zIn
, u32 N
){
260 if( zIn
==0 || ((N
+p
->nUsed
+2 >= p
->nAlloc
) && jsonGrow(p
,N
+2)!=0) ) return;
261 p
->zBuf
[p
->nUsed
++] = '"';
263 unsigned char c
= ((unsigned const char*)zIn
)[i
];
264 if( c
=='"' || c
=='\\' ){
266 if( (p
->nUsed
+N
+3-i
> p
->nAlloc
) && jsonGrow(p
,N
+3-i
)!=0 ) return;
267 p
->zBuf
[p
->nUsed
++] = '\\';
269 static const char aSpecial
[] = {
270 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
271 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
273 assert( sizeof(aSpecial
)==32 );
274 assert( aSpecial
['\b']=='b' );
275 assert( aSpecial
['\f']=='f' );
276 assert( aSpecial
['\n']=='n' );
277 assert( aSpecial
['\r']=='r' );
278 assert( aSpecial
['\t']=='t' );
281 goto json_simple_escape
;
283 if( (p
->nUsed
+N
+7+i
> p
->nAlloc
) && jsonGrow(p
,N
+7-i
)!=0 ) return;
284 p
->zBuf
[p
->nUsed
++] = '\\';
285 p
->zBuf
[p
->nUsed
++] = 'u';
286 p
->zBuf
[p
->nUsed
++] = '0';
287 p
->zBuf
[p
->nUsed
++] = '0';
288 p
->zBuf
[p
->nUsed
++] = '0' + (c
>>4);
289 c
= "0123456789abcdef"[c
&0xf];
291 p
->zBuf
[p
->nUsed
++] = c
;
293 p
->zBuf
[p
->nUsed
++] = '"';
294 assert( p
->nUsed
<p
->nAlloc
);
298 ** Append a function parameter value to the JSON string under
301 static void jsonAppendValue(
302 JsonString
*p
, /* Append to this JSON string */
303 sqlite3_value
*pValue
/* Value to append */
305 switch( sqlite3_value_type(pValue
) ){
307 jsonAppendRaw(p
, "null", 4);
312 const char *z
= (const char*)sqlite3_value_text(pValue
);
313 u32 n
= (u32
)sqlite3_value_bytes(pValue
);
314 jsonAppendRaw(p
, z
, n
);
318 const char *z
= (const char*)sqlite3_value_text(pValue
);
319 u32 n
= (u32
)sqlite3_value_bytes(pValue
);
320 if( sqlite3_value_subtype(pValue
)==JSON_SUBTYPE
){
321 jsonAppendRaw(p
, z
, n
);
323 jsonAppendString(p
, z
, n
);
329 sqlite3_result_error(p
->pCtx
, "JSON cannot hold BLOB values", -1);
339 /* Make the JSON in p the result of the SQL function.
341 static void jsonResult(JsonString
*p
){
343 sqlite3_result_text64(p
->pCtx
, p
->zBuf
, p
->nUsed
,
344 p
->bStatic
? SQLITE_TRANSIENT
: sqlite3_free
,
348 assert( p
->bStatic
);
351 /**************************************************************************
352 ** Utility routines for dealing with JsonNode and JsonParse objects
353 **************************************************************************/
356 ** Return the number of consecutive JsonNode slots need to represent
357 ** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
358 ** OBJECT types, the number might be larger.
360 ** Appended elements are not counted. The value returned is the number
361 ** by which the JsonNode counter should increment in order to go to the
364 static u32
jsonNodeSize(JsonNode
*pNode
){
365 return pNode
->eType
>=JSON_ARRAY
? pNode
->n
+1 : 1;
369 ** Reclaim all memory allocated by a JsonParse object. But do not
370 ** delete the JsonParse object itself.
372 static void jsonParseReset(JsonParse
*pParse
){
373 sqlite3_free(pParse
->aNode
);
377 sqlite3_free(pParse
->aUp
);
382 ** Free a JsonParse object that was obtained from sqlite3_malloc().
384 static void jsonParseFree(JsonParse
*pParse
){
385 jsonParseReset(pParse
);
386 sqlite3_free(pParse
);
390 ** Convert the JsonNode pNode into a pure JSON string and
391 ** append to pOut. Subsubstructure is also included. Return
392 ** the number of JsonNode objects that are encoded.
394 static void jsonRenderNode(
395 JsonNode
*pNode
, /* The node to render */
396 JsonString
*pOut
, /* Write JSON here */
397 sqlite3_value
**aReplace
/* Replacement values */
400 if( pNode
->jnFlags
& (JNODE_REPLACE
|JNODE_PATCH
) ){
401 if( (pNode
->jnFlags
& JNODE_REPLACE
)!=0 && ALWAYS(aReplace
!=0) ){
402 assert( pNode
->eU
==4 );
403 jsonAppendValue(pOut
, aReplace
[pNode
->u
.iReplace
]);
406 assert( pNode
->eU
==5 );
407 pNode
= pNode
->u
.pPatch
;
409 switch( pNode
->eType
){
411 assert( pNode
->eType
==JSON_NULL
);
412 jsonAppendRaw(pOut
, "null", 4);
416 jsonAppendRaw(pOut
, "true", 4);
420 jsonAppendRaw(pOut
, "false", 5);
424 if( pNode
->jnFlags
& JNODE_RAW
){
425 assert( pNode
->eU
==1 );
426 jsonAppendString(pOut
, pNode
->u
.zJContent
, pNode
->n
);
429 /* no break */ deliberate_fall_through
433 assert( pNode
->eU
==1 );
434 jsonAppendRaw(pOut
, pNode
->u
.zJContent
, pNode
->n
);
439 jsonAppendChar(pOut
, '[');
441 while( j
<=pNode
->n
){
442 if( (pNode
[j
].jnFlags
& JNODE_REMOVE
)==0 ){
443 jsonAppendSeparator(pOut
);
444 jsonRenderNode(&pNode
[j
], pOut
, aReplace
);
446 j
+= jsonNodeSize(&pNode
[j
]);
448 if( (pNode
->jnFlags
& JNODE_APPEND
)==0 ) break;
449 assert( pNode
->eU
==2 );
450 pNode
= &pNode
[pNode
->u
.iAppend
];
453 jsonAppendChar(pOut
, ']');
458 jsonAppendChar(pOut
, '{');
460 while( j
<=pNode
->n
){
461 if( (pNode
[j
+1].jnFlags
& JNODE_REMOVE
)==0 ){
462 jsonAppendSeparator(pOut
);
463 jsonRenderNode(&pNode
[j
], pOut
, aReplace
);
464 jsonAppendChar(pOut
, ':');
465 jsonRenderNode(&pNode
[j
+1], pOut
, aReplace
);
467 j
+= 1 + jsonNodeSize(&pNode
[j
+1]);
469 if( (pNode
->jnFlags
& JNODE_APPEND
)==0 ) break;
470 assert( pNode
->eU
==2 );
471 pNode
= &pNode
[pNode
->u
.iAppend
];
474 jsonAppendChar(pOut
, '}');
481 ** Return a JsonNode and all its descendents as a JSON string.
483 static void jsonReturnJson(
484 JsonNode
*pNode
, /* Node to return */
485 sqlite3_context
*pCtx
, /* Return value for this function */
486 sqlite3_value
**aReplace
/* Array of replacement values */
490 jsonRenderNode(pNode
, &s
, aReplace
);
492 sqlite3_result_subtype(pCtx
, JSON_SUBTYPE
);
496 ** Translate a single byte of Hex into an integer.
497 ** This routine only works if h really is a valid hexadecimal
498 ** character: 0..9a..fA..F
500 static u8
jsonHexToInt(int h
){
501 assert( (h
>='0' && h
<='9') || (h
>='a' && h
<='f') || (h
>='A' && h
<='F') );
507 return (u8
)(h
& 0xf);
511 ** Convert a 4-byte hex string into an integer
513 static u32
jsonHexToInt4(const char *z
){
515 assert( sqlite3Isxdigit(z
[0]) );
516 assert( sqlite3Isxdigit(z
[1]) );
517 assert( sqlite3Isxdigit(z
[2]) );
518 assert( sqlite3Isxdigit(z
[3]) );
519 v
= (jsonHexToInt(z
[0])<<12)
520 + (jsonHexToInt(z
[1])<<8)
521 + (jsonHexToInt(z
[2])<<4)
522 + jsonHexToInt(z
[3]);
527 ** Make the JsonNode the return value of the function.
529 static void jsonReturn(
530 JsonNode
*pNode
, /* Node to return */
531 sqlite3_context
*pCtx
, /* Return value for this function */
532 sqlite3_value
**aReplace
/* Array of replacement values */
534 switch( pNode
->eType
){
536 assert( pNode
->eType
==JSON_NULL
);
537 sqlite3_result_null(pCtx
);
541 sqlite3_result_int(pCtx
, 1);
545 sqlite3_result_int(pCtx
, 0);
551 assert( pNode
->eU
==1 );
552 z
= pNode
->u
.zJContent
;
553 if( z
[0]=='-' ){ z
++; }
554 while( z
[0]>='0' && z
[0]<='9' ){
555 unsigned v
= *(z
++) - '0';
556 if( i
>=LARGEST_INT64
/10 ){
557 if( i
>LARGEST_INT64
/10 ) goto int_as_real
;
558 if( z
[0]>='0' && z
[0]<='9' ) goto int_as_real
;
559 if( v
==9 ) goto int_as_real
;
561 if( pNode
->u
.zJContent
[0]=='-' ){
562 sqlite3_result_int64(pCtx
, SMALLEST_INT64
);
571 if( pNode
->u
.zJContent
[0]=='-' ){ i
= -i
; }
572 sqlite3_result_int64(pCtx
, i
);
575 int_as_real
: ; /* no break */ deliberate_fall_through
579 #ifdef SQLITE_AMALGAMATION
581 assert( pNode
->eU
==1 );
582 z
= pNode
->u
.zJContent
;
583 sqlite3AtoF(z
, &r
, sqlite3Strlen30(z
), SQLITE_UTF8
);
585 assert( pNode
->eU
==1 );
586 r
= strtod(pNode
->u
.zJContent
, 0);
588 sqlite3_result_double(pCtx
, r
);
592 #if 0 /* Never happens because JNODE_RAW is only set by json_set(),
593 ** json_insert() and json_replace() and those routines do not
594 ** call jsonReturn() */
595 if( pNode
->jnFlags
& JNODE_RAW
){
596 assert( pNode
->eU
==1 );
597 sqlite3_result_text(pCtx
, pNode
->u
.zJContent
, pNode
->n
,
601 assert( (pNode
->jnFlags
& JNODE_RAW
)==0 );
602 if( (pNode
->jnFlags
& JNODE_ESCAPE
)==0 ){
603 /* JSON formatted without any backslash-escapes */
604 assert( pNode
->eU
==1 );
605 sqlite3_result_text(pCtx
, pNode
->u
.zJContent
+1, pNode
->n
-2,
608 /* Translate JSON formatted string into raw text */
614 assert( pNode
->eU
==1 );
615 z
= pNode
->u
.zJContent
;
616 zOut
= sqlite3_malloc( n
+1 );
618 sqlite3_result_error_nomem(pCtx
);
621 for(i
=1, j
=0; i
<n
-1; i
++){
628 u32 v
= jsonHexToInt4(z
+i
+1);
633 }else if( v
<=0x7ff ){
634 zOut
[j
++] = (char)(0xc0 | (v
>>6));
635 zOut
[j
++] = 0x80 | (v
&0x3f);
638 if( (v
&0xfc00)==0xd800
642 && ((vlo
= jsonHexToInt4(z
+i
+3))&0xfc00)==0xdc00
644 /* We have a surrogate pair */
645 v
= ((v
&0x3ff)<<10) + (vlo
&0x3ff) + 0x10000;
647 zOut
[j
++] = 0xf0 | (v
>>18);
648 zOut
[j
++] = 0x80 | ((v
>>12)&0x3f);
649 zOut
[j
++] = 0x80 | ((v
>>6)&0x3f);
650 zOut
[j
++] = 0x80 | (v
&0x3f);
652 zOut
[j
++] = 0xe0 | (v
>>12);
653 zOut
[j
++] = 0x80 | ((v
>>6)&0x3f);
654 zOut
[j
++] = 0x80 | (v
&0x3f);
674 sqlite3_result_text(pCtx
, zOut
, j
, sqlite3_free
);
680 jsonReturnJson(pNode
, pCtx
, aReplace
);
686 /* Forward reference */
687 static int jsonParseAddNode(JsonParse
*,u32
,u32
,const char*);
690 ** A macro to hint to the compiler that a function should not be
693 #if defined(__GNUC__)
694 # define JSON_NOINLINE __attribute__((noinline))
695 #elif defined(_MSC_VER) && _MSC_VER>=1310
696 # define JSON_NOINLINE __declspec(noinline)
698 # define JSON_NOINLINE
702 static JSON_NOINLINE
int jsonParseAddNodeExpand(
703 JsonParse
*pParse
, /* Append the node to this object */
704 u32 eType
, /* Node type */
705 u32 n
, /* Content size or sub-node count */
706 const char *zContent
/* Content */
710 assert( pParse
->nNode
>=pParse
->nAlloc
);
711 if( pParse
->oom
) return -1;
712 nNew
= pParse
->nAlloc
*2 + 10;
713 pNew
= sqlite3_realloc64(pParse
->aNode
, sizeof(JsonNode
)*nNew
);
718 pParse
->nAlloc
= nNew
;
719 pParse
->aNode
= pNew
;
720 assert( pParse
->nNode
<pParse
->nAlloc
);
721 return jsonParseAddNode(pParse
, eType
, n
, zContent
);
725 ** Create a new JsonNode instance based on the arguments and append that
726 ** instance to the JsonParse. Return the index in pParse->aNode[] of the
727 ** new node, or -1 if a memory allocation fails.
729 static int jsonParseAddNode(
730 JsonParse
*pParse
, /* Append the node to this object */
731 u32 eType
, /* Node type */
732 u32 n
, /* Content size or sub-node count */
733 const char *zContent
/* Content */
736 if( pParse
->aNode
==0 || pParse
->nNode
>=pParse
->nAlloc
){
737 return jsonParseAddNodeExpand(pParse
, eType
, n
, zContent
);
739 p
= &pParse
->aNode
[pParse
->nNode
];
740 p
->eType
= (u8
)eType
;
742 VVA( p
->eU
= zContent
? 1 : 0 );
744 p
->u
.zJContent
= zContent
;
745 return pParse
->nNode
++;
749 ** Return true if z[] begins with 4 (or more) hexadecimal digits
751 static int jsonIs4Hex(const char *z
){
753 for(i
=0; i
<4; i
++) if( !sqlite3Isxdigit(z
[i
]) ) return 0;
758 ** Parse a single JSON value which begins at pParse->zJson[i]. Return the
759 ** index of the first character past the end of the value parsed.
761 ** Return negative for a syntax error. Special cases: return -2 if the
762 ** first non-whitespace character is '}' and return -3 if the first
763 ** non-whitespace character is ']'.
765 static int jsonParseValue(JsonParse
*pParse
, u32 i
){
771 const char *z
= pParse
->zJson
;
772 while( fast_isspace(z
[i
]) ){ i
++; }
773 if( (c
= z
[i
])=='{' ){
775 iThis
= jsonParseAddNode(pParse
, JSON_OBJECT
, 0, 0);
776 if( iThis
<0 ) return -1;
778 while( fast_isspace(z
[j
]) ){ j
++; }
779 if( ++pParse
->iDepth
> JSON_MAX_DEPTH
) return -1;
780 x
= jsonParseValue(pParse
, j
);
783 if( x
==(-2) && pParse
->nNode
==(u32
)iThis
+1 ) return j
+1;
786 if( pParse
->oom
) return -1;
787 pNode
= &pParse
->aNode
[pParse
->nNode
-1];
788 if( pNode
->eType
!=JSON_STRING
) return -1;
789 pNode
->jnFlags
|= JNODE_LABEL
;
791 while( fast_isspace(z
[j
]) ){ j
++; }
792 if( z
[j
]!=':' ) return -1;
794 x
= jsonParseValue(pParse
, j
);
798 while( fast_isspace(z
[j
]) ){ j
++; }
800 if( c
==',' ) continue;
801 if( c
!='}' ) return -1;
804 pParse
->aNode
[iThis
].n
= pParse
->nNode
- (u32
)iThis
- 1;
808 iThis
= jsonParseAddNode(pParse
, JSON_ARRAY
, 0, 0);
809 if( iThis
<0 ) return -1;
810 memset(&pParse
->aNode
[iThis
].u
, 0, sizeof(pParse
->aNode
[iThis
].u
));
812 while( fast_isspace(z
[j
]) ){ j
++; }
813 if( ++pParse
->iDepth
> JSON_MAX_DEPTH
) return -1;
814 x
= jsonParseValue(pParse
, j
);
817 if( x
==(-3) && pParse
->nNode
==(u32
)iThis
+1 ) return j
+1;
821 while( fast_isspace(z
[j
]) ){ j
++; }
823 if( c
==',' ) continue;
824 if( c
!=']' ) return -1;
827 pParse
->aNode
[iThis
].n
= pParse
->nNode
- (u32
)iThis
- 1;
835 if( (c
& ~0x1f)==0 ){
836 /* Control characters are not allowed in strings */
841 if( c
=='"' || c
=='\\' || c
=='/' || c
=='b' || c
=='f'
842 || c
=='n' || c
=='r' || c
=='t'
843 || (c
=='u' && jsonIs4Hex(z
+j
+1)) ){
844 jnFlags
= JNODE_ESCAPE
;
853 jsonParseAddNode(pParse
, JSON_STRING
, j
+1-i
, &z
[i
]);
854 if( !pParse
->oom
) pParse
->aNode
[pParse
->nNode
-1].jnFlags
= jnFlags
;
857 && strncmp(z
+i
,"null",4)==0
858 && !sqlite3Isalnum(z
[i
+4]) ){
859 jsonParseAddNode(pParse
, JSON_NULL
, 0, 0);
862 && strncmp(z
+i
,"true",4)==0
863 && !sqlite3Isalnum(z
[i
+4]) ){
864 jsonParseAddNode(pParse
, JSON_TRUE
, 0, 0);
867 && strncmp(z
+i
,"false",5)==0
868 && !sqlite3Isalnum(z
[i
+5]) ){
869 jsonParseAddNode(pParse
, JSON_FALSE
, 0, 0);
871 }else if( c
=='-' || (c
>='0' && c
<='9') ){
877 j
= c
=='-' ? i
+1 : i
;
878 if( z
[j
]=='0' && z
[j
+1]>='0' && z
[j
+1]<='9' ) return -1;
883 if( c
>='0' && c
<='9' ) continue;
885 if( z
[j
-1]=='-' ) return -1;
886 if( seenDP
) return -1;
890 if( c
=='e' || c
=='E' ){
891 if( z
[j
-1]<'0' ) return -1;
892 if( seenE
) return -1;
895 if( c
=='+' || c
=='-' ){
899 if( c
<'0' || c
>'9' ) return -1;
904 if( z
[j
-1]<'0' ) return -1;
905 jsonParseAddNode(pParse
, seenDP
? JSON_REAL
: JSON_INT
,
909 return -2; /* End of {...} */
911 return -3; /* End of [...] */
913 return 0; /* End of file */
915 return -1; /* Syntax error */
920 ** Parse a complete JSON string. Return 0 on success or non-zero if there
921 ** are any errors. If an error occurs, free all memory associated with
924 ** pParse is uninitialized when this routine is called.
926 static int jsonParse(
927 JsonParse
*pParse
, /* Initialize and fill this JsonParse object */
928 sqlite3_context
*pCtx
, /* Report errors here */
929 const char *zJson
/* Input JSON text to be parsed */
932 memset(pParse
, 0, sizeof(*pParse
));
933 if( zJson
==0 ) return 1;
934 pParse
->zJson
= zJson
;
935 i
= jsonParseValue(pParse
, 0);
936 if( pParse
->oom
) i
= -1;
938 assert( pParse
->iDepth
==0 );
939 while( fast_isspace(zJson
[i
]) ) i
++;
940 if( zJson
[i
] ) i
= -1;
945 sqlite3_result_error_nomem(pCtx
);
947 sqlite3_result_error(pCtx
, "malformed JSON", -1);
950 jsonParseReset(pParse
);
956 /* Mark node i of pParse as being a child of iParent. Call recursively
957 ** to fill in all the descendants of node i.
959 static void jsonParseFillInParentage(JsonParse
*pParse
, u32 i
, u32 iParent
){
960 JsonNode
*pNode
= &pParse
->aNode
[i
];
962 pParse
->aUp
[i
] = iParent
;
963 switch( pNode
->eType
){
965 for(j
=1; j
<=pNode
->n
; j
+= jsonNodeSize(pNode
+j
)){
966 jsonParseFillInParentage(pParse
, i
+j
, i
);
971 for(j
=1; j
<=pNode
->n
; j
+= jsonNodeSize(pNode
+j
+1)+1){
972 pParse
->aUp
[i
+j
] = i
;
973 jsonParseFillInParentage(pParse
, i
+j
+1, i
);
984 ** Compute the parentage of all nodes in a completed parse.
986 static int jsonParseFindParents(JsonParse
*pParse
){
988 assert( pParse
->aUp
==0 );
989 aUp
= pParse
->aUp
= sqlite3_malloc64( sizeof(u32
)*pParse
->nNode
);
994 jsonParseFillInParentage(pParse
, 0, 0);
999 ** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
1001 #define JSON_CACHE_ID (-429938) /* First cache entry */
1002 #define JSON_CACHE_SZ 4 /* Max number of cache entries */
1005 ** Obtain a complete parse of the JSON found in the first argument
1006 ** of the argv array. Use the sqlite3_get_auxdata() cache for this
1007 ** parse if it is available. If the cache is not available or if it
1008 ** is no longer valid, parse the JSON again and return the new parse,
1009 ** and also register the new parse so that it will be available for
1010 ** future sqlite3_get_auxdata() calls.
1012 static JsonParse
*jsonParseCached(
1013 sqlite3_context
*pCtx
,
1014 sqlite3_value
**argv
,
1015 sqlite3_context
*pErrCtx
1017 const char *zJson
= (const char*)sqlite3_value_text(argv
[0]);
1018 int nJson
= sqlite3_value_bytes(argv
[0]);
1020 JsonParse
*pMatch
= 0;
1023 u32 iMinHold
= 0xffffffff;
1025 if( zJson
==0 ) return 0;
1026 for(iKey
=0; iKey
<JSON_CACHE_SZ
; iKey
++){
1027 p
= (JsonParse
*)sqlite3_get_auxdata(pCtx
, JSON_CACHE_ID
+iKey
);
1034 && memcmp(p
->zJson
,zJson
,nJson
)==0
1038 }else if( p
->iHold
<iMinHold
){
1039 iMinHold
= p
->iHold
;
1042 if( p
->iHold
>iMaxHold
){
1043 iMaxHold
= p
->iHold
;
1048 pMatch
->iHold
= iMaxHold
+1;
1051 p
= sqlite3_malloc64( sizeof(*p
) + nJson
+ 1 );
1053 sqlite3_result_error_nomem(pCtx
);
1056 memset(p
, 0, sizeof(*p
));
1057 p
->zJson
= (char*)&p
[1];
1058 memcpy((char*)p
->zJson
, zJson
, nJson
+1);
1059 if( jsonParse(p
, pErrCtx
, p
->zJson
) ){
1064 p
->iHold
= iMaxHold
+1;
1065 sqlite3_set_auxdata(pCtx
, JSON_CACHE_ID
+iMinKey
, p
,
1066 (void(*)(void*))jsonParseFree
);
1067 return (JsonParse
*)sqlite3_get_auxdata(pCtx
, JSON_CACHE_ID
+iMinKey
);
1071 ** Compare the OBJECT label at pNode against zKey,nKey. Return true on
1074 static int jsonLabelCompare(JsonNode
*pNode
, const char *zKey
, u32 nKey
){
1075 assert( pNode
->eU
==1 );
1076 if( pNode
->jnFlags
& JNODE_RAW
){
1077 if( pNode
->n
!=nKey
) return 0;
1078 return strncmp(pNode
->u
.zJContent
, zKey
, nKey
)==0;
1080 if( pNode
->n
!=nKey
+2 ) return 0;
1081 return strncmp(pNode
->u
.zJContent
+1, zKey
, nKey
)==0;
1085 /* forward declaration */
1086 static JsonNode
*jsonLookupAppend(JsonParse
*,const char*,int*,const char**);
1089 ** Search along zPath to find the node specified. Return a pointer
1090 ** to that node, or NULL if zPath is malformed or if there is no such
1093 ** If pApnd!=0, then try to append new nodes to complete zPath if it is
1094 ** possible to do so and if no existing node corresponds to zPath. If
1095 ** new nodes are appended *pApnd is set to 1.
1097 static JsonNode
*jsonLookupStep(
1098 JsonParse
*pParse
, /* The JSON to search */
1099 u32 iRoot
, /* Begin the search at this node */
1100 const char *zPath
, /* The path to search */
1101 int *pApnd
, /* Append nodes to complete path if not NULL */
1102 const char **pzErr
/* Make *pzErr point to any syntax error in zPath */
1106 JsonNode
*pRoot
= &pParse
->aNode
[iRoot
];
1107 if( zPath
[0]==0 ) return pRoot
;
1108 if( pRoot
->jnFlags
& JNODE_REPLACE
) return 0;
1109 if( zPath
[0]=='.' ){
1110 if( pRoot
->eType
!=JSON_OBJECT
) return 0;
1112 if( zPath
[0]=='"' ){
1114 for(i
=1; zPath
[i
] && zPath
[i
]!='"'; i
++){}
1122 testcase( nKey
==0 );
1125 for(i
=0; zPath
[i
] && zPath
[i
]!='.' && zPath
[i
]!='['; i
++){}
1134 while( j
<=pRoot
->n
){
1135 if( jsonLabelCompare(pRoot
+j
, zKey
, nKey
) ){
1136 return jsonLookupStep(pParse
, iRoot
+j
+1, &zPath
[i
], pApnd
, pzErr
);
1139 j
+= jsonNodeSize(&pRoot
[j
]);
1141 if( (pRoot
->jnFlags
& JNODE_APPEND
)==0 ) break;
1142 assert( pRoot
->eU
==2 );
1143 iRoot
+= pRoot
->u
.iAppend
;
1144 pRoot
= &pParse
->aNode
[iRoot
];
1150 iStart
= jsonParseAddNode(pParse
, JSON_OBJECT
, 2, 0);
1151 iLabel
= jsonParseAddNode(pParse
, JSON_STRING
, nKey
, zKey
);
1153 pNode
= jsonLookupAppend(pParse
, zPath
, pApnd
, pzErr
);
1154 if( pParse
->oom
) return 0;
1156 pRoot
= &pParse
->aNode
[iRoot
];
1157 assert( pRoot
->eU
==0 );
1158 pRoot
->u
.iAppend
= iStart
- iRoot
;
1159 pRoot
->jnFlags
|= JNODE_APPEND
;
1160 VVA( pRoot
->eU
= 2 );
1161 pParse
->aNode
[iLabel
].jnFlags
|= JNODE_RAW
;
1165 }else if( zPath
[0]=='[' ){
1168 while( sqlite3Isdigit(zPath
[j
]) ){
1169 i
= i
*10 + zPath
[j
] - '0';
1172 if( j
<2 || zPath
[j
]!=']' ){
1173 if( zPath
[1]=='#' ){
1174 JsonNode
*pBase
= pRoot
;
1176 if( pRoot
->eType
!=JSON_ARRAY
) return 0;
1178 while( j
<=pBase
->n
){
1179 if( (pBase
[j
].jnFlags
& JNODE_REMOVE
)==0 ) i
++;
1180 j
+= jsonNodeSize(&pBase
[j
]);
1182 if( (pBase
->jnFlags
& JNODE_APPEND
)==0 ) break;
1183 assert( pBase
->eU
==2 );
1184 iBase
+= pBase
->u
.iAppend
;
1185 pBase
= &pParse
->aNode
[iBase
];
1189 if( zPath
[2]=='-' && sqlite3Isdigit(zPath
[3]) ){
1193 x
= x
*10 + zPath
[j
] - '0';
1195 }while( sqlite3Isdigit(zPath
[j
]) );
1199 if( zPath
[j
]!=']' ){
1208 if( pRoot
->eType
!=JSON_ARRAY
) return 0;
1212 while( j
<=pRoot
->n
&& (i
>0 || (pRoot
[j
].jnFlags
& JNODE_REMOVE
)!=0) ){
1213 if( (pRoot
[j
].jnFlags
& JNODE_REMOVE
)==0 ) i
--;
1214 j
+= jsonNodeSize(&pRoot
[j
]);
1216 if( (pRoot
->jnFlags
& JNODE_APPEND
)==0 ) break;
1217 assert( pRoot
->eU
==2 );
1218 iRoot
+= pRoot
->u
.iAppend
;
1219 pRoot
= &pParse
->aNode
[iRoot
];
1223 return jsonLookupStep(pParse
, iRoot
+j
, zPath
, pApnd
, pzErr
);
1225 if( i
==0 && pApnd
){
1228 iStart
= jsonParseAddNode(pParse
, JSON_ARRAY
, 1, 0);
1229 pNode
= jsonLookupAppend(pParse
, zPath
, pApnd
, pzErr
);
1230 if( pParse
->oom
) return 0;
1232 pRoot
= &pParse
->aNode
[iRoot
];
1233 assert( pRoot
->eU
==0 );
1234 pRoot
->u
.iAppend
= iStart
- iRoot
;
1235 pRoot
->jnFlags
|= JNODE_APPEND
;
1236 VVA( pRoot
->eU
= 2 );
1247 ** Append content to pParse that will complete zPath. Return a pointer
1248 ** to the inserted node, or return NULL if the append fails.
1250 static JsonNode
*jsonLookupAppend(
1251 JsonParse
*pParse
, /* Append content to the JSON parse */
1252 const char *zPath
, /* Description of content to append */
1253 int *pApnd
, /* Set this flag to 1 */
1254 const char **pzErr
/* Make this point to any syntax error */
1258 jsonParseAddNode(pParse
, JSON_NULL
, 0, 0);
1259 return pParse
->oom
? 0 : &pParse
->aNode
[pParse
->nNode
-1];
1261 if( zPath
[0]=='.' ){
1262 jsonParseAddNode(pParse
, JSON_OBJECT
, 0, 0);
1263 }else if( strncmp(zPath
,"[0]",3)==0 ){
1264 jsonParseAddNode(pParse
, JSON_ARRAY
, 0, 0);
1268 if( pParse
->oom
) return 0;
1269 return jsonLookupStep(pParse
, pParse
->nNode
-1, zPath
, pApnd
, pzErr
);
1273 ** Return the text of a syntax error message on a JSON path. Space is
1274 ** obtained from sqlite3_malloc().
1276 static char *jsonPathSyntaxError(const char *zErr
){
1277 return sqlite3_mprintf("JSON path error near '%q'", zErr
);
1281 ** Do a node lookup using zPath. Return a pointer to the node on success.
1282 ** Return NULL if not found or if there is an error.
1284 ** On an error, write an error message into pCtx and increment the
1285 ** pParse->nErr counter.
1287 ** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
1288 ** nodes are appended.
1290 static JsonNode
*jsonLookup(
1291 JsonParse
*pParse
, /* The JSON to search */
1292 const char *zPath
, /* The path to search */
1293 int *pApnd
, /* Append nodes to complete path if not NULL */
1294 sqlite3_context
*pCtx
/* Report errors here, if not NULL */
1296 const char *zErr
= 0;
1297 JsonNode
*pNode
= 0;
1300 if( zPath
==0 ) return 0;
1301 if( zPath
[0]!='$' ){
1306 pNode
= jsonLookupStep(pParse
, 0, zPath
, pApnd
, &zErr
);
1307 if( zErr
==0 ) return pNode
;
1311 assert( zErr
!=0 && pCtx
!=0 );
1312 zMsg
= jsonPathSyntaxError(zErr
);
1314 sqlite3_result_error(pCtx
, zMsg
, -1);
1317 sqlite3_result_error_nomem(pCtx
);
1324 ** Report the wrong number of arguments for json_insert(), json_replace()
1327 static void jsonWrongNumArgs(
1328 sqlite3_context
*pCtx
,
1329 const char *zFuncName
1331 char *zMsg
= sqlite3_mprintf("json_%s() needs an odd number of arguments",
1333 sqlite3_result_error(pCtx
, zMsg
, -1);
1338 ** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
1340 static void jsonRemoveAllNulls(JsonNode
*pNode
){
1342 assert( pNode
->eType
==JSON_OBJECT
);
1344 for(i
=2; i
<=n
; i
+= jsonNodeSize(&pNode
[i
])+1){
1345 switch( pNode
[i
].eType
){
1347 pNode
[i
].jnFlags
|= JNODE_REMOVE
;
1350 jsonRemoveAllNulls(&pNode
[i
]);
1357 /****************************************************************************
1358 ** SQL functions used for testing and debugging
1359 ****************************************************************************/
1363 ** The json_parse(JSON) function returns a string which describes
1364 ** a parse of the JSON provided. Or it returns NULL if JSON is not
1367 static void jsonParseFunc(
1368 sqlite3_context
*ctx
,
1370 sqlite3_value
**argv
1372 JsonString s
; /* Output string - not real JSON */
1373 JsonParse x
; /* The parse */
1377 if( jsonParse(&x
, ctx
, (const char*)sqlite3_value_text(argv
[0])) ) return;
1378 jsonParseFindParents(&x
);
1380 for(i
=0; i
<x
.nNode
; i
++){
1382 if( x
.aNode
[i
].jnFlags
& JNODE_LABEL
){
1383 assert( x
.aNode
[i
].eType
==JSON_STRING
);
1386 zType
= jsonType
[x
.aNode
[i
].eType
];
1388 jsonPrintf(100, &s
,"node %3u: %7s n=%-4d up=%-4d",
1389 i
, zType
, x
.aNode
[i
].n
, x
.aUp
[i
]);
1390 assert( x
.aNode
[i
].eU
==0 || x
.aNode
[i
].eU
==1 );
1391 if( x
.aNode
[i
].u
.zJContent
!=0 ){
1392 assert( x
.aNode
[i
].eU
==1 );
1393 jsonAppendRaw(&s
, " ", 1);
1394 jsonAppendRaw(&s
, x
.aNode
[i
].u
.zJContent
, x
.aNode
[i
].n
);
1396 assert( x
.aNode
[i
].eU
==0 );
1398 jsonAppendRaw(&s
, "\n", 1);
1405 ** The json_test1(JSON) function return true (1) if the input is JSON
1406 ** text generated by another json function. It returns (0) if the input
1407 ** is not known to be JSON.
1409 static void jsonTest1Func(
1410 sqlite3_context
*ctx
,
1412 sqlite3_value
**argv
1414 UNUSED_PARAMETER(argc
);
1415 sqlite3_result_int(ctx
, sqlite3_value_subtype(argv
[0])==JSON_SUBTYPE
);
1417 #endif /* SQLITE_DEBUG */
1419 /****************************************************************************
1420 ** Scalar SQL function implementations
1421 ****************************************************************************/
1424 ** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
1425 ** corresponding to the SQL value input. Mostly this means putting
1426 ** double-quotes around strings and returning the unquoted string "null"
1427 ** when given a NULL input.
1429 static void jsonQuoteFunc(
1430 sqlite3_context
*ctx
,
1432 sqlite3_value
**argv
1435 UNUSED_PARAMETER(argc
);
1438 jsonAppendValue(&jx
, argv
[0]);
1440 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
1444 ** Implementation of the json_array(VALUE,...) function. Return a JSON
1445 ** array that contains all values given in arguments. Or if any argument
1446 ** is a BLOB, throw an error.
1448 static void jsonArrayFunc(
1449 sqlite3_context
*ctx
,
1451 sqlite3_value
**argv
1457 jsonAppendChar(&jx
, '[');
1458 for(i
=0; i
<argc
; i
++){
1459 jsonAppendSeparator(&jx
);
1460 jsonAppendValue(&jx
, argv
[i
]);
1462 jsonAppendChar(&jx
, ']');
1464 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
1469 ** json_array_length(JSON)
1470 ** json_array_length(JSON, PATH)
1472 ** Return the number of elements in the top-level JSON array.
1473 ** Return 0 if the input is not a well-formed JSON array.
1475 static void jsonArrayLengthFunc(
1476 sqlite3_context
*ctx
,
1478 sqlite3_value
**argv
1480 JsonParse
*p
; /* The parse */
1481 sqlite3_int64 n
= 0;
1485 p
= jsonParseCached(ctx
, argv
, ctx
);
1489 const char *zPath
= (const char*)sqlite3_value_text(argv
[1]);
1490 pNode
= jsonLookup(p
, zPath
, 0, ctx
);
1497 if( pNode
->eType
==JSON_ARRAY
){
1498 assert( (pNode
->jnFlags
& JNODE_APPEND
)==0 );
1499 for(i
=1; i
<=pNode
->n
; n
++){
1500 i
+= jsonNodeSize(&pNode
[i
]);
1503 sqlite3_result_int64(ctx
, n
);
1507 ** Bit values for the flags passed into jsonExtractFunc() or
1508 ** jsonSetFunc() via the user-data value.
1510 #define JSON_JSON 0x01 /* Result is always JSON */
1511 #define JSON_SQL 0x02 /* Result is always SQL */
1512 #define JSON_ABPATH 0x03 /* Allow abbreviated JSON path specs */
1513 #define JSON_ISSET 0x04 /* json_set(), not json_insert() */
1516 ** json_extract(JSON, PATH, ...)
1520 ** Return the element described by PATH. Return NULL if that PATH element
1523 ** If JSON_JSON is set or if more that one PATH argument is supplied then
1524 ** always return a JSON representation of the result. If JSON_SQL is set,
1525 ** then always return an SQL representation of the result. If neither flag
1526 ** is present and argc==2, then return JSON for objects and arrays and SQL
1527 ** for all other values.
1529 ** When multiple PATH arguments are supplied, the result is a JSON array
1530 ** containing the result of each PATH.
1532 ** Abbreviated JSON path expressions are allows if JSON_ABPATH, for
1533 ** compatibility with PG.
1535 static void jsonExtractFunc(
1536 sqlite3_context
*ctx
,
1538 sqlite3_value
**argv
1540 JsonParse
*p
; /* The parse */
1543 int flags
= SQLITE_PTR_TO_INT(sqlite3_user_data(ctx
));
1546 if( argc
<2 ) return;
1547 p
= jsonParseCached(ctx
, argv
, ctx
);
1550 /* With a single PATH argument */
1551 zPath
= (const char*)sqlite3_value_text(argv
[1]);
1552 if( zPath
==0 ) return;
1553 if( flags
& JSON_ABPATH
){
1554 if( zPath
[0]!='$' ){
1555 /* The -> and ->> operators accept abbreviated PATH arguments. This
1556 ** is mostly for compatibility with PostgreSQL, but also for
1559 ** NUMBER ==> $[NUMBER] // PG compatible
1560 ** LABEL ==> $.LABEL // PG compatible
1561 ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience
1564 if( sqlite3Isdigit(zPath
[0]) ){
1565 jsonAppendRaw(&jx
, "$[", 2);
1566 jsonAppendRaw(&jx
, zPath
, (int)strlen(zPath
));
1567 jsonAppendRaw(&jx
, "]", 2);
1569 jsonAppendRaw(&jx
, "$.", 1 + (zPath
[0]!='['));
1570 jsonAppendRaw(&jx
, zPath
, (int)strlen(zPath
));
1571 jsonAppendChar(&jx
, 0);
1573 pNode
= jx
.bErr
? 0 : jsonLookup(p
, jx
.zBuf
, 0, ctx
);
1576 pNode
= jsonLookup(p
, zPath
, 0, ctx
);
1579 if( flags
& JSON_JSON
){
1580 jsonReturnJson(pNode
, ctx
, 0);
1582 jsonReturn(pNode
, ctx
, 0);
1583 sqlite3_result_subtype(ctx
, 0);
1587 pNode
= jsonLookup(p
, zPath
, 0, ctx
);
1588 if( p
->nErr
==0 && pNode
) jsonReturn(pNode
, ctx
, 0);
1591 /* Two or more PATH arguments results in a JSON array with each
1592 ** element of the array being the value selected by one of the PATHs */
1595 jsonAppendChar(&jx
, '[');
1596 for(i
=1; i
<argc
; i
++){
1597 zPath
= (const char*)sqlite3_value_text(argv
[i
]);
1598 pNode
= jsonLookup(p
, zPath
, 0, ctx
);
1599 if( p
->nErr
) break;
1600 jsonAppendSeparator(&jx
);
1602 jsonRenderNode(pNode
, &jx
, 0);
1604 jsonAppendRaw(&jx
, "null", 4);
1608 jsonAppendChar(&jx
, ']');
1610 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
1616 /* This is the RFC 7396 MergePatch algorithm.
1618 static JsonNode
*jsonMergePatch(
1619 JsonParse
*pParse
, /* The JSON parser that contains the TARGET */
1620 u32 iTarget
, /* Node of the TARGET in pParse */
1621 JsonNode
*pPatch
/* The PATCH */
1626 if( pPatch
->eType
!=JSON_OBJECT
){
1629 assert( iTarget
>=0 && iTarget
<pParse
->nNode
);
1630 pTarget
= &pParse
->aNode
[iTarget
];
1631 assert( (pPatch
->jnFlags
& JNODE_APPEND
)==0 );
1632 if( pTarget
->eType
!=JSON_OBJECT
){
1633 jsonRemoveAllNulls(pPatch
);
1637 for(i
=1; i
<pPatch
->n
; i
+= jsonNodeSize(&pPatch
[i
+1])+1){
1640 assert( pPatch
[i
].eType
==JSON_STRING
);
1641 assert( pPatch
[i
].jnFlags
& JNODE_LABEL
);
1642 assert( pPatch
[i
].eU
==1 );
1644 zKey
= pPatch
[i
].u
.zJContent
;
1645 assert( (pPatch
[i
].jnFlags
& JNODE_RAW
)==0 );
1646 for(j
=1; j
<pTarget
->n
; j
+= jsonNodeSize(&pTarget
[j
+1])+1 ){
1647 assert( pTarget
[j
].eType
==JSON_STRING
);
1648 assert( pTarget
[j
].jnFlags
& JNODE_LABEL
);
1649 assert( (pPatch
[i
].jnFlags
& JNODE_RAW
)==0 );
1650 if( pTarget
[j
].n
==nKey
&& strncmp(pTarget
[j
].u
.zJContent
,zKey
,nKey
)==0 ){
1651 if( pTarget
[j
+1].jnFlags
& (JNODE_REMOVE
|JNODE_PATCH
) ) break;
1652 if( pPatch
[i
+1].eType
==JSON_NULL
){
1653 pTarget
[j
+1].jnFlags
|= JNODE_REMOVE
;
1655 JsonNode
*pNew
= jsonMergePatch(pParse
, iTarget
+j
+1, &pPatch
[i
+1]);
1656 if( pNew
==0 ) return 0;
1657 pTarget
= &pParse
->aNode
[iTarget
];
1658 if( pNew
!=&pTarget
[j
+1] ){
1659 assert( pTarget
[j
+1].eU
==0
1660 || pTarget
[j
+1].eU
==1
1661 || pTarget
[j
+1].eU
==2 );
1662 testcase( pTarget
[j
+1].eU
==1 );
1663 testcase( pTarget
[j
+1].eU
==2 );
1664 VVA( pTarget
[j
+1].eU
= 5 );
1665 pTarget
[j
+1].u
.pPatch
= pNew
;
1666 pTarget
[j
+1].jnFlags
|= JNODE_PATCH
;
1672 if( j
>=pTarget
->n
&& pPatch
[i
+1].eType
!=JSON_NULL
){
1674 iStart
= jsonParseAddNode(pParse
, JSON_OBJECT
, 2, 0);
1675 jsonParseAddNode(pParse
, JSON_STRING
, nKey
, zKey
);
1676 iPatch
= jsonParseAddNode(pParse
, JSON_TRUE
, 0, 0);
1677 if( pParse
->oom
) return 0;
1678 jsonRemoveAllNulls(pPatch
);
1679 pTarget
= &pParse
->aNode
[iTarget
];
1680 assert( pParse
->aNode
[iRoot
].eU
==0 || pParse
->aNode
[iRoot
].eU
==2 );
1681 testcase( pParse
->aNode
[iRoot
].eU
==2 );
1682 pParse
->aNode
[iRoot
].jnFlags
|= JNODE_APPEND
;
1683 VVA( pParse
->aNode
[iRoot
].eU
= 2 );
1684 pParse
->aNode
[iRoot
].u
.iAppend
= iStart
- iRoot
;
1686 assert( pParse
->aNode
[iPatch
].eU
==0 );
1687 VVA( pParse
->aNode
[iPatch
].eU
= 5 );
1688 pParse
->aNode
[iPatch
].jnFlags
|= JNODE_PATCH
;
1689 pParse
->aNode
[iPatch
].u
.pPatch
= &pPatch
[i
+1];
1696 ** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
1697 ** object that is the result of running the RFC 7396 MergePatch() algorithm
1698 ** on the two arguments.
1700 static void jsonPatchFunc(
1701 sqlite3_context
*ctx
,
1703 sqlite3_value
**argv
1705 JsonParse x
; /* The JSON that is being patched */
1706 JsonParse y
; /* The patch */
1707 JsonNode
*pResult
; /* The result of the merge */
1709 UNUSED_PARAMETER(argc
);
1710 if( jsonParse(&x
, ctx
, (const char*)sqlite3_value_text(argv
[0])) ) return;
1711 if( jsonParse(&y
, ctx
, (const char*)sqlite3_value_text(argv
[1])) ){
1715 pResult
= jsonMergePatch(&x
, 0, y
.aNode
);
1716 assert( pResult
!=0 || x
.oom
);
1718 jsonReturnJson(pResult
, ctx
, 0);
1720 sqlite3_result_error_nomem(ctx
);
1728 ** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
1729 ** object that contains all name/value given in arguments. Or if any name
1730 ** is not a string or if any value is a BLOB, throw an error.
1732 static void jsonObjectFunc(
1733 sqlite3_context
*ctx
,
1735 sqlite3_value
**argv
1743 sqlite3_result_error(ctx
, "json_object() requires an even number "
1744 "of arguments", -1);
1748 jsonAppendChar(&jx
, '{');
1749 for(i
=0; i
<argc
; i
+=2){
1750 if( sqlite3_value_type(argv
[i
])!=SQLITE_TEXT
){
1751 sqlite3_result_error(ctx
, "json_object() labels must be TEXT", -1);
1755 jsonAppendSeparator(&jx
);
1756 z
= (const char*)sqlite3_value_text(argv
[i
]);
1757 n
= (u32
)sqlite3_value_bytes(argv
[i
]);
1758 jsonAppendString(&jx
, z
, n
);
1759 jsonAppendChar(&jx
, ':');
1760 jsonAppendValue(&jx
, argv
[i
+1]);
1762 jsonAppendChar(&jx
, '}');
1764 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
1769 ** json_remove(JSON, PATH, ...)
1771 ** Remove the named elements from JSON and return the result. malformed
1772 ** JSON or PATH arguments result in an error.
1774 static void jsonRemoveFunc(
1775 sqlite3_context
*ctx
,
1777 sqlite3_value
**argv
1779 JsonParse x
; /* The parse */
1784 if( argc
<1 ) return;
1785 if( jsonParse(&x
, ctx
, (const char*)sqlite3_value_text(argv
[0])) ) return;
1787 for(i
=1; i
<(u32
)argc
; i
++){
1788 zPath
= (const char*)sqlite3_value_text(argv
[i
]);
1789 if( zPath
==0 ) goto remove_done
;
1790 pNode
= jsonLookup(&x
, zPath
, 0, ctx
);
1791 if( x
.nErr
) goto remove_done
;
1792 if( pNode
) pNode
->jnFlags
|= JNODE_REMOVE
;
1794 if( (x
.aNode
[0].jnFlags
& JNODE_REMOVE
)==0 ){
1795 jsonReturnJson(x
.aNode
, ctx
, 0);
1802 ** json_replace(JSON, PATH, VALUE, ...)
1804 ** Replace the value at PATH with VALUE. If PATH does not already exist,
1805 ** this routine is a no-op. If JSON or PATH is malformed, throw an error.
1807 static void jsonReplaceFunc(
1808 sqlite3_context
*ctx
,
1810 sqlite3_value
**argv
1812 JsonParse x
; /* The parse */
1817 if( argc
<1 ) return;
1819 jsonWrongNumArgs(ctx
, "replace");
1822 if( jsonParse(&x
, ctx
, (const char*)sqlite3_value_text(argv
[0])) ) return;
1824 for(i
=1; i
<(u32
)argc
; i
+=2){
1825 zPath
= (const char*)sqlite3_value_text(argv
[i
]);
1826 pNode
= jsonLookup(&x
, zPath
, 0, ctx
);
1827 if( x
.nErr
) goto replace_err
;
1829 assert( pNode
->eU
==0 || pNode
->eU
==1 || pNode
->eU
==4 );
1830 testcase( pNode
->eU
!=0 && pNode
->eU
!=1 );
1831 pNode
->jnFlags
|= (u8
)JNODE_REPLACE
;
1832 VVA( pNode
->eU
= 4 );
1833 pNode
->u
.iReplace
= i
+ 1;
1836 if( x
.aNode
[0].jnFlags
& JNODE_REPLACE
){
1837 assert( x
.aNode
[0].eU
==4 );
1838 sqlite3_result_value(ctx
, argv
[x
.aNode
[0].u
.iReplace
]);
1840 jsonReturnJson(x
.aNode
, ctx
, argv
);
1848 ** json_set(JSON, PATH, VALUE, ...)
1850 ** Set the value at PATH to VALUE. Create the PATH if it does not already
1851 ** exist. Overwrite existing values that do exist.
1852 ** If JSON or PATH is malformed, throw an error.
1854 ** json_insert(JSON, PATH, VALUE, ...)
1856 ** Create PATH and initialize it to VALUE. If PATH already exists, this
1857 ** routine is a no-op. If JSON or PATH is malformed, throw an error.
1859 static void jsonSetFunc(
1860 sqlite3_context
*ctx
,
1862 sqlite3_value
**argv
1864 JsonParse x
; /* The parse */
1869 int bIsSet
= sqlite3_user_data(ctx
)!=0;
1871 if( argc
<1 ) return;
1873 jsonWrongNumArgs(ctx
, bIsSet
? "set" : "insert");
1876 if( jsonParse(&x
, ctx
, (const char*)sqlite3_value_text(argv
[0])) ) return;
1878 for(i
=1; i
<(u32
)argc
; i
+=2){
1879 zPath
= (const char*)sqlite3_value_text(argv
[i
]);
1881 pNode
= jsonLookup(&x
, zPath
, &bApnd
, ctx
);
1883 sqlite3_result_error_nomem(ctx
);
1887 }else if( pNode
&& (bApnd
|| bIsSet
) ){
1888 testcase( pNode
->eU
!=0 && pNode
->eU
!=1 );
1889 assert( pNode
->eU
!=3 && pNode
->eU
!=5 );
1890 VVA( pNode
->eU
= 4 );
1891 pNode
->jnFlags
|= (u8
)JNODE_REPLACE
;
1892 pNode
->u
.iReplace
= i
+ 1;
1895 if( x
.aNode
[0].jnFlags
& JNODE_REPLACE
){
1896 assert( x
.aNode
[0].eU
==4 );
1897 sqlite3_result_value(ctx
, argv
[x
.aNode
[0].u
.iReplace
]);
1899 jsonReturnJson(x
.aNode
, ctx
, argv
);
1907 ** json_type(JSON, PATH)
1909 ** Return the top-level "type" of a JSON string. json_type() raises an
1910 ** error if either the JSON or PATH inputs are not well-formed.
1912 static void jsonTypeFunc(
1913 sqlite3_context
*ctx
,
1915 sqlite3_value
**argv
1917 JsonParse
*p
; /* The parse */
1921 p
= jsonParseCached(ctx
, argv
, ctx
);
1924 zPath
= (const char*)sqlite3_value_text(argv
[1]);
1925 pNode
= jsonLookup(p
, zPath
, 0, ctx
);
1930 sqlite3_result_text(ctx
, jsonType
[pNode
->eType
], -1, SQLITE_STATIC
);
1937 ** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
1938 ** Return 0 otherwise.
1940 static void jsonValidFunc(
1941 sqlite3_context
*ctx
,
1943 sqlite3_value
**argv
1945 JsonParse
*p
; /* The parse */
1946 UNUSED_PARAMETER(argc
);
1947 p
= jsonParseCached(ctx
, argv
, 0);
1948 sqlite3_result_int(ctx
, p
!=0);
1952 /****************************************************************************
1953 ** Aggregate SQL function implementations
1954 ****************************************************************************/
1956 ** json_group_array(VALUE)
1958 ** Return a JSON array composed of all values in the aggregate.
1960 static void jsonArrayStep(
1961 sqlite3_context
*ctx
,
1963 sqlite3_value
**argv
1966 UNUSED_PARAMETER(argc
);
1967 pStr
= (JsonString
*)sqlite3_aggregate_context(ctx
, sizeof(*pStr
));
1969 if( pStr
->zBuf
==0 ){
1970 jsonInit(pStr
, ctx
);
1971 jsonAppendChar(pStr
, '[');
1972 }else if( pStr
->nUsed
>1 ){
1973 jsonAppendChar(pStr
, ',');
1976 jsonAppendValue(pStr
, argv
[0]);
1979 static void jsonArrayCompute(sqlite3_context
*ctx
, int isFinal
){
1981 pStr
= (JsonString
*)sqlite3_aggregate_context(ctx
, 0);
1984 jsonAppendChar(pStr
, ']');
1986 if( pStr
->bErr
==1 ) sqlite3_result_error_nomem(ctx
);
1987 assert( pStr
->bStatic
);
1988 }else if( isFinal
){
1989 sqlite3_result_text(ctx
, pStr
->zBuf
, (int)pStr
->nUsed
,
1990 pStr
->bStatic
? SQLITE_TRANSIENT
: sqlite3_free
);
1993 sqlite3_result_text(ctx
, pStr
->zBuf
, (int)pStr
->nUsed
, SQLITE_TRANSIENT
);
1997 sqlite3_result_text(ctx
, "[]", 2, SQLITE_STATIC
);
1999 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
2001 static void jsonArrayValue(sqlite3_context
*ctx
){
2002 jsonArrayCompute(ctx
, 0);
2004 static void jsonArrayFinal(sqlite3_context
*ctx
){
2005 jsonArrayCompute(ctx
, 1);
2008 #ifndef SQLITE_OMIT_WINDOWFUNC
2010 ** This method works for both json_group_array() and json_group_object().
2011 ** It works by removing the first element of the group by searching forward
2012 ** to the first comma (",") that is not within a string and deleting all
2013 ** text through that comma.
2015 static void jsonGroupInverse(
2016 sqlite3_context
*ctx
,
2018 sqlite3_value
**argv
2026 UNUSED_PARAMETER(argc
);
2027 UNUSED_PARAMETER(argv
);
2028 pStr
= (JsonString
*)sqlite3_aggregate_context(ctx
, 0);
2030 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
2031 ** always have been called to initalize it */
2032 if( NEVER(!pStr
) ) return;
2035 for(i
=1; i
<pStr
->nUsed
&& ((c
= z
[i
])!=',' || inStr
|| nNest
); i
++){
2038 }else if( c
=='\\' ){
2041 if( c
=='{' || c
=='[' ) nNest
++;
2042 if( c
=='}' || c
==']' ) nNest
--;
2045 if( i
<pStr
->nUsed
){
2047 memmove(&z
[1], &z
[i
+1], (size_t)pStr
->nUsed
-1);
2054 # define jsonGroupInverse 0
2059 ** json_group_obj(NAME,VALUE)
2061 ** Return a JSON object composed of all names and values in the aggregate.
2063 static void jsonObjectStep(
2064 sqlite3_context
*ctx
,
2066 sqlite3_value
**argv
2071 UNUSED_PARAMETER(argc
);
2072 pStr
= (JsonString
*)sqlite3_aggregate_context(ctx
, sizeof(*pStr
));
2074 if( pStr
->zBuf
==0 ){
2075 jsonInit(pStr
, ctx
);
2076 jsonAppendChar(pStr
, '{');
2077 }else if( pStr
->nUsed
>1 ){
2078 jsonAppendChar(pStr
, ',');
2081 z
= (const char*)sqlite3_value_text(argv
[0]);
2082 n
= (u32
)sqlite3_value_bytes(argv
[0]);
2083 jsonAppendString(pStr
, z
, n
);
2084 jsonAppendChar(pStr
, ':');
2085 jsonAppendValue(pStr
, argv
[1]);
2088 static void jsonObjectCompute(sqlite3_context
*ctx
, int isFinal
){
2090 pStr
= (JsonString
*)sqlite3_aggregate_context(ctx
, 0);
2092 jsonAppendChar(pStr
, '}');
2094 if( pStr
->bErr
==1 ) sqlite3_result_error_nomem(ctx
);
2095 assert( pStr
->bStatic
);
2096 }else if( isFinal
){
2097 sqlite3_result_text(ctx
, pStr
->zBuf
, (int)pStr
->nUsed
,
2098 pStr
->bStatic
? SQLITE_TRANSIENT
: sqlite3_free
);
2101 sqlite3_result_text(ctx
, pStr
->zBuf
, (int)pStr
->nUsed
, SQLITE_TRANSIENT
);
2105 sqlite3_result_text(ctx
, "{}", 2, SQLITE_STATIC
);
2107 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
2109 static void jsonObjectValue(sqlite3_context
*ctx
){
2110 jsonObjectCompute(ctx
, 0);
2112 static void jsonObjectFinal(sqlite3_context
*ctx
){
2113 jsonObjectCompute(ctx
, 1);
2118 #ifndef SQLITE_OMIT_VIRTUALTABLE
2119 /****************************************************************************
2120 ** The json_each virtual table
2121 ****************************************************************************/
2122 typedef struct JsonEachCursor JsonEachCursor
;
2123 struct JsonEachCursor
{
2124 sqlite3_vtab_cursor base
; /* Base class - must be first */
2125 u32 iRowid
; /* The rowid */
2126 u32 iBegin
; /* The first node of the scan */
2127 u32 i
; /* Index in sParse.aNode[] of current row */
2128 u32 iEnd
; /* EOF when i equals or exceeds this value */
2129 u8 eType
; /* Type of top-level element */
2130 u8 bRecursive
; /* True for json_tree(). False for json_each() */
2131 char *zJson
; /* Input JSON */
2132 char *zRoot
; /* Path by which to filter zJson */
2133 JsonParse sParse
; /* Parse of the input JSON */
2136 /* Constructor for the json_each virtual table */
2137 static int jsonEachConnect(
2140 int argc
, const char *const*argv
,
2141 sqlite3_vtab
**ppVtab
,
2147 /* Column numbers */
2149 #define JEACH_VALUE 1
2150 #define JEACH_TYPE 2
2151 #define JEACH_ATOM 3
2153 #define JEACH_PARENT 5
2154 #define JEACH_FULLKEY 6
2155 #define JEACH_PATH 7
2156 /* The xBestIndex method assumes that the JSON and ROOT columns are
2157 ** the last two columns in the table. Should this ever changes, be
2158 ** sure to update the xBestIndex method. */
2159 #define JEACH_JSON 8
2160 #define JEACH_ROOT 9
2162 UNUSED_PARAMETER(pzErr
);
2163 UNUSED_PARAMETER(argv
);
2164 UNUSED_PARAMETER(argc
);
2165 UNUSED_PARAMETER(pAux
);
2166 rc
= sqlite3_declare_vtab(db
,
2167 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
2168 "json HIDDEN,root HIDDEN)");
2169 if( rc
==SQLITE_OK
){
2170 pNew
= *ppVtab
= sqlite3_malloc( sizeof(*pNew
) );
2171 if( pNew
==0 ) return SQLITE_NOMEM
;
2172 memset(pNew
, 0, sizeof(*pNew
));
2173 sqlite3_vtab_config(db
, SQLITE_VTAB_INNOCUOUS
);
2178 /* destructor for json_each virtual table */
2179 static int jsonEachDisconnect(sqlite3_vtab
*pVtab
){
2180 sqlite3_free(pVtab
);
2184 /* constructor for a JsonEachCursor object for json_each(). */
2185 static int jsonEachOpenEach(sqlite3_vtab
*p
, sqlite3_vtab_cursor
**ppCursor
){
2186 JsonEachCursor
*pCur
;
2188 UNUSED_PARAMETER(p
);
2189 pCur
= sqlite3_malloc( sizeof(*pCur
) );
2190 if( pCur
==0 ) return SQLITE_NOMEM
;
2191 memset(pCur
, 0, sizeof(*pCur
));
2192 *ppCursor
= &pCur
->base
;
2196 /* constructor for a JsonEachCursor object for json_tree(). */
2197 static int jsonEachOpenTree(sqlite3_vtab
*p
, sqlite3_vtab_cursor
**ppCursor
){
2198 int rc
= jsonEachOpenEach(p
, ppCursor
);
2199 if( rc
==SQLITE_OK
){
2200 JsonEachCursor
*pCur
= (JsonEachCursor
*)*ppCursor
;
2201 pCur
->bRecursive
= 1;
2206 /* Reset a JsonEachCursor back to its original state. Free any memory
2208 static void jsonEachCursorReset(JsonEachCursor
*p
){
2209 sqlite3_free(p
->zJson
);
2210 sqlite3_free(p
->zRoot
);
2211 jsonParseReset(&p
->sParse
);
2220 /* Destructor for a jsonEachCursor object */
2221 static int jsonEachClose(sqlite3_vtab_cursor
*cur
){
2222 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
2223 jsonEachCursorReset(p
);
2228 /* Return TRUE if the jsonEachCursor object has been advanced off the end
2229 ** of the JSON object */
2230 static int jsonEachEof(sqlite3_vtab_cursor
*cur
){
2231 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
2232 return p
->i
>= p
->iEnd
;
2235 /* Advance the cursor to the next element for json_tree() */
2236 static int jsonEachNext(sqlite3_vtab_cursor
*cur
){
2237 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
2238 if( p
->bRecursive
){
2239 if( p
->sParse
.aNode
[p
->i
].jnFlags
& JNODE_LABEL
) p
->i
++;
2243 u32 iUp
= p
->sParse
.aUp
[p
->i
];
2244 JsonNode
*pUp
= &p
->sParse
.aNode
[iUp
];
2245 p
->eType
= pUp
->eType
;
2246 if( pUp
->eType
==JSON_ARRAY
){
2247 assert( pUp
->eU
==0 || pUp
->eU
==3 );
2248 testcase( pUp
->eU
==3 );
2260 p
->i
+= jsonNodeSize(&p
->sParse
.aNode
[p
->i
]);
2265 p
->i
+= 1 + jsonNodeSize(&p
->sParse
.aNode
[p
->i
+1]);
2278 /* Append an object label to the JSON Path being constructed
2281 static void jsonAppendObjectPathElement(
2287 assert( pNode
->eType
==JSON_STRING
);
2288 assert( pNode
->jnFlags
& JNODE_LABEL
);
2289 assert( pNode
->eU
==1 );
2290 z
= pNode
->u
.zJContent
;
2293 assert( z
[0]=='"' );
2294 assert( z
[nn
-1]=='"' );
2295 if( nn
>2 && sqlite3Isalpha(z
[1]) ){
2296 for(jj
=2; jj
<nn
-1 && sqlite3Isalnum(z
[jj
]); jj
++){}
2302 jsonPrintf(nn
+2, pStr
, ".%.*s", nn
, z
);
2305 /* Append the name of the path for element i to pStr
2307 static void jsonEachComputePath(
2308 JsonEachCursor
*p
, /* The cursor */
2309 JsonString
*pStr
, /* Write the path here */
2310 u32 i
/* Path to this element */
2312 JsonNode
*pNode
, *pUp
;
2315 jsonAppendChar(pStr
, '$');
2318 iUp
= p
->sParse
.aUp
[i
];
2319 jsonEachComputePath(p
, pStr
, iUp
);
2320 pNode
= &p
->sParse
.aNode
[i
];
2321 pUp
= &p
->sParse
.aNode
[iUp
];
2322 if( pUp
->eType
==JSON_ARRAY
){
2323 assert( pUp
->eU
==3 || (pUp
->eU
==0 && pUp
->u
.iKey
==0) );
2324 testcase( pUp
->eU
==0 );
2325 jsonPrintf(30, pStr
, "[%d]", pUp
->u
.iKey
);
2327 assert( pUp
->eType
==JSON_OBJECT
);
2328 if( (pNode
->jnFlags
& JNODE_LABEL
)==0 ) pNode
--;
2329 jsonAppendObjectPathElement(pStr
, pNode
);
2333 /* Return the value of a column */
2334 static int jsonEachColumn(
2335 sqlite3_vtab_cursor
*cur
, /* The cursor */
2336 sqlite3_context
*ctx
, /* First argument to sqlite3_result_...() */
2337 int i
/* Which column to return */
2339 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
2340 JsonNode
*pThis
= &p
->sParse
.aNode
[p
->i
];
2343 if( p
->i
==0 ) break;
2344 if( p
->eType
==JSON_OBJECT
){
2345 jsonReturn(pThis
, ctx
, 0);
2346 }else if( p
->eType
==JSON_ARRAY
){
2348 if( p
->bRecursive
){
2349 if( p
->iRowid
==0 ) break;
2350 assert( p
->sParse
.aNode
[p
->sParse
.aUp
[p
->i
]].eU
==3 );
2351 iKey
= p
->sParse
.aNode
[p
->sParse
.aUp
[p
->i
]].u
.iKey
;
2355 sqlite3_result_int64(ctx
, (sqlite3_int64
)iKey
);
2360 if( pThis
->jnFlags
& JNODE_LABEL
) pThis
++;
2361 jsonReturn(pThis
, ctx
, 0);
2365 if( pThis
->jnFlags
& JNODE_LABEL
) pThis
++;
2366 sqlite3_result_text(ctx
, jsonType
[pThis
->eType
], -1, SQLITE_STATIC
);
2370 if( pThis
->jnFlags
& JNODE_LABEL
) pThis
++;
2371 if( pThis
->eType
>=JSON_ARRAY
) break;
2372 jsonReturn(pThis
, ctx
, 0);
2376 sqlite3_result_int64(ctx
,
2377 (sqlite3_int64
)p
->i
+ ((pThis
->jnFlags
& JNODE_LABEL
)!=0));
2380 case JEACH_PARENT
: {
2381 if( p
->i
>p
->iBegin
&& p
->bRecursive
){
2382 sqlite3_result_int64(ctx
, (sqlite3_int64
)p
->sParse
.aUp
[p
->i
]);
2386 case JEACH_FULLKEY
: {
2389 if( p
->bRecursive
){
2390 jsonEachComputePath(p
, &x
, p
->i
);
2393 jsonAppendRaw(&x
, p
->zRoot
, (int)strlen(p
->zRoot
));
2395 jsonAppendChar(&x
, '$');
2397 if( p
->eType
==JSON_ARRAY
){
2398 jsonPrintf(30, &x
, "[%d]", p
->iRowid
);
2399 }else if( p
->eType
==JSON_OBJECT
){
2400 jsonAppendObjectPathElement(&x
, pThis
);
2407 if( p
->bRecursive
){
2410 jsonEachComputePath(p
, &x
, p
->sParse
.aUp
[p
->i
]);
2414 /* For json_each() path and root are the same so fall through
2415 ** into the root case */
2416 /* no break */ deliberate_fall_through
2419 const char *zRoot
= p
->zRoot
;
2420 if( zRoot
==0 ) zRoot
= "$";
2421 sqlite3_result_text(ctx
, zRoot
, -1, SQLITE_STATIC
);
2425 assert( i
==JEACH_JSON
);
2426 sqlite3_result_text(ctx
, p
->sParse
.zJson
, -1, SQLITE_STATIC
);
2433 /* Return the current rowid value */
2434 static int jsonEachRowid(sqlite3_vtab_cursor
*cur
, sqlite_int64
*pRowid
){
2435 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
2436 *pRowid
= p
->iRowid
;
2440 /* The query strategy is to look for an equality constraint on the json
2441 ** column. Without such a constraint, the table cannot operate. idxNum is
2442 ** 1 if the constraint is found, 3 if the constraint and zRoot are found,
2445 static int jsonEachBestIndex(
2447 sqlite3_index_info
*pIdxInfo
2449 int i
; /* Loop counter or computed array index */
2450 int aIdx
[2]; /* Index of constraints for JSON and ROOT */
2451 int unusableMask
= 0; /* Mask of unusable JSON and ROOT constraints */
2452 int idxMask
= 0; /* Mask of usable == constraints JSON and ROOT */
2453 const struct sqlite3_index_constraint
*pConstraint
;
2455 /* This implementation assumes that JSON and ROOT are the last two
2456 ** columns in the table */
2457 assert( JEACH_ROOT
== JEACH_JSON
+1 );
2458 UNUSED_PARAMETER(tab
);
2459 aIdx
[0] = aIdx
[1] = -1;
2460 pConstraint
= pIdxInfo
->aConstraint
;
2461 for(i
=0; i
<pIdxInfo
->nConstraint
; i
++, pConstraint
++){
2464 if( pConstraint
->iColumn
< JEACH_JSON
) continue;
2465 iCol
= pConstraint
->iColumn
- JEACH_JSON
;
2466 assert( iCol
==0 || iCol
==1 );
2467 testcase( iCol
==0 );
2469 if( pConstraint
->usable
==0 ){
2470 unusableMask
|= iMask
;
2471 }else if( pConstraint
->op
==SQLITE_INDEX_CONSTRAINT_EQ
){
2476 if( (unusableMask
& ~idxMask
)!=0 ){
2477 /* If there are any unusable constraints on JSON or ROOT, then reject
2478 ** this entire plan */
2479 return SQLITE_CONSTRAINT
;
2482 /* No JSON input. Leave estimatedCost at the huge value that it was
2483 ** initialized to to discourage the query planner from selecting this
2485 pIdxInfo
->idxNum
= 0;
2487 pIdxInfo
->estimatedCost
= 1.0;
2489 pIdxInfo
->aConstraintUsage
[i
].argvIndex
= 1;
2490 pIdxInfo
->aConstraintUsage
[i
].omit
= 1;
2492 pIdxInfo
->idxNum
= 1; /* Only JSON supplied. Plan 1 */
2495 pIdxInfo
->aConstraintUsage
[i
].argvIndex
= 2;
2496 pIdxInfo
->aConstraintUsage
[i
].omit
= 1;
2497 pIdxInfo
->idxNum
= 3; /* Both JSON and ROOT are supplied. Plan 3 */
2503 /* Start a search on a new JSON string */
2504 static int jsonEachFilter(
2505 sqlite3_vtab_cursor
*cur
,
2506 int idxNum
, const char *idxStr
,
2507 int argc
, sqlite3_value
**argv
2509 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
2511 const char *zRoot
= 0;
2514 UNUSED_PARAMETER(idxStr
);
2515 UNUSED_PARAMETER(argc
);
2516 jsonEachCursorReset(p
);
2517 if( idxNum
==0 ) return SQLITE_OK
;
2518 z
= (const char*)sqlite3_value_text(argv
[0]);
2519 if( z
==0 ) return SQLITE_OK
;
2520 n
= sqlite3_value_bytes(argv
[0]);
2521 p
->zJson
= sqlite3_malloc64( n
+1 );
2522 if( p
->zJson
==0 ) return SQLITE_NOMEM
;
2523 memcpy(p
->zJson
, z
, (size_t)n
+1);
2524 if( jsonParse(&p
->sParse
, 0, p
->zJson
) ){
2525 int rc
= SQLITE_NOMEM
;
2526 if( p
->sParse
.oom
==0 ){
2527 sqlite3_free(cur
->pVtab
->zErrMsg
);
2528 cur
->pVtab
->zErrMsg
= sqlite3_mprintf("malformed JSON");
2529 if( cur
->pVtab
->zErrMsg
) rc
= SQLITE_ERROR
;
2531 jsonEachCursorReset(p
);
2533 }else if( p
->bRecursive
&& jsonParseFindParents(&p
->sParse
) ){
2534 jsonEachCursorReset(p
);
2535 return SQLITE_NOMEM
;
2537 JsonNode
*pNode
= 0;
2539 const char *zErr
= 0;
2540 zRoot
= (const char*)sqlite3_value_text(argv
[1]);
2541 if( zRoot
==0 ) return SQLITE_OK
;
2542 n
= sqlite3_value_bytes(argv
[1]);
2543 p
->zRoot
= sqlite3_malloc64( n
+1 );
2544 if( p
->zRoot
==0 ) return SQLITE_NOMEM
;
2545 memcpy(p
->zRoot
, zRoot
, (size_t)n
+1);
2546 if( zRoot
[0]!='$' ){
2549 pNode
= jsonLookupStep(&p
->sParse
, 0, p
->zRoot
+1, 0, &zErr
);
2552 sqlite3_free(cur
->pVtab
->zErrMsg
);
2553 cur
->pVtab
->zErrMsg
= jsonPathSyntaxError(zErr
);
2554 jsonEachCursorReset(p
);
2555 return cur
->pVtab
->zErrMsg
? SQLITE_ERROR
: SQLITE_NOMEM
;
2556 }else if( pNode
==0 ){
2560 pNode
= p
->sParse
.aNode
;
2562 p
->iBegin
= p
->i
= (int)(pNode
- p
->sParse
.aNode
);
2563 p
->eType
= pNode
->eType
;
2564 if( p
->eType
>=JSON_ARRAY
){
2565 assert( pNode
->eU
==0 );
2566 VVA( pNode
->eU
= 3 );
2568 p
->iEnd
= p
->i
+ pNode
->n
+ 1;
2569 if( p
->bRecursive
){
2570 p
->eType
= p
->sParse
.aNode
[p
->sParse
.aUp
[p
->i
]].eType
;
2571 if( p
->i
>0 && (p
->sParse
.aNode
[p
->i
-1].jnFlags
& JNODE_LABEL
)!=0 ){
2584 /* The methods of the json_each virtual table */
2585 static sqlite3_module jsonEachModule
= {
2588 jsonEachConnect
, /* xConnect */
2589 jsonEachBestIndex
, /* xBestIndex */
2590 jsonEachDisconnect
, /* xDisconnect */
2592 jsonEachOpenEach
, /* xOpen - open a cursor */
2593 jsonEachClose
, /* xClose - close a cursor */
2594 jsonEachFilter
, /* xFilter - configure scan constraints */
2595 jsonEachNext
, /* xNext - advance a cursor */
2596 jsonEachEof
, /* xEof - check for end of scan */
2597 jsonEachColumn
, /* xColumn - read data */
2598 jsonEachRowid
, /* xRowid - read data */
2604 0, /* xFindMethod */
2608 0, /* xRollbackTo */
2612 /* The methods of the json_tree virtual table. */
2613 static sqlite3_module jsonTreeModule
= {
2616 jsonEachConnect
, /* xConnect */
2617 jsonEachBestIndex
, /* xBestIndex */
2618 jsonEachDisconnect
, /* xDisconnect */
2620 jsonEachOpenTree
, /* xOpen - open a cursor */
2621 jsonEachClose
, /* xClose - close a cursor */
2622 jsonEachFilter
, /* xFilter - configure scan constraints */
2623 jsonEachNext
, /* xNext - advance a cursor */
2624 jsonEachEof
, /* xEof - check for end of scan */
2625 jsonEachColumn
, /* xColumn - read data */
2626 jsonEachRowid
, /* xRowid - read data */
2632 0, /* xFindMethod */
2636 0, /* xRollbackTo */
2639 #endif /* SQLITE_OMIT_VIRTUALTABLE */
2640 #endif /* !defined(SQLITE_OMIT_JSON) */
2643 ** Register JSON functions.
2645 void sqlite3RegisterJsonFunctions(void){
2646 #ifndef SQLITE_OMIT_JSON
2647 static FuncDef aJsonFunc
[] = {
2648 JFUNCTION(json
, 1, 0, jsonRemoveFunc
),
2649 JFUNCTION(json_array
, -1, 0, jsonArrayFunc
),
2650 JFUNCTION(json_array_length
, 1, 0, jsonArrayLengthFunc
),
2651 JFUNCTION(json_array_length
, 2, 0, jsonArrayLengthFunc
),
2652 JFUNCTION(json_extract
, -1, 0, jsonExtractFunc
),
2653 JFUNCTION(->, 2, JSON_JSON
, jsonExtractFunc
),
2654 JFUNCTION(->>, 2, JSON_SQL
, jsonExtractFunc
),
2655 JFUNCTION(json_insert
, -1, 0, jsonSetFunc
),
2656 JFUNCTION(json_object
, -1, 0, jsonObjectFunc
),
2657 JFUNCTION(json_patch
, 2, 0, jsonPatchFunc
),
2658 JFUNCTION(json_quote
, 1, 0, jsonQuoteFunc
),
2659 JFUNCTION(json_remove
, -1, 0, jsonRemoveFunc
),
2660 JFUNCTION(json_replace
, -1, 0, jsonReplaceFunc
),
2661 JFUNCTION(json_set
, -1, JSON_ISSET
, jsonSetFunc
),
2662 JFUNCTION(json_type
, 1, 0, jsonTypeFunc
),
2663 JFUNCTION(json_type
, 2, 0, jsonTypeFunc
),
2664 JFUNCTION(json_valid
, 1, 0, jsonValidFunc
),
2666 JFUNCTION(json_parse
, 1, 0, jsonParseFunc
),
2667 JFUNCTION(json_test1
, 1, 0, jsonTest1Func
),
2669 WAGGREGATE(json_group_array
, 1, 0, 0,
2670 jsonArrayStep
, jsonArrayFinal
, jsonArrayValue
, jsonGroupInverse
,
2671 SQLITE_SUBTYPE
|SQLITE_UTF8
|SQLITE_DETERMINISTIC
|SQLITE_INNOCUOUS
),
2672 WAGGREGATE(json_group_object
, 2, 0, 0,
2673 jsonObjectStep
, jsonObjectFinal
, jsonObjectValue
, jsonGroupInverse
,
2674 SQLITE_SUBTYPE
|SQLITE_UTF8
|SQLITE_DETERMINISTIC
|SQLITE_INNOCUOUS
)
2676 sqlite3InsertBuiltinFuncs(aJsonFunc
, ArraySize(aJsonFunc
));
2680 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
2682 ** Register the JSON table-valued functions
2684 int sqlite3JsonTableFunctions(sqlite3
*db
){
2686 static const struct {
2688 sqlite3_module
*pModule
;
2690 { "json_each", &jsonEachModule
},
2691 { "json_tree", &jsonTreeModule
},
2694 for(i
=0; i
<sizeof(aMod
)/sizeof(aMod
[0]) && rc
==SQLITE_OK
; i
++){
2695 rc
= sqlite3_create_module(db
, aMod
[i
].zName
, aMod
[i
].pModule
, 0);
2699 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) */