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 ** The original design stored all JSON as pure text, canonical RFC-8259.
19 ** Support for JSON-5 extensions was added with version 3.42.0 (2023-05-16).
20 ** All generated JSON text still conforms strictly to RFC-8259, but text
21 ** with JSON-5 extensions is accepted as input.
23 ** Beginning with version 3.45.0 (pending), these routines also accept
24 ** BLOB values that have JSON encoded using a binary representation we
25 ** call JSONB. The name JSONB comes from PostgreSQL, however the on-disk
26 ** format SQLite JSONB is completely different and incompatible with
29 ** Decoding and interpreting JSONB is still O(N) where N is the size of
30 ** the input, the same as text JSON. However, the constant of proportionality
31 ** for JSONB is much smaller due to faster parsing. The size of each
32 ** element in JSONB is encoded in its header, so there is no need to search
33 ** for delimiters using persnickety syntax rules. JSONB seems to be about
34 ** 3x faster than text JSON as a result. JSONB is also tends to be slightly
35 ** smaller than text JSON, by 5% or 10%, but there are corner cases where
36 ** JSONB can be slightly larger. So you are not far mistaken to say that
37 ** a JSONB blob is the same size as the equivalent RFC-8259 text.
40 ** THE JSONB ENCODING:
42 ** Every JSON element is encoded in JSONB as a header and a payload.
43 ** The header is between 1 and 9 bytes in size. The payload is zero
46 ** The lower 4 bits of the first byte of the header determines the
52 ** 3: INT -- RFC-8259 integer literal
53 ** 4: INT5 -- JSON5 integer literal
54 ** 5: FLOAT -- RFC-8259 floating point literal
55 ** 6: FLOAT5 -- JSON5 floating point literal
56 ** 7: TEXT -- Text literal acceptable to both SQL and JSON
57 ** 8: TEXTJ -- Text containing RFC-8259 escapes
58 ** 9: TEXT5 -- Text containing JSON5 and/or RFC-8259 escapes
59 ** 10: TEXTRAW -- Text containing unescaped syntax characters
63 ** The other three possible values (13-15) are reserved for future
66 ** The upper 4 bits of the first byte determine the size of the header
67 ** and sometimes also the size of the payload. If X is the first byte
68 ** of the element and if X>>4 is between 0 and 11, then the payload
69 ** will be that many bytes in size and the header is exactly one byte
70 ** in size. Other four values for X>>4 (12-15) indicate that the header
71 ** is more than one byte in size and that the payload size is determined
72 ** by the remainder of the header, interpreted as a unsigned big-endian
75 ** Value of X>>4 Size integer Total header size
76 ** ------------- -------------------- -----------------
77 ** 12 1 byte (0-255) 2
78 ** 13 2 byte (0-65535) 3
79 ** 14 4 byte (0-4294967295) 5
80 ** 15 8 byte (0-1.8e19) 9
82 ** The payload size need not be expressed in its minimal form. For example,
83 ** if the payload size is 10, the size can be expressed in any of 5 different
84 ** ways: (1) (X>>4)==10, (2) (X>>4)==12 following by on 0x0a byte,
85 ** (3) (X>>4)==13 followed by 0x00 and 0x0a, (4) (X>>4)==14 followed by
86 ** 0x00 0x00 0x00 0x0a, or (5) (X>>4)==15 followed by 7 bytes of 0x00 and
87 ** a single byte of 0x0a. The shorter forms are preferred, of course, but
88 ** sometimes when generating JSONB, the payload size is not known in advance
89 ** and it is convenient to reserve sufficient header space to cover the
90 ** largest possible payload size and then come back later and patch up
91 ** the size when it becomes known, resulting in a non-minimal encoding.
93 ** The value (X>>4)==15 is not actually used in the current implementation
94 ** (as SQLite is currently unable handle BLOBs larger than about 2GB)
95 ** but is included in the design to allow for future enhancements.
97 ** The payload follows the header. NULL, TRUE, and FALSE have no payload and
98 ** their payload size must always be zero. The payload for INT, INT5,
99 ** FLOAT, FLOAT5, TEXT, TEXTJ, TEXT5, and TEXTROW is text. Note that the
100 ** "..." or '...' delimiters are omitted from the various text encodings.
101 ** The payload for ARRAY and OBJECT is a list of additional elements that
102 ** are the content for the array or object. The payload for an OBJECT
103 ** must be an even number of elements. The first element of each pair is
104 ** the label and must be of type TEXT, TEXTJ, TEXT5, or TEXTRAW.
106 ** A valid JSONB blob consists of a single element, as described above.
107 ** Usually this will be an ARRAY or OBJECT element which has many more
108 ** elements as its content. But the overall blob is just a single element.
110 ** Input validation for JSONB blobs simply checks that the element type
111 ** code is between 0 and 12 and that the total size of the element
112 ** (header plus payload) is the same as the size of the BLOB. If those
113 ** checks are true, the BLOB is assumed to be JSONB and processing continues.
114 ** Errors are only raised if some other miscoding is discovered during
117 #ifndef SQLITE_OMIT_JSON
118 #include "sqliteInt.h"
120 /* JSONB element types
122 #define JSONB_NULL 0 /* "null" */
123 #define JSONB_TRUE 1 /* "true" */
124 #define JSONB_FALSE 2 /* "false" */
125 #define JSONB_INT 3 /* integer acceptable to JSON and SQL */
126 #define JSONB_INT5 4 /* integer in 0x000 notation */
127 #define JSONB_FLOAT 5 /* float acceptable to JSON and SQL */
128 #define JSONB_FLOAT5 6 /* float with JSON5 extensions */
129 #define JSONB_TEXT 7 /* Text compatible with both JSON and SQL */
130 #define JSONB_TEXTJ 8 /* Text with JSON escapes */
131 #define JSONB_TEXT5 9 /* Text with JSON-5 escape */
132 #define JSONB_TEXTRAW 10 /* SQL text that needs escaping for JSON */
133 #define JSONB_ARRAY 11 /* An array */
134 #define JSONB_OBJECT 12 /* An object */
136 /* Human-readable names for the JSONB values:
138 static const char * const jsonbType
[] = {
139 "null", "true", "false", "integer", "integer",
140 "real", "real", "text", "text", "text",
141 "text", "array", "object"
145 ** Growing our own isspace() routine this way is twice as fast as
146 ** the library isspace() function, resulting in a 7% overall performance
147 ** increase for the text-JSON parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
149 static const char jsonIsSpace
[] = {
150 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
151 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
152 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
153 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
154 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
155 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
157 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
161 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
162 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
163 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
165 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
166 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
168 #define fast_isspace(x) (jsonIsSpace[(unsigned char)x])
171 ** Characters that are special to JSON. Control charaters,
174 static const char jsonIsOk
[256] = {
175 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
177 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
178 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
179 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
180 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
181 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
182 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
184 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
185 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
186 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
187 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
188 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
189 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
190 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
191 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
194 /* Put code used only for testing inside the JSON_VVA() macro.
196 #if !defined(SQLITE_DEBUG) && !defined(SQLITE_COVERAGE_TEST)
199 # define JSON_VVA(X) X
203 typedef struct JsonString JsonString
;
204 typedef struct JsonParse JsonParse
;
206 /* An instance of this object represents a JSON string
207 ** under construction. Really, this is a generic string accumulator
208 ** that can be and is used to create strings other than JSON.
211 sqlite3_context
*pCtx
; /* Function context - put error messages here */
212 char *zBuf
; /* Append JSON content here */
213 u64 nAlloc
; /* Bytes of storage available in zBuf[] */
214 u64 nUsed
; /* Bytes of zBuf[] currently used */
215 u8 bStatic
; /* True if zBuf is static space */
216 u8 eErr
; /* True if an error has been encountered */
217 char zSpace
[100]; /* Initial static space */
220 /* Allowed values for JsonString.eErr */
221 #define JSTRING_OOM 0x01 /* Out of memory */
222 #define JSTRING_MALFORMED 0x02 /* Malformed JSONB */
223 #define JSTRING_ERR 0x04 /* Error already sent to sqlite3_result */
225 /* The "subtype" set for text JSON values passed through using
226 ** sqlite3_result_subtype() and sqlite3_value_subtype().
228 #define JSON_SUBTYPE 74 /* Ascii for "J" */
231 ** Bit values for the flags passed into jsonExtractFunc() or
232 ** jsonSetFunc() via the user-data value.
234 #define JSON_JSON 0x01 /* Result is always JSON */
235 #define JSON_SQL 0x02 /* Result is always SQL */
236 #define JSON_ABPATH 0x03 /* Allow abbreviated JSON path specs */
237 #define JSON_ISSET 0x04 /* json_set(), not json_insert() */
238 #define JSON_BLOB 0x08 /* Use the BLOB output format */
241 /* A parsed JSON value. Lifecycle:
243 ** 1. JSON comes in and is parsed into a JSONB value in aBlob. The
244 ** original text is stored in zJson.
246 ** 2. The aBlob is searched using the JSON path notation, if needed.
248 ** 3. Zero or more changes are made to aBlob (via json_remove() or
249 ** json_replace() or similar).
251 ** 4. New JSON text is generated from the aBlob for output.
253 ** Step 1 is omitted if the input is a BLOB in the JSONB format. Step 4
254 ** is omitted if the output is JSONB or some other value that is not
258 u8
*aBlob
; /* JSONB representation of JSON value */
259 u32 nBlob
; /* Bytes of aBlob[] actually used */
260 u32 nBlobAlloc
; /* Bytes allocated to aBlob[]. 0 if aBlob is external */
261 char *zJson
; /* Original JSON string (before edits) */
262 u16 iDepth
; /* Nesting depth */
263 u8 nErr
; /* Number of errors seen */
264 u8 oom
; /* Set to true if out of memory */
265 u8 bJsonIsRCStr
; /* True if zJson is an RCStr */
266 u8 hasNonstd
; /* True if input uses non-standard features like JSON5 */
267 u32 nJPRef
; /* Number of references to this object */
268 int nJson
; /* Length of the zJson string in bytes */
269 u32 iErr
; /* Error location in zJson[] */
270 /* Search and edit information. See jsonLookupBlobStep() */
271 u8 eEdit
; /* Edit operation to apply */
272 int delta
; /* Size change due to the edit */
273 u32 nIns
; /* Number of bytes to insert */
274 u32 iLabel
; /* Location of label if search landed on an object value */
275 u8
*aIns
; /* Content to be inserted */
278 /* Allowed values for JsonParse.eEdit */
279 #define JEDIT_DEL 1 /* Delete if exists */
280 #define JEDIT_REPL 2 /* Overwrite if exists */
281 #define JEDIT_INS 3 /* Insert if not exists */
282 #define JEDIT_SET 4 /* Insert or overwrite */
285 ** Maximum nesting depth of JSON for this implementation.
287 ** This limit is needed to avoid a stack overflow in the recursive
288 ** descent parser. A depth of 1000 is far deeper than any sane JSON
289 ** should go. Historical note: This limit was 2000 prior to version 3.42.0
291 #define JSON_MAX_DEPTH 1000
294 ** Allowed values for the flgs argument to jsonParseFuncArg();
296 #define JSON_EDITABLE 0x01 /* Generate a writable JsonParse object */
297 #define JSON_KEEPERROR 0x02 /* Return non-NULL even if there is an error */
299 /**************************************************************************
300 ** Forward references
301 **************************************************************************/
302 static void jsonReturnStringAsBlob(JsonString
*);
303 static int jsonFuncArgMightBeBinary(sqlite3_value
*pJson
);
304 static u32
jsonXlateBlobToText(const JsonParse
*,u32
,JsonString
*);
305 static void jsonReturnParse(sqlite3_context
*,JsonParse
*);
306 static JsonParse
*jsonParseFuncArg(sqlite3_context
*,sqlite3_value
*,u32
);
308 /**************************************************************************
309 ** Utility routines for dealing with JsonString objects
310 **************************************************************************/
312 /* Turn uninitialized bulk memory into a valid JsonString object
313 ** holding a zero-length string.
315 static void jsonStringZero(JsonString
*p
){
317 p
->nAlloc
= sizeof(p
->zSpace
);
322 /* Initialize the JsonString object
324 static void jsonStringInit(JsonString
*p
, sqlite3_context
*pCtx
){
330 /* Free all allocated memory and reset the JsonString object back to its
333 static void jsonStringReset(JsonString
*p
){
334 if( !p
->bStatic
) sqlite3RCStrUnref(p
->zBuf
);
338 /* Report an out-of-memory (OOM) condition
340 static void jsonStringOom(JsonString
*p
){
341 p
->eErr
|= JSTRING_OOM
;
342 if( p
->pCtx
) sqlite3_result_error_nomem(p
->pCtx
);
346 /* Enlarge pJson->zBuf so that it can hold at least N more bytes.
347 ** Return zero on success. Return non-zero on an OOM error
349 static int jsonStringGrow(JsonString
*p
, u32 N
){
350 u64 nTotal
= N
<p
->nAlloc
? p
->nAlloc
*2 : p
->nAlloc
+N
+10;
353 if( p
->eErr
) return 1;
354 zNew
= sqlite3RCStrNew(nTotal
);
359 memcpy(zNew
, p
->zBuf
, (size_t)p
->nUsed
);
363 p
->zBuf
= sqlite3RCStrResize(p
->zBuf
, nTotal
);
365 p
->eErr
|= JSTRING_OOM
;
374 /* Append N bytes from zIn onto the end of the JsonString string.
376 static SQLITE_NOINLINE
void jsonStringExpandAndAppend(
382 if( jsonStringGrow(p
,N
) ) return;
383 memcpy(p
->zBuf
+p
->nUsed
, zIn
, N
);
386 static void jsonAppendRaw(JsonString
*p
, const char *zIn
, u32 N
){
388 if( N
+p
->nUsed
>= p
->nAlloc
){
389 jsonStringExpandAndAppend(p
,zIn
,N
);
391 memcpy(p
->zBuf
+p
->nUsed
, zIn
, N
);
395 static void jsonAppendRawNZ(JsonString
*p
, const char *zIn
, u32 N
){
397 if( N
+p
->nUsed
>= p
->nAlloc
){
398 jsonStringExpandAndAppend(p
,zIn
,N
);
400 memcpy(p
->zBuf
+p
->nUsed
, zIn
, N
);
406 /* Append formatted text (not to exceed N bytes) to the JsonString.
408 static void jsonPrintf(int N
, JsonString
*p
, const char *zFormat
, ...){
410 if( (p
->nUsed
+ N
>= p
->nAlloc
) && jsonStringGrow(p
, N
) ) return;
411 va_start(ap
, zFormat
);
412 sqlite3_vsnprintf(N
, p
->zBuf
+p
->nUsed
, zFormat
, ap
);
414 p
->nUsed
+= (int)strlen(p
->zBuf
+p
->nUsed
);
417 /* Append a single character
419 static SQLITE_NOINLINE
void jsonAppendCharExpand(JsonString
*p
, char c
){
420 if( jsonStringGrow(p
,1) ) return;
421 p
->zBuf
[p
->nUsed
++] = c
;
423 static void jsonAppendChar(JsonString
*p
, char c
){
424 if( p
->nUsed
>=p
->nAlloc
){
425 jsonAppendCharExpand(p
,c
);
427 p
->zBuf
[p
->nUsed
++] = c
;
431 /* Make sure there is a zero terminator on p->zBuf[]
433 static void jsonStringTerminate(JsonString
*p
){
434 if( p
->nUsed
<p
->nAlloc
|| jsonStringGrow(p
,1) ){
435 p
->zBuf
[p
->nUsed
] = 0;
439 /* Try to force the string to be a zero-terminated RCStr string.
441 ** Return true on success. Return false if an OOM prevents this
444 static int jsonForceRCStr(JsonString
*p
){
445 jsonAppendChar(p
, 0);
446 if( p
->eErr
) return 0;
448 if( p
->bStatic
==0 ) return 1;
451 jsonStringGrow(p
, p
->nUsed
);
453 return p
->bStatic
==0;
457 /* Append a comma separator to the output buffer, if the previous
458 ** character is not '[' or '{'.
460 static void jsonAppendSeparator(JsonString
*p
){
462 if( p
->nUsed
==0 ) return;
463 c
= p
->zBuf
[p
->nUsed
-1];
464 if( c
=='[' || c
=='{' ) return;
465 jsonAppendChar(p
, ',');
468 /* Append the N-byte string in zIn to the end of the JsonString string
469 ** under construction. Enclose the string in "..." and escape
470 ** any double-quotes or backslash characters contained within the
473 static void jsonAppendString(JsonString
*p
, const char *zIn
, u32 N
){
476 if( (N
+p
->nUsed
+2 >= p
->nAlloc
) && jsonStringGrow(p
,N
+2)!=0 ) return;
477 p
->zBuf
[p
->nUsed
++] = '"';
479 unsigned char c
= ((unsigned const char*)zIn
)[i
];
481 p
->zBuf
[p
->nUsed
++] = c
;
482 }else if( c
=='"' || c
=='\\' ){
484 if( (p
->nUsed
+N
+3-i
> p
->nAlloc
) && jsonStringGrow(p
,N
+3-i
)!=0 ) return;
485 p
->zBuf
[p
->nUsed
++] = '\\';
486 p
->zBuf
[p
->nUsed
++] = c
;
488 p
->zBuf
[p
->nUsed
++] = c
;
490 static const char aSpecial
[] = {
491 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
492 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
494 assert( sizeof(aSpecial
)==32 );
495 assert( aSpecial
['\b']=='b' );
496 assert( aSpecial
['\f']=='f' );
497 assert( aSpecial
['\n']=='n' );
498 assert( aSpecial
['\r']=='r' );
499 assert( aSpecial
['\t']=='t' );
500 assert( c
>=0 && c
<sizeof(aSpecial
) );
503 goto json_simple_escape
;
505 if( (p
->nUsed
+N
+7+i
> p
->nAlloc
) && jsonStringGrow(p
,N
+7-i
)!=0 ) return;
506 p
->zBuf
[p
->nUsed
++] = '\\';
507 p
->zBuf
[p
->nUsed
++] = 'u';
508 p
->zBuf
[p
->nUsed
++] = '0';
509 p
->zBuf
[p
->nUsed
++] = '0';
510 p
->zBuf
[p
->nUsed
++] = "0123456789abcdef"[c
>>4];
511 p
->zBuf
[p
->nUsed
++] = "0123456789abcdef"[c
&0xf];
514 p
->zBuf
[p
->nUsed
++] = '"';
515 assert( p
->nUsed
<p
->nAlloc
);
519 ** Append an sqlite3_value (such as a function parameter) to the JSON
520 ** string under construction in p.
522 static void jsonAppendSqlValue(
523 JsonString
*p
, /* Append to this JSON string */
524 sqlite3_value
*pValue
/* Value to append */
526 switch( sqlite3_value_type(pValue
) ){
528 jsonAppendRawNZ(p
, "null", 4);
532 jsonPrintf(100, p
, "%!0.15g", sqlite3_value_double(pValue
));
535 case SQLITE_INTEGER
: {
536 const char *z
= (const char*)sqlite3_value_text(pValue
);
537 u32 n
= (u32
)sqlite3_value_bytes(pValue
);
538 jsonAppendRaw(p
, z
, n
);
542 const char *z
= (const char*)sqlite3_value_text(pValue
);
543 u32 n
= (u32
)sqlite3_value_bytes(pValue
);
544 if( sqlite3_value_subtype(pValue
)==JSON_SUBTYPE
){
545 jsonAppendRaw(p
, z
, n
);
547 jsonAppendString(p
, z
, n
);
552 if( jsonFuncArgMightBeBinary(pValue
) ){
554 memset(&px
, 0, sizeof(px
));
555 px
.aBlob
= (u8
*)sqlite3_value_blob(pValue
);
556 px
.nBlob
= sqlite3_value_bytes(pValue
);
557 jsonXlateBlobToText(&px
, 0, p
);
558 }else if( p
->eErr
==0 ){
559 sqlite3_result_error(p
->pCtx
, "JSON cannot hold BLOB values", -1);
560 p
->eErr
= JSTRING_ERR
;
568 /* Make the text in p (which is probably a generated JSON text string)
569 ** the result of the SQL function.
571 ** The JsonString is reset.
573 static void jsonReturnString(JsonString
*p
){
575 int flags
= SQLITE_PTR_TO_INT(sqlite3_user_data(p
->pCtx
));
576 if( flags
& JSON_BLOB
){
577 jsonReturnStringAsBlob(p
);
578 }else if( p
->bStatic
){
579 sqlite3_result_text64(p
->pCtx
, p
->zBuf
, p
->nUsed
,
580 SQLITE_TRANSIENT
, SQLITE_UTF8
);
581 }else if( jsonForceRCStr(p
) ){
582 sqlite3RCStrRef(p
->zBuf
);
583 sqlite3_result_text64(p
->pCtx
, p
->zBuf
, p
->nUsed
,
587 sqlite3_result_error_nomem(p
->pCtx
);
589 }else if( p
->eErr
& JSTRING_OOM
){
590 sqlite3_result_error_nomem(p
->pCtx
);
591 }else if( p
->eErr
& JSTRING_MALFORMED
){
592 sqlite3_result_error(p
->pCtx
, "malformed JSON", -1);
597 /**************************************************************************
598 ** Utility routines for dealing with JsonParse objects
599 **************************************************************************/
602 ** Reclaim all memory allocated by a JsonParse object. But do not
603 ** delete the JsonParse object itself.
605 static void jsonParseReset(JsonParse
*pParse
){
606 assert( pParse
->nJPRef
<=1 );
607 if( pParse
->bJsonIsRCStr
){
608 sqlite3RCStrUnref(pParse
->zJson
);
610 pParse
->bJsonIsRCStr
= 0;
612 if( pParse
->nBlobAlloc
){
613 sqlite3_free(pParse
->aBlob
);
616 pParse
->nBlobAlloc
= 0;
621 ** Free a JsonParse object that was obtained from sqlite3_malloc().
623 ** Note that destroying JsonParse might call sqlite3RCStrUnref() to
624 ** destroy the zJson value. The RCStr object might recursively invoke
625 ** JsonParse to destroy this pParse object again. Take care to ensure
626 ** that this recursive destructor sequence terminates harmlessly.
628 static void jsonParseFree(JsonParse
*pParse
){
630 if( pParse
->nJPRef
>1 ){
633 jsonParseReset(pParse
);
634 sqlite3_free(pParse
);
640 ** Translate a single byte of Hex into an integer.
641 ** This routine only works if h really is a valid hexadecimal
642 ** character: 0..9a..fA..F
644 static u8
jsonHexToInt(int h
){
645 if( !sqlite3Isxdigit(h
) ) return 0;
651 return (u8
)(h
& 0xf);
655 ** Convert a 4-byte hex string into an integer
657 static u32
jsonHexToInt4(const char *z
){
659 v
= (jsonHexToInt(z
[0])<<12)
660 + (jsonHexToInt(z
[1])<<8)
661 + (jsonHexToInt(z
[2])<<4)
662 + jsonHexToInt(z
[3]);
667 ** A macro to hint to the compiler that a function should not be
670 #if defined(__GNUC__)
671 # define JSON_NOINLINE __attribute__((noinline))
672 #elif defined(_MSC_VER) && _MSC_VER>=1310
673 # define JSON_NOINLINE __declspec(noinline)
675 # define JSON_NOINLINE
680 ** Return true if z[] begins with 2 (or more) hexadecimal digits
682 static int jsonIs2Hex(const char *z
){
683 return sqlite3Isxdigit(z
[0]) && sqlite3Isxdigit(z
[1]);
687 ** Return true if z[] begins with 4 (or more) hexadecimal digits
689 static int jsonIs4Hex(const char *z
){
690 return jsonIs2Hex(z
) && jsonIs2Hex(&z
[2]);
694 ** Return the number of bytes of JSON5 whitespace at the beginning of
695 ** the input string z[].
697 ** JSON5 whitespace consists of any of the following characters:
699 ** Unicode UTF-8 Name
700 ** U+0009 09 horizontal tab
701 ** U+000a 0a line feed
702 ** U+000b 0b vertical tab
703 ** U+000c 0c form feed
704 ** U+000d 0d carriage return
706 ** U+00a0 c2 a0 non-breaking space
707 ** U+1680 e1 9a 80 ogham space mark
708 ** U+2000 e2 80 80 en quad
709 ** U+2001 e2 80 81 em quad
710 ** U+2002 e2 80 82 en space
711 ** U+2003 e2 80 83 em space
712 ** U+2004 e2 80 84 three-per-em space
713 ** U+2005 e2 80 85 four-per-em space
714 ** U+2006 e2 80 86 six-per-em space
715 ** U+2007 e2 80 87 figure space
716 ** U+2008 e2 80 88 punctuation space
717 ** U+2009 e2 80 89 thin space
718 ** U+200a e2 80 8a hair space
719 ** U+2028 e2 80 a8 line separator
720 ** U+2029 e2 80 a9 paragraph separator
721 ** U+202f e2 80 af narrow no-break space (NNBSP)
722 ** U+205f e2 81 9f medium mathematical space (MMSP)
723 ** U+3000 e3 80 80 ideographical space
724 ** U+FEFF ef bb bf byte order mark
726 ** In addition, comments between '/', '*' and '*', '/' and
727 ** from '/', '/' to end-of-line are also considered to be whitespace.
729 static int json5Whitespace(const char *zIn
){
731 const u8
*z
= (u8
*)zIn
;
732 while( 1 /*exit by "goto whitespace_done"*/ ){
744 if( z
[n
+1]=='*' && z
[n
+2]!=0 ){
746 for(j
=n
+3; z
[j
]!='/' || z
[j
-1]!='*'; j
++){
747 if( z
[j
]==0 ) goto whitespace_done
;
751 }else if( z
[n
+1]=='/' ){
754 for(j
=n
+2; (c
= z
[j
])!=0; j
++){
755 if( c
=='\n' || c
=='\r' ) break;
756 if( 0xe2==(u8
)c
&& 0x80==(u8
)z
[j
+1]
757 && (0xa8==(u8
)z
[j
+2] || 0xa9==(u8
)z
[j
+2])
767 goto whitespace_done
;
774 goto whitespace_done
;
777 if( z
[n
+1]==0x9a && z
[n
+2]==0x80 ){
781 goto whitespace_done
;
786 if( c
<0x80 ) goto whitespace_done
;
787 if( c
<=0x8a || c
==0xa8 || c
==0xa9 || c
==0xaf ){
791 }else if( z
[n
+1]==0x81 && z
[n
+2]==0x9f ){
795 goto whitespace_done
;
798 if( z
[n
+1]==0x80 && z
[n
+2]==0x80 ){
802 goto whitespace_done
;
805 if( z
[n
+1]==0xbb && z
[n
+2]==0xbf ){
809 goto whitespace_done
;
812 goto whitespace_done
;
821 ** Extra floating-point literals to allow in JSON.
823 static const struct NanInfName
{
832 { 'i', 'I', 3, JSONB_FLOAT
, 7, "inf", "9.0e999" },
833 { 'i', 'I', 8, JSONB_FLOAT
, 7, "infinity", "9.0e999" },
834 { 'n', 'N', 3, JSONB_NULL
, 4, "NaN", "null" },
835 { 'q', 'Q', 4, JSONB_NULL
, 4, "QNaN", "null" },
836 { 's', 'S', 4, JSONB_NULL
, 4, "SNaN", "null" },
840 ** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
842 #define JSON_CACHE_ID (-429938) /* First cache entry */
843 #define JSON_CACHE_SZ 4 /* Max number of cache entries */
847 ** Compute the text of an error in JSON path syntax.
849 ** If ctx is not NULL then push the error message into ctx and return NULL.
850 * If ctx is NULL, then return the text of the error message.
852 static char *jsonPathSyntaxError(const char *zErr
, sqlite3_context
*ctx
){
853 char *zMsg
= sqlite3_mprintf("JSON path error near '%q'", zErr
);
854 if( ctx
==0 ) return zMsg
;
856 sqlite3_result_error_nomem(ctx
);
858 sqlite3_result_error(ctx
, zMsg
, -1);
865 ** Report the wrong number of arguments for json_insert(), json_replace()
868 static void jsonWrongNumArgs(
869 sqlite3_context
*pCtx
,
870 const char *zFuncName
872 char *zMsg
= sqlite3_mprintf("json_%s() needs an odd number of arguments",
874 sqlite3_result_error(pCtx
, zMsg
, -1);
878 /****************************************************************************
879 ** Utility routines for dealing with the binary BLOB representation of JSON
880 ****************************************************************************/
884 ** Expand pParse->aBlob so that it holds at least N bytes.
886 ** Return the number of errors.
888 static int jsonBlobExpand(JsonParse
*pParse
, u32 N
){
891 assert( N
>pParse
->nBlobAlloc
);
892 if( pParse
->nBlobAlloc
==0 ){
895 t
= pParse
->nBlobAlloc
*2;
898 aNew
= sqlite3_realloc64( pParse
->aBlob
, t
);
899 if( aNew
==0 ){ pParse
->oom
= 1; return 1; }
900 pParse
->aBlob
= aNew
;
901 pParse
->nBlobAlloc
= t
;
906 ** If pParse->aBlob is not previously editable (because it is taken
907 ** from sqlite3_value_blob(), as indicated by the fact that
908 ** pParse->nBlobAlloc==0 and pParse->nBlob>0) then make it editable
909 ** by making a copy into space obtained from malloc.
911 ** Return true on success. Return false on OOM.
913 static int jsonBlobMakeEditable(JsonParse
*pParse
, u32 nExtra
){
916 if( pParse
->nBlobAlloc
>0 ) return 1;
917 aOld
= pParse
->aBlob
;
918 nSize
= pParse
->nBlob
+ nExtra
;
920 if( jsonBlobExpand(pParse
, nSize
) ){
923 assert( pParse
->nBlobAlloc
>= pParse
->nBlob
+ nExtra
);
924 memcpy(pParse
->aBlob
, aOld
, pParse
->nBlob
);
929 /* Expand pParse->aBlob and append N bytes.
931 ** Return the number of errors.
933 static SQLITE_NOINLINE
int jsonBlobExpandAndAppend(
938 if( jsonBlobExpand(pParse
, pParse
->nBlob
+N
) ) return 1;
939 memcpy(&pParse
->aBlob
[pParse
->nBlob
], aData
, N
);
944 /* Append a single character. Return 1 if an error occurs.
946 static int jsonBlobAppendOneByte(JsonParse
*pParse
, u8 c
){
947 if( pParse
->nBlob
>= pParse
->nBlobAlloc
){
948 return jsonBlobExpandAndAppend(pParse
, &c
, 1);
950 pParse
->aBlob
[pParse
->nBlob
++] = c
;
954 /* Append bytes. Return 1 if an error occurs.
956 static int jsonBlobAppendNBytes(JsonParse
*pParse
, const u8
*aData
, u32 N
){
957 if( pParse
->nBlob
+N
> pParse
->nBlobAlloc
){
958 return jsonBlobExpandAndAppend(pParse
, aData
, N
);
960 memcpy(&pParse
->aBlob
[pParse
->nBlob
], aData
, N
);
965 /* Append an node type byte together with the payload size.
967 static void jsonBlobAppendNodeType(
974 jsonBlobAppendOneByte(pParse
, eType
| (szPayload
<<4));
975 }else if( szPayload
<=0xff ){
977 a
[1] = szPayload
& 0xff;
978 jsonBlobAppendNBytes(pParse
, a
, 2);
979 }else if( szPayload
<=0xffff ){
981 a
[1] = (szPayload
>> 8) & 0xff;
982 a
[2] = szPayload
& 0xff;
983 jsonBlobAppendNBytes(pParse
, a
, 3);
986 a
[1] = (szPayload
>> 24) & 0xff;
987 a
[2] = (szPayload
>> 16) & 0xff;
988 a
[3] = (szPayload
>> 8) & 0xff;
989 a
[4] = szPayload
& 0xff;
990 jsonBlobAppendNBytes(pParse
, a
, 5);
994 /* Change the payload size for the node at index i to be szPayload.
996 static int jsonBlobChangePayloadSize(
1006 if( pParse
->oom
) return 0;
1007 a
= &pParse
->aBlob
[i
];
1011 }else if( szType
==12 ){
1013 }else if( szType
==13 ){
1018 if( szPayload
<=11 ){
1020 }else if( szPayload
<=0xff ){
1022 }else if( szPayload
<=0xffff ){
1027 delta
= nNeeded
- nExtra
;
1029 u32 newSize
= pParse
->nBlob
+ delta
;
1031 if( newSize
>pParse
->nBlobAlloc
&& jsonBlobExpand(pParse
, newSize
) ){
1032 return 0; /* OOM error. Error state recorded in pParse->oom. */
1034 a
= &pParse
->aBlob
[i
];
1035 memmove(&a
[1+delta
], &a
[1], pParse
->nBlob
- (i
+1));
1037 memmove(&a
[1], &a
[1-delta
], pParse
->nBlob
- (i
+1-delta
));
1039 pParse
->nBlob
= newSize
;
1042 a
[0] = (a
[0] & 0x0f) | (szPayload
<<4);
1043 }else if( nNeeded
==1 ){
1044 a
[0] = (a
[0] & 0x0f) | 0xc0;
1045 a
[1] = szPayload
& 0xff;
1046 }else if( nNeeded
==2 ){
1047 a
[0] = (a
[0] & 0x0f) | 0xd0;
1048 a
[1] = (szPayload
>> 8) & 0xff;
1049 a
[2] = szPayload
& 0xff;
1051 a
[0] = (a
[0] & 0x0f) | 0xe0;
1052 a
[1] = (szPayload
>> 24) & 0xff;
1053 a
[2] = (szPayload
>> 16) & 0xff;
1054 a
[3] = (szPayload
>> 8) & 0xff;
1055 a
[4] = szPayload
& 0xff;
1061 ** If z[0] is 'u' and is followed by exactly 4 hexadecimal character,
1062 ** then set *pOp to JSONB_TEXTJ and return true. If not, do not make
1063 ** any changes to *pOp and return false.
1065 static int jsonIs4HexB(const char *z
, int *pOp
){
1066 if( z
[0]!='u' ) return 0;
1067 if( !sqlite3Isxdigit(z
[1]) ) return 0;
1068 if( !sqlite3Isxdigit(z
[2]) ) return 0;
1069 if( !sqlite3Isxdigit(z
[3]) ) return 0;
1070 if( !sqlite3Isxdigit(z
[4]) ) return 0;
1076 ** Translate a single element of JSON text at pParse->zJson[i] into
1077 ** its equivalent binary JSONB representation. Append the translation into
1078 ** pParse->aBlob[] beginning at pParse->nBlob. The size of
1079 ** pParse->aBlob[] is increased as necessary.
1081 ** Return the index of the first character past the end of the element parsed,
1082 ** or one of the following special result codes:
1087 ** -3 ']' seen \___ For these returns, pParse->iErr is set to
1088 ** -4 ',' seen / the index in zJson[] of the seen character
1091 static int jsonXlateTextToBlob(JsonParse
*pParse
, u32 i
){
1097 const char *z
= pParse
->zJson
;
1102 iThis
= pParse
->nBlob
;
1103 jsonBlobAppendNodeType(pParse
, JSONB_OBJECT
, (pParse
->nJson
-i
)*2);
1104 if( ++pParse
->iDepth
> JSON_MAX_DEPTH
){
1108 iStart
= pParse
->nBlob
;
1110 u32 iBlob
= pParse
->nBlob
;
1111 x
= jsonXlateTextToBlob(pParse
, j
);
1116 if( pParse
->nBlob
!=(u32
)iStart
) pParse
->hasNonstd
= 1;
1119 j
+= json5Whitespace(&z
[j
]);
1121 if( sqlite3JsonId1(z
[j
])
1122 || (z
[j
]=='\\' && jsonIs4HexB(&z
[j
+1], &op
))
1125 while( (sqlite3JsonId2(z
[k
]) && json5Whitespace(&z
[k
])==0)
1126 || (z
[k
]=='\\' && jsonIs4HexB(&z
[k
+1], &op
))
1130 assert( iBlob
==pParse
->nBlob
);
1131 jsonBlobAppendNodeType(pParse
, op
, k
-j
);
1132 jsonBlobAppendNBytes(pParse
, (const u8
*)&z
[j
], k
-j
);
1133 pParse
->hasNonstd
= 1;
1136 if( x
!=-1 ) pParse
->iErr
= j
;
1140 if( pParse
->oom
) return -1;
1141 t
= pParse
->aBlob
[iBlob
] & 0x0f;
1142 if( t
<JSONB_TEXT
|| t
>JSONB_TEXTRAW
){
1150 if( fast_isspace(z
[j
]) ){
1151 do{ j
++; }while( fast_isspace(z
[j
]) );
1154 goto parse_object_value
;
1157 x
= jsonXlateTextToBlob(pParse
, j
);
1159 if( x
!=(-1) ) pParse
->iErr
= j
;
1165 x
= jsonXlateTextToBlob(pParse
, j
);
1167 if( x
!=(-1) ) pParse
->iErr
= j
;
1173 }else if( z
[j
]=='}' ){
1176 if( fast_isspace(z
[j
]) ){
1177 do{ j
++; }while( fast_isspace(z
[j
]) );
1180 }else if( z
[j
]=='}' ){
1184 x
= jsonXlateTextToBlob(pParse
, j
);
1197 jsonBlobChangePayloadSize(pParse
, iThis
, pParse
->nBlob
- iStart
);
1203 iThis
= pParse
->nBlob
;
1204 jsonBlobAppendNodeType(pParse
, JSONB_ARRAY
, pParse
->nJson
- i
);
1205 iStart
= pParse
->nBlob
;
1206 if( pParse
->oom
) return -1;
1207 if( ++pParse
->iDepth
> JSON_MAX_DEPTH
){
1212 x
= jsonXlateTextToBlob(pParse
, j
);
1216 if( pParse
->nBlob
!=iStart
) pParse
->hasNonstd
= 1;
1219 if( x
!=(-1) ) pParse
->iErr
= j
;
1225 }else if( z
[j
]==']' ){
1228 if( fast_isspace(z
[j
]) ){
1229 do{ j
++; }while( fast_isspace(z
[j
]) );
1232 }else if( z
[j
]==']' ){
1236 x
= jsonXlateTextToBlob(pParse
, j
);
1249 jsonBlobChangePayloadSize(pParse
, iThis
, pParse
->nBlob
- iStart
);
1257 pParse
->hasNonstd
= 1;
1258 opcode
= JSONB_TEXT
;
1262 opcode
= JSONB_TEXT
;
1271 if( jsonIsOk
[(unsigned char)z
[j
]] ) continue;
1275 }else if( c
=='\\' ){
1277 if( c
=='"' || c
=='\\' || c
=='/' || c
=='b' || c
=='f'
1278 || c
=='n' || c
=='r' || c
=='t'
1279 || (c
=='u' && jsonIs4Hex(&z
[j
+1])) ){
1280 if( opcode
==JSONB_TEXT
) opcode
= JSONB_TEXTJ
;
1281 }else if( c
=='\'' || c
=='0' || c
=='v' || c
=='\n'
1282 || (0xe2==(u8
)c
&& 0x80==(u8
)z
[j
+1]
1283 && (0xa8==(u8
)z
[j
+2] || 0xa9==(u8
)z
[j
+2]))
1284 || (c
=='x' && jsonIs2Hex(&z
[j
+1])) ){
1285 opcode
= JSONB_TEXT5
;
1286 pParse
->hasNonstd
= 1;
1287 }else if( c
=='\r' ){
1288 if( z
[j
+1]=='\n' ) j
++;
1289 opcode
= JSONB_TEXT5
;
1290 pParse
->hasNonstd
= 1;
1295 }else if( c
<=0x1f ){
1296 /* Control characters are not allowed in strings */
1301 jsonBlobAppendNodeType(pParse
, opcode
, j
-1-i
);
1302 jsonBlobAppendNBytes(pParse
, (const u8
*)&z
[i
+1], j
-1-i
);
1306 if( strncmp(z
+i
,"true",4)==0 && !sqlite3Isalnum(z
[i
+4]) ){
1307 jsonBlobAppendOneByte(pParse
, JSONB_TRUE
);
1314 if( strncmp(z
+i
,"false",5)==0 && !sqlite3Isalnum(z
[i
+5]) ){
1315 jsonBlobAppendOneByte(pParse
, JSONB_FALSE
);
1323 pParse
->hasNonstd
= 1;
1324 t
= 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
1327 if( sqlite3Isdigit(z
[i
+1]) ){
1328 pParse
->hasNonstd
= 1;
1329 t
= 0x03; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
1331 goto parse_number_2
;
1347 t
= 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
1350 assert( '-' < '0' );
1351 assert( '+' < '0' );
1352 assert( '.' < '0' );
1357 if( (z
[i
+1]=='x' || z
[i
+1]=='X') && sqlite3Isxdigit(z
[i
+2]) ){
1359 pParse
->hasNonstd
= 1;
1361 for(j
=i
+3; sqlite3Isxdigit(z
[j
]); j
++){}
1362 goto parse_number_finish
;
1363 }else if( sqlite3Isdigit(z
[i
+1]) ){
1368 if( !sqlite3Isdigit(z
[i
+1]) ){
1369 /* JSON5 allows for "+Infinity" and "-Infinity" using exactly
1370 ** that case. SQLite also allows these in any case and it allows
1371 ** "+inf" and "-inf". */
1372 if( (z
[i
+1]=='I' || z
[i
+1]=='i')
1373 && sqlite3StrNICmp(&z
[i
+1], "inf",3)==0
1375 pParse
->hasNonstd
= 1;
1377 jsonBlobAppendNodeType(pParse
, JSONB_FLOAT
, 6);
1378 jsonBlobAppendNBytes(pParse
, (const u8
*)"-9e999", 6);
1380 jsonBlobAppendNodeType(pParse
, JSONB_FLOAT
, 5);
1381 jsonBlobAppendNBytes(pParse
, (const u8
*)"9e999", 5);
1383 return i
+ (sqlite3StrNICmp(&z
[i
+4],"inity",5)==0 ? 9 : 4);
1386 pParse
->hasNonstd
= 1;
1388 goto parse_number_2
;
1394 if( sqlite3Isdigit(z
[i
+2]) ){
1397 }else if( (z
[i
+2]=='x' || z
[i
+2]=='X') && sqlite3Isxdigit(z
[i
+3]) ){
1398 pParse
->hasNonstd
= 1;
1400 for(j
=i
+4; sqlite3Isxdigit(z
[j
]); j
++){}
1401 goto parse_number_finish
;
1410 if( sqlite3Isdigit(c
) ) continue;
1412 if( (t
& 0x02)!=0 ){
1419 if( c
=='e' || c
=='E' ){
1421 if( ALWAYS(z
[j
-1]=='.') && ALWAYS(j
-2>=i
) && sqlite3Isdigit(z
[j
-2]) ){
1422 pParse
->hasNonstd
= 1;
1436 if( c
=='+' || c
=='-' ){
1440 if( c
<'0' || c
>'9' ){
1449 if( ALWAYS(z
[j
-1]=='.') && ALWAYS(j
-2>=i
) && sqlite3Isdigit(z
[j
-2]) ){
1450 pParse
->hasNonstd
= 1;
1457 parse_number_finish
:
1458 assert( JSONB_INT
+0x01==JSONB_INT5
);
1459 assert( JSONB_FLOAT
+0x01==JSONB_FLOAT5
);
1460 assert( JSONB_INT
+0x02==JSONB_FLOAT
);
1461 if( z
[i
]=='+' ) i
++;
1462 jsonBlobAppendNodeType(pParse
, JSONB_INT
+t
, j
-i
);
1463 jsonBlobAppendNBytes(pParse
, (const u8
*)&z
[i
], j
-i
);
1468 return -2; /* End of {...} */
1472 return -3; /* End of [...] */
1476 return -4; /* List separator */
1480 return -5; /* Object label/value separator */
1483 return 0; /* End of file */
1491 }while( fast_isspace(z
[i
]) );
1492 goto json_parse_restart
;
1502 j
= json5Whitespace(&z
[i
]);
1505 pParse
->hasNonstd
= 1;
1506 goto json_parse_restart
;
1512 if( strncmp(z
+i
,"null",4)==0 && !sqlite3Isalnum(z
[i
+4]) ){
1513 jsonBlobAppendOneByte(pParse
, JSONB_NULL
);
1516 /* fall-through into the default case that checks for NaN */
1522 for(k
=0; k
<sizeof(aNanInfName
)/sizeof(aNanInfName
[0]); k
++){
1523 if( c
!=aNanInfName
[k
].c1
&& c
!=aNanInfName
[k
].c2
) continue;
1524 nn
= aNanInfName
[k
].n
;
1525 if( sqlite3StrNICmp(&z
[i
], aNanInfName
[k
].zMatch
, nn
)!=0 ){
1528 if( sqlite3Isalnum(z
[i
+nn
]) ) continue;
1529 if( aNanInfName
[k
].eType
==JSONB_FLOAT
){
1530 jsonBlobAppendOneByte(pParse
, JSONB_FLOAT
| 0x50);
1531 jsonBlobAppendNBytes(pParse
, (const u8
*)"9e999", 5);
1533 jsonBlobAppendOneByte(pParse
, JSONB_NULL
);
1535 pParse
->hasNonstd
= 1;
1539 return -1; /* Syntax error */
1541 } /* End switch(z[i]) */
1546 ** Parse a complete JSON string. Return 0 on success or non-zero if there
1547 ** are any errors. If an error occurs, free all memory held by pParse,
1548 ** but not pParse itself.
1550 ** pParse must be initialized to an empty parse object prior to calling
1553 static int jsonConvertTextToBlob(
1554 JsonParse
*pParse
, /* Initialize and fill this JsonParse object */
1555 sqlite3_context
*pCtx
/* Report errors here */
1558 const char *zJson
= pParse
->zJson
;
1559 i
= jsonXlateTextToBlob(pParse
, 0);
1560 if( pParse
->oom
) i
= -1;
1562 assert( pParse
->iDepth
==0 );
1563 while( fast_isspace(zJson
[i
]) ) i
++;
1565 i
+= json5Whitespace(&zJson
[i
]);
1567 jsonParseReset(pParse
);
1570 pParse
->hasNonstd
= 1;
1576 sqlite3_result_error_nomem(pCtx
);
1578 sqlite3_result_error(pCtx
, "malformed JSON", -1);
1581 jsonParseReset(pParse
);
1588 ** The input string pStr is a well-formed JSON text string. Convert
1589 ** this into the JSONB format and make it the return value of the
1592 static void jsonReturnStringAsBlob(JsonString
*pStr
){
1594 memset(&px
, 0, sizeof(px
));
1595 jsonStringTerminate(pStr
);
1596 px
.zJson
= pStr
->zBuf
;
1597 px
.nJson
= pStr
->nUsed
;
1598 (void)jsonXlateTextToBlob(&px
, 0);
1600 sqlite3_free(px
.aBlob
);
1601 sqlite3_result_error_nomem(pStr
->pCtx
);
1603 sqlite3_result_blob(pStr
->pCtx
, px
.aBlob
, px
.nBlob
, sqlite3_free
);
1607 /* The byte at index i is a node type-code. This routine
1608 ** determines the payload size for that node and writes that
1609 ** payload size in to *pSz. It returns the offset from i to the
1610 ** beginning of the payload. Return 0 on error.
1612 static u32
jsonbPayloadSize(const JsonParse
*pParse
, u32 i
, u32
*pSz
){
1616 if( NEVER(i
>pParse
->nBlob
) ){
1620 x
= pParse
->aBlob
[i
]>>4;
1625 if( i
+1>=pParse
->nBlob
){
1629 sz
= pParse
->aBlob
[i
+1];
1632 if( i
+2>=pParse
->nBlob
){
1636 sz
= (pParse
->aBlob
[i
+1]<<8) + pParse
->aBlob
[i
+2];
1639 if( i
+4>=pParse
->nBlob
){
1643 sz
= (pParse
->aBlob
[i
+1]<<24) + (pParse
->aBlob
[i
+2]<<16) +
1644 (pParse
->aBlob
[i
+3]<<8) + pParse
->aBlob
[i
+4];
1647 if( i
+sz
+n
> pParse
->nBlob
1648 && i
+sz
+n
> pParse
->nBlob
-pParse
->delta
1659 ** Translate the binary JSONB representation of JSON beginning at
1660 ** pParse->aBlob[i] into a JSON text string. Append the JSON
1661 ** text onto the end of pOut. Return the index in pParse->aBlob[]
1662 ** of the first byte past the end of the element that is translated.
1664 ** If an error is detected in the BLOB input, the pOut->eErr flag
1665 ** might get set to JSTRING_MALFORMED. But not all BLOB input errors
1666 ** are detected. So a malformed JSONB input might either result
1667 ** in an error, or in incorrect JSON.
1669 ** The pOut->eErr JSTRING_OOM flag is set on a OOM.
1671 static u32
jsonXlateBlobToText(
1672 const JsonParse
*pParse
, /* the complete parse of the JSON */
1673 u32 i
, /* Start rendering at this index */
1674 JsonString
*pOut
/* Write JSON here */
1678 n
= jsonbPayloadSize(pParse
, i
, &sz
);
1680 pOut
->eErr
|= JSTRING_MALFORMED
;
1681 return pParse
->nBlob
+1;
1683 switch( pParse
->aBlob
[i
] & 0x0f ){
1685 jsonAppendRawNZ(pOut
, "null", 4);
1689 jsonAppendRawNZ(pOut
, "true", 4);
1693 jsonAppendRawNZ(pOut
, "false", 5);
1698 jsonAppendRaw(pOut
, (const char*)&pParse
->aBlob
[i
+n
], sz
);
1701 case JSONB_INT5
: { /* Integer literal in hexadecimal notation */
1703 sqlite3_uint64 u
= 0;
1704 const char *zIn
= (const char*)&pParse
->aBlob
[i
+n
];
1707 jsonAppendChar(pOut
, '-');
1709 }else if( zIn
[0]=='+' ){
1713 if( !sqlite3Isxdigit(zIn
[k
]) ){
1714 pOut
->eErr
|= JSTRING_MALFORMED
;
1716 }else if( (u
>>60)!=0 ){
1719 u
= u
*16 + sqlite3HexToInt(zIn
[k
]);
1722 jsonPrintf(100,pOut
,bOverflow
?"9.0e999":"%llu", u
);
1725 case JSONB_FLOAT5
: { /* Float literal missing digits beside "." */
1727 const char *zIn
= (const char*)&pParse
->aBlob
[i
+n
];
1729 jsonAppendChar(pOut
, '-');
1733 jsonAppendChar(pOut
, '0');
1736 jsonAppendChar(pOut
, zIn
[k
]);
1737 if( zIn
[k
]=='.' && (k
+1==sz
|| !sqlite3Isdigit(zIn
[k
+1])) ){
1738 jsonAppendChar(pOut
, '0');
1744 jsonAppendChar(pOut
, '"');
1745 jsonAppendRaw(pOut
, (const char*)&pParse
->aBlob
[i
+n
], sz
);
1746 jsonAppendChar(pOut
, '"');
1754 zIn
= (const char*)&pParse
->aBlob
[i
+n
];
1755 jsonAppendChar(pOut
, '"');
1757 for(k
=0; k
<sz2
&& zIn
[k
]!='\\' && zIn
[k
]!='"'; k
++){}
1759 jsonAppendRawNZ(pOut
, zIn
, k
);
1767 jsonAppendRawNZ(pOut
, "\\\"", 2);
1773 if( sz2
>0 ) pOut
->eErr
|= JSTRING_MALFORMED
;
1776 assert( zIn
[0]=='\\' );
1777 switch( (u8
)zIn
[1] ){
1779 jsonAppendChar(pOut
, '\'');
1782 jsonAppendRawNZ(pOut
, "\\u0009", 6);
1786 pOut
->eErr
|= JSTRING_MALFORMED
;
1790 jsonAppendRawNZ(pOut
, "\\u00", 4);
1791 jsonAppendRawNZ(pOut
, &zIn
[2], 2);
1796 jsonAppendRawNZ(pOut
, "\\u0000", 6);
1799 if( sz2
>2 && zIn
[2]=='\n' ){
1807 /* '\' followed by either U+2028 or U+2029 is ignored as
1808 ** whitespace. Not that in UTF8, U+2028 is 0xe2 0x80 0x29.
1809 ** U+2029 is the same except for the last byte */
1812 || (0xa8!=(u8
)zIn
[3] && 0xa9!=(u8
)zIn
[3])
1814 pOut
->eErr
|= JSTRING_MALFORMED
;
1822 jsonAppendRawNZ(pOut
, zIn
, 2);
1827 pOut
->eErr
|= JSTRING_MALFORMED
;
1833 jsonAppendChar(pOut
, '"');
1836 case JSONB_TEXTRAW
: {
1837 jsonAppendString(pOut
, (const char*)&pParse
->aBlob
[i
+n
], sz
);
1841 jsonAppendChar(pOut
, '[');
1845 j
= jsonXlateBlobToText(pParse
, j
, pOut
);
1846 jsonAppendChar(pOut
, ',');
1848 if( sz
>0 ) pOut
->nUsed
--;
1849 jsonAppendChar(pOut
, ']');
1852 case JSONB_OBJECT
: {
1854 jsonAppendChar(pOut
, '{');
1858 j
= jsonXlateBlobToText(pParse
, j
, pOut
);
1859 jsonAppendChar(pOut
, (x
++ & 1) ? ',' : ':');
1861 if( x
& 1 ) pOut
->eErr
|= JSTRING_MALFORMED
;
1862 if( sz
>0 ) pOut
->nUsed
--;
1863 jsonAppendChar(pOut
, '}');
1868 pOut
->eErr
|= JSTRING_MALFORMED
;
1875 /* Return true if the input pJson
1877 ** For performance reasons, this routine does not do a detailed check of the
1878 ** input BLOB to ensure that it is well-formed. Hence, false positives are
1879 ** possible. False negatives should never occur, however.
1881 static int jsonFuncArgMightBeBinary(sqlite3_value
*pJson
){
1886 if( sqlite3_value_type(pJson
)!=SQLITE_BLOB
) return 0;
1887 aBlob
= sqlite3_value_blob(pJson
);
1888 nBlob
= sqlite3_value_bytes(pJson
);
1889 if( nBlob
<1 ) return 0;
1890 if( aBlob
==0 || (aBlob
[0] & 0x0f)>JSONB_OBJECT
) return 0;
1891 memset(&s
, 0, sizeof(s
));
1892 s
.aBlob
= (u8
*)aBlob
;
1894 n
= jsonbPayloadSize(&s
, 0, &sz
);
1895 if( n
==0 ) return 0;
1896 if( sz
+n
!=(u32
)nBlob
) return 0;
1897 if( (aBlob
[0] & 0x0f)<=JSONB_FALSE
&& sz
>0 ) return 0;
1898 return sz
+n
==(u32
)nBlob
;
1902 ** Given that a JSONB_ARRAY object starts at offset i, return
1903 ** the number of entries in that array.
1905 static u32
jsonbArrayCount(JsonParse
*pParse
, u32 iRoot
){
1908 n
= jsonbPayloadSize(pParse
, iRoot
, &sz
);
1910 for(i
=iRoot
+n
; n
>0 && i
<iEnd
; i
+=sz
+n
, k
++){
1911 n
= jsonbPayloadSize(pParse
, i
, &sz
);
1917 ** Edit the size of the element at iRoot by the amount in pParse->delta.
1919 static void jsonAfterEditSizeAdjust(JsonParse
*pParse
, u32 iRoot
){
1922 assert( pParse
->delta
!=0 );
1923 assert( pParse
->nBlobAlloc
>= pParse
->nBlob
);
1924 nBlob
= pParse
->nBlob
;
1925 pParse
->nBlob
= pParse
->nBlobAlloc
;
1926 (void)jsonbPayloadSize(pParse
, iRoot
, &sz
);
1927 pParse
->nBlob
= nBlob
;
1928 sz
+= pParse
->delta
;
1929 pParse
->delta
+= jsonBlobChangePayloadSize(pParse
, iRoot
, sz
);
1933 ** Modify the JSONB blob at pParse->aBlob by removing nDel bytes of
1934 ** content beginning at iDel, and replacing them with nIns bytes of
1935 ** content given by aIns.
1937 ** nDel may be zero, in which case no bytes are removed. But iDel is
1938 ** still important as new bytes will be insert beginning at iDel.
1940 ** aIns may be zero, in which case space is created to hold nIns bytes
1941 ** beginning at iDel, but that space is uninitialized.
1943 ** Set pParse->oom if an OOM occurs.
1945 static void jsonBlobEdit(
1946 JsonParse
*pParse
, /* The JSONB to be modified is in pParse->aBlob */
1947 u32 iDel
, /* First byte to be removed */
1948 u32 nDel
, /* Number of bytes to remove */
1949 const u8
*aIns
, /* Content to insert */
1950 u32 nIns
/* Bytes of content to insert */
1952 i64 d
= (i64
)nIns
- (i64
)nDel
;
1954 if( pParse
->nBlob
+ d
> pParse
->nBlobAlloc
){
1955 jsonBlobExpand(pParse
, pParse
->nBlob
+d
);
1956 if( pParse
->oom
) return;
1958 memmove(&pParse
->aBlob
[iDel
+nIns
],
1959 &pParse
->aBlob
[iDel
+nDel
],
1960 pParse
->nBlob
- (iDel
+nDel
));
1964 if( nIns
&& aIns
) memcpy(&pParse
->aBlob
[iDel
], aIns
, nIns
);
1968 ** Error returns from jsonLookupBlobStep()
1970 #define JSON_BLOB_ERROR 0xffffffff
1971 #define JSON_BLOB_NOTFOUND 0xfffffffe
1972 #define JSON_BLOB_PATHERROR 0xfffffffd
1973 #define JSON_BLOB_ISERROR(x) ((x)>=JSON_BLOB_PATHERROR)
1976 ** Search along zPath to find the Json element specified. Return an
1977 ** index into pParse->aBlob[] for the start of that element's value.
1979 ** Return JSON_BLOB_NOTFOUND if no such element exists.
1981 static u32
jsonLookupBlobStep(
1982 JsonParse
*pParse
, /* The JSON to search */
1983 u32 iRoot
, /* Begin the search at this element of aBlob[] */
1984 const char *zPath
, /* The path to search */
1985 u32 iLabel
/* Label if iRoot is a value of in an object */
1987 u32 i
, j
, k
, nKey
, sz
, n
, iEnd
, rc
;
1990 static const u8 emptyObject
[] = { JSONB_ARRAY
, JSONB_OBJECT
};
1993 if( pParse
->eEdit
&& jsonBlobMakeEditable(pParse
, pParse
->nIns
) ){
1994 n
= jsonbPayloadSize(pParse
, iRoot
, &sz
);
1996 if( pParse
->eEdit
==JEDIT_DEL
){
1998 sz
+= iRoot
- iLabel
;
2001 jsonBlobEdit(pParse
, iRoot
, sz
, 0, 0);
2002 }else if( pParse
->eEdit
==JEDIT_INS
){
2003 /* Already exists, so json_insert() is a no-op */
2005 /* json_set() or json_replace() */
2006 jsonBlobEdit(pParse
, iRoot
, sz
, pParse
->aIns
, pParse
->nIns
);
2009 pParse
->iLabel
= iLabel
;
2012 if( zPath
[0]=='.' ){
2013 x
= pParse
->aBlob
[iRoot
];
2015 if( zPath
[0]=='"' ){
2017 for(i
=1; zPath
[i
] && zPath
[i
]!='"'; i
++){}
2022 return JSON_BLOB_PATHERROR
;
2024 testcase( nKey
==0 );
2027 for(i
=0; zPath
[i
] && zPath
[i
]!='.' && zPath
[i
]!='['; i
++){}
2030 return JSON_BLOB_PATHERROR
;
2033 if( (x
& 0x0f)!=JSONB_OBJECT
) return JSON_BLOB_NOTFOUND
;
2034 n
= jsonbPayloadSize(pParse
, iRoot
, &sz
);
2035 j
= iRoot
+ n
; /* j is the index of a label */
2038 x
= pParse
->aBlob
[j
] & 0x0f;
2039 if( x
<JSONB_TEXT
|| x
>JSONB_TEXTRAW
) return JSON_BLOB_ERROR
;
2040 n
= jsonbPayloadSize(pParse
, j
, &sz
);
2041 if( n
==0 ) return JSON_BLOB_ERROR
;
2042 k
= j
+n
; /* k is the index of the label text */
2043 if( k
+sz
>=iEnd
) return JSON_BLOB_ERROR
;
2044 if( sz
==nKey
&& memcmp(&pParse
->aBlob
[k
], zKey
, nKey
)==0 ){
2045 u32 v
= k
+sz
; /* v is the index of the value */
2046 if( ((pParse
->aBlob
[v
])&0x0f)>JSONB_OBJECT
) return JSON_BLOB_ERROR
;
2047 n
= jsonbPayloadSize(pParse
, v
, &sz
);
2048 if( n
==0 || v
+n
+sz
>iEnd
) return JSON_BLOB_ERROR
;
2050 rc
= jsonLookupBlobStep(pParse
, v
, &zPath
[i
], j
);
2051 if( pParse
->delta
) jsonAfterEditSizeAdjust(pParse
, iRoot
);
2055 if( ((pParse
->aBlob
[j
])&0x0f)>JSONB_OBJECT
) return JSON_BLOB_ERROR
;
2056 n
= jsonbPayloadSize(pParse
, j
, &sz
);
2057 if( n
==0 ) return JSON_BLOB_ERROR
;
2060 if( j
>iEnd
) return JSON_BLOB_ERROR
;
2061 if( pParse
->eEdit
>=JEDIT_INS
){
2062 u32 nIns
; /* Total bytes to insert (label+value) */
2063 JsonParse v
; /* BLOB encoding of the value to be inserted */
2064 JsonParse ix
; /* Header of the label to be inserted */
2065 u8 aLabel
[16]; /* Buffer space for ix */
2066 testcase( pParse
->eEdit
==JEDIT_INS
);
2067 testcase( pParse
->eEdit
==JEDIT_SET
);
2068 memset(&ix
, 0, sizeof(ix
));
2070 ix
.nBlobAlloc
= sizeof(aLabel
);
2071 jsonBlobAppendNodeType(&ix
,JSONB_TEXTRAW
, nKey
);
2072 memset(&v
, 0, sizeof(v
));
2074 v
.nBlob
= pParse
->nIns
;
2075 v
.aBlob
= pParse
->aIns
;
2078 v
.aBlob
= (u8
*)&emptyObject
[zPath
[i
]=='.'];
2079 v
.eEdit
= pParse
->eEdit
;
2080 v
.nIns
= pParse
->nIns
;
2081 v
.aIns
= pParse
->aIns
;
2082 rc
= jsonLookupBlobStep(&v
, 0, &zPath
[i
], 0);
2083 if( JSON_BLOB_ISERROR(rc
) || v
.oom
){
2084 pParse
->oom
|= v
.oom
;
2089 if( jsonBlobMakeEditable(pParse
, ix
.nBlob
+nKey
+v
.nBlob
) ){
2090 nIns
= ix
.nBlob
+ nKey
+ v
.nBlob
;
2091 jsonBlobEdit(pParse
, j
, 0, 0, nIns
);
2092 memcpy(&pParse
->aBlob
[j
], ix
.aBlob
, ix
.nBlob
);
2094 memcpy(&pParse
->aBlob
[k
], zKey
, nKey
);
2096 memcpy(&pParse
->aBlob
[k
], v
.aBlob
, v
.nBlob
);
2097 if( pParse
->delta
) jsonAfterEditSizeAdjust(pParse
, iRoot
);
2102 }else if( zPath
[0]=='[' ){
2103 x
= pParse
->aBlob
[iRoot
] & 0x0f;
2104 if( x
!=JSONB_ARRAY
) return JSON_BLOB_NOTFOUND
;
2105 n
= jsonbPayloadSize(pParse
, iRoot
, &sz
);
2108 while( sqlite3Isdigit(zPath
[i
]) ){
2109 k
= k
*10 + zPath
[i
] - '0';
2112 if( i
<2 || zPath
[i
]!=']' ){
2113 if( zPath
[1]=='#' ){
2114 k
= jsonbArrayCount(pParse
, iRoot
);
2116 if( zPath
[2]=='-' && sqlite3Isdigit(zPath
[3]) ){
2117 unsigned int nn
= 0;
2120 nn
= nn
*10 + zPath
[i
] - '0';
2122 }while( sqlite3Isdigit(zPath
[i
]) );
2123 if( nn
>k
) return JSON_BLOB_NOTFOUND
;
2126 if( zPath
[i
]!=']' ){
2127 return JSON_BLOB_PATHERROR
;
2130 return JSON_BLOB_PATHERROR
;
2137 rc
= jsonLookupBlobStep(pParse
, j
, &zPath
[i
+1], 0);
2138 if( pParse
->delta
) jsonAfterEditSizeAdjust(pParse
, iRoot
);
2142 n
= jsonbPayloadSize(pParse
, j
, &sz
);
2143 if( n
==0 ) return JSON_BLOB_ERROR
;
2146 if( j
>iEnd
) return JSON_BLOB_ERROR
;
2147 if( k
>0 ) return JSON_BLOB_NOTFOUND
;
2148 if( pParse
->eEdit
>=JEDIT_INS
){
2150 testcase( pParse
->eEdit
==JEDIT_INS
);
2151 testcase( pParse
->eEdit
==JEDIT_SET
);
2152 memset(&v
, 0, sizeof(v
));
2153 if( zPath
[i
+1]==0 ){
2154 v
.aBlob
= pParse
->aIns
;
2155 v
.nBlob
= pParse
->nIns
;
2158 v
.aBlob
= (u8
*)&emptyObject
[zPath
[i
+1]=='.'];
2159 v
.eEdit
= pParse
->eEdit
;
2160 v
.nIns
= pParse
->nIns
;
2161 v
.aIns
= pParse
->aIns
;
2162 rc
= jsonLookupBlobStep(&v
, 0, &zPath
[i
+1], 0);
2163 if( JSON_BLOB_ISERROR(rc
) || v
.oom
){
2164 pParse
->oom
|= v
.oom
;
2169 if( jsonBlobMakeEditable(pParse
, v
.nBlob
) ){
2170 jsonBlobEdit(pParse
, j
, 0, v
.aBlob
, v
.nBlob
);
2173 if( pParse
->delta
) jsonAfterEditSizeAdjust(pParse
, iRoot
);
2177 return JSON_BLOB_PATHERROR
;
2179 return JSON_BLOB_NOTFOUND
;
2183 ** Convert a JSON BLOB into text and make that text the return value
2184 ** of an SQL function.
2186 static void jsonReturnTextJsonFromBlob(
2187 sqlite3_context
*ctx
,
2194 if( aBlob
==0 ) return;
2195 memset(&x
, 0, sizeof(x
));
2196 x
.aBlob
= (u8
*)aBlob
;
2198 jsonStringInit(&s
, ctx
);
2199 jsonXlateBlobToText(&x
, 0, &s
);
2200 jsonReturnString(&s
);
2205 ** Return the value of the BLOB node at index i.
2207 ** If the value is a primitive, return it as an SQL value.
2208 ** If the value is an array or object, return it as either
2209 ** JSON text or the BLOB encoding, depending on the JSON_B flag
2212 static void jsonReturnFromBlob(
2213 JsonParse
*pParse
, /* Complete JSON parse tree */
2214 u32 i
, /* Index of the node */
2215 sqlite3_context
*pCtx
, /* Return value for this function */
2216 int textOnly
/* return text JSON. Disregard user-data */
2220 sqlite3
*db
= sqlite3_context_db_handle(pCtx
);
2222 n
= jsonbPayloadSize(pParse
, i
, &sz
);
2224 switch( pParse
->aBlob
[i
] & 0x0f ){
2226 sqlite3_result_null(pCtx
);
2230 sqlite3_result_int(pCtx
, 1);
2234 sqlite3_result_int(pCtx
, 0);
2239 sqlite3_int64 iRes
= 0;
2242 char x
= (char)pParse
->aBlob
[i
+n
];
2243 if( x
=='-' && ALWAYS(sz
>0) ){ n
++; sz
--; bNeg
= 1; }
2244 z
= sqlite3DbStrNDup(db
, (const char*)&pParse
->aBlob
[i
+n
], (int)sz
);
2246 rc
= sqlite3DecOrHexToI64(z
, &iRes
);
2247 sqlite3DbFree(db
, z
);
2249 sqlite3_result_int64(pCtx
, bNeg
? -iRes
: iRes
);
2250 }else if( rc
==3 && bNeg
){
2251 sqlite3_result_int64(pCtx
, SMALLEST_INT64
);
2253 if( bNeg
){ n
--; sz
++; }
2263 z
= sqlite3DbStrNDup(db
, (const char*)&pParse
->aBlob
[i
+n
], (int)sz
);
2265 sqlite3AtoF(z
, &r
, sqlite3Strlen30(z
), SQLITE_UTF8
);
2266 sqlite3DbFree(db
, z
);
2267 sqlite3_result_double(pCtx
, r
);
2272 sqlite3_result_text(pCtx
, (char*)&pParse
->aBlob
[i
+n
], sz
,
2278 /* Translate JSON formatted string into raw text */
2283 z
= (const char*)&pParse
->aBlob
[i
+n
];
2284 zOut
= sqlite3_malloc( nOut
+1 );
2286 sqlite3_result_error_nomem(pCtx
);
2289 for(iIn
=iOut
=0; iIn
<sz
; iIn
++){
2294 u32 v
= jsonHexToInt4(z
+iIn
+1);
2298 zOut
[iOut
++] = (char)v
;
2299 }else if( v
<=0x7ff ){
2300 zOut
[iOut
++] = (char)(0xc0 | (v
>>6));
2301 zOut
[iOut
++] = 0x80 | (v
&0x3f);
2304 if( (v
&0xfc00)==0xd800
2308 && ((vlo
= jsonHexToInt4(z
+iIn
+3))&0xfc00)==0xdc00
2310 /* We have a surrogate pair */
2311 v
= ((v
&0x3ff)<<10) + (vlo
&0x3ff) + 0x10000;
2313 zOut
[iOut
++] = 0xf0 | (v
>>18);
2314 zOut
[iOut
++] = 0x80 | ((v
>>12)&0x3f);
2315 zOut
[iOut
++] = 0x80 | ((v
>>6)&0x3f);
2316 zOut
[iOut
++] = 0x80 | (v
&0x3f);
2318 zOut
[iOut
++] = 0xe0 | (v
>>12);
2319 zOut
[iOut
++] = 0x80 | ((v
>>6)&0x3f);
2320 zOut
[iOut
++] = 0x80 | (v
&0x3f);
2336 }else if( c
=='\'' || c
=='"' || c
=='/' || c
=='\\' ){
2337 /* pass through unchanged */
2341 c
= (jsonHexToInt(z
[iIn
+1])<<4) | jsonHexToInt(z
[iIn
+2]);
2343 }else if( c
=='\r' && z
[i
+1]=='\n' ){
2346 }else if( 0xe2==(u8
)c
){
2347 assert( 0x80==(u8
)z
[i
+1] );
2348 assert( 0xa8==(u8
)z
[i
+2] || 0xa9==(u8
)z
[i
+2] );
2354 } /* end if( c=='\\' ) */
2358 sqlite3_result_text(pCtx
, zOut
, iOut
, sqlite3_free
);
2362 case JSONB_OBJECT
: {
2363 int flags
= textOnly
? 0 : SQLITE_PTR_TO_INT(sqlite3_user_data(pCtx
));
2364 if( flags
& JSON_BLOB
){
2365 sqlite3_result_blob(pCtx
, &pParse
->aBlob
[i
], sz
+n
, SQLITE_TRANSIENT
);
2367 jsonReturnTextJsonFromBlob(pCtx
, &pParse
->aBlob
[i
], sz
+n
);
2372 sqlite3_result_error(pCtx
, "malformed JSON", -1);
2379 ** pArg is a function argument that might be an SQL value or a JSON
2380 ** value. Figure out what it is and encode it as a JSONB blob.
2381 ** Return the results in pParse.
2383 ** pParse is uninitialized upon entry. This routine will handle the
2384 ** initialization of pParse. The result will be contained in
2385 ** pParse->aBlob and pParse->nBlob. pParse->aBlob might be dynamically
2386 ** allocated (if pParse->nBlobAlloc is greater than zero) in which case
2387 ** the caller is responsible for freeing the space allocated to pParse->aBlob
2388 ** when it has finished with it. Or pParse->aBlob might be a static string
2389 ** or a value obtained from sqlite3_value_blob(pArg).
2391 ** If the argument is a BLOB that is clearly not a JSONB, then this
2392 ** function might set an error message in ctx and return non-zero.
2393 ** It might also set an error message and return non-zero on an OOM error.
2395 static int jsonFunctionArgToBlob(
2396 sqlite3_context
*ctx
,
2397 sqlite3_value
*pArg
,
2400 int eType
= sqlite3_value_type(pArg
);
2401 static u8 aNull
[] = { 0x00 };
2402 memset(pParse
, 0, sizeof(pParse
[0]));
2405 pParse
->aBlob
= aNull
;
2410 if( jsonFuncArgMightBeBinary(pArg
) ){
2411 pParse
->aBlob
= (u8
*)sqlite3_value_blob(pArg
);
2412 pParse
->nBlob
= sqlite3_value_bytes(pArg
);
2414 sqlite3_result_error(ctx
, "JSON cannot hold BLOB values", -1);
2420 const char *zJson
= (const char*)sqlite3_value_text(pArg
);
2421 int nJson
= sqlite3_value_bytes(pArg
);
2422 if( zJson
==0 ) return 1;
2423 if( sqlite3_value_subtype(pArg
)==JSON_SUBTYPE
){
2424 pParse
->zJson
= (char*)zJson
;
2425 pParse
->nJson
= nJson
;
2426 if( jsonConvertTextToBlob(pParse
, ctx
) ){
2427 sqlite3_result_error(ctx
, "malformed JSON", -1);
2428 sqlite3_free(pParse
->aBlob
);
2429 memset(pParse
, 0, sizeof(pParse
[0]));
2433 jsonBlobAppendNodeType(pParse
, JSONB_TEXTRAW
, nJson
);
2434 jsonBlobAppendNBytes(pParse
, (const u8
*)zJson
, nJson
);
2439 case SQLITE_INTEGER
: {
2440 int n
= sqlite3_value_bytes(pArg
);
2441 const char *z
= (const char*)sqlite3_value_text(pArg
);
2442 int e
= eType
==SQLITE_INTEGER
? JSONB_INT
: JSONB_FLOAT
;
2443 if( z
==0 ) return 1;
2444 jsonBlobAppendNodeType(pParse
, e
, n
);
2445 jsonBlobAppendNBytes(pParse
, (const u8
*)z
, n
);
2450 sqlite3_result_error_nomem(ctx
);
2458 ** Generate a bad path error for json_extract()
2460 static void jsonBadPathError(
2461 sqlite3_context
*ctx
, /* The function call containing the error */
2462 const char *zPath
/* The path with the problem */
2464 sqlite3
*db
= sqlite3_context_db_handle(ctx
);
2465 char *zMsg
= sqlite3MPrintf(db
, "bad JSON path: %Q", zPath
);
2466 sqlite3_result_error(ctx
, zMsg
, -1);
2467 sqlite3DbFree(db
, zMsg
);
2470 /* argv[0] is a BLOB that seems likely to be a JSONB. Subsequent
2471 ** arguments come in parse where each pair contains a JSON path and
2472 ** content to insert or set at that patch. Do the updates
2473 ** and return the result.
2475 ** The specific operation is determined by eEdit, which can be one
2476 ** of JEDIT_INS, JEDIT_REPL, or JEDIT_SET.
2478 static void jsonInsertIntoBlob(
2479 sqlite3_context
*ctx
,
2481 sqlite3_value
**argv
,
2482 int eEdit
/* JEDIT_INS, JEDIT_REPL, or JEDIT_SET */
2486 const char *zPath
= 0;
2491 assert( (argc
&1)==1 );
2492 flgs
= argc
==1 ? 0 : JSON_EDITABLE
;
2493 p
= jsonParseFuncArg(ctx
, argv
[0], flgs
);
2495 for(i
=1; i
<argc
-1; i
+=2){
2496 if( sqlite3_value_type(argv
[i
])==SQLITE_NULL
) continue;
2497 zPath
= (const char*)sqlite3_value_text(argv
[i
]);
2499 sqlite3_result_error_nomem(ctx
);
2503 if( zPath
[0]!='$' ) goto jsonInsertIntoBlob_patherror
;
2504 if( jsonFunctionArgToBlob(ctx
, argv
[i
+1], &ax
) ){
2505 jsonParseReset(&ax
);
2510 if( eEdit
==JEDIT_REPL
|| eEdit
==JEDIT_SET
){
2511 jsonBlobEdit(p
, 0, p
->nBlob
, ax
.aBlob
, ax
.nBlob
);
2519 rc
= jsonLookupBlobStep(p
, 0, zPath
+1, 0);
2521 jsonParseReset(&ax
);
2522 if( rc
==JSON_BLOB_NOTFOUND
) continue;
2523 if( JSON_BLOB_ISERROR(rc
) ) goto jsonInsertIntoBlob_patherror
;
2525 jsonReturnParse(ctx
, p
);
2529 jsonInsertIntoBlob_patherror
:
2531 if( rc
==JSON_BLOB_ERROR
){
2532 sqlite3_result_error(ctx
, "malformed JSON", -1);
2534 jsonBadPathError(ctx
, zPath
);
2540 ** Generate a JsonParse object, containing valid JSONB in aBlob and nBlob,
2541 ** from the SQL function argument pArg. Return a pointer to the new
2542 ** JsonParse object.
2544 ** Ownership of the new JsonParse object is passed to the caller. The
2545 ** caller should invoke jsonParseFree() on the return value when it
2546 ** has finished using it.
2548 ** If any errors are detected, an appropriate error messages is set
2549 ** using sqlite3_result_error() or the equivalent and this routine
2550 ** returns NULL. This routine also returns NULL if the pArg argument
2551 ** is an SQL NULL value, but no error message is set in that case. This
2552 ** is so that SQL functions that are given NULL arguments will return
2555 static JsonParse
*jsonParseFuncArg(
2556 sqlite3_context
*ctx
,
2557 sqlite3_value
*pArg
,
2560 int eType
; /* Datatype of pArg */
2561 JsonParse
*p
= 0; /* Value to be returned */
2564 eType
= sqlite3_value_type(pArg
);
2565 if( eType
==SQLITE_NULL
){
2568 p
= sqlite3_malloc64( sizeof(*p
) );
2569 if( p
==0 ) goto json_pfa_oom
;
2570 memset(p
, 0, sizeof(*p
));
2571 if( eType
==SQLITE_BLOB
){
2573 p
->aBlob
= (u8
*)sqlite3_value_blob(pArg
);
2574 p
->nBlob
= (u32
)sqlite3_value_bytes(pArg
);
2576 goto json_pfa_malformed
;
2581 if( (p
->aBlob
[0] & 0x0f)>JSONB_OBJECT
){
2582 goto json_pfa_malformed
;
2584 n
= jsonbPayloadSize(p
, 0, &sz
);
2587 || ((p
->aBlob
[0] & 0x0f)<=JSONB_FALSE
&& sz
>0)
2590 goto json_pfa_malformed
;
2592 if( (flgs
& JSON_EDITABLE
)!=0 && jsonBlobMakeEditable(p
, 0)==0 ){
2597 /* TODO: Check in the cache */
2598 p
->zJson
= (char*)sqlite3_value_text(pArg
);
2599 p
->nJson
= sqlite3_value_bytes(pArg
);
2600 if( p
->nJson
==0 ) goto json_pfa_malformed
;
2601 if( p
->zJson
==0 ) goto json_pfa_oom
;
2602 if( flgs
& JSON_KEEPERROR
) ctx
= 0;
2603 if( jsonConvertTextToBlob(p
, ctx
) ){
2604 if( flgs
& JSON_KEEPERROR
){
2615 if( flgs
& JSON_KEEPERROR
){
2620 sqlite3_result_error(ctx
, "malformed JSON", -1);
2626 sqlite3_result_error_nomem(ctx
);
2631 ** Make the return value of a JSON function either the raw JSONB blob
2632 ** or make it JSON text, depending on whether the JSON_BLOB flag is
2633 ** set on the function.
2635 static void jsonReturnParse(
2636 sqlite3_context
*ctx
,
2641 sqlite3_result_error_nomem(ctx
);
2644 flgs
= SQLITE_PTR_TO_INT(sqlite3_user_data(ctx
));
2645 if( flgs
& JSON_BLOB
){
2646 sqlite3_result_blob(ctx
, p
->aBlob
, p
->nBlob
,
2647 p
->nBlobAlloc
>0 ? SQLITE_DYNAMIC
: SQLITE_TRANSIENT
);
2651 jsonStringInit(&s
, ctx
);
2652 jsonXlateBlobToText(p
, 0, &s
);
2653 jsonReturnString(&s
);
2654 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
2658 /****************************************************************************
2659 ** SQL functions used for testing and debugging
2660 ****************************************************************************/
2664 ** Decode JSONB bytes in aBlob[] starting at iStart through but not
2665 ** including iEnd. Indent the
2666 ** content by nIndent spaces.
2668 static void jsonDebugPrintBlob(
2669 JsonParse
*pParse
, /* JSON content */
2670 u32 iStart
, /* Start rendering here */
2671 u32 iEnd
, /* Do not render this byte or any byte after this one */
2672 int nIndent
/* Indent by this many spaces */
2674 while( iStart
<iEnd
){
2675 u32 i
, n
, nn
, sz
= 0;
2676 int showContent
= 1;
2677 u8 x
= pParse
->aBlob
[iStart
] & 0x0f;
2678 u32 savedNBlob
= pParse
->nBlob
;
2679 printf("%5d:%*s", iStart
, nIndent
, "");
2680 if( pParse
->nBlobAlloc
>pParse
->nBlob
){
2681 pParse
->nBlob
= pParse
->nBlobAlloc
;
2683 nn
= n
= jsonbPayloadSize(pParse
, iStart
, &sz
);
2685 if( sz
>0 && x
<JSONB_ARRAY
){
2688 for(i
=0; i
<nn
; i
++) printf(" %02x", pParse
->aBlob
[iStart
+i
]);
2690 printf(" ERROR invalid node size\n");
2691 iStart
= n
==0 ? iStart
+1 : iEnd
;
2694 pParse
->nBlob
= savedNBlob
;
2695 if( iStart
+n
+sz
>iEnd
){
2697 if( iEnd
>pParse
->nBlob
){
2698 if( pParse
->nBlobAlloc
>0 && iEnd
>pParse
->nBlobAlloc
){
2699 iEnd
= pParse
->nBlobAlloc
;
2701 iEnd
= pParse
->nBlob
;
2707 case JSONB_NULL
: printf("null"); break;
2708 case JSONB_TRUE
: printf("true"); break;
2709 case JSONB_FALSE
: printf("false"); break;
2710 case JSONB_INT
: printf("int"); break;
2711 case JSONB_INT5
: printf("int5"); break;
2712 case JSONB_FLOAT
: printf("float"); break;
2713 case JSONB_FLOAT5
: printf("float5"); break;
2714 case JSONB_TEXT
: printf("text"); break;
2715 case JSONB_TEXTJ
: printf("textj"); break;
2716 case JSONB_TEXT5
: printf("text5"); break;
2717 case JSONB_TEXTRAW
: printf("textraw"); break;
2719 printf("array, %u bytes\n", sz
);
2720 jsonDebugPrintBlob(pParse
, iStart
+n
, iStart
+n
+sz
, nIndent
+2);
2724 case JSONB_OBJECT
: {
2725 printf("object, %u bytes\n", sz
);
2726 jsonDebugPrintBlob(pParse
, iStart
+n
, iStart
+n
+sz
, nIndent
+2);
2731 printf("ERROR: unknown node type\n");
2737 if( sz
==0 && x
<=JSONB_FALSE
){
2742 for(i
=iStart
+n
; i
<iStart
+n
+sz
; i
++){
2743 u8 c
= pParse
->aBlob
[i
];
2744 if( c
<0x20 || c
>=0x7f ) c
= '.';
2753 static void jsonShowParse(JsonParse
*pParse
){
2755 printf("NULL pointer\n");
2758 printf("nBlobAlloc = %u\n", pParse
->nBlobAlloc
);
2759 printf("nBlob = %u\n", pParse
->nBlob
);
2760 printf("delta = %d\n", pParse
->delta
);
2761 if( pParse
->nBlob
==0 ) return;
2762 printf("content (bytes 0..%u):\n", pParse
->nBlob
-1);
2764 jsonDebugPrintBlob(pParse
, 0, pParse
->nBlob
, 0);
2766 #endif /* SQLITE_DEBUG */
2770 ** SQL function: json_parse(JSON)
2772 ** Parse JSON using jsonParseFuncArg(). Then print a dump of that
2773 ** parse on standard output.
2775 static void jsonParseFunc(
2776 sqlite3_context
*ctx
,
2778 sqlite3_value
**argv
2780 JsonParse
*p
; /* The parse */
2783 p
= jsonParseFuncArg(ctx
, argv
[0], 0);
2789 ** The json_test1(JSON) function return true (1) if the input is JSON
2790 ** text generated by another json function. It returns (0) if the input
2791 ** is not known to be JSON.
2793 static void jsonTest1Func(
2794 sqlite3_context
*ctx
,
2796 sqlite3_value
**argv
2798 UNUSED_PARAMETER(argc
);
2799 sqlite3_result_int(ctx
, sqlite3_value_subtype(argv
[0])==JSON_SUBTYPE
);
2802 /* SQL Function: jsonb_test2(BLOB_JSON)
2804 ** Render BLOB_JSON back into text.
2805 ** Development testing only.
2807 static void jsonbTest2(
2808 sqlite3_context
*ctx
,
2810 sqlite3_value
**argv
2814 UNUSED_PARAMETER(argc
);
2816 aBlob
= (const u8
*)sqlite3_value_blob(argv
[0]);
2817 nBlob
= sqlite3_value_bytes(argv
[0]);
2818 jsonReturnTextJsonFromBlob(ctx
, aBlob
, nBlob
);
2820 #endif /* SQLITE_DEBUG */
2822 /****************************************************************************
2823 ** Scalar SQL function implementations
2824 ****************************************************************************/
2826 /* SQL Function: jsonb(JSON)
2828 ** Convert the input argument into JSONB (the SQLite binary encoding of
2831 ** If the input is TEXT, or NUMERIC, try to parse it as JSON. If the fails,
2832 ** raise an error. Otherwise, return the resulting BLOB value.
2834 ** If the input is a BLOB, check to see if the input is a plausible
2835 ** JSONB. If it is, return it unchanged. Raise an error if it is not.
2836 ** Note that there could be internal inconsistencies in the BLOB - this
2837 ** routine does not do a full byte-for-byte validity check of the
2840 ** If the input is NULL, return NULL.
2842 static void jsonbFunc(
2843 sqlite3_context
*ctx
,
2845 sqlite3_value
**argv
2851 UNUSED_PARAMETER(argc
);
2853 if( sqlite3_value_type(argv
[0])==SQLITE_NULL
){
2855 }else if( jsonFuncArgMightBeBinary(argv
[0]) ){
2856 sqlite3_result_value(ctx
, argv
[0]);
2858 zJson
= (const char*)sqlite3_value_text(argv
[0]);
2859 if( zJson
==0 ) return;
2860 nJson
= sqlite3_value_bytes(argv
[0]);
2862 memset(&x
, 0, sizeof(x
));
2863 x
.zJson
= (char*)zJson
;
2865 if( jsonConvertTextToBlob(pParse
, ctx
) ){
2866 sqlite3_result_error(ctx
, "malformed JSON", -1);
2868 sqlite3_result_blob(ctx
, pParse
->aBlob
, pParse
->nBlob
, sqlite3_free
);
2871 pParse
->nBlobAlloc
= 0;
2873 jsonParseReset(pParse
);
2878 ** Implementation of the json_quote(VALUE) function. Return a JSON value
2879 ** corresponding to the SQL value input. Mostly this means putting
2880 ** double-quotes around strings and returning the unquoted string "null"
2881 ** when given a NULL input.
2883 static void jsonQuoteFunc(
2884 sqlite3_context
*ctx
,
2886 sqlite3_value
**argv
2889 UNUSED_PARAMETER(argc
);
2891 jsonStringInit(&jx
, ctx
);
2892 jsonAppendSqlValue(&jx
, argv
[0]);
2893 jsonReturnString(&jx
);
2894 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
2898 ** Implementation of the json_array(VALUE,...) function. Return a JSON
2899 ** array that contains all values given in arguments. Or if any argument
2900 ** is a BLOB, throw an error.
2902 static void jsonArrayFunc(
2903 sqlite3_context
*ctx
,
2905 sqlite3_value
**argv
2910 jsonStringInit(&jx
, ctx
);
2911 jsonAppendChar(&jx
, '[');
2912 for(i
=0; i
<argc
; i
++){
2913 jsonAppendSeparator(&jx
);
2914 jsonAppendSqlValue(&jx
, argv
[i
]);
2916 jsonAppendChar(&jx
, ']');
2917 jsonReturnString(&jx
);
2918 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
2922 ** json_array_length(JSON)
2923 ** json_array_length(JSON, PATH)
2925 ** Return the number of elements in the top-level JSON array.
2926 ** Return 0 if the input is not a well-formed JSON array.
2928 static void jsonArrayLengthFunc(
2929 sqlite3_context
*ctx
,
2931 sqlite3_value
**argv
2933 JsonParse
*p
; /* The parse */
2934 sqlite3_int64 cnt
= 0;
2938 p
= jsonParseFuncArg(ctx
, argv
[0], 0);
2941 const char *zPath
= (const char*)sqlite3_value_text(argv
[1]);
2946 i
= jsonLookupBlobStep(p
, 0, zPath
[0]=='$' ? zPath
+1 : "@", 0);
2947 if( JSON_BLOB_ISERROR(i
) ){
2948 if( i
==JSON_BLOB_NOTFOUND
){
2950 }else if( i
==JSON_BLOB_PATHERROR
){
2951 jsonBadPathError(ctx
, zPath
);
2953 sqlite3_result_error(ctx
, "malformed JSON", -1);
2961 if( (p
->aBlob
[i
] & 0x0f)==JSONB_ARRAY
){
2962 u32 n
, sz
= 0, iEnd
;
2963 n
= jsonbPayloadSize(p
, i
, &sz
);
2964 if( n
==0 ) eErr
= 2;
2967 while( eErr
==0 && i
<iEnd
){
2969 n
= jsonbPayloadSize(p
, i
, &sz
);
2970 if( n
==0 ) eErr
= 2;
2975 if( eErr
==2 ) sqlite3_result_error(ctx
, "malformed JSON", -1);
2977 sqlite3_result_int64(ctx
, cnt
);
2983 ** json_extract(JSON, PATH, ...)
2987 ** Return the element described by PATH. Return NULL if that PATH element
2990 ** If JSON_JSON is set or if more that one PATH argument is supplied then
2991 ** always return a JSON representation of the result. If JSON_SQL is set,
2992 ** then always return an SQL representation of the result. If neither flag
2993 ** is present and argc==2, then return JSON for objects and arrays and SQL
2994 ** for all other values.
2996 ** When multiple PATH arguments are supplied, the result is a JSON array
2997 ** containing the result of each PATH.
2999 ** Abbreviated JSON path expressions are allows if JSON_ABPATH, for
3000 ** compatibility with PG.
3002 static void jsonExtractFunc(
3003 sqlite3_context
*ctx
,
3005 sqlite3_value
**argv
3007 JsonParse
*p
= 0; /* The parse */
3008 int flags
; /* Flags associated with the function */
3009 int i
; /* Loop counter */
3010 JsonString jx
; /* String for array result */
3012 if( argc
<2 ) return;
3013 p
= jsonParseFuncArg(ctx
, argv
[0], 0);
3015 flags
= SQLITE_PTR_TO_INT(sqlite3_user_data(ctx
));
3016 jsonStringInit(&jx
, ctx
);
3018 jsonAppendChar(&jx
, '[');
3020 for(i
=1; i
<argc
; i
++){
3021 /* With a single PATH argument */
3022 const char *zPath
= (const char*)sqlite3_value_text(argv
[i
]);
3023 int nPath
= sqlite3_value_bytes(argv
[i
]);
3025 if( zPath
==0 ) goto json_extract_error
;
3026 if( zPath
[0]=='$' ){
3027 j
= jsonLookupBlobStep(p
, 0, zPath
+1, 0);
3028 }else if( (flags
& JSON_ABPATH
) ){
3029 /* The -> and ->> operators accept abbreviated PATH arguments. This
3030 ** is mostly for compatibility with PostgreSQL, but also for
3033 ** NUMBER ==> $[NUMBER] // PG compatible
3034 ** LABEL ==> $.LABEL // PG compatible
3035 ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience
3037 jsonStringInit(&jx
, ctx
);
3038 if( sqlite3Isdigit(zPath
[0]) ){
3039 jsonAppendRawNZ(&jx
, "[", 1);
3040 jsonAppendRaw(&jx
, zPath
, nPath
);
3041 jsonAppendRawNZ(&jx
, "]", 2);
3042 }else if( zPath
[0]!='[' ){
3043 jsonAppendRawNZ(&jx
, ".", 1);
3044 jsonAppendRaw(&jx
, zPath
, nPath
);
3045 jsonAppendChar(&jx
, 0);
3047 jsonAppendRaw(&jx
, zPath
, nPath
);
3049 jsonStringTerminate(&jx
);
3050 j
= jsonLookupBlobStep(p
, 0, jx
.zBuf
, 0);
3051 jsonStringReset(&jx
);
3053 jsonBadPathError(ctx
, zPath
);
3054 goto json_extract_error
;
3058 if( flags
& JSON_JSON
){
3059 jsonStringInit(&jx
, ctx
);
3060 jsonXlateBlobToText(p
, j
, &jx
);
3061 jsonReturnString(&jx
);
3062 jsonStringReset(&jx
);
3063 assert( (flags
& JSON_BLOB
)==0 );
3064 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
3066 jsonReturnFromBlob(p
, j
, ctx
, 0);
3067 if( (flags
& (JSON_SQL
|JSON_BLOB
))==0
3068 && (p
->aBlob
[j
]&0x0f)>=JSONB_ARRAY
3070 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
3074 jsonAppendSeparator(&jx
);
3075 jsonXlateBlobToText(p
, j
, &jx
);
3077 }else if( j
==JSON_BLOB_NOTFOUND
){
3079 goto json_extract_error
; /* Return NULL if not found */
3081 jsonAppendSeparator(&jx
);
3082 jsonAppendRawNZ(&jx
, "null", 4);
3084 }else if( j
==JSON_BLOB_ERROR
){
3085 sqlite3_result_error(ctx
, "malformed JSON", -1);
3086 goto json_extract_error
;
3088 jsonBadPathError(ctx
, zPath
);
3089 goto json_extract_error
;
3093 jsonAppendChar(&jx
, ']');
3094 jsonReturnString(&jx
);
3095 if( (flags
& JSON_BLOB
)==0 ){
3096 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
3100 jsonStringReset(&jx
);
3106 ** Return codes for jsonMergePatchBlob()
3108 #define JSON_MERGE_OK 0 /* Success */
3109 #define JSON_MERGE_BADTARGET 1 /* Malformed TARGET blob */
3110 #define JSON_MERGE_BADPATCH 2 /* Malformed PATCH blob */
3111 #define JSON_MERGE_OOM 3 /* Out-of-memory condition */
3114 ** RFC-7396 MergePatch for two JSONB blobs.
3116 ** pTarget is the target. pPatch is the patch. The target is updated
3117 ** in place. The patch is read-only.
3119 ** The original RFC-7396 algorithm is this:
3121 ** define MergePatch(Target, Patch):
3122 ** if Patch is an Object:
3123 ** if Target is not an Object:
3124 ** Target = {} # Ignore the contents and set it to an empty Object
3125 ** for each Name/Value pair in Patch:
3126 ** if Value is null:
3127 ** if Name exists in Target:
3128 ** remove the Name/Value pair from Target
3130 ** Target[Name] = MergePatch(Target[Name], Value)
3135 ** Here is an equivalent algorithm restructured to show the actual
3138 ** 01 define MergePatch(Target, Patch):
3139 ** 02 if Patch is not an Object:
3141 ** 04 else: // if Patch is an Object
3142 ** 05 if Target is not an Object:
3144 ** 07 for each Name/Value pair in Patch:
3145 ** 08 if Name exists in Target:
3146 ** 09 if Value is null:
3147 ** 10 remove the Name/Value pair from Target
3149 ** 12 Target[name] = MergePatch(Target[Name], Value)
3150 ** 13 else if Value is not NULL:
3151 ** 14 if Value is not an Object:
3152 ** 15 Target[name] = Value
3154 ** 17 Target[name] = MergePatch('{}',value)
3157 ** ^---- Line numbers referenced in comments in the implementation
3159 static int jsonMergePatchBlob(
3160 JsonParse
*pTarget
, /* The JSON parser that contains the TARGET */
3161 u32 iTarget
, /* Index of TARGET in pTarget->aBlob[] */
3162 const JsonParse
*pPatch
, /* The PATCH */
3163 u32 iPatch
/* Index of PATCH in pPatch->aBlob[] */
3165 u8 x
; /* Type of a single node */
3166 u32 n
, sz
=0; /* Return values from jsonbPayloadSize() */
3167 u32 iTCursor
; /* Cursor position while scanning the target object */
3168 u32 iTStart
; /* First label in the target object */
3169 u32 iTEndBE
; /* Original first byte past end of target, before edit */
3170 u32 iTEnd
; /* Current first byte past end of target */
3171 u8 eTLabel
; /* Node type of the target label */
3172 u32 iTLabel
; /* Index of the label */
3173 u32 nTLabel
; /* Header size in bytes for the target label */
3174 u32 szTLabel
; /* Size of the target label payload */
3175 u32 iTValue
; /* Index of the target value */
3176 u32 nTValue
; /* Header size of the target value */
3177 u32 szTValue
; /* Payload size for the target value */
3179 u32 iPCursor
; /* Cursor position while scanning the patch */
3180 u32 iPEnd
; /* First byte past the end of the patch */
3181 u8 ePLabel
; /* Node type of the patch label */
3182 u32 iPLabel
; /* Start of patch label */
3183 u32 nPLabel
; /* Size of header on the patch label */
3184 u32 szPLabel
; /* Payload size of the patch label */
3185 u32 iPValue
; /* Start of patch value */
3186 u32 nPValue
; /* Header size for the patch value */
3187 u32 szPValue
; /* Payload size of the patch value */
3189 assert( iTarget
>=0 && iTarget
<pTarget
->nBlob
);
3190 assert( iPatch
>=0 && iPatch
<pPatch
->nBlob
);
3191 x
= pPatch
->aBlob
[iPatch
] & 0x0f;
3192 if( x
!=JSONB_OBJECT
){ /* Algorithm line 02 */
3193 u32 szPatch
; /* Total size of the patch, header+payload */
3194 u32 szTarget
; /* Total size of the target, header+payload */
3195 n
= jsonbPayloadSize(pPatch
, iPatch
, &sz
);
3198 n
= jsonbPayloadSize(pTarget
, iTarget
, &sz
);
3200 jsonBlobEdit(pTarget
, iTarget
, szTarget
, pPatch
->aBlob
+iPatch
, szPatch
);
3201 return pTarget
->oom
? JSON_MERGE_OOM
: JSON_MERGE_OK
; /* Line 03 */
3203 x
= pTarget
->aBlob
[iTarget
] & 0x0f;
3204 if( x
!=JSONB_OBJECT
){ /* Algorithm line 05 */
3205 n
= jsonbPayloadSize(pTarget
, iTarget
, &sz
);
3206 jsonBlobEdit(pTarget
, iTarget
+n
, sz
, 0, 0);
3207 x
= pTarget
->aBlob
[iTarget
];
3208 pTarget
->aBlob
[iTarget
] = (x
& 0xf0) | JSONB_OBJECT
;
3210 n
= jsonbPayloadSize(pPatch
, iPatch
, &sz
);
3211 if( n
==0 ) return JSON_MERGE_BADPATCH
;
3212 iPCursor
= iPatch
+n
;
3213 iPEnd
= iPCursor
+sz
;
3214 n
= jsonbPayloadSize(pTarget
, iTarget
, &sz
);
3215 if( n
==0 ) return JSON_MERGE_BADTARGET
;
3216 iTStart
= iTarget
+n
;
3217 iTEndBE
= iTStart
+sz
;
3219 while( iPCursor
<iPEnd
){ /* Algorithm line 07 */
3221 ePLabel
= pPatch
->aBlob
[iPCursor
] & 0x0f;
3222 if( ePLabel
<JSONB_TEXT
|| ePLabel
>JSONB_TEXTRAW
){
3223 return JSON_MERGE_BADPATCH
;
3225 nPLabel
= jsonbPayloadSize(pPatch
, iPCursor
, &szPLabel
);
3226 if( nPLabel
==0 ) return JSON_MERGE_BADPATCH
;
3227 iPValue
= iPCursor
+ nPLabel
+ szPLabel
;
3228 if( iPCursor
>=iPEnd
) return JSON_MERGE_BADPATCH
;
3229 nPValue
= jsonbPayloadSize(pPatch
, iPValue
, &szPValue
);
3230 if( nPValue
==0 ) return JSON_MERGE_BADPATCH
;
3231 iPCursor
= iPValue
+ nPValue
+ szPValue
;
3232 if( iPCursor
>iPEnd
) return JSON_MERGE_BADPATCH
;
3235 iTEnd
= iTEndBE
+ pTarget
->delta
;
3236 while( iTCursor
<iTEnd
){
3238 eTLabel
= pTarget
->aBlob
[iTCursor
] & 0x0f;
3239 if( eTLabel
<JSONB_TEXT
|| eTLabel
>JSONB_TEXTRAW
){
3240 return JSON_MERGE_BADTARGET
;
3242 nTLabel
= jsonbPayloadSize(pTarget
, iTCursor
, &szTLabel
);
3243 if( nTLabel
==0 ) return JSON_MERGE_BADTARGET
;
3244 iTValue
= iTLabel
+ nTLabel
+ szTLabel
;
3245 if( iTValue
>=iTEnd
) return JSON_MERGE_BADTARGET
;
3246 nTValue
= jsonbPayloadSize(pTarget
, iTValue
, &szTValue
);
3247 if( nTValue
==0 ) return JSON_MERGE_BADTARGET
;
3248 if( iTValue
+ nTValue
+ szTValue
> iTEnd
) return JSON_MERGE_BADTARGET
;
3249 if( eTLabel
==ePLabel
){
3251 if( szTLabel
==szPLabel
3252 && memcmp(&pTarget
->aBlob
[iTLabel
+nTLabel
],
3253 &pPatch
->aBlob
[iPLabel
+nPLabel
], szTLabel
)==0
3255 break; /* Labels match. */
3258 /* Should rarely happen */
3261 jsonStringInit(&s1
, 0);
3262 jsonXlateBlobToText(pTarget
, iTLabel
, &s1
);
3263 if( s1
.eErr
) return JSON_MERGE_OOM
;
3264 jsonStringInit(&s2
, 0);
3265 jsonXlateBlobToText(pPatch
, iPLabel
, &s2
);
3266 if( s2
.eErr
) return JSON_MERGE_OOM
;
3267 if( s1
.nUsed
==s2
.nUsed
&& memcmp(s1
.zBuf
, s2
.zBuf
, s1
.nUsed
)==0 ){
3272 jsonStringReset(&s1
);
3273 jsonStringReset(&s2
);
3274 if( isEqual
) break;
3276 iTCursor
= iTValue
+ nTValue
+ szTValue
;
3278 x
= pPatch
->aBlob
[iPValue
] & 0x0f;
3279 if( iTCursor
<iTEnd
){
3280 /* A match was found. Algorithm line 08 */
3282 /* Patch value is NULL. Algorithm line 09 */
3283 jsonBlobEdit(pTarget
, iTLabel
, nTLabel
+szTLabel
+nTValue
+szTValue
,
3285 if( pTarget
->oom
) return JSON_MERGE_OOM
;
3287 /* Algorithm line 12 */
3288 int rc
, savedDelta
= pTarget
->delta
;
3290 rc
= jsonMergePatchBlob(pTarget
, iTValue
, pPatch
, iPValue
);
3292 pTarget
->delta
+= savedDelta
;
3294 }else if( x
>0 ){ /* Algorithm line 13 */
3295 /* No match and patch value is not NULL */
3296 u32 szNew
= szPLabel
+nPLabel
;
3297 if( (pPatch
->aBlob
[iPValue
] & 0x0f)!=JSONB_OBJECT
){ /* Line 14 */
3298 jsonBlobEdit(pTarget
, iTEnd
, 0, 0, szPValue
+nPValue
+szNew
);
3299 if( pTarget
->oom
) return JSON_MERGE_OOM
;
3300 memcpy(&pTarget
->aBlob
[iTEnd
], &pPatch
->aBlob
[iPLabel
], szNew
);
3301 memcpy(&pTarget
->aBlob
[iTEnd
+szNew
],
3302 &pPatch
->aBlob
[iPValue
], szPValue
+nPValue
);
3305 jsonBlobEdit(pTarget
, iTEnd
, 0, 0, szNew
+1);
3306 if( pTarget
->oom
) return JSON_MERGE_OOM
;
3307 memcpy(&pTarget
->aBlob
[iTEnd
], &pPatch
->aBlob
[iPLabel
], szNew
);
3308 pTarget
->aBlob
[iTEnd
+szNew
] = 0x00;
3309 savedDelta
= pTarget
->delta
;
3311 rc
= jsonMergePatchBlob(pTarget
, iTEnd
+szNew
,pPatch
,iPValue
);
3313 pTarget
->delta
+= savedDelta
;
3317 if( pTarget
->delta
) jsonAfterEditSizeAdjust(pTarget
, iTarget
);
3318 return pTarget
->oom
? JSON_MERGE_OOM
: JSON_MERGE_OK
;
3323 ** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
3324 ** object that is the result of running the RFC 7396 MergePatch() algorithm
3325 ** on the two arguments.
3327 static void jsonPatchFunc(
3328 sqlite3_context
*ctx
,
3330 sqlite3_value
**argv
3332 JsonParse
*pTarget
; /* The TARGET */
3333 JsonParse
*pPatch
; /* The PATCH */
3334 int rc
; /* Result code */
3336 UNUSED_PARAMETER(argc
);
3338 pTarget
= jsonParseFuncArg(ctx
, argv
[0], JSON_EDITABLE
);
3339 if( pTarget
==0 ) return;
3340 pPatch
= jsonParseFuncArg(ctx
, argv
[1], 0);
3342 rc
= jsonMergePatchBlob(pTarget
, 0, pPatch
, 0);
3343 if( rc
==JSON_MERGE_OK
){
3344 jsonReturnParse(ctx
, pTarget
);
3345 }else if( rc
==JSON_MERGE_OOM
){
3346 sqlite3_result_error_nomem(ctx
);
3348 sqlite3_result_error(ctx
, "malformed JSON", -1);
3350 jsonParseFree(pPatch
);
3352 jsonParseFree(pTarget
);
3357 ** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
3358 ** object that contains all name/value given in arguments. Or if any name
3359 ** is not a string or if any value is a BLOB, throw an error.
3361 static void jsonObjectFunc(
3362 sqlite3_context
*ctx
,
3364 sqlite3_value
**argv
3372 sqlite3_result_error(ctx
, "json_object() requires an even number "
3373 "of arguments", -1);
3376 jsonStringInit(&jx
, ctx
);
3377 jsonAppendChar(&jx
, '{');
3378 for(i
=0; i
<argc
; i
+=2){
3379 if( sqlite3_value_type(argv
[i
])!=SQLITE_TEXT
){
3380 sqlite3_result_error(ctx
, "json_object() labels must be TEXT", -1);
3381 jsonStringReset(&jx
);
3384 jsonAppendSeparator(&jx
);
3385 z
= (const char*)sqlite3_value_text(argv
[i
]);
3386 n
= (u32
)sqlite3_value_bytes(argv
[i
]);
3387 jsonAppendString(&jx
, z
, n
);
3388 jsonAppendChar(&jx
, ':');
3389 jsonAppendSqlValue(&jx
, argv
[i
+1]);
3391 jsonAppendChar(&jx
, '}');
3392 jsonReturnString(&jx
);
3393 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
3398 ** json_remove(JSON, PATH, ...)
3400 ** Remove the named elements from JSON and return the result. malformed
3401 ** JSON or PATH arguments result in an error.
3403 static void jsonRemoveFunc(
3404 sqlite3_context
*ctx
,
3406 sqlite3_value
**argv
3408 JsonParse
*p
; /* The parse */
3409 const char *zPath
= 0; /* Path of element to be removed */
3410 u32 i
; /* Loop counter */
3411 int rc
; /* Subroutine return code */
3413 if( argc
<1 ) return;
3414 p
= jsonParseFuncArg(ctx
, argv
[0], JSON_EDITABLE
);
3416 for(i
=1; i
<argc
; i
++){
3417 if( sqlite3_value_type(argv
[i
])==SQLITE_NULL
){
3418 goto json_remove_return_null
;
3420 zPath
= (const char*)sqlite3_value_text(argv
[i
]);
3422 goto json_remove_patherror
;
3424 if( zPath
[0]!='$' ){
3425 goto json_remove_patherror
;
3428 /* json_remove(j,'$') returns NULL */
3429 goto json_remove_return_null
;
3431 p
->eEdit
= JEDIT_DEL
;
3433 rc
= jsonLookupBlobStep(p
, 0, zPath
+1, 0);
3434 if( rc
==JSON_BLOB_NOTFOUND
) continue;
3435 if( JSON_BLOB_ISERROR(rc
) ) goto json_remove_patherror
;
3437 jsonReturnParse(ctx
, p
);
3441 json_remove_patherror
:
3443 jsonPathSyntaxError(zPath
, ctx
);
3446 json_remove_return_null
:
3452 ** json_replace(JSON, PATH, VALUE, ...)
3454 ** Replace the value at PATH with VALUE. If PATH does not already exist,
3455 ** this routine is a no-op. If JSON or PATH is malformed, throw an error.
3457 static void jsonReplaceFunc(
3458 sqlite3_context
*ctx
,
3460 sqlite3_value
**argv
3462 if( argc
<1 ) return;
3464 jsonWrongNumArgs(ctx
, "replace");
3467 jsonInsertIntoBlob(ctx
, argc
, argv
, JEDIT_REPL
);
3472 ** json_set(JSON, PATH, VALUE, ...)
3474 ** Set the value at PATH to VALUE. Create the PATH if it does not already
3475 ** exist. Overwrite existing values that do exist.
3476 ** If JSON or PATH is malformed, throw an error.
3478 ** json_insert(JSON, PATH, VALUE, ...)
3480 ** Create PATH and initialize it to VALUE. If PATH already exists, this
3481 ** routine is a no-op. If JSON or PATH is malformed, throw an error.
3483 static void jsonSetFunc(
3484 sqlite3_context
*ctx
,
3486 sqlite3_value
**argv
3489 int flags
= SQLITE_PTR_TO_INT(sqlite3_user_data(ctx
));
3490 int bIsSet
= (flags
&JSON_ISSET
)!=0;
3492 if( argc
<1 ) return;
3494 jsonWrongNumArgs(ctx
, bIsSet
? "set" : "insert");
3497 jsonInsertIntoBlob(ctx
, argc
, argv
, bIsSet
? JEDIT_SET
: JEDIT_INS
);
3502 ** json_type(JSON, PATH)
3504 ** Return the top-level "type" of a JSON string. json_type() raises an
3505 ** error if either the JSON or PATH inputs are not well-formed.
3507 static void jsonTypeFunc(
3508 sqlite3_context
*ctx
,
3510 sqlite3_value
**argv
3512 JsonParse
*p
; /* The parse */
3513 const char *zPath
= 0;
3516 p
= jsonParseFuncArg(ctx
, argv
[0], 0);
3519 if( sqlite3_value_type(argv
[1])==SQLITE_NULL
){
3520 goto json_type_done
;
3522 if( sqlite3_value_bytes(argv
[1])==0 ){
3523 jsonBadPathError(ctx
, "");
3524 goto json_type_done
;
3526 zPath
= (const char*)sqlite3_value_text(argv
[1]);
3528 sqlite3_result_error_nomem(ctx
);
3529 goto json_type_done
;
3531 if( zPath
[0]!='$' ){
3532 jsonBadPathError(ctx
, zPath
);
3533 goto json_type_done
;
3535 i
= jsonLookupBlobStep(p
, 0, zPath
+1, 0);
3536 if( JSON_BLOB_ISERROR(i
) ){
3537 if( i
==JSON_BLOB_NOTFOUND
){
3539 }else if( i
==JSON_BLOB_PATHERROR
){
3540 jsonBadPathError(ctx
, zPath
);
3542 sqlite3_result_error(ctx
, "malformed JSON", -1);
3544 goto json_type_done
;
3549 sqlite3_result_text(ctx
, jsonbType
[p
->aBlob
[i
]&0x0f], -1, SQLITE_STATIC
);
3556 ** json_valid(JSON, FLAGS)
3558 ** Check the JSON argument to see if it is well-formed. The FLAGS argument
3559 ** encodes the various constraints on what is meant by "well-formed":
3561 ** 0x01 Canonical RFC-8259 JSON text
3562 ** 0x02 JSON text with optional JSON-5 extensions
3563 ** 0x04 Superficially appears to be JSONB
3564 ** 0x08 Strictly well-formed JSONB
3566 ** If the FLAGS argument is omitted, it defaults to 1. Useful values for
3569 ** 1 Strict canonical JSON text
3570 ** 2 JSON text perhaps with JSON-5 extensions
3571 ** 4 Superficially appears to be JSONB
3572 ** 5 Canonical JSON text or superficial JSONB
3573 ** 6 JSON-5 text or superficial JSONB
3575 ** 9 Canonical JSON text or strict JSONB
3576 ** 10 JSON-5 text or strict JSONB
3578 ** Other flag combinations are redundant. For example, every canonical
3579 ** JSON text is also well-formed JSON-5 text, so FLAG values 2 and 3
3580 ** are the same. Similarly, any input that passes a strict JSONB validation
3581 ** will also pass the superficial validation so 12 through 15 are the same
3582 ** as 8 through 11 respectively.
3584 ** This routine runs in linear time to validate text and when doing strict
3585 ** JSONB validation. Superficial JSONB validation is constant time,
3586 ** assuming the BLOB is already in memory. The performance advantage
3587 ** of superficial JSONB validation is why that option is provided.
3588 ** Application developers can choose to do fast superficial validation or
3589 ** slower strict validation, according to their specific needs.
3591 ** Only the lower four bits of the FLAGS argument are currently used.
3592 ** Higher bits are reserved for future expansion. To facilitate
3593 ** compatibility, the current implementation raises an error if any bit
3594 ** in FLAGS is set other than the lower four bits.
3596 ** The original circa 2015 implementation of the JSON routines in
3597 ** SQLite only supported canonical RFC-8259 JSON text and the json_valid()
3598 ** function only accepted one argument. That is why the default value
3599 ** for the FLAGS argument is 1, since FLAGS=1 causes this routine to only
3600 ** recognize canonical RFC-8259 JSON text as valid. The extra FLAGS
3601 ** argument was added when the JSON routines were extended to support
3602 ** JSON5-like extensions and binary JSONB stored in BLOBs.
3606 ** * Raise an error if FLAGS is outside the range of 1 to 15.
3607 ** * Return NULL if the input is NULL
3608 ** * Return 1 if the input is well-formed.
3609 ** * Return 0 if the input is not well-formed.
3611 static void jsonValidFunc(
3612 sqlite3_context
*ctx
,
3614 sqlite3_value
**argv
3616 JsonParse
*p
; /* The parse */
3620 i64 f
= sqlite3_value_int64(argv
[1]);
3622 sqlite3_result_error(ctx
, "FLAGS parameter to json_valid() must be"
3623 " between 1 and 15", -1);
3628 switch( sqlite3_value_type(argv
[0]) ){
3630 #ifdef SQLITE_LEGACY_JSON_VALID
3631 /* Incorrect legacy behavior was to return FALSE for a NULL input */
3632 sqlite3_result_int(ctx
, 0);
3637 if( (flags
& 0x0c)!=0 && jsonFuncArgMightBeBinary(argv
[0]) ){
3638 /* TO-DO: strict checking if flags & 0x08 */
3645 if( (flags
& 0x3)==0 ) break;
3646 memset(&px
, 0, sizeof(px
));
3648 p
= jsonParseFuncArg(ctx
, argv
[0], JSON_KEEPERROR
);
3651 sqlite3_result_error_nomem(ctx
);
3652 }else if( p
->nErr
){
3654 }else if( (flags
& 0x02)!=0 || p
->hasNonstd
==0 ){
3659 sqlite3_result_error_nomem(ctx
);
3664 sqlite3_result_int(ctx
, res
);
3668 ** json_error_position(JSON)
3670 ** If the argument is NULL, return NULL
3672 ** If the argument is TEXT then try to interpret that text as JSON and
3673 ** return the 1-based character position for where the parser first recognized
3674 ** that the input was not valid JSON, or return 0 if the input text looks
3675 ** ok. JSON-5 extensions are accepted.
3677 ** If the argument is a BLOB then try to interpret the blob as a JSONB
3678 ** and return the 1-based byte offset of the first position that is
3679 ** misformatted. Return 0 if the input BLOB seems to be well-formed.
3681 ** Numeric inputs are converted into text, which is usually valid
3682 ** JSON-5, so they should return 0.
3684 static void jsonErrorFunc(
3685 sqlite3_context
*ctx
,
3687 sqlite3_value
**argv
3689 i64 iErrPos
= 0; /* Error position to be returned */
3693 UNUSED_PARAMETER(argc
);
3694 switch( sqlite3_value_type(argv
[0]) ){
3699 if( !jsonFuncArgMightBeBinary(argv
[0]) ) iErrPos
= 1;
3703 memset(&s
, 0, sizeof(s
));
3704 s
.zJson
= (char*)sqlite3_value_text(argv
[0]);
3705 s
.nJson
= sqlite3_value_bytes(argv
[0]);
3711 sqlite3_result_error_nomem(ctx
);
3714 if( jsonConvertTextToBlob(&s
,0) ){
3717 sqlite3_result_error_nomem(ctx
);
3721 /* Convert byte-offset s.iErr into a character offset */
3722 for(k
=0; k
<s
.iErr
&& s
.zJson
[k
]; k
++){
3723 if( (s
.zJson
[k
] & 0xc0)!=0x80 ) iErrPos
++;
3730 sqlite3_result_int64(ctx
, iErrPos
);
3734 /****************************************************************************
3735 ** Aggregate SQL function implementations
3736 ****************************************************************************/
3738 ** json_group_array(VALUE)
3740 ** Return a JSON array composed of all values in the aggregate.
3742 static void jsonArrayStep(
3743 sqlite3_context
*ctx
,
3745 sqlite3_value
**argv
3748 UNUSED_PARAMETER(argc
);
3749 pStr
= (JsonString
*)sqlite3_aggregate_context(ctx
, sizeof(*pStr
));
3751 if( pStr
->zBuf
==0 ){
3752 jsonStringInit(pStr
, ctx
);
3753 jsonAppendChar(pStr
, '[');
3754 }else if( pStr
->nUsed
>1 ){
3755 jsonAppendChar(pStr
, ',');
3758 jsonAppendSqlValue(pStr
, argv
[0]);
3761 static void jsonArrayCompute(sqlite3_context
*ctx
, int isFinal
){
3763 pStr
= (JsonString
*)sqlite3_aggregate_context(ctx
, 0);
3767 jsonAppendChar(pStr
, ']');
3768 flags
= SQLITE_PTR_TO_INT(sqlite3_user_data(ctx
));
3770 jsonReturnString(pStr
);
3772 }else if( flags
& JSON_BLOB
){
3773 jsonReturnStringAsBlob(pStr
);
3775 sqlite3RCStrUnref(pStr
->zBuf
);
3780 }else if( isFinal
){
3781 sqlite3_result_text(ctx
, pStr
->zBuf
, (int)pStr
->nUsed
,
3782 pStr
->bStatic
? SQLITE_TRANSIENT
:
3786 sqlite3_result_text(ctx
, pStr
->zBuf
, (int)pStr
->nUsed
, SQLITE_TRANSIENT
);
3790 sqlite3_result_text(ctx
, "[]", 2, SQLITE_STATIC
);
3792 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
3794 static void jsonArrayValue(sqlite3_context
*ctx
){
3795 jsonArrayCompute(ctx
, 0);
3797 static void jsonArrayFinal(sqlite3_context
*ctx
){
3798 jsonArrayCompute(ctx
, 1);
3801 #ifndef SQLITE_OMIT_WINDOWFUNC
3803 ** This method works for both json_group_array() and json_group_object().
3804 ** It works by removing the first element of the group by searching forward
3805 ** to the first comma (",") that is not within a string and deleting all
3806 ** text through that comma.
3808 static void jsonGroupInverse(
3809 sqlite3_context
*ctx
,
3811 sqlite3_value
**argv
3819 UNUSED_PARAMETER(argc
);
3820 UNUSED_PARAMETER(argv
);
3821 pStr
= (JsonString
*)sqlite3_aggregate_context(ctx
, 0);
3823 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
3824 ** always have been called to initialize it */
3825 if( NEVER(!pStr
) ) return;
3828 for(i
=1; i
<pStr
->nUsed
&& ((c
= z
[i
])!=',' || inStr
|| nNest
); i
++){
3831 }else if( c
=='\\' ){
3834 if( c
=='{' || c
=='[' ) nNest
++;
3835 if( c
=='}' || c
==']' ) nNest
--;
3838 if( i
<pStr
->nUsed
){
3840 memmove(&z
[1], &z
[i
+1], (size_t)pStr
->nUsed
-1);
3847 # define jsonGroupInverse 0
3852 ** json_group_obj(NAME,VALUE)
3854 ** Return a JSON object composed of all names and values in the aggregate.
3856 static void jsonObjectStep(
3857 sqlite3_context
*ctx
,
3859 sqlite3_value
**argv
3864 UNUSED_PARAMETER(argc
);
3865 pStr
= (JsonString
*)sqlite3_aggregate_context(ctx
, sizeof(*pStr
));
3867 if( pStr
->zBuf
==0 ){
3868 jsonStringInit(pStr
, ctx
);
3869 jsonAppendChar(pStr
, '{');
3870 }else if( pStr
->nUsed
>1 ){
3871 jsonAppendChar(pStr
, ',');
3874 z
= (const char*)sqlite3_value_text(argv
[0]);
3875 n
= (u32
)sqlite3_value_bytes(argv
[0]);
3876 jsonAppendString(pStr
, z
, n
);
3877 jsonAppendChar(pStr
, ':');
3878 jsonAppendSqlValue(pStr
, argv
[1]);
3881 static void jsonObjectCompute(sqlite3_context
*ctx
, int isFinal
){
3883 pStr
= (JsonString
*)sqlite3_aggregate_context(ctx
, 0);
3886 jsonAppendChar(pStr
, '}');
3888 flags
= SQLITE_PTR_TO_INT(sqlite3_user_data(ctx
));
3890 jsonReturnString(pStr
);
3892 }else if( flags
& JSON_BLOB
){
3893 jsonReturnStringAsBlob(pStr
);
3895 sqlite3RCStrUnref(pStr
->zBuf
);
3900 }else if( isFinal
){
3901 sqlite3_result_text(ctx
, pStr
->zBuf
, (int)pStr
->nUsed
,
3902 pStr
->bStatic
? SQLITE_TRANSIENT
:
3906 sqlite3_result_text(ctx
, pStr
->zBuf
, (int)pStr
->nUsed
, SQLITE_TRANSIENT
);
3910 sqlite3_result_text(ctx
, "{}", 2, SQLITE_STATIC
);
3912 sqlite3_result_subtype(ctx
, JSON_SUBTYPE
);
3914 static void jsonObjectValue(sqlite3_context
*ctx
){
3915 jsonObjectCompute(ctx
, 0);
3917 static void jsonObjectFinal(sqlite3_context
*ctx
){
3918 jsonObjectCompute(ctx
, 1);
3923 #ifndef SQLITE_OMIT_VIRTUALTABLE
3924 /****************************************************************************
3925 ** The json_each virtual table
3926 ****************************************************************************/
3927 typedef struct JsonParent JsonParent
;
3929 u32 iHead
; /* Start of object or array */
3930 u32 iValue
; /* Start of the value */
3931 u32 iEnd
; /* First byte past the end */
3932 u32 nPath
; /* Length of path */
3933 i64 iKey
; /* Key for JSONB_ARRAY */
3936 typedef struct JsonEachCursor JsonEachCursor
;
3937 struct JsonEachCursor
{
3938 sqlite3_vtab_cursor base
; /* Base class - must be first */
3939 u32 iRowid
; /* The rowid */
3940 u32 i
; /* Index in sParse.aBlob[] of current row */
3941 u32 iEnd
; /* EOF when i equals or exceeds this value */
3942 u32 nRoot
; /* Size of the root path in bytes */
3943 u8 eType
; /* Type of the container for element i */
3944 u8 bRecursive
; /* True for json_tree(). False for json_each() */
3945 u32 nParent
; /* Current nesting depth */
3946 u32 nParentAlloc
; /* Space allocated for aParent[] */
3947 JsonParent
*aParent
; /* Parent elements of i */
3948 sqlite3
*db
; /* Database connection */
3949 JsonString path
; /* Current path */
3950 JsonParse sParse
; /* Parse of the input JSON */
3952 typedef struct JsonEachConnection JsonEachConnection
;
3953 struct JsonEachConnection
{
3954 sqlite3_vtab base
; /* Base class - must be first */
3955 sqlite3
*db
; /* Database connection */
3959 /* Constructor for the json_each virtual table */
3960 static int jsonEachConnect(
3963 int argc
, const char *const*argv
,
3964 sqlite3_vtab
**ppVtab
,
3967 JsonEachConnection
*pNew
;
3970 /* Column numbers */
3972 #define JEACH_VALUE 1
3973 #define JEACH_TYPE 2
3974 #define JEACH_ATOM 3
3976 #define JEACH_PARENT 5
3977 #define JEACH_FULLKEY 6
3978 #define JEACH_PATH 7
3979 /* The xBestIndex method assumes that the JSON and ROOT columns are
3980 ** the last two columns in the table. Should this ever changes, be
3981 ** sure to update the xBestIndex method. */
3982 #define JEACH_JSON 8
3983 #define JEACH_ROOT 9
3985 UNUSED_PARAMETER(pzErr
);
3986 UNUSED_PARAMETER(argv
);
3987 UNUSED_PARAMETER(argc
);
3988 UNUSED_PARAMETER(pAux
);
3989 rc
= sqlite3_declare_vtab(db
,
3990 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
3991 "json HIDDEN,root HIDDEN)");
3992 if( rc
==SQLITE_OK
){
3993 pNew
= (JsonEachConnection
*)(*ppVtab
= sqlite3_malloc( sizeof(*pNew
) ));
3994 if( pNew
==0 ) return SQLITE_NOMEM
;
3995 memset(pNew
, 0, sizeof(*pNew
));
3996 sqlite3_vtab_config(db
, SQLITE_VTAB_INNOCUOUS
);
4002 /* destructor for json_each virtual table */
4003 static int jsonEachDisconnect(sqlite3_vtab
*pVtab
){
4004 sqlite3_free(pVtab
);
4008 /* constructor for a JsonEachCursor object for json_each(). */
4009 static int jsonEachOpenEach(sqlite3_vtab
*p
, sqlite3_vtab_cursor
**ppCursor
){
4010 JsonEachConnection
*pVtab
= (JsonEachConnection
*)p
;
4011 JsonEachCursor
*pCur
;
4013 UNUSED_PARAMETER(p
);
4014 pCur
= sqlite3_malloc( sizeof(*pCur
) );
4015 if( pCur
==0 ) return SQLITE_NOMEM
;
4016 memset(pCur
, 0, sizeof(*pCur
));
4017 pCur
->db
= pVtab
->db
;
4018 jsonStringZero(&pCur
->path
);
4019 *ppCursor
= &pCur
->base
;
4023 /* constructor for a JsonEachCursor object for json_tree(). */
4024 static int jsonEachOpenTree(sqlite3_vtab
*p
, sqlite3_vtab_cursor
**ppCursor
){
4025 int rc
= jsonEachOpenEach(p
, ppCursor
);
4026 if( rc
==SQLITE_OK
){
4027 JsonEachCursor
*pCur
= (JsonEachCursor
*)*ppCursor
;
4028 pCur
->bRecursive
= 1;
4033 /* Reset a JsonEachCursor back to its original state. Free any memory
4035 static void jsonEachCursorReset(JsonEachCursor
*p
){
4036 jsonParseReset(&p
->sParse
);
4037 jsonStringReset(&p
->path
);
4038 sqlite3DbFree(p
->db
, p
->aParent
);
4043 p
->nParentAlloc
= 0;
4048 /* Destructor for a jsonEachCursor object */
4049 static int jsonEachClose(sqlite3_vtab_cursor
*cur
){
4050 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
4051 jsonEachCursorReset(p
);
4057 /* Return TRUE if the jsonEachCursor object has been advanced off the end
4058 ** of the JSON object */
4059 static int jsonEachEof(sqlite3_vtab_cursor
*cur
){
4060 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
4061 return p
->i
>= p
->iEnd
;
4065 ** If the cursor is currently pointing at the label of a object entry,
4066 ** then return the index of the value. For all other cases, return the
4067 ** current pointer position, which is the value.
4069 static int jsonSkipLabel(JsonEachCursor
*p
){
4070 if( p
->eType
==JSONB_OBJECT
){
4072 u32 n
= jsonbPayloadSize(&p
->sParse
, p
->i
, &sz
);
4073 return p
->i
+ n
+ sz
;
4080 ** Append the path name for the current element.
4082 static void jsonAppendPathName(JsonEachCursor
*p
){
4083 assert( p
->nParent
>0 );
4084 assert( p
->eType
==JSONB_ARRAY
|| p
->eType
==JSONB_OBJECT
);
4085 if( p
->eType
==JSONB_ARRAY
){
4086 jsonPrintf(30, &p
->path
, "[%lld]", p
->aParent
[p
->nParent
-1].iKey
);
4088 u32 n
, sz
= 0, k
, i
;
4091 n
= jsonbPayloadSize(&p
->sParse
, p
->i
, &sz
);
4093 z
= (const char*)&p
->sParse
.aBlob
[k
];
4094 if( sz
==0 || !sqlite3Isalpha(z
[0]) ){
4097 for(i
=0; i
<sz
; i
++){
4098 if( !sqlite3Isalnum(z
[i
]) ){
4105 jsonPrintf(sz
+4,&p
->path
,".\"%.*s\"", sz
, z
);
4107 jsonPrintf(sz
+2,&p
->path
,".%.*s", sz
, z
);
4112 /* Advance the cursor to the next element for json_tree() */
4113 static int jsonEachNext(sqlite3_vtab_cursor
*cur
){
4114 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
4116 if( p
->bRecursive
){
4120 u32 i
= jsonSkipLabel(p
);
4121 x
= p
->sParse
.aBlob
[i
] & 0x0f;
4122 n
= jsonbPayloadSize(&p
->sParse
, i
, &sz
);
4123 if( x
==JSONB_OBJECT
|| x
==JSONB_ARRAY
){
4124 JsonParent
*pParent
;
4125 if( p
->nParent
>=p
->nParentAlloc
){
4128 nNew
= p
->nParentAlloc
*2 + 3;
4129 pNew
= sqlite3DbRealloc(p
->db
, p
->aParent
, sizeof(JsonParent
)*nNew
);
4130 if( pNew
==0 ) return SQLITE_NOMEM
;
4131 p
->nParentAlloc
= (u32
)nNew
;
4135 pParent
= &p
->aParent
[p
->nParent
];
4136 pParent
->iHead
= p
->i
;
4137 pParent
->iValue
= i
;
4138 pParent
->iEnd
= i
+ n
+ sz
;
4140 pParent
->nPath
= (u32
)p
->path
.nUsed
;
4141 if( p
->eType
&& p
->nParent
){
4142 jsonAppendPathName(p
);
4143 if( p
->path
.eErr
) rc
= SQLITE_NOMEM
;
4150 while( p
->nParent
>0 && p
->i
>= p
->aParent
[p
->nParent
-1].iEnd
){
4152 p
->path
.nUsed
= p
->aParent
[p
->nParent
].nPath
;
4157 JsonParent
*pParent
= &p
->aParent
[p
->nParent
-1];
4158 u32 i
= pParent
->iValue
;
4159 p
->eType
= p
->sParse
.aBlob
[i
] & 0x0f;
4166 u32 i
= jsonSkipLabel(p
);
4167 n
= jsonbPayloadSize(&p
->sParse
, i
, &sz
);
4170 if( p
->eType
==JSONB_ARRAY
&& p
->nParent
){
4171 p
->aParent
[p
->nParent
-1].iKey
++;
4177 /* Length of the path for rowid==0 in bRecursive mode.
4179 static int jsonEachPathLength(JsonEachCursor
*p
){
4180 u32 n
= p
->path
.nUsed
;
4181 if( p
->iRowid
==0 && p
->bRecursive
&& n
>1 ){
4182 if( p
->path
.zBuf
[n
-1]==']' ){
4186 }while( p
->path
.zBuf
[n
]!='[' );
4189 jsonbPayloadSize(&p
->sParse
, p
->i
, &sz
);
4190 if( p
->path
.zBuf
[n
-1]=='"' ) sz
+= 2;
4192 while( p
->path
.zBuf
[n
]!='.' ){
4201 /* Return the value of a column */
4202 static int jsonEachColumn(
4203 sqlite3_vtab_cursor
*cur
, /* The cursor */
4204 sqlite3_context
*ctx
, /* First argument to sqlite3_result_...() */
4205 int iColumn
/* Which column to return */
4207 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
4210 if( p
->nParent
==0 ){
4212 if( p
->nRoot
==1 ) break;
4213 j
= jsonEachPathLength(p
);
4217 }else if( p
->path
.zBuf
[j
]=='[' ){
4219 sqlite3Atoi64(&p
->path
.zBuf
[j
+1], &x
, n
-1, SQLITE_UTF8
);
4220 sqlite3_result_int64(ctx
, x
);
4221 }else if( p
->path
.zBuf
[j
+1]=='"' ){
4222 sqlite3_result_text(ctx
, &p
->path
.zBuf
[j
+2], n
-3, SQLITE_TRANSIENT
);
4224 sqlite3_result_text(ctx
, &p
->path
.zBuf
[j
+1], n
-1, SQLITE_TRANSIENT
);
4228 if( p
->eType
==JSONB_OBJECT
){
4229 jsonReturnFromBlob(&p
->sParse
, p
->i
, ctx
, 1);
4231 assert( p
->eType
==JSONB_ARRAY
);
4232 sqlite3_result_int64(ctx
, p
->aParent
[p
->nParent
-1].iKey
);
4237 u32 i
= jsonSkipLabel(p
);
4238 jsonReturnFromBlob(&p
->sParse
, i
, ctx
, 1);
4242 u32 i
= jsonSkipLabel(p
);
4243 u8 eType
= eType
= p
->sParse
.aBlob
[i
] & 0x0f;
4244 sqlite3_result_text(ctx
, jsonbType
[eType
], -1, SQLITE_STATIC
);
4248 u32 i
= jsonSkipLabel(p
);
4249 if( (p
->sParse
.aBlob
[i
] & 0x0f)<JSONB_ARRAY
){
4250 jsonReturnFromBlob(&p
->sParse
, i
, ctx
, 1);
4255 sqlite3_result_int64(ctx
, (sqlite3_int64
)p
->i
);
4258 case JEACH_PARENT
: {
4259 if( p
->nParent
>0 && p
->bRecursive
){
4260 sqlite3_result_int64(ctx
, p
->aParent
[p
->nParent
-1].iHead
);
4264 case JEACH_FULLKEY
: {
4265 u64 nBase
= p
->path
.nUsed
;
4266 if( p
->nParent
) jsonAppendPathName(p
);
4267 sqlite3_result_text64(ctx
, p
->path
.zBuf
, p
->path
.nUsed
,
4268 SQLITE_TRANSIENT
, SQLITE_UTF8
);
4269 p
->path
.nUsed
= nBase
;
4273 u32 n
= jsonEachPathLength(p
);
4274 sqlite3_result_text64(ctx
, p
->path
.zBuf
, n
,
4275 SQLITE_TRANSIENT
, SQLITE_UTF8
);
4279 sqlite3_result_text(ctx
, p
->path
.zBuf
, p
->nRoot
, SQLITE_STATIC
);
4283 if( p
->sParse
.zJson
==0 ){
4284 sqlite3_result_blob(ctx
, p
->sParse
.aBlob
, p
->sParse
.nBlob
,
4287 sqlite3_result_text(ctx
, p
->sParse
.zJson
, -1, SQLITE_STATIC
);
4295 /* Return the current rowid value */
4296 static int jsonEachRowid(sqlite3_vtab_cursor
*cur
, sqlite_int64
*pRowid
){
4297 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
4298 *pRowid
= p
->iRowid
;
4302 /* The query strategy is to look for an equality constraint on the json
4303 ** column. Without such a constraint, the table cannot operate. idxNum is
4304 ** 1 if the constraint is found, 3 if the constraint and zRoot are found,
4307 static int jsonEachBestIndex(
4309 sqlite3_index_info
*pIdxInfo
4311 int i
; /* Loop counter or computed array index */
4312 int aIdx
[2]; /* Index of constraints for JSON and ROOT */
4313 int unusableMask
= 0; /* Mask of unusable JSON and ROOT constraints */
4314 int idxMask
= 0; /* Mask of usable == constraints JSON and ROOT */
4315 const struct sqlite3_index_constraint
*pConstraint
;
4317 /* This implementation assumes that JSON and ROOT are the last two
4318 ** columns in the table */
4319 assert( JEACH_ROOT
== JEACH_JSON
+1 );
4320 UNUSED_PARAMETER(tab
);
4321 aIdx
[0] = aIdx
[1] = -1;
4322 pConstraint
= pIdxInfo
->aConstraint
;
4323 for(i
=0; i
<pIdxInfo
->nConstraint
; i
++, pConstraint
++){
4326 if( pConstraint
->iColumn
< JEACH_JSON
) continue;
4327 iCol
= pConstraint
->iColumn
- JEACH_JSON
;
4328 assert( iCol
==0 || iCol
==1 );
4329 testcase( iCol
==0 );
4331 if( pConstraint
->usable
==0 ){
4332 unusableMask
|= iMask
;
4333 }else if( pConstraint
->op
==SQLITE_INDEX_CONSTRAINT_EQ
){
4338 if( pIdxInfo
->nOrderBy
>0
4339 && pIdxInfo
->aOrderBy
[0].iColumn
<0
4340 && pIdxInfo
->aOrderBy
[0].desc
==0
4342 pIdxInfo
->orderByConsumed
= 1;
4345 if( (unusableMask
& ~idxMask
)!=0 ){
4346 /* If there are any unusable constraints on JSON or ROOT, then reject
4347 ** this entire plan */
4348 return SQLITE_CONSTRAINT
;
4351 /* No JSON input. Leave estimatedCost at the huge value that it was
4352 ** initialized to to discourage the query planner from selecting this
4354 pIdxInfo
->idxNum
= 0;
4356 pIdxInfo
->estimatedCost
= 1.0;
4358 pIdxInfo
->aConstraintUsage
[i
].argvIndex
= 1;
4359 pIdxInfo
->aConstraintUsage
[i
].omit
= 1;
4361 pIdxInfo
->idxNum
= 1; /* Only JSON supplied. Plan 1 */
4364 pIdxInfo
->aConstraintUsage
[i
].argvIndex
= 2;
4365 pIdxInfo
->aConstraintUsage
[i
].omit
= 1;
4366 pIdxInfo
->idxNum
= 3; /* Both JSON and ROOT are supplied. Plan 3 */
4372 /* Start a search on a new JSON string */
4373 static int jsonEachFilter(
4374 sqlite3_vtab_cursor
*cur
,
4375 int idxNum
, const char *idxStr
,
4376 int argc
, sqlite3_value
**argv
4378 JsonEachCursor
*p
= (JsonEachCursor
*)cur
;
4379 const char *zRoot
= 0;
4382 UNUSED_PARAMETER(idxStr
);
4383 UNUSED_PARAMETER(argc
);
4384 jsonEachCursorReset(p
);
4385 if( idxNum
==0 ) return SQLITE_OK
;
4386 memset(&p
->sParse
, 0, sizeof(p
->sParse
));
4387 p
->sParse
.nJPRef
= 1;
4388 if( jsonFuncArgMightBeBinary(argv
[0]) ){
4389 p
->sParse
.nBlob
= sqlite3_value_bytes(argv
[0]);
4390 p
->sParse
.aBlob
= (u8
*)sqlite3_value_blob(argv
[0]);
4391 if( p
->sParse
.aBlob
==0 ){
4392 return SQLITE_NOMEM
;
4395 p
->sParse
.zJson
= (char*)sqlite3_value_text(argv
[0]);
4396 p
->sParse
.nJson
= sqlite3_value_bytes(argv
[0]);
4397 if( p
->sParse
.zJson
==0 ){
4401 if( jsonConvertTextToBlob(&p
->sParse
, 0) ){
4402 if( p
->sParse
.oom
){
4403 return SQLITE_NOMEM
;
4405 sqlite3_free(cur
->pVtab
->zErrMsg
);
4406 cur
->pVtab
->zErrMsg
= sqlite3_mprintf("malformed JSON");
4407 jsonEachCursorReset(p
);
4408 return cur
->pVtab
->zErrMsg
? SQLITE_ERROR
: SQLITE_NOMEM
;
4412 zRoot
= (const char*)sqlite3_value_text(argv
[1]);
4413 if( zRoot
==0 ) return SQLITE_OK
;
4414 if( zRoot
[0]!='$' ){
4415 sqlite3_free(cur
->pVtab
->zErrMsg
);
4416 cur
->pVtab
->zErrMsg
= jsonPathSyntaxError(zRoot
, 0);
4417 jsonEachCursorReset(p
);
4418 return cur
->pVtab
->zErrMsg
? SQLITE_ERROR
: SQLITE_NOMEM
;
4420 p
->nRoot
= sqlite3_value_bytes(argv
[1]);
4425 i
= jsonLookupBlobStep(&p
->sParse
, 0, zRoot
+1, 0);
4426 if( JSON_BLOB_ISERROR(i
) ){
4427 if( i
==JSON_BLOB_NOTFOUND
){
4433 sqlite3_free(cur
->pVtab
->zErrMsg
);
4434 cur
->pVtab
->zErrMsg
= jsonPathSyntaxError(zRoot
, 0);
4435 jsonEachCursorReset(p
);
4436 return cur
->pVtab
->zErrMsg
? SQLITE_ERROR
: SQLITE_NOMEM
;
4438 if( p
->sParse
.iLabel
){
4439 p
->i
= p
->sParse
.iLabel
;
4440 p
->eType
= JSONB_OBJECT
;
4443 p
->eType
= JSONB_ARRAY
;
4446 jsonAppendRaw(&p
->path
, zRoot
, p
->nRoot
);
4451 jsonAppendRaw(&p
->path
, "$", 1);
4454 n
= jsonbPayloadSize(&p
->sParse
, i
, &sz
);
4456 if( (p
->sParse
.aBlob
[i
] & 0x0f)>=JSONB_ARRAY
&& !p
->bRecursive
){
4458 p
->eType
= p
->sParse
.aBlob
[i
] & 0x0f;
4459 p
->aParent
= sqlite3DbMallocZero(p
->db
, sizeof(JsonParent
));
4460 if( p
->aParent
==0 ) return SQLITE_NOMEM
;
4462 p
->nParentAlloc
= 1;
4463 p
->aParent
[0].iKey
= 0;
4464 p
->aParent
[0].iEnd
= p
->iEnd
;
4465 p
->aParent
[0].iHead
= p
->i
;
4466 p
->aParent
[0].iValue
= i
;
4471 /* The methods of the json_each virtual table */
4472 static sqlite3_module jsonEachModule
= {
4475 jsonEachConnect
, /* xConnect */
4476 jsonEachBestIndex
, /* xBestIndex */
4477 jsonEachDisconnect
, /* xDisconnect */
4479 jsonEachOpenEach
, /* xOpen - open a cursor */
4480 jsonEachClose
, /* xClose - close a cursor */
4481 jsonEachFilter
, /* xFilter - configure scan constraints */
4482 jsonEachNext
, /* xNext - advance a cursor */
4483 jsonEachEof
, /* xEof - check for end of scan */
4484 jsonEachColumn
, /* xColumn - read data */
4485 jsonEachRowid
, /* xRowid - read data */
4491 0, /* xFindMethod */
4495 0, /* xRollbackTo */
4496 0, /* xShadowName */
4500 /* The methods of the json_tree virtual table. */
4501 static sqlite3_module jsonTreeModule
= {
4504 jsonEachConnect
, /* xConnect */
4505 jsonEachBestIndex
, /* xBestIndex */
4506 jsonEachDisconnect
, /* xDisconnect */
4508 jsonEachOpenTree
, /* xOpen - open a cursor */
4509 jsonEachClose
, /* xClose - close a cursor */
4510 jsonEachFilter
, /* xFilter - configure scan constraints */
4511 jsonEachNext
, /* xNext - advance a cursor */
4512 jsonEachEof
, /* xEof - check for end of scan */
4513 jsonEachColumn
, /* xColumn - read data */
4514 jsonEachRowid
, /* xRowid - read data */
4520 0, /* xFindMethod */
4524 0, /* xRollbackTo */
4525 0, /* xShadowName */
4528 #endif /* SQLITE_OMIT_VIRTUALTABLE */
4529 #endif /* !defined(SQLITE_OMIT_JSON) */
4532 ** Register JSON functions.
4534 void sqlite3RegisterJsonFunctions(void){
4535 #ifndef SQLITE_OMIT_JSON
4536 static FuncDef aJsonFunc
[] = {
4537 /* sqlite3_result_subtype() ----, ,--- sqlite3_value_subtype() */
4539 /* Uses cache ------, | | ,---- Returns JSONB */
4541 /* Number of arguments ---, | | | | ,--- Flags */
4543 JFUNCTION(json
, 1,1,1, 0,0,0, jsonRemoveFunc
),
4544 JFUNCTION(jsonb
, 1,1,0, 0,1,0, jsonbFunc
),
4545 JFUNCTION(json_array
, -1,0,1, 1,0,0, jsonArrayFunc
),
4546 JFUNCTION(jsonb_array
, -1,0,1, 1,1,0, jsonArrayFunc
),
4547 JFUNCTION(json_array_length
, 1,1,0, 0,0,0, jsonArrayLengthFunc
),
4548 JFUNCTION(json_array_length
, 2,1,0, 0,0,0, jsonArrayLengthFunc
),
4549 JFUNCTION(json_error_position
,1,1,0, 0,0,0, jsonErrorFunc
),
4550 JFUNCTION(json_extract
, -1,1,1, 0,0,0, jsonExtractFunc
),
4551 JFUNCTION(jsonb_extract
, -1,1,0, 0,1,0, jsonExtractFunc
),
4552 JFUNCTION(->, 2,1,1, 0,0,JSON_JSON
, jsonExtractFunc
),
4553 JFUNCTION(->>, 2,1,0, 0,0,JSON_SQL
, jsonExtractFunc
),
4554 JFUNCTION(json_insert
, -1,1,1, 1,0,0, jsonSetFunc
),
4555 JFUNCTION(jsonb_insert
, -1,1,0, 1,1,0, jsonSetFunc
),
4556 JFUNCTION(json_object
, -1,0,1, 1,0,0, jsonObjectFunc
),
4557 JFUNCTION(jsonb_object
, -1,0,1, 1,1,0, jsonObjectFunc
),
4558 JFUNCTION(json_patch
, 2,1,1, 0,0,0, jsonPatchFunc
),
4559 JFUNCTION(jsonb_patch
, 2,1,0, 0,1,0, jsonPatchFunc
),
4560 JFUNCTION(json_quote
, 1,0,1, 1,0,0, jsonQuoteFunc
),
4561 JFUNCTION(json_remove
, -1,1,1, 0,0,0, jsonRemoveFunc
),
4562 JFUNCTION(jsonb_remove
, -1,1,0, 0,1,0, jsonRemoveFunc
),
4563 JFUNCTION(json_replace
, -1,1,1, 1,0,0, jsonReplaceFunc
),
4564 JFUNCTION(jsonb_replace
, -1,1,0, 1,1,0, jsonReplaceFunc
),
4565 JFUNCTION(json_set
, -1,1,1, 1,0,JSON_ISSET
, jsonSetFunc
),
4566 JFUNCTION(jsonb_set
, -1,1,0, 1,1,JSON_ISSET
, jsonSetFunc
),
4567 JFUNCTION(json_type
, 1,1,0, 0,0,0, jsonTypeFunc
),
4568 JFUNCTION(json_type
, 2,1,0, 0,0,0, jsonTypeFunc
),
4569 JFUNCTION(json_valid
, 1,1,0, 0,0,0, jsonValidFunc
),
4570 JFUNCTION(json_valid
, 2,1,0, 0,0,0, jsonValidFunc
),
4572 JFUNCTION(json_parse
, 1,1,0, 0,0,0, jsonParseFunc
),
4573 JFUNCTION(json_test1
, 1,1,0, 1,0,0, jsonTest1Func
),
4574 JFUNCTION(jsonb_test2
, 1,1,0, 0,1,0, jsonbTest2
),
4576 WAGGREGATE(json_group_array
, 1, 0, 0,
4577 jsonArrayStep
, jsonArrayFinal
, jsonArrayValue
, jsonGroupInverse
,
4578 SQLITE_SUBTYPE
|SQLITE_RESULT_SUBTYPE
|SQLITE_UTF8
|
4579 SQLITE_DETERMINISTIC
),
4580 WAGGREGATE(jsonb_group_array
, 1, JSON_BLOB
, 0,
4581 jsonArrayStep
, jsonArrayFinal
, jsonArrayValue
, jsonGroupInverse
,
4582 SQLITE_SUBTYPE
|SQLITE_RESULT_SUBTYPE
|SQLITE_UTF8
|SQLITE_DETERMINISTIC
),
4583 WAGGREGATE(json_group_object
, 2, 0, 0,
4584 jsonObjectStep
, jsonObjectFinal
, jsonObjectValue
, jsonGroupInverse
,
4585 SQLITE_SUBTYPE
|SQLITE_RESULT_SUBTYPE
|SQLITE_UTF8
|SQLITE_DETERMINISTIC
),
4586 WAGGREGATE(jsonb_group_object
,2, JSON_BLOB
, 0,
4587 jsonObjectStep
, jsonObjectFinal
, jsonObjectValue
, jsonGroupInverse
,
4588 SQLITE_SUBTYPE
|SQLITE_RESULT_SUBTYPE
|SQLITE_UTF8
|
4589 SQLITE_DETERMINISTIC
)
4591 sqlite3InsertBuiltinFuncs(aJsonFunc
, ArraySize(aJsonFunc
));
4595 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
4597 ** Register the JSON table-valued functions
4599 int sqlite3JsonTableFunctions(sqlite3
*db
){
4601 static const struct {
4603 sqlite3_module
*pModule
;
4605 { "json_each", &jsonEachModule
},
4606 { "json_tree", &jsonTreeModule
},
4609 for(i
=0; i
<sizeof(aMod
)/sizeof(aMod
[0]) && rc
==SQLITE_OK
; i
++){
4610 rc
= sqlite3_create_module(db
, aMod
[i
].zName
, aMod
[i
].pModule
, 0);
4614 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) */