Remove all trace of JsonNode from the JSON implementation. The JSONB format
[sqlite.git] / src / json.c
blob713dc76da9897072173c7647ef99d2ad5421fc1f
1 /*
2 ** 2015-08-12
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 ******************************************************************************
13 ** This SQLite JSON functions.
15 ** This file began as an extension in ext/misc/json1.c in 2015. That
16 ** extension proved so useful that it has now been moved into the core.
18 ** 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
27 ** PostgreSQL JSONB.
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
44 ** or more bytes.
46 ** The lower 4 bits of the first byte of the header determines the
47 ** element type:
49 ** 0: NULL
50 ** 1: TRUE
51 ** 2: FALSE
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
60 ** 11: ARRAY
61 ** 12: OBJECT
63 ** The other three possible values (13-15) are reserved for future
64 ** enhancements.
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
73 ** integer.
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
115 ** processing.
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,
172 ** '"' and '\\'.
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)
197 # define JSON_VVA(X)
198 #else
199 # define JSON_VVA(X) X
200 #endif
202 /* Objects */
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.
210 struct JsonString {
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
255 ** JSON text.
257 struct JsonParse {
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){
316 p->zBuf = p->zSpace;
317 p->nAlloc = sizeof(p->zSpace);
318 p->nUsed = 0;
319 p->bStatic = 1;
322 /* Initialize the JsonString object
324 static void jsonStringInit(JsonString *p, sqlite3_context *pCtx){
325 p->pCtx = pCtx;
326 p->eErr = 0;
327 jsonStringZero(p);
330 /* Free all allocated memory and reset the JsonString object back to its
331 ** initial state.
333 static void jsonStringReset(JsonString *p){
334 if( !p->bStatic ) sqlite3RCStrUnref(p->zBuf);
335 jsonStringZero(p);
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);
343 jsonStringReset(p);
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;
351 char *zNew;
352 if( p->bStatic ){
353 if( p->eErr ) return 1;
354 zNew = sqlite3RCStrNew(nTotal);
355 if( zNew==0 ){
356 jsonStringOom(p);
357 return SQLITE_NOMEM;
359 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
360 p->zBuf = zNew;
361 p->bStatic = 0;
362 }else{
363 p->zBuf = sqlite3RCStrResize(p->zBuf, nTotal);
364 if( p->zBuf==0 ){
365 p->eErr |= JSTRING_OOM;
366 jsonStringZero(p);
367 return SQLITE_NOMEM;
370 p->nAlloc = nTotal;
371 return SQLITE_OK;
374 /* Append N bytes from zIn onto the end of the JsonString string.
376 static SQLITE_NOINLINE void jsonStringExpandAndAppend(
377 JsonString *p,
378 const char *zIn,
379 u32 N
381 assert( N>0 );
382 if( jsonStringGrow(p,N) ) return;
383 memcpy(p->zBuf+p->nUsed, zIn, N);
384 p->nUsed += N;
386 static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
387 if( N==0 ) return;
388 if( N+p->nUsed >= p->nAlloc ){
389 jsonStringExpandAndAppend(p,zIn,N);
390 }else{
391 memcpy(p->zBuf+p->nUsed, zIn, N);
392 p->nUsed += N;
395 static void jsonAppendRawNZ(JsonString *p, const char *zIn, u32 N){
396 if( N==0 ) return;
397 if( N+p->nUsed >= p->nAlloc ){
398 jsonStringExpandAndAppend(p,zIn,N);
399 }else{
400 memcpy(p->zBuf+p->nUsed, zIn, N);
401 p->nUsed += N;
406 /* Append formatted text (not to exceed N bytes) to the JsonString.
408 static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
409 va_list ap;
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);
413 va_end(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);
426 }else{
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
442 ** from happening.
444 static int jsonForceRCStr(JsonString *p){
445 jsonAppendChar(p, 0);
446 if( p->eErr ) return 0;
447 p->nUsed--;
448 if( p->bStatic==0 ) return 1;
449 p->nAlloc = 0;
450 p->nUsed++;
451 jsonStringGrow(p, p->nUsed);
452 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){
461 char c;
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
471 ** string.
473 static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
474 u32 i;
475 if( zIn==0 ) return;
476 if( (N+p->nUsed+2 >= p->nAlloc) && jsonStringGrow(p,N+2)!=0 ) return;
477 p->zBuf[p->nUsed++] = '"';
478 for(i=0; i<N; i++){
479 unsigned char c = ((unsigned const char*)zIn)[i];
480 if( jsonIsOk[c] ){
481 p->zBuf[p->nUsed++] = c;
482 }else if( c=='"' || c=='\\' ){
483 json_simple_escape:
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;
487 }else if( c=='\'' ){
488 p->zBuf[p->nUsed++] = c;
489 }else{
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) );
501 if( aSpecial[c] ){
502 c = aSpecial[c];
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) ){
527 case SQLITE_NULL: {
528 jsonAppendRawNZ(p, "null", 4);
529 break;
531 case SQLITE_FLOAT: {
532 jsonPrintf(100, p, "%!0.15g", sqlite3_value_double(pValue));
533 break;
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);
539 break;
541 case SQLITE_TEXT: {
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);
546 }else{
547 jsonAppendString(p, z, n);
549 break;
551 default: {
552 if( jsonFuncArgMightBeBinary(pValue) ){
553 JsonParse px;
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;
561 jsonStringReset(p);
563 break;
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){
574 if( p->eErr==0 ){
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,
584 sqlite3RCStrUnref,
585 SQLITE_UTF8);
586 }else{
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);
594 jsonStringReset(p);
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);
609 pParse->zJson = 0;
610 pParse->bJsonIsRCStr = 0;
612 if( pParse->nBlobAlloc ){
613 sqlite3_free(pParse->aBlob);
614 pParse->aBlob = 0;
615 pParse->nBlob = 0;
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){
629 if( pParse ){
630 if( pParse->nJPRef>1 ){
631 pParse->nJPRef--;
632 }else{
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;
646 #ifdef SQLITE_EBCDIC
647 h += 9*(1&~(h>>4));
648 #else
649 h += 9*(1&(h>>6));
650 #endif
651 return (u8)(h & 0xf);
655 ** Convert a 4-byte hex string into an integer
657 static u32 jsonHexToInt4(const char *z){
658 u32 v;
659 v = (jsonHexToInt(z[0])<<12)
660 + (jsonHexToInt(z[1])<<8)
661 + (jsonHexToInt(z[2])<<4)
662 + jsonHexToInt(z[3]);
663 return v;
667 ** A macro to hint to the compiler that a function should not be
668 ** inlined.
670 #if defined(__GNUC__)
671 # define JSON_NOINLINE __attribute__((noinline))
672 #elif defined(_MSC_VER) && _MSC_VER>=1310
673 # define JSON_NOINLINE __declspec(noinline)
674 #else
675 # define JSON_NOINLINE
676 #endif
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
705 ** U+0020 20 space
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){
730 int n = 0;
731 const u8 *z = (u8*)zIn;
732 while( 1 /*exit by "goto whitespace_done"*/ ){
733 switch( z[n] ){
734 case 0x09:
735 case 0x0a:
736 case 0x0b:
737 case 0x0c:
738 case 0x0d:
739 case 0x20: {
740 n++;
741 break;
743 case '/': {
744 if( z[n+1]=='*' && z[n+2]!=0 ){
745 int j;
746 for(j=n+3; z[j]!='/' || z[j-1]!='*'; j++){
747 if( z[j]==0 ) goto whitespace_done;
749 n = j+1;
750 break;
751 }else if( z[n+1]=='/' ){
752 int j;
753 char c;
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])
759 j += 2;
760 break;
763 n = j;
764 if( z[n] ) n++;
765 break;
767 goto whitespace_done;
769 case 0xc2: {
770 if( z[n+1]==0xa0 ){
771 n += 2;
772 break;
774 goto whitespace_done;
776 case 0xe1: {
777 if( z[n+1]==0x9a && z[n+2]==0x80 ){
778 n += 3;
779 break;
781 goto whitespace_done;
783 case 0xe2: {
784 if( z[n+1]==0x80 ){
785 u8 c = z[n+2];
786 if( c<0x80 ) goto whitespace_done;
787 if( c<=0x8a || c==0xa8 || c==0xa9 || c==0xaf ){
788 n += 3;
789 break;
791 }else if( z[n+1]==0x81 && z[n+2]==0x9f ){
792 n += 3;
793 break;
795 goto whitespace_done;
797 case 0xe3: {
798 if( z[n+1]==0x80 && z[n+2]==0x80 ){
799 n += 3;
800 break;
802 goto whitespace_done;
804 case 0xef: {
805 if( z[n+1]==0xbb && z[n+2]==0xbf ){
806 n += 3;
807 break;
809 goto whitespace_done;
811 default: {
812 goto whitespace_done;
816 whitespace_done:
817 return n;
821 ** Extra floating-point literals to allow in JSON.
823 static const struct NanInfName {
824 char c1;
825 char c2;
826 char n;
827 char eType;
828 char nRepl;
829 char *zMatch;
830 char *zRepl;
831 } aNanInfName[] = {
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;
855 if( zMsg==0 ){
856 sqlite3_result_error_nomem(ctx);
857 }else{
858 sqlite3_result_error(ctx, zMsg, -1);
859 sqlite3_free(zMsg);
861 return 0;
865 ** Report the wrong number of arguments for json_insert(), json_replace()
866 ** or json_set().
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",
873 zFuncName);
874 sqlite3_result_error(pCtx, zMsg, -1);
875 sqlite3_free(zMsg);
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){
889 u8 *aNew;
890 u32 t;
891 assert( N>pParse->nBlobAlloc );
892 if( pParse->nBlobAlloc==0 ){
893 t = 100;
894 }else{
895 t = pParse->nBlobAlloc*2;
897 if( t<N ) t = N+100;
898 aNew = sqlite3_realloc64( pParse->aBlob, t );
899 if( aNew==0 ){ pParse->oom = 1; return 1; }
900 pParse->aBlob = aNew;
901 pParse->nBlobAlloc = t;
902 return 0;
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){
914 u8 *aOld;
915 u32 nSize;
916 if( pParse->nBlobAlloc>0 ) return 1;
917 aOld = pParse->aBlob;
918 nSize = pParse->nBlob + nExtra;
919 pParse->aBlob = 0;
920 if( jsonBlobExpand(pParse, nSize) ){
921 return 0;
923 assert( pParse->nBlobAlloc >= pParse->nBlob + nExtra );
924 memcpy(pParse->aBlob, aOld, pParse->nBlob);
925 return 1;
929 /* Expand pParse->aBlob and append N bytes.
931 ** Return the number of errors.
933 static SQLITE_NOINLINE int jsonBlobExpandAndAppend(
934 JsonParse *pParse,
935 const u8 *aData,
936 u32 N
938 if( jsonBlobExpand(pParse, pParse->nBlob+N) ) return 1;
939 memcpy(&pParse->aBlob[pParse->nBlob], aData, N);
940 pParse->nBlob += N;
941 return 0;
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;
951 return 0;
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);
961 pParse->nBlob += N;
962 return 0;
965 /* Append an node type byte together with the payload size.
967 static void jsonBlobAppendNodeType(
968 JsonParse *pParse,
969 u8 eType,
970 u32 szPayload
972 u8 a[5];
973 if( szPayload<=11 ){
974 jsonBlobAppendOneByte(pParse, eType | (szPayload<<4));
975 }else if( szPayload<=0xff ){
976 a[0] = eType | 0xc0;
977 a[1] = szPayload & 0xff;
978 jsonBlobAppendNBytes(pParse, a, 2);
979 }else if( szPayload<=0xffff ){
980 a[0] = eType | 0xd0;
981 a[1] = (szPayload >> 8) & 0xff;
982 a[2] = szPayload & 0xff;
983 jsonBlobAppendNBytes(pParse, a, 3);
984 }else{
985 a[0] = eType | 0xe0;
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(
997 JsonParse *pParse,
998 u32 i,
999 u32 szPayload
1001 u8 *a;
1002 u8 szType;
1003 u8 nExtra;
1004 u8 nNeeded;
1005 int delta;
1006 if( pParse->oom ) return 0;
1007 a = &pParse->aBlob[i];
1008 szType = a[0]>>4;
1009 if( szType<=11 ){
1010 nExtra = 0;
1011 }else if( szType==12 ){
1012 nExtra = 1;
1013 }else if( szType==13 ){
1014 nExtra = 2;
1015 }else{
1016 nExtra = 4;
1018 if( szPayload<=11 ){
1019 nNeeded = 0;
1020 }else if( szPayload<=0xff ){
1021 nNeeded = 1;
1022 }else if( szPayload<=0xffff ){
1023 nNeeded = 2;
1024 }else{
1025 nNeeded = 4;
1027 delta = nNeeded - nExtra;
1028 if( delta ){
1029 u32 newSize = pParse->nBlob + delta;
1030 if( delta>0 ){
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));
1036 }else{
1037 memmove(&a[1], &a[1-delta], pParse->nBlob - (i+1-delta));
1039 pParse->nBlob = newSize;
1041 if( nNeeded==0 ){
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;
1050 }else{
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;
1057 return delta;
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;
1071 *pOp = JSONB_TEXTJ;
1072 return 1;
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:
1084 ** 0 End of input
1085 ** -1 Syntax error
1086 ** -2 '}' seen \
1087 ** -3 ']' seen \___ For these returns, pParse->iErr is set to
1088 ** -4 ',' seen / the index in zJson[] of the seen character
1089 ** -5 ':' seen /
1091 static int jsonXlateTextToBlob(JsonParse *pParse, u32 i){
1092 char c;
1093 u32 j;
1094 u32 iThis, iStart;
1095 int x;
1096 u8 t;
1097 const char *z = pParse->zJson;
1098 json_parse_restart:
1099 switch( (u8)z[i] ){
1100 case '{': {
1101 /* Parse object */
1102 iThis = pParse->nBlob;
1103 jsonBlobAppendNodeType(pParse, JSONB_OBJECT, (pParse->nJson-i)*2);
1104 if( ++pParse->iDepth > JSON_MAX_DEPTH ){
1105 pParse->iErr = i;
1106 return -1;
1108 iStart = pParse->nBlob;
1109 for(j=i+1;;j++){
1110 u32 iBlob = pParse->nBlob;
1111 x = jsonXlateTextToBlob(pParse, j);
1112 if( x<=0 ){
1113 int op;
1114 if( x==(-2) ){
1115 j = pParse->iErr;
1116 if( pParse->nBlob!=(u32)iStart ) pParse->hasNonstd = 1;
1117 break;
1119 j += json5Whitespace(&z[j]);
1120 op = JSONB_TEXT;
1121 if( sqlite3JsonId1(z[j])
1122 || (z[j]=='\\' && jsonIs4HexB(&z[j+1], &op))
1124 int k = j+1;
1125 while( (sqlite3JsonId2(z[k]) && json5Whitespace(&z[k])==0)
1126 || (z[k]=='\\' && jsonIs4HexB(&z[k+1], &op))
1128 k++;
1130 assert( iBlob==pParse->nBlob );
1131 jsonBlobAppendNodeType(pParse, op, k-j);
1132 jsonBlobAppendNBytes(pParse, (const u8*)&z[j], k-j);
1133 pParse->hasNonstd = 1;
1134 x = k;
1135 }else{
1136 if( x!=-1 ) pParse->iErr = j;
1137 return -1;
1140 if( pParse->oom ) return -1;
1141 t = pParse->aBlob[iBlob] & 0x0f;
1142 if( t<JSONB_TEXT || t>JSONB_TEXTRAW ){
1143 pParse->iErr = j;
1144 return -1;
1146 j = x;
1147 if( z[j]==':' ){
1148 j++;
1149 }else{
1150 if( fast_isspace(z[j]) ){
1151 do{ j++; }while( fast_isspace(z[j]) );
1152 if( z[j]==':' ){
1153 j++;
1154 goto parse_object_value;
1157 x = jsonXlateTextToBlob(pParse, j);
1158 if( x!=(-5) ){
1159 if( x!=(-1) ) pParse->iErr = j;
1160 return -1;
1162 j = pParse->iErr+1;
1164 parse_object_value:
1165 x = jsonXlateTextToBlob(pParse, j);
1166 if( x<=0 ){
1167 if( x!=(-1) ) pParse->iErr = j;
1168 return -1;
1170 j = x;
1171 if( z[j]==',' ){
1172 continue;
1173 }else if( z[j]=='}' ){
1174 break;
1175 }else{
1176 if( fast_isspace(z[j]) ){
1177 do{ j++; }while( fast_isspace(z[j]) );
1178 if( z[j]==',' ){
1179 continue;
1180 }else if( z[j]=='}' ){
1181 break;
1184 x = jsonXlateTextToBlob(pParse, j);
1185 if( x==(-4) ){
1186 j = pParse->iErr;
1187 continue;
1189 if( x==(-2) ){
1190 j = pParse->iErr;
1191 break;
1194 pParse->iErr = j;
1195 return -1;
1197 jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart);
1198 pParse->iDepth--;
1199 return j+1;
1201 case '[': {
1202 /* Parse array */
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 ){
1208 pParse->iErr = i;
1209 return -1;
1211 for(j=i+1;;j++){
1212 x = jsonXlateTextToBlob(pParse, j);
1213 if( x<=0 ){
1214 if( x==(-3) ){
1215 j = pParse->iErr;
1216 if( pParse->nBlob!=iStart ) pParse->hasNonstd = 1;
1217 break;
1219 if( x!=(-1) ) pParse->iErr = j;
1220 return -1;
1222 j = x;
1223 if( z[j]==',' ){
1224 continue;
1225 }else if( z[j]==']' ){
1226 break;
1227 }else{
1228 if( fast_isspace(z[j]) ){
1229 do{ j++; }while( fast_isspace(z[j]) );
1230 if( z[j]==',' ){
1231 continue;
1232 }else if( z[j]==']' ){
1233 break;
1236 x = jsonXlateTextToBlob(pParse, j);
1237 if( x==(-4) ){
1238 j = pParse->iErr;
1239 continue;
1241 if( x==(-3) ){
1242 j = pParse->iErr;
1243 break;
1246 pParse->iErr = j;
1247 return -1;
1249 jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart);
1250 pParse->iDepth--;
1251 return j+1;
1253 case '\'': {
1254 u8 opcode;
1255 char cDelim;
1256 int nn;
1257 pParse->hasNonstd = 1;
1258 opcode = JSONB_TEXT;
1259 goto parse_string;
1260 case '"':
1261 /* Parse string */
1262 opcode = JSONB_TEXT;
1263 parse_string:
1264 cDelim = z[i];
1265 nn = pParse->nJson;
1266 for(j=i+1; 1; j++){
1267 if( j>=nn ){
1268 pParse->iErr = j;
1269 return -1;
1271 if( jsonIsOk[(unsigned char)z[j]] ) continue;
1272 c = z[j];
1273 if( c==cDelim ){
1274 break;
1275 }else if( c=='\\' ){
1276 c = z[++j];
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;
1291 }else{
1292 pParse->iErr = j;
1293 return -1;
1295 }else if( c<=0x1f ){
1296 /* Control characters are not allowed in strings */
1297 pParse->iErr = j;
1298 return -1;
1301 jsonBlobAppendNodeType(pParse, opcode, j-1-i);
1302 jsonBlobAppendNBytes(pParse, (const u8*)&z[i+1], j-1-i);
1303 return j+1;
1305 case 't': {
1306 if( strncmp(z+i,"true",4)==0 && !sqlite3Isalnum(z[i+4]) ){
1307 jsonBlobAppendOneByte(pParse, JSONB_TRUE);
1308 return i+4;
1310 pParse->iErr = i;
1311 return -1;
1313 case 'f': {
1314 if( strncmp(z+i,"false",5)==0 && !sqlite3Isalnum(z[i+5]) ){
1315 jsonBlobAppendOneByte(pParse, JSONB_FALSE);
1316 return i+5;
1318 pParse->iErr = i;
1319 return -1;
1321 case '+': {
1322 u8 seenE;
1323 pParse->hasNonstd = 1;
1324 t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
1325 goto parse_number;
1326 case '.':
1327 if( sqlite3Isdigit(z[i+1]) ){
1328 pParse->hasNonstd = 1;
1329 t = 0x03; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
1330 seenE = 0;
1331 goto parse_number_2;
1333 pParse->iErr = i;
1334 return -1;
1335 case '-':
1336 case '0':
1337 case '1':
1338 case '2':
1339 case '3':
1340 case '4':
1341 case '5':
1342 case '6':
1343 case '7':
1344 case '8':
1345 case '9':
1346 /* Parse number */
1347 t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
1348 parse_number:
1349 seenE = 0;
1350 assert( '-' < '0' );
1351 assert( '+' < '0' );
1352 assert( '.' < '0' );
1353 c = z[i];
1355 if( c<='0' ){
1356 if( c=='0' ){
1357 if( (z[i+1]=='x' || z[i+1]=='X') && sqlite3Isxdigit(z[i+2]) ){
1358 assert( t==0x00 );
1359 pParse->hasNonstd = 1;
1360 t = 0x01;
1361 for(j=i+3; sqlite3Isxdigit(z[j]); j++){}
1362 goto parse_number_finish;
1363 }else if( sqlite3Isdigit(z[i+1]) ){
1364 pParse->iErr = i+1;
1365 return -1;
1367 }else{
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;
1376 if( z[i]=='-' ){
1377 jsonBlobAppendNodeType(pParse, JSONB_FLOAT, 6);
1378 jsonBlobAppendNBytes(pParse, (const u8*)"-9e999", 6);
1379 }else{
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);
1385 if( z[i+1]=='.' ){
1386 pParse->hasNonstd = 1;
1387 t |= 0x01;
1388 goto parse_number_2;
1390 pParse->iErr = i;
1391 return -1;
1393 if( z[i+1]=='0' ){
1394 if( sqlite3Isdigit(z[i+2]) ){
1395 pParse->iErr = i+1;
1396 return -1;
1397 }else if( (z[i+2]=='x' || z[i+2]=='X') && sqlite3Isxdigit(z[i+3]) ){
1398 pParse->hasNonstd = 1;
1399 t |= 0x01;
1400 for(j=i+4; sqlite3Isxdigit(z[j]); j++){}
1401 goto parse_number_finish;
1407 parse_number_2:
1408 for(j=i+1;; j++){
1409 c = z[j];
1410 if( sqlite3Isdigit(c) ) continue;
1411 if( c=='.' ){
1412 if( (t & 0x02)!=0 ){
1413 pParse->iErr = j;
1414 return -1;
1416 t |= 0x02;
1417 continue;
1419 if( c=='e' || c=='E' ){
1420 if( z[j-1]<'0' ){
1421 if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){
1422 pParse->hasNonstd = 1;
1423 t |= 0x01;
1424 }else{
1425 pParse->iErr = j;
1426 return -1;
1429 if( seenE ){
1430 pParse->iErr = j;
1431 return -1;
1433 t |= 0x02;
1434 seenE = 1;
1435 c = z[j+1];
1436 if( c=='+' || c=='-' ){
1437 j++;
1438 c = z[j+1];
1440 if( c<'0' || c>'9' ){
1441 pParse->iErr = j;
1442 return -1;
1444 continue;
1446 break;
1448 if( z[j-1]<'0' ){
1449 if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){
1450 pParse->hasNonstd = 1;
1451 t |= 0x01;
1452 }else{
1453 pParse->iErr = j;
1454 return -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);
1464 return j;
1466 case '}': {
1467 pParse->iErr = i;
1468 return -2; /* End of {...} */
1470 case ']': {
1471 pParse->iErr = i;
1472 return -3; /* End of [...] */
1474 case ',': {
1475 pParse->iErr = i;
1476 return -4; /* List separator */
1478 case ':': {
1479 pParse->iErr = i;
1480 return -5; /* Object label/value separator */
1482 case 0: {
1483 return 0; /* End of file */
1485 case 0x09:
1486 case 0x0a:
1487 case 0x0d:
1488 case 0x20: {
1490 i++;
1491 }while( fast_isspace(z[i]) );
1492 goto json_parse_restart;
1494 case 0x0b:
1495 case 0x0c:
1496 case '/':
1497 case 0xc2:
1498 case 0xe1:
1499 case 0xe2:
1500 case 0xe3:
1501 case 0xef: {
1502 j = json5Whitespace(&z[i]);
1503 if( j>0 ){
1504 i += j;
1505 pParse->hasNonstd = 1;
1506 goto json_parse_restart;
1508 pParse->iErr = i;
1509 return -1;
1511 case 'n': {
1512 if( strncmp(z+i,"null",4)==0 && !sqlite3Isalnum(z[i+4]) ){
1513 jsonBlobAppendOneByte(pParse, JSONB_NULL);
1514 return i+4;
1516 /* fall-through into the default case that checks for NaN */
1518 default: {
1519 u32 k;
1520 int nn;
1521 c = z[i];
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 ){
1526 continue;
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);
1532 }else{
1533 jsonBlobAppendOneByte(pParse, JSONB_NULL);
1535 pParse->hasNonstd = 1;
1536 return i + nn;
1538 pParse->iErr = i;
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
1551 ** this routine.
1553 static int jsonConvertTextToBlob(
1554 JsonParse *pParse, /* Initialize and fill this JsonParse object */
1555 sqlite3_context *pCtx /* Report errors here */
1557 int i;
1558 const char *zJson = pParse->zJson;
1559 i = jsonXlateTextToBlob(pParse, 0);
1560 if( pParse->oom ) i = -1;
1561 if( i>0 ){
1562 assert( pParse->iDepth==0 );
1563 while( fast_isspace(zJson[i]) ) i++;
1564 if( zJson[i] ){
1565 i += json5Whitespace(&zJson[i]);
1566 if( zJson[i] ){
1567 jsonParseReset(pParse);
1568 return 1;
1570 pParse->hasNonstd = 1;
1573 if( i<=0 ){
1574 if( pCtx!=0 ){
1575 if( pParse->oom ){
1576 sqlite3_result_error_nomem(pCtx);
1577 }else{
1578 sqlite3_result_error(pCtx, "malformed JSON", -1);
1581 jsonParseReset(pParse);
1582 return 1;
1584 return 0;
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
1590 ** SQL function.
1592 static void jsonReturnStringAsBlob(JsonString *pStr){
1593 JsonParse px;
1594 memset(&px, 0, sizeof(px));
1595 jsonStringTerminate(pStr);
1596 px.zJson = pStr->zBuf;
1597 px.nJson = pStr->nUsed;
1598 (void)jsonXlateTextToBlob(&px, 0);
1599 if( px.oom ){
1600 sqlite3_free(px.aBlob);
1601 sqlite3_result_error_nomem(pStr->pCtx);
1602 }else{
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){
1613 u8 x;
1614 u32 sz;
1615 u32 n;
1616 if( NEVER(i>pParse->nBlob) ){
1617 *pSz = 0;
1618 return 0;
1620 x = pParse->aBlob[i]>>4;
1621 if( x<=11 ){
1622 sz = x;
1623 n = 1;
1624 }else if( x==12 ){
1625 if( i+1>=pParse->nBlob ){
1626 *pSz = 0;
1627 return 0;
1629 sz = pParse->aBlob[i+1];
1630 n = 2;
1631 }else if( x==13 ){
1632 if( i+2>=pParse->nBlob ){
1633 *pSz = 0;
1634 return 0;
1636 sz = (pParse->aBlob[i+1]<<8) + pParse->aBlob[i+2];
1637 n = 3;
1638 }else{
1639 if( i+4>=pParse->nBlob ){
1640 *pSz = 0;
1641 return 0;
1643 sz = (pParse->aBlob[i+1]<<24) + (pParse->aBlob[i+2]<<16) +
1644 (pParse->aBlob[i+3]<<8) + pParse->aBlob[i+4];
1645 n = 5;
1647 if( i+sz+n > pParse->nBlob
1648 && i+sz+n > pParse->nBlob-pParse->delta
1650 sz = 0;
1651 n = 0;
1653 *pSz = sz;
1654 return n;
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 */
1676 u32 sz, n, j, iEnd;
1678 n = jsonbPayloadSize(pParse, i, &sz);
1679 if( n==0 ){
1680 pOut->eErr |= JSTRING_MALFORMED;
1681 return pParse->nBlob+1;
1683 switch( pParse->aBlob[i] & 0x0f ){
1684 case JSONB_NULL: {
1685 jsonAppendRawNZ(pOut, "null", 4);
1686 return i+1;
1688 case JSONB_TRUE: {
1689 jsonAppendRawNZ(pOut, "true", 4);
1690 return i+1;
1692 case JSONB_FALSE: {
1693 jsonAppendRawNZ(pOut, "false", 5);
1694 return i+1;
1696 case JSONB_INT:
1697 case JSONB_FLOAT: {
1698 jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n], sz);
1699 break;
1701 case JSONB_INT5: { /* Integer literal in hexadecimal notation */
1702 u32 k = 2;
1703 sqlite3_uint64 u = 0;
1704 const char *zIn = (const char*)&pParse->aBlob[i+n];
1705 int bOverflow = 0;
1706 if( zIn[0]=='-' ){
1707 jsonAppendChar(pOut, '-');
1708 k++;
1709 }else if( zIn[0]=='+' ){
1710 k++;
1712 for(; k<sz; k++){
1713 if( !sqlite3Isxdigit(zIn[k]) ){
1714 pOut->eErr |= JSTRING_MALFORMED;
1715 break;
1716 }else if( (u>>60)!=0 ){
1717 bOverflow = 1;
1718 }else{
1719 u = u*16 + sqlite3HexToInt(zIn[k]);
1722 jsonPrintf(100,pOut,bOverflow?"9.0e999":"%llu", u);
1723 break;
1725 case JSONB_FLOAT5: { /* Float literal missing digits beside "." */
1726 u32 k = 0;
1727 const char *zIn = (const char*)&pParse->aBlob[i+n];
1728 if( zIn[0]=='-' ){
1729 jsonAppendChar(pOut, '-');
1730 k++;
1732 if( zIn[k]=='.' ){
1733 jsonAppendChar(pOut, '0');
1735 for(; k<sz; k++){
1736 jsonAppendChar(pOut, zIn[k]);
1737 if( zIn[k]=='.' && (k+1==sz || !sqlite3Isdigit(zIn[k+1])) ){
1738 jsonAppendChar(pOut, '0');
1741 break;
1743 case JSONB_TEXTJ: {
1744 jsonAppendChar(pOut, '"');
1745 jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n], sz);
1746 jsonAppendChar(pOut, '"');
1747 break;
1749 case JSONB_TEXT:
1750 case JSONB_TEXT5: {
1751 const char *zIn;
1752 u32 k;
1753 u32 sz2 = sz;
1754 zIn = (const char*)&pParse->aBlob[i+n];
1755 jsonAppendChar(pOut, '"');
1756 while( sz2>0 ){
1757 for(k=0; k<sz2 && zIn[k]!='\\' && zIn[k]!='"'; k++){}
1758 if( k>0 ){
1759 jsonAppendRawNZ(pOut, zIn, k);
1760 if( k>=sz2 ){
1761 break;
1763 zIn += k;
1764 sz2 -= k;
1766 if( zIn[0]=='"' ){
1767 jsonAppendRawNZ(pOut, "\\\"", 2);
1768 zIn++;
1769 sz2--;
1770 continue;
1772 if( sz2<2 ){
1773 if( sz2>0 ) pOut->eErr |= JSTRING_MALFORMED;
1774 if( sz2==0 ) break;
1776 assert( zIn[0]=='\\' );
1777 switch( (u8)zIn[1] ){
1778 case '\'':
1779 jsonAppendChar(pOut, '\'');
1780 break;
1781 case 'v':
1782 jsonAppendRawNZ(pOut, "\\u0009", 6);
1783 break;
1784 case 'x':
1785 if( sz2<2 ){
1786 pOut->eErr |= JSTRING_MALFORMED;
1787 sz2 = 0;
1788 break;
1790 jsonAppendRawNZ(pOut, "\\u00", 4);
1791 jsonAppendRawNZ(pOut, &zIn[2], 2);
1792 zIn += 2;
1793 sz2 -= 2;
1794 break;
1795 case '0':
1796 jsonAppendRawNZ(pOut, "\\u0000", 6);
1797 break;
1798 case '\r':
1799 if( sz2>2 && zIn[2]=='\n' ){
1800 zIn++;
1801 sz2--;
1803 break;
1804 case '\n':
1805 break;
1806 case 0xe2:
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 */
1810 if( sz2<4
1811 || 0x80!=(u8)zIn[2]
1812 || (0xa8!=(u8)zIn[3] && 0xa9!=(u8)zIn[3])
1814 pOut->eErr |= JSTRING_MALFORMED;
1815 k = sz2;
1816 break;
1818 zIn += 2;
1819 sz2 -= 2;
1820 break;
1821 default:
1822 jsonAppendRawNZ(pOut, zIn, 2);
1823 break;
1825 if( sz2<2 ){
1826 sz2 = 0;
1827 pOut->eErr |= JSTRING_MALFORMED;
1828 break;
1830 zIn += 2;
1831 sz2 -= 2;
1833 jsonAppendChar(pOut, '"');
1834 break;
1836 case JSONB_TEXTRAW: {
1837 jsonAppendString(pOut, (const char*)&pParse->aBlob[i+n], sz);
1838 break;
1840 case JSONB_ARRAY: {
1841 jsonAppendChar(pOut, '[');
1842 j = i+n;
1843 iEnd = j+sz;
1844 while( j<iEnd ){
1845 j = jsonXlateBlobToText(pParse, j, pOut);
1846 jsonAppendChar(pOut, ',');
1848 if( sz>0 ) pOut->nUsed--;
1849 jsonAppendChar(pOut, ']');
1850 break;
1852 case JSONB_OBJECT: {
1853 int x = 0;
1854 jsonAppendChar(pOut, '{');
1855 j = i+n;
1856 iEnd = j+sz;
1857 while( j<iEnd ){
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, '}');
1864 break;
1867 default: {
1868 pOut->eErr |= JSTRING_MALFORMED;
1869 break;
1872 return i+n+sz;
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){
1882 u32 sz, n;
1883 const u8 *aBlob;
1884 int nBlob;
1885 JsonParse s;
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;
1893 s.nBlob = nBlob;
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){
1906 u32 n, sz, i, iEnd;
1907 u32 k = 0;
1908 n = jsonbPayloadSize(pParse, iRoot, &sz);
1909 iEnd = iRoot+n+sz;
1910 for(i=iRoot+n; n>0 && i<iEnd; i+=sz+n, k++){
1911 n = jsonbPayloadSize(pParse, i, &sz);
1913 return k;
1917 ** Edit the size of the element at iRoot by the amount in pParse->delta.
1919 static void jsonAfterEditSizeAdjust(JsonParse *pParse, u32 iRoot){
1920 u32 sz = 0;
1921 u32 nBlob;
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;
1953 if( d!=0 ){
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));
1961 pParse->nBlob += d;
1962 pParse->delta += d;
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;
1988 const char *zKey;
1989 u8 x;
1990 static const u8 emptyObject[] = { JSONB_ARRAY, JSONB_OBJECT };
1992 if( zPath[0]==0 ){
1993 if( pParse->eEdit && jsonBlobMakeEditable(pParse, pParse->nIns) ){
1994 n = jsonbPayloadSize(pParse, iRoot, &sz);
1995 sz += n;
1996 if( pParse->eEdit==JEDIT_DEL ){
1997 if( iLabel>0 ){
1998 sz += iRoot - iLabel;
1999 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 */
2004 }else{
2005 /* json_set() or json_replace() */
2006 jsonBlobEdit(pParse, iRoot, sz, pParse->aIns, pParse->nIns);
2009 pParse->iLabel = iLabel;
2010 return iRoot;
2012 if( zPath[0]=='.' ){
2013 x = pParse->aBlob[iRoot];
2014 zPath++;
2015 if( zPath[0]=='"' ){
2016 zKey = zPath + 1;
2017 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
2018 nKey = i-1;
2019 if( zPath[i] ){
2020 i++;
2021 }else{
2022 return JSON_BLOB_PATHERROR;
2024 testcase( nKey==0 );
2025 }else{
2026 zKey = zPath;
2027 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
2028 nKey = i;
2029 if( nKey==0 ){
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 */
2036 iEnd = j+sz;
2037 while( j<iEnd ){
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;
2049 assert( j>0 );
2050 rc = jsonLookupBlobStep(pParse, v, &zPath[i], j);
2051 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
2052 return rc;
2054 j = k+sz;
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;
2058 j += n+sz;
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));
2069 ix.aBlob = aLabel;
2070 ix.nBlobAlloc = sizeof(aLabel);
2071 jsonBlobAppendNodeType(&ix,JSONB_TEXTRAW, nKey);
2072 memset(&v, 0, sizeof(v));
2073 if( zPath[i]==0 ){
2074 v.nBlob = pParse->nIns;
2075 v.aBlob = pParse->aIns;
2076 }else{
2077 v.nBlob = 1;
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;
2085 jsonParseReset(&v);
2086 return rc;
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);
2093 k = j + ix.nBlob;
2094 memcpy(&pParse->aBlob[k], zKey, nKey);
2095 k += nKey;
2096 memcpy(&pParse->aBlob[k], v.aBlob, v.nBlob);
2097 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
2099 jsonParseReset(&v);
2100 return j;
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);
2106 k = 0;
2107 i = 1;
2108 while( sqlite3Isdigit(zPath[i]) ){
2109 k = k*10 + zPath[i] - '0';
2110 i++;
2112 if( i<2 || zPath[i]!=']' ){
2113 if( zPath[1]=='#' ){
2114 k = jsonbArrayCount(pParse, iRoot);
2115 i = 2;
2116 if( zPath[2]=='-' && sqlite3Isdigit(zPath[3]) ){
2117 unsigned int nn = 0;
2118 i = 3;
2120 nn = nn*10 + zPath[i] - '0';
2121 i++;
2122 }while( sqlite3Isdigit(zPath[i]) );
2123 if( nn>k ) return JSON_BLOB_NOTFOUND;
2124 k -= nn;
2126 if( zPath[i]!=']' ){
2127 return JSON_BLOB_PATHERROR;
2129 }else{
2130 return JSON_BLOB_PATHERROR;
2133 j = iRoot+n;
2134 iEnd = j+sz;
2135 while( j<iEnd ){
2136 if( k==0 ){
2137 rc = jsonLookupBlobStep(pParse, j, &zPath[i+1], 0);
2138 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
2139 return rc;
2141 k--;
2142 n = jsonbPayloadSize(pParse, j, &sz);
2143 if( n==0 ) return JSON_BLOB_ERROR;
2144 j += n+sz;
2146 if( j>iEnd ) return JSON_BLOB_ERROR;
2147 if( k>0 ) return JSON_BLOB_NOTFOUND;
2148 if( pParse->eEdit>=JEDIT_INS ){
2149 JsonParse v;
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;
2156 }else{
2157 v.nBlob = 1;
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;
2165 jsonParseReset(&v);
2166 return rc;
2169 if( jsonBlobMakeEditable(pParse, v.nBlob) ){
2170 jsonBlobEdit(pParse, j, 0, v.aBlob, v.nBlob);
2172 jsonParseReset(&v);
2173 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
2174 return j;
2176 }else{
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,
2188 const u8 *aBlob,
2189 u32 nBlob
2191 JsonParse x;
2192 JsonString s;
2194 if( aBlob==0 ) return;
2195 memset(&x, 0, sizeof(x));
2196 x.aBlob = (u8*)aBlob;
2197 x.nBlob = nBlob;
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
2210 ** on the userdata.
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 */
2218 u32 n, sz;
2219 int rc;
2220 sqlite3 *db = sqlite3_context_db_handle(pCtx);
2222 n = jsonbPayloadSize(pParse, i, &sz);
2223 if( n==0 ) return;
2224 switch( pParse->aBlob[i] & 0x0f ){
2225 case JSONB_NULL: {
2226 sqlite3_result_null(pCtx);
2227 break;
2229 case JSONB_TRUE: {
2230 sqlite3_result_int(pCtx, 1);
2231 break;
2233 case JSONB_FALSE: {
2234 sqlite3_result_int(pCtx, 0);
2235 break;
2237 case JSONB_INT5:
2238 case JSONB_INT: {
2239 sqlite3_int64 iRes = 0;
2240 char *z;
2241 int bNeg = 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);
2245 if( z==0 ) return;
2246 rc = sqlite3DecOrHexToI64(z, &iRes);
2247 sqlite3DbFree(db, z);
2248 if( rc<=1 ){
2249 sqlite3_result_int64(pCtx, bNeg ? -iRes : iRes);
2250 }else if( rc==3 && bNeg ){
2251 sqlite3_result_int64(pCtx, SMALLEST_INT64);
2252 }else{
2253 if( bNeg ){ n--; sz++; }
2254 goto to_double;
2256 break;
2258 case JSONB_FLOAT5:
2259 case JSONB_FLOAT: {
2260 double r;
2261 char *z;
2262 to_double:
2263 z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz);
2264 if( z==0 ) return;
2265 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
2266 sqlite3DbFree(db, z);
2267 sqlite3_result_double(pCtx, r);
2268 break;
2270 case JSONB_TEXTRAW:
2271 case JSONB_TEXT: {
2272 sqlite3_result_text(pCtx, (char*)&pParse->aBlob[i+n], sz,
2273 SQLITE_TRANSIENT);
2274 break;
2276 case JSONB_TEXT5:
2277 case JSONB_TEXTJ: {
2278 /* Translate JSON formatted string into raw text */
2279 u32 iIn, iOut;
2280 const char *z;
2281 char *zOut;
2282 u32 nOut = sz;
2283 z = (const char*)&pParse->aBlob[i+n];
2284 zOut = sqlite3_malloc( nOut+1 );
2285 if( zOut==0 ){
2286 sqlite3_result_error_nomem(pCtx);
2287 break;
2289 for(iIn=iOut=0; iIn<sz; iIn++){
2290 char c = z[iIn];
2291 if( c=='\\' ){
2292 c = z[++iIn];
2293 if( c=='u' ){
2294 u32 v = jsonHexToInt4(z+iIn+1);
2295 iIn += 4;
2296 if( v==0 ) break;
2297 if( v<=0x7f ){
2298 zOut[iOut++] = (char)v;
2299 }else if( v<=0x7ff ){
2300 zOut[iOut++] = (char)(0xc0 | (v>>6));
2301 zOut[iOut++] = 0x80 | (v&0x3f);
2302 }else{
2303 u32 vlo;
2304 if( (v&0xfc00)==0xd800
2305 && i<n-6
2306 && z[iIn+1]=='\\'
2307 && z[iIn+2]=='u'
2308 && ((vlo = jsonHexToInt4(z+iIn+3))&0xfc00)==0xdc00
2310 /* We have a surrogate pair */
2311 v = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
2312 iIn += 6;
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);
2317 }else{
2318 zOut[iOut++] = 0xe0 | (v>>12);
2319 zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
2320 zOut[iOut++] = 0x80 | (v&0x3f);
2323 continue;
2324 }else if( c=='b' ){
2325 c = '\b';
2326 }else if( c=='f' ){
2327 c = '\f';
2328 }else if( c=='n' ){
2329 c = '\n';
2330 }else if( c=='r' ){
2331 c = '\r';
2332 }else if( c=='t' ){
2333 c = '\t';
2334 }else if( c=='v' ){
2335 c = '\v';
2336 }else if( c=='\'' || c=='"' || c=='/' || c=='\\' ){
2337 /* pass through unchanged */
2338 }else if( c=='0' ){
2339 c = 0;
2340 }else if( c=='x' ){
2341 c = (jsonHexToInt(z[iIn+1])<<4) | jsonHexToInt(z[iIn+2]);
2342 iIn += 2;
2343 }else if( c=='\r' && z[i+1]=='\n' ){
2344 iIn++;
2345 continue;
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] );
2349 iIn += 2;
2350 continue;
2351 }else{
2352 continue;
2354 } /* end if( c=='\\' ) */
2355 zOut[iOut++] = c;
2356 } /* end for() */
2357 zOut[iOut] = 0;
2358 sqlite3_result_text(pCtx, zOut, iOut, sqlite3_free);
2359 break;
2361 case JSONB_ARRAY:
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);
2366 }else{
2367 jsonReturnTextJsonFromBlob(pCtx, &pParse->aBlob[i], sz+n);
2369 break;
2371 default: {
2372 sqlite3_result_error(pCtx, "malformed JSON", -1);
2373 break;
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,
2398 JsonParse *pParse
2400 int eType = sqlite3_value_type(pArg);
2401 static u8 aNull[] = { 0x00 };
2402 memset(pParse, 0, sizeof(pParse[0]));
2403 switch( eType ){
2404 case SQLITE_NULL: {
2405 pParse->aBlob = aNull;
2406 pParse->nBlob = 1;
2407 return 0;
2409 case SQLITE_BLOB: {
2410 if( jsonFuncArgMightBeBinary(pArg) ){
2411 pParse->aBlob = (u8*)sqlite3_value_blob(pArg);
2412 pParse->nBlob = sqlite3_value_bytes(pArg);
2413 }else{
2414 sqlite3_result_error(ctx, "JSON cannot hold BLOB values", -1);
2415 return 1;
2417 break;
2419 case SQLITE_TEXT: {
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]));
2430 return 1;
2432 }else{
2433 jsonBlobAppendNodeType(pParse, JSONB_TEXTRAW, nJson);
2434 jsonBlobAppendNBytes(pParse, (const u8*)zJson, nJson);
2436 break;
2438 case SQLITE_FLOAT:
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);
2446 break;
2449 if( pParse->oom ){
2450 sqlite3_result_error_nomem(ctx);
2451 return 1;
2452 }else{
2453 return 0;
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,
2480 int argc,
2481 sqlite3_value **argv,
2482 int eEdit /* JEDIT_INS, JEDIT_REPL, or JEDIT_SET */
2484 int i;
2485 u32 rc = 0;
2486 const char *zPath = 0;
2487 int flgs;
2488 JsonParse *p;
2489 JsonParse ax;
2491 assert( (argc&1)==1 );
2492 flgs = argc==1 ? 0 : JSON_EDITABLE;
2493 p = jsonParseFuncArg(ctx, argv[0], flgs);
2494 if( p==0 ) return;
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]);
2498 if( zPath==0 ){
2499 sqlite3_result_error_nomem(ctx);
2500 jsonParseFree(p);
2501 return;
2503 if( zPath[0]!='$' ) goto jsonInsertIntoBlob_patherror;
2504 if( jsonFunctionArgToBlob(ctx, argv[i+1], &ax) ){
2505 jsonParseReset(&ax);
2506 jsonParseFree(p);
2507 return;
2509 if( zPath[1]==0 ){
2510 if( eEdit==JEDIT_REPL || eEdit==JEDIT_SET ){
2511 jsonBlobEdit(p, 0, p->nBlob, ax.aBlob, ax.nBlob);
2513 rc = 0;
2514 }else{
2515 p->eEdit = eEdit;
2516 p->nIns = ax.nBlob;
2517 p->aIns = ax.aBlob;
2518 p->delta = 0;
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);
2526 jsonParseFree(p);
2527 return;
2529 jsonInsertIntoBlob_patherror:
2530 jsonParseFree(p);
2531 if( rc==JSON_BLOB_ERROR ){
2532 sqlite3_result_error(ctx, "malformed JSON", -1);
2533 }else{
2534 jsonBadPathError(ctx, zPath);
2536 return;
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
2553 ** a NULL value.
2555 static JsonParse *jsonParseFuncArg(
2556 sqlite3_context *ctx,
2557 sqlite3_value *pArg,
2558 u32 flgs
2560 int eType; /* Datatype of pArg */
2561 JsonParse *p = 0; /* Value to be returned */
2563 assert( ctx!=0 );
2564 eType = sqlite3_value_type(pArg);
2565 if( eType==SQLITE_NULL ){
2566 return 0;
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 ){
2572 u32 n, sz = 0;
2573 p->aBlob = (u8*)sqlite3_value_blob(pArg);
2574 p->nBlob = (u32)sqlite3_value_bytes(pArg);
2575 if( p->nBlob==0 ){
2576 goto json_pfa_malformed;
2578 if( p->aBlob==0 ){
2579 goto json_pfa_oom;
2581 if( (p->aBlob[0] & 0x0f)>JSONB_OBJECT ){
2582 goto json_pfa_malformed;
2584 n = jsonbPayloadSize(p, 0, &sz);
2585 if( n==0
2586 || sz+n!=p->nBlob
2587 || ((p->aBlob[0] & 0x0f)<=JSONB_FALSE && sz>0)
2588 || sz+n!=p->nBlob
2590 goto json_pfa_malformed;
2592 if( (flgs & JSON_EDITABLE)!=0 && jsonBlobMakeEditable(p, 0)==0 ){
2593 goto json_pfa_oom;
2595 return p;
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 ){
2605 p->nErr = 1;
2606 return p;
2607 }else{
2608 jsonParseFree(p);
2609 return 0;
2612 return p;
2614 json_pfa_malformed:
2615 if( flgs & JSON_KEEPERROR ){
2616 p->nErr = 1;
2617 return p;
2618 }else{
2619 jsonParseFree(p);
2620 sqlite3_result_error(ctx, "malformed JSON", -1);
2621 return 0;
2624 json_pfa_oom:
2625 jsonParseFree(p);
2626 sqlite3_result_error_nomem(ctx);
2627 return 0;
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,
2637 JsonParse *p
2639 int flgs;
2640 if( p->oom ){
2641 sqlite3_result_error_nomem(ctx);
2642 return;
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);
2648 p->nBlobAlloc = 0;
2649 }else{
2650 JsonString s;
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 ****************************************************************************/
2662 #if SQLITE_DEBUG
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);
2684 if( nn==0 ) nn = 1;
2685 if( sz>0 && x<JSONB_ARRAY ){
2686 nn += sz;
2688 for(i=0; i<nn; i++) printf(" %02x", pParse->aBlob[iStart+i]);
2689 if( n==0 ){
2690 printf(" ERROR invalid node size\n");
2691 iStart = n==0 ? iStart+1 : iEnd;
2692 continue;
2694 pParse->nBlob = savedNBlob;
2695 if( iStart+n+sz>iEnd ){
2696 iEnd = iStart+n+sz;
2697 if( iEnd>pParse->nBlob ){
2698 if( pParse->nBlobAlloc>0 && iEnd>pParse->nBlobAlloc ){
2699 iEnd = pParse->nBlobAlloc;
2700 }else{
2701 iEnd = pParse->nBlob;
2705 printf(" <-- ");
2706 switch( x ){
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;
2718 case JSONB_ARRAY: {
2719 printf("array, %u bytes\n", sz);
2720 jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2);
2721 showContent = 0;
2722 break;
2724 case JSONB_OBJECT: {
2725 printf("object, %u bytes\n", sz);
2726 jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2);
2727 showContent = 0;
2728 break;
2730 default: {
2731 printf("ERROR: unknown node type\n");
2732 showContent = 0;
2733 break;
2736 if( showContent ){
2737 if( sz==0 && x<=JSONB_FALSE ){
2738 printf("\n");
2739 }else{
2740 u32 i;
2741 printf(": \"");
2742 for(i=iStart+n; i<iStart+n+sz; i++){
2743 u8 c = pParse->aBlob[i];
2744 if( c<0x20 || c>=0x7f ) c = '.';
2745 putchar(c);
2747 printf("\"\n");
2750 iStart += n + sz;
2753 static void jsonShowParse(JsonParse *pParse){
2754 if( pParse==0 ){
2755 printf("NULL pointer\n");
2756 return;
2757 }else{
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 */
2768 #ifdef 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,
2777 int argc,
2778 sqlite3_value **argv
2780 JsonParse *p; /* The parse */
2782 assert( argc==1 );
2783 p = jsonParseFuncArg(ctx, argv[0], 0);
2784 jsonShowParse(p);
2785 jsonParseFree(p);
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,
2795 int argc,
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,
2809 int argc,
2810 sqlite3_value **argv
2812 const u8 *aBlob;
2813 int nBlob;
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
2829 ** JSON).
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
2838 ** JSON blob.
2840 ** If the input is NULL, return NULL.
2842 static void jsonbFunc(
2843 sqlite3_context *ctx,
2844 int argc,
2845 sqlite3_value **argv
2847 JsonParse *pParse;
2848 int nJson;
2849 const char *zJson;
2850 JsonParse x;
2851 UNUSED_PARAMETER(argc);
2853 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
2854 /* No-op */
2855 }else if( jsonFuncArgMightBeBinary(argv[0]) ){
2856 sqlite3_result_value(ctx, argv[0]);
2857 }else{
2858 zJson = (const char*)sqlite3_value_text(argv[0]);
2859 if( zJson==0 ) return;
2860 nJson = sqlite3_value_bytes(argv[0]);
2861 pParse = &x;
2862 memset(&x, 0, sizeof(x));
2863 x.zJson = (char*)zJson;
2864 x.nJson = nJson;
2865 if( jsonConvertTextToBlob(pParse, ctx) ){
2866 sqlite3_result_error(ctx, "malformed JSON", -1);
2867 }else{
2868 sqlite3_result_blob(ctx, pParse->aBlob, pParse->nBlob, sqlite3_free);
2869 pParse->aBlob = 0;
2870 pParse->nBlob = 0;
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,
2885 int argc,
2886 sqlite3_value **argv
2888 JsonString jx;
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,
2904 int argc,
2905 sqlite3_value **argv
2907 int i;
2908 JsonString jx;
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,
2930 int argc,
2931 sqlite3_value **argv
2933 JsonParse *p; /* The parse */
2934 sqlite3_int64 cnt = 0;
2935 u32 i;
2936 u8 eErr = 0;
2938 p = jsonParseFuncArg(ctx, argv[0], 0);
2939 if( p==0 ) return;
2940 if( argc==2 ){
2941 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
2942 if( zPath==0 ){
2943 jsonParseFree(p);
2944 return;
2946 i = jsonLookupBlobStep(p, 0, zPath[0]=='$' ? zPath+1 : "@", 0);
2947 if( JSON_BLOB_ISERROR(i) ){
2948 if( i==JSON_BLOB_NOTFOUND ){
2949 /* no-op */
2950 }else if( i==JSON_BLOB_PATHERROR ){
2951 jsonBadPathError(ctx, zPath);
2952 }else{
2953 sqlite3_result_error(ctx, "malformed JSON", -1);
2955 eErr = 1;
2956 i = 0;
2958 }else{
2959 i = 0;
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;
2965 iEnd = i+n+sz;
2966 i += n;
2967 while( eErr==0 && i<iEnd ){
2968 cnt++;
2969 n = jsonbPayloadSize(p, i, &sz);
2970 if( n==0 ) eErr = 2;
2971 i += n+sz;
2974 if( eErr ){
2975 if( eErr==2 ) sqlite3_result_error(ctx, "malformed JSON", -1);
2976 }else{
2977 sqlite3_result_int64(ctx, cnt);
2979 jsonParseFree(p);
2983 ** json_extract(JSON, PATH, ...)
2984 ** "->"(JSON,PATH)
2985 ** "->>"(JSON,PATH)
2987 ** Return the element described by PATH. Return NULL if that PATH element
2988 ** is not found.
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,
3004 int argc,
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);
3014 if( p==0 ) return;
3015 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
3016 jsonStringInit(&jx, ctx);
3017 if( argc>2 ){
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]);
3024 u32 j;
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
3031 ** convenience.
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);
3046 }else{
3047 jsonAppendRaw(&jx, zPath, nPath);
3049 jsonStringTerminate(&jx);
3050 j = jsonLookupBlobStep(p, 0, jx.zBuf, 0);
3051 jsonStringReset(&jx);
3052 }else{
3053 jsonBadPathError(ctx, zPath);
3054 goto json_extract_error;
3056 if( j<p->nBlob ){
3057 if( argc==2 ){
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);
3065 }else{
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);
3073 }else{
3074 jsonAppendSeparator(&jx);
3075 jsonXlateBlobToText(p, j, &jx);
3077 }else if( j==JSON_BLOB_NOTFOUND ){
3078 if( argc==2 ){
3079 goto json_extract_error; /* Return NULL if not found */
3080 }else{
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;
3087 }else{
3088 jsonBadPathError(ctx, zPath);
3089 goto json_extract_error;
3092 if( argc>2 ){
3093 jsonAppendChar(&jx, ']');
3094 jsonReturnString(&jx);
3095 if( (flags & JSON_BLOB)==0 ){
3096 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3099 json_extract_error:
3100 jsonStringReset(&jx);
3101 jsonParseFree(p);
3102 return;
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
3129 ** else:
3130 ** Target[Name] = MergePatch(Target[Name], Value)
3131 ** return Target
3132 ** else:
3133 ** return Patch
3135 ** Here is an equivalent algorithm restructured to show the actual
3136 ** implementation:
3138 ** 01 define MergePatch(Target, Patch):
3139 ** 02 if Patch is not an Object:
3140 ** 03 return Patch
3141 ** 04 else: // if Patch is an Object
3142 ** 05 if Target is not an Object:
3143 ** 06 Target = {}
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
3148 ** 11 else
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
3153 ** 16 else:
3154 ** 17 Target[name] = MergePatch('{}',value)
3155 ** 18 return Target
3156 ** |
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);
3196 szPatch = n+sz;
3197 sz = 0;
3198 n = jsonbPayloadSize(pTarget, iTarget, &sz);
3199 szTarget = n+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 */
3220 iPLabel = iPCursor;
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;
3234 iTCursor = iTStart;
3235 iTEnd = iTEndBE + pTarget->delta;
3236 while( iTCursor<iTEnd ){
3237 iTLabel = iTCursor;
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 ){
3250 /* Common case */
3251 if( szTLabel==szPLabel
3252 && memcmp(&pTarget->aBlob[iTLabel+nTLabel],
3253 &pPatch->aBlob[iPLabel+nPLabel], szTLabel)==0
3255 break; /* Labels match. */
3257 }else{
3258 /* Should rarely happen */
3259 JsonString s1, s2;
3260 int isEqual;
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 ){
3268 isEqual = 1;
3269 }else{
3270 isEqual = 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 */
3281 if( x==0 ){
3282 /* Patch value is NULL. Algorithm line 09 */
3283 jsonBlobEdit(pTarget, iTLabel, nTLabel+szTLabel+nTValue+szTValue,
3284 0, 0);
3285 if( pTarget->oom ) return JSON_MERGE_OOM;
3286 }else{
3287 /* Algorithm line 12 */
3288 int rc, savedDelta = pTarget->delta;
3289 pTarget->delta = 0;
3290 rc = jsonMergePatchBlob(pTarget, iTValue, pPatch, iPValue);
3291 if( rc ) return rc;
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);
3303 }else{
3304 int rc, savedDelta;
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;
3310 pTarget->delta = 0;
3311 rc = jsonMergePatchBlob(pTarget, iTEnd+szNew,pPatch,iPValue);
3312 if( rc ) return rc;
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,
3329 int argc,
3330 sqlite3_value **argv
3332 JsonParse *pTarget; /* The TARGET */
3333 JsonParse *pPatch; /* The PATCH */
3334 int rc; /* Result code */
3336 UNUSED_PARAMETER(argc);
3337 assert( argc==2 );
3338 pTarget = jsonParseFuncArg(ctx, argv[0], JSON_EDITABLE);
3339 if( pTarget==0 ) return;
3340 pPatch = jsonParseFuncArg(ctx, argv[1], 0);
3341 if( pPatch ){
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);
3347 }else{
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,
3363 int argc,
3364 sqlite3_value **argv
3366 int i;
3367 JsonString jx;
3368 const char *z;
3369 u32 n;
3371 if( argc&1 ){
3372 sqlite3_result_error(ctx, "json_object() requires an even number "
3373 "of arguments", -1);
3374 return;
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);
3382 return;
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,
3405 int argc,
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);
3415 if( p==0 ) return;
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]);
3421 if( zPath==0 ){
3422 goto json_remove_patherror;
3424 if( zPath[0]!='$' ){
3425 goto json_remove_patherror;
3427 if( zPath[1]==0 ){
3428 /* json_remove(j,'$') returns NULL */
3429 goto json_remove_return_null;
3431 p->eEdit = JEDIT_DEL;
3432 p->delta = 0;
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);
3438 jsonParseFree(p);
3439 return;
3441 json_remove_patherror:
3442 jsonParseFree(p);
3443 jsonPathSyntaxError(zPath, ctx);
3444 return;
3446 json_remove_return_null:
3447 jsonParseFree(p);
3448 return;
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,
3459 int argc,
3460 sqlite3_value **argv
3462 if( argc<1 ) return;
3463 if( (argc&1)==0 ) {
3464 jsonWrongNumArgs(ctx, "replace");
3465 return;
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,
3485 int argc,
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;
3493 if( (argc&1)==0 ) {
3494 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
3495 return;
3497 jsonInsertIntoBlob(ctx, argc, argv, bIsSet ? JEDIT_SET : JEDIT_INS);
3501 ** json_type(JSON)
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,
3509 int argc,
3510 sqlite3_value **argv
3512 JsonParse *p; /* The parse */
3513 const char *zPath = 0;
3514 u32 i;
3516 p = jsonParseFuncArg(ctx, argv[0], 0);
3517 if( p==0 ) return;
3518 if( argc==2 ){
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]);
3527 if( zPath==0 ){
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 ){
3538 /* no-op */
3539 }else if( i==JSON_BLOB_PATHERROR ){
3540 jsonBadPathError(ctx, zPath);
3541 }else{
3542 sqlite3_result_error(ctx, "malformed JSON", -1);
3544 goto json_type_done;
3546 }else{
3547 i = 0;
3549 sqlite3_result_text(ctx, jsonbType[p->aBlob[i]&0x0f], -1, SQLITE_STATIC);
3550 json_type_done:
3551 jsonParseFree(p);
3555 ** json_valid(JSON)
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
3567 ** FLAGS include:
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
3574 ** 8 Strict 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.
3604 ** Return Values:
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,
3613 int argc,
3614 sqlite3_value **argv
3616 JsonParse *p; /* The parse */
3617 u8 flags = 1;
3618 u8 res = 0;
3619 if( argc==2 ){
3620 i64 f = sqlite3_value_int64(argv[1]);
3621 if( f<1 || f>15 ){
3622 sqlite3_result_error(ctx, "FLAGS parameter to json_valid() must be"
3623 " between 1 and 15", -1);
3624 return;
3626 flags = f & 0x0f;
3628 switch( sqlite3_value_type(argv[0]) ){
3629 case SQLITE_NULL: {
3630 #ifdef SQLITE_LEGACY_JSON_VALID
3631 /* Incorrect legacy behavior was to return FALSE for a NULL input */
3632 sqlite3_result_int(ctx, 0);
3633 #endif
3634 return;
3636 case SQLITE_BLOB: {
3637 if( (flags & 0x0c)!=0 && jsonFuncArgMightBeBinary(argv[0]) ){
3638 /* TO-DO: strict checking if flags & 0x08 */
3639 res = 1;
3641 break;
3643 default: {
3644 JsonParse px;
3645 if( (flags & 0x3)==0 ) break;
3646 memset(&px, 0, sizeof(px));
3648 p = jsonParseFuncArg(ctx, argv[0], JSON_KEEPERROR);
3649 if( p ){
3650 if( p->oom ){
3651 sqlite3_result_error_nomem(ctx);
3652 }else if( p->nErr ){
3653 /* no-op */
3654 }else if( (flags & 0x02)!=0 || p->hasNonstd==0 ){
3655 res = 1;
3657 jsonParseFree(p);
3658 }else{
3659 sqlite3_result_error_nomem(ctx);
3661 break;
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,
3686 int argc,
3687 sqlite3_value **argv
3689 i64 iErrPos = 0; /* Error position to be returned */
3690 JsonParse s;
3692 assert( argc==1 );
3693 UNUSED_PARAMETER(argc);
3694 switch( sqlite3_value_type(argv[0]) ){
3695 case SQLITE_NULL: {
3696 return;
3698 case SQLITE_BLOB: {
3699 if( !jsonFuncArgMightBeBinary(argv[0]) ) iErrPos = 1;
3700 break;
3702 default: {
3703 memset(&s, 0, sizeof(s));
3704 s.zJson = (char*)sqlite3_value_text(argv[0]);
3705 s.nJson = sqlite3_value_bytes(argv[0]);
3706 if( s.nJson==0 ){
3707 iErrPos = 1;
3708 break;
3710 if( s.zJson==0 ){
3711 sqlite3_result_error_nomem(ctx);
3712 return;
3714 if( jsonConvertTextToBlob(&s,0) ){
3715 u32 k;
3716 if( s.oom ){
3717 sqlite3_result_error_nomem(ctx);
3718 jsonParseReset(&s);
3719 return;
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++;
3725 iErrPos++;
3727 jsonParseReset(&s);
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,
3744 int argc,
3745 sqlite3_value **argv
3747 JsonString *pStr;
3748 UNUSED_PARAMETER(argc);
3749 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
3750 if( pStr ){
3751 if( pStr->zBuf==0 ){
3752 jsonStringInit(pStr, ctx);
3753 jsonAppendChar(pStr, '[');
3754 }else if( pStr->nUsed>1 ){
3755 jsonAppendChar(pStr, ',');
3757 pStr->pCtx = ctx;
3758 jsonAppendSqlValue(pStr, argv[0]);
3761 static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
3762 JsonString *pStr;
3763 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
3764 if( pStr ){
3765 int flags;
3766 pStr->pCtx = ctx;
3767 jsonAppendChar(pStr, ']');
3768 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
3769 if( pStr->eErr ){
3770 jsonReturnString(pStr);
3771 return;
3772 }else if( flags & JSON_BLOB ){
3773 jsonReturnStringAsBlob(pStr);
3774 if( isFinal ){
3775 sqlite3RCStrUnref(pStr->zBuf);
3776 }else{
3777 pStr->nUsed--;
3779 return;
3780 }else if( isFinal ){
3781 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
3782 pStr->bStatic ? SQLITE_TRANSIENT :
3783 sqlite3RCStrUnref);
3784 pStr->bStatic = 1;
3785 }else{
3786 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
3787 pStr->nUsed--;
3789 }else{
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,
3810 int argc,
3811 sqlite3_value **argv
3813 unsigned int i;
3814 int inStr = 0;
3815 int nNest = 0;
3816 char *z;
3817 char c;
3818 JsonString *pStr;
3819 UNUSED_PARAMETER(argc);
3820 UNUSED_PARAMETER(argv);
3821 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
3822 #ifdef NEVER
3823 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
3824 ** always have been called to initialize it */
3825 if( NEVER(!pStr) ) return;
3826 #endif
3827 z = pStr->zBuf;
3828 for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){
3829 if( c=='"' ){
3830 inStr = !inStr;
3831 }else if( c=='\\' ){
3832 i++;
3833 }else if( !inStr ){
3834 if( c=='{' || c=='[' ) nNest++;
3835 if( c=='}' || c==']' ) nNest--;
3838 if( i<pStr->nUsed ){
3839 pStr->nUsed -= i;
3840 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
3841 z[pStr->nUsed] = 0;
3842 }else{
3843 pStr->nUsed = 1;
3846 #else
3847 # define jsonGroupInverse 0
3848 #endif
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,
3858 int argc,
3859 sqlite3_value **argv
3861 JsonString *pStr;
3862 const char *z;
3863 u32 n;
3864 UNUSED_PARAMETER(argc);
3865 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
3866 if( pStr ){
3867 if( pStr->zBuf==0 ){
3868 jsonStringInit(pStr, ctx);
3869 jsonAppendChar(pStr, '{');
3870 }else if( pStr->nUsed>1 ){
3871 jsonAppendChar(pStr, ',');
3873 pStr->pCtx = ctx;
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){
3882 JsonString *pStr;
3883 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
3884 if( pStr ){
3885 int flags;
3886 jsonAppendChar(pStr, '}');
3887 pStr->pCtx = ctx;
3888 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
3889 if( pStr->eErr ){
3890 jsonReturnString(pStr);
3891 return;
3892 }else if( flags & JSON_BLOB ){
3893 jsonReturnStringAsBlob(pStr);
3894 if( isFinal ){
3895 sqlite3RCStrUnref(pStr->zBuf);
3896 }else{
3897 pStr->nUsed--;
3899 return;
3900 }else if( isFinal ){
3901 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
3902 pStr->bStatic ? SQLITE_TRANSIENT :
3903 sqlite3RCStrUnref);
3904 pStr->bStatic = 1;
3905 }else{
3906 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
3907 pStr->nUsed--;
3909 }else{
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;
3928 struct 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(
3961 sqlite3 *db,
3962 void *pAux,
3963 int argc, const char *const*argv,
3964 sqlite3_vtab **ppVtab,
3965 char **pzErr
3967 JsonEachConnection *pNew;
3968 int rc;
3970 /* Column numbers */
3971 #define JEACH_KEY 0
3972 #define JEACH_VALUE 1
3973 #define JEACH_TYPE 2
3974 #define JEACH_ATOM 3
3975 #define JEACH_ID 4
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);
3997 pNew->db = db;
3999 return rc;
4002 /* destructor for json_each virtual table */
4003 static int jsonEachDisconnect(sqlite3_vtab *pVtab){
4004 sqlite3_free(pVtab);
4005 return SQLITE_OK;
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;
4020 return SQLITE_OK;
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;
4030 return rc;
4033 /* Reset a JsonEachCursor back to its original state. Free any memory
4034 ** held. */
4035 static void jsonEachCursorReset(JsonEachCursor *p){
4036 jsonParseReset(&p->sParse);
4037 jsonStringReset(&p->path);
4038 sqlite3DbFree(p->db, p->aParent);
4039 p->iRowid = 0;
4040 p->i = 0;
4041 p->aParent = 0;
4042 p->nParent = 0;
4043 p->nParentAlloc = 0;
4044 p->iEnd = 0;
4045 p->eType = 0;
4048 /* Destructor for a jsonEachCursor object */
4049 static int jsonEachClose(sqlite3_vtab_cursor *cur){
4050 JsonEachCursor *p = (JsonEachCursor*)cur;
4051 jsonEachCursorReset(p);
4053 sqlite3_free(cur);
4054 return SQLITE_OK;
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 ){
4071 u32 sz = 0;
4072 u32 n = jsonbPayloadSize(&p->sParse, p->i, &sz);
4073 return p->i + n + sz;
4074 }else{
4075 return p->i;
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);
4087 }else{
4088 u32 n, sz = 0, k, i;
4089 const char *z;
4090 int needQuote = 0;
4091 n = jsonbPayloadSize(&p->sParse, p->i, &sz);
4092 k = p->i + n;
4093 z = (const char*)&p->sParse.aBlob[k];
4094 if( sz==0 || !sqlite3Isalpha(z[0]) ){
4095 needQuote = 1;
4096 }else{
4097 for(i=0; i<sz; i++){
4098 if( !sqlite3Isalnum(z[i]) ){
4099 needQuote = 1;
4100 break;
4104 if( needQuote ){
4105 jsonPrintf(sz+4,&p->path,".\"%.*s\"", sz, z);
4106 }else{
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;
4115 int rc = SQLITE_OK;
4116 if( p->bRecursive ){
4117 u8 x;
4118 u8 levelChange = 0;
4119 u32 n, sz = 0;
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 ){
4126 JsonParent *pNew;
4127 u64 nNew;
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;
4132 p->aParent = pNew;
4134 levelChange = 1;
4135 pParent = &p->aParent[p->nParent];
4136 pParent->iHead = p->i;
4137 pParent->iValue = i;
4138 pParent->iEnd = i + n + sz;
4139 pParent->iKey = -1;
4140 pParent->nPath = (u32)p->path.nUsed;
4141 if( p->eType && p->nParent ){
4142 jsonAppendPathName(p);
4143 if( p->path.eErr ) rc = SQLITE_NOMEM;
4145 p->nParent++;
4146 p->i = i + n;
4147 }else{
4148 p->i = i + n + sz;
4150 while( p->nParent>0 && p->i >= p->aParent[p->nParent-1].iEnd ){
4151 p->nParent--;
4152 p->path.nUsed = p->aParent[p->nParent].nPath;
4153 levelChange = 1;
4155 if( levelChange ){
4156 if( p->nParent>0 ){
4157 JsonParent *pParent = &p->aParent[p->nParent-1];
4158 u32 i = pParent->iValue;
4159 p->eType = p->sParse.aBlob[i] & 0x0f;
4160 }else{
4161 p->eType = 0;
4164 }else{
4165 u32 n, sz = 0;
4166 u32 i = jsonSkipLabel(p);
4167 n = jsonbPayloadSize(&p->sParse, i, &sz);
4168 p->i = i + n + sz;
4170 if( p->eType==JSONB_ARRAY && p->nParent ){
4171 p->aParent[p->nParent-1].iKey++;
4173 p->iRowid++;
4174 return rc;
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]==']' ){
4184 n--;
4185 assert( n>0 );
4186 }while( p->path.zBuf[n]!='[' );
4187 }else{
4188 u32 sz = 0;
4189 jsonbPayloadSize(&p->sParse, p->i, &sz);
4190 if( p->path.zBuf[n-1]=='"' ) sz += 2;
4191 n -= sz;
4192 while( p->path.zBuf[n]!='.' ){
4193 n--;
4194 assert( n>0 );
4198 return 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;
4208 switch( iColumn ){
4209 case JEACH_KEY: {
4210 if( p->nParent==0 ){
4211 u32 n, j;
4212 if( p->nRoot==1 ) break;
4213 j = jsonEachPathLength(p);
4214 n = p->nRoot - j;
4215 if( n==0 ){
4216 break;
4217 }else if( p->path.zBuf[j]=='[' ){
4218 i64 x;
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);
4223 }else{
4224 sqlite3_result_text(ctx, &p->path.zBuf[j+1], n-1, SQLITE_TRANSIENT);
4226 break;
4228 if( p->eType==JSONB_OBJECT ){
4229 jsonReturnFromBlob(&p->sParse, p->i, ctx, 1);
4230 }else{
4231 assert( p->eType==JSONB_ARRAY );
4232 sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iKey);
4234 break;
4236 case JEACH_VALUE: {
4237 u32 i = jsonSkipLabel(p);
4238 jsonReturnFromBlob(&p->sParse, i, ctx, 1);
4239 break;
4241 case JEACH_TYPE: {
4242 u32 i = jsonSkipLabel(p);
4243 u8 eType = eType = p->sParse.aBlob[i] & 0x0f;
4244 sqlite3_result_text(ctx, jsonbType[eType], -1, SQLITE_STATIC);
4245 break;
4247 case JEACH_ATOM: {
4248 u32 i = jsonSkipLabel(p);
4249 if( (p->sParse.aBlob[i] & 0x0f)<JSONB_ARRAY ){
4250 jsonReturnFromBlob(&p->sParse, i, ctx, 1);
4252 break;
4254 case JEACH_ID: {
4255 sqlite3_result_int64(ctx, (sqlite3_int64)p->i);
4256 break;
4258 case JEACH_PARENT: {
4259 if( p->nParent>0 && p->bRecursive ){
4260 sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iHead);
4262 break;
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;
4270 break;
4272 case JEACH_PATH: {
4273 u32 n = jsonEachPathLength(p);
4274 sqlite3_result_text64(ctx, p->path.zBuf, n,
4275 SQLITE_TRANSIENT, SQLITE_UTF8);
4276 break;
4278 default: {
4279 sqlite3_result_text(ctx, p->path.zBuf, p->nRoot, SQLITE_STATIC);
4280 break;
4282 case JEACH_JSON: {
4283 if( p->sParse.zJson==0 ){
4284 sqlite3_result_blob(ctx, p->sParse.aBlob, p->sParse.nBlob,
4285 SQLITE_STATIC);
4286 }else{
4287 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
4289 break;
4292 return SQLITE_OK;
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;
4299 return SQLITE_OK;
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,
4305 ** and 0 otherwise.
4307 static int jsonEachBestIndex(
4308 sqlite3_vtab *tab,
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++){
4324 int iCol;
4325 int iMask;
4326 if( pConstraint->iColumn < JEACH_JSON ) continue;
4327 iCol = pConstraint->iColumn - JEACH_JSON;
4328 assert( iCol==0 || iCol==1 );
4329 testcase( iCol==0 );
4330 iMask = 1 << iCol;
4331 if( pConstraint->usable==0 ){
4332 unusableMask |= iMask;
4333 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
4334 aIdx[iCol] = i;
4335 idxMask |= iMask;
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;
4350 if( aIdx[0]<0 ){
4351 /* No JSON input. Leave estimatedCost at the huge value that it was
4352 ** initialized to to discourage the query planner from selecting this
4353 ** plan. */
4354 pIdxInfo->idxNum = 0;
4355 }else{
4356 pIdxInfo->estimatedCost = 1.0;
4357 i = aIdx[0];
4358 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
4359 pIdxInfo->aConstraintUsage[i].omit = 1;
4360 if( aIdx[1]<0 ){
4361 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
4362 }else{
4363 i = aIdx[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 */
4369 return SQLITE_OK;
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;
4380 u32 i, n, sz;
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;
4394 }else{
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 ){
4398 p->i = p->iEnd = 0;
4399 return SQLITE_OK;
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;
4411 if( idxNum==3 ){
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]);
4421 if( zRoot[1]==0 ){
4422 i = p->i = 0;
4423 p->eType = 0;
4424 }else{
4425 i = jsonLookupBlobStep(&p->sParse, 0, zRoot+1, 0);
4426 if( JSON_BLOB_ISERROR(i) ){
4427 if( i==JSON_BLOB_NOTFOUND ){
4428 p->i = 0;
4429 p->eType = 0;
4430 p->iEnd = 0;
4431 return SQLITE_OK;
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;
4441 }else{
4442 p->i = i;
4443 p->eType = JSONB_ARRAY;
4446 jsonAppendRaw(&p->path, zRoot, p->nRoot);
4447 }else{
4448 i = p->i = 0;
4449 p->eType = 0;
4450 p->nRoot = 1;
4451 jsonAppendRaw(&p->path, "$", 1);
4453 p->nParent = 0;
4454 n = jsonbPayloadSize(&p->sParse, i, &sz);
4455 p->iEnd = i+n+sz;
4456 if( (p->sParse.aBlob[i] & 0x0f)>=JSONB_ARRAY && !p->bRecursive ){
4457 p->i = i + n;
4458 p->eType = p->sParse.aBlob[i] & 0x0f;
4459 p->aParent = sqlite3DbMallocZero(p->db, sizeof(JsonParent));
4460 if( p->aParent==0 ) return SQLITE_NOMEM;
4461 p->nParent = 1;
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;
4468 return SQLITE_OK;
4471 /* The methods of the json_each virtual table */
4472 static sqlite3_module jsonEachModule = {
4473 0, /* iVersion */
4474 0, /* xCreate */
4475 jsonEachConnect, /* xConnect */
4476 jsonEachBestIndex, /* xBestIndex */
4477 jsonEachDisconnect, /* xDisconnect */
4478 0, /* xDestroy */
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 */
4486 0, /* xUpdate */
4487 0, /* xBegin */
4488 0, /* xSync */
4489 0, /* xCommit */
4490 0, /* xRollback */
4491 0, /* xFindMethod */
4492 0, /* xRename */
4493 0, /* xSavepoint */
4494 0, /* xRelease */
4495 0, /* xRollbackTo */
4496 0, /* xShadowName */
4497 0 /* xIntegrity */
4500 /* The methods of the json_tree virtual table. */
4501 static sqlite3_module jsonTreeModule = {
4502 0, /* iVersion */
4503 0, /* xCreate */
4504 jsonEachConnect, /* xConnect */
4505 jsonEachBestIndex, /* xBestIndex */
4506 jsonEachDisconnect, /* xDisconnect */
4507 0, /* xDestroy */
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 */
4515 0, /* xUpdate */
4516 0, /* xBegin */
4517 0, /* xSync */
4518 0, /* xCommit */
4519 0, /* xRollback */
4520 0, /* xFindMethod */
4521 0, /* xRename */
4522 0, /* xSavepoint */
4523 0, /* xRelease */
4524 0, /* xRollbackTo */
4525 0, /* xShadowName */
4526 0 /* xIntegrity */
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() */
4538 /* | | */
4539 /* Uses cache ------, | | ,---- Returns JSONB */
4540 /* | | | | */
4541 /* Number of arguments ---, | | | | ,--- Flags */
4542 /* | | | | | | */
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),
4571 #if SQLITE_DEBUG
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),
4575 #endif
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));
4592 #endif
4595 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
4597 ** Register the JSON table-valued functions
4599 int sqlite3JsonTableFunctions(sqlite3 *db){
4600 int rc = SQLITE_OK;
4601 static const struct {
4602 const char *zName;
4603 sqlite3_module *pModule;
4604 } aMod[] = {
4605 { "json_each", &jsonEachModule },
4606 { "json_tree", &jsonTreeModule },
4608 unsigned int i;
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);
4612 return rc;
4614 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) */