Minor JNI cleanups.
[sqlite.git] / src / json.c
blobf8d4aa2a77661a9bafda1325c04ae195276f8634
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,
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,
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51 #define fast_isspace(x) (jsonIsSpace[(unsigned char)x])
54 ** Characters that are special to JSON. Control charaters,
55 ** '"' and '\\'.
57 static const char jsonIsOk[256] = {
58 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
61 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
62 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
63 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
64 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
65 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
67 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
68 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
69 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
70 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
71 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
72 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
73 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
74 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
78 #if !defined(SQLITE_DEBUG) && !defined(SQLITE_COVERAGE_TEST)
79 # define VVA(X)
80 #else
81 # define VVA(X) X
82 #endif
84 /* Objects */
85 typedef struct JsonString JsonString;
86 typedef struct JsonNode JsonNode;
87 typedef struct JsonParse JsonParse;
88 typedef struct JsonCleanup JsonCleanup;
90 /* An instance of this object represents a JSON string
91 ** under construction. Really, this is a generic string accumulator
92 ** that can be and is used to create strings other than JSON.
94 struct JsonString {
95 sqlite3_context *pCtx; /* Function context - put error messages here */
96 char *zBuf; /* Append JSON content here */
97 u64 nAlloc; /* Bytes of storage available in zBuf[] */
98 u64 nUsed; /* Bytes of zBuf[] currently used */
99 u8 bStatic; /* True if zBuf is static space */
100 u8 bErr; /* True if an error has been encountered */
101 char zSpace[100]; /* Initial static space */
104 /* A deferred cleanup task. A list of JsonCleanup objects might be
105 ** run when the JsonParse object is destroyed.
107 struct JsonCleanup {
108 JsonCleanup *pJCNext; /* Next in a list */
109 void (*xOp)(void*); /* Routine to run */
110 void *pArg; /* Argument to xOp() */
113 /* JSON type values
115 #define JSON_SUBST 0 /* Special edit node. Uses u.iPrev */
116 #define JSON_NULL 1
117 #define JSON_TRUE 2
118 #define JSON_FALSE 3
119 #define JSON_INT 4
120 #define JSON_REAL 5
121 #define JSON_STRING 6
122 #define JSON_ARRAY 7
123 #define JSON_OBJECT 8
125 /* The "subtype" set for JSON values */
126 #define JSON_SUBTYPE 74 /* Ascii for "J" */
129 ** Names of the various JSON types:
131 static const char * const jsonType[] = {
132 "subst",
133 "null", "true", "false", "integer", "real", "text", "array", "object"
136 /* Bit values for the JsonNode.jnFlag field
138 #define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
139 #define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
140 #define JNODE_REMOVE 0x04 /* Do not output */
141 #define JNODE_REPLACE 0x08 /* Target of a JSON_SUBST node */
142 #define JNODE_APPEND 0x10 /* More ARRAY/OBJECT entries at u.iAppend */
143 #define JNODE_LABEL 0x20 /* Is a label of an object */
144 #define JNODE_JSON5 0x40 /* Node contains JSON5 enhancements */
147 /* A single node of parsed JSON. An array of these nodes describes
148 ** a parse of JSON + edits.
150 ** Use the json_parse() SQL function (available when compiled with
151 ** -DSQLITE_DEBUG) to see a dump of complete JsonParse objects, including
152 ** a complete listing and decoding of the array of JsonNodes.
154 struct JsonNode {
155 u8 eType; /* One of the JSON_ type values */
156 u8 jnFlags; /* JNODE flags */
157 u8 eU; /* Which union element to use */
158 u32 n; /* Bytes of content for INT, REAL or STRING
159 ** Number of sub-nodes for ARRAY and OBJECT
160 ** Node that SUBST applies to */
161 union {
162 const char *zJContent; /* 1: Content for INT, REAL, and STRING */
163 u32 iAppend; /* 2: More terms for ARRAY and OBJECT */
164 u32 iKey; /* 3: Key for ARRAY objects in json_tree() */
165 u32 iPrev; /* 4: Previous SUBST node, or 0 */
166 } u;
170 /* A parsed and possibly edited JSON string. Lifecycle:
172 ** 1. JSON comes in and is parsed into an array aNode[]. The original
173 ** JSON text is stored in zJson.
175 ** 2. Zero or more changes are made (via json_remove() or json_replace()
176 ** or similar) to the aNode[] array.
178 ** 3. A new, edited and mimified JSON string is generated from aNode
179 ** and stored in zAlt. The JsonParse object always owns zAlt.
181 ** Step 1 always happens. Step 2 and 3 may or may not happen, depending
182 ** on the operation.
184 ** aNode[].u.zJContent entries typically point into zJson. Hence zJson
185 ** must remain valid for the lifespan of the parse. For edits,
186 ** aNode[].u.zJContent might point to malloced space other than zJson.
187 ** Entries in pClup are responsible for freeing that extra malloced space.
189 ** When walking the parse tree in aNode[], edits are ignored if useMod is
190 ** false.
192 struct JsonParse {
193 u32 nNode; /* Number of slots of aNode[] used */
194 u32 nAlloc; /* Number of slots of aNode[] allocated */
195 JsonNode *aNode; /* Array of nodes containing the parse */
196 char *zJson; /* Original JSON string (before edits) */
197 char *zAlt; /* Revised and/or mimified JSON */
198 u32 *aUp; /* Index of parent of each node */
199 JsonCleanup *pClup;/* Cleanup operations prior to freeing this object */
200 u16 iDepth; /* Nesting depth */
201 u8 nErr; /* Number of errors seen */
202 u8 oom; /* Set to true if out of memory */
203 u8 bJsonIsRCStr; /* True if zJson is an RCStr */
204 u8 hasNonstd; /* True if input uses non-standard features like JSON5 */
205 u8 useMod; /* Actually use the edits contain inside aNode */
206 u8 hasMod; /* aNode contains edits from the original zJson */
207 u32 nJPRef; /* Number of references to this object */
208 int nJson; /* Length of the zJson string in bytes */
209 int nAlt; /* Length of alternative JSON string zAlt, in bytes */
210 u32 iErr; /* Error location in zJson[] */
211 u32 iSubst; /* Last JSON_SUBST entry in aNode[] */
212 u32 iHold; /* Age of this entry in the cache for LRU replacement */
216 ** Maximum nesting depth of JSON for this implementation.
218 ** This limit is needed to avoid a stack overflow in the recursive
219 ** descent parser. A depth of 1000 is far deeper than any sane JSON
220 ** should go. Historical note: This limit was 2000 prior to version 3.42.0
222 #define JSON_MAX_DEPTH 1000
224 /**************************************************************************
225 ** Utility routines for dealing with JsonString objects
226 **************************************************************************/
228 /* Set the JsonString object to an empty string
230 static void jsonZero(JsonString *p){
231 p->zBuf = p->zSpace;
232 p->nAlloc = sizeof(p->zSpace);
233 p->nUsed = 0;
234 p->bStatic = 1;
237 /* Initialize the JsonString object
239 static void jsonInit(JsonString *p, sqlite3_context *pCtx){
240 p->pCtx = pCtx;
241 p->bErr = 0;
242 jsonZero(p);
245 /* Free all allocated memory and reset the JsonString object back to its
246 ** initial state.
248 static void jsonReset(JsonString *p){
249 if( !p->bStatic ) sqlite3RCStrUnref(p->zBuf);
250 jsonZero(p);
253 /* Report an out-of-memory (OOM) condition
255 static void jsonOom(JsonString *p){
256 p->bErr = 1;
257 sqlite3_result_error_nomem(p->pCtx);
258 jsonReset(p);
261 /* Enlarge pJson->zBuf so that it can hold at least N more bytes.
262 ** Return zero on success. Return non-zero on an OOM error
264 static int jsonGrow(JsonString *p, u32 N){
265 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
266 char *zNew;
267 if( p->bStatic ){
268 if( p->bErr ) return 1;
269 zNew = sqlite3RCStrNew(nTotal);
270 if( zNew==0 ){
271 jsonOom(p);
272 return SQLITE_NOMEM;
274 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
275 p->zBuf = zNew;
276 p->bStatic = 0;
277 }else{
278 p->zBuf = sqlite3RCStrResize(p->zBuf, nTotal);
279 if( p->zBuf==0 ){
280 p->bErr = 1;
281 jsonZero(p);
282 return SQLITE_NOMEM;
285 p->nAlloc = nTotal;
286 return SQLITE_OK;
289 /* Append N bytes from zIn onto the end of the JsonString string.
291 static SQLITE_NOINLINE void jsonAppendExpand(
292 JsonString *p,
293 const char *zIn,
294 u32 N
296 assert( N>0 );
297 if( jsonGrow(p,N) ) return;
298 memcpy(p->zBuf+p->nUsed, zIn, N);
299 p->nUsed += N;
301 static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
302 if( N==0 ) return;
303 if( N+p->nUsed >= p->nAlloc ){
304 jsonAppendExpand(p,zIn,N);
305 }else{
306 memcpy(p->zBuf+p->nUsed, zIn, N);
307 p->nUsed += N;
310 static void jsonAppendRawNZ(JsonString *p, const char *zIn, u32 N){
311 assert( N>0 );
312 if( N+p->nUsed >= p->nAlloc ){
313 jsonAppendExpand(p,zIn,N);
314 }else{
315 memcpy(p->zBuf+p->nUsed, zIn, N);
316 p->nUsed += N;
321 /* Append formatted text (not to exceed N bytes) to the JsonString.
323 static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
324 va_list ap;
325 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
326 va_start(ap, zFormat);
327 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
328 va_end(ap);
329 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
332 /* Append a single character
334 static SQLITE_NOINLINE void jsonAppendCharExpand(JsonString *p, char c){
335 if( jsonGrow(p,1) ) return;
336 p->zBuf[p->nUsed++] = c;
338 static void jsonAppendChar(JsonString *p, char c){
339 if( p->nUsed>=p->nAlloc ){
340 jsonAppendCharExpand(p,c);
341 }else{
342 p->zBuf[p->nUsed++] = c;
346 /* Try to force the string to be a zero-terminated RCStr string.
348 ** Return true on success. Return false if an OOM prevents this
349 ** from happening.
351 static int jsonForceRCStr(JsonString *p){
352 jsonAppendChar(p, 0);
353 if( p->bErr ) return 0;
354 p->nUsed--;
355 if( p->bStatic==0 ) return 1;
356 p->nAlloc = 0;
357 p->nUsed++;
358 jsonGrow(p, p->nUsed);
359 p->nUsed--;
360 return p->bStatic==0;
364 /* Append a comma separator to the output buffer, if the previous
365 ** character is not '[' or '{'.
367 static void jsonAppendSeparator(JsonString *p){
368 char c;
369 if( p->nUsed==0 ) return;
370 c = p->zBuf[p->nUsed-1];
371 if( c=='[' || c=='{' ) return;
372 jsonAppendChar(p, ',');
375 /* Append the N-byte string in zIn to the end of the JsonString string
376 ** under construction. Enclose the string in "..." and escape
377 ** any double-quotes or backslash characters contained within the
378 ** string.
380 static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
381 u32 i;
382 if( zIn==0 || ((N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0) ) return;
383 p->zBuf[p->nUsed++] = '"';
384 for(i=0; i<N; i++){
385 unsigned char c = ((unsigned const char*)zIn)[i];
386 if( jsonIsOk[c] ){
387 p->zBuf[p->nUsed++] = c;
388 }else if( c=='"' || c=='\\' ){
389 json_simple_escape:
390 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
391 p->zBuf[p->nUsed++] = '\\';
392 p->zBuf[p->nUsed++] = c;
393 }else if( c=='\'' ){
394 p->zBuf[p->nUsed++] = c;
395 }else{
396 static const char aSpecial[] = {
397 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
398 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
400 assert( sizeof(aSpecial)==32 );
401 assert( aSpecial['\b']=='b' );
402 assert( aSpecial['\f']=='f' );
403 assert( aSpecial['\n']=='n' );
404 assert( aSpecial['\r']=='r' );
405 assert( aSpecial['\t']=='t' );
406 assert( c>=0 && c<sizeof(aSpecial) );
407 if( aSpecial[c] ){
408 c = aSpecial[c];
409 goto json_simple_escape;
411 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
412 p->zBuf[p->nUsed++] = '\\';
413 p->zBuf[p->nUsed++] = 'u';
414 p->zBuf[p->nUsed++] = '0';
415 p->zBuf[p->nUsed++] = '0';
416 p->zBuf[p->nUsed++] = "0123456789abcdef"[c>>4];
417 p->zBuf[p->nUsed++] = "0123456789abcdef"[c&0xf];
420 p->zBuf[p->nUsed++] = '"';
421 assert( p->nUsed<p->nAlloc );
425 ** The zIn[0..N] string is a JSON5 string literal. Append to p a translation
426 ** of the string literal that standard JSON and that omits all JSON5
427 ** features.
429 static void jsonAppendNormalizedString(JsonString *p, const char *zIn, u32 N){
430 u32 i;
431 jsonAppendChar(p, '"');
432 zIn++;
433 N -= 2;
434 while( N>0 ){
435 for(i=0; i<N && zIn[i]!='\\'; i++){}
436 if( i>0 ){
437 jsonAppendRawNZ(p, zIn, i);
438 zIn += i;
439 N -= i;
440 if( N==0 ) break;
442 assert( zIn[0]=='\\' );
443 switch( (u8)zIn[1] ){
444 case '\'':
445 jsonAppendChar(p, '\'');
446 break;
447 case 'v':
448 jsonAppendRawNZ(p, "\\u0009", 6);
449 break;
450 case 'x':
451 jsonAppendRawNZ(p, "\\u00", 4);
452 jsonAppendRawNZ(p, &zIn[2], 2);
453 zIn += 2;
454 N -= 2;
455 break;
456 case '0':
457 jsonAppendRawNZ(p, "\\u0000", 6);
458 break;
459 case '\r':
460 if( zIn[2]=='\n' ){
461 zIn++;
462 N--;
464 break;
465 case '\n':
466 break;
467 case 0xe2:
468 assert( N>=4 );
469 assert( 0x80==(u8)zIn[2] );
470 assert( 0xa8==(u8)zIn[3] || 0xa9==(u8)zIn[3] );
471 zIn += 2;
472 N -= 2;
473 break;
474 default:
475 jsonAppendRawNZ(p, zIn, 2);
476 break;
478 zIn += 2;
479 N -= 2;
481 jsonAppendChar(p, '"');
485 ** The zIn[0..N] string is a JSON5 integer literal. Append to p a translation
486 ** of the string literal that standard JSON and that omits all JSON5
487 ** features.
489 static void jsonAppendNormalizedInt(JsonString *p, const char *zIn, u32 N){
490 if( zIn[0]=='+' ){
491 zIn++;
492 N--;
493 }else if( zIn[0]=='-' ){
494 jsonAppendChar(p, '-');
495 zIn++;
496 N--;
498 if( zIn[0]=='0' && (zIn[1]=='x' || zIn[1]=='X') ){
499 sqlite3_int64 i = 0;
500 int rc = sqlite3DecOrHexToI64(zIn, &i);
501 if( rc<=1 ){
502 jsonPrintf(100,p,"%lld",i);
503 }else{
504 assert( rc==2 );
505 jsonAppendRawNZ(p, "9.0e999", 7);
507 return;
509 assert( N>0 );
510 jsonAppendRawNZ(p, zIn, N);
514 ** The zIn[0..N] string is a JSON5 real literal. Append to p a translation
515 ** of the string literal that standard JSON and that omits all JSON5
516 ** features.
518 static void jsonAppendNormalizedReal(JsonString *p, const char *zIn, u32 N){
519 u32 i;
520 if( zIn[0]=='+' ){
521 zIn++;
522 N--;
523 }else if( zIn[0]=='-' ){
524 jsonAppendChar(p, '-');
525 zIn++;
526 N--;
528 if( zIn[0]=='.' ){
529 jsonAppendChar(p, '0');
531 for(i=0; i<N; i++){
532 if( zIn[i]=='.' && (i+1==N || !sqlite3Isdigit(zIn[i+1])) ){
533 i++;
534 jsonAppendRaw(p, zIn, i);
535 zIn += i;
536 N -= i;
537 jsonAppendChar(p, '0');
538 break;
541 if( N>0 ){
542 jsonAppendRawNZ(p, zIn, N);
549 ** Append a function parameter value to the JSON string under
550 ** construction.
552 static void jsonAppendValue(
553 JsonString *p, /* Append to this JSON string */
554 sqlite3_value *pValue /* Value to append */
556 switch( sqlite3_value_type(pValue) ){
557 case SQLITE_NULL: {
558 jsonAppendRawNZ(p, "null", 4);
559 break;
561 case SQLITE_FLOAT: {
562 jsonPrintf(100, p, "%!0.15g", sqlite3_value_double(pValue));
563 break;
565 case SQLITE_INTEGER: {
566 const char *z = (const char*)sqlite3_value_text(pValue);
567 u32 n = (u32)sqlite3_value_bytes(pValue);
568 jsonAppendRaw(p, z, n);
569 break;
571 case SQLITE_TEXT: {
572 const char *z = (const char*)sqlite3_value_text(pValue);
573 u32 n = (u32)sqlite3_value_bytes(pValue);
574 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
575 jsonAppendRaw(p, z, n);
576 }else{
577 jsonAppendString(p, z, n);
579 break;
581 default: {
582 if( p->bErr==0 ){
583 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
584 p->bErr = 2;
585 jsonReset(p);
587 break;
593 /* Make the JSON in p the result of the SQL function.
595 ** The JSON string is reset.
597 static void jsonResult(JsonString *p){
598 if( p->bErr==0 ){
599 if( p->bStatic ){
600 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
601 SQLITE_TRANSIENT, SQLITE_UTF8);
602 }else if( jsonForceRCStr(p) ){
603 sqlite3RCStrRef(p->zBuf);
604 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
605 (void(*)(void*))sqlite3RCStrUnref,
606 SQLITE_UTF8);
609 if( p->bErr==1 ){
610 sqlite3_result_error_nomem(p->pCtx);
612 jsonReset(p);
615 /**************************************************************************
616 ** Utility routines for dealing with JsonNode and JsonParse objects
617 **************************************************************************/
620 ** Return the number of consecutive JsonNode slots need to represent
621 ** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
622 ** OBJECT types, the number might be larger.
624 ** Appended elements are not counted. The value returned is the number
625 ** by which the JsonNode counter should increment in order to go to the
626 ** next peer value.
628 static u32 jsonNodeSize(JsonNode *pNode){
629 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
633 ** Reclaim all memory allocated by a JsonParse object. But do not
634 ** delete the JsonParse object itself.
636 static void jsonParseReset(JsonParse *pParse){
637 while( pParse->pClup ){
638 JsonCleanup *pTask = pParse->pClup;
639 pParse->pClup = pTask->pJCNext;
640 pTask->xOp(pTask->pArg);
641 sqlite3_free(pTask);
643 assert( pParse->nJPRef<=1 );
644 if( pParse->aNode ){
645 sqlite3_free(pParse->aNode);
646 pParse->aNode = 0;
648 pParse->nNode = 0;
649 pParse->nAlloc = 0;
650 if( pParse->aUp ){
651 sqlite3_free(pParse->aUp);
652 pParse->aUp = 0;
654 if( pParse->bJsonIsRCStr ){
655 sqlite3RCStrUnref(pParse->zJson);
656 pParse->zJson = 0;
657 pParse->bJsonIsRCStr = 0;
659 if( pParse->zAlt ){
660 sqlite3RCStrUnref(pParse->zAlt);
661 pParse->zAlt = 0;
666 ** Free a JsonParse object that was obtained from sqlite3_malloc().
668 ** Note that destroying JsonParse might call sqlite3RCStrUnref() to
669 ** destroy the zJson value. The RCStr object might recursively invoke
670 ** JsonParse to destroy this pParse object again. Take care to ensure
671 ** that this recursive destructor sequence terminates harmlessly.
673 static void jsonParseFree(JsonParse *pParse){
674 if( pParse->nJPRef>1 ){
675 pParse->nJPRef--;
676 }else{
677 jsonParseReset(pParse);
678 sqlite3_free(pParse);
683 ** Add a cleanup task to the JsonParse object.
685 ** If an OOM occurs, the cleanup operation happens immediately
686 ** and this function returns SQLITE_NOMEM.
688 static int jsonParseAddCleanup(
689 JsonParse *pParse, /* Add the cleanup task to this parser */
690 void(*xOp)(void*), /* The cleanup task */
691 void *pArg /* Argument to the cleanup */
693 JsonCleanup *pTask = sqlite3_malloc64( sizeof(*pTask) );
694 if( pTask==0 ){
695 pParse->oom = 1;
696 xOp(pArg);
697 return SQLITE_ERROR;
699 pTask->pJCNext = pParse->pClup;
700 pParse->pClup = pTask;
701 pTask->xOp = xOp;
702 pTask->pArg = pArg;
703 return SQLITE_OK;
707 ** Convert the JsonNode pNode into a pure JSON string and
708 ** append to pOut. Subsubstructure is also included. Return
709 ** the number of JsonNode objects that are encoded.
711 static void jsonRenderNode(
712 JsonParse *pParse, /* the complete parse of the JSON */
713 JsonNode *pNode, /* The node to render */
714 JsonString *pOut /* Write JSON here */
716 assert( pNode!=0 );
717 while( (pNode->jnFlags & JNODE_REPLACE)!=0 && pParse->useMod ){
718 u32 idx = (u32)(pNode - pParse->aNode);
719 u32 i = pParse->iSubst;
720 while( 1 /*exit-by-break*/ ){
721 assert( i<pParse->nNode );
722 assert( pParse->aNode[i].eType==JSON_SUBST );
723 assert( pParse->aNode[i].eU==4 );
724 assert( pParse->aNode[i].u.iPrev<i );
725 if( pParse->aNode[i].n==idx ){
726 pNode = &pParse->aNode[i+1];
727 break;
729 i = pParse->aNode[i].u.iPrev;
732 switch( pNode->eType ){
733 default: {
734 assert( pNode->eType==JSON_NULL );
735 jsonAppendRawNZ(pOut, "null", 4);
736 break;
738 case JSON_TRUE: {
739 jsonAppendRawNZ(pOut, "true", 4);
740 break;
742 case JSON_FALSE: {
743 jsonAppendRawNZ(pOut, "false", 5);
744 break;
746 case JSON_STRING: {
747 assert( pNode->eU==1 );
748 if( pNode->jnFlags & JNODE_RAW ){
749 if( pNode->jnFlags & JNODE_LABEL ){
750 jsonAppendChar(pOut, '"');
751 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
752 jsonAppendChar(pOut, '"');
753 }else{
754 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
756 }else if( pNode->jnFlags & JNODE_JSON5 ){
757 jsonAppendNormalizedString(pOut, pNode->u.zJContent, pNode->n);
758 }else{
759 assert( pNode->n>0 );
760 jsonAppendRawNZ(pOut, pNode->u.zJContent, pNode->n);
762 break;
764 case JSON_REAL: {
765 assert( pNode->eU==1 );
766 if( pNode->jnFlags & JNODE_JSON5 ){
767 jsonAppendNormalizedReal(pOut, pNode->u.zJContent, pNode->n);
768 }else{
769 assert( pNode->n>0 );
770 jsonAppendRawNZ(pOut, pNode->u.zJContent, pNode->n);
772 break;
774 case JSON_INT: {
775 assert( pNode->eU==1 );
776 if( pNode->jnFlags & JNODE_JSON5 ){
777 jsonAppendNormalizedInt(pOut, pNode->u.zJContent, pNode->n);
778 }else{
779 assert( pNode->n>0 );
780 jsonAppendRawNZ(pOut, pNode->u.zJContent, pNode->n);
782 break;
784 case JSON_ARRAY: {
785 u32 j = 1;
786 jsonAppendChar(pOut, '[');
787 for(;;){
788 while( j<=pNode->n ){
789 if( (pNode[j].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ){
790 jsonAppendSeparator(pOut);
791 jsonRenderNode(pParse, &pNode[j], pOut);
793 j += jsonNodeSize(&pNode[j]);
795 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
796 if( pParse->useMod==0 ) break;
797 assert( pNode->eU==2 );
798 pNode = &pParse->aNode[pNode->u.iAppend];
799 j = 1;
801 jsonAppendChar(pOut, ']');
802 break;
804 case JSON_OBJECT: {
805 u32 j = 1;
806 jsonAppendChar(pOut, '{');
807 for(;;){
808 while( j<=pNode->n ){
809 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ){
810 jsonAppendSeparator(pOut);
811 jsonRenderNode(pParse, &pNode[j], pOut);
812 jsonAppendChar(pOut, ':');
813 jsonRenderNode(pParse, &pNode[j+1], pOut);
815 j += 1 + jsonNodeSize(&pNode[j+1]);
817 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
818 if( pParse->useMod==0 ) break;
819 assert( pNode->eU==2 );
820 pNode = &pParse->aNode[pNode->u.iAppend];
821 j = 1;
823 jsonAppendChar(pOut, '}');
824 break;
830 ** Return a JsonNode and all its descendants as a JSON string.
832 static void jsonReturnJson(
833 JsonParse *pParse, /* The complete JSON */
834 JsonNode *pNode, /* Node to return */
835 sqlite3_context *pCtx, /* Return value for this function */
836 int bGenerateAlt /* Also store the rendered text in zAlt */
838 JsonString s;
839 if( pParse->oom ){
840 sqlite3_result_error_nomem(pCtx);
841 return;
843 if( pParse->nErr==0 ){
844 jsonInit(&s, pCtx);
845 jsonRenderNode(pParse, pNode, &s);
846 if( bGenerateAlt && pParse->zAlt==0 && jsonForceRCStr(&s) ){
847 pParse->zAlt = sqlite3RCStrRef(s.zBuf);
848 pParse->nAlt = s.nUsed;
850 jsonResult(&s);
851 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
856 ** Translate a single byte of Hex into an integer.
857 ** This routine only works if h really is a valid hexadecimal
858 ** character: 0..9a..fA..F
860 static u8 jsonHexToInt(int h){
861 assert( (h>='0' && h<='9') || (h>='a' && h<='f') || (h>='A' && h<='F') );
862 #ifdef SQLITE_EBCDIC
863 h += 9*(1&~(h>>4));
864 #else
865 h += 9*(1&(h>>6));
866 #endif
867 return (u8)(h & 0xf);
871 ** Convert a 4-byte hex string into an integer
873 static u32 jsonHexToInt4(const char *z){
874 u32 v;
875 assert( sqlite3Isxdigit(z[0]) );
876 assert( sqlite3Isxdigit(z[1]) );
877 assert( sqlite3Isxdigit(z[2]) );
878 assert( sqlite3Isxdigit(z[3]) );
879 v = (jsonHexToInt(z[0])<<12)
880 + (jsonHexToInt(z[1])<<8)
881 + (jsonHexToInt(z[2])<<4)
882 + jsonHexToInt(z[3]);
883 return v;
887 ** Make the JsonNode the return value of the function.
889 static void jsonReturn(
890 JsonParse *pParse, /* Complete JSON parse tree */
891 JsonNode *pNode, /* Node to return */
892 sqlite3_context *pCtx /* Return value for this function */
894 switch( pNode->eType ){
895 default: {
896 assert( pNode->eType==JSON_NULL );
897 sqlite3_result_null(pCtx);
898 break;
900 case JSON_TRUE: {
901 sqlite3_result_int(pCtx, 1);
902 break;
904 case JSON_FALSE: {
905 sqlite3_result_int(pCtx, 0);
906 break;
908 case JSON_INT: {
909 sqlite3_int64 i = 0;
910 int rc;
911 int bNeg = 0;
912 const char *z;
914 assert( pNode->eU==1 );
915 z = pNode->u.zJContent;
916 if( z[0]=='-' ){ z++; bNeg = 1; }
917 else if( z[0]=='+' ){ z++; }
918 rc = sqlite3DecOrHexToI64(z, &i);
919 if( rc<=1 ){
920 sqlite3_result_int64(pCtx, bNeg ? -i : i);
921 }else if( rc==3 && bNeg ){
922 sqlite3_result_int64(pCtx, SMALLEST_INT64);
923 }else{
924 goto to_double;
926 break;
928 case JSON_REAL: {
929 double r;
930 const char *z;
931 assert( pNode->eU==1 );
932 to_double:
933 z = pNode->u.zJContent;
934 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
935 sqlite3_result_double(pCtx, r);
936 break;
938 case JSON_STRING: {
939 if( pNode->jnFlags & JNODE_RAW ){
940 assert( pNode->eU==1 );
941 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
942 SQLITE_TRANSIENT);
943 }else if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
944 /* JSON formatted without any backslash-escapes */
945 assert( pNode->eU==1 );
946 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
947 SQLITE_TRANSIENT);
948 }else{
949 /* Translate JSON formatted string into raw text */
950 u32 i;
951 u32 n = pNode->n;
952 const char *z;
953 char *zOut;
954 u32 j;
955 u32 nOut = n;
956 assert( pNode->eU==1 );
957 z = pNode->u.zJContent;
958 zOut = sqlite3_malloc( nOut+1 );
959 if( zOut==0 ){
960 sqlite3_result_error_nomem(pCtx);
961 break;
963 for(i=1, j=0; i<n-1; i++){
964 char c = z[i];
965 if( c=='\\' ){
966 c = z[++i];
967 if( c=='u' ){
968 u32 v = jsonHexToInt4(z+i+1);
969 i += 4;
970 if( v==0 ) break;
971 if( v<=0x7f ){
972 zOut[j++] = (char)v;
973 }else if( v<=0x7ff ){
974 zOut[j++] = (char)(0xc0 | (v>>6));
975 zOut[j++] = 0x80 | (v&0x3f);
976 }else{
977 u32 vlo;
978 if( (v&0xfc00)==0xd800
979 && i<n-6
980 && z[i+1]=='\\'
981 && z[i+2]=='u'
982 && ((vlo = jsonHexToInt4(z+i+3))&0xfc00)==0xdc00
984 /* We have a surrogate pair */
985 v = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
986 i += 6;
987 zOut[j++] = 0xf0 | (v>>18);
988 zOut[j++] = 0x80 | ((v>>12)&0x3f);
989 zOut[j++] = 0x80 | ((v>>6)&0x3f);
990 zOut[j++] = 0x80 | (v&0x3f);
991 }else{
992 zOut[j++] = 0xe0 | (v>>12);
993 zOut[j++] = 0x80 | ((v>>6)&0x3f);
994 zOut[j++] = 0x80 | (v&0x3f);
997 continue;
998 }else if( c=='b' ){
999 c = '\b';
1000 }else if( c=='f' ){
1001 c = '\f';
1002 }else if( c=='n' ){
1003 c = '\n';
1004 }else if( c=='r' ){
1005 c = '\r';
1006 }else if( c=='t' ){
1007 c = '\t';
1008 }else if( c=='v' ){
1009 c = '\v';
1010 }else if( c=='\'' || c=='"' || c=='/' || c=='\\' ){
1011 /* pass through unchanged */
1012 }else if( c=='0' ){
1013 c = 0;
1014 }else if( c=='x' ){
1015 c = (jsonHexToInt(z[i+1])<<4) | jsonHexToInt(z[i+2]);
1016 i += 2;
1017 }else if( c=='\r' && z[i+1]=='\n' ){
1018 i++;
1019 continue;
1020 }else if( 0xe2==(u8)c ){
1021 assert( 0x80==(u8)z[i+1] );
1022 assert( 0xa8==(u8)z[i+2] || 0xa9==(u8)z[i+2] );
1023 i += 2;
1024 continue;
1025 }else{
1026 continue;
1028 } /* end if( c=='\\' ) */
1029 zOut[j++] = c;
1030 } /* end for() */
1031 zOut[j] = 0;
1032 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
1034 break;
1036 case JSON_ARRAY:
1037 case JSON_OBJECT: {
1038 jsonReturnJson(pParse, pNode, pCtx, 0);
1039 break;
1044 /* Forward reference */
1045 static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
1048 ** A macro to hint to the compiler that a function should not be
1049 ** inlined.
1051 #if defined(__GNUC__)
1052 # define JSON_NOINLINE __attribute__((noinline))
1053 #elif defined(_MSC_VER) && _MSC_VER>=1310
1054 # define JSON_NOINLINE __declspec(noinline)
1055 #else
1056 # define JSON_NOINLINE
1057 #endif
1061 ** Add a single node to pParse->aNode after first expanding the
1062 ** size of the aNode array. Return the index of the new node.
1064 ** If an OOM error occurs, set pParse->oom and return -1.
1066 static JSON_NOINLINE int jsonParseAddNodeExpand(
1067 JsonParse *pParse, /* Append the node to this object */
1068 u32 eType, /* Node type */
1069 u32 n, /* Content size or sub-node count */
1070 const char *zContent /* Content */
1072 u32 nNew;
1073 JsonNode *pNew;
1074 assert( pParse->nNode>=pParse->nAlloc );
1075 if( pParse->oom ) return -1;
1076 nNew = pParse->nAlloc*2 + 10;
1077 pNew = sqlite3_realloc64(pParse->aNode, sizeof(JsonNode)*nNew);
1078 if( pNew==0 ){
1079 pParse->oom = 1;
1080 return -1;
1082 pParse->nAlloc = sqlite3_msize(pNew)/sizeof(JsonNode);
1083 pParse->aNode = pNew;
1084 assert( pParse->nNode<pParse->nAlloc );
1085 return jsonParseAddNode(pParse, eType, n, zContent);
1089 ** Create a new JsonNode instance based on the arguments and append that
1090 ** instance to the JsonParse. Return the index in pParse->aNode[] of the
1091 ** new node, or -1 if a memory allocation fails.
1093 static int jsonParseAddNode(
1094 JsonParse *pParse, /* Append the node to this object */
1095 u32 eType, /* Node type */
1096 u32 n, /* Content size or sub-node count */
1097 const char *zContent /* Content */
1099 JsonNode *p;
1100 assert( pParse->aNode!=0 || pParse->nNode>=pParse->nAlloc );
1101 if( pParse->nNode>=pParse->nAlloc ){
1102 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
1104 assert( pParse->aNode!=0 );
1105 p = &pParse->aNode[pParse->nNode];
1106 assert( p!=0 );
1107 p->eType = (u8)(eType & 0xff);
1108 p->jnFlags = (u8)(eType >> 8);
1109 VVA( p->eU = zContent ? 1 : 0 );
1110 p->n = n;
1111 p->u.zJContent = zContent;
1112 return pParse->nNode++;
1116 ** Add an array of new nodes to the current pParse->aNode array.
1117 ** Return the index of the first node added.
1119 ** If an OOM error occurs, set pParse->oom.
1121 static void jsonParseAddNodeArray(
1122 JsonParse *pParse, /* Append the node to this object */
1123 JsonNode *aNode, /* Array of nodes to add */
1124 u32 nNode /* Number of elements in aNew */
1126 assert( aNode!=0 );
1127 assert( nNode>=1 );
1128 if( pParse->nNode + nNode > pParse->nAlloc ){
1129 u32 nNew = pParse->nNode + nNode;
1130 JsonNode *aNew = sqlite3_realloc64(pParse->aNode, nNew*sizeof(JsonNode));
1131 if( aNew==0 ){
1132 pParse->oom = 1;
1133 return;
1135 pParse->nAlloc = sqlite3_msize(aNew)/sizeof(JsonNode);
1136 pParse->aNode = aNew;
1138 memcpy(&pParse->aNode[pParse->nNode], aNode, nNode*sizeof(JsonNode));
1139 pParse->nNode += nNode;
1143 ** Add a new JSON_SUBST node. The node immediately following
1144 ** this new node will be the substitute content for iNode.
1146 static int jsonParseAddSubstNode(
1147 JsonParse *pParse, /* Add the JSON_SUBST here */
1148 u32 iNode /* References this node */
1150 int idx = jsonParseAddNode(pParse, JSON_SUBST, iNode, 0);
1151 if( pParse->oom ) return -1;
1152 pParse->aNode[iNode].jnFlags |= JNODE_REPLACE;
1153 pParse->aNode[idx].eU = 4;
1154 pParse->aNode[idx].u.iPrev = pParse->iSubst;
1155 pParse->iSubst = idx;
1156 pParse->hasMod = 1;
1157 pParse->useMod = 1;
1158 return idx;
1162 ** Return true if z[] begins with 2 (or more) hexadecimal digits
1164 static int jsonIs2Hex(const char *z){
1165 return sqlite3Isxdigit(z[0]) && sqlite3Isxdigit(z[1]);
1169 ** Return true if z[] begins with 4 (or more) hexadecimal digits
1171 static int jsonIs4Hex(const char *z){
1172 return jsonIs2Hex(z) && jsonIs2Hex(&z[2]);
1176 ** Return the number of bytes of JSON5 whitespace at the beginning of
1177 ** the input string z[].
1179 ** JSON5 whitespace consists of any of the following characters:
1181 ** Unicode UTF-8 Name
1182 ** U+0009 09 horizontal tab
1183 ** U+000a 0a line feed
1184 ** U+000b 0b vertical tab
1185 ** U+000c 0c form feed
1186 ** U+000d 0d carriage return
1187 ** U+0020 20 space
1188 ** U+00a0 c2 a0 non-breaking space
1189 ** U+1680 e1 9a 80 ogham space mark
1190 ** U+2000 e2 80 80 en quad
1191 ** U+2001 e2 80 81 em quad
1192 ** U+2002 e2 80 82 en space
1193 ** U+2003 e2 80 83 em space
1194 ** U+2004 e2 80 84 three-per-em space
1195 ** U+2005 e2 80 85 four-per-em space
1196 ** U+2006 e2 80 86 six-per-em space
1197 ** U+2007 e2 80 87 figure space
1198 ** U+2008 e2 80 88 punctuation space
1199 ** U+2009 e2 80 89 thin space
1200 ** U+200a e2 80 8a hair space
1201 ** U+2028 e2 80 a8 line separator
1202 ** U+2029 e2 80 a9 paragraph separator
1203 ** U+202f e2 80 af narrow no-break space (NNBSP)
1204 ** U+205f e2 81 9f medium mathematical space (MMSP)
1205 ** U+3000 e3 80 80 ideographical space
1206 ** U+FEFF ef bb bf byte order mark
1208 ** In addition, comments between '/', '*' and '*', '/' and
1209 ** from '/', '/' to end-of-line are also considered to be whitespace.
1211 static int json5Whitespace(const char *zIn){
1212 int n = 0;
1213 const u8 *z = (u8*)zIn;
1214 while( 1 /*exit by "goto whitespace_done"*/ ){
1215 switch( z[n] ){
1216 case 0x09:
1217 case 0x0a:
1218 case 0x0b:
1219 case 0x0c:
1220 case 0x0d:
1221 case 0x20: {
1222 n++;
1223 break;
1225 case '/': {
1226 if( z[n+1]=='*' && z[n+2]!=0 ){
1227 int j;
1228 for(j=n+3; z[j]!='/' || z[j-1]!='*'; j++){
1229 if( z[j]==0 ) goto whitespace_done;
1231 n = j+1;
1232 break;
1233 }else if( z[n+1]=='/' ){
1234 int j;
1235 char c;
1236 for(j=n+2; (c = z[j])!=0; j++){
1237 if( c=='\n' || c=='\r' ) break;
1238 if( 0xe2==(u8)c && 0x80==(u8)z[j+1]
1239 && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2])
1241 j += 2;
1242 break;
1245 n = j;
1246 if( z[n] ) n++;
1247 break;
1249 goto whitespace_done;
1251 case 0xc2: {
1252 if( z[n+1]==0xa0 ){
1253 n += 2;
1254 break;
1256 goto whitespace_done;
1258 case 0xe1: {
1259 if( z[n+1]==0x9a && z[n+2]==0x80 ){
1260 n += 3;
1261 break;
1263 goto whitespace_done;
1265 case 0xe2: {
1266 if( z[n+1]==0x80 ){
1267 u8 c = z[n+2];
1268 if( c<0x80 ) goto whitespace_done;
1269 if( c<=0x8a || c==0xa8 || c==0xa9 || c==0xaf ){
1270 n += 3;
1271 break;
1273 }else if( z[n+1]==0x81 && z[n+2]==0x9f ){
1274 n += 3;
1275 break;
1277 goto whitespace_done;
1279 case 0xe3: {
1280 if( z[n+1]==0x80 && z[n+2]==0x80 ){
1281 n += 3;
1282 break;
1284 goto whitespace_done;
1286 case 0xef: {
1287 if( z[n+1]==0xbb && z[n+2]==0xbf ){
1288 n += 3;
1289 break;
1291 goto whitespace_done;
1293 default: {
1294 goto whitespace_done;
1298 whitespace_done:
1299 return n;
1303 ** Extra floating-point literals to allow in JSON.
1305 static const struct NanInfName {
1306 char c1;
1307 char c2;
1308 char n;
1309 char eType;
1310 char nRepl;
1311 char *zMatch;
1312 char *zRepl;
1313 } aNanInfName[] = {
1314 { 'i', 'I', 3, JSON_REAL, 7, "inf", "9.0e999" },
1315 { 'i', 'I', 8, JSON_REAL, 7, "infinity", "9.0e999" },
1316 { 'n', 'N', 3, JSON_NULL, 4, "NaN", "null" },
1317 { 'q', 'Q', 4, JSON_NULL, 4, "QNaN", "null" },
1318 { 's', 'S', 4, JSON_NULL, 4, "SNaN", "null" },
1322 ** Parse a single JSON value which begins at pParse->zJson[i]. Return the
1323 ** index of the first character past the end of the value parsed.
1325 ** Special return values:
1327 ** 0 End of input
1328 ** -1 Syntax error
1329 ** -2 '}' seen
1330 ** -3 ']' seen
1331 ** -4 ',' seen
1332 ** -5 ':' seen
1334 static int jsonParseValue(JsonParse *pParse, u32 i){
1335 char c;
1336 u32 j;
1337 int iThis;
1338 int x;
1339 JsonNode *pNode;
1340 const char *z = pParse->zJson;
1341 json_parse_restart:
1342 switch( (u8)z[i] ){
1343 case '{': {
1344 /* Parse object */
1345 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
1346 if( iThis<0 ) return -1;
1347 if( ++pParse->iDepth > JSON_MAX_DEPTH ){
1348 pParse->iErr = i;
1349 return -1;
1351 for(j=i+1;;j++){
1352 u32 nNode = pParse->nNode;
1353 x = jsonParseValue(pParse, j);
1354 if( x<=0 ){
1355 if( x==(-2) ){
1356 j = pParse->iErr;
1357 if( pParse->nNode!=(u32)iThis+1 ) pParse->hasNonstd = 1;
1358 break;
1360 j += json5Whitespace(&z[j]);
1361 if( sqlite3JsonId1(z[j])
1362 || (z[j]=='\\' && z[j+1]=='u' && jsonIs4Hex(&z[j+2]))
1364 int k = j+1;
1365 while( (sqlite3JsonId2(z[k]) && json5Whitespace(&z[k])==0)
1366 || (z[k]=='\\' && z[k+1]=='u' && jsonIs4Hex(&z[k+2]))
1368 k++;
1370 jsonParseAddNode(pParse, JSON_STRING | (JNODE_RAW<<8), k-j, &z[j]);
1371 pParse->hasNonstd = 1;
1372 x = k;
1373 }else{
1374 if( x!=-1 ) pParse->iErr = j;
1375 return -1;
1378 if( pParse->oom ) return -1;
1379 pNode = &pParse->aNode[nNode];
1380 if( pNode->eType!=JSON_STRING ){
1381 pParse->iErr = j;
1382 return -1;
1384 pNode->jnFlags |= JNODE_LABEL;
1385 j = x;
1386 if( z[j]==':' ){
1387 j++;
1388 }else{
1389 if( fast_isspace(z[j]) ){
1390 do{ j++; }while( fast_isspace(z[j]) );
1391 if( z[j]==':' ){
1392 j++;
1393 goto parse_object_value;
1396 x = jsonParseValue(pParse, j);
1397 if( x!=(-5) ){
1398 if( x!=(-1) ) pParse->iErr = j;
1399 return -1;
1401 j = pParse->iErr+1;
1403 parse_object_value:
1404 x = jsonParseValue(pParse, j);
1405 if( x<=0 ){
1406 if( x!=(-1) ) pParse->iErr = j;
1407 return -1;
1409 j = x;
1410 if( z[j]==',' ){
1411 continue;
1412 }else if( z[j]=='}' ){
1413 break;
1414 }else{
1415 if( fast_isspace(z[j]) ){
1416 do{ j++; }while( fast_isspace(z[j]) );
1417 if( z[j]==',' ){
1418 continue;
1419 }else if( z[j]=='}' ){
1420 break;
1423 x = jsonParseValue(pParse, j);
1424 if( x==(-4) ){
1425 j = pParse->iErr;
1426 continue;
1428 if( x==(-2) ){
1429 j = pParse->iErr;
1430 break;
1433 pParse->iErr = j;
1434 return -1;
1436 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
1437 pParse->iDepth--;
1438 return j+1;
1440 case '[': {
1441 /* Parse array */
1442 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
1443 if( iThis<0 ) return -1;
1444 if( ++pParse->iDepth > JSON_MAX_DEPTH ){
1445 pParse->iErr = i;
1446 return -1;
1448 memset(&pParse->aNode[iThis].u, 0, sizeof(pParse->aNode[iThis].u));
1449 for(j=i+1;;j++){
1450 x = jsonParseValue(pParse, j);
1451 if( x<=0 ){
1452 if( x==(-3) ){
1453 j = pParse->iErr;
1454 if( pParse->nNode!=(u32)iThis+1 ) pParse->hasNonstd = 1;
1455 break;
1457 if( x!=(-1) ) pParse->iErr = j;
1458 return -1;
1460 j = x;
1461 if( z[j]==',' ){
1462 continue;
1463 }else if( z[j]==']' ){
1464 break;
1465 }else{
1466 if( fast_isspace(z[j]) ){
1467 do{ j++; }while( fast_isspace(z[j]) );
1468 if( z[j]==',' ){
1469 continue;
1470 }else if( z[j]==']' ){
1471 break;
1474 x = jsonParseValue(pParse, j);
1475 if( x==(-4) ){
1476 j = pParse->iErr;
1477 continue;
1479 if( x==(-3) ){
1480 j = pParse->iErr;
1481 break;
1484 pParse->iErr = j;
1485 return -1;
1487 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
1488 pParse->iDepth--;
1489 return j+1;
1491 case '\'': {
1492 u8 jnFlags;
1493 char cDelim;
1494 pParse->hasNonstd = 1;
1495 jnFlags = JNODE_JSON5;
1496 goto parse_string;
1497 case '"':
1498 /* Parse string */
1499 jnFlags = 0;
1500 parse_string:
1501 cDelim = z[i];
1502 for(j=i+1; 1; j++){
1503 if( jsonIsOk[(unsigned char)z[j]] ) continue;
1504 c = z[j];
1505 if( c==cDelim ){
1506 break;
1507 }else if( c=='\\' ){
1508 c = z[++j];
1509 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
1510 || c=='n' || c=='r' || c=='t'
1511 || (c=='u' && jsonIs4Hex(&z[j+1])) ){
1512 jnFlags |= JNODE_ESCAPE;
1513 }else if( c=='\'' || c=='0' || c=='v' || c=='\n'
1514 || (0xe2==(u8)c && 0x80==(u8)z[j+1]
1515 && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2]))
1516 || (c=='x' && jsonIs2Hex(&z[j+1])) ){
1517 jnFlags |= (JNODE_ESCAPE|JNODE_JSON5);
1518 pParse->hasNonstd = 1;
1519 }else if( c=='\r' ){
1520 if( z[j+1]=='\n' ) j++;
1521 jnFlags |= (JNODE_ESCAPE|JNODE_JSON5);
1522 pParse->hasNonstd = 1;
1523 }else{
1524 pParse->iErr = j;
1525 return -1;
1527 }else if( c<=0x1f ){
1528 /* Control characters are not allowed in strings */
1529 pParse->iErr = j;
1530 return -1;
1533 jsonParseAddNode(pParse, JSON_STRING | (jnFlags<<8), j+1-i, &z[i]);
1534 return j+1;
1536 case 't': {
1537 if( strncmp(z+i,"true",4)==0 && !sqlite3Isalnum(z[i+4]) ){
1538 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
1539 return i+4;
1541 pParse->iErr = i;
1542 return -1;
1544 case 'f': {
1545 if( strncmp(z+i,"false",5)==0 && !sqlite3Isalnum(z[i+5]) ){
1546 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
1547 return i+5;
1549 pParse->iErr = i;
1550 return -1;
1552 case '+': {
1553 u8 seenDP, seenE, jnFlags;
1554 pParse->hasNonstd = 1;
1555 jnFlags = JNODE_JSON5;
1556 goto parse_number;
1557 case '.':
1558 if( sqlite3Isdigit(z[i+1]) ){
1559 pParse->hasNonstd = 1;
1560 jnFlags = JNODE_JSON5;
1561 seenE = 0;
1562 seenDP = JSON_REAL;
1563 goto parse_number_2;
1565 pParse->iErr = i;
1566 return -1;
1567 case '-':
1568 case '0':
1569 case '1':
1570 case '2':
1571 case '3':
1572 case '4':
1573 case '5':
1574 case '6':
1575 case '7':
1576 case '8':
1577 case '9':
1578 /* Parse number */
1579 jnFlags = 0;
1580 parse_number:
1581 seenDP = JSON_INT;
1582 seenE = 0;
1583 assert( '-' < '0' );
1584 assert( '+' < '0' );
1585 assert( '.' < '0' );
1586 c = z[i];
1588 if( c<='0' ){
1589 if( c=='0' ){
1590 if( (z[i+1]=='x' || z[i+1]=='X') && sqlite3Isxdigit(z[i+2]) ){
1591 assert( seenDP==JSON_INT );
1592 pParse->hasNonstd = 1;
1593 jnFlags |= JNODE_JSON5;
1594 for(j=i+3; sqlite3Isxdigit(z[j]); j++){}
1595 goto parse_number_finish;
1596 }else if( sqlite3Isdigit(z[i+1]) ){
1597 pParse->iErr = i+1;
1598 return -1;
1600 }else{
1601 if( !sqlite3Isdigit(z[i+1]) ){
1602 /* JSON5 allows for "+Infinity" and "-Infinity" using exactly
1603 ** that case. SQLite also allows these in any case and it allows
1604 ** "+inf" and "-inf". */
1605 if( (z[i+1]=='I' || z[i+1]=='i')
1606 && sqlite3StrNICmp(&z[i+1], "inf",3)==0
1608 pParse->hasNonstd = 1;
1609 if( z[i]=='-' ){
1610 jsonParseAddNode(pParse, JSON_REAL, 8, "-9.0e999");
1611 }else{
1612 jsonParseAddNode(pParse, JSON_REAL, 7, "9.0e999");
1614 return i + (sqlite3StrNICmp(&z[i+4],"inity",5)==0 ? 9 : 4);
1616 if( z[i+1]=='.' ){
1617 pParse->hasNonstd = 1;
1618 jnFlags |= JNODE_JSON5;
1619 goto parse_number_2;
1621 pParse->iErr = i;
1622 return -1;
1624 if( z[i+1]=='0' ){
1625 if( sqlite3Isdigit(z[i+2]) ){
1626 pParse->iErr = i+1;
1627 return -1;
1628 }else if( (z[i+2]=='x' || z[i+2]=='X') && sqlite3Isxdigit(z[i+3]) ){
1629 pParse->hasNonstd = 1;
1630 jnFlags |= JNODE_JSON5;
1631 for(j=i+4; sqlite3Isxdigit(z[j]); j++){}
1632 goto parse_number_finish;
1637 parse_number_2:
1638 for(j=i+1;; j++){
1639 c = z[j];
1640 if( sqlite3Isdigit(c) ) continue;
1641 if( c=='.' ){
1642 if( seenDP==JSON_REAL ){
1643 pParse->iErr = j;
1644 return -1;
1646 seenDP = JSON_REAL;
1647 continue;
1649 if( c=='e' || c=='E' ){
1650 if( z[j-1]<'0' ){
1651 if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){
1652 pParse->hasNonstd = 1;
1653 jnFlags |= JNODE_JSON5;
1654 }else{
1655 pParse->iErr = j;
1656 return -1;
1659 if( seenE ){
1660 pParse->iErr = j;
1661 return -1;
1663 seenDP = JSON_REAL;
1664 seenE = 1;
1665 c = z[j+1];
1666 if( c=='+' || c=='-' ){
1667 j++;
1668 c = z[j+1];
1670 if( c<'0' || c>'9' ){
1671 pParse->iErr = j;
1672 return -1;
1674 continue;
1676 break;
1678 if( z[j-1]<'0' ){
1679 if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){
1680 pParse->hasNonstd = 1;
1681 jnFlags |= JNODE_JSON5;
1682 }else{
1683 pParse->iErr = j;
1684 return -1;
1687 parse_number_finish:
1688 jsonParseAddNode(pParse, seenDP | (jnFlags<<8), j - i, &z[i]);
1689 return j;
1691 case '}': {
1692 pParse->iErr = i;
1693 return -2; /* End of {...} */
1695 case ']': {
1696 pParse->iErr = i;
1697 return -3; /* End of [...] */
1699 case ',': {
1700 pParse->iErr = i;
1701 return -4; /* List separator */
1703 case ':': {
1704 pParse->iErr = i;
1705 return -5; /* Object label/value separator */
1707 case 0: {
1708 return 0; /* End of file */
1710 case 0x09:
1711 case 0x0a:
1712 case 0x0d:
1713 case 0x20: {
1715 i++;
1716 }while( fast_isspace(z[i]) );
1717 goto json_parse_restart;
1719 case 0x0b:
1720 case 0x0c:
1721 case '/':
1722 case 0xc2:
1723 case 0xe1:
1724 case 0xe2:
1725 case 0xe3:
1726 case 0xef: {
1727 j = json5Whitespace(&z[i]);
1728 if( j>0 ){
1729 i += j;
1730 pParse->hasNonstd = 1;
1731 goto json_parse_restart;
1733 pParse->iErr = i;
1734 return -1;
1736 case 'n': {
1737 if( strncmp(z+i,"null",4)==0 && !sqlite3Isalnum(z[i+4]) ){
1738 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
1739 return i+4;
1741 /* fall-through into the default case that checks for NaN */
1743 default: {
1744 u32 k;
1745 int nn;
1746 c = z[i];
1747 for(k=0; k<sizeof(aNanInfName)/sizeof(aNanInfName[0]); k++){
1748 if( c!=aNanInfName[k].c1 && c!=aNanInfName[k].c2 ) continue;
1749 nn = aNanInfName[k].n;
1750 if( sqlite3StrNICmp(&z[i], aNanInfName[k].zMatch, nn)!=0 ){
1751 continue;
1753 if( sqlite3Isalnum(z[i+nn]) ) continue;
1754 jsonParseAddNode(pParse, aNanInfName[k].eType,
1755 aNanInfName[k].nRepl, aNanInfName[k].zRepl);
1756 pParse->hasNonstd = 1;
1757 return i + nn;
1759 pParse->iErr = i;
1760 return -1; /* Syntax error */
1762 } /* End switch(z[i]) */
1766 ** Parse a complete JSON string. Return 0 on success or non-zero if there
1767 ** are any errors. If an error occurs, free all memory held by pParse,
1768 ** but not pParse itself.
1770 ** pParse must be initialized to an empty parse object prior to calling
1771 ** this routine.
1773 static int jsonParse(
1774 JsonParse *pParse, /* Initialize and fill this JsonParse object */
1775 sqlite3_context *pCtx /* Report errors here */
1777 int i;
1778 const char *zJson = pParse->zJson;
1779 i = jsonParseValue(pParse, 0);
1780 if( pParse->oom ) i = -1;
1781 if( i>0 ){
1782 assert( pParse->iDepth==0 );
1783 while( fast_isspace(zJson[i]) ) i++;
1784 if( zJson[i] ){
1785 i += json5Whitespace(&zJson[i]);
1786 if( zJson[i] ){
1787 jsonParseReset(pParse);
1788 return 1;
1790 pParse->hasNonstd = 1;
1793 if( i<=0 ){
1794 if( pCtx!=0 ){
1795 if( pParse->oom ){
1796 sqlite3_result_error_nomem(pCtx);
1797 }else{
1798 sqlite3_result_error(pCtx, "malformed JSON", -1);
1801 jsonParseReset(pParse);
1802 return 1;
1804 return 0;
1808 /* Mark node i of pParse as being a child of iParent. Call recursively
1809 ** to fill in all the descendants of node i.
1811 static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
1812 JsonNode *pNode = &pParse->aNode[i];
1813 u32 j;
1814 pParse->aUp[i] = iParent;
1815 switch( pNode->eType ){
1816 case JSON_ARRAY: {
1817 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
1818 jsonParseFillInParentage(pParse, i+j, i);
1820 break;
1822 case JSON_OBJECT: {
1823 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
1824 pParse->aUp[i+j] = i;
1825 jsonParseFillInParentage(pParse, i+j+1, i);
1827 break;
1829 default: {
1830 break;
1836 ** Compute the parentage of all nodes in a completed parse.
1838 static int jsonParseFindParents(JsonParse *pParse){
1839 u32 *aUp;
1840 assert( pParse->aUp==0 );
1841 aUp = pParse->aUp = sqlite3_malloc64( sizeof(u32)*pParse->nNode );
1842 if( aUp==0 ){
1843 pParse->oom = 1;
1844 return SQLITE_NOMEM;
1846 jsonParseFillInParentage(pParse, 0, 0);
1847 return SQLITE_OK;
1851 ** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
1853 #define JSON_CACHE_ID (-429938) /* First cache entry */
1854 #define JSON_CACHE_SZ 4 /* Max number of cache entries */
1857 ** Obtain a complete parse of the JSON found in the pJson argument
1859 ** Use the sqlite3_get_auxdata() cache to find a preexisting parse
1860 ** if it is available. If the cache is not available or if it
1861 ** is no longer valid, parse the JSON again and return the new parse.
1862 ** Also register the new parse so that it will be available for
1863 ** future sqlite3_get_auxdata() calls.
1865 ** If an error occurs and pErrCtx!=0 then report the error on pErrCtx
1866 ** and return NULL.
1868 ** The returned pointer (if it is not NULL) is owned by the cache in
1869 ** most cases, not the caller. The caller does NOT need to invoke
1870 ** jsonParseFree(), in most cases.
1872 ** Except, if an error occurs and pErrCtx==0 then return the JsonParse
1873 ** object with JsonParse.nErr non-zero and the caller will own the JsonParse
1874 ** object. In that case, it will be the responsibility of the caller to
1875 ** invoke jsonParseFree(). To summarize:
1877 ** pErrCtx!=0 || p->nErr==0 ==> Return value p is owned by the
1878 ** cache. Call does not need to
1879 ** free it.
1881 ** pErrCtx==0 && p->nErr!=0 ==> Return value is owned by the caller
1882 ** and so the caller must free it.
1884 static JsonParse *jsonParseCached(
1885 sqlite3_context *pCtx, /* Context to use for cache search */
1886 sqlite3_value *pJson, /* Function param containing JSON text */
1887 sqlite3_context *pErrCtx, /* Write parse errors here if not NULL */
1888 int bUnedited /* No prior edits allowed */
1890 char *zJson = (char*)sqlite3_value_text(pJson);
1891 int nJson = sqlite3_value_bytes(pJson);
1892 JsonParse *p;
1893 JsonParse *pMatch = 0;
1894 int iKey;
1895 int iMinKey = 0;
1896 u32 iMinHold = 0xffffffff;
1897 u32 iMaxHold = 0;
1898 int bJsonRCStr;
1900 if( zJson==0 ) return 0;
1901 for(iKey=0; iKey<JSON_CACHE_SZ; iKey++){
1902 p = (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iKey);
1903 if( p==0 ){
1904 iMinKey = iKey;
1905 break;
1907 if( pMatch==0
1908 && p->nJson==nJson
1909 && (p->hasMod==0 || bUnedited==0)
1910 && (p->zJson==zJson || memcmp(p->zJson,zJson,nJson)==0)
1912 p->nErr = 0;
1913 p->useMod = 0;
1914 pMatch = p;
1915 }else
1916 if( pMatch==0
1917 && p->zAlt!=0
1918 && bUnedited==0
1919 && p->nAlt==nJson
1920 && memcmp(p->zAlt, zJson, nJson)==0
1922 p->nErr = 0;
1923 p->useMod = 1;
1924 pMatch = p;
1925 }else if( p->iHold<iMinHold ){
1926 iMinHold = p->iHold;
1927 iMinKey = iKey;
1929 if( p->iHold>iMaxHold ){
1930 iMaxHold = p->iHold;
1933 if( pMatch ){
1934 /* The input JSON text was found in the cache. Use the preexisting
1935 ** parse of this JSON */
1936 pMatch->nErr = 0;
1937 pMatch->iHold = iMaxHold+1;
1938 assert( pMatch->nJPRef>0 ); /* pMatch is owned by the cache */
1939 return pMatch;
1942 /* The input JSON was not found anywhere in the cache. We will need
1943 ** to parse it ourselves and generate a new JsonParse object.
1945 bJsonRCStr = sqlite3ValueIsOfClass(pJson,(void(*)(void*))sqlite3RCStrUnref);
1946 p = sqlite3_malloc64( sizeof(*p) + (bJsonRCStr ? 0 : nJson+1) );
1947 if( p==0 ){
1948 sqlite3_result_error_nomem(pCtx);
1949 return 0;
1951 memset(p, 0, sizeof(*p));
1952 if( bJsonRCStr ){
1953 p->zJson = sqlite3RCStrRef(zJson);
1954 p->bJsonIsRCStr = 1;
1955 }else{
1956 p->zJson = (char*)&p[1];
1957 memcpy(p->zJson, zJson, nJson+1);
1959 p->nJPRef = 1;
1960 if( jsonParse(p, pErrCtx) ){
1961 if( pErrCtx==0 ){
1962 p->nErr = 1;
1963 assert( p->nJPRef==1 ); /* Caller will own the new JsonParse object p */
1964 return p;
1966 jsonParseFree(p);
1967 return 0;
1969 p->nJson = nJson;
1970 p->iHold = iMaxHold+1;
1971 /* Transfer ownership of the new JsonParse to the cache */
1972 sqlite3_set_auxdata(pCtx, JSON_CACHE_ID+iMinKey, p,
1973 (void(*)(void*))jsonParseFree);
1974 return (JsonParse*)sqlite3_get_auxdata(pCtx, JSON_CACHE_ID+iMinKey);
1978 ** Compare the OBJECT label at pNode against zKey,nKey. Return true on
1979 ** a match.
1981 static int jsonLabelCompare(const JsonNode *pNode, const char *zKey, u32 nKey){
1982 assert( pNode->eU==1 );
1983 if( pNode->jnFlags & JNODE_RAW ){
1984 if( pNode->n!=nKey ) return 0;
1985 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
1986 }else{
1987 if( pNode->n!=nKey+2 ) return 0;
1988 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
1991 static int jsonSameLabel(const JsonNode *p1, const JsonNode *p2){
1992 if( p1->jnFlags & JNODE_RAW ){
1993 return jsonLabelCompare(p2, p1->u.zJContent, p1->n);
1994 }else if( p2->jnFlags & JNODE_RAW ){
1995 return jsonLabelCompare(p1, p2->u.zJContent, p2->n);
1996 }else{
1997 return p1->n==p2->n && strncmp(p1->u.zJContent,p2->u.zJContent,p1->n)==0;
2001 /* forward declaration */
2002 static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
2005 ** Search along zPath to find the node specified. Return a pointer
2006 ** to that node, or NULL if zPath is malformed or if there is no such
2007 ** node.
2009 ** If pApnd!=0, then try to append new nodes to complete zPath if it is
2010 ** possible to do so and if no existing node corresponds to zPath. If
2011 ** new nodes are appended *pApnd is set to 1.
2013 static JsonNode *jsonLookupStep(
2014 JsonParse *pParse, /* The JSON to search */
2015 u32 iRoot, /* Begin the search at this node */
2016 const char *zPath, /* The path to search */
2017 int *pApnd, /* Append nodes to complete path if not NULL */
2018 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
2020 u32 i, j, nKey;
2021 const char *zKey;
2022 JsonNode *pRoot;
2023 if( pParse->oom ) return 0;
2024 pRoot = &pParse->aNode[iRoot];
2025 if( pRoot->jnFlags & (JNODE_REPLACE|JNODE_REMOVE) && pParse->useMod ){
2026 while( (pRoot->jnFlags & JNODE_REPLACE)!=0 ){
2027 u32 idx = (u32)(pRoot - pParse->aNode);
2028 i = pParse->iSubst;
2029 while( 1 /*exit-by-break*/ ){
2030 assert( i<pParse->nNode );
2031 assert( pParse->aNode[i].eType==JSON_SUBST );
2032 assert( pParse->aNode[i].eU==4 );
2033 assert( pParse->aNode[i].u.iPrev<i );
2034 if( pParse->aNode[i].n==idx ){
2035 pRoot = &pParse->aNode[i+1];
2036 iRoot = i+1;
2037 break;
2039 i = pParse->aNode[i].u.iPrev;
2042 if( pRoot->jnFlags & JNODE_REMOVE ){
2043 return 0;
2046 if( zPath[0]==0 ) return pRoot;
2047 if( zPath[0]=='.' ){
2048 if( pRoot->eType!=JSON_OBJECT ) return 0;
2049 zPath++;
2050 if( zPath[0]=='"' ){
2051 zKey = zPath + 1;
2052 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
2053 nKey = i-1;
2054 if( zPath[i] ){
2055 i++;
2056 }else{
2057 *pzErr = zPath;
2058 return 0;
2060 testcase( nKey==0 );
2061 }else{
2062 zKey = zPath;
2063 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
2064 nKey = i;
2065 if( nKey==0 ){
2066 *pzErr = zPath;
2067 return 0;
2070 j = 1;
2071 for(;;){
2072 while( j<=pRoot->n ){
2073 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
2074 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
2076 j++;
2077 j += jsonNodeSize(&pRoot[j]);
2079 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
2080 if( pParse->useMod==0 ) break;
2081 assert( pRoot->eU==2 );
2082 iRoot = pRoot->u.iAppend;
2083 pRoot = &pParse->aNode[iRoot];
2084 j = 1;
2086 if( pApnd ){
2087 u32 iStart, iLabel;
2088 JsonNode *pNode;
2089 assert( pParse->useMod );
2090 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
2091 iLabel = jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
2092 zPath += i;
2093 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
2094 if( pParse->oom ) return 0;
2095 if( pNode ){
2096 pRoot = &pParse->aNode[iRoot];
2097 assert( pRoot->eU==0 );
2098 pRoot->u.iAppend = iStart;
2099 pRoot->jnFlags |= JNODE_APPEND;
2100 VVA( pRoot->eU = 2 );
2101 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
2103 return pNode;
2105 }else if( zPath[0]=='[' ){
2106 i = 0;
2107 j = 1;
2108 while( sqlite3Isdigit(zPath[j]) ){
2109 i = i*10 + zPath[j] - '0';
2110 j++;
2112 if( j<2 || zPath[j]!=']' ){
2113 if( zPath[1]=='#' ){
2114 JsonNode *pBase = pRoot;
2115 int iBase = iRoot;
2116 if( pRoot->eType!=JSON_ARRAY ) return 0;
2117 for(;;){
2118 while( j<=pBase->n ){
2119 if( (pBase[j].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ) i++;
2120 j += jsonNodeSize(&pBase[j]);
2122 if( (pBase->jnFlags & JNODE_APPEND)==0 ) break;
2123 if( pParse->useMod==0 ) break;
2124 assert( pBase->eU==2 );
2125 iBase = pBase->u.iAppend;
2126 pBase = &pParse->aNode[iBase];
2127 j = 1;
2129 j = 2;
2130 if( zPath[2]=='-' && sqlite3Isdigit(zPath[3]) ){
2131 unsigned int x = 0;
2132 j = 3;
2134 x = x*10 + zPath[j] - '0';
2135 j++;
2136 }while( sqlite3Isdigit(zPath[j]) );
2137 if( x>i ) return 0;
2138 i -= x;
2140 if( zPath[j]!=']' ){
2141 *pzErr = zPath;
2142 return 0;
2144 }else{
2145 *pzErr = zPath;
2146 return 0;
2149 if( pRoot->eType!=JSON_ARRAY ) return 0;
2150 zPath += j + 1;
2151 j = 1;
2152 for(;;){
2153 while( j<=pRoot->n
2154 && (i>0 || ((pRoot[j].jnFlags & JNODE_REMOVE)!=0 && pParse->useMod))
2156 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 || pParse->useMod==0 ) i--;
2157 j += jsonNodeSize(&pRoot[j]);
2159 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
2160 if( pParse->useMod==0 ) break;
2161 assert( pRoot->eU==2 );
2162 iRoot = pRoot->u.iAppend;
2163 pRoot = &pParse->aNode[iRoot];
2164 j = 1;
2166 if( j<=pRoot->n ){
2167 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
2169 if( i==0 && pApnd ){
2170 u32 iStart;
2171 JsonNode *pNode;
2172 assert( pParse->useMod );
2173 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
2174 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
2175 if( pParse->oom ) return 0;
2176 if( pNode ){
2177 pRoot = &pParse->aNode[iRoot];
2178 assert( pRoot->eU==0 );
2179 pRoot->u.iAppend = iStart;
2180 pRoot->jnFlags |= JNODE_APPEND;
2181 VVA( pRoot->eU = 2 );
2183 return pNode;
2185 }else{
2186 *pzErr = zPath;
2188 return 0;
2192 ** Append content to pParse that will complete zPath. Return a pointer
2193 ** to the inserted node, or return NULL if the append fails.
2195 static JsonNode *jsonLookupAppend(
2196 JsonParse *pParse, /* Append content to the JSON parse */
2197 const char *zPath, /* Description of content to append */
2198 int *pApnd, /* Set this flag to 1 */
2199 const char **pzErr /* Make this point to any syntax error */
2201 *pApnd = 1;
2202 if( zPath[0]==0 ){
2203 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
2204 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
2206 if( zPath[0]=='.' ){
2207 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
2208 }else if( strncmp(zPath,"[0]",3)==0 ){
2209 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
2210 }else{
2211 return 0;
2213 if( pParse->oom ) return 0;
2214 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
2218 ** Return the text of a syntax error message on a JSON path. Space is
2219 ** obtained from sqlite3_malloc().
2221 static char *jsonPathSyntaxError(const char *zErr){
2222 return sqlite3_mprintf("JSON path error near '%q'", zErr);
2226 ** Do a node lookup using zPath. Return a pointer to the node on success.
2227 ** Return NULL if not found or if there is an error.
2229 ** On an error, write an error message into pCtx and increment the
2230 ** pParse->nErr counter.
2232 ** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
2233 ** nodes are appended.
2235 static JsonNode *jsonLookup(
2236 JsonParse *pParse, /* The JSON to search */
2237 const char *zPath, /* The path to search */
2238 int *pApnd, /* Append nodes to complete path if not NULL */
2239 sqlite3_context *pCtx /* Report errors here, if not NULL */
2241 const char *zErr = 0;
2242 JsonNode *pNode = 0;
2243 char *zMsg;
2245 if( zPath==0 ) return 0;
2246 if( zPath[0]!='$' ){
2247 zErr = zPath;
2248 goto lookup_err;
2250 zPath++;
2251 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
2252 if( zErr==0 ) return pNode;
2254 lookup_err:
2255 pParse->nErr++;
2256 assert( zErr!=0 && pCtx!=0 );
2257 zMsg = jsonPathSyntaxError(zErr);
2258 if( zMsg ){
2259 sqlite3_result_error(pCtx, zMsg, -1);
2260 sqlite3_free(zMsg);
2261 }else{
2262 sqlite3_result_error_nomem(pCtx);
2264 return 0;
2269 ** Report the wrong number of arguments for json_insert(), json_replace()
2270 ** or json_set().
2272 static void jsonWrongNumArgs(
2273 sqlite3_context *pCtx,
2274 const char *zFuncName
2276 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
2277 zFuncName);
2278 sqlite3_result_error(pCtx, zMsg, -1);
2279 sqlite3_free(zMsg);
2283 ** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
2285 static void jsonRemoveAllNulls(JsonNode *pNode){
2286 int i, n;
2287 assert( pNode->eType==JSON_OBJECT );
2288 n = pNode->n;
2289 for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
2290 switch( pNode[i].eType ){
2291 case JSON_NULL:
2292 pNode[i].jnFlags |= JNODE_REMOVE;
2293 break;
2294 case JSON_OBJECT:
2295 jsonRemoveAllNulls(&pNode[i]);
2296 break;
2302 /****************************************************************************
2303 ** SQL functions used for testing and debugging
2304 ****************************************************************************/
2306 #if SQLITE_DEBUG
2308 ** Print N node entries.
2310 static void jsonDebugPrintNodeEntries(
2311 JsonNode *aNode, /* First node entry to print */
2312 int N /* Number of node entries to print */
2314 int i;
2315 for(i=0; i<N; i++){
2316 const char *zType;
2317 if( aNode[i].jnFlags & JNODE_LABEL ){
2318 zType = "label";
2319 }else{
2320 zType = jsonType[aNode[i].eType];
2322 printf("node %4u: %-7s n=%-5d", i, zType, aNode[i].n);
2323 if( (aNode[i].jnFlags & ~JNODE_LABEL)!=0 ){
2324 u8 f = aNode[i].jnFlags;
2325 if( f & JNODE_RAW ) printf(" RAW");
2326 if( f & JNODE_ESCAPE ) printf(" ESCAPE");
2327 if( f & JNODE_REMOVE ) printf(" REMOVE");
2328 if( f & JNODE_REPLACE ) printf(" REPLACE");
2329 if( f & JNODE_APPEND ) printf(" APPEND");
2330 if( f & JNODE_JSON5 ) printf(" JSON5");
2332 switch( aNode[i].eU ){
2333 case 1: printf(" zJContent=[%.*s]\n",
2334 aNode[i].n, aNode[i].u.zJContent); break;
2335 case 2: printf(" iAppend=%u\n", aNode[i].u.iAppend); break;
2336 case 3: printf(" iKey=%u\n", aNode[i].u.iKey); break;
2337 case 4: printf(" iPrev=%u\n", aNode[i].u.iPrev); break;
2338 default: printf("\n");
2342 #endif /* SQLITE_DEBUG */
2345 #if 0 /* 1 for debugging. 0 normally. Requires -DSQLITE_DEBUG too */
2346 static void jsonDebugPrintParse(JsonParse *p){
2347 jsonDebugPrintNodeEntries(p->aNode, p->nNode);
2349 static void jsonDebugPrintNode(JsonNode *pNode){
2350 jsonDebugPrintNodeEntries(pNode, jsonNodeSize(pNode));
2352 #else
2353 /* The usual case */
2354 # define jsonDebugPrintNode(X)
2355 # define jsonDebugPrintParse(X)
2356 #endif
2358 #ifdef SQLITE_DEBUG
2360 ** SQL function: json_parse(JSON)
2362 ** Parse JSON using jsonParseCached(). Then print a dump of that
2363 ** parse on standard output. Return the mimified JSON result, just
2364 ** like the json() function.
2366 static void jsonParseFunc(
2367 sqlite3_context *ctx,
2368 int argc,
2369 sqlite3_value **argv
2371 JsonParse *p; /* The parse */
2373 assert( argc==1 );
2374 p = jsonParseCached(ctx, argv[0], ctx, 0);
2375 if( p==0 ) return;
2376 printf("nNode = %u\n", p->nNode);
2377 printf("nAlloc = %u\n", p->nAlloc);
2378 printf("nJson = %d\n", p->nJson);
2379 printf("nAlt = %d\n", p->nAlt);
2380 printf("nErr = %u\n", p->nErr);
2381 printf("oom = %u\n", p->oom);
2382 printf("hasNonstd = %u\n", p->hasNonstd);
2383 printf("useMod = %u\n", p->useMod);
2384 printf("hasMod = %u\n", p->hasMod);
2385 printf("nJPRef = %u\n", p->nJPRef);
2386 printf("iSubst = %u\n", p->iSubst);
2387 printf("iHold = %u\n", p->iHold);
2388 jsonDebugPrintNodeEntries(p->aNode, p->nNode);
2389 jsonReturnJson(p, p->aNode, ctx, 1);
2393 ** The json_test1(JSON) function return true (1) if the input is JSON
2394 ** text generated by another json function. It returns (0) if the input
2395 ** is not known to be JSON.
2397 static void jsonTest1Func(
2398 sqlite3_context *ctx,
2399 int argc,
2400 sqlite3_value **argv
2402 UNUSED_PARAMETER(argc);
2403 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
2405 #endif /* SQLITE_DEBUG */
2407 /****************************************************************************
2408 ** Scalar SQL function implementations
2409 ****************************************************************************/
2412 ** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
2413 ** corresponding to the SQL value input. Mostly this means putting
2414 ** double-quotes around strings and returning the unquoted string "null"
2415 ** when given a NULL input.
2417 static void jsonQuoteFunc(
2418 sqlite3_context *ctx,
2419 int argc,
2420 sqlite3_value **argv
2422 JsonString jx;
2423 UNUSED_PARAMETER(argc);
2425 jsonInit(&jx, ctx);
2426 jsonAppendValue(&jx, argv[0]);
2427 jsonResult(&jx);
2428 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2432 ** Implementation of the json_array(VALUE,...) function. Return a JSON
2433 ** array that contains all values given in arguments. Or if any argument
2434 ** is a BLOB, throw an error.
2436 static void jsonArrayFunc(
2437 sqlite3_context *ctx,
2438 int argc,
2439 sqlite3_value **argv
2441 int i;
2442 JsonString jx;
2444 jsonInit(&jx, ctx);
2445 jsonAppendChar(&jx, '[');
2446 for(i=0; i<argc; i++){
2447 jsonAppendSeparator(&jx);
2448 jsonAppendValue(&jx, argv[i]);
2450 jsonAppendChar(&jx, ']');
2451 jsonResult(&jx);
2452 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2457 ** json_array_length(JSON)
2458 ** json_array_length(JSON, PATH)
2460 ** Return the number of elements in the top-level JSON array.
2461 ** Return 0 if the input is not a well-formed JSON array.
2463 static void jsonArrayLengthFunc(
2464 sqlite3_context *ctx,
2465 int argc,
2466 sqlite3_value **argv
2468 JsonParse *p; /* The parse */
2469 sqlite3_int64 n = 0;
2470 u32 i;
2471 JsonNode *pNode;
2473 p = jsonParseCached(ctx, argv[0], ctx, 0);
2474 if( p==0 ) return;
2475 assert( p->nNode );
2476 if( argc==2 ){
2477 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
2478 pNode = jsonLookup(p, zPath, 0, ctx);
2479 }else{
2480 pNode = p->aNode;
2482 if( pNode==0 ){
2483 return;
2485 if( pNode->eType==JSON_ARRAY ){
2486 while( 1 /*exit-by-break*/ ){
2487 for(i=1; i<=pNode->n; n++){
2488 i += jsonNodeSize(&pNode[i]);
2490 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
2491 if( p->useMod==0 ) break;
2492 assert( pNode->eU==2 );
2493 pNode = &p->aNode[pNode->u.iAppend];
2496 sqlite3_result_int64(ctx, n);
2500 ** Bit values for the flags passed into jsonExtractFunc() or
2501 ** jsonSetFunc() via the user-data value.
2503 #define JSON_JSON 0x01 /* Result is always JSON */
2504 #define JSON_SQL 0x02 /* Result is always SQL */
2505 #define JSON_ABPATH 0x03 /* Allow abbreviated JSON path specs */
2506 #define JSON_ISSET 0x04 /* json_set(), not json_insert() */
2509 ** json_extract(JSON, PATH, ...)
2510 ** "->"(JSON,PATH)
2511 ** "->>"(JSON,PATH)
2513 ** Return the element described by PATH. Return NULL if that PATH element
2514 ** is not found.
2516 ** If JSON_JSON is set or if more that one PATH argument is supplied then
2517 ** always return a JSON representation of the result. If JSON_SQL is set,
2518 ** then always return an SQL representation of the result. If neither flag
2519 ** is present and argc==2, then return JSON for objects and arrays and SQL
2520 ** for all other values.
2522 ** When multiple PATH arguments are supplied, the result is a JSON array
2523 ** containing the result of each PATH.
2525 ** Abbreviated JSON path expressions are allows if JSON_ABPATH, for
2526 ** compatibility with PG.
2528 static void jsonExtractFunc(
2529 sqlite3_context *ctx,
2530 int argc,
2531 sqlite3_value **argv
2533 JsonParse *p; /* The parse */
2534 JsonNode *pNode;
2535 const char *zPath;
2536 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
2537 JsonString jx;
2539 if( argc<2 ) return;
2540 p = jsonParseCached(ctx, argv[0], ctx, 0);
2541 if( p==0 ) return;
2542 if( argc==2 ){
2543 /* With a single PATH argument */
2544 zPath = (const char*)sqlite3_value_text(argv[1]);
2545 if( zPath==0 ) return;
2546 if( flags & JSON_ABPATH ){
2547 if( zPath[0]!='$' || (zPath[1]!='.' && zPath[1]!='[' && zPath[1]!=0) ){
2548 /* The -> and ->> operators accept abbreviated PATH arguments. This
2549 ** is mostly for compatibility with PostgreSQL, but also for
2550 ** convenience.
2552 ** NUMBER ==> $[NUMBER] // PG compatible
2553 ** LABEL ==> $.LABEL // PG compatible
2554 ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience
2556 jsonInit(&jx, ctx);
2557 if( sqlite3Isdigit(zPath[0]) ){
2558 jsonAppendRawNZ(&jx, "$[", 2);
2559 jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
2560 jsonAppendRawNZ(&jx, "]", 2);
2561 }else{
2562 jsonAppendRawNZ(&jx, "$.", 1 + (zPath[0]!='['));
2563 jsonAppendRaw(&jx, zPath, (int)strlen(zPath));
2564 jsonAppendChar(&jx, 0);
2566 pNode = jx.bErr ? 0 : jsonLookup(p, jx.zBuf, 0, ctx);
2567 jsonReset(&jx);
2568 }else{
2569 pNode = jsonLookup(p, zPath, 0, ctx);
2571 if( pNode ){
2572 if( flags & JSON_JSON ){
2573 jsonReturnJson(p, pNode, ctx, 0);
2574 }else{
2575 jsonReturn(p, pNode, ctx);
2576 sqlite3_result_subtype(ctx, 0);
2579 }else{
2580 pNode = jsonLookup(p, zPath, 0, ctx);
2581 if( p->nErr==0 && pNode ) jsonReturn(p, pNode, ctx);
2583 }else{
2584 /* Two or more PATH arguments results in a JSON array with each
2585 ** element of the array being the value selected by one of the PATHs */
2586 int i;
2587 jsonInit(&jx, ctx);
2588 jsonAppendChar(&jx, '[');
2589 for(i=1; i<argc; i++){
2590 zPath = (const char*)sqlite3_value_text(argv[i]);
2591 pNode = jsonLookup(p, zPath, 0, ctx);
2592 if( p->nErr ) break;
2593 jsonAppendSeparator(&jx);
2594 if( pNode ){
2595 jsonRenderNode(p, pNode, &jx);
2596 }else{
2597 jsonAppendRawNZ(&jx, "null", 4);
2600 if( i==argc ){
2601 jsonAppendChar(&jx, ']');
2602 jsonResult(&jx);
2603 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2605 jsonReset(&jx);
2609 /* This is the RFC 7396 MergePatch algorithm.
2611 static JsonNode *jsonMergePatch(
2612 JsonParse *pParse, /* The JSON parser that contains the TARGET */
2613 u32 iTarget, /* Node of the TARGET in pParse */
2614 JsonNode *pPatch /* The PATCH */
2616 u32 i, j;
2617 u32 iRoot;
2618 JsonNode *pTarget;
2619 if( pPatch->eType!=JSON_OBJECT ){
2620 return pPatch;
2622 assert( iTarget<pParse->nNode );
2623 pTarget = &pParse->aNode[iTarget];
2624 assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
2625 if( pTarget->eType!=JSON_OBJECT ){
2626 jsonRemoveAllNulls(pPatch);
2627 return pPatch;
2629 iRoot = iTarget;
2630 for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
2631 u32 nKey;
2632 const char *zKey;
2633 assert( pPatch[i].eType==JSON_STRING );
2634 assert( pPatch[i].jnFlags & JNODE_LABEL );
2635 assert( pPatch[i].eU==1 );
2636 nKey = pPatch[i].n;
2637 zKey = pPatch[i].u.zJContent;
2638 for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
2639 assert( pTarget[j].eType==JSON_STRING );
2640 assert( pTarget[j].jnFlags & JNODE_LABEL );
2641 if( jsonSameLabel(&pPatch[i], &pTarget[j]) ){
2642 if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_REPLACE) ) break;
2643 if( pPatch[i+1].eType==JSON_NULL ){
2644 pTarget[j+1].jnFlags |= JNODE_REMOVE;
2645 }else{
2646 JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
2647 if( pNew==0 ) return 0;
2648 if( pNew!=&pParse->aNode[iTarget+j+1] ){
2649 jsonParseAddSubstNode(pParse, iTarget+j+1);
2650 jsonParseAddNodeArray(pParse, pNew, jsonNodeSize(pNew));
2652 pTarget = &pParse->aNode[iTarget];
2654 break;
2657 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
2658 int iStart;
2659 JsonNode *pApnd;
2660 u32 nApnd;
2661 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
2662 jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
2663 pApnd = &pPatch[i+1];
2664 if( pApnd->eType==JSON_OBJECT ) jsonRemoveAllNulls(pApnd);
2665 nApnd = jsonNodeSize(pApnd);
2666 jsonParseAddNodeArray(pParse, pApnd, jsonNodeSize(pApnd));
2667 if( pParse->oom ) return 0;
2668 pParse->aNode[iStart].n = 1+nApnd;
2669 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
2670 pParse->aNode[iRoot].u.iAppend = iStart;
2671 VVA( pParse->aNode[iRoot].eU = 2 );
2672 iRoot = iStart;
2673 pTarget = &pParse->aNode[iTarget];
2676 return pTarget;
2680 ** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
2681 ** object that is the result of running the RFC 7396 MergePatch() algorithm
2682 ** on the two arguments.
2684 static void jsonPatchFunc(
2685 sqlite3_context *ctx,
2686 int argc,
2687 sqlite3_value **argv
2689 JsonParse *pX; /* The JSON that is being patched */
2690 JsonParse *pY; /* The patch */
2691 JsonNode *pResult; /* The result of the merge */
2693 UNUSED_PARAMETER(argc);
2694 pX = jsonParseCached(ctx, argv[0], ctx, 1);
2695 if( pX==0 ) return;
2696 assert( pX->hasMod==0 );
2697 pX->hasMod = 1;
2698 pY = jsonParseCached(ctx, argv[1], ctx, 1);
2699 if( pY==0 ) return;
2700 pX->useMod = 1;
2701 pY->useMod = 1;
2702 pResult = jsonMergePatch(pX, 0, pY->aNode);
2703 assert( pResult!=0 || pX->oom );
2704 if( pResult && pX->oom==0 ){
2705 jsonDebugPrintParse(pX);
2706 jsonDebugPrintNode(pResult);
2707 jsonReturnJson(pX, pResult, ctx, 0);
2708 }else{
2709 sqlite3_result_error_nomem(ctx);
2715 ** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
2716 ** object that contains all name/value given in arguments. Or if any name
2717 ** is not a string or if any value is a BLOB, throw an error.
2719 static void jsonObjectFunc(
2720 sqlite3_context *ctx,
2721 int argc,
2722 sqlite3_value **argv
2724 int i;
2725 JsonString jx;
2726 const char *z;
2727 u32 n;
2729 if( argc&1 ){
2730 sqlite3_result_error(ctx, "json_object() requires an even number "
2731 "of arguments", -1);
2732 return;
2734 jsonInit(&jx, ctx);
2735 jsonAppendChar(&jx, '{');
2736 for(i=0; i<argc; i+=2){
2737 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
2738 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
2739 jsonReset(&jx);
2740 return;
2742 jsonAppendSeparator(&jx);
2743 z = (const char*)sqlite3_value_text(argv[i]);
2744 n = (u32)sqlite3_value_bytes(argv[i]);
2745 jsonAppendString(&jx, z, n);
2746 jsonAppendChar(&jx, ':');
2747 jsonAppendValue(&jx, argv[i+1]);
2749 jsonAppendChar(&jx, '}');
2750 jsonResult(&jx);
2751 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
2756 ** json_remove(JSON, PATH, ...)
2758 ** Remove the named elements from JSON and return the result. malformed
2759 ** JSON or PATH arguments result in an error.
2761 static void jsonRemoveFunc(
2762 sqlite3_context *ctx,
2763 int argc,
2764 sqlite3_value **argv
2766 JsonParse *pParse; /* The parse */
2767 JsonNode *pNode;
2768 const char *zPath;
2769 u32 i;
2771 if( argc<1 ) return;
2772 pParse = jsonParseCached(ctx, argv[0], ctx, argc>1);
2773 if( pParse==0 ) return;
2774 for(i=1; i<(u32)argc; i++){
2775 zPath = (const char*)sqlite3_value_text(argv[i]);
2776 if( zPath==0 ) goto remove_done;
2777 pNode = jsonLookup(pParse, zPath, 0, ctx);
2778 if( pParse->nErr ) goto remove_done;
2779 if( pNode ){
2780 pNode->jnFlags |= JNODE_REMOVE;
2781 pParse->hasMod = 1;
2782 pParse->useMod = 1;
2785 if( (pParse->aNode[0].jnFlags & JNODE_REMOVE)==0 ){
2786 jsonReturnJson(pParse, pParse->aNode, ctx, 1);
2788 remove_done:
2789 jsonDebugPrintParse(p);
2793 ** Substitute the value at iNode with the pValue parameter.
2795 static void jsonReplaceNode(
2796 sqlite3_context *pCtx,
2797 JsonParse *p,
2798 int iNode,
2799 sqlite3_value *pValue
2801 int idx = jsonParseAddSubstNode(p, iNode);
2802 if( idx<=0 ){
2803 assert( p->oom );
2804 return;
2806 switch( sqlite3_value_type(pValue) ){
2807 case SQLITE_NULL: {
2808 jsonParseAddNode(p, JSON_NULL, 0, 0);
2809 break;
2811 case SQLITE_FLOAT: {
2812 char *z = sqlite3_mprintf("%!0.15g", sqlite3_value_double(pValue));
2813 int n;
2814 if( z==0 ){
2815 p->oom = 1;
2816 break;
2818 n = sqlite3Strlen30(z);
2819 jsonParseAddNode(p, JSON_REAL, n, z);
2820 jsonParseAddCleanup(p, sqlite3_free, z);
2821 break;
2823 case SQLITE_INTEGER: {
2824 char *z = sqlite3_mprintf("%lld", sqlite3_value_int64(pValue));
2825 int n;
2826 if( z==0 ){
2827 p->oom = 1;
2828 break;
2830 n = sqlite3Strlen30(z);
2831 jsonParseAddNode(p, JSON_INT, n, z);
2832 jsonParseAddCleanup(p, sqlite3_free, z);
2834 break;
2836 case SQLITE_TEXT: {
2837 const char *z = (const char*)sqlite3_value_text(pValue);
2838 u32 n = (u32)sqlite3_value_bytes(pValue);
2839 if( z==0 ){
2840 p->oom = 1;
2841 break;
2843 if( sqlite3_value_subtype(pValue)!=JSON_SUBTYPE ){
2844 char *zCopy = sqlite3DbStrDup(0, z);
2845 int k;
2846 if( zCopy ){
2847 jsonParseAddCleanup(p, sqlite3_free, zCopy);
2848 }else{
2849 p->oom = 1;
2850 sqlite3_result_error_nomem(pCtx);
2852 k = jsonParseAddNode(p, JSON_STRING, n, zCopy);
2853 assert( k>0 || p->oom );
2854 if( p->oom==0 ) p->aNode[k].jnFlags |= JNODE_RAW;
2855 }else{
2856 JsonParse *pPatch = jsonParseCached(pCtx, pValue, pCtx, 1);
2857 if( pPatch==0 ){
2858 p->oom = 1;
2859 break;
2861 jsonParseAddNodeArray(p, pPatch->aNode, pPatch->nNode);
2862 /* The nodes copied out of pPatch and into p likely contain
2863 ** u.zJContent pointers into pPatch->zJson. So preserve the
2864 ** content of pPatch until p is destroyed. */
2865 assert( pPatch->nJPRef>=1 );
2866 pPatch->nJPRef++;
2867 jsonParseAddCleanup(p, (void(*)(void*))jsonParseFree, pPatch);
2869 break;
2871 default: {
2872 jsonParseAddNode(p, JSON_NULL, 0, 0);
2873 sqlite3_result_error(pCtx, "JSON cannot hold BLOB values", -1);
2874 p->nErr++;
2875 break;
2881 ** json_replace(JSON, PATH, VALUE, ...)
2883 ** Replace the value at PATH with VALUE. If PATH does not already exist,
2884 ** this routine is a no-op. If JSON or PATH is malformed, throw an error.
2886 static void jsonReplaceFunc(
2887 sqlite3_context *ctx,
2888 int argc,
2889 sqlite3_value **argv
2891 JsonParse *pParse; /* The parse */
2892 JsonNode *pNode;
2893 const char *zPath;
2894 u32 i;
2896 if( argc<1 ) return;
2897 if( (argc&1)==0 ) {
2898 jsonWrongNumArgs(ctx, "replace");
2899 return;
2901 pParse = jsonParseCached(ctx, argv[0], ctx, argc>1);
2902 if( pParse==0 ) return;
2903 for(i=1; i<(u32)argc; i+=2){
2904 zPath = (const char*)sqlite3_value_text(argv[i]);
2905 pParse->useMod = 1;
2906 pNode = jsonLookup(pParse, zPath, 0, ctx);
2907 if( pParse->nErr ) goto replace_err;
2908 if( pNode ){
2909 jsonReplaceNode(ctx, pParse, (u32)(pNode - pParse->aNode), argv[i+1]);
2912 jsonReturnJson(pParse, pParse->aNode, ctx, 1);
2913 replace_err:
2914 jsonDebugPrintParse(pParse);
2919 ** json_set(JSON, PATH, VALUE, ...)
2921 ** Set the value at PATH to VALUE. Create the PATH if it does not already
2922 ** exist. Overwrite existing values that do exist.
2923 ** If JSON or PATH is malformed, throw an error.
2925 ** json_insert(JSON, PATH, VALUE, ...)
2927 ** Create PATH and initialize it to VALUE. If PATH already exists, this
2928 ** routine is a no-op. If JSON or PATH is malformed, throw an error.
2930 static void jsonSetFunc(
2931 sqlite3_context *ctx,
2932 int argc,
2933 sqlite3_value **argv
2935 JsonParse *pParse; /* The parse */
2936 JsonNode *pNode;
2937 const char *zPath;
2938 u32 i;
2939 int bApnd;
2940 int bIsSet = sqlite3_user_data(ctx)!=0;
2942 if( argc<1 ) return;
2943 if( (argc&1)==0 ) {
2944 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
2945 return;
2947 pParse = jsonParseCached(ctx, argv[0], ctx, argc>1);
2948 if( pParse==0 ) return;
2949 for(i=1; i<(u32)argc; i+=2){
2950 zPath = (const char*)sqlite3_value_text(argv[i]);
2951 bApnd = 0;
2952 pParse->useMod = 1;
2953 pNode = jsonLookup(pParse, zPath, &bApnd, ctx);
2954 if( pParse->oom ){
2955 sqlite3_result_error_nomem(ctx);
2956 goto jsonSetDone;
2957 }else if( pParse->nErr ){
2958 goto jsonSetDone;
2959 }else if( pNode && (bApnd || bIsSet) ){
2960 jsonReplaceNode(ctx, pParse, (u32)(pNode - pParse->aNode), argv[i+1]);
2963 jsonDebugPrintParse(pParse);
2964 jsonReturnJson(pParse, pParse->aNode, ctx, 1);
2966 jsonSetDone:
2967 /* no cleanup required */;
2971 ** json_type(JSON)
2972 ** json_type(JSON, PATH)
2974 ** Return the top-level "type" of a JSON string. json_type() raises an
2975 ** error if either the JSON or PATH inputs are not well-formed.
2977 static void jsonTypeFunc(
2978 sqlite3_context *ctx,
2979 int argc,
2980 sqlite3_value **argv
2982 JsonParse *p; /* The parse */
2983 const char *zPath;
2984 JsonNode *pNode;
2986 p = jsonParseCached(ctx, argv[0], ctx, 0);
2987 if( p==0 ) return;
2988 if( argc==2 ){
2989 zPath = (const char*)sqlite3_value_text(argv[1]);
2990 pNode = jsonLookup(p, zPath, 0, ctx);
2991 }else{
2992 pNode = p->aNode;
2994 if( pNode ){
2995 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
3000 ** json_valid(JSON)
3002 ** Return 1 if JSON is a well-formed canonical JSON string according
3003 ** to RFC-7159. Return 0 otherwise.
3005 static void jsonValidFunc(
3006 sqlite3_context *ctx,
3007 int argc,
3008 sqlite3_value **argv
3010 JsonParse *p; /* The parse */
3011 UNUSED_PARAMETER(argc);
3012 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
3013 #ifdef SQLITE_LEGACY_JSON_VALID
3014 /* Incorrect legacy behavior was to return FALSE for a NULL input */
3015 sqlite3_result_int(ctx, 0);
3016 #endif
3017 return;
3019 p = jsonParseCached(ctx, argv[0], 0, 0);
3020 if( p==0 || p->oom ){
3021 sqlite3_result_error_nomem(ctx);
3022 sqlite3_free(p);
3023 }else{
3024 sqlite3_result_int(ctx, p->nErr==0 && (p->hasNonstd==0 || p->useMod));
3025 if( p->nErr ) jsonParseFree(p);
3030 ** json_error_position(JSON)
3032 ** If the argument is not an interpretable JSON string, then return the 1-based
3033 ** character position at which the parser first recognized that the input
3034 ** was in error. The left-most character is 1. If the string is valid
3035 ** JSON, then return 0.
3037 ** Note that json_valid() is only true for strictly conforming canonical JSON.
3038 ** But this routine returns zero if the input contains extension. Thus:
3040 ** (1) If the input X is strictly conforming canonical JSON:
3042 ** json_valid(X) returns true
3043 ** json_error_position(X) returns 0
3045 ** (2) If the input X is JSON but it includes extension (such as JSON5) that
3046 ** are not part of RFC-8259:
3048 ** json_valid(X) returns false
3049 ** json_error_position(X) return 0
3051 ** (3) If the input X cannot be interpreted as JSON even taking extensions
3052 ** into account:
3054 ** json_valid(X) return false
3055 ** json_error_position(X) returns 1 or more
3057 static void jsonErrorFunc(
3058 sqlite3_context *ctx,
3059 int argc,
3060 sqlite3_value **argv
3062 JsonParse *p; /* The parse */
3063 UNUSED_PARAMETER(argc);
3064 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
3065 p = jsonParseCached(ctx, argv[0], 0, 0);
3066 if( p==0 || p->oom ){
3067 sqlite3_result_error_nomem(ctx);
3068 sqlite3_free(p);
3069 }else if( p->nErr==0 ){
3070 sqlite3_result_int(ctx, 0);
3071 }else{
3072 int n = 1;
3073 u32 i;
3074 const char *z = (const char*)sqlite3_value_text(argv[0]);
3075 for(i=0; i<p->iErr && ALWAYS(z[i]); i++){
3076 if( (z[i]&0xc0)!=0x80 ) n++;
3078 sqlite3_result_int(ctx, n);
3079 jsonParseFree(p);
3084 /****************************************************************************
3085 ** Aggregate SQL function implementations
3086 ****************************************************************************/
3088 ** json_group_array(VALUE)
3090 ** Return a JSON array composed of all values in the aggregate.
3092 static void jsonArrayStep(
3093 sqlite3_context *ctx,
3094 int argc,
3095 sqlite3_value **argv
3097 JsonString *pStr;
3098 UNUSED_PARAMETER(argc);
3099 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
3100 if( pStr ){
3101 if( pStr->zBuf==0 ){
3102 jsonInit(pStr, ctx);
3103 jsonAppendChar(pStr, '[');
3104 }else if( pStr->nUsed>1 ){
3105 jsonAppendChar(pStr, ',');
3107 pStr->pCtx = ctx;
3108 jsonAppendValue(pStr, argv[0]);
3111 static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
3112 JsonString *pStr;
3113 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
3114 if( pStr ){
3115 pStr->pCtx = ctx;
3116 jsonAppendChar(pStr, ']');
3117 if( pStr->bErr ){
3118 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
3119 assert( pStr->bStatic );
3120 }else if( isFinal ){
3121 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
3122 pStr->bStatic ? SQLITE_TRANSIENT :
3123 (void(*)(void*))sqlite3RCStrUnref);
3124 pStr->bStatic = 1;
3125 }else{
3126 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
3127 pStr->nUsed--;
3129 }else{
3130 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
3132 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3134 static void jsonArrayValue(sqlite3_context *ctx){
3135 jsonArrayCompute(ctx, 0);
3137 static void jsonArrayFinal(sqlite3_context *ctx){
3138 jsonArrayCompute(ctx, 1);
3141 #ifndef SQLITE_OMIT_WINDOWFUNC
3143 ** This method works for both json_group_array() and json_group_object().
3144 ** It works by removing the first element of the group by searching forward
3145 ** to the first comma (",") that is not within a string and deleting all
3146 ** text through that comma.
3148 static void jsonGroupInverse(
3149 sqlite3_context *ctx,
3150 int argc,
3151 sqlite3_value **argv
3153 unsigned int i;
3154 int inStr = 0;
3155 int nNest = 0;
3156 char *z;
3157 char c;
3158 JsonString *pStr;
3159 UNUSED_PARAMETER(argc);
3160 UNUSED_PARAMETER(argv);
3161 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
3162 #ifdef NEVER
3163 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
3164 ** always have been called to initialize it */
3165 if( NEVER(!pStr) ) return;
3166 #endif
3167 z = pStr->zBuf;
3168 for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){
3169 if( c=='"' ){
3170 inStr = !inStr;
3171 }else if( c=='\\' ){
3172 i++;
3173 }else if( !inStr ){
3174 if( c=='{' || c=='[' ) nNest++;
3175 if( c=='}' || c==']' ) nNest--;
3178 if( i<pStr->nUsed ){
3179 pStr->nUsed -= i;
3180 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
3181 z[pStr->nUsed] = 0;
3182 }else{
3183 pStr->nUsed = 1;
3186 #else
3187 # define jsonGroupInverse 0
3188 #endif
3192 ** json_group_obj(NAME,VALUE)
3194 ** Return a JSON object composed of all names and values in the aggregate.
3196 static void jsonObjectStep(
3197 sqlite3_context *ctx,
3198 int argc,
3199 sqlite3_value **argv
3201 JsonString *pStr;
3202 const char *z;
3203 u32 n;
3204 UNUSED_PARAMETER(argc);
3205 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
3206 if( pStr ){
3207 if( pStr->zBuf==0 ){
3208 jsonInit(pStr, ctx);
3209 jsonAppendChar(pStr, '{');
3210 }else if( pStr->nUsed>1 ){
3211 jsonAppendChar(pStr, ',');
3213 pStr->pCtx = ctx;
3214 z = (const char*)sqlite3_value_text(argv[0]);
3215 n = (u32)sqlite3_value_bytes(argv[0]);
3216 jsonAppendString(pStr, z, n);
3217 jsonAppendChar(pStr, ':');
3218 jsonAppendValue(pStr, argv[1]);
3221 static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
3222 JsonString *pStr;
3223 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
3224 if( pStr ){
3225 jsonAppendChar(pStr, '}');
3226 if( pStr->bErr ){
3227 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
3228 assert( pStr->bStatic );
3229 }else if( isFinal ){
3230 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
3231 pStr->bStatic ? SQLITE_TRANSIENT :
3232 (void(*)(void*))sqlite3RCStrUnref);
3233 pStr->bStatic = 1;
3234 }else{
3235 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
3236 pStr->nUsed--;
3238 }else{
3239 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
3241 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3243 static void jsonObjectValue(sqlite3_context *ctx){
3244 jsonObjectCompute(ctx, 0);
3246 static void jsonObjectFinal(sqlite3_context *ctx){
3247 jsonObjectCompute(ctx, 1);
3252 #ifndef SQLITE_OMIT_VIRTUALTABLE
3253 /****************************************************************************
3254 ** The json_each virtual table
3255 ****************************************************************************/
3256 typedef struct JsonEachCursor JsonEachCursor;
3257 struct JsonEachCursor {
3258 sqlite3_vtab_cursor base; /* Base class - must be first */
3259 u32 iRowid; /* The rowid */
3260 u32 iBegin; /* The first node of the scan */
3261 u32 i; /* Index in sParse.aNode[] of current row */
3262 u32 iEnd; /* EOF when i equals or exceeds this value */
3263 u8 eType; /* Type of top-level element */
3264 u8 bRecursive; /* True for json_tree(). False for json_each() */
3265 char *zJson; /* Input JSON */
3266 char *zRoot; /* Path by which to filter zJson */
3267 JsonParse sParse; /* Parse of the input JSON */
3270 /* Constructor for the json_each virtual table */
3271 static int jsonEachConnect(
3272 sqlite3 *db,
3273 void *pAux,
3274 int argc, const char *const*argv,
3275 sqlite3_vtab **ppVtab,
3276 char **pzErr
3278 sqlite3_vtab *pNew;
3279 int rc;
3281 /* Column numbers */
3282 #define JEACH_KEY 0
3283 #define JEACH_VALUE 1
3284 #define JEACH_TYPE 2
3285 #define JEACH_ATOM 3
3286 #define JEACH_ID 4
3287 #define JEACH_PARENT 5
3288 #define JEACH_FULLKEY 6
3289 #define JEACH_PATH 7
3290 /* The xBestIndex method assumes that the JSON and ROOT columns are
3291 ** the last two columns in the table. Should this ever changes, be
3292 ** sure to update the xBestIndex method. */
3293 #define JEACH_JSON 8
3294 #define JEACH_ROOT 9
3296 UNUSED_PARAMETER(pzErr);
3297 UNUSED_PARAMETER(argv);
3298 UNUSED_PARAMETER(argc);
3299 UNUSED_PARAMETER(pAux);
3300 rc = sqlite3_declare_vtab(db,
3301 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
3302 "json HIDDEN,root HIDDEN)");
3303 if( rc==SQLITE_OK ){
3304 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
3305 if( pNew==0 ) return SQLITE_NOMEM;
3306 memset(pNew, 0, sizeof(*pNew));
3307 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3309 return rc;
3312 /* destructor for json_each virtual table */
3313 static int jsonEachDisconnect(sqlite3_vtab *pVtab){
3314 sqlite3_free(pVtab);
3315 return SQLITE_OK;
3318 /* constructor for a JsonEachCursor object for json_each(). */
3319 static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3320 JsonEachCursor *pCur;
3322 UNUSED_PARAMETER(p);
3323 pCur = sqlite3_malloc( sizeof(*pCur) );
3324 if( pCur==0 ) return SQLITE_NOMEM;
3325 memset(pCur, 0, sizeof(*pCur));
3326 *ppCursor = &pCur->base;
3327 return SQLITE_OK;
3330 /* constructor for a JsonEachCursor object for json_tree(). */
3331 static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3332 int rc = jsonEachOpenEach(p, ppCursor);
3333 if( rc==SQLITE_OK ){
3334 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
3335 pCur->bRecursive = 1;
3337 return rc;
3340 /* Reset a JsonEachCursor back to its original state. Free any memory
3341 ** held. */
3342 static void jsonEachCursorReset(JsonEachCursor *p){
3343 sqlite3_free(p->zRoot);
3344 jsonParseReset(&p->sParse);
3345 p->iRowid = 0;
3346 p->i = 0;
3347 p->iEnd = 0;
3348 p->eType = 0;
3349 p->zJson = 0;
3350 p->zRoot = 0;
3353 /* Destructor for a jsonEachCursor object */
3354 static int jsonEachClose(sqlite3_vtab_cursor *cur){
3355 JsonEachCursor *p = (JsonEachCursor*)cur;
3356 jsonEachCursorReset(p);
3357 sqlite3_free(cur);
3358 return SQLITE_OK;
3361 /* Return TRUE if the jsonEachCursor object has been advanced off the end
3362 ** of the JSON object */
3363 static int jsonEachEof(sqlite3_vtab_cursor *cur){
3364 JsonEachCursor *p = (JsonEachCursor*)cur;
3365 return p->i >= p->iEnd;
3368 /* Advance the cursor to the next element for json_tree() */
3369 static int jsonEachNext(sqlite3_vtab_cursor *cur){
3370 JsonEachCursor *p = (JsonEachCursor*)cur;
3371 if( p->bRecursive ){
3372 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
3373 p->i++;
3374 p->iRowid++;
3375 if( p->i<p->iEnd ){
3376 u32 iUp = p->sParse.aUp[p->i];
3377 JsonNode *pUp = &p->sParse.aNode[iUp];
3378 p->eType = pUp->eType;
3379 if( pUp->eType==JSON_ARRAY ){
3380 assert( pUp->eU==0 || pUp->eU==3 );
3381 testcase( pUp->eU==3 );
3382 VVA( pUp->eU = 3 );
3383 if( iUp==p->i-1 ){
3384 pUp->u.iKey = 0;
3385 }else{
3386 pUp->u.iKey++;
3390 }else{
3391 switch( p->eType ){
3392 case JSON_ARRAY: {
3393 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
3394 p->iRowid++;
3395 break;
3397 case JSON_OBJECT: {
3398 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
3399 p->iRowid++;
3400 break;
3402 default: {
3403 p->i = p->iEnd;
3404 break;
3408 return SQLITE_OK;
3411 /* Append an object label to the JSON Path being constructed
3412 ** in pStr.
3414 static void jsonAppendObjectPathElement(
3415 JsonString *pStr,
3416 JsonNode *pNode
3418 int jj, nn;
3419 const char *z;
3420 assert( pNode->eType==JSON_STRING );
3421 assert( pNode->jnFlags & JNODE_LABEL );
3422 assert( pNode->eU==1 );
3423 z = pNode->u.zJContent;
3424 nn = pNode->n;
3425 if( (pNode->jnFlags & JNODE_RAW)==0 ){
3426 assert( nn>=2 );
3427 assert( z[0]=='"' || z[0]=='\'' );
3428 assert( z[nn-1]=='"' || z[0]=='\'' );
3429 if( nn>2 && sqlite3Isalpha(z[1]) ){
3430 for(jj=2; jj<nn-1 && sqlite3Isalnum(z[jj]); jj++){}
3431 if( jj==nn-1 ){
3432 z++;
3433 nn -= 2;
3437 jsonPrintf(nn+2, pStr, ".%.*s", nn, z);
3440 /* Append the name of the path for element i to pStr
3442 static void jsonEachComputePath(
3443 JsonEachCursor *p, /* The cursor */
3444 JsonString *pStr, /* Write the path here */
3445 u32 i /* Path to this element */
3447 JsonNode *pNode, *pUp;
3448 u32 iUp;
3449 if( i==0 ){
3450 jsonAppendChar(pStr, '$');
3451 return;
3453 iUp = p->sParse.aUp[i];
3454 jsonEachComputePath(p, pStr, iUp);
3455 pNode = &p->sParse.aNode[i];
3456 pUp = &p->sParse.aNode[iUp];
3457 if( pUp->eType==JSON_ARRAY ){
3458 assert( pUp->eU==3 || (pUp->eU==0 && pUp->u.iKey==0) );
3459 testcase( pUp->eU==0 );
3460 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
3461 }else{
3462 assert( pUp->eType==JSON_OBJECT );
3463 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
3464 jsonAppendObjectPathElement(pStr, pNode);
3468 /* Return the value of a column */
3469 static int jsonEachColumn(
3470 sqlite3_vtab_cursor *cur, /* The cursor */
3471 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
3472 int i /* Which column to return */
3474 JsonEachCursor *p = (JsonEachCursor*)cur;
3475 JsonNode *pThis = &p->sParse.aNode[p->i];
3476 switch( i ){
3477 case JEACH_KEY: {
3478 if( p->i==0 ) break;
3479 if( p->eType==JSON_OBJECT ){
3480 jsonReturn(&p->sParse, pThis, ctx);
3481 }else if( p->eType==JSON_ARRAY ){
3482 u32 iKey;
3483 if( p->bRecursive ){
3484 if( p->iRowid==0 ) break;
3485 assert( p->sParse.aNode[p->sParse.aUp[p->i]].eU==3 );
3486 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
3487 }else{
3488 iKey = p->iRowid;
3490 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
3492 break;
3494 case JEACH_VALUE: {
3495 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
3496 jsonReturn(&p->sParse, pThis, ctx);
3497 break;
3499 case JEACH_TYPE: {
3500 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
3501 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
3502 break;
3504 case JEACH_ATOM: {
3505 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
3506 if( pThis->eType>=JSON_ARRAY ) break;
3507 jsonReturn(&p->sParse, pThis, ctx);
3508 break;
3510 case JEACH_ID: {
3511 sqlite3_result_int64(ctx,
3512 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
3513 break;
3515 case JEACH_PARENT: {
3516 if( p->i>p->iBegin && p->bRecursive ){
3517 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
3519 break;
3521 case JEACH_FULLKEY: {
3522 JsonString x;
3523 jsonInit(&x, ctx);
3524 if( p->bRecursive ){
3525 jsonEachComputePath(p, &x, p->i);
3526 }else{
3527 if( p->zRoot ){
3528 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
3529 }else{
3530 jsonAppendChar(&x, '$');
3532 if( p->eType==JSON_ARRAY ){
3533 jsonPrintf(30, &x, "[%d]", p->iRowid);
3534 }else if( p->eType==JSON_OBJECT ){
3535 jsonAppendObjectPathElement(&x, pThis);
3538 jsonResult(&x);
3539 break;
3541 case JEACH_PATH: {
3542 if( p->bRecursive ){
3543 JsonString x;
3544 jsonInit(&x, ctx);
3545 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
3546 jsonResult(&x);
3547 break;
3549 /* For json_each() path and root are the same so fall through
3550 ** into the root case */
3551 /* no break */ deliberate_fall_through
3553 default: {
3554 const char *zRoot = p->zRoot;
3555 if( zRoot==0 ) zRoot = "$";
3556 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
3557 break;
3559 case JEACH_JSON: {
3560 assert( i==JEACH_JSON );
3561 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
3562 break;
3565 return SQLITE_OK;
3568 /* Return the current rowid value */
3569 static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3570 JsonEachCursor *p = (JsonEachCursor*)cur;
3571 *pRowid = p->iRowid;
3572 return SQLITE_OK;
3575 /* The query strategy is to look for an equality constraint on the json
3576 ** column. Without such a constraint, the table cannot operate. idxNum is
3577 ** 1 if the constraint is found, 3 if the constraint and zRoot are found,
3578 ** and 0 otherwise.
3580 static int jsonEachBestIndex(
3581 sqlite3_vtab *tab,
3582 sqlite3_index_info *pIdxInfo
3584 int i; /* Loop counter or computed array index */
3585 int aIdx[2]; /* Index of constraints for JSON and ROOT */
3586 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
3587 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
3588 const struct sqlite3_index_constraint *pConstraint;
3590 /* This implementation assumes that JSON and ROOT are the last two
3591 ** columns in the table */
3592 assert( JEACH_ROOT == JEACH_JSON+1 );
3593 UNUSED_PARAMETER(tab);
3594 aIdx[0] = aIdx[1] = -1;
3595 pConstraint = pIdxInfo->aConstraint;
3596 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3597 int iCol;
3598 int iMask;
3599 if( pConstraint->iColumn < JEACH_JSON ) continue;
3600 iCol = pConstraint->iColumn - JEACH_JSON;
3601 assert( iCol==0 || iCol==1 );
3602 testcase( iCol==0 );
3603 iMask = 1 << iCol;
3604 if( pConstraint->usable==0 ){
3605 unusableMask |= iMask;
3606 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
3607 aIdx[iCol] = i;
3608 idxMask |= iMask;
3611 if( pIdxInfo->nOrderBy>0
3612 && pIdxInfo->aOrderBy[0].iColumn<0
3613 && pIdxInfo->aOrderBy[0].desc==0
3615 pIdxInfo->orderByConsumed = 1;
3618 if( (unusableMask & ~idxMask)!=0 ){
3619 /* If there are any unusable constraints on JSON or ROOT, then reject
3620 ** this entire plan */
3621 return SQLITE_CONSTRAINT;
3623 if( aIdx[0]<0 ){
3624 /* No JSON input. Leave estimatedCost at the huge value that it was
3625 ** initialized to to discourage the query planner from selecting this
3626 ** plan. */
3627 pIdxInfo->idxNum = 0;
3628 }else{
3629 pIdxInfo->estimatedCost = 1.0;
3630 i = aIdx[0];
3631 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
3632 pIdxInfo->aConstraintUsage[i].omit = 1;
3633 if( aIdx[1]<0 ){
3634 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
3635 }else{
3636 i = aIdx[1];
3637 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
3638 pIdxInfo->aConstraintUsage[i].omit = 1;
3639 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
3642 return SQLITE_OK;
3645 /* Start a search on a new JSON string */
3646 static int jsonEachFilter(
3647 sqlite3_vtab_cursor *cur,
3648 int idxNum, const char *idxStr,
3649 int argc, sqlite3_value **argv
3651 JsonEachCursor *p = (JsonEachCursor*)cur;
3652 const char *z;
3653 const char *zRoot = 0;
3654 sqlite3_int64 n;
3656 UNUSED_PARAMETER(idxStr);
3657 UNUSED_PARAMETER(argc);
3658 jsonEachCursorReset(p);
3659 if( idxNum==0 ) return SQLITE_OK;
3660 z = (const char*)sqlite3_value_text(argv[0]);
3661 if( z==0 ) return SQLITE_OK;
3662 memset(&p->sParse, 0, sizeof(p->sParse));
3663 p->sParse.nJPRef = 1;
3664 if( sqlite3ValueIsOfClass(argv[0], (void(*)(void*))sqlite3RCStrUnref) ){
3665 p->sParse.zJson = sqlite3RCStrRef((char*)z);
3666 }else{
3667 n = sqlite3_value_bytes(argv[0]);
3668 p->sParse.zJson = sqlite3RCStrNew( n+1 );
3669 if( p->sParse.zJson==0 ) return SQLITE_NOMEM;
3670 memcpy(p->sParse.zJson, z, (size_t)n+1);
3672 p->sParse.bJsonIsRCStr = 1;
3673 p->zJson = p->sParse.zJson;
3674 if( jsonParse(&p->sParse, 0) ){
3675 int rc = SQLITE_NOMEM;
3676 if( p->sParse.oom==0 ){
3677 sqlite3_free(cur->pVtab->zErrMsg);
3678 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
3679 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
3681 jsonEachCursorReset(p);
3682 return rc;
3683 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
3684 jsonEachCursorReset(p);
3685 return SQLITE_NOMEM;
3686 }else{
3687 JsonNode *pNode = 0;
3688 if( idxNum==3 ){
3689 const char *zErr = 0;
3690 zRoot = (const char*)sqlite3_value_text(argv[1]);
3691 if( zRoot==0 ) return SQLITE_OK;
3692 n = sqlite3_value_bytes(argv[1]);
3693 p->zRoot = sqlite3_malloc64( n+1 );
3694 if( p->zRoot==0 ) return SQLITE_NOMEM;
3695 memcpy(p->zRoot, zRoot, (size_t)n+1);
3696 if( zRoot[0]!='$' ){
3697 zErr = zRoot;
3698 }else{
3699 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
3701 if( zErr ){
3702 sqlite3_free(cur->pVtab->zErrMsg);
3703 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
3704 jsonEachCursorReset(p);
3705 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
3706 }else if( pNode==0 ){
3707 return SQLITE_OK;
3709 }else{
3710 pNode = p->sParse.aNode;
3712 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
3713 p->eType = pNode->eType;
3714 if( p->eType>=JSON_ARRAY ){
3715 assert( pNode->eU==0 );
3716 VVA( pNode->eU = 3 );
3717 pNode->u.iKey = 0;
3718 p->iEnd = p->i + pNode->n + 1;
3719 if( p->bRecursive ){
3720 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
3721 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
3722 p->i--;
3724 }else{
3725 p->i++;
3727 }else{
3728 p->iEnd = p->i+1;
3731 return SQLITE_OK;
3734 /* The methods of the json_each virtual table */
3735 static sqlite3_module jsonEachModule = {
3736 0, /* iVersion */
3737 0, /* xCreate */
3738 jsonEachConnect, /* xConnect */
3739 jsonEachBestIndex, /* xBestIndex */
3740 jsonEachDisconnect, /* xDisconnect */
3741 0, /* xDestroy */
3742 jsonEachOpenEach, /* xOpen - open a cursor */
3743 jsonEachClose, /* xClose - close a cursor */
3744 jsonEachFilter, /* xFilter - configure scan constraints */
3745 jsonEachNext, /* xNext - advance a cursor */
3746 jsonEachEof, /* xEof - check for end of scan */
3747 jsonEachColumn, /* xColumn - read data */
3748 jsonEachRowid, /* xRowid - read data */
3749 0, /* xUpdate */
3750 0, /* xBegin */
3751 0, /* xSync */
3752 0, /* xCommit */
3753 0, /* xRollback */
3754 0, /* xFindMethod */
3755 0, /* xRename */
3756 0, /* xSavepoint */
3757 0, /* xRelease */
3758 0, /* xRollbackTo */
3759 0 /* xShadowName */
3762 /* The methods of the json_tree virtual table. */
3763 static sqlite3_module jsonTreeModule = {
3764 0, /* iVersion */
3765 0, /* xCreate */
3766 jsonEachConnect, /* xConnect */
3767 jsonEachBestIndex, /* xBestIndex */
3768 jsonEachDisconnect, /* xDisconnect */
3769 0, /* xDestroy */
3770 jsonEachOpenTree, /* xOpen - open a cursor */
3771 jsonEachClose, /* xClose - close a cursor */
3772 jsonEachFilter, /* xFilter - configure scan constraints */
3773 jsonEachNext, /* xNext - advance a cursor */
3774 jsonEachEof, /* xEof - check for end of scan */
3775 jsonEachColumn, /* xColumn - read data */
3776 jsonEachRowid, /* xRowid - read data */
3777 0, /* xUpdate */
3778 0, /* xBegin */
3779 0, /* xSync */
3780 0, /* xCommit */
3781 0, /* xRollback */
3782 0, /* xFindMethod */
3783 0, /* xRename */
3784 0, /* xSavepoint */
3785 0, /* xRelease */
3786 0, /* xRollbackTo */
3787 0 /* xShadowName */
3789 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3790 #endif /* !defined(SQLITE_OMIT_JSON) */
3793 ** Register JSON functions.
3795 void sqlite3RegisterJsonFunctions(void){
3796 #ifndef SQLITE_OMIT_JSON
3797 static FuncDef aJsonFunc[] = {
3798 JFUNCTION(json, 1, 0, jsonRemoveFunc),
3799 JFUNCTION(json_array, -1, 0, jsonArrayFunc),
3800 JFUNCTION(json_array_length, 1, 0, jsonArrayLengthFunc),
3801 JFUNCTION(json_array_length, 2, 0, jsonArrayLengthFunc),
3802 JFUNCTION(json_error_position,1, 0, jsonErrorFunc),
3803 JFUNCTION(json_extract, -1, 0, jsonExtractFunc),
3804 JFUNCTION(->, 2, JSON_JSON, jsonExtractFunc),
3805 JFUNCTION(->>, 2, JSON_SQL, jsonExtractFunc),
3806 JFUNCTION(json_insert, -1, 0, jsonSetFunc),
3807 JFUNCTION(json_object, -1, 0, jsonObjectFunc),
3808 JFUNCTION(json_patch, 2, 0, jsonPatchFunc),
3809 JFUNCTION(json_quote, 1, 0, jsonQuoteFunc),
3810 JFUNCTION(json_remove, -1, 0, jsonRemoveFunc),
3811 JFUNCTION(json_replace, -1, 0, jsonReplaceFunc),
3812 JFUNCTION(json_set, -1, JSON_ISSET, jsonSetFunc),
3813 JFUNCTION(json_type, 1, 0, jsonTypeFunc),
3814 JFUNCTION(json_type, 2, 0, jsonTypeFunc),
3815 JFUNCTION(json_valid, 1, 0, jsonValidFunc),
3816 #if SQLITE_DEBUG
3817 JFUNCTION(json_parse, 1, 0, jsonParseFunc),
3818 JFUNCTION(json_test1, 1, 0, jsonTest1Func),
3819 #endif
3820 WAGGREGATE(json_group_array, 1, 0, 0,
3821 jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,
3822 SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC),
3823 WAGGREGATE(json_group_object, 2, 0, 0,
3824 jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,
3825 SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC)
3827 sqlite3InsertBuiltinFuncs(aJsonFunc, ArraySize(aJsonFunc));
3828 #endif
3831 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
3833 ** Register the JSON table-valued functions
3835 int sqlite3JsonTableFunctions(sqlite3 *db){
3836 int rc = SQLITE_OK;
3837 static const struct {
3838 const char *zName;
3839 sqlite3_module *pModule;
3840 } aMod[] = {
3841 { "json_each", &jsonEachModule },
3842 { "json_tree", &jsonTreeModule },
3844 unsigned int i;
3845 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
3846 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
3848 return rc;
3850 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) */