4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 ******************************************************************************
13 ** This SQLite JSON functions.
15 ** This file began as an extension in ext/misc/json1.c in 2015. That
16 ** extension proved so useful that it has now been moved into the core.
18 ** For the time being, all JSON is stored as pure text. (We might add
19 ** a JSONB type in the future which stores a binary encoding of JSON in
20 ** a BLOB, but there is no support for JSONB in the current implementation.
21 ** This implementation parses JSON text at 250 MB/s, so it is hard to see
22 ** how JSONB might improve on that.)
24 #ifndef SQLITE_OMIT_JSON
25 #include "sqliteInt.h"
28 ** Growing our own isspace() routine this way is twice as fast as
29 ** the library isspace() function, resulting in a 7% overall performance
30 ** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
32 static const char jsonIsSpace
[] = {
33 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
35 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
38 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
39 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
40 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
41 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50 #define fast_isspace(x) (jsonIsSpace[(unsigned char)x])
52 #if !defined(SQLITE_DEBUG) && !defined(SQLITE_COVERAGE_TEST)
59 typedef struct JsonString JsonString
;
60 typedef struct JsonNode JsonNode
;
61 typedef struct JsonParse JsonParse
;
63 /* An instance of this object represents a JSON string
64 ** under construction. Really, this is a generic string accumulator
65 ** that can be and is used to create strings other than JSON.
68 sqlite3_context
*pCtx
; /* Function context - put error messages here */
69 char *zBuf
; /* Append JSON content here */
70 u64 nAlloc
; /* Bytes of storage available in zBuf[] */
71 u64 nUsed
; /* Bytes of zBuf[] currently used */
72 u8 bStatic
; /* True if zBuf is static space */
73 u8 bErr
; /* True if an error has been encountered */
74 char zSpace
[100]; /* Initial static space */
88 /* The "subtype" set for JSON values */
89 #define JSON_SUBTYPE 74 /* Ascii for "J" */
92 ** Names of the various JSON types:
94 static const char * const jsonType
[] = {
95 "null", "true", "false", "integer", "real", "text", "array", "object"
98 /* Bit values for the JsonNode.jnFlag field
100 #define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
101 #define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
102 #define JNODE_REMOVE 0x04 /* Do not output */
103 #define JNODE_REPLACE 0x08 /* Replace with JsonNode.u.iReplace */
104 #define JNODE_PATCH 0x10 /* Patch with JsonNode.u.pPatch */
105 #define JNODE_APPEND 0x20 /* More ARRAY/OBJECT entries at u.iAppend */
106 #define JNODE_LABEL 0x40 /* Is a label of an object */
107 #define JNODE_JSON5 0x80 /* Node contains JSON5 enhancements */
110 /* A single node of parsed JSON
113 u8 eType
; /* One of the JSON_ type values */
114 u8 jnFlags
; /* JNODE flags */
115 u8 eU
; /* Which union element to use */
116 u32 n
; /* Bytes of content, or number of sub-nodes */
118 const char *zJContent
; /* 1: Content for INT, REAL, and STRING */
119 u32 iAppend
; /* 2: More terms for ARRAY and OBJECT */
120 u32 iKey
; /* 3: Key for ARRAY objects in json_tree() */
121 u32 iReplace
; /* 4: Replacement content for JNODE_REPLACE */
122 JsonNode
*pPatch
; /* 5: Node chain of patch for JNODE_PATCH */
126 /* A completely parsed JSON string
129 u32 nNode
; /* Number of slots of aNode[] used */
130 u32 nAlloc
; /* Number of slots of aNode[] allocated */
131 JsonNode
*aNode
; /* Array of nodes containing the parse */
132 const char *zJson
; /* Original JSON string */
133 u32
*aUp
; /* Index of parent of each node */
134 u16 iDepth
; /* Nesting depth */
135 u8 nErr
; /* Number of errors seen */
136 u8 oom
; /* Set to true if out of memory */
137 u8 hasNonstd
; /* True if input uses non-standard features like JSON5 */
138 int nJson
; /* Length of the zJson string in bytes */
139 u32 iErr
; /* Error location in zJson[] */
140 u32 iHold
; /* Replace cache line with the lowest iHold value */
144 ** Maximum nesting depth of JSON for this implementation.
146 ** This limit is needed to avoid a stack overflow in the recursive
147 ** descent parser. A depth of 1000 is far deeper than any sane JSON
148 ** should go. Historical note: This limit was 2000 prior to version 3.42.0
150 #define JSON_MAX_DEPTH 1000
152 /**************************************************************************
153 ** Utility routines for dealing with JsonString objects
154 **************************************************************************/
156 /* Set the JsonString object to an empty string
158 static void jsonZero(JsonString
*p
){
160 p
->nAlloc
= sizeof(p
->zSpace
);
165 /* Initialize the JsonString object
167 static void jsonInit(JsonString
*p
, sqlite3_context
*pCtx
){
174 /* Free all allocated memory and reset the JsonString object back to its
177 static void jsonReset(JsonString
*p
){
178 if( !p
->bStatic
) sqlite3_free(p
->zBuf
);
183 /* Report an out-of-memory (OOM) condition
185 static void jsonOom(JsonString
*p
){
187 sqlite3_result_error_nomem(p
->pCtx
);
191 /* Enlarge pJson->zBuf so that it can hold at least N more bytes.
192 ** Return zero on success. Return non-zero on an OOM error
194 static int jsonGrow(JsonString
*p
, u32 N
){
195 u64 nTotal
= N
<p
->nAlloc
? p
->nAlloc
*2 : p
->nAlloc
+N
+10;
198 if( p
->bErr
) return 1;
199 zNew
= sqlite3_malloc64(nTotal
);
204 memcpy(zNew
, p
->zBuf
, (size_t)p
->nUsed
);
208 zNew
= sqlite3_realloc64(p
->zBuf
, nTotal
);
219 /* Append N bytes from zIn onto the end of the JsonString string.
221 static void jsonAppendRaw(JsonString
*p
, const char *zIn
, u32 N
){
223 if( (N
+p
->nUsed
>= p
->nAlloc
) && jsonGrow(p
,N
)!=0 ) return;
224 memcpy(p
->zBuf
+p
->nUsed
, zIn
, N
);
228 /* Append formatted text (not to exceed N bytes) to the JsonString.
230 static void jsonPrintf(int N
, JsonString
*p
, const char *zFormat
, ...){
232 if( (p
->nUsed
+ N
>= p
->nAlloc
) && jsonGrow(p
, N
) ) return;
233 va_start(ap
, zFormat
);
234 sqlite3_vsnprintf(N
, p
->zBuf
+p
->nUsed
, zFormat
, ap
);
236 p
->nUsed
+= (int)strlen(p
->zBuf
+p
->nUsed
);
239 /* Append a single character
241 static void jsonAppendChar(JsonString
*p
, char c
){
242 if( p
->nUsed
>=p
->nAlloc
&& jsonGrow(p
,1)!=0 ) return;
243 p
->zBuf
[p
->nUsed
++] = c
;
246 /* Append a comma separator to the output buffer, if the previous
247 ** character is not '[' or '{'.
249 static void jsonAppendSeparator(JsonString
*p
){
251 if( p
->nUsed
==0 ) return;
252 c
= p
->zBuf
[p
->nUsed
-1];
253 if( c
!='[' && c
!='{' ) jsonAppendChar(p
, ',');
256 /* Append the N-byte string in zIn to the end of the JsonString string
257 ** under construction. Enclose the string in "..." and escape
258 ** any double-quotes or backslash characters contained within the
261 static void jsonAppendString(JsonString
*p
, const char *zIn
, u32 N
){
263 if( zIn
==0 || ((N
+p
->nUsed
+2 >= p
->nAlloc
) && jsonGrow(p
,N
+2)!=0) ) return;
264 p
->zBuf
[p
->nUsed
++] = '"';
266 unsigned char c
= ((unsigned const char*)zIn
)[i
];
267 if( c
=='"' || c
=='\\' ){
269 if( (p
->nUsed
+N
+3-i
> p
->nAlloc
) && jsonGrow(p
,N
+3-i
)!=0 ) return;
270 p
->zBuf
[p
->nUsed
++] = '\\';
272 static const char aSpecial
[] = {
273 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
274 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
276 assert( sizeof(aSpecial
)==32 );
277 assert( aSpecial
['\b']=='b' );
278 assert( aSpecial
['\f']=='f' );
279 assert( aSpecial
['\n']=='n' );
280 assert( aSpecial
['\r']=='r' );
281 assert( aSpecial
['\t']=='t' );
284 goto json_simple_escape
;
286 if( (p
->nUsed
+N
+7+i
> p
->nAlloc
) && jsonGrow(p
,N
+7-i
)!=0 ) return;
287 p
->zBuf
[p
->nUsed
++] = '\\';
288 p
->zBuf
[p
->nUsed
++] = 'u';
289 p
->zBuf
[p
->nUsed
++] = '0';
290 p
->zBuf
[p
->nUsed
++] = '0';
291 p
->zBuf
[p
->nUsed
++] = '0' + (c
>>4);
292 c
= "0123456789abcdef"[c
&0xf];
294 p
->zBuf
[p
->nUsed
++] = c
;
296 p
->zBuf
[p
->nUsed
++] = '"';
297 assert( p
->nUsed
<p
->nAlloc
);
301 ** The zIn[0..N] string is a JSON5 string literal. Append to p a translation
302 ** of the string literal that standard JSON and that omits all JSON5
305 static void jsonAppendNormalizedString(JsonString
*p
, const char *zIn
, u32 N
){
307 jsonAppendChar(p
, '"');
311 for(i
=0; i
<N
&& zIn
[i
]!='\\'; i
++){}
313 jsonAppendRaw(p
, zIn
, i
);
318 assert( zIn
[0]=='\\' );
319 switch( (u8
)zIn
[1] ){
321 jsonAppendChar(p
, '\'');
324 jsonAppendRaw(p
, "\\u0009", 6);
327 jsonAppendRaw(p
, "\\u00", 4);
328 jsonAppendRaw(p
, &zIn
[2], 2);
333 jsonAppendRaw(p
, "\\u0000", 6);
345 assert( 0x80==(u8
)zIn
[2] );
346 assert( 0xa8==(u8
)zIn
[3] || 0xa9==(u8
)zIn
[3] );
351 jsonAppendRaw(p
, zIn
, 2);
357 jsonAppendChar(p
, '"');
361 ** The zIn[0..N] string is a JSON5 integer literal. Append to p a translation
362 ** of the string literal that standard JSON and that omits all JSON5
365 static void jsonAppendNormalizedInt(JsonString
*p
, const char *zIn
, u32 N
){
369 }else if( zIn
[0]=='-' ){
370 jsonAppendChar(p
, '-');
374 if( zIn
[0]=='0' && (zIn
[1]=='x' || zIn
[1]=='X') ){
376 int rc
= sqlite3DecOrHexToI64(zIn
, &i
);
378 jsonPrintf(100,p
,"%lld",i
);
381 jsonAppendRaw(p
, "9.0e999", 7);
385 jsonAppendRaw(p
, zIn
, N
);
389 ** The zIn[0..N] string is a JSON5 real literal. Append to p a translation
390 ** of the string literal that standard JSON and that omits all JSON5
393 static void jsonAppendNormalizedReal(JsonString
*p
, const char *zIn
, u32 N
){
398 }else if( zIn
[0]=='-' ){
399 jsonAppendChar(p
, '-');
404 jsonAppendChar(p
, '0');
407 if( zIn
[i
]=='.' && (i
+1==N
|| !sqlite3Isdigit(zIn
[i
+1])) ){
409 jsonAppendRaw(p
, zIn
, i
);
412 jsonAppendChar(p
, '0');
417 jsonAppendRaw(p
, zIn
, N
);
424 ** Append a function parameter value to the JSON string under
427 static void jsonAppendValue(
428 JsonString
*p
, /* Append to this JSON string */
429 sqlite3_value
*pValue
/* Value to append */
431 switch( sqlite3_value_type(pValue
) ){
433 jsonAppendRaw(p
, "null", 4);
437 jsonPrintf(100, p
, "%!0.15g", sqlite3_value_double(pValue
));
440 case SQLITE_INTEGER
: {
441 const char *z
= (const char*)sqlite3_value_text(pValue
);
442 u32 n
= (u32
)sqlite3_value_bytes(pValue
);
443 jsonAppendRaw(p
, z
, n
);
447 const char *z
= (const char*)sqlite3_value_text(pValue
);
448 u32 n
= (u32
)sqlite3_value_bytes(pValue
);
449 if( sqlite3_value_subtype(pValue
)==JSON_SUBTYPE
){
450 jsonAppendRaw(p
, z
, n
);
452 jsonAppendString(p
, z
, n
);
458 sqlite3_result_error(p
->pCtx
, "JSON cannot hold BLOB values", -1);
468 /* Make the JSON in p the result of the SQL function.
470 static void jsonResult(JsonString
*p
){
472 sqlite3_result_text64(p
->pCtx
, p
->zBuf
, p
->nUsed
,
473 p
->bStatic
? SQLITE_TRANSIENT
: sqlite3_free
,
477 assert( p
->bStatic
);
480 /**************************************************************************
481 ** Utility routines for dealing with JsonNode and JsonParse objects
482 **************************************************************************/
485 ** Return the number of consecutive JsonNode slots need to represent
486 ** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
487 ** OBJECT types, the number might be larger.
489 ** Appended elements are not counted. The value returned is the number
490 ** by which the JsonNode counter should increment in order to go to the
493 static u32
jsonNodeSize(JsonNode
*pNode
){
494 return pNode
->eType
>=JSON_ARRAY
? pNode
->n
+1 : 1;
498 ** Reclaim all memory allocated by a JsonParse object. But do not
499 ** delete the JsonParse object itself.
501 static void jsonParseReset(JsonParse
*pParse
){
502 sqlite3_free(pParse
->aNode
);
506 sqlite3_free(pParse
->aUp
);
511 ** Free a JsonParse object that was obtained from sqlite3_malloc().
513 static void jsonParseFree(JsonParse
*pParse
){
514 jsonParseReset(pParse
);
515 sqlite3_free(pParse
);
519 ** Convert the JsonNode pNode into a pure JSON string and
520 ** append to pOut. Subsubstructure is also included. Return
521 ** the number of JsonNode objects that are encoded.
523 static void jsonRenderNode(
524 JsonNode
*pNode
, /* The node to render */
525 JsonString
*pOut
, /* Write JSON here */
526 sqlite3_value
**aReplace
/* Replacement values */
529 if( pNode
->jnFlags
& (JNODE_REPLACE
|JNODE_PATCH
) ){
530 if( (pNode
->jnFlags
& JNODE_REPLACE
)!=0 && ALWAYS(aReplace
!=0) ){
531 assert( pNode
->eU
==4 );
532 jsonAppendValue(pOut
, aReplace
[pNode
->u
.iReplace
]);
535 assert( pNode
->eU
==5 );
536 pNode
= pNode
->u
.pPatch
;
538 switch( pNode
->eType
){
540 assert( pNode
->eType
==JSON_NULL
);
541 jsonAppendRaw(pOut
, "null", 4);
545 jsonAppendRaw(pOut
, "true", 4);
549 jsonAppendRaw(pOut
, "false", 5);
553 assert( pNode
->eU
==1 );
554 if( pNode
->jnFlags
& JNODE_RAW
){
555 if( pNode
->jnFlags
& JNODE_LABEL
){
556 jsonAppendChar(pOut
, '"');
557 jsonAppendRaw(pOut
, pNode
->u
.zJContent
, pNode
->n
);
558 jsonAppendChar(pOut
, '"');
560 jsonAppendString(pOut
, pNode
->u
.zJContent
, pNode
->n
);
562 }else if( pNode
->jnFlags
& JNODE_JSON5
){
563 jsonAppendNormalizedString(pOut
, pNode
->u
.zJContent
, pNode
->n
);
565 jsonAppendRaw(pOut
, pNode
->u
.zJContent
, pNode
->n
);
570 assert( pNode
->eU
==1 );
571 if( pNode
->jnFlags
& JNODE_JSON5
){
572 jsonAppendNormalizedReal(pOut
, pNode
->u
.zJContent
, pNode
->n
);
574 jsonAppendRaw(pOut
, pNode
->u
.zJContent
, pNode
->n
);
579 assert( pNode
->eU
==1 );
580 if( pNode
->jnFlags
& JNODE_JSON5
){
581 jsonAppendNormalizedInt(pOut
, pNode
->u
.zJContent
, pNode
->n
);
583 jsonAppendRaw(pOut
, pNode
->u
.zJContent
, pNode
->n
);
589 jsonAppendChar(pOut
, '[');
591 while( j
<=pNode
->n
){
592 if( (pNode
[j
].jnFlags
& JNODE_REMOVE
)==0 ){
593 jsonAppendSeparator(pOut
);
594 jsonRenderNode(&pNode
[j
], pOut
, aReplace
);
596 j
+= jsonNodeSize(&pNode
[j
]);
598 if( (pNode
->jnFlags
& JNODE_APPEND
)==0 ) break;
599 assert( pNode
->eU
==2 );
600 pNode
= &pNode
[pNode
->u
.iAppend
];
603 jsonAppendChar(pOut
, ']');
608 jsonAppendChar(pOut
, '{');
610 while( j
<=pNode
->n
){
611 if( (pNode
[j
+1].jnFlags
& JNODE_REMOVE
)==0 ){
612 jsonAppendSeparator(pOut
);
613 jsonRenderNode(&pNode
[j
], pOut
, aReplace
);
614 jsonAppendChar(pOut
, ':');
615 jsonRenderNode(&pNode
[j
+1], pOut
, aReplace
);
617 j
+= 1 + jsonNodeSize(&pNode
[j
+1]);
619 if( (pNode
->jnFlags
& JNODE_APPEND
)==0 ) break;
620 assert( pNode
->eU
==2 );
621 pNode
= &pNode
[pNode
->u
.iAppend
];
624 jsonAppendChar(pOut
, '}');
631 ** Return a JsonNode and all its descendents as a JSON string.
633 static void jsonReturnJson(
634 JsonNode
*pNode
, /* Node to return */
635 sqlite3_context
*pCtx
, /* Return value for this function */
636 sqlite3_value
**aReplace
/* Array of replacement values */
640 jsonRenderNode(pNode
, &s
, aReplace
);
642 sqlite3_result_subtype(pCtx
, JSON_SUBTYPE
);
646 ** Translate a single byte of Hex into an integer.
647 ** This routine only works if h really is a valid hexadecimal
648 ** character: 0..9a..fA..F
650 static u8
jsonHexToInt(int h
){
651 assert( (h
>='0' && h
<='9') || (h
>='a' && h
<='f') || (h
>='A' && h
<='F') );
657 return (u8
)(h
& 0xf);
661 ** Convert a 4-byte hex string into an integer
663 static u32
jsonHexToInt4(const char *z
){
665 assert( sqlite3Isxdigit(z
[0]) );
666 assert( sqlite3Isxdigit(z
[1]) );
667 assert( sqlite3Isxdigit(z
[2]) );
668 assert( sqlite3Isxdigit(z
[3]) );
669 v
= (jsonHexToInt(z
[0])<<12)
670 + (jsonHexToInt(z
[1])<<8)
671 + (jsonHexToInt(z
[2])<<4)
672 + jsonHexToInt(z
[3]);
677 ** Make the JsonNode the return value of the function.
679 static void jsonReturn(
680 JsonNode
*pNode
, /* Node to return */
681 sqlite3_context
*pCtx
, /* Return value for this function */
682 sqlite3_value
**aReplace
/* Array of replacement values */
684 switch( pNode
->eType
){
686 assert( pNode
->eType
==JSON_NULL
);
687 sqlite3_result_null(pCtx
);
691 sqlite3_result_int(pCtx
, 1);
695 sqlite3_result_int(pCtx
, 0);
705 assert( pNode
->eU
==1 );
706 z
= pNode
->u
.zJContent
;
707 if( z
[0]=='-' ){ z
++; bNeg
= 1; }
708 else if( z
[0]=='+' ){ z
++; }
709 rc
= sqlite3DecOrHexToI64(z
, &i
);
711 sqlite3_result_int64(pCtx
, bNeg
? -i
: i
);
712 }else if( rc
==3 && bNeg
){
713 sqlite3_result_int64(pCtx
, SMALLEST_INT64
);
722 assert( pNode
->eU
==1 );
724 z
= pNode
->u
.zJContent
;
725 sqlite3AtoF(z
, &r
, sqlite3Strlen30(z
), SQLITE_UTF8
);
726 sqlite3_result_double(pCtx
, r
);
730 if( pNode
->jnFlags
& JNODE_RAW
){
731 assert( pNode
->eU
==1 );
732 sqlite3_result_text(pCtx
, pNode
->u
.zJContent
, pNode
->n
,
734 }else if( (pNode
->jnFlags
& JNODE_ESCAPE
)==0 ){
735 /* JSON formatted without any backslash-escapes */
736 assert( pNode
->eU
==1 );
737 sqlite3_result_text(pCtx
, pNode
->u
.zJContent
+1, pNode
->n
-2,
740 /* Translate JSON formatted string into raw text */
747 assert( pNode
->eU
==1 );
748 z
= pNode
->u
.zJContent
;
749 zOut
= sqlite3_malloc( nOut
+1 );
751 sqlite3_result_error_nomem(pCtx
);
754 for(i
=1, j
=0; i
<n
-1; i
++){
759 u32 v
= jsonHexToInt4(z
+i
+1);
764 }else if( v
<=0x7ff ){
765 zOut
[j
++] = (char)(0xc0 | (v
>>6));
766 zOut
[j
++] = 0x80 | (v
&0x3f);
769 if( (v
&0xfc00)==0xd800
773 && ((vlo
= jsonHexToInt4(z
+i
+3))&0xfc00)==0xdc00
775 /* We have a surrogate pair */
776 v
= ((v
&0x3ff)<<10) + (vlo
&0x3ff) + 0x10000;
778 zOut
[j
++] = 0xf0 | (v
>>18);
779 zOut
[j
++] = 0x80 | ((v
>>12)&0x3f);
780 zOut
[j
++] = 0x80 | ((v
>>6)&0x3f);
781 zOut
[j
++] = 0x80 | (v
&0x3f);
783 zOut
[j
++] = 0xe0 | (v
>>12);
784 zOut
[j
++] = 0x80 | ((v
>>6)&0x3f);
785 zOut
[j
++] = 0x80 | (v
&0x3f);
801 }else if( c
=='\'' || c
=='"' || c
=='/' || c
=='\\' ){
802 /* pass through unchanged */
806 c
= (jsonHexToInt(z
[i
+1])<<4) | jsonHexToInt(z
[i
+2]);
808 }else if( c
=='\r' && z
[i
+1]=='\n' ){
811 }else if( 0xe2==(u8
)c
){
812 assert( 0x80==(u8
)z
[i
+1] );
813 assert( 0xa8==(u8
)z
[i
+2] || 0xa9==(u8
)z
[i
+2] );
819 } /* end if( c=='\\' ) */
823 sqlite3_result_text(pCtx
, zOut
, j
, sqlite3_free
);
829 jsonReturnJson(pNode
, pCtx
, aReplace
);
835 /* Forward reference */
836 static int jsonParseAddNode(JsonParse
*,u32
,u32
,const char*);
839 ** A macro to hint to the compiler that a function should not be
842 #if defined(__GNUC__)
843 # define JSON_NOINLINE __attribute__((noinline))
844 #elif defined(_MSC_VER) && _MSC_VER>=1310
845 # define JSON_NOINLINE __declspec(noinline)
847 # define JSON_NOINLINE
851 static JSON_NOINLINE
int jsonParseAddNodeExpand(
852 JsonParse
*pParse
, /* Append the node to this object */
853 u32 eType
, /* Node type */
854 u32 n
, /* Content size or sub-node count */
855 const char *zContent
/* Content */
859 assert( pParse
->nNode
>=pParse
->nAlloc
);
860 if( pParse
->oom
) return -1;
861 nNew
= pParse
->nAlloc
*2 + 10;
862 pNew
= sqlite3_realloc64(pParse
->aNode
, sizeof(JsonNode
)*nNew
);
867 pParse
->nAlloc
= nNew
;
868 pParse
->aNode
= pNew
;
869 assert( pParse
->nNode
<pParse
->nAlloc
);
870 return jsonParseAddNode(pParse
, eType
, n
, zContent
);
874 ** Create a new JsonNode instance based on the arguments and append that
875 ** instance to the JsonParse. Return the index in pParse->aNode[] of the
876 ** new node, or -1 if a memory allocation fails.
878 static int jsonParseAddNode(
879 JsonParse
*pParse
, /* Append the node to this object */
880 u32 eType
, /* Node type */
881 u32 n
, /* Content size or sub-node count */
882 const char *zContent
/* Content */
885 if( pParse
->aNode
==0 || pParse
->nNode
>=pParse
->nAlloc
){
886 return jsonParseAddNodeExpand(pParse
, eType
, n
, zContent
);
888 p
= &pParse
->aNode
[pParse
->nNode
];
889 p
->eType
= (u8
)(eType
& 0xff);
890 p
->jnFlags
= (u8
)(eType
>> 8);
891 VVA( p
->eU
= zContent
? 1 : 0 );
893 p
->u
.zJContent
= zContent
;
894 return pParse
->nNode
++;
898 ** Return true if z[] begins with 2 (or more) hexadecimal digits
900 static int jsonIs2Hex(const char *z
){
901 return sqlite3Isxdigit(z
[0]) && sqlite3Isxdigit(z
[1]);
905 ** Return true if z[] begins with 4 (or more) hexadecimal digits
907 static int jsonIs4Hex(const char *z
){
908 return jsonIs2Hex(z
) && jsonIs2Hex(&z
[2]);
912 ** Return the number of bytes of JSON5 whitespace at the beginning of
913 ** the input string z[].
915 ** JSON5 whitespace consists of any of the following characters:
917 ** Unicode UTF-8 Name
918 ** U+0009 09 horizontal tab
919 ** U+000a 0a line feed
920 ** U+000b 0b vertical tab
921 ** U+000c 0c form feed
922 ** U+000d 0d carriage return
924 ** U+00a0 c2 a0 non-breaking space
925 ** U+1680 e1 9a 80 ogham space mark
926 ** U+2000 e2 80 80 en quad
927 ** U+2001 e2 80 81 em quad
928 ** U+2002 e2 80 82 en space
929 ** U+2003 e2 80 83 em space
930 ** U+2004 e2 80 84 three-per-em space
931 ** U+2005 e2 80 85 four-per-em space
932 ** U+2006 e2 80 86 six-per-em space
933 ** U+2007 e2 80 87 figure space
934 ** U+2008 e2 80 88 punctuation space
935 ** U+2009 e2 80 89 thin space
936 ** U+200a e2 80 8a hair space
937 ** U+2028 e2 80 a8 line separator
938 ** U+2029 e2 80 a9 paragraph separator
939 ** U+202f e2 80 af narrow no-break space (NNBSP)
940 ** U+205f e2 81 9f medium mathematical space (MMSP)
941 ** U+3000 e3 80 80 ideographical space
942 ** U+FEFF ef bb bf byte order mark
944 ** In addition, comments between '/', '*' and '*', '/' and
945 ** from '/', '/' to end-of-line are also considered to be whitespace.
947 static int json5Whitespace(const char *zIn
){
949 const u8
*z
= (u8
*)zIn
;
950 while( 1 /*exit by "goto whitespace_done"*/ ){
962 if( z
[n
+1]=='*' && z
[n
+2]!=0 ){
964 for(j
=n
+3; z
[j
]!='/' || z
[j
-1]!='*'; j
++){
965 if( z
[j
]==0 ) goto whitespace_done
;
969 }else if( z
[n
+1]=='/' ){
972 for(j
=n
+2; (c
= z
[j
])!=0; j
++){
973 if( c
=='\n' || c
=='\r' ) break;
974 if( 0xe2==(u8
)c
&& 0x80==(u8
)z
[j
+1]
975 && (0xa8==(u8
)z
[j
+2] || 0xa9==(u8
)z
[j
+2])
985 goto whitespace_done
;
992 goto whitespace_done
;
995 if( z
[n
+1]==0x9a && z
[n
+2]==0x80 ){
999 goto whitespace_done
;
1004 if( c
<0x80 ) goto whitespace_done
;
1005 if( c
<=0x8a || c
==0xa8 || c
==0xa9 || c
==0xaf ){
1009 }else if( z
[n
+1]==0x81 && z
[n
+2]==0x9f ){
1013 goto whitespace_done
;
1016 if( z
[n
+1]==0x80 && z
[n
+2]==0x80 ){
1020 goto whitespace_done
;
1023 if( z
[n
+1]==0xbb && z
[n
+2]==0xbf ){
1027 goto whitespace_done
;
1030 goto whitespace_done
;
1039 ** Extra floating-point literals to allow in JSON.
1041 static const struct NanInfName
{
1050 { 'i', 'I', 3, JSON_REAL
, 7, "inf", "9.0e999" },
1051 { 'i', 'I', 8, JSON_REAL
, 7, "infinity", "9.0e999" },
1052 { 'n', 'N', 3, JSON_NULL
, 4, "NaN", "null" },
1053 { 'q', 'Q', 4, JSON_NULL
, 4, "QNaN", "null" },
1054 { 's', 'S', 4, JSON_NULL
, 4, "SNaN", "null" },
1058 ** Parse a single JSON value which begins at pParse->zJson[i]. Return the
1059 ** index of the first character past the end of the value parsed.
1061 ** Special return values:
1070 static int jsonParseValue(JsonParse
*pParse
, u32 i
){
1076 const char *z
= pParse
->zJson
;
1081 iThis
= jsonParseAddNode(pParse
, JSON_OBJECT
, 0, 0);
1082 if( iThis
<0 ) return -1;
1083 if( ++pParse
->iDepth
> JSON_MAX_DEPTH
){
1088 u32 nNode
= pParse
->nNode
;
1089 x
= jsonParseValue(pParse
, j
);
1093 if( pParse
->nNode
!=(u32
)iThis
+1 ) pParse
->hasNonstd
= 1;
1096 j
+= json5Whitespace(&z
[j
]);
1097 if( sqlite3JsonId1(z
[j
])
1098 || (z
[j
]=='\\' && z
[j
+1]=='u' && jsonIs4Hex(&z
[j
+2]))
1101 while( (sqlite3JsonId2(z
[k
]) && json5Whitespace(&z
[k
])==0)
1102 || (z
[k
]=='\\' && z
[k
+1]=='u' && jsonIs4Hex(&z
[k
+2]))
1106 jsonParseAddNode(pParse
, JSON_STRING
| (JNODE_RAW
<<8), k
-j
, &z
[j
]);
1107 pParse
->hasNonstd
= 1;
1110 if( x
!=-1 ) pParse
->iErr
= j
;
1114 if( pParse
->oom
) return -1;
1115 pNode
= &pParse
->aNode
[nNode
];
1116 if( pNode
->eType
!=JSON_STRING
){
1120 pNode
->jnFlags
|= JNODE_LABEL
;
1125 if( fast_isspace(z
[j
]) ){
1126 do{ j
++; }while( fast_isspace(z
[j
]) );
1129 goto parse_object_value
;
1132 x
= jsonParseValue(pParse
, j
);
1134 if( x
!=(-1) ) pParse
->iErr
= j
;
1140 x
= jsonParseValue(pParse
, j
);
1142 if( x
!=(-1) ) pParse
->iErr
= j
;
1148 }else if( z
[j
]=='}' ){
1151 if( fast_isspace(z
[j
]) ){
1152 do{ j
++; }while( fast_isspace(z
[j
]) );
1155 }else if( z
[j
]=='}' ){
1159 x
= jsonParseValue(pParse
, j
);
1172 pParse
->aNode
[iThis
].n
= pParse
->nNode
- (u32
)iThis
- 1;
1178 iThis
= jsonParseAddNode(pParse
, JSON_ARRAY
, 0, 0);
1179 if( iThis
<0 ) return -1;
1180 if( ++pParse
->iDepth
> JSON_MAX_DEPTH
){
1184 memset(&pParse
->aNode
[iThis
].u
, 0, sizeof(pParse
->aNode
[iThis
].u
));
1186 x
= jsonParseValue(pParse
, j
);
1190 if( pParse
->nNode
!=(u32
)iThis
+1 ) pParse
->hasNonstd
= 1;
1193 if( x
!=(-1) ) pParse
->iErr
= j
;
1199 }else if( z
[j
]==']' ){
1202 if( fast_isspace(z
[j
]) ){
1203 do{ j
++; }while( fast_isspace(z
[j
]) );
1206 }else if( z
[j
]==']' ){
1210 x
= jsonParseValue(pParse
, j
);
1223 pParse
->aNode
[iThis
].n
= pParse
->nNode
- (u32
)iThis
- 1;
1230 pParse
->hasNonstd
= 1;
1231 jnFlags
= JNODE_JSON5
;
1241 if( (c
& ~0x1f)==0 ){
1242 /* Control characters are not allowed in strings */
1248 if( c
=='"' || c
=='\\' || c
=='/' || c
=='b' || c
=='f'
1249 || c
=='n' || c
=='r' || c
=='t'
1250 || (c
=='u' && jsonIs4Hex(&z
[j
+1])) ){
1251 jnFlags
|= JNODE_ESCAPE
;
1252 }else if( c
=='\'' || c
=='0' || c
=='v' || c
=='\n'
1253 || (0xe2==(u8
)c
&& 0x80==(u8
)z
[j
+1]
1254 && (0xa8==(u8
)z
[j
+2] || 0xa9==(u8
)z
[j
+2]))
1255 || (c
=='x' && jsonIs2Hex(&z
[j
+1])) ){
1256 jnFlags
|= (JNODE_ESCAPE
|JNODE_JSON5
);
1257 pParse
->hasNonstd
= 1;
1258 }else if( c
=='\r' ){
1259 if( z
[j
+1]=='\n' ) j
++;
1260 jnFlags
|= (JNODE_ESCAPE
|JNODE_JSON5
);
1261 pParse
->hasNonstd
= 1;
1266 }else if( c
==cDelim
){
1271 jsonParseAddNode(pParse
, JSON_STRING
| (jnFlags
<<8), j
+1-i
, &z
[i
]);
1275 if( strncmp(z
+i
,"true",4)==0 && !sqlite3Isalnum(z
[i
+4]) ){
1276 jsonParseAddNode(pParse
, JSON_TRUE
, 0, 0);
1283 if( strncmp(z
+i
,"false",5)==0 && !sqlite3Isalnum(z
[i
+5]) ){
1284 jsonParseAddNode(pParse
, JSON_FALSE
, 0, 0);
1291 u8 seenDP
, seenE
, jnFlags
;
1292 pParse
->hasNonstd
= 1;
1293 jnFlags
= JNODE_JSON5
;
1296 if( sqlite3Isdigit(z
[i
+1]) ){
1297 pParse
->hasNonstd
= 1;
1298 jnFlags
= JNODE_JSON5
;
1301 goto parse_number_2
;
1321 assert( '-' < '0' );
1322 assert( '+' < '0' );
1323 assert( '.' < '0' );
1328 if( (z
[i
+1]=='x' || z
[i
+1]=='X') && sqlite3Isxdigit(z
[i
+2]) ){
1329 assert( seenDP
==JSON_INT
);
1330 pParse
->hasNonstd
= 1;
1331 jnFlags
|= JNODE_JSON5
;
1332 for(j
=i
+3; sqlite3Isxdigit(z
[j
]); j
++){}
1333 goto parse_number_finish
;
1334 }else if( sqlite3Isdigit(z
[i
+1]) ){
1339 if( !sqlite3Isdigit(z
[i
+1]) ){
1340 /* JSON5 allows for "+Infinity" and "-Infinity" using exactly
1341 ** that case. SQLite also allows these in any case and it allows
1342 ** "+inf" and "-inf". */
1343 if( (z
[i
+1]=='I' || z
[i
+1]=='i')
1344 && sqlite3StrNICmp(&z
[i
+1], "inf",3)==0
1346 pParse
->hasNonstd
= 1;
1348 jsonParseAddNode(pParse
, JSON_REAL
, 8, "-9.0e999");
1350 jsonParseAddNode(pParse
, JSON_REAL
, 7, "9.0e999");
1352 return i
+ (sqlite3StrNICmp(&z
[i
+4],"inity",5)==0 ? 9 : 4);
1355 pParse
->hasNonstd
= 1;
1356 jnFlags
|= JNODE_JSON5
;
1357 goto parse_number_2
;
1363 if( sqlite3Isdigit(z
[i
+2]) ){
1366 }else if( (z
[i
+2]=='x' || z
[i
+2]=='X') && sqlite3Isxdigit(z
[i
+3]) ){
1367 pParse
->hasNonstd
= 1;
1368 jnFlags
|= JNODE_JSON5
;
1369 for(j
=i
+4; sqlite3Isxdigit(z
[j
]); j
++){}
1370 goto parse_number_finish
;
1378 if( sqlite3Isdigit(c
) ) continue;
1380 if( seenDP
==JSON_REAL
){
1387 if( c
=='e' || c
=='E' ){
1389 if( ALWAYS(z
[j
-1]=='.') && ALWAYS(j
-2>=i
) && sqlite3Isdigit(z
[j
-2]) ){
1390 pParse
->hasNonstd
= 1;
1391 jnFlags
|= JNODE_JSON5
;
1404 if( c
=='+' || c
=='-' ){
1408 if( c
<'0' || c
>'9' ){
1417 if( ALWAYS(z
[j
-1]=='.') && ALWAYS(j
-2>=i
) && sqlite3Isdigit(z
[j
-2]) ){
1418 pParse
->hasNonstd
= 1;
1419 jnFlags
|= JNODE_JSON5
;
1425 parse_number_finish
:
1426 jsonParseAddNode(pParse
, seenDP
| (jnFlags
<<8), j
- i
, &z
[i
]);
1431 return -2; /* End of {...} */
1435 return -3; /* End of [...] */
1439 return -4; /* List separator */
1443 return -5; /* Object label/value separator */
1446 return 0; /* End of file */
1454 }while( fast_isspace(z
[i
]) );
1455 goto json_parse_restart
;
1465 j
= json5Whitespace(&z
[i
]);
1468 pParse
->hasNonstd
= 1;
1469 goto json_parse_restart
;
1475 if( strncmp(z
+i
,"null",4)==0 && !sqlite3Isalnum(z
[i
+4]) ){
1476 jsonParseAddNode(pParse
, JSON_NULL
, 0, 0);
1479 /* fall-through into the default case that checks for NaN */
1485 for(k
=0; k
<sizeof(aNanInfName
)/sizeof(aNanInfName
[0]); k
++){
1486 if( c
!=aNanInfName
[k
].c1
&& c
!=aNanInfName
[k
].c2
) continue;
1487 nn
= aNanInfName
[k
].n
;
1488 if( sqlite3StrNICmp(&z
[i
], aNanInfName
[k
].zMatch
, nn
)!=0 ){
1491 if( sqlite3Isalnum(z
[i
+nn
]) ) continue;
1492 jsonParseAddNode(pParse
, aNanInfName
[k
].eType
,
1493 aNanInfName
[k
].nRepl
, aNanInfName
[k
].zRepl
);
1494 pParse
->hasNonstd
= 1;
1498 return -1; /* Syntax error */
1500 } /* End switch(z[i]) */
1504 ** Parse a complete JSON string. Return 0 on success or non-zero if there
1505 ** are any errors. If an error occurs, free all memory associated with
1508 ** pParse is uninitialized when this routine is called.
1510 static int jsonParse(
1511 JsonParse
*pParse
, /* Initialize and fill this JsonParse object */
1512 sqlite3_context
*pCtx
, /* Report errors here */
1513 const char *zJson
/* Input JSON text to be parsed */
1516 memset(pParse
, 0, sizeof(*pParse
));
1517 if( zJson
==0 ) return 1;
1518 pParse
->zJson
= zJson
;
1519 i
= jsonParseValue(pParse
, 0);
1520 if( pParse
->oom
) i
= -1;
1522 assert( pParse
->iDepth
==0 );
1523 while( fast_isspace(zJson
[i
]) ) i
++;
1525 i
+= json5Whitespace(&zJson
[i
]);
1527 jsonParseReset(pParse
);
1530 pParse
->hasNonstd
= 1;
1536 sqlite3_result_error_nomem(pCtx
);
1538 sqlite3_result_error(pCtx
, "malformed JSON", -1);
1541 jsonParseReset(pParse
);
1547 /* Mark node i of pParse as being a child of iParent. Call recursively
1548 ** to fill in all the descendants of node i.
1550 static void jsonParseFillInParentage(JsonParse
*pParse
, u32 i
, u32 iParent
){
1551 JsonNode
*pNode
= &pParse
->aNode
[i
];
1553 pParse
->aUp
[i
] = iParent
;
1554 switch( pNode
->eType
){
1556 for(j
=1; j
<=pNode
->n
; j
+= jsonNodeSize(pNode
+j
)){
1557 jsonParseFillInParentage(pParse
, i
+j
, i
);
1562 for(j
=1; j
<=pNode
->n
; j
+= jsonNodeSize(pNode
+j
+1)+1){
1563 pParse
->aUp
[i
+j
] = i
;
1564 jsonParseFillInParentage(pParse
, i
+j
+1, i
);
1575 ** Compute the parentage of all nodes in a completed parse.
1577 static int jsonParseFindParents(JsonParse
*pParse
){
1579 assert( pParse
->aUp
==0 );
1580 aUp
= pParse
->aUp
= sqlite3_malloc64( sizeof(u32
)*pParse
->nNode
);
1583 return SQLITE_NOMEM
;
1585 jsonParseFillInParentage(pParse
, 0, 0);
1590 ** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
1592 #define JSON_CACHE_ID (-429938) /* First cache entry */
1593 #define JSON_CACHE_SZ 4 /* Max number of cache entries */
1596 ** Obtain a complete parse of the JSON found in the first argument
1597 ** of the argv array. Use the sqlite3_get_auxdata() cache for this
1598 ** parse if it is available. If the cache is not available or if it
1599 ** is no longer valid, parse the JSON again and return the new parse,
1600 ** and also register the new parse so that it will be available for
1601 ** future sqlite3_get_auxdata() calls.
1603 ** If an error occurs and pErrCtx!=0 then report the error on pErrCtx
1606 ** If an error occurs and pErrCtx==0 then return the Parse object with
1607 ** JsonParse.nErr non-zero. If the caller invokes this routine with
1608 ** pErrCtx==0 and it gets back a JsonParse with nErr!=0, then the caller
1609 ** is responsible for invoking jsonParseFree() on the returned value.
1610 ** But the caller may invoke jsonParseFree() *only* if pParse->nErr!=0.
1612 static JsonParse
*jsonParseCached(
1613 sqlite3_context
*pCtx
,
1614 sqlite3_value
**argv
,
1615 sqlite3_context
*pErrCtx
1617 const char *zJson
= (const char*)sqlite3_value_text(argv
[0]);
1618 int nJson
= sqlite3_value_bytes(argv
[0]);
1620 JsonParse
*pMatch
= 0;
1623 u32 iMinHold
= 0xffffffff;
1625 if( zJson
==0 ) return 0;
1626 for(iKey
=0; iKey
<JSON_CACHE_SZ
; iKey
++){
1627 p
= (JsonParse
*)sqlite3_get_auxdata(pCtx
, JSON_CACHE_ID
+iKey
);
1634 && memcmp(p
->zJson
,zJson
,nJson
)==0
1638 }else if( p
->iHold
<iMinHold
){
1639 iMinHold
= p
->iHold
;
1642 if( p
->iHold
>iMaxHold
){
1643 iMaxHold
= p
->iHold
;
1648 pMatch
->iHold
= iMaxHold
+1;
1651 p
= sqlite3_malloc64( sizeof(*p
) + nJson
+ 1 );
1653 sqlite3_result_error_nomem(pCtx
);
1656 memset(p
, 0, sizeof(*p
));
1657 p
->zJson
= (char*)&p
[1];
1658 memcpy((char*)p
->zJson
, zJson
, nJson
+1);
1659 if( jsonParse(p
, pErrCtx
, p
->zJson
) ){
1668 p
->iHold
= iMaxHold
+1;
1669 sqlite3_set_auxdata(pCtx
, JSON_CACHE_ID
+iMinKey
, p
,
1670 (void(*)(void*))jsonParseFree
);
1671 return (JsonParse
*)sqlite3_get_auxdata(pCtx
, JSON_CACHE_ID
+iMinKey
);
1675 ** Compare the OBJECT label at pNode against zKey,nKey. Return true on
1678 static int jsonLabelCompare(const JsonNode
*pNode
, const char *zKey
, u32 nKey
){
1679 assert( pNode
->eU
==1 );
1680 if( pNode
->jnFlags
& JNODE_RAW
){
1681 if( pNode
->n
!=nKey
) return 0;
1682 return strncmp(pNode
->u
.zJContent
, zKey
, nKey
)==0;
1684 if( pNode
->n
!=nKey
+2 ) return 0;
1685 return strncmp(pNode
->u
.zJContent
+1, zKey
, nKey
)==0;
1688 static int jsonSameLabel(const JsonNode
*p1
, const JsonNode
*p2
){
1689 if( p1
->jnFlags
& JNODE_RAW
){
1690 return jsonLabelCompare(p2
, p1
->u
.zJContent
, p1
->n
);
1691 }else if( p2
->jnFlags
& JNODE_RAW
){
1692 return jsonLabelCompare(p1
, p2
->u
.zJContent
, p2
->n
);
1694 return p1
->n
==p2
->n
&& strncmp(p1
->u
.zJContent
,p2
->u
.zJContent
,p1
->n
)==0;
1698 /* forward declaration */
1699 static JsonNode
*jsonLookupAppend(JsonParse
*,const char*,int*,const char**);
1702 ** Search along zPath to find the node specified. Return a pointer
1703 ** to that node, or NULL if zPath is malformed or if there is no such
1706 ** If pApnd!=0, then try to append new nodes to complete zPath if it is
1707 ** possible to do so and if no existing node corresponds to zPath. If
1708 ** new nodes are appended *pApnd is set to 1.
1710 static JsonNode
*jsonLookupStep(
1711 JsonParse
*pParse
, /* The JSON to search */
1712 u32 iRoot
, /* Begin the search at this node */
1713 const char *zPath
, /* The path to search */
1714 int *pApnd
, /* Append nodes to complete path if not NULL */
1715 const char **pzErr
/* Make *pzErr point to any syntax error in zPath */
1719 JsonNode
*pRoot
= &pParse
->aNode
[iRoot
];
1720 if( zPath
[0]==0 ) return pRoot
;
1721 if( pRoot
->jnFlags
& JNODE_REPLACE
) return 0;
1722 if( zPath
[0]=='.' ){
1723 if( pRoot
->eType
!=JSON_OBJECT
) return 0;
1725 if( zPath
[0]=='"' ){
1727 for(i
=1; zPath
[i
] && zPath
[i
]!='"'; i
++){}
1735 testcase( nKey
==0 );
1738 for(i
=0; zPath
[i
] && zPath
[i
]!='.' && zPath
[i
]!='['; i
++){}
1747 while( j
<=pRoot
->n
){
1748 if( jsonLabelCompare(pRoot
+j
, zKey
, nKey
) ){
1749 return jsonLookupStep(pParse
, iRoot
+j
+1, &zPath
[i
], pApnd
, pzErr
);
1752 j
+= jsonNodeSize(&pRoot
[j
]);
1754 if( (pRoot
->jnFlags
& JNODE_APPEND
)==0 ) break;
1755 assert( pRoot
->eU
==2 );
1756 iRoot
+= pRoot
->u
.iAppend
;
1757 pRoot
= &pParse
->aNode
[iRoot
];
1763 iStart
= jsonParseAddNode(pParse
, JSON_OBJECT
, 2, 0);
1764 iLabel
= jsonParseAddNode(pParse
, JSON_STRING
, nKey
, zKey
);
1766 pNode
= jsonLookupAppend(pParse
, zPath
, pApnd
, pzErr
);
1767 if( pParse
->oom
) return 0;
1769 pRoot
= &pParse
->aNode
[iRoot
];
1770 assert( pRoot
->eU
==0 );
1771 pRoot
->u
.iAppend
= iStart
- iRoot
;
1772 pRoot
->jnFlags
|= JNODE_APPEND
;
1773 VVA( pRoot
->eU
= 2 );
1774 pParse
->aNode
[iLabel
].jnFlags
|= JNODE_RAW
;
1778 }else if( zPath
[0]=='[' ){
1781 while( sqlite3Isdigit(zPath
[j
]) ){
1782 i
= i
*10 + zPath
[j
] - '0';
1785 if( j
<2 || zPath
[j
]!=']' ){
1786 if( zPath
[1]=='#' ){
1787 JsonNode
*pBase
= pRoot
;
1789 if( pRoot
->eType
!=JSON_ARRAY
) return 0;
1791 while( j
<=pBase
->n
){
1792 if( (pBase
[j
].jnFlags
& JNODE_REMOVE
)==0 ) i
++;
1793 j
+= jsonNodeSize(&pBase
[j
]);
1795 if( (pBase
->jnFlags
& JNODE_APPEND
)==0 ) break;
1796 assert( pBase
->eU
==2 );
1797 iBase
+= pBase
->u
.iAppend
;
1798 pBase
= &pParse
->aNode
[iBase
];
1802 if( zPath
[2]=='-' && sqlite3Isdigit(zPath
[3]) ){
1806 x
= x
*10 + zPath
[j
] - '0';
1808 }while( sqlite3Isdigit(zPath
[j
]) );
1812 if( zPath
[j
]!=']' ){
1821 if( pRoot
->eType
!=JSON_ARRAY
) return 0;
1825 while( j
<=pRoot
->n
&& (i
>0 || (pRoot
[j
].jnFlags
& JNODE_REMOVE
)!=0) ){
1826 if( (pRoot
[j
].jnFlags
& JNODE_REMOVE
)==0 ) i
--;
1827 j
+= jsonNodeSize(&pRoot
[j
]);
1829 if( (pRoot
->jnFlags
& JNODE_APPEND
)==0 ) break;
1830 assert( pRoot
->eU
==2 );
1831 iRoot
+= pRoot
->u
.iAppend
;
1832 pRoot
= &pParse
->aNode
[iRoot
];
1836 return jsonLookupStep(pParse
, iRoot
+j
, zPath
, pApnd
, pzErr
);
1838 if( i
==0 && pApnd
){
1841 iStart
= jsonParseAddNode(pParse
, JSON_ARRAY
, 1, 0);
1842 pNode
= jsonLookupAppend(pParse
, zPath
, pApnd
, pzErr
);
1843 if( pParse
->oom
) return 0;
1845 pRoot
= &pParse
->aNode
[iRoot
];
1846 assert( pRoot
->eU
==0 );
1847 pRoot
->u
.iAppend
= iStart
- iRoot
;
1848 pRoot
->jnFlags
|= JNODE_APPEND
;
1849 VVA( pRoot
->eU
= 2 );
1860 ** Append content to pParse that will complete zPath. Return a pointer
1861 ** to the inserted node, or return NULL if the append fails.
1863 static JsonNode
*jsonLookupAppend(
1864 JsonParse
*pParse
, /* Append content to the JSON parse */
1865 const char *zPath
, /* Description of content to append */
1866 int *pApnd
, /* Set this flag to 1 */
1867 const char **pzErr
/* Make this point to any syntax error */
1871 jsonParseAddNode(pParse
, JSON_NULL
, 0, 0);
1872 return pParse
->oom
? 0 : &pParse
->aNode
[pParse
->nNode
-1];
1874 if( zPath
[0]=='.' ){
1875 jsonParseAddNode(pParse
, JSON_OBJECT
, 0, 0);
1876 }else if( strncmp(zPath
,"[0]",3)==0 ){
1877 jsonParseAddNode(pParse
, JSON_ARRAY
, 0, 0);
1881 if( pParse
->oom
) return 0;
1882 return jsonLookupStep(pParse
, pParse
->nNode
-1, zPath
, pApnd
, pzErr
);
1886 ** Return the text of a syntax error message on a JSON path. Space is
1887 ** obtained from sqlite3_malloc().
1889 static char *jsonPathSyntaxError(const char *zErr
){
1890 return sqlite3_mprintf("JSON path error near '%q'", zErr
);
1894 ** Do a node lookup using zPath. Return a pointer to the node on success.
1895 ** Return NULL if not found or if there is an error.
1897 ** On an error, write an error message into pCtx and increment the
1898 ** pParse->nErr counter.
1900 ** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
1901 ** nodes are appended.
1903 static JsonNode
*jsonLookup(
1904 JsonParse
*pParse
, /* The JSON to search */
1905 const char *zPath
, /* The path to search */
1906 int *pApnd
, /* Append nodes to complete path if not NULL */
1907 sqlite3_context
*pCtx
/* Report errors here, if not NULL */
1909 const char *zErr
= 0;
1910 JsonNode
*pNode
= 0;
1913 if( zPath
==0 ) return 0;
1914 if( zPath
[0]!='$' ){
1919 pNode
= jsonLookupStep(pParse
, 0, zPath
, pApnd
, &zErr
);
1920 if( zErr
==0 ) return pNode
;
1924 assert( zErr
!=0 && pCtx
!=0 );
1925 zMsg
= jsonPathSyntaxError(zErr
);
1927 sqlite3_result_error(pCtx
, zMsg
, -1);
1930 sqlite3_result_error_nomem(pCtx
);
1937 ** Report the wrong number of arguments for json_insert(), json_replace()
1940 static void jsonWrongNumArgs(
1941 sqlite3_context
*pCtx
,
1942 const char *zFuncName
1944 char *zMsg
= sqlite3_mprintf("json_%s() needs an odd number of arguments",
1946 sqlite3_result_error(pCtx
, zMsg
, -1);
1951 ** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
1953 static void jsonRemoveAllNulls(JsonNode
*pNode
){
1955 assert( pNode
->eType
==JSON_OBJECT
);
1957 for(i
=2; i
<=n
; i
+= jsonNodeSize(&pNode
[i
])+1){
1958 switch( pNode
[i
].eType
){
1960 pNode
[i
].jnFlags
|= JNODE_REMOVE
;
1963 jsonRemoveAllNulls(&pNode
[i
]);
1970 /****************************************************************************
1971 ** SQL functions used for testing and debugging
1972 ****************************************************************************/
1976 ** The json_parse(JSON) function returns a string which describes
1977 ** a parse of the JSON provided. Or it returns NULL if JSON is not
1980 static void jsonParseFunc(
1981 sqlite3_context
*ctx
,
1983 sqlite3_value
**argv
1985 JsonString s
; /* Output string - not real JSON */
1986 JsonParse x
; /* The parse */
1990 if( jsonParse(&x
, ctx
, (const char*)sqlite3_value_text(argv
[0])) ) return;
1991 jsonParseFindParents(&x
);
1993 for(i
=0; i
<x
.nNode
; i
++){
1995 if( x
.aNode
[i
].jnFlags
& JNODE_LABEL
){
1996 assert( x
.aNode
[i
].eType
==JSON_STRING
);
1999 zType
= jsonType
[x
.aNode
[i
].eType
];
2001 jsonPrintf(100, &s
,"node %3u: %7s n=%-4d up=%-4d",
2002 i
, zType
, x
.aNode
[i
].n
, x
.aUp
[i
]);
2003 assert( x
.aNode
[i
].eU
==0 || x
.aNode
[i
].eU
==1 );
2004 if( x
.aNode
[i
].u
.zJContent
!=0 ){
2005 assert( x
.aNode
[i
].eU
==1 );
2006 jsonAppendRaw(&s
, " ", 1);
2007 jsonAppendRaw(&s
, x
.aNode
[i
].u
.zJContent
, x
.aNode
[i
].n
);
2009 assert( x
.aNode
[i
].eU
==0 );
2011 jsonAppendRaw(&s
, "\n", 1);
2018 ** The json_test1(JSON) function return true (1) if the input is JSON
2019 ** text generated by another json function. It returns (0) if the input
2020 ** is not known to be JSON.
2022 static void jsonTest1Func(
2023 sqlite3_context
*ctx
,
2025 sqlite3_value
**argv
2027 UNUSED_PARAMETER(argc
);
2028 sqlite3_result_int(ctx
, sqlite3_value_subtype(argv
[0])==JSON_SUBTYPE
);
2030 #endif /* SQLITE_DEBUG */
2032 /****************************************************************************
2033 ** Scalar SQL function implementations
2034 ****************************************************************************/
2037 ** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
2038 ** corresponding to the SQL value input. Mostly this means putting
2039 ** double-quotes around strings and returning the unquoted string "null"
2040 ** when given a NULL input.
2042 static void jsonQuoteFunc(
2043 sqlite3_context
*ctx
,
2045 sqlite3_value
**argv
2048 UNUSED_PARAMETER(argc
);
2051 jsonAppendValue(&jx
, argv
[0]);
2053 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
2057 ** Implementation of the json_array(VALUE,...) function. Return a JSON
2058 ** array that contains all values given in arguments. Or if any argument
2059 ** is a BLOB, throw an error.
2061 static void jsonArrayFunc(
2062 sqlite3_context
*ctx
,
2064 sqlite3_value
**argv
2070 jsonAppendChar(&jx
, '[');
2071 for(i
=0; i
<argc
; i
++){
2072 jsonAppendSeparator(&jx
);
2073 jsonAppendValue(&jx
, argv
[i
]);
2075 jsonAppendChar(&jx
, ']');
2077 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
2082 ** json_array_length(JSON)
2083 ** json_array_length(JSON, PATH)
2085 ** Return the number of elements in the top-level JSON array.
2086 ** Return 0 if the input is not a well-formed JSON array.
2088 static void jsonArrayLengthFunc(
2089 sqlite3_context
*ctx
,
2091 sqlite3_value
**argv
2093 JsonParse
*p
; /* The parse */
2094 sqlite3_int64 n
= 0;
2098 p
= jsonParseCached(ctx
, argv
, ctx
);
2102 const char *zPath
= (const char*)sqlite3_value_text(argv
[1]);
2103 pNode
= jsonLookup(p
, zPath
, 0, ctx
);
2110 if( pNode
->eType
==JSON_ARRAY
){
2111 assert( (pNode
->jnFlags
& JNODE_APPEND
)==0 );
2112 for(i
=1; i
<=pNode
->n
; n
++){
2113 i
+= jsonNodeSize(&pNode
[i
]);
2116 sqlite3_result_int64(ctx
, n
);
2120 ** Bit values for the flags passed into jsonExtractFunc() or
2121 ** jsonSetFunc() via the user-data value.
2123 #define JSON_JSON 0x01 /* Result is always JSON */
2124 #define JSON_SQL 0x02 /* Result is always SQL */
2125 #define JSON_ABPATH 0x03 /* Allow abbreviated JSON path specs */
2126 #define JSON_ISSET 0x04 /* json_set(), not json_insert() */
2129 ** json_extract(JSON, PATH, ...)
2133 ** Return the element described by PATH. Return NULL if that PATH element
2136 ** If JSON_JSON is set or if more that one PATH argument is supplied then
2137 ** always return a JSON representation of the result. If JSON_SQL is set,
2138 ** then always return an SQL representation of the result. If neither flag
2139 ** is present and argc==2, then return JSON for objects and arrays and SQL
2140 ** for all other values.
2142 ** When multiple PATH arguments are supplied, the result is a JSON array
2143 ** containing the result of each PATH.
2145 ** Abbreviated JSON path expressions are allows if JSON_ABPATH, for
2146 ** compatibility with PG.
2148 static void jsonExtractFunc(
2149 sqlite3_context
*ctx
,
2151 sqlite3_value
**argv
2153 JsonParse
*p
; /* The parse */
2156 int flags
= SQLITE_PTR_TO_INT(sqlite3_user_data(ctx
));
2159 if( argc
<2 ) return;
2160 p
= jsonParseCached(ctx
, argv
, ctx
);
2163 /* With a single PATH argument */
2164 zPath
= (const char*)sqlite3_value_text(argv
[1]);
2165 if( zPath
==0 ) return;
2166 if( flags
& JSON_ABPATH
){
2167 if( zPath
[0]!='$' || (zPath
[1]!='.' && zPath
[1]!='[' && zPath
[1]!=0) ){
2168 /* The -> and ->> operators accept abbreviated PATH arguments. This
2169 ** is mostly for compatibility with PostgreSQL, but also for
2172 ** NUMBER ==> $[NUMBER] // PG compatible
2173 ** LABEL ==> $.LABEL // PG compatible
2174 ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience
2177 if( sqlite3Isdigit(zPath
[0]) ){
2178 jsonAppendRaw(&jx
, "$[", 2);
2179 jsonAppendRaw(&jx
, zPath
, (int)strlen(zPath
));
2180 jsonAppendRaw(&jx
, "]", 2);
2182 jsonAppendRaw(&jx
, "$.", 1 + (zPath
[0]!='['));
2183 jsonAppendRaw(&jx
, zPath
, (int)strlen(zPath
));
2184 jsonAppendChar(&jx
, 0);
2186 pNode
= jx
.bErr
? 0 : jsonLookup(p
, jx
.zBuf
, 0, ctx
);
2189 pNode
= jsonLookup(p
, zPath
, 0, ctx
);
2192 if( flags
& JSON_JSON
){
2193 jsonReturnJson(pNode
, ctx
, 0);
2195 jsonReturn(pNode
, ctx
, 0);
2196 sqlite3_result_subtype(ctx
, 0);
2200 pNode
= jsonLookup(p
, zPath
, 0, ctx
);
2201 if( p
->nErr
==0 && pNode
) jsonReturn(pNode
, ctx
, 0);
2204 /* Two or more PATH arguments results in a JSON array with each
2205 ** element of the array being the value selected by one of the PATHs */
2208 jsonAppendChar(&jx
, '[');
2209 for(i
=1; i
<argc
; i
++){
2210 zPath
= (const char*)sqlite3_value_text(argv
[i
]);
2211 pNode
= jsonLookup(p
, zPath
, 0, ctx
);
2212 if( p
->nErr
) break;
2213 jsonAppendSeparator(&jx
);
2215 jsonRenderNode(pNode
, &jx
, 0);
2217 jsonAppendRaw(&jx
, "null", 4);
2221 jsonAppendChar(&jx
, ']');
2223 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
2229 /* This is the RFC 7396 MergePatch algorithm.
2231 static JsonNode
*jsonMergePatch(
2232 JsonParse
*pParse
, /* The JSON parser that contains the TARGET */
2233 u32 iTarget
, /* Node of the TARGET in pParse */
2234 JsonNode
*pPatch
/* The PATCH */
2239 if( pPatch
->eType
!=JSON_OBJECT
){
2242 assert( iTarget
<pParse
->nNode
);
2243 pTarget
= &pParse
->aNode
[iTarget
];
2244 assert( (pPatch
->jnFlags
& JNODE_APPEND
)==0 );
2245 if( pTarget
->eType
!=JSON_OBJECT
){
2246 jsonRemoveAllNulls(pPatch
);
2250 for(i
=1; i
<pPatch
->n
; i
+= jsonNodeSize(&pPatch
[i
+1])+1){
2253 assert( pPatch
[i
].eType
==JSON_STRING
);
2254 assert( pPatch
[i
].jnFlags
& JNODE_LABEL
);
2255 assert( pPatch
[i
].eU
==1 );
2257 zKey
= pPatch
[i
].u
.zJContent
;
2258 for(j
=1; j
<pTarget
->n
; j
+= jsonNodeSize(&pTarget
[j
+1])+1 ){
2259 assert( pTarget
[j
].eType
==JSON_STRING
);
2260 assert( pTarget
[j
].jnFlags
& JNODE_LABEL
);
2261 if( jsonSameLabel(&pPatch
[i
], &pTarget
[j
]) ){
2262 if( pTarget
[j
+1].jnFlags
& (JNODE_REMOVE
|JNODE_PATCH
) ) break;
2263 if( pPatch
[i
+1].eType
==JSON_NULL
){
2264 pTarget
[j
+1].jnFlags
|= JNODE_REMOVE
;
2266 JsonNode
*pNew
= jsonMergePatch(pParse
, iTarget
+j
+1, &pPatch
[i
+1]);
2267 if( pNew
==0 ) return 0;
2268 pTarget
= &pParse
->aNode
[iTarget
];
2269 if( pNew
!=&pTarget
[j
+1] ){
2270 assert( pTarget
[j
+1].eU
==0
2271 || pTarget
[j
+1].eU
==1
2272 || pTarget
[j
+1].eU
==2 );
2273 testcase( pTarget
[j
+1].eU
==1 );
2274 testcase( pTarget
[j
+1].eU
==2 );
2275 VVA( pTarget
[j
+1].eU
= 5 );
2276 pTarget
[j
+1].u
.pPatch
= pNew
;
2277 pTarget
[j
+1].jnFlags
|= JNODE_PATCH
;
2283 if( j
>=pTarget
->n
&& pPatch
[i
+1].eType
!=JSON_NULL
){
2285 iStart
= jsonParseAddNode(pParse
, JSON_OBJECT
, 2, 0);
2286 jsonParseAddNode(pParse
, JSON_STRING
, nKey
, zKey
);
2287 iPatch
= jsonParseAddNode(pParse
, JSON_TRUE
, 0, 0);
2288 if( pParse
->oom
) return 0;
2289 jsonRemoveAllNulls(pPatch
);
2290 pTarget
= &pParse
->aNode
[iTarget
];
2291 assert( pParse
->aNode
[iRoot
].eU
==0 || pParse
->aNode
[iRoot
].eU
==2 );
2292 testcase( pParse
->aNode
[iRoot
].eU
==2 );
2293 pParse
->aNode
[iRoot
].jnFlags
|= JNODE_APPEND
;
2294 VVA( pParse
->aNode
[iRoot
].eU
= 2 );
2295 pParse
->aNode
[iRoot
].u
.iAppend
= iStart
- iRoot
;
2297 assert( pParse
->aNode
[iPatch
].eU
==0 );
2298 VVA( pParse
->aNode
[iPatch
].eU
= 5 );
2299 pParse
->aNode
[iPatch
].jnFlags
|= JNODE_PATCH
;
2300 pParse
->aNode
[iPatch
].u
.pPatch
= &pPatch
[i
+1];
2307 ** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
2308 ** object that is the result of running the RFC 7396 MergePatch() algorithm
2309 ** on the two arguments.
2311 static void jsonPatchFunc(
2312 sqlite3_context
*ctx
,
2314 sqlite3_value
**argv
2316 JsonParse x
; /* The JSON that is being patched */
2317 JsonParse y
; /* The patch */
2318 JsonNode
*pResult
; /* The result of the merge */
2320 UNUSED_PARAMETER(argc
);
2321 if( jsonParse(&x
, ctx
, (const char*)sqlite3_value_text(argv
[0])) ) return;
2322 if( jsonParse(&y
, ctx
, (const char*)sqlite3_value_text(argv
[1])) ){
2326 pResult
= jsonMergePatch(&x
, 0, y
.aNode
);
2327 assert( pResult
!=0 || x
.oom
);
2329 jsonReturnJson(pResult
, ctx
, 0);
2331 sqlite3_result_error_nomem(ctx
);
2339 ** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
2340 ** object that contains all name/value given in arguments. Or if any name
2341 ** is not a string or if any value is a BLOB, throw an error.
2343 static void jsonObjectFunc(
2344 sqlite3_context
*ctx
,
2346 sqlite3_value
**argv
2354 sqlite3_result_error(ctx
, "json_object() requires an even number "
2355 "of arguments", -1);
2359 jsonAppendChar(&jx
, '{');
2360 for(i
=0; i
<argc
; i
+=2){
2361 if( sqlite3_value_type(argv
[i
])!=SQLITE_TEXT
){
2362 sqlite3_result_error(ctx
, "json_object() labels must be TEXT", -1);
2366 jsonAppendSeparator(&jx
);
2367 z
= (const char*)sqlite3_value_text(argv
[i
]);
2368 n
= (u32
)sqlite3_value_bytes(argv
[i
]);
2369 jsonAppendString(&jx
, z
, n
);
2370 jsonAppendChar(&jx
, ':');
2371 jsonAppendValue(&jx
, argv
[i
+1]);
2373 jsonAppendChar(&jx
, '}');
2375 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
2380 ** json_remove(JSON, PATH, ...)
2382 ** Remove the named elements from JSON and return the result. malformed
2383 ** JSON or PATH arguments result in an error.
2385 static void jsonRemoveFunc(
2386 sqlite3_context
*ctx
,
2388 sqlite3_value
**argv
2390 JsonParse x
; /* The parse */
2395 if( argc
<1 ) return;
2396 if( jsonParse(&x
, ctx
, (const char*)sqlite3_value_text(argv
[0])) ) return;
2398 for(i
=1; i
<(u32
)argc
; i
++){
2399 zPath
= (const char*)sqlite3_value_text(argv
[i
]);
2400 if( zPath
==0 ) goto remove_done
;
2401 pNode
= jsonLookup(&x
, zPath
, 0, ctx
);
2402 if( x
.nErr
) goto remove_done
;
2403 if( pNode
) pNode
->jnFlags
|= JNODE_REMOVE
;
2405 if( (x
.aNode
[0].jnFlags
& JNODE_REMOVE
)==0 ){
2406 jsonReturnJson(x
.aNode
, ctx
, 0);
2413 ** json_replace(JSON, PATH, VALUE, ...)
2415 ** Replace the value at PATH with VALUE. If PATH does not already exist,
2416 ** this routine is a no-op. If JSON or PATH is malformed, throw an error.
2418 static void jsonReplaceFunc(
2419 sqlite3_context
*ctx
,
2421 sqlite3_value
**argv
2423 JsonParse x
; /* The parse */
2428 if( argc
<1 ) return;
2430 jsonWrongNumArgs(ctx
, "replace");
2433 if( jsonParse(&x
, ctx
, (const char*)sqlite3_value_text(argv
[0])) ) return;
2435 for(i
=1; i
<(u32
)argc
; i
+=2){
2436 zPath
= (const char*)sqlite3_value_text(argv
[i
]);
2437 pNode
= jsonLookup(&x
, zPath
, 0, ctx
);
2438 if( x
.nErr
) goto replace_err
;
2440 assert( pNode
->eU
==0 || pNode
->eU
==1 || pNode
->eU
==4 );
2441 testcase( pNode
->eU
!=0 && pNode
->eU
!=1 );
2442 pNode
->jnFlags
|= (u8
)JNODE_REPLACE
;
2443 VVA( pNode
->eU
= 4 );
2444 pNode
->u
.iReplace
= i
+ 1;
2447 if( x
.aNode
[0].jnFlags
& JNODE_REPLACE
){
2448 assert( x
.aNode
[0].eU
==4 );
2449 sqlite3_result_value(ctx
, argv
[x
.aNode
[0].u
.iReplace
]);
2451 jsonReturnJson(x
.aNode
, ctx
, argv
);
2459 ** json_set(JSON, PATH, VALUE, ...)
2461 ** Set the value at PATH to VALUE. Create the PATH if it does not already
2462 ** exist. Overwrite existing values that do exist.
2463 ** If JSON or PATH is malformed, throw an error.
2465 ** json_insert(JSON, PATH, VALUE, ...)
2467 ** Create PATH and initialize it to VALUE. If PATH already exists, this
2468 ** routine is a no-op. If JSON or PATH is malformed, throw an error.
2470 static void jsonSetFunc(
2471 sqlite3_context
*ctx
,
2473 sqlite3_value
**argv
2475 JsonParse x
; /* The parse */
2480 int bIsSet
= sqlite3_user_data(ctx
)!=0;
2482 if( argc
<1 ) return;
2484 jsonWrongNumArgs(ctx
, bIsSet
? "set" : "insert");
2487 if( jsonParse(&x
, ctx
, (const char*)sqlite3_value_text(argv
[0])) ) return;
2489 for(i
=1; i
<(u32
)argc
; i
+=2){
2490 zPath
= (const char*)sqlite3_value_text(argv
[i
]);
2492 pNode
= jsonLookup(&x
, zPath
, &bApnd
, ctx
);
2494 sqlite3_result_error_nomem(ctx
);
2498 }else if( pNode
&& (bApnd
|| bIsSet
) ){
2499 testcase( pNode
->eU
!=0 && pNode
->eU
!=1 );
2500 assert( pNode
->eU
!=3 && pNode
->eU
!=5 );
2501 VVA( pNode
->eU
= 4 );
2502 pNode
->jnFlags
|= (u8
)JNODE_REPLACE
;
2503 pNode
->u
.iReplace
= i
+ 1;
2506 if( x
.aNode
[0].jnFlags
& JNODE_REPLACE
){
2507 assert( x
.aNode
[0].eU
==4 );
2508 sqlite3_result_value(ctx
, argv
[x
.aNode
[0].u
.iReplace
]);
2510 jsonReturnJson(x
.aNode
, ctx
, argv
);
2518 ** json_type(JSON, PATH)
2520 ** Return the top-level "type" of a JSON string. json_type() raises an
2521 ** error if either the JSON or PATH inputs are not well-formed.
2523 static void jsonTypeFunc(
2524 sqlite3_context
*ctx
,
2526 sqlite3_value
**argv
2528 JsonParse
*p
; /* The parse */
2532 p
= jsonParseCached(ctx
, argv
, ctx
);
2535 zPath
= (const char*)sqlite3_value_text(argv
[1]);
2536 pNode
= jsonLookup(p
, zPath
, 0, ctx
);
2541 sqlite3_result_text(ctx
, jsonType
[pNode
->eType
], -1, SQLITE_STATIC
);
2548 ** Return 1 if JSON is a well-formed canonical JSON string according
2549 ** to RFC-7159. Return 0 otherwise.
2551 static void jsonValidFunc(
2552 sqlite3_context
*ctx
,
2554 sqlite3_value
**argv
2556 JsonParse
*p
; /* The parse */
2557 UNUSED_PARAMETER(argc
);
2558 if( sqlite3_value_type(argv
[0])==SQLITE_NULL
) return;
2559 p
= jsonParseCached(ctx
, argv
, 0);
2560 if( p
==0 || p
->oom
){
2561 sqlite3_result_error_nomem(ctx
);
2564 sqlite3_result_int(ctx
, p
->nErr
==0 && p
->hasNonstd
==0);
2565 if( p
->nErr
) jsonParseFree(p
);
2570 ** json_error_position(JSON)
2572 ** If the argument is not an interpretable JSON string, then return the 1-based
2573 ** character position at which the parser first recognized that the input
2574 ** was in error. The left-most character is 1. If the string is valid
2575 ** JSON, then return 0.
2577 ** Note that json_valid() is only true for strictly conforming canonical JSON.
2578 ** But this routine returns zero if the input contains extension. Thus:
2580 ** (1) If the input X is strictly conforming canonical JSON:
2582 ** json_valid(X) returns true
2583 ** json_error_position(X) returns 0
2585 ** (2) If the input X is JSON but it includes extension (such as JSON5) that
2586 ** are not part of RFC-8259:
2588 ** json_valid(X) returns false
2589 ** json_error_position(X) return 0
2591 ** (3) If the input X cannot be interpreted as JSON even taking extensions
2594 ** json_valid(X) return false
2595 ** json_error_position(X) returns 1 or more
2597 static void jsonErrorFunc(
2598 sqlite3_context
*ctx
,
2600 sqlite3_value
**argv
2602 JsonParse
*p
; /* The parse */
2603 UNUSED_PARAMETER(argc
);
2604 if( sqlite3_value_type(argv
[0])==SQLITE_NULL
) return;
2605 p
= jsonParseCached(ctx
, argv
, 0);
2606 if( p
==0 || p
->oom
){
2607 sqlite3_result_error_nomem(ctx
);
2609 }else if( p
->nErr
==0 ){
2610 sqlite3_result_int(ctx
, 0);
2614 const char *z
= p
->zJson
;
2615 for(i
=0; i
<p
->iErr
&& ALWAYS(z
[i
]); i
++){
2616 if( (z
[i
]&0xc0)!=0x80 ) n
++;
2618 sqlite3_result_int(ctx
, n
);
2624 /****************************************************************************
2625 ** Aggregate SQL function implementations
2626 ****************************************************************************/
2628 ** json_group_array(VALUE)
2630 ** Return a JSON array composed of all values in the aggregate.
2632 static void jsonArrayStep(
2633 sqlite3_context
*ctx
,
2635 sqlite3_value
**argv
2638 UNUSED_PARAMETER(argc
);
2639 pStr
= (JsonString
*)sqlite3_aggregate_context(ctx
, sizeof(*pStr
));
2641 if( pStr
->zBuf
==0 ){
2642 jsonInit(pStr
, ctx
);
2643 jsonAppendChar(pStr
, '[');
2644 }else if( pStr
->nUsed
>1 ){
2645 jsonAppendChar(pStr
, ',');
2648 jsonAppendValue(pStr
, argv
[0]);
2651 static void jsonArrayCompute(sqlite3_context
*ctx
, int isFinal
){
2653 pStr
= (JsonString
*)sqlite3_aggregate_context(ctx
, 0);
2656 jsonAppendChar(pStr
, ']');
2658 if( pStr
->bErr
==1 ) sqlite3_result_error_nomem(ctx
);
2659 assert( pStr
->bStatic
);
2660 }else if( isFinal
){
2661 sqlite3_result_text(ctx
, pStr
->zBuf
, (int)pStr
->nUsed
,
2662 pStr
->bStatic
? SQLITE_TRANSIENT
: sqlite3_free
);
2665 sqlite3_result_text(ctx
, pStr
->zBuf
, (int)pStr
->nUsed
, SQLITE_TRANSIENT
);
2669 sqlite3_result_text(ctx
, "[]", 2, SQLITE_STATIC
);
2671 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
2673 static void jsonArrayValue(sqlite3_context
*ctx
){
2674 jsonArrayCompute(ctx
, 0);
2676 static void jsonArrayFinal(sqlite3_context
*ctx
){
2677 jsonArrayCompute(ctx
, 1);
2680 #ifndef SQLITE_OMIT_WINDOWFUNC
2682 ** This method works for both json_group_array() and json_group_object().
2683 ** It works by removing the first element of the group by searching forward
2684 ** to the first comma (",") that is not within a string and deleting all
2685 ** text through that comma.
2687 static void jsonGroupInverse(
2688 sqlite3_context
*ctx
,
2690 sqlite3_value
**argv
2698 UNUSED_PARAMETER(argc
);
2699 UNUSED_PARAMETER(argv
);
2700 pStr
= (JsonString
*)sqlite3_aggregate_context(ctx
, 0);
2702 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
2703 ** always have been called to initalize it */
2704 if( NEVER(!pStr
) ) return;
2707 for(i
=1; i
<pStr
->nUsed
&& ((c
= z
[i
])!=',' || inStr
|| nNest
); i
++){
2710 }else if( c
=='\\' ){
2713 if( c
=='{' || c
=='[' ) nNest
++;
2714 if( c
=='}' || c
==']' ) nNest
--;
2717 if( i
<pStr
->nUsed
){
2719 memmove(&z
[1], &z
[i
+1], (size_t)pStr
->nUsed
-1);
2726 # define jsonGroupInverse 0
2731 ** json_group_obj(NAME,VALUE)
2733 ** Return a JSON object composed of all names and values in the aggregate.
2735 static void jsonObjectStep(
2736 sqlite3_context
*ctx
,
2738 sqlite3_value
**argv
2743 UNUSED_PARAMETER(argc
);
2744 pStr
= (JsonString
*)sqlite3_aggregate_context(ctx
, sizeof(*pStr
));
2746 if( pStr
->zBuf
==0 ){
2747 jsonInit(pStr
, ctx
);
2748 jsonAppendChar(pStr
, '{');
2749 }else if( pStr
->nUsed
>1 ){
2750 jsonAppendChar(pStr
, ',');
2753 z
= (const char*)sqlite3_value_text(argv
[0]);
2754 n
= (u32
)sqlite3_value_bytes(argv
[0]);
2755 jsonAppendString(pStr
, z
, n
);
2756 jsonAppendChar(pStr
, ':');
2757 jsonAppendValue(pStr
, argv
[1]);
2760 static void jsonObjectCompute(sqlite3_context
*ctx
, int isFinal
){
2762 pStr
= (JsonString
*)sqlite3_aggregate_context(ctx
, 0);
2764 jsonAppendChar(pStr
, '}');
2766 if( pStr
->bErr
==1 ) sqlite3_result_error_nomem(ctx
);
2767 assert( pStr
->bStatic
);
2768 }else if( isFinal
){
2769 sqlite3_result_text(ctx
, pStr
->zBuf
, (int)pStr
->nUsed
,
2770 pStr
->bStatic
? SQLITE_TRANSIENT
: sqlite3_free
);
2773 sqlite3_result_text(ctx
, pStr
->zBuf
, (int)pStr
->nUsed
, SQLITE_TRANSIENT
);
2777 sqlite3_result_text(ctx
, "{}", 2, SQLITE_STATIC
);
2779 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
2781 static void jsonObjectValue(sqlite3_context
*ctx
){
2782 jsonObjectCompute(ctx
, 0);
2784 static void jsonObjectFinal(sqlite3_context
*ctx
){
2785 jsonObjectCompute(ctx
, 1);
2790 #ifndef SQLITE_OMIT_VIRTUALTABLE
2791 /****************************************************************************
2792 ** The json_each virtual table
2793 ****************************************************************************/
2794 typedef struct JsonEachCursor JsonEachCursor
;
2795 struct JsonEachCursor
{
2796 sqlite3_vtab_cursor base
; /* Base class - must be first */
2797 u32 iRowid
; /* The rowid */
2798 u32 iBegin
; /* The first node of the scan */
2799 u32 i
; /* Index in sParse.aNode[] of current row */
2800 u32 iEnd
; /* EOF when i equals or exceeds this value */
2801 u8 eType
; /* Type of top-level element */
2802 u8 bRecursive
; /* True for json_tree(). False for json_each() */
2803 char *zJson
; /* Input JSON */
2804 char *zRoot
; /* Path by which to filter zJson */
2805 JsonParse sParse
; /* Parse of the input JSON */
2808 /* Constructor for the json_each virtual table */
2809 static int jsonEachConnect(
2812 int argc
, const char *const*argv
,
2813 sqlite3_vtab
**ppVtab
,
2819 /* Column numbers */
2821 #define JEACH_VALUE 1
2822 #define JEACH_TYPE 2
2823 #define JEACH_ATOM 3
2825 #define JEACH_PARENT 5
2826 #define JEACH_FULLKEY 6
2827 #define JEACH_PATH 7
2828 /* The xBestIndex method assumes that the JSON and ROOT columns are
2829 ** the last two columns in the table. Should this ever changes, be
2830 ** sure to update the xBestIndex method. */
2831 #define JEACH_JSON 8
2832 #define JEACH_ROOT 9
2834 UNUSED_PARAMETER(pzErr
);
2835 UNUSED_PARAMETER(argv
);
2836 UNUSED_PARAMETER(argc
);
2837 UNUSED_PARAMETER(pAux
);
2838 rc
= sqlite3_declare_vtab(db
,
2839 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
2840 "json HIDDEN,root HIDDEN)");
2841 if( rc
==SQLITE_OK
){
2842 pNew
= *ppVtab
= sqlite3_malloc( sizeof(*pNew
) );
2843 if( pNew
==0 ) return SQLITE_NOMEM
;
2844 memset(pNew
, 0, sizeof(*pNew
));
2845 sqlite3_vtab_config(db
, SQLITE_VTAB_INNOCUOUS
);
2850 /* destructor for json_each virtual table */
2851 static int jsonEachDisconnect(sqlite3_vtab
*pVtab
){
2852 sqlite3_free(pVtab
);
2856 /* constructor for a JsonEachCursor object for json_each(). */
2857 static int jsonEachOpenEach(sqlite3_vtab
*p
, sqlite3_vtab_cursor
**ppCursor
){
2858 JsonEachCursor
*pCur
;
2860 UNUSED_PARAMETER(p
);
2861 pCur
= sqlite3_malloc( sizeof(*pCur
) );
2862 if( pCur
==0 ) return SQLITE_NOMEM
;
2863 memset(pCur
, 0, sizeof(*pCur
));
2864 *ppCursor
= &pCur
->base
;
2868 /* constructor for a JsonEachCursor object for json_tree(). */
2869 static int jsonEachOpenTree(sqlite3_vtab
*p
, sqlite3_vtab_cursor
**ppCursor
){
2870 int rc
= jsonEachOpenEach(p
, ppCursor
);
2871 if( rc
==SQLITE_OK
){
2872 JsonEachCursor
*pCur
= (JsonEachCursor
*)*ppCursor
;
2873 pCur
->bRecursive
= 1;
2878 /* Reset a JsonEachCursor back to its original state. Free any memory
2880 static void jsonEachCursorReset(JsonEachCursor
*p
){
2881 sqlite3_free(p
->zJson
);
2882 sqlite3_free(p
->zRoot
);
2883 jsonParseReset(&p
->sParse
);
2892 /* Destructor for a jsonEachCursor object */
2893 static int jsonEachClose(sqlite3_vtab_cursor
*cur
){
2894 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
2895 jsonEachCursorReset(p
);
2900 /* Return TRUE if the jsonEachCursor object has been advanced off the end
2901 ** of the JSON object */
2902 static int jsonEachEof(sqlite3_vtab_cursor
*cur
){
2903 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
2904 return p
->i
>= p
->iEnd
;
2907 /* Advance the cursor to the next element for json_tree() */
2908 static int jsonEachNext(sqlite3_vtab_cursor
*cur
){
2909 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
2910 if( p
->bRecursive
){
2911 if( p
->sParse
.aNode
[p
->i
].jnFlags
& JNODE_LABEL
) p
->i
++;
2915 u32 iUp
= p
->sParse
.aUp
[p
->i
];
2916 JsonNode
*pUp
= &p
->sParse
.aNode
[iUp
];
2917 p
->eType
= pUp
->eType
;
2918 if( pUp
->eType
==JSON_ARRAY
){
2919 assert( pUp
->eU
==0 || pUp
->eU
==3 );
2920 testcase( pUp
->eU
==3 );
2932 p
->i
+= jsonNodeSize(&p
->sParse
.aNode
[p
->i
]);
2937 p
->i
+= 1 + jsonNodeSize(&p
->sParse
.aNode
[p
->i
+1]);
2950 /* Append an object label to the JSON Path being constructed
2953 static void jsonAppendObjectPathElement(
2959 assert( pNode
->eType
==JSON_STRING
);
2960 assert( pNode
->jnFlags
& JNODE_LABEL
);
2961 assert( pNode
->eU
==1 );
2962 z
= pNode
->u
.zJContent
;
2964 if( (pNode
->jnFlags
& JNODE_RAW
)==0 ){
2966 assert( z
[0]=='"' || z
[0]=='\'' );
2967 assert( z
[nn
-1]=='"' || z
[0]=='\'' );
2968 if( nn
>2 && sqlite3Isalpha(z
[1]) ){
2969 for(jj
=2; jj
<nn
-1 && sqlite3Isalnum(z
[jj
]); jj
++){}
2976 jsonPrintf(nn
+2, pStr
, ".%.*s", nn
, z
);
2979 /* Append the name of the path for element i to pStr
2981 static void jsonEachComputePath(
2982 JsonEachCursor
*p
, /* The cursor */
2983 JsonString
*pStr
, /* Write the path here */
2984 u32 i
/* Path to this element */
2986 JsonNode
*pNode
, *pUp
;
2989 jsonAppendChar(pStr
, '$');
2992 iUp
= p
->sParse
.aUp
[i
];
2993 jsonEachComputePath(p
, pStr
, iUp
);
2994 pNode
= &p
->sParse
.aNode
[i
];
2995 pUp
= &p
->sParse
.aNode
[iUp
];
2996 if( pUp
->eType
==JSON_ARRAY
){
2997 assert( pUp
->eU
==3 || (pUp
->eU
==0 && pUp
->u
.iKey
==0) );
2998 testcase( pUp
->eU
==0 );
2999 jsonPrintf(30, pStr
, "[%d]", pUp
->u
.iKey
);
3001 assert( pUp
->eType
==JSON_OBJECT
);
3002 if( (pNode
->jnFlags
& JNODE_LABEL
)==0 ) pNode
--;
3003 jsonAppendObjectPathElement(pStr
, pNode
);
3007 /* Return the value of a column */
3008 static int jsonEachColumn(
3009 sqlite3_vtab_cursor
*cur
, /* The cursor */
3010 sqlite3_context
*ctx
, /* First argument to sqlite3_result_...() */
3011 int i
/* Which column to return */
3013 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
3014 JsonNode
*pThis
= &p
->sParse
.aNode
[p
->i
];
3017 if( p
->i
==0 ) break;
3018 if( p
->eType
==JSON_OBJECT
){
3019 jsonReturn(pThis
, ctx
, 0);
3020 }else if( p
->eType
==JSON_ARRAY
){
3022 if( p
->bRecursive
){
3023 if( p
->iRowid
==0 ) break;
3024 assert( p
->sParse
.aNode
[p
->sParse
.aUp
[p
->i
]].eU
==3 );
3025 iKey
= p
->sParse
.aNode
[p
->sParse
.aUp
[p
->i
]].u
.iKey
;
3029 sqlite3_result_int64(ctx
, (sqlite3_int64
)iKey
);
3034 if( pThis
->jnFlags
& JNODE_LABEL
) pThis
++;
3035 jsonReturn(pThis
, ctx
, 0);
3039 if( pThis
->jnFlags
& JNODE_LABEL
) pThis
++;
3040 sqlite3_result_text(ctx
, jsonType
[pThis
->eType
], -1, SQLITE_STATIC
);
3044 if( pThis
->jnFlags
& JNODE_LABEL
) pThis
++;
3045 if( pThis
->eType
>=JSON_ARRAY
) break;
3046 jsonReturn(pThis
, ctx
, 0);
3050 sqlite3_result_int64(ctx
,
3051 (sqlite3_int64
)p
->i
+ ((pThis
->jnFlags
& JNODE_LABEL
)!=0));
3054 case JEACH_PARENT
: {
3055 if( p
->i
>p
->iBegin
&& p
->bRecursive
){
3056 sqlite3_result_int64(ctx
, (sqlite3_int64
)p
->sParse
.aUp
[p
->i
]);
3060 case JEACH_FULLKEY
: {
3063 if( p
->bRecursive
){
3064 jsonEachComputePath(p
, &x
, p
->i
);
3067 jsonAppendRaw(&x
, p
->zRoot
, (int)strlen(p
->zRoot
));
3069 jsonAppendChar(&x
, '$');
3071 if( p
->eType
==JSON_ARRAY
){
3072 jsonPrintf(30, &x
, "[%d]", p
->iRowid
);
3073 }else if( p
->eType
==JSON_OBJECT
){
3074 jsonAppendObjectPathElement(&x
, pThis
);
3081 if( p
->bRecursive
){
3084 jsonEachComputePath(p
, &x
, p
->sParse
.aUp
[p
->i
]);
3088 /* For json_each() path and root are the same so fall through
3089 ** into the root case */
3090 /* no break */ deliberate_fall_through
3093 const char *zRoot
= p
->zRoot
;
3094 if( zRoot
==0 ) zRoot
= "$";
3095 sqlite3_result_text(ctx
, zRoot
, -1, SQLITE_STATIC
);
3099 assert( i
==JEACH_JSON
);
3100 sqlite3_result_text(ctx
, p
->sParse
.zJson
, -1, SQLITE_STATIC
);
3107 /* Return the current rowid value */
3108 static int jsonEachRowid(sqlite3_vtab_cursor
*cur
, sqlite_int64
*pRowid
){
3109 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
3110 *pRowid
= p
->iRowid
;
3114 /* The query strategy is to look for an equality constraint on the json
3115 ** column. Without such a constraint, the table cannot operate. idxNum is
3116 ** 1 if the constraint is found, 3 if the constraint and zRoot are found,
3119 static int jsonEachBestIndex(
3121 sqlite3_index_info
*pIdxInfo
3123 int i
; /* Loop counter or computed array index */
3124 int aIdx
[2]; /* Index of constraints for JSON and ROOT */
3125 int unusableMask
= 0; /* Mask of unusable JSON and ROOT constraints */
3126 int idxMask
= 0; /* Mask of usable == constraints JSON and ROOT */
3127 const struct sqlite3_index_constraint
*pConstraint
;
3129 /* This implementation assumes that JSON and ROOT are the last two
3130 ** columns in the table */
3131 assert( JEACH_ROOT
== JEACH_JSON
+1 );
3132 UNUSED_PARAMETER(tab
);
3133 aIdx
[0] = aIdx
[1] = -1;
3134 pConstraint
= pIdxInfo
->aConstraint
;
3135 for(i
=0; i
<pIdxInfo
->nConstraint
; i
++, pConstraint
++){
3138 if( pConstraint
->iColumn
< JEACH_JSON
) continue;
3139 iCol
= pConstraint
->iColumn
- JEACH_JSON
;
3140 assert( iCol
==0 || iCol
==1 );
3141 testcase( iCol
==0 );
3143 if( pConstraint
->usable
==0 ){
3144 unusableMask
|= iMask
;
3145 }else if( pConstraint
->op
==SQLITE_INDEX_CONSTRAINT_EQ
){
3150 if( pIdxInfo
->nOrderBy
>0
3151 && pIdxInfo
->aOrderBy
[0].iColumn
<0
3152 && pIdxInfo
->aOrderBy
[0].desc
==0
3154 pIdxInfo
->orderByConsumed
= 1;
3157 if( (unusableMask
& ~idxMask
)!=0 ){
3158 /* If there are any unusable constraints on JSON or ROOT, then reject
3159 ** this entire plan */
3160 return SQLITE_CONSTRAINT
;
3163 /* No JSON input. Leave estimatedCost at the huge value that it was
3164 ** initialized to to discourage the query planner from selecting this
3166 pIdxInfo
->idxNum
= 0;
3168 pIdxInfo
->estimatedCost
= 1.0;
3170 pIdxInfo
->aConstraintUsage
[i
].argvIndex
= 1;
3171 pIdxInfo
->aConstraintUsage
[i
].omit
= 1;
3173 pIdxInfo
->idxNum
= 1; /* Only JSON supplied. Plan 1 */
3176 pIdxInfo
->aConstraintUsage
[i
].argvIndex
= 2;
3177 pIdxInfo
->aConstraintUsage
[i
].omit
= 1;
3178 pIdxInfo
->idxNum
= 3; /* Both JSON and ROOT are supplied. Plan 3 */
3184 /* Start a search on a new JSON string */
3185 static int jsonEachFilter(
3186 sqlite3_vtab_cursor
*cur
,
3187 int idxNum
, const char *idxStr
,
3188 int argc
, sqlite3_value
**argv
3190 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
3192 const char *zRoot
= 0;
3195 UNUSED_PARAMETER(idxStr
);
3196 UNUSED_PARAMETER(argc
);
3197 jsonEachCursorReset(p
);
3198 if( idxNum
==0 ) return SQLITE_OK
;
3199 z
= (const char*)sqlite3_value_text(argv
[0]);
3200 if( z
==0 ) return SQLITE_OK
;
3201 n
= sqlite3_value_bytes(argv
[0]);
3202 p
->zJson
= sqlite3_malloc64( n
+1 );
3203 if( p
->zJson
==0 ) return SQLITE_NOMEM
;
3204 memcpy(p
->zJson
, z
, (size_t)n
+1);
3205 if( jsonParse(&p
->sParse
, 0, p
->zJson
) ){
3206 int rc
= SQLITE_NOMEM
;
3207 if( p
->sParse
.oom
==0 ){
3208 sqlite3_free(cur
->pVtab
->zErrMsg
);
3209 cur
->pVtab
->zErrMsg
= sqlite3_mprintf("malformed JSON");
3210 if( cur
->pVtab
->zErrMsg
) rc
= SQLITE_ERROR
;
3212 jsonEachCursorReset(p
);
3214 }else if( p
->bRecursive
&& jsonParseFindParents(&p
->sParse
) ){
3215 jsonEachCursorReset(p
);
3216 return SQLITE_NOMEM
;
3218 JsonNode
*pNode
= 0;
3220 const char *zErr
= 0;
3221 zRoot
= (const char*)sqlite3_value_text(argv
[1]);
3222 if( zRoot
==0 ) return SQLITE_OK
;
3223 n
= sqlite3_value_bytes(argv
[1]);
3224 p
->zRoot
= sqlite3_malloc64( n
+1 );
3225 if( p
->zRoot
==0 ) return SQLITE_NOMEM
;
3226 memcpy(p
->zRoot
, zRoot
, (size_t)n
+1);
3227 if( zRoot
[0]!='$' ){
3230 pNode
= jsonLookupStep(&p
->sParse
, 0, p
->zRoot
+1, 0, &zErr
);
3233 sqlite3_free(cur
->pVtab
->zErrMsg
);
3234 cur
->pVtab
->zErrMsg
= jsonPathSyntaxError(zErr
);
3235 jsonEachCursorReset(p
);
3236 return cur
->pVtab
->zErrMsg
? SQLITE_ERROR
: SQLITE_NOMEM
;
3237 }else if( pNode
==0 ){
3241 pNode
= p
->sParse
.aNode
;
3243 p
->iBegin
= p
->i
= (int)(pNode
- p
->sParse
.aNode
);
3244 p
->eType
= pNode
->eType
;
3245 if( p
->eType
>=JSON_ARRAY
){
3246 assert( pNode
->eU
==0 );
3247 VVA( pNode
->eU
= 3 );
3249 p
->iEnd
= p
->i
+ pNode
->n
+ 1;
3250 if( p
->bRecursive
){
3251 p
->eType
= p
->sParse
.aNode
[p
->sParse
.aUp
[p
->i
]].eType
;
3252 if( p
->i
>0 && (p
->sParse
.aNode
[p
->i
-1].jnFlags
& JNODE_LABEL
)!=0 ){
3265 /* The methods of the json_each virtual table */
3266 static sqlite3_module jsonEachModule
= {
3269 jsonEachConnect
, /* xConnect */
3270 jsonEachBestIndex
, /* xBestIndex */
3271 jsonEachDisconnect
, /* xDisconnect */
3273 jsonEachOpenEach
, /* xOpen - open a cursor */
3274 jsonEachClose
, /* xClose - close a cursor */
3275 jsonEachFilter
, /* xFilter - configure scan constraints */
3276 jsonEachNext
, /* xNext - advance a cursor */
3277 jsonEachEof
, /* xEof - check for end of scan */
3278 jsonEachColumn
, /* xColumn - read data */
3279 jsonEachRowid
, /* xRowid - read data */
3285 0, /* xFindMethod */
3289 0, /* xRollbackTo */
3293 /* The methods of the json_tree virtual table. */
3294 static sqlite3_module jsonTreeModule
= {
3297 jsonEachConnect
, /* xConnect */
3298 jsonEachBestIndex
, /* xBestIndex */
3299 jsonEachDisconnect
, /* xDisconnect */
3301 jsonEachOpenTree
, /* xOpen - open a cursor */
3302 jsonEachClose
, /* xClose - close a cursor */
3303 jsonEachFilter
, /* xFilter - configure scan constraints */
3304 jsonEachNext
, /* xNext - advance a cursor */
3305 jsonEachEof
, /* xEof - check for end of scan */
3306 jsonEachColumn
, /* xColumn - read data */
3307 jsonEachRowid
, /* xRowid - read data */
3313 0, /* xFindMethod */
3317 0, /* xRollbackTo */
3320 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3321 #endif /* !defined(SQLITE_OMIT_JSON) */
3324 ** Register JSON functions.
3326 void sqlite3RegisterJsonFunctions(void){
3327 #ifndef SQLITE_OMIT_JSON
3328 static FuncDef aJsonFunc
[] = {
3329 JFUNCTION(json
, 1, 0, jsonRemoveFunc
),
3330 JFUNCTION(json_array
, -1, 0, jsonArrayFunc
),
3331 JFUNCTION(json_array_length
, 1, 0, jsonArrayLengthFunc
),
3332 JFUNCTION(json_array_length
, 2, 0, jsonArrayLengthFunc
),
3333 JFUNCTION(json_error_position
,1, 0, jsonErrorFunc
),
3334 JFUNCTION(json_extract
, -1, 0, jsonExtractFunc
),
3335 JFUNCTION(->, 2, JSON_JSON
, jsonExtractFunc
),
3336 JFUNCTION(->>, 2, JSON_SQL
, jsonExtractFunc
),
3337 JFUNCTION(json_insert
, -1, 0, jsonSetFunc
),
3338 JFUNCTION(json_object
, -1, 0, jsonObjectFunc
),
3339 JFUNCTION(json_patch
, 2, 0, jsonPatchFunc
),
3340 JFUNCTION(json_quote
, 1, 0, jsonQuoteFunc
),
3341 JFUNCTION(json_remove
, -1, 0, jsonRemoveFunc
),
3342 JFUNCTION(json_replace
, -1, 0, jsonReplaceFunc
),
3343 JFUNCTION(json_set
, -1, JSON_ISSET
, jsonSetFunc
),
3344 JFUNCTION(json_type
, 1, 0, jsonTypeFunc
),
3345 JFUNCTION(json_type
, 2, 0, jsonTypeFunc
),
3346 JFUNCTION(json_valid
, 1, 0, jsonValidFunc
),
3348 JFUNCTION(json_parse
, 1, 0, jsonParseFunc
),
3349 JFUNCTION(json_test1
, 1, 0, jsonTest1Func
),
3351 WAGGREGATE(json_group_array
, 1, 0, 0,
3352 jsonArrayStep
, jsonArrayFinal
, jsonArrayValue
, jsonGroupInverse
,
3353 SQLITE_SUBTYPE
|SQLITE_UTF8
|SQLITE_DETERMINISTIC
),
3354 WAGGREGATE(json_group_object
, 2, 0, 0,
3355 jsonObjectStep
, jsonObjectFinal
, jsonObjectValue
, jsonGroupInverse
,
3356 SQLITE_SUBTYPE
|SQLITE_UTF8
|SQLITE_DETERMINISTIC
)
3358 sqlite3InsertBuiltinFuncs(aJsonFunc
, ArraySize(aJsonFunc
));
3362 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
3364 ** Register the JSON table-valued functions
3366 int sqlite3JsonTableFunctions(sqlite3
*db
){
3368 static const struct {
3370 sqlite3_module
*pModule
;
3372 { "json_each", &jsonEachModule
},
3373 { "json_tree", &jsonTreeModule
},
3376 for(i
=0; i
<sizeof(aMod
)/sizeof(aMod
[0]) && rc
==SQLITE_OK
; i
++){
3377 rc
= sqlite3_create_module(db
, aMod
[i
].zName
, aMod
[i
].pModule
, 0);
3381 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) */