downgrade memory unlock failures to info level and fix function name in log output
[sqlcipher.git] / src / json.c
blob5bfa869f6bf8a1e8356d6e7dc9cd91f8d696d1ac
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 ** 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 (circa 2024-01-01), these routines also
24 ** accept BLOB values that have JSON encoded using a binary representation
25 ** called "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 ** Additional information can be found in the doc/jsonb.md file of the
118 ** canonical SQLite source tree.
120 #ifndef SQLITE_OMIT_JSON
121 #include "sqliteInt.h"
123 /* JSONB element types
125 #define JSONB_NULL 0 /* "null" */
126 #define JSONB_TRUE 1 /* "true" */
127 #define JSONB_FALSE 2 /* "false" */
128 #define JSONB_INT 3 /* integer acceptable to JSON and SQL */
129 #define JSONB_INT5 4 /* integer in 0x000 notation */
130 #define JSONB_FLOAT 5 /* float acceptable to JSON and SQL */
131 #define JSONB_FLOAT5 6 /* float with JSON5 extensions */
132 #define JSONB_TEXT 7 /* Text compatible with both JSON and SQL */
133 #define JSONB_TEXTJ 8 /* Text with JSON escapes */
134 #define JSONB_TEXT5 9 /* Text with JSON-5 escape */
135 #define JSONB_TEXTRAW 10 /* SQL text that needs escaping for JSON */
136 #define JSONB_ARRAY 11 /* An array */
137 #define JSONB_OBJECT 12 /* An object */
139 /* Human-readable names for the JSONB values. The index for each
140 ** string must correspond to the JSONB_* integer above.
142 static const char * const jsonbType[] = {
143 "null", "true", "false", "integer", "integer",
144 "real", "real", "text", "text", "text",
145 "text", "array", "object", "", "", "", ""
149 ** Growing our own isspace() routine this way is twice as fast as
150 ** the library isspace() function, resulting in a 7% overall performance
151 ** increase for the text-JSON parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
153 static const char jsonIsSpace[] = {
154 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
155 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
156 1, 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,
158 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,
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,
167 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
168 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
169 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
170 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172 #define jsonIsspace(x) (jsonIsSpace[(unsigned char)x])
175 ** The set of all space characters recognized by jsonIsspace().
176 ** Useful as the second argument to strspn().
178 static const char jsonSpaces[] = "\011\012\015\040";
181 ** Characters that are special to JSON. Control characters,
182 ** '"' and '\\' and '\''. Actually, '\'' is not special to
183 ** canonical JSON, but it is special in JSON-5, so we include
184 ** it in the set of special characters.
186 static const char jsonIsOk[256] = {
187 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
188 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
189 1, 1, 0, 1, 1, 1, 1, 0, 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,
192 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
193 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
194 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
196 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
197 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
198 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
199 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
200 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
201 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
202 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
203 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
206 /* Objects */
207 typedef struct JsonCache JsonCache;
208 typedef struct JsonString JsonString;
209 typedef struct JsonParse JsonParse;
212 ** Magic number used for the JSON parse cache in sqlite3_get_auxdata()
214 #define JSON_CACHE_ID (-429938) /* Cache entry */
215 #define JSON_CACHE_SIZE 4 /* Max number of cache entries */
218 ** jsonUnescapeOneChar() returns this invalid code point if it encounters
219 ** a syntax error.
221 #define JSON_INVALID_CHAR 0x99999
223 /* A cache mapping JSON text into JSONB blobs.
225 ** Each cache entry is a JsonParse object with the following restrictions:
227 ** * The bReadOnly flag must be set
229 ** * The aBlob[] array must be owned by the JsonParse object. In other
230 ** words, nBlobAlloc must be non-zero.
232 ** * eEdit and delta must be zero.
234 ** * zJson must be an RCStr. In other words bJsonIsRCStr must be true.
236 struct JsonCache {
237 sqlite3 *db; /* Database connection */
238 int nUsed; /* Number of active entries in the cache */
239 JsonParse *a[JSON_CACHE_SIZE]; /* One line for each cache entry */
242 /* An instance of this object represents a JSON string
243 ** under construction. Really, this is a generic string accumulator
244 ** that can be and is used to create strings other than JSON.
246 ** If the generated string is longer than will fit into the zSpace[] buffer,
247 ** then it will be an RCStr string. This aids with caching of large
248 ** JSON strings.
250 struct JsonString {
251 sqlite3_context *pCtx; /* Function context - put error messages here */
252 char *zBuf; /* Append JSON content here */
253 u64 nAlloc; /* Bytes of storage available in zBuf[] */
254 u64 nUsed; /* Bytes of zBuf[] currently used */
255 u8 bStatic; /* True if zBuf is static space */
256 u8 eErr; /* True if an error has been encountered */
257 char zSpace[100]; /* Initial static space */
260 /* Allowed values for JsonString.eErr */
261 #define JSTRING_OOM 0x01 /* Out of memory */
262 #define JSTRING_MALFORMED 0x02 /* Malformed JSONB */
263 #define JSTRING_ERR 0x04 /* Error already sent to sqlite3_result */
265 /* The "subtype" set for text JSON values passed through using
266 ** sqlite3_result_subtype() and sqlite3_value_subtype().
268 #define JSON_SUBTYPE 74 /* Ascii for "J" */
271 ** Bit values for the flags passed into various SQL function implementations
272 ** via the sqlite3_user_data() value.
274 #define JSON_JSON 0x01 /* Result is always JSON */
275 #define JSON_SQL 0x02 /* Result is always SQL */
276 #define JSON_ABPATH 0x03 /* Allow abbreviated JSON path specs */
277 #define JSON_ISSET 0x04 /* json_set(), not json_insert() */
278 #define JSON_BLOB 0x08 /* Use the BLOB output format */
281 /* A parsed JSON value. Lifecycle:
283 ** 1. JSON comes in and is parsed into a JSONB value in aBlob. The
284 ** original text is stored in zJson. This step is skipped if the
285 ** input is JSONB instead of text JSON.
287 ** 2. The aBlob[] array is searched using the JSON path notation, if needed.
289 ** 3. Zero or more changes are made to aBlob[] (via json_remove() or
290 ** json_replace() or json_patch() or similar).
292 ** 4. New JSON text is generated from the aBlob[] for output. This step
293 ** is skipped if the function is one of the jsonb_* functions that
294 ** returns JSONB instead of text JSON.
296 struct JsonParse {
297 u8 *aBlob; /* JSONB representation of JSON value */
298 u32 nBlob; /* Bytes of aBlob[] actually used */
299 u32 nBlobAlloc; /* Bytes allocated to aBlob[]. 0 if aBlob is external */
300 char *zJson; /* Json text used for parsing */
301 sqlite3 *db; /* The database connection to which this object belongs */
302 int nJson; /* Length of the zJson string in bytes */
303 u32 nJPRef; /* Number of references to this object */
304 u32 iErr; /* Error location in zJson[] */
305 u16 iDepth; /* Nesting depth */
306 u8 nErr; /* Number of errors seen */
307 u8 oom; /* Set to true if out of memory */
308 u8 bJsonIsRCStr; /* True if zJson is an RCStr */
309 u8 hasNonstd; /* True if input uses non-standard features like JSON5 */
310 u8 bReadOnly; /* Do not modify. */
311 /* Search and edit information. See jsonLookupStep() */
312 u8 eEdit; /* Edit operation to apply */
313 int delta; /* Size change due to the edit */
314 u32 nIns; /* Number of bytes to insert */
315 u32 iLabel; /* Location of label if search landed on an object value */
316 u8 *aIns; /* Content to be inserted */
319 /* Allowed values for JsonParse.eEdit */
320 #define JEDIT_DEL 1 /* Delete if exists */
321 #define JEDIT_REPL 2 /* Overwrite if exists */
322 #define JEDIT_INS 3 /* Insert if not exists */
323 #define JEDIT_SET 4 /* Insert or overwrite */
326 ** Maximum nesting depth of JSON for this implementation.
328 ** This limit is needed to avoid a stack overflow in the recursive
329 ** descent parser. A depth of 1000 is far deeper than any sane JSON
330 ** should go. Historical note: This limit was 2000 prior to version 3.42.0
332 #ifndef SQLITE_JSON_MAX_DEPTH
333 # define JSON_MAX_DEPTH 1000
334 #else
335 # define JSON_MAX_DEPTH SQLITE_JSON_MAX_DEPTH
336 #endif
339 ** Allowed values for the flgs argument to jsonParseFuncArg();
341 #define JSON_EDITABLE 0x01 /* Generate a writable JsonParse object */
342 #define JSON_KEEPERROR 0x02 /* Return non-NULL even if there is an error */
344 /**************************************************************************
345 ** Forward references
346 **************************************************************************/
347 static void jsonReturnStringAsBlob(JsonString*);
348 static int jsonFuncArgMightBeBinary(sqlite3_value *pJson);
349 static u32 jsonTranslateBlobToText(const JsonParse*,u32,JsonString*);
350 static void jsonReturnParse(sqlite3_context*,JsonParse*);
351 static JsonParse *jsonParseFuncArg(sqlite3_context*,sqlite3_value*,u32);
352 static void jsonParseFree(JsonParse*);
353 static u32 jsonbPayloadSize(const JsonParse*, u32, u32*);
354 static u32 jsonUnescapeOneChar(const char*, u32, u32*);
356 /**************************************************************************
357 ** Utility routines for dealing with JsonCache objects
358 **************************************************************************/
361 ** Free a JsonCache object.
363 static void jsonCacheDelete(JsonCache *p){
364 int i;
365 for(i=0; i<p->nUsed; i++){
366 jsonParseFree(p->a[i]);
368 sqlite3DbFree(p->db, p);
370 static void jsonCacheDeleteGeneric(void *p){
371 jsonCacheDelete((JsonCache*)p);
375 ** Insert a new entry into the cache. If the cache is full, expel
376 ** the least recently used entry. Return SQLITE_OK on success or a
377 ** result code otherwise.
379 ** Cache entries are stored in age order, oldest first.
381 static int jsonCacheInsert(
382 sqlite3_context *ctx, /* The SQL statement context holding the cache */
383 JsonParse *pParse /* The parse object to be added to the cache */
385 JsonCache *p;
387 assert( pParse->zJson!=0 );
388 assert( pParse->bJsonIsRCStr );
389 assert( pParse->delta==0 );
390 p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID);
391 if( p==0 ){
392 sqlite3 *db = sqlite3_context_db_handle(ctx);
393 p = sqlite3DbMallocZero(db, sizeof(*p));
394 if( p==0 ) return SQLITE_NOMEM;
395 p->db = db;
396 sqlite3_set_auxdata(ctx, JSON_CACHE_ID, p, jsonCacheDeleteGeneric);
397 p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID);
398 if( p==0 ) return SQLITE_NOMEM;
400 if( p->nUsed >= JSON_CACHE_SIZE ){
401 jsonParseFree(p->a[0]);
402 memmove(p->a, &p->a[1], (JSON_CACHE_SIZE-1)*sizeof(p->a[0]));
403 p->nUsed = JSON_CACHE_SIZE-1;
405 assert( pParse->nBlobAlloc>0 );
406 pParse->eEdit = 0;
407 pParse->nJPRef++;
408 pParse->bReadOnly = 1;
409 p->a[p->nUsed] = pParse;
410 p->nUsed++;
411 return SQLITE_OK;
415 ** Search for a cached translation the json text supplied by pArg. Return
416 ** the JsonParse object if found. Return NULL if not found.
418 ** When a match if found, the matching entry is moved to become the
419 ** most-recently used entry if it isn't so already.
421 ** The JsonParse object returned still belongs to the Cache and might
422 ** be deleted at any moment. If the caller whants the JsonParse to
423 ** linger, it needs to increment the nPJRef reference counter.
425 static JsonParse *jsonCacheSearch(
426 sqlite3_context *ctx, /* The SQL statement context holding the cache */
427 sqlite3_value *pArg /* Function argument containing SQL text */
429 JsonCache *p;
430 int i;
431 const char *zJson;
432 int nJson;
434 if( sqlite3_value_type(pArg)!=SQLITE_TEXT ){
435 return 0;
437 zJson = (const char*)sqlite3_value_text(pArg);
438 if( zJson==0 ) return 0;
439 nJson = sqlite3_value_bytes(pArg);
441 p = sqlite3_get_auxdata(ctx, JSON_CACHE_ID);
442 if( p==0 ){
443 return 0;
445 for(i=0; i<p->nUsed; i++){
446 if( p->a[i]->zJson==zJson ) break;
448 if( i>=p->nUsed ){
449 for(i=0; i<p->nUsed; i++){
450 if( p->a[i]->nJson!=nJson ) continue;
451 if( memcmp(p->a[i]->zJson, zJson, nJson)==0 ) break;
454 if( i<p->nUsed ){
455 if( i<p->nUsed-1 ){
456 /* Make the matching entry the most recently used entry */
457 JsonParse *tmp = p->a[i];
458 memmove(&p->a[i], &p->a[i+1], (p->nUsed-i-1)*sizeof(tmp));
459 p->a[p->nUsed-1] = tmp;
460 i = p->nUsed - 1;
462 assert( p->a[i]->delta==0 );
463 return p->a[i];
464 }else{
465 return 0;
469 /**************************************************************************
470 ** Utility routines for dealing with JsonString objects
471 **************************************************************************/
473 /* Turn uninitialized bulk memory into a valid JsonString object
474 ** holding a zero-length string.
476 static void jsonStringZero(JsonString *p){
477 p->zBuf = p->zSpace;
478 p->nAlloc = sizeof(p->zSpace);
479 p->nUsed = 0;
480 p->bStatic = 1;
483 /* Initialize the JsonString object
485 static void jsonStringInit(JsonString *p, sqlite3_context *pCtx){
486 p->pCtx = pCtx;
487 p->eErr = 0;
488 jsonStringZero(p);
491 /* Free all allocated memory and reset the JsonString object back to its
492 ** initial state.
494 static void jsonStringReset(JsonString *p){
495 if( !p->bStatic ) sqlite3RCStrUnref(p->zBuf);
496 jsonStringZero(p);
499 /* Report an out-of-memory (OOM) condition
501 static void jsonStringOom(JsonString *p){
502 p->eErr |= JSTRING_OOM;
503 if( p->pCtx ) sqlite3_result_error_nomem(p->pCtx);
504 jsonStringReset(p);
507 /* Enlarge pJson->zBuf so that it can hold at least N more bytes.
508 ** Return zero on success. Return non-zero on an OOM error
510 static int jsonStringGrow(JsonString *p, u32 N){
511 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
512 char *zNew;
513 if( p->bStatic ){
514 if( p->eErr ) return 1;
515 zNew = sqlite3RCStrNew(nTotal);
516 if( zNew==0 ){
517 jsonStringOom(p);
518 return SQLITE_NOMEM;
520 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
521 p->zBuf = zNew;
522 p->bStatic = 0;
523 }else{
524 p->zBuf = sqlite3RCStrResize(p->zBuf, nTotal);
525 if( p->zBuf==0 ){
526 p->eErr |= JSTRING_OOM;
527 jsonStringZero(p);
528 return SQLITE_NOMEM;
531 p->nAlloc = nTotal;
532 return SQLITE_OK;
535 /* Append N bytes from zIn onto the end of the JsonString string.
537 static SQLITE_NOINLINE void jsonStringExpandAndAppend(
538 JsonString *p,
539 const char *zIn,
540 u32 N
542 assert( N>0 );
543 if( jsonStringGrow(p,N) ) return;
544 memcpy(p->zBuf+p->nUsed, zIn, N);
545 p->nUsed += N;
547 static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
548 if( N==0 ) return;
549 if( N+p->nUsed >= p->nAlloc ){
550 jsonStringExpandAndAppend(p,zIn,N);
551 }else{
552 memcpy(p->zBuf+p->nUsed, zIn, N);
553 p->nUsed += N;
556 static void jsonAppendRawNZ(JsonString *p, const char *zIn, u32 N){
557 assert( N>0 );
558 if( N+p->nUsed >= p->nAlloc ){
559 jsonStringExpandAndAppend(p,zIn,N);
560 }else{
561 memcpy(p->zBuf+p->nUsed, zIn, N);
562 p->nUsed += N;
567 /* Append formatted text (not to exceed N bytes) to the JsonString.
569 static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
570 va_list ap;
571 if( (p->nUsed + N >= p->nAlloc) && jsonStringGrow(p, N) ) return;
572 va_start(ap, zFormat);
573 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
574 va_end(ap);
575 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
578 /* Append a single character
580 static SQLITE_NOINLINE void jsonAppendCharExpand(JsonString *p, char c){
581 if( jsonStringGrow(p,1) ) return;
582 p->zBuf[p->nUsed++] = c;
584 static void jsonAppendChar(JsonString *p, char c){
585 if( p->nUsed>=p->nAlloc ){
586 jsonAppendCharExpand(p,c);
587 }else{
588 p->zBuf[p->nUsed++] = c;
592 /* Remove a single character from the end of the string
594 static void jsonStringTrimOneChar(JsonString *p){
595 if( p->eErr==0 ){
596 assert( p->nUsed>0 );
597 p->nUsed--;
602 /* Make sure there is a zero terminator on p->zBuf[]
604 ** Return true on success. Return false if an OOM prevents this
605 ** from happening.
607 static int jsonStringTerminate(JsonString *p){
608 jsonAppendChar(p, 0);
609 jsonStringTrimOneChar(p);
610 return p->eErr==0;
613 /* Append a comma separator to the output buffer, if the previous
614 ** character is not '[' or '{'.
616 static void jsonAppendSeparator(JsonString *p){
617 char c;
618 if( p->nUsed==0 ) return;
619 c = p->zBuf[p->nUsed-1];
620 if( c=='[' || c=='{' ) return;
621 jsonAppendChar(p, ',');
624 /* Append the N-byte string in zIn to the end of the JsonString string
625 ** under construction. Enclose the string in double-quotes ("...") and
626 ** escape any double-quotes or backslash characters contained within the
627 ** string.
629 ** This routine is a high-runner. There is a measurable performance
630 ** increase associated with unwinding the jsonIsOk[] loop.
632 static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
633 u32 k;
634 u8 c;
635 const u8 *z = (const u8*)zIn;
636 if( z==0 ) return;
637 if( (N+p->nUsed+2 >= p->nAlloc) && jsonStringGrow(p,N+2)!=0 ) return;
638 p->zBuf[p->nUsed++] = '"';
639 while( 1 /*exit-by-break*/ ){
640 k = 0;
641 /* The following while() is the 4-way unwound equivalent of
643 ** while( k<N && jsonIsOk[z[k]] ){ k++; }
645 while( 1 /* Exit by break */ ){
646 if( k+3>=N ){
647 while( k<N && jsonIsOk[z[k]] ){ k++; }
648 break;
650 if( !jsonIsOk[z[k]] ){
651 break;
653 if( !jsonIsOk[z[k+1]] ){
654 k += 1;
655 break;
657 if( !jsonIsOk[z[k+2]] ){
658 k += 2;
659 break;
661 if( !jsonIsOk[z[k+3]] ){
662 k += 3;
663 break;
664 }else{
665 k += 4;
668 if( k>=N ){
669 if( k>0 ){
670 memcpy(&p->zBuf[p->nUsed], z, k);
671 p->nUsed += k;
673 break;
675 if( k>0 ){
676 memcpy(&p->zBuf[p->nUsed], z, k);
677 p->nUsed += k;
678 z += k;
679 N -= k;
681 c = z[0];
682 if( c=='"' || c=='\\' ){
683 json_simple_escape:
684 if( (p->nUsed+N+3 > p->nAlloc) && jsonStringGrow(p,N+3)!=0 ) return;
685 p->zBuf[p->nUsed++] = '\\';
686 p->zBuf[p->nUsed++] = c;
687 }else if( c=='\'' ){
688 p->zBuf[p->nUsed++] = c;
689 }else{
690 static const char aSpecial[] = {
691 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
692 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
694 assert( sizeof(aSpecial)==32 );
695 assert( aSpecial['\b']=='b' );
696 assert( aSpecial['\f']=='f' );
697 assert( aSpecial['\n']=='n' );
698 assert( aSpecial['\r']=='r' );
699 assert( aSpecial['\t']=='t' );
700 assert( c>=0 && c<sizeof(aSpecial) );
701 if( aSpecial[c] ){
702 c = aSpecial[c];
703 goto json_simple_escape;
705 if( (p->nUsed+N+7 > p->nAlloc) && jsonStringGrow(p,N+7)!=0 ) return;
706 p->zBuf[p->nUsed++] = '\\';
707 p->zBuf[p->nUsed++] = 'u';
708 p->zBuf[p->nUsed++] = '0';
709 p->zBuf[p->nUsed++] = '0';
710 p->zBuf[p->nUsed++] = "0123456789abcdef"[c>>4];
711 p->zBuf[p->nUsed++] = "0123456789abcdef"[c&0xf];
713 z++;
714 N--;
716 p->zBuf[p->nUsed++] = '"';
717 assert( p->nUsed<p->nAlloc );
721 ** Append an sqlite3_value (such as a function parameter) to the JSON
722 ** string under construction in p.
724 static void jsonAppendSqlValue(
725 JsonString *p, /* Append to this JSON string */
726 sqlite3_value *pValue /* Value to append */
728 switch( sqlite3_value_type(pValue) ){
729 case SQLITE_NULL: {
730 jsonAppendRawNZ(p, "null", 4);
731 break;
733 case SQLITE_FLOAT: {
734 jsonPrintf(100, p, "%!0.15g", sqlite3_value_double(pValue));
735 break;
737 case SQLITE_INTEGER: {
738 const char *z = (const char*)sqlite3_value_text(pValue);
739 u32 n = (u32)sqlite3_value_bytes(pValue);
740 jsonAppendRaw(p, z, n);
741 break;
743 case SQLITE_TEXT: {
744 const char *z = (const char*)sqlite3_value_text(pValue);
745 u32 n = (u32)sqlite3_value_bytes(pValue);
746 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
747 jsonAppendRaw(p, z, n);
748 }else{
749 jsonAppendString(p, z, n);
751 break;
753 default: {
754 if( jsonFuncArgMightBeBinary(pValue) ){
755 JsonParse px;
756 memset(&px, 0, sizeof(px));
757 px.aBlob = (u8*)sqlite3_value_blob(pValue);
758 px.nBlob = sqlite3_value_bytes(pValue);
759 jsonTranslateBlobToText(&px, 0, p);
760 }else if( p->eErr==0 ){
761 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
762 p->eErr = JSTRING_ERR;
763 jsonStringReset(p);
765 break;
770 /* Make the text in p (which is probably a generated JSON text string)
771 ** the result of the SQL function.
773 ** The JsonString is reset.
775 ** If pParse and ctx are both non-NULL, then the SQL string in p is
776 ** loaded into the zJson field of the pParse object as a RCStr and the
777 ** pParse is added to the cache.
779 static void jsonReturnString(
780 JsonString *p, /* String to return */
781 JsonParse *pParse, /* JSONB source or NULL */
782 sqlite3_context *ctx /* Where to cache */
784 assert( (pParse!=0)==(ctx!=0) );
785 assert( ctx==0 || ctx==p->pCtx );
786 if( p->eErr==0 ){
787 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(p->pCtx));
788 if( flags & JSON_BLOB ){
789 jsonReturnStringAsBlob(p);
790 }else if( p->bStatic ){
791 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
792 SQLITE_TRANSIENT, SQLITE_UTF8);
793 }else if( jsonStringTerminate(p) ){
794 if( pParse && pParse->bJsonIsRCStr==0 && pParse->nBlobAlloc>0 ){
795 int rc;
796 pParse->zJson = sqlite3RCStrRef(p->zBuf);
797 pParse->nJson = p->nUsed;
798 pParse->bJsonIsRCStr = 1;
799 rc = jsonCacheInsert(ctx, pParse);
800 if( rc==SQLITE_NOMEM ){
801 sqlite3_result_error_nomem(ctx);
802 jsonStringReset(p);
803 return;
806 sqlite3_result_text64(p->pCtx, sqlite3RCStrRef(p->zBuf), p->nUsed,
807 sqlite3RCStrUnref,
808 SQLITE_UTF8);
809 }else{
810 sqlite3_result_error_nomem(p->pCtx);
812 }else if( p->eErr & JSTRING_OOM ){
813 sqlite3_result_error_nomem(p->pCtx);
814 }else if( p->eErr & JSTRING_MALFORMED ){
815 sqlite3_result_error(p->pCtx, "malformed JSON", -1);
817 jsonStringReset(p);
820 /**************************************************************************
821 ** Utility routines for dealing with JsonParse objects
822 **************************************************************************/
825 ** Reclaim all memory allocated by a JsonParse object. But do not
826 ** delete the JsonParse object itself.
828 static void jsonParseReset(JsonParse *pParse){
829 assert( pParse->nJPRef<=1 );
830 if( pParse->bJsonIsRCStr ){
831 sqlite3RCStrUnref(pParse->zJson);
832 pParse->zJson = 0;
833 pParse->nJson = 0;
834 pParse->bJsonIsRCStr = 0;
836 if( pParse->nBlobAlloc ){
837 sqlite3DbFree(pParse->db, pParse->aBlob);
838 pParse->aBlob = 0;
839 pParse->nBlob = 0;
840 pParse->nBlobAlloc = 0;
845 ** Decrement the reference count on the JsonParse object. When the
846 ** count reaches zero, free the object.
848 static void jsonParseFree(JsonParse *pParse){
849 if( pParse ){
850 if( pParse->nJPRef>1 ){
851 pParse->nJPRef--;
852 }else{
853 jsonParseReset(pParse);
854 sqlite3DbFree(pParse->db, pParse);
859 /**************************************************************************
860 ** Utility routines for the JSON text parser
861 **************************************************************************/
864 ** Translate a single byte of Hex into an integer.
865 ** This routine only gives a correct answer if h really is a valid hexadecimal
866 ** character: 0..9a..fA..F. But unlike sqlite3HexToInt(), it does not
867 ** assert() if the digit is not hex.
869 static u8 jsonHexToInt(int h){
870 #ifdef SQLITE_ASCII
871 h += 9*(1&(h>>6));
872 #endif
873 #ifdef SQLITE_EBCDIC
874 h += 9*(1&~(h>>4));
875 #endif
876 return (u8)(h & 0xf);
880 ** Convert a 4-byte hex string into an integer
882 static u32 jsonHexToInt4(const char *z){
883 u32 v;
884 v = (jsonHexToInt(z[0])<<12)
885 + (jsonHexToInt(z[1])<<8)
886 + (jsonHexToInt(z[2])<<4)
887 + jsonHexToInt(z[3]);
888 return v;
892 ** Return true if z[] begins with 2 (or more) hexadecimal digits
894 static int jsonIs2Hex(const char *z){
895 return sqlite3Isxdigit(z[0]) && sqlite3Isxdigit(z[1]);
899 ** Return true if z[] begins with 4 (or more) hexadecimal digits
901 static int jsonIs4Hex(const char *z){
902 return jsonIs2Hex(z) && jsonIs2Hex(&z[2]);
906 ** Return the number of bytes of JSON5 whitespace at the beginning of
907 ** the input string z[].
909 ** JSON5 whitespace consists of any of the following characters:
911 ** Unicode UTF-8 Name
912 ** U+0009 09 horizontal tab
913 ** U+000a 0a line feed
914 ** U+000b 0b vertical tab
915 ** U+000c 0c form feed
916 ** U+000d 0d carriage return
917 ** U+0020 20 space
918 ** U+00a0 c2 a0 non-breaking space
919 ** U+1680 e1 9a 80 ogham space mark
920 ** U+2000 e2 80 80 en quad
921 ** U+2001 e2 80 81 em quad
922 ** U+2002 e2 80 82 en space
923 ** U+2003 e2 80 83 em space
924 ** U+2004 e2 80 84 three-per-em space
925 ** U+2005 e2 80 85 four-per-em space
926 ** U+2006 e2 80 86 six-per-em space
927 ** U+2007 e2 80 87 figure space
928 ** U+2008 e2 80 88 punctuation space
929 ** U+2009 e2 80 89 thin space
930 ** U+200a e2 80 8a hair space
931 ** U+2028 e2 80 a8 line separator
932 ** U+2029 e2 80 a9 paragraph separator
933 ** U+202f e2 80 af narrow no-break space (NNBSP)
934 ** U+205f e2 81 9f medium mathematical space (MMSP)
935 ** U+3000 e3 80 80 ideographical space
936 ** U+FEFF ef bb bf byte order mark
938 ** In addition, comments between '/', '*' and '*', '/' and
939 ** from '/', '/' to end-of-line are also considered to be whitespace.
941 static int json5Whitespace(const char *zIn){
942 int n = 0;
943 const u8 *z = (u8*)zIn;
944 while( 1 /*exit by "goto whitespace_done"*/ ){
945 switch( z[n] ){
946 case 0x09:
947 case 0x0a:
948 case 0x0b:
949 case 0x0c:
950 case 0x0d:
951 case 0x20: {
952 n++;
953 break;
955 case '/': {
956 if( z[n+1]=='*' && z[n+2]!=0 ){
957 int j;
958 for(j=n+3; z[j]!='/' || z[j-1]!='*'; j++){
959 if( z[j]==0 ) goto whitespace_done;
961 n = j+1;
962 break;
963 }else if( z[n+1]=='/' ){
964 int j;
965 char c;
966 for(j=n+2; (c = z[j])!=0; j++){
967 if( c=='\n' || c=='\r' ) break;
968 if( 0xe2==(u8)c && 0x80==(u8)z[j+1]
969 && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2])
971 j += 2;
972 break;
975 n = j;
976 if( z[n] ) n++;
977 break;
979 goto whitespace_done;
981 case 0xc2: {
982 if( z[n+1]==0xa0 ){
983 n += 2;
984 break;
986 goto whitespace_done;
988 case 0xe1: {
989 if( z[n+1]==0x9a && z[n+2]==0x80 ){
990 n += 3;
991 break;
993 goto whitespace_done;
995 case 0xe2: {
996 if( z[n+1]==0x80 ){
997 u8 c = z[n+2];
998 if( c<0x80 ) goto whitespace_done;
999 if( c<=0x8a || c==0xa8 || c==0xa9 || c==0xaf ){
1000 n += 3;
1001 break;
1003 }else if( z[n+1]==0x81 && z[n+2]==0x9f ){
1004 n += 3;
1005 break;
1007 goto whitespace_done;
1009 case 0xe3: {
1010 if( z[n+1]==0x80 && z[n+2]==0x80 ){
1011 n += 3;
1012 break;
1014 goto whitespace_done;
1016 case 0xef: {
1017 if( z[n+1]==0xbb && z[n+2]==0xbf ){
1018 n += 3;
1019 break;
1021 goto whitespace_done;
1023 default: {
1024 goto whitespace_done;
1028 whitespace_done:
1029 return n;
1033 ** Extra floating-point literals to allow in JSON.
1035 static const struct NanInfName {
1036 char c1;
1037 char c2;
1038 char n;
1039 char eType;
1040 char nRepl;
1041 char *zMatch;
1042 char *zRepl;
1043 } aNanInfName[] = {
1044 { 'i', 'I', 3, JSONB_FLOAT, 7, "inf", "9.0e999" },
1045 { 'i', 'I', 8, JSONB_FLOAT, 7, "infinity", "9.0e999" },
1046 { 'n', 'N', 3, JSONB_NULL, 4, "NaN", "null" },
1047 { 'q', 'Q', 4, JSONB_NULL, 4, "QNaN", "null" },
1048 { 's', 'S', 4, JSONB_NULL, 4, "SNaN", "null" },
1053 ** Report the wrong number of arguments for json_insert(), json_replace()
1054 ** or json_set().
1056 static void jsonWrongNumArgs(
1057 sqlite3_context *pCtx,
1058 const char *zFuncName
1060 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
1061 zFuncName);
1062 sqlite3_result_error(pCtx, zMsg, -1);
1063 sqlite3_free(zMsg);
1066 /****************************************************************************
1067 ** Utility routines for dealing with the binary BLOB representation of JSON
1068 ****************************************************************************/
1071 ** Expand pParse->aBlob so that it holds at least N bytes.
1073 ** Return the number of errors.
1075 static int jsonBlobExpand(JsonParse *pParse, u32 N){
1076 u8 *aNew;
1077 u32 t;
1078 assert( N>pParse->nBlobAlloc );
1079 if( pParse->nBlobAlloc==0 ){
1080 t = 100;
1081 }else{
1082 t = pParse->nBlobAlloc*2;
1084 if( t<N ) t = N+100;
1085 aNew = sqlite3DbRealloc(pParse->db, pParse->aBlob, t);
1086 if( aNew==0 ){ pParse->oom = 1; return 1; }
1087 pParse->aBlob = aNew;
1088 pParse->nBlobAlloc = t;
1089 return 0;
1093 ** If pParse->aBlob is not previously editable (because it is taken
1094 ** from sqlite3_value_blob(), as indicated by the fact that
1095 ** pParse->nBlobAlloc==0 and pParse->nBlob>0) then make it editable
1096 ** by making a copy into space obtained from malloc.
1098 ** Return true on success. Return false on OOM.
1100 static int jsonBlobMakeEditable(JsonParse *pParse, u32 nExtra){
1101 u8 *aOld;
1102 u32 nSize;
1103 assert( !pParse->bReadOnly );
1104 if( pParse->oom ) return 0;
1105 if( pParse->nBlobAlloc>0 ) return 1;
1106 aOld = pParse->aBlob;
1107 nSize = pParse->nBlob + nExtra;
1108 pParse->aBlob = 0;
1109 if( jsonBlobExpand(pParse, nSize) ){
1110 return 0;
1112 assert( pParse->nBlobAlloc >= pParse->nBlob + nExtra );
1113 memcpy(pParse->aBlob, aOld, pParse->nBlob);
1114 return 1;
1117 /* Expand pParse->aBlob and append one bytes.
1119 static SQLITE_NOINLINE void jsonBlobExpandAndAppendOneByte(
1120 JsonParse *pParse,
1121 u8 c
1123 jsonBlobExpand(pParse, pParse->nBlob+1);
1124 if( pParse->oom==0 ){
1125 assert( pParse->nBlob+1<=pParse->nBlobAlloc );
1126 pParse->aBlob[pParse->nBlob++] = c;
1130 /* Append a single character.
1132 static void jsonBlobAppendOneByte(JsonParse *pParse, u8 c){
1133 if( pParse->nBlob >= pParse->nBlobAlloc ){
1134 jsonBlobExpandAndAppendOneByte(pParse, c);
1135 }else{
1136 pParse->aBlob[pParse->nBlob++] = c;
1140 /* Slow version of jsonBlobAppendNode() that first resizes the
1141 ** pParse->aBlob structure.
1143 static void jsonBlobAppendNode(JsonParse*,u8,u32,const void*);
1144 static SQLITE_NOINLINE void jsonBlobExpandAndAppendNode(
1145 JsonParse *pParse,
1146 u8 eType,
1147 u32 szPayload,
1148 const void *aPayload
1150 if( jsonBlobExpand(pParse, pParse->nBlob+szPayload+9) ) return;
1151 jsonBlobAppendNode(pParse, eType, szPayload, aPayload);
1155 /* Append an node type byte together with the payload size and
1156 ** possibly also the payload.
1158 ** If aPayload is not NULL, then it is a pointer to the payload which
1159 ** is also appended. If aPayload is NULL, the pParse->aBlob[] array
1160 ** is resized (if necessary) so that it is big enough to hold the
1161 ** payload, but the payload is not appended and pParse->nBlob is left
1162 ** pointing to where the first byte of payload will eventually be.
1164 static void jsonBlobAppendNode(
1165 JsonParse *pParse, /* The JsonParse object under construction */
1166 u8 eType, /* Node type. One of JSONB_* */
1167 u32 szPayload, /* Number of bytes of payload */
1168 const void *aPayload /* The payload. Might be NULL */
1170 u8 *a;
1171 if( pParse->nBlob+szPayload+9 > pParse->nBlobAlloc ){
1172 jsonBlobExpandAndAppendNode(pParse,eType,szPayload,aPayload);
1173 return;
1175 assert( pParse->aBlob!=0 );
1176 a = &pParse->aBlob[pParse->nBlob];
1177 if( szPayload<=11 ){
1178 a[0] = eType | (szPayload<<4);
1179 pParse->nBlob += 1;
1180 }else if( szPayload<=0xff ){
1181 a[0] = eType | 0xc0;
1182 a[1] = szPayload & 0xff;
1183 pParse->nBlob += 2;
1184 }else if( szPayload<=0xffff ){
1185 a[0] = eType | 0xd0;
1186 a[1] = (szPayload >> 8) & 0xff;
1187 a[2] = szPayload & 0xff;
1188 pParse->nBlob += 3;
1189 }else{
1190 a[0] = eType | 0xe0;
1191 a[1] = (szPayload >> 24) & 0xff;
1192 a[2] = (szPayload >> 16) & 0xff;
1193 a[3] = (szPayload >> 8) & 0xff;
1194 a[4] = szPayload & 0xff;
1195 pParse->nBlob += 5;
1197 if( aPayload ){
1198 pParse->nBlob += szPayload;
1199 memcpy(&pParse->aBlob[pParse->nBlob-szPayload], aPayload, szPayload);
1203 /* Change the payload size for the node at index i to be szPayload.
1205 static int jsonBlobChangePayloadSize(
1206 JsonParse *pParse,
1207 u32 i,
1208 u32 szPayload
1210 u8 *a;
1211 u8 szType;
1212 u8 nExtra;
1213 u8 nNeeded;
1214 int delta;
1215 if( pParse->oom ) return 0;
1216 a = &pParse->aBlob[i];
1217 szType = a[0]>>4;
1218 if( szType<=11 ){
1219 nExtra = 0;
1220 }else if( szType==12 ){
1221 nExtra = 1;
1222 }else if( szType==13 ){
1223 nExtra = 2;
1224 }else{
1225 nExtra = 4;
1227 if( szPayload<=11 ){
1228 nNeeded = 0;
1229 }else if( szPayload<=0xff ){
1230 nNeeded = 1;
1231 }else if( szPayload<=0xffff ){
1232 nNeeded = 2;
1233 }else{
1234 nNeeded = 4;
1236 delta = nNeeded - nExtra;
1237 if( delta ){
1238 u32 newSize = pParse->nBlob + delta;
1239 if( delta>0 ){
1240 if( newSize>pParse->nBlobAlloc && jsonBlobExpand(pParse, newSize) ){
1241 return 0; /* OOM error. Error state recorded in pParse->oom. */
1243 a = &pParse->aBlob[i];
1244 memmove(&a[1+delta], &a[1], pParse->nBlob - (i+1));
1245 }else{
1246 memmove(&a[1], &a[1-delta], pParse->nBlob - (i+1-delta));
1248 pParse->nBlob = newSize;
1250 if( nNeeded==0 ){
1251 a[0] = (a[0] & 0x0f) | (szPayload<<4);
1252 }else if( nNeeded==1 ){
1253 a[0] = (a[0] & 0x0f) | 0xc0;
1254 a[1] = szPayload & 0xff;
1255 }else if( nNeeded==2 ){
1256 a[0] = (a[0] & 0x0f) | 0xd0;
1257 a[1] = (szPayload >> 8) & 0xff;
1258 a[2] = szPayload & 0xff;
1259 }else{
1260 a[0] = (a[0] & 0x0f) | 0xe0;
1261 a[1] = (szPayload >> 24) & 0xff;
1262 a[2] = (szPayload >> 16) & 0xff;
1263 a[3] = (szPayload >> 8) & 0xff;
1264 a[4] = szPayload & 0xff;
1266 return delta;
1270 ** If z[0] is 'u' and is followed by exactly 4 hexadecimal character,
1271 ** then set *pOp to JSONB_TEXTJ and return true. If not, do not make
1272 ** any changes to *pOp and return false.
1274 static int jsonIs4HexB(const char *z, int *pOp){
1275 if( z[0]!='u' ) return 0;
1276 if( !jsonIs4Hex(&z[1]) ) return 0;
1277 *pOp = JSONB_TEXTJ;
1278 return 1;
1282 ** Check a single element of the JSONB in pParse for validity.
1284 ** The element to be checked starts at offset i and must end at on the
1285 ** last byte before iEnd.
1287 ** Return 0 if everything is correct. Return the 1-based byte offset of the
1288 ** error if a problem is detected. (In other words, if the error is at offset
1289 ** 0, return 1).
1291 static u32 jsonbValidityCheck(
1292 const JsonParse *pParse, /* Input JSONB. Only aBlob and nBlob are used */
1293 u32 i, /* Start of element as pParse->aBlob[i] */
1294 u32 iEnd, /* One more than the last byte of the element */
1295 u32 iDepth /* Current nesting depth */
1297 u32 n, sz, j, k;
1298 const u8 *z;
1299 u8 x;
1300 if( iDepth>JSON_MAX_DEPTH ) return i+1;
1301 sz = 0;
1302 n = jsonbPayloadSize(pParse, i, &sz);
1303 if( NEVER(n==0) ) return i+1; /* Checked by caller */
1304 if( NEVER(i+n+sz!=iEnd) ) return i+1; /* Checked by caller */
1305 z = pParse->aBlob;
1306 x = z[i] & 0x0f;
1307 switch( x ){
1308 case JSONB_NULL:
1309 case JSONB_TRUE:
1310 case JSONB_FALSE: {
1311 return n+sz==1 ? 0 : i+1;
1313 case JSONB_INT: {
1314 if( sz<1 ) return i+1;
1315 j = i+n;
1316 if( z[j]=='-' ){
1317 j++;
1318 if( sz<2 ) return i+1;
1320 k = i+n+sz;
1321 while( j<k ){
1322 if( sqlite3Isdigit(z[j]) ){
1323 j++;
1324 }else{
1325 return j+1;
1328 return 0;
1330 case JSONB_INT5: {
1331 if( sz<3 ) return i+1;
1332 j = i+n;
1333 if( z[j]=='-' ){
1334 if( sz<4 ) return i+1;
1335 j++;
1337 if( z[j]!='0' ) return i+1;
1338 if( z[j+1]!='x' && z[j+1]!='X' ) return j+2;
1339 j += 2;
1340 k = i+n+sz;
1341 while( j<k ){
1342 if( sqlite3Isxdigit(z[j]) ){
1343 j++;
1344 }else{
1345 return j+1;
1348 return 0;
1350 case JSONB_FLOAT:
1351 case JSONB_FLOAT5: {
1352 u8 seen = 0; /* 0: initial. 1: '.' seen 2: 'e' seen */
1353 if( sz<2 ) return i+1;
1354 j = i+n;
1355 k = j+sz;
1356 if( z[j]=='-' ){
1357 j++;
1358 if( sz<3 ) return i+1;
1360 if( z[j]=='.' ){
1361 if( x==JSONB_FLOAT ) return j+1;
1362 if( !sqlite3Isdigit(z[j+1]) ) return j+1;
1363 j += 2;
1364 seen = 1;
1365 }else if( z[j]=='0' && x==JSONB_FLOAT ){
1366 if( j+3>k ) return j+1;
1367 if( z[j+1]!='.' && z[j+1]!='e' && z[j+1]!='E' ) return j+1;
1368 j++;
1370 for(; j<k; j++){
1371 if( sqlite3Isdigit(z[j]) ) continue;
1372 if( z[j]=='.' ){
1373 if( seen>0 ) return j+1;
1374 if( x==JSONB_FLOAT && (j==k-1 || !sqlite3Isdigit(z[j+1])) ){
1375 return j+1;
1377 seen = 1;
1378 continue;
1380 if( z[j]=='e' || z[j]=='E' ){
1381 if( seen==2 ) return j+1;
1382 if( j==k-1 ) return j+1;
1383 if( z[j+1]=='+' || z[j+1]=='-' ){
1384 j++;
1385 if( j==k-1 ) return j+1;
1387 seen = 2;
1388 continue;
1390 return j+1;
1392 if( seen==0 ) return i+1;
1393 return 0;
1395 case JSONB_TEXT: {
1396 j = i+n;
1397 k = j+sz;
1398 while( j<k ){
1399 if( !jsonIsOk[z[j]] && z[j]!='\'' ) return j+1;
1400 j++;
1402 return 0;
1404 case JSONB_TEXTJ:
1405 case JSONB_TEXT5: {
1406 j = i+n;
1407 k = j+sz;
1408 while( j<k ){
1409 if( !jsonIsOk[z[j]] && z[j]!='\'' ){
1410 if( z[j]=='"' ){
1411 if( x==JSONB_TEXTJ ) return j+1;
1412 }else if( z[j]!='\\' || j+1>=k ){
1413 return j+1;
1414 }else if( strchr("\"\\/bfnrt",z[j+1])!=0 ){
1415 j++;
1416 }else if( z[j+1]=='u' ){
1417 if( j+5>=k ) return j+1;
1418 if( !jsonIs4Hex((const char*)&z[j+2]) ) return j+1;
1419 j++;
1420 }else if( x!=JSONB_TEXT5 ){
1421 return j+1;
1422 }else{
1423 u32 c = 0;
1424 u32 szC = jsonUnescapeOneChar((const char*)&z[j], k-j, &c);
1425 if( c==JSON_INVALID_CHAR ) return j+1;
1426 j += szC - 1;
1429 j++;
1431 return 0;
1433 case JSONB_TEXTRAW: {
1434 return 0;
1436 case JSONB_ARRAY: {
1437 u32 sub;
1438 j = i+n;
1439 k = j+sz;
1440 while( j<k ){
1441 sz = 0;
1442 n = jsonbPayloadSize(pParse, j, &sz);
1443 if( n==0 ) return j+1;
1444 if( j+n+sz>k ) return j+1;
1445 sub = jsonbValidityCheck(pParse, j, j+n+sz, iDepth+1);
1446 if( sub ) return sub;
1447 j += n + sz;
1449 assert( j==k );
1450 return 0;
1452 case JSONB_OBJECT: {
1453 u32 cnt = 0;
1454 u32 sub;
1455 j = i+n;
1456 k = j+sz;
1457 while( j<k ){
1458 sz = 0;
1459 n = jsonbPayloadSize(pParse, j, &sz);
1460 if( n==0 ) return j+1;
1461 if( j+n+sz>k ) return j+1;
1462 if( (cnt & 1)==0 ){
1463 x = z[j] & 0x0f;
1464 if( x<JSONB_TEXT || x>JSONB_TEXTRAW ) return j+1;
1466 sub = jsonbValidityCheck(pParse, j, j+n+sz, iDepth+1);
1467 if( sub ) return sub;
1468 cnt++;
1469 j += n + sz;
1471 assert( j==k );
1472 if( (cnt & 1)!=0 ) return j+1;
1473 return 0;
1475 default: {
1476 return i+1;
1482 ** Translate a single element of JSON text at pParse->zJson[i] into
1483 ** its equivalent binary JSONB representation. Append the translation into
1484 ** pParse->aBlob[] beginning at pParse->nBlob. The size of
1485 ** pParse->aBlob[] is increased as necessary.
1487 ** Return the index of the first character past the end of the element parsed,
1488 ** or one of the following special result codes:
1490 ** 0 End of input
1491 ** -1 Syntax error or OOM
1492 ** -2 '}' seen \
1493 ** -3 ']' seen \___ For these returns, pParse->iErr is set to
1494 ** -4 ',' seen / the index in zJson[] of the seen character
1495 ** -5 ':' seen /
1497 static int jsonTranslateTextToBlob(JsonParse *pParse, u32 i){
1498 char c;
1499 u32 j;
1500 u32 iThis, iStart;
1501 int x;
1502 u8 t;
1503 const char *z = pParse->zJson;
1504 json_parse_restart:
1505 switch( (u8)z[i] ){
1506 case '{': {
1507 /* Parse object */
1508 iThis = pParse->nBlob;
1509 jsonBlobAppendNode(pParse, JSONB_OBJECT, pParse->nJson-i, 0);
1510 if( ++pParse->iDepth > JSON_MAX_DEPTH ){
1511 pParse->iErr = i;
1512 return -1;
1514 iStart = pParse->nBlob;
1515 for(j=i+1;;j++){
1516 u32 iBlob = pParse->nBlob;
1517 x = jsonTranslateTextToBlob(pParse, j);
1518 if( x<=0 ){
1519 int op;
1520 if( x==(-2) ){
1521 j = pParse->iErr;
1522 if( pParse->nBlob!=(u32)iStart ) pParse->hasNonstd = 1;
1523 break;
1525 j += json5Whitespace(&z[j]);
1526 op = JSONB_TEXT;
1527 if( sqlite3JsonId1(z[j])
1528 || (z[j]=='\\' && jsonIs4HexB(&z[j+1], &op))
1530 int k = j+1;
1531 while( (sqlite3JsonId2(z[k]) && json5Whitespace(&z[k])==0)
1532 || (z[k]=='\\' && jsonIs4HexB(&z[k+1], &op))
1534 k++;
1536 assert( iBlob==pParse->nBlob );
1537 jsonBlobAppendNode(pParse, op, k-j, &z[j]);
1538 pParse->hasNonstd = 1;
1539 x = k;
1540 }else{
1541 if( x!=-1 ) pParse->iErr = j;
1542 return -1;
1545 if( pParse->oom ) return -1;
1546 t = pParse->aBlob[iBlob] & 0x0f;
1547 if( t<JSONB_TEXT || t>JSONB_TEXTRAW ){
1548 pParse->iErr = j;
1549 return -1;
1551 j = x;
1552 if( z[j]==':' ){
1553 j++;
1554 }else{
1555 if( jsonIsspace(z[j]) ){
1556 /* strspn() is not helpful here */
1557 do{ j++; }while( jsonIsspace(z[j]) );
1558 if( z[j]==':' ){
1559 j++;
1560 goto parse_object_value;
1563 x = jsonTranslateTextToBlob(pParse, j);
1564 if( x!=(-5) ){
1565 if( x!=(-1) ) pParse->iErr = j;
1566 return -1;
1568 j = pParse->iErr+1;
1570 parse_object_value:
1571 x = jsonTranslateTextToBlob(pParse, j);
1572 if( x<=0 ){
1573 if( x!=(-1) ) pParse->iErr = j;
1574 return -1;
1576 j = x;
1577 if( z[j]==',' ){
1578 continue;
1579 }else if( z[j]=='}' ){
1580 break;
1581 }else{
1582 if( jsonIsspace(z[j]) ){
1583 j += 1 + (u32)strspn(&z[j+1], jsonSpaces);
1584 if( z[j]==',' ){
1585 continue;
1586 }else if( z[j]=='}' ){
1587 break;
1590 x = jsonTranslateTextToBlob(pParse, j);
1591 if( x==(-4) ){
1592 j = pParse->iErr;
1593 continue;
1595 if( x==(-2) ){
1596 j = pParse->iErr;
1597 break;
1600 pParse->iErr = j;
1601 return -1;
1603 jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart);
1604 pParse->iDepth--;
1605 return j+1;
1607 case '[': {
1608 /* Parse array */
1609 iThis = pParse->nBlob;
1610 assert( i<=(u32)pParse->nJson );
1611 jsonBlobAppendNode(pParse, JSONB_ARRAY, pParse->nJson - i, 0);
1612 iStart = pParse->nBlob;
1613 if( pParse->oom ) return -1;
1614 if( ++pParse->iDepth > JSON_MAX_DEPTH ){
1615 pParse->iErr = i;
1616 return -1;
1618 for(j=i+1;;j++){
1619 x = jsonTranslateTextToBlob(pParse, j);
1620 if( x<=0 ){
1621 if( x==(-3) ){
1622 j = pParse->iErr;
1623 if( pParse->nBlob!=iStart ) pParse->hasNonstd = 1;
1624 break;
1626 if( x!=(-1) ) pParse->iErr = j;
1627 return -1;
1629 j = x;
1630 if( z[j]==',' ){
1631 continue;
1632 }else if( z[j]==']' ){
1633 break;
1634 }else{
1635 if( jsonIsspace(z[j]) ){
1636 j += 1 + (u32)strspn(&z[j+1], jsonSpaces);
1637 if( z[j]==',' ){
1638 continue;
1639 }else if( z[j]==']' ){
1640 break;
1643 x = jsonTranslateTextToBlob(pParse, j);
1644 if( x==(-4) ){
1645 j = pParse->iErr;
1646 continue;
1648 if( x==(-3) ){
1649 j = pParse->iErr;
1650 break;
1653 pParse->iErr = j;
1654 return -1;
1656 jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart);
1657 pParse->iDepth--;
1658 return j+1;
1660 case '\'': {
1661 u8 opcode;
1662 char cDelim;
1663 pParse->hasNonstd = 1;
1664 opcode = JSONB_TEXT;
1665 goto parse_string;
1666 case '"':
1667 /* Parse string */
1668 opcode = JSONB_TEXT;
1669 parse_string:
1670 cDelim = z[i];
1671 j = i+1;
1672 while( 1 /*exit-by-break*/ ){
1673 if( jsonIsOk[(u8)z[j]] ){
1674 if( !jsonIsOk[(u8)z[j+1]] ){
1675 j += 1;
1676 }else if( !jsonIsOk[(u8)z[j+2]] ){
1677 j += 2;
1678 }else{
1679 j += 3;
1680 continue;
1683 c = z[j];
1684 if( c==cDelim ){
1685 break;
1686 }else if( c=='\\' ){
1687 c = z[++j];
1688 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
1689 || c=='n' || c=='r' || c=='t'
1690 || (c=='u' && jsonIs4Hex(&z[j+1])) ){
1691 if( opcode==JSONB_TEXT ) opcode = JSONB_TEXTJ;
1692 }else if( c=='\'' || c=='0' || c=='v' || c=='\n'
1693 || (0xe2==(u8)c && 0x80==(u8)z[j+1]
1694 && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2]))
1695 || (c=='x' && jsonIs2Hex(&z[j+1])) ){
1696 opcode = JSONB_TEXT5;
1697 pParse->hasNonstd = 1;
1698 }else if( c=='\r' ){
1699 if( z[j+1]=='\n' ) j++;
1700 opcode = JSONB_TEXT5;
1701 pParse->hasNonstd = 1;
1702 }else{
1703 pParse->iErr = j;
1704 return -1;
1706 }else if( c<=0x1f ){
1707 /* Control characters are not allowed in strings */
1708 pParse->iErr = j;
1709 return -1;
1710 }else if( c=='"' ){
1711 opcode = JSONB_TEXT5;
1713 j++;
1715 jsonBlobAppendNode(pParse, opcode, j-1-i, &z[i+1]);
1716 return j+1;
1718 case 't': {
1719 if( strncmp(z+i,"true",4)==0 && !sqlite3Isalnum(z[i+4]) ){
1720 jsonBlobAppendOneByte(pParse, JSONB_TRUE);
1721 return i+4;
1723 pParse->iErr = i;
1724 return -1;
1726 case 'f': {
1727 if( strncmp(z+i,"false",5)==0 && !sqlite3Isalnum(z[i+5]) ){
1728 jsonBlobAppendOneByte(pParse, JSONB_FALSE);
1729 return i+5;
1731 pParse->iErr = i;
1732 return -1;
1734 case '+': {
1735 u8 seenE;
1736 pParse->hasNonstd = 1;
1737 t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
1738 goto parse_number;
1739 case '.':
1740 if( sqlite3Isdigit(z[i+1]) ){
1741 pParse->hasNonstd = 1;
1742 t = 0x03; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
1743 seenE = 0;
1744 goto parse_number_2;
1746 pParse->iErr = i;
1747 return -1;
1748 case '-':
1749 case '0':
1750 case '1':
1751 case '2':
1752 case '3':
1753 case '4':
1754 case '5':
1755 case '6':
1756 case '7':
1757 case '8':
1758 case '9':
1759 /* Parse number */
1760 t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
1761 parse_number:
1762 seenE = 0;
1763 assert( '-' < '0' );
1764 assert( '+' < '0' );
1765 assert( '.' < '0' );
1766 c = z[i];
1768 if( c<='0' ){
1769 if( c=='0' ){
1770 if( (z[i+1]=='x' || z[i+1]=='X') && sqlite3Isxdigit(z[i+2]) ){
1771 assert( t==0x00 );
1772 pParse->hasNonstd = 1;
1773 t = 0x01;
1774 for(j=i+3; sqlite3Isxdigit(z[j]); j++){}
1775 goto parse_number_finish;
1776 }else if( sqlite3Isdigit(z[i+1]) ){
1777 pParse->iErr = i+1;
1778 return -1;
1780 }else{
1781 if( !sqlite3Isdigit(z[i+1]) ){
1782 /* JSON5 allows for "+Infinity" and "-Infinity" using exactly
1783 ** that case. SQLite also allows these in any case and it allows
1784 ** "+inf" and "-inf". */
1785 if( (z[i+1]=='I' || z[i+1]=='i')
1786 && sqlite3StrNICmp(&z[i+1], "inf",3)==0
1788 pParse->hasNonstd = 1;
1789 if( z[i]=='-' ){
1790 jsonBlobAppendNode(pParse, JSONB_FLOAT, 6, "-9e999");
1791 }else{
1792 jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999");
1794 return i + (sqlite3StrNICmp(&z[i+4],"inity",5)==0 ? 9 : 4);
1796 if( z[i+1]=='.' ){
1797 pParse->hasNonstd = 1;
1798 t |= 0x01;
1799 goto parse_number_2;
1801 pParse->iErr = i;
1802 return -1;
1804 if( z[i+1]=='0' ){
1805 if( sqlite3Isdigit(z[i+2]) ){
1806 pParse->iErr = i+1;
1807 return -1;
1808 }else if( (z[i+2]=='x' || z[i+2]=='X') && sqlite3Isxdigit(z[i+3]) ){
1809 pParse->hasNonstd = 1;
1810 t |= 0x01;
1811 for(j=i+4; sqlite3Isxdigit(z[j]); j++){}
1812 goto parse_number_finish;
1818 parse_number_2:
1819 for(j=i+1;; j++){
1820 c = z[j];
1821 if( sqlite3Isdigit(c) ) continue;
1822 if( c=='.' ){
1823 if( (t & 0x02)!=0 ){
1824 pParse->iErr = j;
1825 return -1;
1827 t |= 0x02;
1828 continue;
1830 if( c=='e' || c=='E' ){
1831 if( z[j-1]<'0' ){
1832 if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){
1833 pParse->hasNonstd = 1;
1834 t |= 0x01;
1835 }else{
1836 pParse->iErr = j;
1837 return -1;
1840 if( seenE ){
1841 pParse->iErr = j;
1842 return -1;
1844 t |= 0x02;
1845 seenE = 1;
1846 c = z[j+1];
1847 if( c=='+' || c=='-' ){
1848 j++;
1849 c = z[j+1];
1851 if( c<'0' || c>'9' ){
1852 pParse->iErr = j;
1853 return -1;
1855 continue;
1857 break;
1859 if( z[j-1]<'0' ){
1860 if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){
1861 pParse->hasNonstd = 1;
1862 t |= 0x01;
1863 }else{
1864 pParse->iErr = j;
1865 return -1;
1868 parse_number_finish:
1869 assert( JSONB_INT+0x01==JSONB_INT5 );
1870 assert( JSONB_FLOAT+0x01==JSONB_FLOAT5 );
1871 assert( JSONB_INT+0x02==JSONB_FLOAT );
1872 if( z[i]=='+' ) i++;
1873 jsonBlobAppendNode(pParse, JSONB_INT+t, j-i, &z[i]);
1874 return j;
1876 case '}': {
1877 pParse->iErr = i;
1878 return -2; /* End of {...} */
1880 case ']': {
1881 pParse->iErr = i;
1882 return -3; /* End of [...] */
1884 case ',': {
1885 pParse->iErr = i;
1886 return -4; /* List separator */
1888 case ':': {
1889 pParse->iErr = i;
1890 return -5; /* Object label/value separator */
1892 case 0: {
1893 return 0; /* End of file */
1895 case 0x09:
1896 case 0x0a:
1897 case 0x0d:
1898 case 0x20: {
1899 i += 1 + (u32)strspn(&z[i+1], jsonSpaces);
1900 goto json_parse_restart;
1902 case 0x0b:
1903 case 0x0c:
1904 case '/':
1905 case 0xc2:
1906 case 0xe1:
1907 case 0xe2:
1908 case 0xe3:
1909 case 0xef: {
1910 j = json5Whitespace(&z[i]);
1911 if( j>0 ){
1912 i += j;
1913 pParse->hasNonstd = 1;
1914 goto json_parse_restart;
1916 pParse->iErr = i;
1917 return -1;
1919 case 'n': {
1920 if( strncmp(z+i,"null",4)==0 && !sqlite3Isalnum(z[i+4]) ){
1921 jsonBlobAppendOneByte(pParse, JSONB_NULL);
1922 return i+4;
1924 /* fall-through into the default case that checks for NaN */
1926 default: {
1927 u32 k;
1928 int nn;
1929 c = z[i];
1930 for(k=0; k<sizeof(aNanInfName)/sizeof(aNanInfName[0]); k++){
1931 if( c!=aNanInfName[k].c1 && c!=aNanInfName[k].c2 ) continue;
1932 nn = aNanInfName[k].n;
1933 if( sqlite3StrNICmp(&z[i], aNanInfName[k].zMatch, nn)!=0 ){
1934 continue;
1936 if( sqlite3Isalnum(z[i+nn]) ) continue;
1937 if( aNanInfName[k].eType==JSONB_FLOAT ){
1938 jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999");
1939 }else{
1940 jsonBlobAppendOneByte(pParse, JSONB_NULL);
1942 pParse->hasNonstd = 1;
1943 return i + nn;
1945 pParse->iErr = i;
1946 return -1; /* Syntax error */
1948 } /* End switch(z[i]) */
1953 ** Parse a complete JSON string. Return 0 on success or non-zero if there
1954 ** are any errors. If an error occurs, free all memory held by pParse,
1955 ** but not pParse itself.
1957 ** pParse must be initialized to an empty parse object prior to calling
1958 ** this routine.
1960 static int jsonConvertTextToBlob(
1961 JsonParse *pParse, /* Initialize and fill this JsonParse object */
1962 sqlite3_context *pCtx /* Report errors here */
1964 int i;
1965 const char *zJson = pParse->zJson;
1966 i = jsonTranslateTextToBlob(pParse, 0);
1967 if( pParse->oom ) i = -1;
1968 if( i>0 ){
1969 #ifdef SQLITE_DEBUG
1970 assert( pParse->iDepth==0 );
1971 if( sqlite3Config.bJsonSelfcheck ){
1972 assert( jsonbValidityCheck(pParse, 0, pParse->nBlob, 0)==0 );
1974 #endif
1975 while( jsonIsspace(zJson[i]) ) i++;
1976 if( zJson[i] ){
1977 i += json5Whitespace(&zJson[i]);
1978 if( zJson[i] ){
1979 if( pCtx ) sqlite3_result_error(pCtx, "malformed JSON", -1);
1980 jsonParseReset(pParse);
1981 return 1;
1983 pParse->hasNonstd = 1;
1986 if( i<=0 ){
1987 if( pCtx!=0 ){
1988 if( pParse->oom ){
1989 sqlite3_result_error_nomem(pCtx);
1990 }else{
1991 sqlite3_result_error(pCtx, "malformed JSON", -1);
1994 jsonParseReset(pParse);
1995 return 1;
1997 return 0;
2001 ** The input string pStr is a well-formed JSON text string. Convert
2002 ** this into the JSONB format and make it the return value of the
2003 ** SQL function.
2005 static void jsonReturnStringAsBlob(JsonString *pStr){
2006 JsonParse px;
2007 memset(&px, 0, sizeof(px));
2008 jsonStringTerminate(pStr);
2009 if( pStr->eErr ){
2010 sqlite3_result_error_nomem(pStr->pCtx);
2011 return;
2013 px.zJson = pStr->zBuf;
2014 px.nJson = pStr->nUsed;
2015 px.db = sqlite3_context_db_handle(pStr->pCtx);
2016 (void)jsonTranslateTextToBlob(&px, 0);
2017 if( px.oom ){
2018 sqlite3DbFree(px.db, px.aBlob);
2019 sqlite3_result_error_nomem(pStr->pCtx);
2020 }else{
2021 assert( px.nBlobAlloc>0 );
2022 assert( !px.bReadOnly );
2023 sqlite3_result_blob(pStr->pCtx, px.aBlob, px.nBlob, SQLITE_DYNAMIC);
2027 /* The byte at index i is a node type-code. This routine
2028 ** determines the payload size for that node and writes that
2029 ** payload size in to *pSz. It returns the offset from i to the
2030 ** beginning of the payload. Return 0 on error.
2032 static u32 jsonbPayloadSize(const JsonParse *pParse, u32 i, u32 *pSz){
2033 u8 x;
2034 u32 sz;
2035 u32 n;
2036 if( NEVER(i>pParse->nBlob) ){
2037 *pSz = 0;
2038 return 0;
2040 x = pParse->aBlob[i]>>4;
2041 if( x<=11 ){
2042 sz = x;
2043 n = 1;
2044 }else if( x==12 ){
2045 if( i+1>=pParse->nBlob ){
2046 *pSz = 0;
2047 return 0;
2049 sz = pParse->aBlob[i+1];
2050 n = 2;
2051 }else if( x==13 ){
2052 if( i+2>=pParse->nBlob ){
2053 *pSz = 0;
2054 return 0;
2056 sz = (pParse->aBlob[i+1]<<8) + pParse->aBlob[i+2];
2057 n = 3;
2058 }else if( x==14 ){
2059 if( i+4>=pParse->nBlob ){
2060 *pSz = 0;
2061 return 0;
2063 sz = ((u32)pParse->aBlob[i+1]<<24) + (pParse->aBlob[i+2]<<16) +
2064 (pParse->aBlob[i+3]<<8) + pParse->aBlob[i+4];
2065 n = 5;
2066 }else{
2067 if( i+8>=pParse->nBlob
2068 || pParse->aBlob[i+1]!=0
2069 || pParse->aBlob[i+2]!=0
2070 || pParse->aBlob[i+3]!=0
2071 || pParse->aBlob[i+4]!=0
2073 *pSz = 0;
2074 return 0;
2076 sz = (pParse->aBlob[i+5]<<24) + (pParse->aBlob[i+6]<<16) +
2077 (pParse->aBlob[i+7]<<8) + pParse->aBlob[i+8];
2078 n = 9;
2080 if( (i64)i+sz+n > pParse->nBlob
2081 && (i64)i+sz+n > pParse->nBlob-pParse->delta
2083 sz = 0;
2084 n = 0;
2086 *pSz = sz;
2087 return n;
2092 ** Translate the binary JSONB representation of JSON beginning at
2093 ** pParse->aBlob[i] into a JSON text string. Append the JSON
2094 ** text onto the end of pOut. Return the index in pParse->aBlob[]
2095 ** of the first byte past the end of the element that is translated.
2097 ** If an error is detected in the BLOB input, the pOut->eErr flag
2098 ** might get set to JSTRING_MALFORMED. But not all BLOB input errors
2099 ** are detected. So a malformed JSONB input might either result
2100 ** in an error, or in incorrect JSON.
2102 ** The pOut->eErr JSTRING_OOM flag is set on a OOM.
2104 static u32 jsonTranslateBlobToText(
2105 const JsonParse *pParse, /* the complete parse of the JSON */
2106 u32 i, /* Start rendering at this index */
2107 JsonString *pOut /* Write JSON here */
2109 u32 sz, n, j, iEnd;
2111 n = jsonbPayloadSize(pParse, i, &sz);
2112 if( n==0 ){
2113 pOut->eErr |= JSTRING_MALFORMED;
2114 return pParse->nBlob+1;
2116 switch( pParse->aBlob[i] & 0x0f ){
2117 case JSONB_NULL: {
2118 jsonAppendRawNZ(pOut, "null", 4);
2119 return i+1;
2121 case JSONB_TRUE: {
2122 jsonAppendRawNZ(pOut, "true", 4);
2123 return i+1;
2125 case JSONB_FALSE: {
2126 jsonAppendRawNZ(pOut, "false", 5);
2127 return i+1;
2129 case JSONB_INT:
2130 case JSONB_FLOAT: {
2131 if( sz==0 ) goto malformed_jsonb;
2132 jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n], sz);
2133 break;
2135 case JSONB_INT5: { /* Integer literal in hexadecimal notation */
2136 u32 k = 2;
2137 sqlite3_uint64 u = 0;
2138 const char *zIn = (const char*)&pParse->aBlob[i+n];
2139 int bOverflow = 0;
2140 if( sz==0 ) goto malformed_jsonb;
2141 if( zIn[0]=='-' ){
2142 jsonAppendChar(pOut, '-');
2143 k++;
2144 }else if( zIn[0]=='+' ){
2145 k++;
2147 for(; k<sz; k++){
2148 if( !sqlite3Isxdigit(zIn[k]) ){
2149 pOut->eErr |= JSTRING_MALFORMED;
2150 break;
2151 }else if( (u>>60)!=0 ){
2152 bOverflow = 1;
2153 }else{
2154 u = u*16 + sqlite3HexToInt(zIn[k]);
2157 jsonPrintf(100,pOut,bOverflow?"9.0e999":"%llu", u);
2158 break;
2160 case JSONB_FLOAT5: { /* Float literal missing digits beside "." */
2161 u32 k = 0;
2162 const char *zIn = (const char*)&pParse->aBlob[i+n];
2163 if( sz==0 ) goto malformed_jsonb;
2164 if( zIn[0]=='-' ){
2165 jsonAppendChar(pOut, '-');
2166 k++;
2168 if( zIn[k]=='.' ){
2169 jsonAppendChar(pOut, '0');
2171 for(; k<sz; k++){
2172 jsonAppendChar(pOut, zIn[k]);
2173 if( zIn[k]=='.' && (k+1==sz || !sqlite3Isdigit(zIn[k+1])) ){
2174 jsonAppendChar(pOut, '0');
2177 break;
2179 case JSONB_TEXT:
2180 case JSONB_TEXTJ: {
2181 jsonAppendChar(pOut, '"');
2182 jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n], sz);
2183 jsonAppendChar(pOut, '"');
2184 break;
2186 case JSONB_TEXT5: {
2187 const char *zIn;
2188 u32 k;
2189 u32 sz2 = sz;
2190 zIn = (const char*)&pParse->aBlob[i+n];
2191 jsonAppendChar(pOut, '"');
2192 while( sz2>0 ){
2193 for(k=0; k<sz2 && zIn[k]!='\\' && zIn[k]!='"'; k++){}
2194 if( k>0 ){
2195 jsonAppendRawNZ(pOut, zIn, k);
2196 if( k>=sz2 ){
2197 break;
2199 zIn += k;
2200 sz2 -= k;
2202 if( zIn[0]=='"' ){
2203 jsonAppendRawNZ(pOut, "\\\"", 2);
2204 zIn++;
2205 sz2--;
2206 continue;
2208 assert( zIn[0]=='\\' );
2209 assert( sz2>=1 );
2210 if( sz2<2 ){
2211 pOut->eErr |= JSTRING_MALFORMED;
2212 break;
2214 switch( (u8)zIn[1] ){
2215 case '\'':
2216 jsonAppendChar(pOut, '\'');
2217 break;
2218 case 'v':
2219 jsonAppendRawNZ(pOut, "\\u0009", 6);
2220 break;
2221 case 'x':
2222 if( sz2<4 ){
2223 pOut->eErr |= JSTRING_MALFORMED;
2224 sz2 = 2;
2225 break;
2227 jsonAppendRawNZ(pOut, "\\u00", 4);
2228 jsonAppendRawNZ(pOut, &zIn[2], 2);
2229 zIn += 2;
2230 sz2 -= 2;
2231 break;
2232 case '0':
2233 jsonAppendRawNZ(pOut, "\\u0000", 6);
2234 break;
2235 case '\r':
2236 if( sz2>2 && zIn[2]=='\n' ){
2237 zIn++;
2238 sz2--;
2240 break;
2241 case '\n':
2242 break;
2243 case 0xe2:
2244 /* '\' followed by either U+2028 or U+2029 is ignored as
2245 ** whitespace. Not that in UTF8, U+2028 is 0xe2 0x80 0x29.
2246 ** U+2029 is the same except for the last byte */
2247 if( sz2<4
2248 || 0x80!=(u8)zIn[2]
2249 || (0xa8!=(u8)zIn[3] && 0xa9!=(u8)zIn[3])
2251 pOut->eErr |= JSTRING_MALFORMED;
2252 sz2 = 2;
2253 break;
2255 zIn += 2;
2256 sz2 -= 2;
2257 break;
2258 default:
2259 jsonAppendRawNZ(pOut, zIn, 2);
2260 break;
2262 assert( sz2>=2 );
2263 zIn += 2;
2264 sz2 -= 2;
2266 jsonAppendChar(pOut, '"');
2267 break;
2269 case JSONB_TEXTRAW: {
2270 jsonAppendString(pOut, (const char*)&pParse->aBlob[i+n], sz);
2271 break;
2273 case JSONB_ARRAY: {
2274 jsonAppendChar(pOut, '[');
2275 j = i+n;
2276 iEnd = j+sz;
2277 while( j<iEnd && pOut->eErr==0 ){
2278 j = jsonTranslateBlobToText(pParse, j, pOut);
2279 jsonAppendChar(pOut, ',');
2281 if( j>iEnd ) pOut->eErr |= JSTRING_MALFORMED;
2282 if( sz>0 ) jsonStringTrimOneChar(pOut);
2283 jsonAppendChar(pOut, ']');
2284 break;
2286 case JSONB_OBJECT: {
2287 int x = 0;
2288 jsonAppendChar(pOut, '{');
2289 j = i+n;
2290 iEnd = j+sz;
2291 while( j<iEnd && pOut->eErr==0 ){
2292 j = jsonTranslateBlobToText(pParse, j, pOut);
2293 jsonAppendChar(pOut, (x++ & 1) ? ',' : ':');
2295 if( (x & 1)!=0 || j>iEnd ) pOut->eErr |= JSTRING_MALFORMED;
2296 if( sz>0 ) jsonStringTrimOneChar(pOut);
2297 jsonAppendChar(pOut, '}');
2298 break;
2301 default: {
2302 malformed_jsonb:
2303 pOut->eErr |= JSTRING_MALFORMED;
2304 break;
2307 return i+n+sz;
2310 /* Return true if the input pJson
2312 ** For performance reasons, this routine does not do a detailed check of the
2313 ** input BLOB to ensure that it is well-formed. Hence, false positives are
2314 ** possible. False negatives should never occur, however.
2316 static int jsonFuncArgMightBeBinary(sqlite3_value *pJson){
2317 u32 sz, n;
2318 const u8 *aBlob;
2319 int nBlob;
2320 JsonParse s;
2321 if( sqlite3_value_type(pJson)!=SQLITE_BLOB ) return 0;
2322 aBlob = sqlite3_value_blob(pJson);
2323 nBlob = sqlite3_value_bytes(pJson);
2324 if( nBlob<1 ) return 0;
2325 if( NEVER(aBlob==0) || (aBlob[0] & 0x0f)>JSONB_OBJECT ) return 0;
2326 memset(&s, 0, sizeof(s));
2327 s.aBlob = (u8*)aBlob;
2328 s.nBlob = nBlob;
2329 n = jsonbPayloadSize(&s, 0, &sz);
2330 if( n==0 ) return 0;
2331 if( sz+n!=(u32)nBlob ) return 0;
2332 if( (aBlob[0] & 0x0f)<=JSONB_FALSE && sz>0 ) return 0;
2333 return sz+n==(u32)nBlob;
2337 ** Given that a JSONB_ARRAY object starts at offset i, return
2338 ** the number of entries in that array.
2340 static u32 jsonbArrayCount(JsonParse *pParse, u32 iRoot){
2341 u32 n, sz, i, iEnd;
2342 u32 k = 0;
2343 n = jsonbPayloadSize(pParse, iRoot, &sz);
2344 iEnd = iRoot+n+sz;
2345 for(i=iRoot+n; n>0 && i<iEnd; i+=sz+n, k++){
2346 n = jsonbPayloadSize(pParse, i, &sz);
2348 return k;
2352 ** Edit the payload size of the element at iRoot by the amount in
2353 ** pParse->delta.
2355 static void jsonAfterEditSizeAdjust(JsonParse *pParse, u32 iRoot){
2356 u32 sz = 0;
2357 u32 nBlob;
2358 assert( pParse->delta!=0 );
2359 assert( pParse->nBlobAlloc >= pParse->nBlob );
2360 nBlob = pParse->nBlob;
2361 pParse->nBlob = pParse->nBlobAlloc;
2362 (void)jsonbPayloadSize(pParse, iRoot, &sz);
2363 pParse->nBlob = nBlob;
2364 sz += pParse->delta;
2365 pParse->delta += jsonBlobChangePayloadSize(pParse, iRoot, sz);
2369 ** Modify the JSONB blob at pParse->aBlob by removing nDel bytes of
2370 ** content beginning at iDel, and replacing them with nIns bytes of
2371 ** content given by aIns.
2373 ** nDel may be zero, in which case no bytes are removed. But iDel is
2374 ** still important as new bytes will be insert beginning at iDel.
2376 ** aIns may be zero, in which case space is created to hold nIns bytes
2377 ** beginning at iDel, but that space is uninitialized.
2379 ** Set pParse->oom if an OOM occurs.
2381 static void jsonBlobEdit(
2382 JsonParse *pParse, /* The JSONB to be modified is in pParse->aBlob */
2383 u32 iDel, /* First byte to be removed */
2384 u32 nDel, /* Number of bytes to remove */
2385 const u8 *aIns, /* Content to insert */
2386 u32 nIns /* Bytes of content to insert */
2388 i64 d = (i64)nIns - (i64)nDel;
2389 if( d!=0 ){
2390 if( pParse->nBlob + d > pParse->nBlobAlloc ){
2391 jsonBlobExpand(pParse, pParse->nBlob+d);
2392 if( pParse->oom ) return;
2394 memmove(&pParse->aBlob[iDel+nIns],
2395 &pParse->aBlob[iDel+nDel],
2396 pParse->nBlob - (iDel+nDel));
2397 pParse->nBlob += d;
2398 pParse->delta += d;
2400 if( nIns && aIns ) memcpy(&pParse->aBlob[iDel], aIns, nIns);
2404 ** Return the number of escaped newlines to be ignored.
2405 ** An escaped newline is a one of the following byte sequences:
2407 ** 0x5c 0x0a
2408 ** 0x5c 0x0d
2409 ** 0x5c 0x0d 0x0a
2410 ** 0x5c 0xe2 0x80 0xa8
2411 ** 0x5c 0xe2 0x80 0xa9
2413 static u32 jsonBytesToBypass(const char *z, u32 n){
2414 u32 i = 0;
2415 while( i+1<n ){
2416 if( z[i]!='\\' ) return i;
2417 if( z[i+1]=='\n' ){
2418 i += 2;
2419 continue;
2421 if( z[i+1]=='\r' ){
2422 if( i+2<n && z[i+2]=='\n' ){
2423 i += 3;
2424 }else{
2425 i += 2;
2427 continue;
2429 if( 0xe2==(u8)z[i+1]
2430 && i+3<n
2431 && 0x80==(u8)z[i+2]
2432 && (0xa8==(u8)z[i+3] || 0xa9==(u8)z[i+3])
2434 i += 4;
2435 continue;
2437 break;
2439 return i;
2443 ** Input z[0..n] defines JSON escape sequence including the leading '\\'.
2444 ** Decode that escape sequence into a single character. Write that
2445 ** character into *piOut. Return the number of bytes in the escape sequence.
2447 ** If there is a syntax error of some kind (for example too few characters
2448 ** after the '\\' to complete the encoding) then *piOut is set to
2449 ** JSON_INVALID_CHAR.
2451 static u32 jsonUnescapeOneChar(const char *z, u32 n, u32 *piOut){
2452 assert( n>0 );
2453 assert( z[0]=='\\' );
2454 if( n<2 ){
2455 *piOut = JSON_INVALID_CHAR;
2456 return n;
2458 switch( (u8)z[1] ){
2459 case 'u': {
2460 u32 v, vlo;
2461 if( n<6 ){
2462 *piOut = JSON_INVALID_CHAR;
2463 return n;
2465 v = jsonHexToInt4(&z[2]);
2466 if( (v & 0xfc00)==0xd800
2467 && n>=12
2468 && z[6]=='\\'
2469 && z[7]=='u'
2470 && ((vlo = jsonHexToInt4(&z[8]))&0xfc00)==0xdc00
2472 *piOut = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
2473 return 12;
2474 }else{
2475 *piOut = v;
2476 return 6;
2479 case 'b': { *piOut = '\b'; return 2; }
2480 case 'f': { *piOut = '\f'; return 2; }
2481 case 'n': { *piOut = '\n'; return 2; }
2482 case 'r': { *piOut = '\r'; return 2; }
2483 case 't': { *piOut = '\t'; return 2; }
2484 case 'v': { *piOut = '\v'; return 2; }
2485 case '0': { *piOut = 0; return 2; }
2486 case '\'':
2487 case '"':
2488 case '/':
2489 case '\\':{ *piOut = z[1]; return 2; }
2490 case 'x': {
2491 if( n<4 ){
2492 *piOut = JSON_INVALID_CHAR;
2493 return n;
2495 *piOut = (jsonHexToInt(z[2])<<4) | jsonHexToInt(z[3]);
2496 return 4;
2498 case 0xe2:
2499 case '\r':
2500 case '\n': {
2501 u32 nSkip = jsonBytesToBypass(z, n);
2502 if( nSkip==0 ){
2503 *piOut = JSON_INVALID_CHAR;
2504 return n;
2505 }else if( nSkip==n ){
2506 *piOut = 0;
2507 return n;
2508 }else if( z[nSkip]=='\\' ){
2509 return nSkip + jsonUnescapeOneChar(&z[nSkip], n-nSkip, piOut);
2510 }else{
2511 int sz = sqlite3Utf8ReadLimited((u8*)&z[nSkip], n-nSkip, piOut);
2512 return nSkip + sz;
2515 default: {
2516 *piOut = JSON_INVALID_CHAR;
2517 return 2;
2524 ** Compare two object labels. Return 1 if they are equal and
2525 ** 0 if they differ.
2527 ** In this version, we know that one or the other or both of the
2528 ** two comparands contains an escape sequence.
2530 static SQLITE_NOINLINE int jsonLabelCompareEscaped(
2531 const char *zLeft, /* The left label */
2532 u32 nLeft, /* Size of the left label in bytes */
2533 int rawLeft, /* True if zLeft contains no escapes */
2534 const char *zRight, /* The right label */
2535 u32 nRight, /* Size of the right label in bytes */
2536 int rawRight /* True if zRight is escape-free */
2538 u32 cLeft, cRight;
2539 assert( rawLeft==0 || rawRight==0 );
2540 while( 1 /*exit-by-return*/ ){
2541 if( nLeft==0 ){
2542 cLeft = 0;
2543 }else if( rawLeft || zLeft[0]!='\\' ){
2544 cLeft = ((u8*)zLeft)[0];
2545 if( cLeft>=0xc0 ){
2546 int sz = sqlite3Utf8ReadLimited((u8*)zLeft, nLeft, &cLeft);
2547 zLeft += sz;
2548 nLeft -= sz;
2549 }else{
2550 zLeft++;
2551 nLeft--;
2553 }else{
2554 u32 n = jsonUnescapeOneChar(zLeft, nLeft, &cLeft);
2555 zLeft += n;
2556 assert( n<=nLeft );
2557 nLeft -= n;
2559 if( nRight==0 ){
2560 cRight = 0;
2561 }else if( rawRight || zRight[0]!='\\' ){
2562 cRight = ((u8*)zRight)[0];
2563 if( cRight>=0xc0 ){
2564 int sz = sqlite3Utf8ReadLimited((u8*)zRight, nRight, &cRight);
2565 zRight += sz;
2566 nRight -= sz;
2567 }else{
2568 zRight++;
2569 nRight--;
2571 }else{
2572 u32 n = jsonUnescapeOneChar(zRight, nRight, &cRight);
2573 zRight += n;
2574 assert( n<=nRight );
2575 nRight -= n;
2577 if( cLeft!=cRight ) return 0;
2578 if( cLeft==0 ) return 1;
2583 ** Compare two object labels. Return 1 if they are equal and
2584 ** 0 if they differ. Return -1 if an OOM occurs.
2586 static int jsonLabelCompare(
2587 const char *zLeft, /* The left label */
2588 u32 nLeft, /* Size of the left label in bytes */
2589 int rawLeft, /* True if zLeft contains no escapes */
2590 const char *zRight, /* The right label */
2591 u32 nRight, /* Size of the right label in bytes */
2592 int rawRight /* True if zRight is escape-free */
2594 if( rawLeft && rawRight ){
2595 /* Simpliest case: Neither label contains escapes. A simple
2596 ** memcmp() is sufficient. */
2597 if( nLeft!=nRight ) return 0;
2598 return memcmp(zLeft, zRight, nLeft)==0;
2599 }else{
2600 return jsonLabelCompareEscaped(zLeft, nLeft, rawLeft,
2601 zRight, nRight, rawRight);
2606 ** Error returns from jsonLookupStep()
2608 #define JSON_LOOKUP_ERROR 0xffffffff
2609 #define JSON_LOOKUP_NOTFOUND 0xfffffffe
2610 #define JSON_LOOKUP_PATHERROR 0xfffffffd
2611 #define JSON_LOOKUP_ISERROR(x) ((x)>=JSON_LOOKUP_PATHERROR)
2613 /* Forward declaration */
2614 static u32 jsonLookupStep(JsonParse*,u32,const char*,u32);
2617 /* This helper routine for jsonLookupStep() populates pIns with
2618 ** binary data that is to be inserted into pParse.
2620 ** In the common case, pIns just points to pParse->aIns and pParse->nIns.
2621 ** But if the zPath of the original edit operation includes path elements
2622 ** that go deeper, additional substructure must be created.
2624 ** For example:
2626 ** json_insert('{}', '$.a.b.c', 123);
2628 ** The search stops at '$.a' But additional substructure must be
2629 ** created for the ".b.c" part of the patch so that the final result
2630 ** is: {"a":{"b":{"c"::123}}}. This routine populates pIns with
2631 ** the binary equivalent of {"b":{"c":123}} so that it can be inserted.
2633 ** The caller is responsible for resetting pIns when it has finished
2634 ** using the substructure.
2636 static u32 jsonCreateEditSubstructure(
2637 JsonParse *pParse, /* The original JSONB that is being edited */
2638 JsonParse *pIns, /* Populate this with the blob data to insert */
2639 const char *zTail /* Tail of the path that determins substructure */
2641 static const u8 emptyObject[] = { JSONB_ARRAY, JSONB_OBJECT };
2642 int rc;
2643 memset(pIns, 0, sizeof(*pIns));
2644 pIns->db = pParse->db;
2645 if( zTail[0]==0 ){
2646 /* No substructure. Just insert what is given in pParse. */
2647 pIns->aBlob = pParse->aIns;
2648 pIns->nBlob = pParse->nIns;
2649 rc = 0;
2650 }else{
2651 /* Construct the binary substructure */
2652 pIns->nBlob = 1;
2653 pIns->aBlob = (u8*)&emptyObject[zTail[0]=='.'];
2654 pIns->eEdit = pParse->eEdit;
2655 pIns->nIns = pParse->nIns;
2656 pIns->aIns = pParse->aIns;
2657 rc = jsonLookupStep(pIns, 0, zTail, 0);
2658 pParse->oom |= pIns->oom;
2660 return rc; /* Error code only */
2664 ** Search along zPath to find the Json element specified. Return an
2665 ** index into pParse->aBlob[] for the start of that element's value.
2667 ** If the value found by this routine is the value half of label/value pair
2668 ** within an object, then set pPath->iLabel to the start of the corresponding
2669 ** label, before returning.
2671 ** Return one of the JSON_LOOKUP error codes if problems are seen.
2673 ** This routine will also modify the blob. If pParse->eEdit is one of
2674 ** JEDIT_DEL, JEDIT_REPL, JEDIT_INS, or JEDIT_SET, then changes might be
2675 ** made to the selected value. If an edit is performed, then the return
2676 ** value does not necessarily point to the select element. If an edit
2677 ** is performed, the return value is only useful for detecting error
2678 ** conditions.
2680 static u32 jsonLookupStep(
2681 JsonParse *pParse, /* The JSON to search */
2682 u32 iRoot, /* Begin the search at this element of aBlob[] */
2683 const char *zPath, /* The path to search */
2684 u32 iLabel /* Label if iRoot is a value of in an object */
2686 u32 i, j, k, nKey, sz, n, iEnd, rc;
2687 const char *zKey;
2688 u8 x;
2690 if( zPath[0]==0 ){
2691 if( pParse->eEdit && jsonBlobMakeEditable(pParse, pParse->nIns) ){
2692 n = jsonbPayloadSize(pParse, iRoot, &sz);
2693 sz += n;
2694 if( pParse->eEdit==JEDIT_DEL ){
2695 if( iLabel>0 ){
2696 sz += iRoot - iLabel;
2697 iRoot = iLabel;
2699 jsonBlobEdit(pParse, iRoot, sz, 0, 0);
2700 }else if( pParse->eEdit==JEDIT_INS ){
2701 /* Already exists, so json_insert() is a no-op */
2702 }else{
2703 /* json_set() or json_replace() */
2704 jsonBlobEdit(pParse, iRoot, sz, pParse->aIns, pParse->nIns);
2707 pParse->iLabel = iLabel;
2708 return iRoot;
2710 if( zPath[0]=='.' ){
2711 int rawKey = 1;
2712 x = pParse->aBlob[iRoot];
2713 zPath++;
2714 if( zPath[0]=='"' ){
2715 zKey = zPath + 1;
2716 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
2717 nKey = i-1;
2718 if( zPath[i] ){
2719 i++;
2720 }else{
2721 return JSON_LOOKUP_PATHERROR;
2723 testcase( nKey==0 );
2724 rawKey = memchr(zKey, '\\', nKey)==0;
2725 }else{
2726 zKey = zPath;
2727 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
2728 nKey = i;
2729 if( nKey==0 ){
2730 return JSON_LOOKUP_PATHERROR;
2733 if( (x & 0x0f)!=JSONB_OBJECT ) return JSON_LOOKUP_NOTFOUND;
2734 n = jsonbPayloadSize(pParse, iRoot, &sz);
2735 j = iRoot + n; /* j is the index of a label */
2736 iEnd = j+sz;
2737 while( j<iEnd ){
2738 int rawLabel;
2739 const char *zLabel;
2740 x = pParse->aBlob[j] & 0x0f;
2741 if( x<JSONB_TEXT || x>JSONB_TEXTRAW ) return JSON_LOOKUP_ERROR;
2742 n = jsonbPayloadSize(pParse, j, &sz);
2743 if( n==0 ) return JSON_LOOKUP_ERROR;
2744 k = j+n; /* k is the index of the label text */
2745 if( k+sz>=iEnd ) return JSON_LOOKUP_ERROR;
2746 zLabel = (const char*)&pParse->aBlob[k];
2747 rawLabel = x==JSONB_TEXT || x==JSONB_TEXTRAW;
2748 if( jsonLabelCompare(zKey, nKey, rawKey, zLabel, sz, rawLabel) ){
2749 u32 v = k+sz; /* v is the index of the value */
2750 if( ((pParse->aBlob[v])&0x0f)>JSONB_OBJECT ) return JSON_LOOKUP_ERROR;
2751 n = jsonbPayloadSize(pParse, v, &sz);
2752 if( n==0 || v+n+sz>iEnd ) return JSON_LOOKUP_ERROR;
2753 assert( j>0 );
2754 rc = jsonLookupStep(pParse, v, &zPath[i], j);
2755 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
2756 return rc;
2758 j = k+sz;
2759 if( ((pParse->aBlob[j])&0x0f)>JSONB_OBJECT ) return JSON_LOOKUP_ERROR;
2760 n = jsonbPayloadSize(pParse, j, &sz);
2761 if( n==0 ) return JSON_LOOKUP_ERROR;
2762 j += n+sz;
2764 if( j>iEnd ) return JSON_LOOKUP_ERROR;
2765 if( pParse->eEdit>=JEDIT_INS ){
2766 u32 nIns; /* Total bytes to insert (label+value) */
2767 JsonParse v; /* BLOB encoding of the value to be inserted */
2768 JsonParse ix; /* Header of the label to be inserted */
2769 testcase( pParse->eEdit==JEDIT_INS );
2770 testcase( pParse->eEdit==JEDIT_SET );
2771 memset(&ix, 0, sizeof(ix));
2772 ix.db = pParse->db;
2773 jsonBlobAppendNode(&ix, rawKey?JSONB_TEXTRAW:JSONB_TEXT5, nKey, 0);
2774 pParse->oom |= ix.oom;
2775 rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i]);
2776 if( !JSON_LOOKUP_ISERROR(rc)
2777 && jsonBlobMakeEditable(pParse, ix.nBlob+nKey+v.nBlob)
2779 assert( !pParse->oom );
2780 nIns = ix.nBlob + nKey + v.nBlob;
2781 jsonBlobEdit(pParse, j, 0, 0, nIns);
2782 if( !pParse->oom ){
2783 assert( pParse->aBlob!=0 ); /* Because pParse->oom!=0 */
2784 assert( ix.aBlob!=0 ); /* Because pPasre->oom!=0 */
2785 memcpy(&pParse->aBlob[j], ix.aBlob, ix.nBlob);
2786 k = j + ix.nBlob;
2787 memcpy(&pParse->aBlob[k], zKey, nKey);
2788 k += nKey;
2789 memcpy(&pParse->aBlob[k], v.aBlob, v.nBlob);
2790 if( ALWAYS(pParse->delta) ) jsonAfterEditSizeAdjust(pParse, iRoot);
2793 jsonParseReset(&v);
2794 jsonParseReset(&ix);
2795 return rc;
2797 }else if( zPath[0]=='[' ){
2798 x = pParse->aBlob[iRoot] & 0x0f;
2799 if( x!=JSONB_ARRAY ) return JSON_LOOKUP_NOTFOUND;
2800 n = jsonbPayloadSize(pParse, iRoot, &sz);
2801 k = 0;
2802 i = 1;
2803 while( sqlite3Isdigit(zPath[i]) ){
2804 k = k*10 + zPath[i] - '0';
2805 i++;
2807 if( i<2 || zPath[i]!=']' ){
2808 if( zPath[1]=='#' ){
2809 k = jsonbArrayCount(pParse, iRoot);
2810 i = 2;
2811 if( zPath[2]=='-' && sqlite3Isdigit(zPath[3]) ){
2812 unsigned int nn = 0;
2813 i = 3;
2815 nn = nn*10 + zPath[i] - '0';
2816 i++;
2817 }while( sqlite3Isdigit(zPath[i]) );
2818 if( nn>k ) return JSON_LOOKUP_NOTFOUND;
2819 k -= nn;
2821 if( zPath[i]!=']' ){
2822 return JSON_LOOKUP_PATHERROR;
2824 }else{
2825 return JSON_LOOKUP_PATHERROR;
2828 j = iRoot+n;
2829 iEnd = j+sz;
2830 while( j<iEnd ){
2831 if( k==0 ){
2832 rc = jsonLookupStep(pParse, j, &zPath[i+1], 0);
2833 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
2834 return rc;
2836 k--;
2837 n = jsonbPayloadSize(pParse, j, &sz);
2838 if( n==0 ) return JSON_LOOKUP_ERROR;
2839 j += n+sz;
2841 if( j>iEnd ) return JSON_LOOKUP_ERROR;
2842 if( k>0 ) return JSON_LOOKUP_NOTFOUND;
2843 if( pParse->eEdit>=JEDIT_INS ){
2844 JsonParse v;
2845 testcase( pParse->eEdit==JEDIT_INS );
2846 testcase( pParse->eEdit==JEDIT_SET );
2847 rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i+1]);
2848 if( !JSON_LOOKUP_ISERROR(rc)
2849 && jsonBlobMakeEditable(pParse, v.nBlob)
2851 assert( !pParse->oom );
2852 jsonBlobEdit(pParse, j, 0, v.aBlob, v.nBlob);
2854 jsonParseReset(&v);
2855 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
2856 return rc;
2858 }else{
2859 return JSON_LOOKUP_PATHERROR;
2861 return JSON_LOOKUP_NOTFOUND;
2865 ** Convert a JSON BLOB into text and make that text the return value
2866 ** of an SQL function.
2868 static void jsonReturnTextJsonFromBlob(
2869 sqlite3_context *ctx,
2870 const u8 *aBlob,
2871 u32 nBlob
2873 JsonParse x;
2874 JsonString s;
2876 if( NEVER(aBlob==0) ) return;
2877 memset(&x, 0, sizeof(x));
2878 x.aBlob = (u8*)aBlob;
2879 x.nBlob = nBlob;
2880 jsonStringInit(&s, ctx);
2881 jsonTranslateBlobToText(&x, 0, &s);
2882 jsonReturnString(&s, 0, 0);
2887 ** Return the value of the BLOB node at index i.
2889 ** If the value is a primitive, return it as an SQL value.
2890 ** If the value is an array or object, return it as either
2891 ** JSON text or the BLOB encoding, depending on the JSON_B flag
2892 ** on the userdata.
2894 static void jsonReturnFromBlob(
2895 JsonParse *pParse, /* Complete JSON parse tree */
2896 u32 i, /* Index of the node */
2897 sqlite3_context *pCtx, /* Return value for this function */
2898 int textOnly /* return text JSON. Disregard user-data */
2900 u32 n, sz;
2901 int rc;
2902 sqlite3 *db = sqlite3_context_db_handle(pCtx);
2904 n = jsonbPayloadSize(pParse, i, &sz);
2905 if( n==0 ){
2906 sqlite3_result_error(pCtx, "malformed JSON", -1);
2907 return;
2909 switch( pParse->aBlob[i] & 0x0f ){
2910 case JSONB_NULL: {
2911 if( sz ) goto returnfromblob_malformed;
2912 sqlite3_result_null(pCtx);
2913 break;
2915 case JSONB_TRUE: {
2916 if( sz ) goto returnfromblob_malformed;
2917 sqlite3_result_int(pCtx, 1);
2918 break;
2920 case JSONB_FALSE: {
2921 if( sz ) goto returnfromblob_malformed;
2922 sqlite3_result_int(pCtx, 0);
2923 break;
2925 case JSONB_INT5:
2926 case JSONB_INT: {
2927 sqlite3_int64 iRes = 0;
2928 char *z;
2929 int bNeg = 0;
2930 char x;
2931 if( sz==0 ) goto returnfromblob_malformed;
2932 x = (char)pParse->aBlob[i+n];
2933 if( x=='-' ){
2934 if( sz<2 ) goto returnfromblob_malformed;
2935 n++;
2936 sz--;
2937 bNeg = 1;
2939 z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz);
2940 if( z==0 ) goto returnfromblob_oom;
2941 rc = sqlite3DecOrHexToI64(z, &iRes);
2942 sqlite3DbFree(db, z);
2943 if( rc==0 ){
2944 sqlite3_result_int64(pCtx, bNeg ? -iRes : iRes);
2945 }else if( rc==3 && bNeg ){
2946 sqlite3_result_int64(pCtx, SMALLEST_INT64);
2947 }else if( rc==1 ){
2948 goto returnfromblob_malformed;
2949 }else{
2950 if( bNeg ){ n--; sz++; }
2951 goto to_double;
2953 break;
2955 case JSONB_FLOAT5:
2956 case JSONB_FLOAT: {
2957 double r;
2958 char *z;
2959 if( sz==0 ) goto returnfromblob_malformed;
2960 to_double:
2961 z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz);
2962 if( z==0 ) goto returnfromblob_oom;
2963 rc = sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
2964 sqlite3DbFree(db, z);
2965 if( rc<=0 ) goto returnfromblob_malformed;
2966 sqlite3_result_double(pCtx, r);
2967 break;
2969 case JSONB_TEXTRAW:
2970 case JSONB_TEXT: {
2971 sqlite3_result_text(pCtx, (char*)&pParse->aBlob[i+n], sz,
2972 SQLITE_TRANSIENT);
2973 break;
2975 case JSONB_TEXT5:
2976 case JSONB_TEXTJ: {
2977 /* Translate JSON formatted string into raw text */
2978 u32 iIn, iOut;
2979 const char *z;
2980 char *zOut;
2981 u32 nOut = sz;
2982 z = (const char*)&pParse->aBlob[i+n];
2983 zOut = sqlite3DbMallocRaw(db, nOut+1);
2984 if( zOut==0 ) goto returnfromblob_oom;
2985 for(iIn=iOut=0; iIn<sz; iIn++){
2986 char c = z[iIn];
2987 if( c=='\\' ){
2988 u32 v;
2989 u32 szEscape = jsonUnescapeOneChar(&z[iIn], sz-iIn, &v);
2990 if( v<=0x7f ){
2991 zOut[iOut++] = (char)v;
2992 }else if( v<=0x7ff ){
2993 assert( szEscape>=2 );
2994 zOut[iOut++] = (char)(0xc0 | (v>>6));
2995 zOut[iOut++] = 0x80 | (v&0x3f);
2996 }else if( v<0x10000 ){
2997 assert( szEscape>=3 );
2998 zOut[iOut++] = 0xe0 | (v>>12);
2999 zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
3000 zOut[iOut++] = 0x80 | (v&0x3f);
3001 }else if( v==JSON_INVALID_CHAR ){
3002 /* Silently ignore illegal unicode */
3003 }else{
3004 assert( szEscape>=4 );
3005 zOut[iOut++] = 0xf0 | (v>>18);
3006 zOut[iOut++] = 0x80 | ((v>>12)&0x3f);
3007 zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
3008 zOut[iOut++] = 0x80 | (v&0x3f);
3010 iIn += szEscape - 1;
3011 }else{
3012 zOut[iOut++] = c;
3014 } /* end for() */
3015 assert( iOut<=nOut );
3016 zOut[iOut] = 0;
3017 sqlite3_result_text(pCtx, zOut, iOut, SQLITE_DYNAMIC);
3018 break;
3020 case JSONB_ARRAY:
3021 case JSONB_OBJECT: {
3022 int flags = textOnly ? 0 : SQLITE_PTR_TO_INT(sqlite3_user_data(pCtx));
3023 if( flags & JSON_BLOB ){
3024 sqlite3_result_blob(pCtx, &pParse->aBlob[i], sz+n, SQLITE_TRANSIENT);
3025 }else{
3026 jsonReturnTextJsonFromBlob(pCtx, &pParse->aBlob[i], sz+n);
3028 break;
3030 default: {
3031 goto returnfromblob_malformed;
3034 return;
3036 returnfromblob_oom:
3037 sqlite3_result_error_nomem(pCtx);
3038 return;
3040 returnfromblob_malformed:
3041 sqlite3_result_error(pCtx, "malformed JSON", -1);
3042 return;
3046 ** pArg is a function argument that might be an SQL value or a JSON
3047 ** value. Figure out what it is and encode it as a JSONB blob.
3048 ** Return the results in pParse.
3050 ** pParse is uninitialized upon entry. This routine will handle the
3051 ** initialization of pParse. The result will be contained in
3052 ** pParse->aBlob and pParse->nBlob. pParse->aBlob might be dynamically
3053 ** allocated (if pParse->nBlobAlloc is greater than zero) in which case
3054 ** the caller is responsible for freeing the space allocated to pParse->aBlob
3055 ** when it has finished with it. Or pParse->aBlob might be a static string
3056 ** or a value obtained from sqlite3_value_blob(pArg).
3058 ** If the argument is a BLOB that is clearly not a JSONB, then this
3059 ** function might set an error message in ctx and return non-zero.
3060 ** It might also set an error message and return non-zero on an OOM error.
3062 static int jsonFunctionArgToBlob(
3063 sqlite3_context *ctx,
3064 sqlite3_value *pArg,
3065 JsonParse *pParse
3067 int eType = sqlite3_value_type(pArg);
3068 static u8 aNull[] = { 0x00 };
3069 memset(pParse, 0, sizeof(pParse[0]));
3070 pParse->db = sqlite3_context_db_handle(ctx);
3071 switch( eType ){
3072 default: {
3073 pParse->aBlob = aNull;
3074 pParse->nBlob = 1;
3075 return 0;
3077 case SQLITE_BLOB: {
3078 if( jsonFuncArgMightBeBinary(pArg) ){
3079 pParse->aBlob = (u8*)sqlite3_value_blob(pArg);
3080 pParse->nBlob = sqlite3_value_bytes(pArg);
3081 }else{
3082 sqlite3_result_error(ctx, "JSON cannot hold BLOB values", -1);
3083 return 1;
3085 break;
3087 case SQLITE_TEXT: {
3088 const char *zJson = (const char*)sqlite3_value_text(pArg);
3089 int nJson = sqlite3_value_bytes(pArg);
3090 if( zJson==0 ) return 1;
3091 if( sqlite3_value_subtype(pArg)==JSON_SUBTYPE ){
3092 pParse->zJson = (char*)zJson;
3093 pParse->nJson = nJson;
3094 if( jsonConvertTextToBlob(pParse, ctx) ){
3095 sqlite3_result_error(ctx, "malformed JSON", -1);
3096 sqlite3DbFree(pParse->db, pParse->aBlob);
3097 memset(pParse, 0, sizeof(pParse[0]));
3098 return 1;
3100 }else{
3101 jsonBlobAppendNode(pParse, JSONB_TEXTRAW, nJson, zJson);
3103 break;
3105 case SQLITE_FLOAT: {
3106 double r = sqlite3_value_double(pArg);
3107 if( NEVER(sqlite3IsNaN(r)) ){
3108 jsonBlobAppendNode(pParse, JSONB_NULL, 0, 0);
3109 }else{
3110 int n = sqlite3_value_bytes(pArg);
3111 const char *z = (const char*)sqlite3_value_text(pArg);
3112 if( z==0 ) return 1;
3113 if( z[0]=='I' ){
3114 jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999");
3115 }else if( z[0]=='-' && z[1]=='I' ){
3116 jsonBlobAppendNode(pParse, JSONB_FLOAT, 6, "-9e999");
3117 }else{
3118 jsonBlobAppendNode(pParse, JSONB_FLOAT, n, z);
3121 break;
3123 case SQLITE_INTEGER: {
3124 int n = sqlite3_value_bytes(pArg);
3125 const char *z = (const char*)sqlite3_value_text(pArg);
3126 if( z==0 ) return 1;
3127 jsonBlobAppendNode(pParse, JSONB_INT, n, z);
3128 break;
3131 if( pParse->oom ){
3132 sqlite3_result_error_nomem(ctx);
3133 return 1;
3134 }else{
3135 return 0;
3140 ** Generate a bad path error.
3142 ** If ctx is not NULL then push the error message into ctx and return NULL.
3143 ** If ctx is NULL, then return the text of the error message.
3145 static char *jsonBadPathError(
3146 sqlite3_context *ctx, /* The function call containing the error */
3147 const char *zPath /* The path with the problem */
3149 char *zMsg = sqlite3_mprintf("bad JSON path: %Q", zPath);
3150 if( ctx==0 ) return zMsg;
3151 if( zMsg ){
3152 sqlite3_result_error(ctx, zMsg, -1);
3153 sqlite3_free(zMsg);
3154 }else{
3155 sqlite3_result_error_nomem(ctx);
3157 return 0;
3160 /* argv[0] is a BLOB that seems likely to be a JSONB. Subsequent
3161 ** arguments come in parse where each pair contains a JSON path and
3162 ** content to insert or set at that patch. Do the updates
3163 ** and return the result.
3165 ** The specific operation is determined by eEdit, which can be one
3166 ** of JEDIT_INS, JEDIT_REPL, or JEDIT_SET.
3168 static void jsonInsertIntoBlob(
3169 sqlite3_context *ctx,
3170 int argc,
3171 sqlite3_value **argv,
3172 int eEdit /* JEDIT_INS, JEDIT_REPL, or JEDIT_SET */
3174 int i;
3175 u32 rc = 0;
3176 const char *zPath = 0;
3177 int flgs;
3178 JsonParse *p;
3179 JsonParse ax;
3181 assert( (argc&1)==1 );
3182 flgs = argc==1 ? 0 : JSON_EDITABLE;
3183 p = jsonParseFuncArg(ctx, argv[0], flgs);
3184 if( p==0 ) return;
3185 for(i=1; i<argc-1; i+=2){
3186 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) continue;
3187 zPath = (const char*)sqlite3_value_text(argv[i]);
3188 if( zPath==0 ){
3189 sqlite3_result_error_nomem(ctx);
3190 jsonParseFree(p);
3191 return;
3193 if( zPath[0]!='$' ) goto jsonInsertIntoBlob_patherror;
3194 if( jsonFunctionArgToBlob(ctx, argv[i+1], &ax) ){
3195 jsonParseReset(&ax);
3196 jsonParseFree(p);
3197 return;
3199 if( zPath[1]==0 ){
3200 if( eEdit==JEDIT_REPL || eEdit==JEDIT_SET ){
3201 jsonBlobEdit(p, 0, p->nBlob, ax.aBlob, ax.nBlob);
3203 rc = 0;
3204 }else{
3205 p->eEdit = eEdit;
3206 p->nIns = ax.nBlob;
3207 p->aIns = ax.aBlob;
3208 p->delta = 0;
3209 rc = jsonLookupStep(p, 0, zPath+1, 0);
3211 jsonParseReset(&ax);
3212 if( rc==JSON_LOOKUP_NOTFOUND ) continue;
3213 if( JSON_LOOKUP_ISERROR(rc) ) goto jsonInsertIntoBlob_patherror;
3215 jsonReturnParse(ctx, p);
3216 jsonParseFree(p);
3217 return;
3219 jsonInsertIntoBlob_patherror:
3220 jsonParseFree(p);
3221 if( rc==JSON_LOOKUP_ERROR ){
3222 sqlite3_result_error(ctx, "malformed JSON", -1);
3223 }else{
3224 jsonBadPathError(ctx, zPath);
3226 return;
3230 ** If pArg is a blob that seems like a JSONB blob, then initialize
3231 ** p to point to that JSONB and return TRUE. If pArg does not seem like
3232 ** a JSONB blob, then return FALSE;
3234 ** This routine is only called if it is already known that pArg is a
3235 ** blob. The only open question is whether or not the blob appears
3236 ** to be a JSONB blob.
3238 static int jsonArgIsJsonb(sqlite3_value *pArg, JsonParse *p){
3239 u32 n, sz = 0;
3240 p->aBlob = (u8*)sqlite3_value_blob(pArg);
3241 p->nBlob = (u32)sqlite3_value_bytes(pArg);
3242 if( p->nBlob==0 ){
3243 p->aBlob = 0;
3244 return 0;
3246 if( NEVER(p->aBlob==0) ){
3247 return 0;
3249 if( (p->aBlob[0] & 0x0f)<=JSONB_OBJECT
3250 && (n = jsonbPayloadSize(p, 0, &sz))>0
3251 && sz+n==p->nBlob
3252 && ((p->aBlob[0] & 0x0f)>JSONB_FALSE || sz==0)
3254 return 1;
3256 p->aBlob = 0;
3257 p->nBlob = 0;
3258 return 0;
3262 ** Generate a JsonParse object, containing valid JSONB in aBlob and nBlob,
3263 ** from the SQL function argument pArg. Return a pointer to the new
3264 ** JsonParse object.
3266 ** Ownership of the new JsonParse object is passed to the caller. The
3267 ** caller should invoke jsonParseFree() on the return value when it
3268 ** has finished using it.
3270 ** If any errors are detected, an appropriate error messages is set
3271 ** using sqlite3_result_error() or the equivalent and this routine
3272 ** returns NULL. This routine also returns NULL if the pArg argument
3273 ** is an SQL NULL value, but no error message is set in that case. This
3274 ** is so that SQL functions that are given NULL arguments will return
3275 ** a NULL value.
3277 static JsonParse *jsonParseFuncArg(
3278 sqlite3_context *ctx,
3279 sqlite3_value *pArg,
3280 u32 flgs
3282 int eType; /* Datatype of pArg */
3283 JsonParse *p = 0; /* Value to be returned */
3284 JsonParse *pFromCache = 0; /* Value taken from cache */
3285 sqlite3 *db; /* The database connection */
3287 assert( ctx!=0 );
3288 eType = sqlite3_value_type(pArg);
3289 if( eType==SQLITE_NULL ){
3290 return 0;
3292 pFromCache = jsonCacheSearch(ctx, pArg);
3293 if( pFromCache ){
3294 pFromCache->nJPRef++;
3295 if( (flgs & JSON_EDITABLE)==0 ){
3296 return pFromCache;
3299 db = sqlite3_context_db_handle(ctx);
3300 rebuild_from_cache:
3301 p = sqlite3DbMallocZero(db, sizeof(*p));
3302 if( p==0 ) goto json_pfa_oom;
3303 memset(p, 0, sizeof(*p));
3304 p->db = db;
3305 p->nJPRef = 1;
3306 if( pFromCache!=0 ){
3307 u32 nBlob = pFromCache->nBlob;
3308 p->aBlob = sqlite3DbMallocRaw(db, nBlob);
3309 if( p->aBlob==0 ) goto json_pfa_oom;
3310 memcpy(p->aBlob, pFromCache->aBlob, nBlob);
3311 p->nBlobAlloc = p->nBlob = nBlob;
3312 p->hasNonstd = pFromCache->hasNonstd;
3313 jsonParseFree(pFromCache);
3314 return p;
3316 if( eType==SQLITE_BLOB ){
3317 if( jsonArgIsJsonb(pArg,p) ){
3318 if( (flgs & JSON_EDITABLE)!=0 && jsonBlobMakeEditable(p, 0)==0 ){
3319 goto json_pfa_oom;
3321 return p;
3323 /* If the blob is not valid JSONB, fall through into trying to cast
3324 ** the blob into text which is then interpreted as JSON. (tag-20240123-a)
3326 ** This goes against all historical documentation about how the SQLite
3327 ** JSON functions were suppose to work. From the beginning, blob was
3328 ** reserved for expansion and a blob value should have raised an error.
3329 ** But it did not, due to a bug. And many applications came to depend
3330 ** upon this buggy behavior, espeically when using the CLI and reading
3331 ** JSON text using readfile(), which returns a blob. For this reason
3332 ** we will continue to support the bug moving forward.
3333 ** See for example https://sqlite.org/forum/forumpost/012136abd5292b8d
3336 p->zJson = (char*)sqlite3_value_text(pArg);
3337 p->nJson = sqlite3_value_bytes(pArg);
3338 if( db->mallocFailed ) goto json_pfa_oom;
3339 if( p->nJson==0 ) goto json_pfa_malformed;
3340 assert( p->zJson!=0 );
3341 if( jsonConvertTextToBlob(p, (flgs & JSON_KEEPERROR) ? 0 : ctx) ){
3342 if( flgs & JSON_KEEPERROR ){
3343 p->nErr = 1;
3344 return p;
3345 }else{
3346 jsonParseFree(p);
3347 return 0;
3349 }else{
3350 int isRCStr = sqlite3ValueIsOfClass(pArg, sqlite3RCStrUnref);
3351 int rc;
3352 if( !isRCStr ){
3353 char *zNew = sqlite3RCStrNew( p->nJson );
3354 if( zNew==0 ) goto json_pfa_oom;
3355 memcpy(zNew, p->zJson, p->nJson);
3356 p->zJson = zNew;
3357 p->zJson[p->nJson] = 0;
3358 }else{
3359 sqlite3RCStrRef(p->zJson);
3361 p->bJsonIsRCStr = 1;
3362 rc = jsonCacheInsert(ctx, p);
3363 if( rc==SQLITE_NOMEM ) goto json_pfa_oom;
3364 if( flgs & JSON_EDITABLE ){
3365 pFromCache = p;
3366 p = 0;
3367 goto rebuild_from_cache;
3370 return p;
3372 json_pfa_malformed:
3373 if( flgs & JSON_KEEPERROR ){
3374 p->nErr = 1;
3375 return p;
3376 }else{
3377 jsonParseFree(p);
3378 sqlite3_result_error(ctx, "malformed JSON", -1);
3379 return 0;
3382 json_pfa_oom:
3383 jsonParseFree(pFromCache);
3384 jsonParseFree(p);
3385 sqlite3_result_error_nomem(ctx);
3386 return 0;
3390 ** Make the return value of a JSON function either the raw JSONB blob
3391 ** or make it JSON text, depending on whether the JSON_BLOB flag is
3392 ** set on the function.
3394 static void jsonReturnParse(
3395 sqlite3_context *ctx,
3396 JsonParse *p
3398 int flgs;
3399 if( p->oom ){
3400 sqlite3_result_error_nomem(ctx);
3401 return;
3403 flgs = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
3404 if( flgs & JSON_BLOB ){
3405 if( p->nBlobAlloc>0 && !p->bReadOnly ){
3406 sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_DYNAMIC);
3407 p->nBlobAlloc = 0;
3408 }else{
3409 sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_TRANSIENT);
3411 }else{
3412 JsonString s;
3413 jsonStringInit(&s, ctx);
3414 p->delta = 0;
3415 jsonTranslateBlobToText(p, 0, &s);
3416 jsonReturnString(&s, p, ctx);
3417 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3421 /****************************************************************************
3422 ** SQL functions used for testing and debugging
3423 ****************************************************************************/
3425 #if SQLITE_DEBUG
3427 ** Decode JSONB bytes in aBlob[] starting at iStart through but not
3428 ** including iEnd. Indent the
3429 ** content by nIndent spaces.
3431 static void jsonDebugPrintBlob(
3432 JsonParse *pParse, /* JSON content */
3433 u32 iStart, /* Start rendering here */
3434 u32 iEnd, /* Do not render this byte or any byte after this one */
3435 int nIndent, /* Indent by this many spaces */
3436 sqlite3_str *pOut /* Generate output into this sqlite3_str object */
3438 while( iStart<iEnd ){
3439 u32 i, n, nn, sz = 0;
3440 int showContent = 1;
3441 u8 x = pParse->aBlob[iStart] & 0x0f;
3442 u32 savedNBlob = pParse->nBlob;
3443 sqlite3_str_appendf(pOut, "%5d:%*s", iStart, nIndent, "");
3444 if( pParse->nBlobAlloc>pParse->nBlob ){
3445 pParse->nBlob = pParse->nBlobAlloc;
3447 nn = n = jsonbPayloadSize(pParse, iStart, &sz);
3448 if( nn==0 ) nn = 1;
3449 if( sz>0 && x<JSONB_ARRAY ){
3450 nn += sz;
3452 for(i=0; i<nn; i++){
3453 sqlite3_str_appendf(pOut, " %02x", pParse->aBlob[iStart+i]);
3455 if( n==0 ){
3456 sqlite3_str_appendf(pOut, " ERROR invalid node size\n");
3457 iStart = n==0 ? iStart+1 : iEnd;
3458 continue;
3460 pParse->nBlob = savedNBlob;
3461 if( iStart+n+sz>iEnd ){
3462 iEnd = iStart+n+sz;
3463 if( iEnd>pParse->nBlob ){
3464 if( pParse->nBlobAlloc>0 && iEnd>pParse->nBlobAlloc ){
3465 iEnd = pParse->nBlobAlloc;
3466 }else{
3467 iEnd = pParse->nBlob;
3471 sqlite3_str_appendall(pOut," <-- ");
3472 switch( x ){
3473 case JSONB_NULL: sqlite3_str_appendall(pOut,"null"); break;
3474 case JSONB_TRUE: sqlite3_str_appendall(pOut,"true"); break;
3475 case JSONB_FALSE: sqlite3_str_appendall(pOut,"false"); break;
3476 case JSONB_INT: sqlite3_str_appendall(pOut,"int"); break;
3477 case JSONB_INT5: sqlite3_str_appendall(pOut,"int5"); break;
3478 case JSONB_FLOAT: sqlite3_str_appendall(pOut,"float"); break;
3479 case JSONB_FLOAT5: sqlite3_str_appendall(pOut,"float5"); break;
3480 case JSONB_TEXT: sqlite3_str_appendall(pOut,"text"); break;
3481 case JSONB_TEXTJ: sqlite3_str_appendall(pOut,"textj"); break;
3482 case JSONB_TEXT5: sqlite3_str_appendall(pOut,"text5"); break;
3483 case JSONB_TEXTRAW: sqlite3_str_appendall(pOut,"textraw"); break;
3484 case JSONB_ARRAY: {
3485 sqlite3_str_appendf(pOut,"array, %u bytes\n", sz);
3486 jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut);
3487 showContent = 0;
3488 break;
3490 case JSONB_OBJECT: {
3491 sqlite3_str_appendf(pOut, "object, %u bytes\n", sz);
3492 jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut);
3493 showContent = 0;
3494 break;
3496 default: {
3497 sqlite3_str_appendall(pOut, "ERROR: unknown node type\n");
3498 showContent = 0;
3499 break;
3502 if( showContent ){
3503 if( sz==0 && x<=JSONB_FALSE ){
3504 sqlite3_str_append(pOut, "\n", 1);
3505 }else{
3506 u32 j;
3507 sqlite3_str_appendall(pOut, ": \"");
3508 for(j=iStart+n; j<iStart+n+sz; j++){
3509 u8 c = pParse->aBlob[j];
3510 if( c<0x20 || c>=0x7f ) c = '.';
3511 sqlite3_str_append(pOut, (char*)&c, 1);
3513 sqlite3_str_append(pOut, "\"\n", 2);
3516 iStart += n + sz;
3519 static void jsonShowParse(JsonParse *pParse){
3520 sqlite3_str out;
3521 char zBuf[1000];
3522 if( pParse==0 ){
3523 printf("NULL pointer\n");
3524 return;
3525 }else{
3526 printf("nBlobAlloc = %u\n", pParse->nBlobAlloc);
3527 printf("nBlob = %u\n", pParse->nBlob);
3528 printf("delta = %d\n", pParse->delta);
3529 if( pParse->nBlob==0 ) return;
3530 printf("content (bytes 0..%u):\n", pParse->nBlob-1);
3532 sqlite3StrAccumInit(&out, 0, zBuf, sizeof(zBuf), 1000000);
3533 jsonDebugPrintBlob(pParse, 0, pParse->nBlob, 0, &out);
3534 printf("%s", sqlite3_str_value(&out));
3535 sqlite3_str_reset(&out);
3537 #endif /* SQLITE_DEBUG */
3539 #ifdef SQLITE_DEBUG
3541 ** SQL function: json_parse(JSON)
3543 ** Parse JSON using jsonParseFuncArg(). Return text that is a
3544 ** human-readable dump of the binary JSONB for the input parameter.
3546 static void jsonParseFunc(
3547 sqlite3_context *ctx,
3548 int argc,
3549 sqlite3_value **argv
3551 JsonParse *p; /* The parse */
3552 sqlite3_str out;
3554 assert( argc>=1 );
3555 sqlite3StrAccumInit(&out, 0, 0, 0, 1000000);
3556 p = jsonParseFuncArg(ctx, argv[0], 0);
3557 if( p==0 ) return;
3558 if( argc==1 ){
3559 jsonDebugPrintBlob(p, 0, p->nBlob, 0, &out);
3560 sqlite3_result_text64(ctx, out.zText, out.nChar, SQLITE_DYNAMIC, SQLITE_UTF8);
3561 }else{
3562 jsonShowParse(p);
3564 jsonParseFree(p);
3566 #endif /* SQLITE_DEBUG */
3568 /****************************************************************************
3569 ** Scalar SQL function implementations
3570 ****************************************************************************/
3573 ** Implementation of the json_quote(VALUE) function. Return a JSON value
3574 ** corresponding to the SQL value input. Mostly this means putting
3575 ** double-quotes around strings and returning the unquoted string "null"
3576 ** when given a NULL input.
3578 static void jsonQuoteFunc(
3579 sqlite3_context *ctx,
3580 int argc,
3581 sqlite3_value **argv
3583 JsonString jx;
3584 UNUSED_PARAMETER(argc);
3586 jsonStringInit(&jx, ctx);
3587 jsonAppendSqlValue(&jx, argv[0]);
3588 jsonReturnString(&jx, 0, 0);
3589 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3593 ** Implementation of the json_array(VALUE,...) function. Return a JSON
3594 ** array that contains all values given in arguments. Or if any argument
3595 ** is a BLOB, throw an error.
3597 static void jsonArrayFunc(
3598 sqlite3_context *ctx,
3599 int argc,
3600 sqlite3_value **argv
3602 int i;
3603 JsonString jx;
3605 jsonStringInit(&jx, ctx);
3606 jsonAppendChar(&jx, '[');
3607 for(i=0; i<argc; i++){
3608 jsonAppendSeparator(&jx);
3609 jsonAppendSqlValue(&jx, argv[i]);
3611 jsonAppendChar(&jx, ']');
3612 jsonReturnString(&jx, 0, 0);
3613 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3617 ** json_array_length(JSON)
3618 ** json_array_length(JSON, PATH)
3620 ** Return the number of elements in the top-level JSON array.
3621 ** Return 0 if the input is not a well-formed JSON array.
3623 static void jsonArrayLengthFunc(
3624 sqlite3_context *ctx,
3625 int argc,
3626 sqlite3_value **argv
3628 JsonParse *p; /* The parse */
3629 sqlite3_int64 cnt = 0;
3630 u32 i;
3631 u8 eErr = 0;
3633 p = jsonParseFuncArg(ctx, argv[0], 0);
3634 if( p==0 ) return;
3635 if( argc==2 ){
3636 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
3637 if( zPath==0 ){
3638 jsonParseFree(p);
3639 return;
3641 i = jsonLookupStep(p, 0, zPath[0]=='$' ? zPath+1 : "@", 0);
3642 if( JSON_LOOKUP_ISERROR(i) ){
3643 if( i==JSON_LOOKUP_NOTFOUND ){
3644 /* no-op */
3645 }else if( i==JSON_LOOKUP_PATHERROR ){
3646 jsonBadPathError(ctx, zPath);
3647 }else{
3648 sqlite3_result_error(ctx, "malformed JSON", -1);
3650 eErr = 1;
3651 i = 0;
3653 }else{
3654 i = 0;
3656 if( (p->aBlob[i] & 0x0f)==JSONB_ARRAY ){
3657 cnt = jsonbArrayCount(p, i);
3659 if( !eErr ) sqlite3_result_int64(ctx, cnt);
3660 jsonParseFree(p);
3663 /* True if the string is all digits */
3664 static int jsonAllDigits(const char *z, int n){
3665 int i;
3666 for(i=0; i<n && sqlite3Isdigit(z[i]); i++){}
3667 return i==n;
3670 /* True if the string is all alphanumerics and underscores */
3671 static int jsonAllAlphanum(const char *z, int n){
3672 int i;
3673 for(i=0; i<n && (sqlite3Isalnum(z[i]) || z[i]=='_'); i++){}
3674 return i==n;
3678 ** json_extract(JSON, PATH, ...)
3679 ** "->"(JSON,PATH)
3680 ** "->>"(JSON,PATH)
3682 ** Return the element described by PATH. Return NULL if that PATH element
3683 ** is not found.
3685 ** If JSON_JSON is set or if more that one PATH argument is supplied then
3686 ** always return a JSON representation of the result. If JSON_SQL is set,
3687 ** then always return an SQL representation of the result. If neither flag
3688 ** is present and argc==2, then return JSON for objects and arrays and SQL
3689 ** for all other values.
3691 ** When multiple PATH arguments are supplied, the result is a JSON array
3692 ** containing the result of each PATH.
3694 ** Abbreviated JSON path expressions are allows if JSON_ABPATH, for
3695 ** compatibility with PG.
3697 static void jsonExtractFunc(
3698 sqlite3_context *ctx,
3699 int argc,
3700 sqlite3_value **argv
3702 JsonParse *p = 0; /* The parse */
3703 int flags; /* Flags associated with the function */
3704 int i; /* Loop counter */
3705 JsonString jx; /* String for array result */
3707 if( argc<2 ) return;
3708 p = jsonParseFuncArg(ctx, argv[0], 0);
3709 if( p==0 ) return;
3710 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
3711 jsonStringInit(&jx, ctx);
3712 if( argc>2 ){
3713 jsonAppendChar(&jx, '[');
3715 for(i=1; i<argc; i++){
3716 /* With a single PATH argument */
3717 const char *zPath = (const char*)sqlite3_value_text(argv[i]);
3718 int nPath;
3719 u32 j;
3720 if( zPath==0 ) goto json_extract_error;
3721 nPath = sqlite3Strlen30(zPath);
3722 if( zPath[0]=='$' ){
3723 j = jsonLookupStep(p, 0, zPath+1, 0);
3724 }else if( (flags & JSON_ABPATH) ){
3725 /* The -> and ->> operators accept abbreviated PATH arguments. This
3726 ** is mostly for compatibility with PostgreSQL, but also for
3727 ** convenience.
3729 ** NUMBER ==> $[NUMBER] // PG compatible
3730 ** LABEL ==> $.LABEL // PG compatible
3731 ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience
3733 jsonStringInit(&jx, ctx);
3734 if( jsonAllDigits(zPath, nPath) ){
3735 jsonAppendRawNZ(&jx, "[", 1);
3736 jsonAppendRaw(&jx, zPath, nPath);
3737 jsonAppendRawNZ(&jx, "]", 2);
3738 }else if( jsonAllAlphanum(zPath, nPath) ){
3739 jsonAppendRawNZ(&jx, ".", 1);
3740 jsonAppendRaw(&jx, zPath, nPath);
3741 }else if( zPath[0]=='[' && nPath>=3 && zPath[nPath-1]==']' ){
3742 jsonAppendRaw(&jx, zPath, nPath);
3743 }else{
3744 jsonAppendRawNZ(&jx, ".\"", 2);
3745 jsonAppendRaw(&jx, zPath, nPath);
3746 jsonAppendRawNZ(&jx, "\"", 1);
3748 jsonStringTerminate(&jx);
3749 j = jsonLookupStep(p, 0, jx.zBuf, 0);
3750 jsonStringReset(&jx);
3751 }else{
3752 jsonBadPathError(ctx, zPath);
3753 goto json_extract_error;
3755 if( j<p->nBlob ){
3756 if( argc==2 ){
3757 if( flags & JSON_JSON ){
3758 jsonStringInit(&jx, ctx);
3759 jsonTranslateBlobToText(p, j, &jx);
3760 jsonReturnString(&jx, 0, 0);
3761 jsonStringReset(&jx);
3762 assert( (flags & JSON_BLOB)==0 );
3763 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3764 }else{
3765 jsonReturnFromBlob(p, j, ctx, 0);
3766 if( (flags & (JSON_SQL|JSON_BLOB))==0
3767 && (p->aBlob[j]&0x0f)>=JSONB_ARRAY
3769 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3772 }else{
3773 jsonAppendSeparator(&jx);
3774 jsonTranslateBlobToText(p, j, &jx);
3776 }else if( j==JSON_LOOKUP_NOTFOUND ){
3777 if( argc==2 ){
3778 goto json_extract_error; /* Return NULL if not found */
3779 }else{
3780 jsonAppendSeparator(&jx);
3781 jsonAppendRawNZ(&jx, "null", 4);
3783 }else if( j==JSON_LOOKUP_ERROR ){
3784 sqlite3_result_error(ctx, "malformed JSON", -1);
3785 goto json_extract_error;
3786 }else{
3787 jsonBadPathError(ctx, zPath);
3788 goto json_extract_error;
3791 if( argc>2 ){
3792 jsonAppendChar(&jx, ']');
3793 jsonReturnString(&jx, 0, 0);
3794 if( (flags & JSON_BLOB)==0 ){
3795 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3798 json_extract_error:
3799 jsonStringReset(&jx);
3800 jsonParseFree(p);
3801 return;
3805 ** Return codes for jsonMergePatch()
3807 #define JSON_MERGE_OK 0 /* Success */
3808 #define JSON_MERGE_BADTARGET 1 /* Malformed TARGET blob */
3809 #define JSON_MERGE_BADPATCH 2 /* Malformed PATCH blob */
3810 #define JSON_MERGE_OOM 3 /* Out-of-memory condition */
3813 ** RFC-7396 MergePatch for two JSONB blobs.
3815 ** pTarget is the target. pPatch is the patch. The target is updated
3816 ** in place. The patch is read-only.
3818 ** The original RFC-7396 algorithm is this:
3820 ** define MergePatch(Target, Patch):
3821 ** if Patch is an Object:
3822 ** if Target is not an Object:
3823 ** Target = {} # Ignore the contents and set it to an empty Object
3824 ** for each Name/Value pair in Patch:
3825 ** if Value is null:
3826 ** if Name exists in Target:
3827 ** remove the Name/Value pair from Target
3828 ** else:
3829 ** Target[Name] = MergePatch(Target[Name], Value)
3830 ** return Target
3831 ** else:
3832 ** return Patch
3834 ** Here is an equivalent algorithm restructured to show the actual
3835 ** implementation:
3837 ** 01 define MergePatch(Target, Patch):
3838 ** 02 if Patch is not an Object:
3839 ** 03 return Patch
3840 ** 04 else: // if Patch is an Object
3841 ** 05 if Target is not an Object:
3842 ** 06 Target = {}
3843 ** 07 for each Name/Value pair in Patch:
3844 ** 08 if Name exists in Target:
3845 ** 09 if Value is null:
3846 ** 10 remove the Name/Value pair from Target
3847 ** 11 else
3848 ** 12 Target[name] = MergePatch(Target[Name], Value)
3849 ** 13 else if Value is not NULL:
3850 ** 14 if Value is not an Object:
3851 ** 15 Target[name] = Value
3852 ** 16 else:
3853 ** 17 Target[name] = MergePatch('{}',value)
3854 ** 18 return Target
3855 ** |
3856 ** ^---- Line numbers referenced in comments in the implementation
3858 static int jsonMergePatch(
3859 JsonParse *pTarget, /* The JSON parser that contains the TARGET */
3860 u32 iTarget, /* Index of TARGET in pTarget->aBlob[] */
3861 const JsonParse *pPatch, /* The PATCH */
3862 u32 iPatch /* Index of PATCH in pPatch->aBlob[] */
3864 u8 x; /* Type of a single node */
3865 u32 n, sz=0; /* Return values from jsonbPayloadSize() */
3866 u32 iTCursor; /* Cursor position while scanning the target object */
3867 u32 iTStart; /* First label in the target object */
3868 u32 iTEndBE; /* Original first byte past end of target, before edit */
3869 u32 iTEnd; /* Current first byte past end of target */
3870 u8 eTLabel; /* Node type of the target label */
3871 u32 iTLabel = 0; /* Index of the label */
3872 u32 nTLabel = 0; /* Header size in bytes for the target label */
3873 u32 szTLabel = 0; /* Size of the target label payload */
3874 u32 iTValue = 0; /* Index of the target value */
3875 u32 nTValue = 0; /* Header size of the target value */
3876 u32 szTValue = 0; /* Payload size for the target value */
3878 u32 iPCursor; /* Cursor position while scanning the patch */
3879 u32 iPEnd; /* First byte past the end of the patch */
3880 u8 ePLabel; /* Node type of the patch label */
3881 u32 iPLabel; /* Start of patch label */
3882 u32 nPLabel; /* Size of header on the patch label */
3883 u32 szPLabel; /* Payload size of the patch label */
3884 u32 iPValue; /* Start of patch value */
3885 u32 nPValue; /* Header size for the patch value */
3886 u32 szPValue; /* Payload size of the patch value */
3888 assert( iTarget>=0 && iTarget<pTarget->nBlob );
3889 assert( iPatch>=0 && iPatch<pPatch->nBlob );
3890 x = pPatch->aBlob[iPatch] & 0x0f;
3891 if( x!=JSONB_OBJECT ){ /* Algorithm line 02 */
3892 u32 szPatch; /* Total size of the patch, header+payload */
3893 u32 szTarget; /* Total size of the target, header+payload */
3894 n = jsonbPayloadSize(pPatch, iPatch, &sz);
3895 szPatch = n+sz;
3896 sz = 0;
3897 n = jsonbPayloadSize(pTarget, iTarget, &sz);
3898 szTarget = n+sz;
3899 jsonBlobEdit(pTarget, iTarget, szTarget, pPatch->aBlob+iPatch, szPatch);
3900 return pTarget->oom ? JSON_MERGE_OOM : JSON_MERGE_OK; /* Line 03 */
3902 x = pTarget->aBlob[iTarget] & 0x0f;
3903 if( x!=JSONB_OBJECT ){ /* Algorithm line 05 */
3904 n = jsonbPayloadSize(pTarget, iTarget, &sz);
3905 jsonBlobEdit(pTarget, iTarget+n, sz, 0, 0);
3906 x = pTarget->aBlob[iTarget];
3907 pTarget->aBlob[iTarget] = (x & 0xf0) | JSONB_OBJECT;
3909 n = jsonbPayloadSize(pPatch, iPatch, &sz);
3910 if( NEVER(n==0) ) return JSON_MERGE_BADPATCH;
3911 iPCursor = iPatch+n;
3912 iPEnd = iPCursor+sz;
3913 n = jsonbPayloadSize(pTarget, iTarget, &sz);
3914 if( NEVER(n==0) ) return JSON_MERGE_BADTARGET;
3915 iTStart = iTarget+n;
3916 iTEndBE = iTStart+sz;
3918 while( iPCursor<iPEnd ){ /* Algorithm line 07 */
3919 iPLabel = iPCursor;
3920 ePLabel = pPatch->aBlob[iPCursor] & 0x0f;
3921 if( ePLabel<JSONB_TEXT || ePLabel>JSONB_TEXTRAW ){
3922 return JSON_MERGE_BADPATCH;
3924 nPLabel = jsonbPayloadSize(pPatch, iPCursor, &szPLabel);
3925 if( nPLabel==0 ) return JSON_MERGE_BADPATCH;
3926 iPValue = iPCursor + nPLabel + szPLabel;
3927 if( iPValue>=iPEnd ) return JSON_MERGE_BADPATCH;
3928 nPValue = jsonbPayloadSize(pPatch, iPValue, &szPValue);
3929 if( nPValue==0 ) return JSON_MERGE_BADPATCH;
3930 iPCursor = iPValue + nPValue + szPValue;
3931 if( iPCursor>iPEnd ) return JSON_MERGE_BADPATCH;
3933 iTCursor = iTStart;
3934 iTEnd = iTEndBE + pTarget->delta;
3935 while( iTCursor<iTEnd ){
3936 int isEqual; /* true if the patch and target labels match */
3937 iTLabel = iTCursor;
3938 eTLabel = pTarget->aBlob[iTCursor] & 0x0f;
3939 if( eTLabel<JSONB_TEXT || eTLabel>JSONB_TEXTRAW ){
3940 return JSON_MERGE_BADTARGET;
3942 nTLabel = jsonbPayloadSize(pTarget, iTCursor, &szTLabel);
3943 if( nTLabel==0 ) return JSON_MERGE_BADTARGET;
3944 iTValue = iTLabel + nTLabel + szTLabel;
3945 if( iTValue>=iTEnd ) return JSON_MERGE_BADTARGET;
3946 nTValue = jsonbPayloadSize(pTarget, iTValue, &szTValue);
3947 if( nTValue==0 ) return JSON_MERGE_BADTARGET;
3948 if( iTValue + nTValue + szTValue > iTEnd ) return JSON_MERGE_BADTARGET;
3949 isEqual = jsonLabelCompare(
3950 (const char*)&pPatch->aBlob[iPLabel+nPLabel],
3951 szPLabel,
3952 (ePLabel==JSONB_TEXT || ePLabel==JSONB_TEXTRAW),
3953 (const char*)&pTarget->aBlob[iTLabel+nTLabel],
3954 szTLabel,
3955 (eTLabel==JSONB_TEXT || eTLabel==JSONB_TEXTRAW));
3956 if( isEqual ) break;
3957 iTCursor = iTValue + nTValue + szTValue;
3959 x = pPatch->aBlob[iPValue] & 0x0f;
3960 if( iTCursor<iTEnd ){
3961 /* A match was found. Algorithm line 08 */
3962 if( x==0 ){
3963 /* Patch value is NULL. Algorithm line 09 */
3964 jsonBlobEdit(pTarget, iTLabel, nTLabel+szTLabel+nTValue+szTValue, 0,0);
3965 /* vvvvvv----- No OOM on a delete-only edit */
3966 if( NEVER(pTarget->oom) ) return JSON_MERGE_OOM;
3967 }else{
3968 /* Algorithm line 12 */
3969 int rc, savedDelta = pTarget->delta;
3970 pTarget->delta = 0;
3971 rc = jsonMergePatch(pTarget, iTValue, pPatch, iPValue);
3972 if( rc ) return rc;
3973 pTarget->delta += savedDelta;
3975 }else if( x>0 ){ /* Algorithm line 13 */
3976 /* No match and patch value is not NULL */
3977 u32 szNew = szPLabel+nPLabel;
3978 if( (pPatch->aBlob[iPValue] & 0x0f)!=JSONB_OBJECT ){ /* Line 14 */
3979 jsonBlobEdit(pTarget, iTEnd, 0, 0, szPValue+nPValue+szNew);
3980 if( pTarget->oom ) return JSON_MERGE_OOM;
3981 memcpy(&pTarget->aBlob[iTEnd], &pPatch->aBlob[iPLabel], szNew);
3982 memcpy(&pTarget->aBlob[iTEnd+szNew],
3983 &pPatch->aBlob[iPValue], szPValue+nPValue);
3984 }else{
3985 int rc, savedDelta;
3986 jsonBlobEdit(pTarget, iTEnd, 0, 0, szNew+1);
3987 if( pTarget->oom ) return JSON_MERGE_OOM;
3988 memcpy(&pTarget->aBlob[iTEnd], &pPatch->aBlob[iPLabel], szNew);
3989 pTarget->aBlob[iTEnd+szNew] = 0x00;
3990 savedDelta = pTarget->delta;
3991 pTarget->delta = 0;
3992 rc = jsonMergePatch(pTarget, iTEnd+szNew,pPatch,iPValue);
3993 if( rc ) return rc;
3994 pTarget->delta += savedDelta;
3998 if( pTarget->delta ) jsonAfterEditSizeAdjust(pTarget, iTarget);
3999 return pTarget->oom ? JSON_MERGE_OOM : JSON_MERGE_OK;
4004 ** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
4005 ** object that is the result of running the RFC 7396 MergePatch() algorithm
4006 ** on the two arguments.
4008 static void jsonPatchFunc(
4009 sqlite3_context *ctx,
4010 int argc,
4011 sqlite3_value **argv
4013 JsonParse *pTarget; /* The TARGET */
4014 JsonParse *pPatch; /* The PATCH */
4015 int rc; /* Result code */
4017 UNUSED_PARAMETER(argc);
4018 assert( argc==2 );
4019 pTarget = jsonParseFuncArg(ctx, argv[0], JSON_EDITABLE);
4020 if( pTarget==0 ) return;
4021 pPatch = jsonParseFuncArg(ctx, argv[1], 0);
4022 if( pPatch ){
4023 rc = jsonMergePatch(pTarget, 0, pPatch, 0);
4024 if( rc==JSON_MERGE_OK ){
4025 jsonReturnParse(ctx, pTarget);
4026 }else if( rc==JSON_MERGE_OOM ){
4027 sqlite3_result_error_nomem(ctx);
4028 }else{
4029 sqlite3_result_error(ctx, "malformed JSON", -1);
4031 jsonParseFree(pPatch);
4033 jsonParseFree(pTarget);
4038 ** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
4039 ** object that contains all name/value given in arguments. Or if any name
4040 ** is not a string or if any value is a BLOB, throw an error.
4042 static void jsonObjectFunc(
4043 sqlite3_context *ctx,
4044 int argc,
4045 sqlite3_value **argv
4047 int i;
4048 JsonString jx;
4049 const char *z;
4050 u32 n;
4052 if( argc&1 ){
4053 sqlite3_result_error(ctx, "json_object() requires an even number "
4054 "of arguments", -1);
4055 return;
4057 jsonStringInit(&jx, ctx);
4058 jsonAppendChar(&jx, '{');
4059 for(i=0; i<argc; i+=2){
4060 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
4061 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
4062 jsonStringReset(&jx);
4063 return;
4065 jsonAppendSeparator(&jx);
4066 z = (const char*)sqlite3_value_text(argv[i]);
4067 n = sqlite3_value_bytes(argv[i]);
4068 jsonAppendString(&jx, z, n);
4069 jsonAppendChar(&jx, ':');
4070 jsonAppendSqlValue(&jx, argv[i+1]);
4072 jsonAppendChar(&jx, '}');
4073 jsonReturnString(&jx, 0, 0);
4074 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
4079 ** json_remove(JSON, PATH, ...)
4081 ** Remove the named elements from JSON and return the result. malformed
4082 ** JSON or PATH arguments result in an error.
4084 static void jsonRemoveFunc(
4085 sqlite3_context *ctx,
4086 int argc,
4087 sqlite3_value **argv
4089 JsonParse *p; /* The parse */
4090 const char *zPath = 0; /* Path of element to be removed */
4091 int i; /* Loop counter */
4092 u32 rc; /* Subroutine return code */
4094 if( argc<1 ) return;
4095 p = jsonParseFuncArg(ctx, argv[0], argc>1 ? JSON_EDITABLE : 0);
4096 if( p==0 ) return;
4097 for(i=1; i<argc; i++){
4098 zPath = (const char*)sqlite3_value_text(argv[i]);
4099 if( zPath==0 ){
4100 goto json_remove_done;
4102 if( zPath[0]!='$' ){
4103 goto json_remove_patherror;
4105 if( zPath[1]==0 ){
4106 /* json_remove(j,'$') returns NULL */
4107 goto json_remove_done;
4109 p->eEdit = JEDIT_DEL;
4110 p->delta = 0;
4111 rc = jsonLookupStep(p, 0, zPath+1, 0);
4112 if( JSON_LOOKUP_ISERROR(rc) ){
4113 if( rc==JSON_LOOKUP_NOTFOUND ){
4114 continue; /* No-op */
4115 }else if( rc==JSON_LOOKUP_PATHERROR ){
4116 jsonBadPathError(ctx, zPath);
4117 }else{
4118 sqlite3_result_error(ctx, "malformed JSON", -1);
4120 goto json_remove_done;
4123 jsonReturnParse(ctx, p);
4124 jsonParseFree(p);
4125 return;
4127 json_remove_patherror:
4128 jsonBadPathError(ctx, zPath);
4130 json_remove_done:
4131 jsonParseFree(p);
4132 return;
4136 ** json_replace(JSON, PATH, VALUE, ...)
4138 ** Replace the value at PATH with VALUE. If PATH does not already exist,
4139 ** this routine is a no-op. If JSON or PATH is malformed, throw an error.
4141 static void jsonReplaceFunc(
4142 sqlite3_context *ctx,
4143 int argc,
4144 sqlite3_value **argv
4146 if( argc<1 ) return;
4147 if( (argc&1)==0 ) {
4148 jsonWrongNumArgs(ctx, "replace");
4149 return;
4151 jsonInsertIntoBlob(ctx, argc, argv, JEDIT_REPL);
4156 ** json_set(JSON, PATH, VALUE, ...)
4158 ** Set the value at PATH to VALUE. Create the PATH if it does not already
4159 ** exist. Overwrite existing values that do exist.
4160 ** If JSON or PATH is malformed, throw an error.
4162 ** json_insert(JSON, PATH, VALUE, ...)
4164 ** Create PATH and initialize it to VALUE. If PATH already exists, this
4165 ** routine is a no-op. If JSON or PATH is malformed, throw an error.
4167 static void jsonSetFunc(
4168 sqlite3_context *ctx,
4169 int argc,
4170 sqlite3_value **argv
4173 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
4174 int bIsSet = (flags&JSON_ISSET)!=0;
4176 if( argc<1 ) return;
4177 if( (argc&1)==0 ) {
4178 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
4179 return;
4181 jsonInsertIntoBlob(ctx, argc, argv, bIsSet ? JEDIT_SET : JEDIT_INS);
4185 ** json_type(JSON)
4186 ** json_type(JSON, PATH)
4188 ** Return the top-level "type" of a JSON string. json_type() raises an
4189 ** error if either the JSON or PATH inputs are not well-formed.
4191 static void jsonTypeFunc(
4192 sqlite3_context *ctx,
4193 int argc,
4194 sqlite3_value **argv
4196 JsonParse *p; /* The parse */
4197 const char *zPath = 0;
4198 u32 i;
4200 p = jsonParseFuncArg(ctx, argv[0], 0);
4201 if( p==0 ) return;
4202 if( argc==2 ){
4203 zPath = (const char*)sqlite3_value_text(argv[1]);
4204 if( zPath==0 ) goto json_type_done;
4205 if( zPath[0]!='$' ){
4206 jsonBadPathError(ctx, zPath);
4207 goto json_type_done;
4209 i = jsonLookupStep(p, 0, zPath+1, 0);
4210 if( JSON_LOOKUP_ISERROR(i) ){
4211 if( i==JSON_LOOKUP_NOTFOUND ){
4212 /* no-op */
4213 }else if( i==JSON_LOOKUP_PATHERROR ){
4214 jsonBadPathError(ctx, zPath);
4215 }else{
4216 sqlite3_result_error(ctx, "malformed JSON", -1);
4218 goto json_type_done;
4220 }else{
4221 i = 0;
4223 sqlite3_result_text(ctx, jsonbType[p->aBlob[i]&0x0f], -1, SQLITE_STATIC);
4224 json_type_done:
4225 jsonParseFree(p);
4229 ** json_valid(JSON)
4230 ** json_valid(JSON, FLAGS)
4232 ** Check the JSON argument to see if it is well-formed. The FLAGS argument
4233 ** encodes the various constraints on what is meant by "well-formed":
4235 ** 0x01 Canonical RFC-8259 JSON text
4236 ** 0x02 JSON text with optional JSON-5 extensions
4237 ** 0x04 Superficially appears to be JSONB
4238 ** 0x08 Strictly well-formed JSONB
4240 ** If the FLAGS argument is omitted, it defaults to 1. Useful values for
4241 ** FLAGS include:
4243 ** 1 Strict canonical JSON text
4244 ** 2 JSON text perhaps with JSON-5 extensions
4245 ** 4 Superficially appears to be JSONB
4246 ** 5 Canonical JSON text or superficial JSONB
4247 ** 6 JSON-5 text or superficial JSONB
4248 ** 8 Strict JSONB
4249 ** 9 Canonical JSON text or strict JSONB
4250 ** 10 JSON-5 text or strict JSONB
4252 ** Other flag combinations are redundant. For example, every canonical
4253 ** JSON text is also well-formed JSON-5 text, so FLAG values 2 and 3
4254 ** are the same. Similarly, any input that passes a strict JSONB validation
4255 ** will also pass the superficial validation so 12 through 15 are the same
4256 ** as 8 through 11 respectively.
4258 ** This routine runs in linear time to validate text and when doing strict
4259 ** JSONB validation. Superficial JSONB validation is constant time,
4260 ** assuming the BLOB is already in memory. The performance advantage
4261 ** of superficial JSONB validation is why that option is provided.
4262 ** Application developers can choose to do fast superficial validation or
4263 ** slower strict validation, according to their specific needs.
4265 ** Only the lower four bits of the FLAGS argument are currently used.
4266 ** Higher bits are reserved for future expansion. To facilitate
4267 ** compatibility, the current implementation raises an error if any bit
4268 ** in FLAGS is set other than the lower four bits.
4270 ** The original circa 2015 implementation of the JSON routines in
4271 ** SQLite only supported canonical RFC-8259 JSON text and the json_valid()
4272 ** function only accepted one argument. That is why the default value
4273 ** for the FLAGS argument is 1, since FLAGS=1 causes this routine to only
4274 ** recognize canonical RFC-8259 JSON text as valid. The extra FLAGS
4275 ** argument was added when the JSON routines were extended to support
4276 ** JSON5-like extensions and binary JSONB stored in BLOBs.
4278 ** Return Values:
4280 ** * Raise an error if FLAGS is outside the range of 1 to 15.
4281 ** * Return NULL if the input is NULL
4282 ** * Return 1 if the input is well-formed.
4283 ** * Return 0 if the input is not well-formed.
4285 static void jsonValidFunc(
4286 sqlite3_context *ctx,
4287 int argc,
4288 sqlite3_value **argv
4290 JsonParse *p; /* The parse */
4291 u8 flags = 1;
4292 u8 res = 0;
4293 if( argc==2 ){
4294 i64 f = sqlite3_value_int64(argv[1]);
4295 if( f<1 || f>15 ){
4296 sqlite3_result_error(ctx, "FLAGS parameter to json_valid() must be"
4297 " between 1 and 15", -1);
4298 return;
4300 flags = f & 0x0f;
4302 switch( sqlite3_value_type(argv[0]) ){
4303 case SQLITE_NULL: {
4304 #ifdef SQLITE_LEGACY_JSON_VALID
4305 /* Incorrect legacy behavior was to return FALSE for a NULL input */
4306 sqlite3_result_int(ctx, 0);
4307 #endif
4308 return;
4310 case SQLITE_BLOB: {
4311 if( jsonFuncArgMightBeBinary(argv[0]) ){
4312 if( flags & 0x04 ){
4313 /* Superficial checking only - accomplished by the
4314 ** jsonFuncArgMightBeBinary() call above. */
4315 res = 1;
4316 }else if( flags & 0x08 ){
4317 /* Strict checking. Check by translating BLOB->TEXT->BLOB. If
4318 ** no errors occur, call that a "strict check". */
4319 JsonParse px;
4320 u32 iErr;
4321 memset(&px, 0, sizeof(px));
4322 px.aBlob = (u8*)sqlite3_value_blob(argv[0]);
4323 px.nBlob = sqlite3_value_bytes(argv[0]);
4324 iErr = jsonbValidityCheck(&px, 0, px.nBlob, 1);
4325 res = iErr==0;
4327 break;
4329 /* Fall through into interpreting the input as text. See note
4330 ** above at tag-20240123-a. */
4331 /* no break */ deliberate_fall_through
4333 default: {
4334 JsonParse px;
4335 if( (flags & 0x3)==0 ) break;
4336 memset(&px, 0, sizeof(px));
4338 p = jsonParseFuncArg(ctx, argv[0], JSON_KEEPERROR);
4339 if( p ){
4340 if( p->oom ){
4341 sqlite3_result_error_nomem(ctx);
4342 }else if( p->nErr ){
4343 /* no-op */
4344 }else if( (flags & 0x02)!=0 || p->hasNonstd==0 ){
4345 res = 1;
4347 jsonParseFree(p);
4348 }else{
4349 sqlite3_result_error_nomem(ctx);
4351 break;
4354 sqlite3_result_int(ctx, res);
4358 ** json_error_position(JSON)
4360 ** If the argument is NULL, return NULL
4362 ** If the argument is BLOB, do a full validity check and return non-zero
4363 ** if the check fails. The return value is the approximate 1-based offset
4364 ** to the byte of the element that contains the first error.
4366 ** Otherwise interpret the argument is TEXT (even if it is numeric) and
4367 ** return the 1-based character position for where the parser first recognized
4368 ** that the input was not valid JSON, or return 0 if the input text looks
4369 ** ok. JSON-5 extensions are accepted.
4371 static void jsonErrorFunc(
4372 sqlite3_context *ctx,
4373 int argc,
4374 sqlite3_value **argv
4376 i64 iErrPos = 0; /* Error position to be returned */
4377 JsonParse s;
4379 assert( argc==1 );
4380 UNUSED_PARAMETER(argc);
4381 memset(&s, 0, sizeof(s));
4382 s.db = sqlite3_context_db_handle(ctx);
4383 if( jsonFuncArgMightBeBinary(argv[0]) ){
4384 s.aBlob = (u8*)sqlite3_value_blob(argv[0]);
4385 s.nBlob = sqlite3_value_bytes(argv[0]);
4386 iErrPos = (i64)jsonbValidityCheck(&s, 0, s.nBlob, 1);
4387 }else{
4388 s.zJson = (char*)sqlite3_value_text(argv[0]);
4389 if( s.zJson==0 ) return; /* NULL input or OOM */
4390 s.nJson = sqlite3_value_bytes(argv[0]);
4391 if( jsonConvertTextToBlob(&s,0) ){
4392 if( s.oom ){
4393 iErrPos = -1;
4394 }else{
4395 /* Convert byte-offset s.iErr into a character offset */
4396 u32 k;
4397 assert( s.zJson!=0 ); /* Because s.oom is false */
4398 for(k=0; k<s.iErr && ALWAYS(s.zJson[k]); k++){
4399 if( (s.zJson[k] & 0xc0)!=0x80 ) iErrPos++;
4401 iErrPos++;
4405 jsonParseReset(&s);
4406 if( iErrPos<0 ){
4407 sqlite3_result_error_nomem(ctx);
4408 }else{
4409 sqlite3_result_int64(ctx, iErrPos);
4413 /****************************************************************************
4414 ** Aggregate SQL function implementations
4415 ****************************************************************************/
4417 ** json_group_array(VALUE)
4419 ** Return a JSON array composed of all values in the aggregate.
4421 static void jsonArrayStep(
4422 sqlite3_context *ctx,
4423 int argc,
4424 sqlite3_value **argv
4426 JsonString *pStr;
4427 UNUSED_PARAMETER(argc);
4428 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
4429 if( pStr ){
4430 if( pStr->zBuf==0 ){
4431 jsonStringInit(pStr, ctx);
4432 jsonAppendChar(pStr, '[');
4433 }else if( pStr->nUsed>1 ){
4434 jsonAppendChar(pStr, ',');
4436 pStr->pCtx = ctx;
4437 jsonAppendSqlValue(pStr, argv[0]);
4440 static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
4441 JsonString *pStr;
4442 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
4443 if( pStr ){
4444 int flags;
4445 pStr->pCtx = ctx;
4446 jsonAppendChar(pStr, ']');
4447 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
4448 if( pStr->eErr ){
4449 jsonReturnString(pStr, 0, 0);
4450 return;
4451 }else if( flags & JSON_BLOB ){
4452 jsonReturnStringAsBlob(pStr);
4453 if( isFinal ){
4454 if( !pStr->bStatic ) sqlite3RCStrUnref(pStr->zBuf);
4455 }else{
4456 jsonStringTrimOneChar(pStr);
4458 return;
4459 }else if( isFinal ){
4460 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
4461 pStr->bStatic ? SQLITE_TRANSIENT :
4462 sqlite3RCStrUnref);
4463 pStr->bStatic = 1;
4464 }else{
4465 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
4466 jsonStringTrimOneChar(pStr);
4468 }else{
4469 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
4471 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
4473 static void jsonArrayValue(sqlite3_context *ctx){
4474 jsonArrayCompute(ctx, 0);
4476 static void jsonArrayFinal(sqlite3_context *ctx){
4477 jsonArrayCompute(ctx, 1);
4480 #ifndef SQLITE_OMIT_WINDOWFUNC
4482 ** This method works for both json_group_array() and json_group_object().
4483 ** It works by removing the first element of the group by searching forward
4484 ** to the first comma (",") that is not within a string and deleting all
4485 ** text through that comma.
4487 static void jsonGroupInverse(
4488 sqlite3_context *ctx,
4489 int argc,
4490 sqlite3_value **argv
4492 unsigned int i;
4493 int inStr = 0;
4494 int nNest = 0;
4495 char *z;
4496 char c;
4497 JsonString *pStr;
4498 UNUSED_PARAMETER(argc);
4499 UNUSED_PARAMETER(argv);
4500 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
4501 #ifdef NEVER
4502 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
4503 ** always have been called to initialize it */
4504 if( NEVER(!pStr) ) return;
4505 #endif
4506 z = pStr->zBuf;
4507 for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){
4508 if( c=='"' ){
4509 inStr = !inStr;
4510 }else if( c=='\\' ){
4511 i++;
4512 }else if( !inStr ){
4513 if( c=='{' || c=='[' ) nNest++;
4514 if( c=='}' || c==']' ) nNest--;
4517 if( i<pStr->nUsed ){
4518 pStr->nUsed -= i;
4519 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
4520 z[pStr->nUsed] = 0;
4521 }else{
4522 pStr->nUsed = 1;
4525 #else
4526 # define jsonGroupInverse 0
4527 #endif
4531 ** json_group_obj(NAME,VALUE)
4533 ** Return a JSON object composed of all names and values in the aggregate.
4535 static void jsonObjectStep(
4536 sqlite3_context *ctx,
4537 int argc,
4538 sqlite3_value **argv
4540 JsonString *pStr;
4541 const char *z;
4542 u32 n;
4543 UNUSED_PARAMETER(argc);
4544 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
4545 if( pStr ){
4546 if( pStr->zBuf==0 ){
4547 jsonStringInit(pStr, ctx);
4548 jsonAppendChar(pStr, '{');
4549 }else if( pStr->nUsed>1 ){
4550 jsonAppendChar(pStr, ',');
4552 pStr->pCtx = ctx;
4553 z = (const char*)sqlite3_value_text(argv[0]);
4554 n = sqlite3Strlen30(z);
4555 jsonAppendString(pStr, z, n);
4556 jsonAppendChar(pStr, ':');
4557 jsonAppendSqlValue(pStr, argv[1]);
4560 static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
4561 JsonString *pStr;
4562 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
4563 if( pStr ){
4564 int flags;
4565 jsonAppendChar(pStr, '}');
4566 pStr->pCtx = ctx;
4567 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
4568 if( pStr->eErr ){
4569 jsonReturnString(pStr, 0, 0);
4570 return;
4571 }else if( flags & JSON_BLOB ){
4572 jsonReturnStringAsBlob(pStr);
4573 if( isFinal ){
4574 if( !pStr->bStatic ) sqlite3RCStrUnref(pStr->zBuf);
4575 }else{
4576 jsonStringTrimOneChar(pStr);
4578 return;
4579 }else if( isFinal ){
4580 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
4581 pStr->bStatic ? SQLITE_TRANSIENT :
4582 sqlite3RCStrUnref);
4583 pStr->bStatic = 1;
4584 }else{
4585 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
4586 jsonStringTrimOneChar(pStr);
4588 }else{
4589 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
4591 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
4593 static void jsonObjectValue(sqlite3_context *ctx){
4594 jsonObjectCompute(ctx, 0);
4596 static void jsonObjectFinal(sqlite3_context *ctx){
4597 jsonObjectCompute(ctx, 1);
4602 #ifndef SQLITE_OMIT_VIRTUALTABLE
4603 /****************************************************************************
4604 ** The json_each virtual table
4605 ****************************************************************************/
4606 typedef struct JsonParent JsonParent;
4607 struct JsonParent {
4608 u32 iHead; /* Start of object or array */
4609 u32 iValue; /* Start of the value */
4610 u32 iEnd; /* First byte past the end */
4611 u32 nPath; /* Length of path */
4612 i64 iKey; /* Key for JSONB_ARRAY */
4615 typedef struct JsonEachCursor JsonEachCursor;
4616 struct JsonEachCursor {
4617 sqlite3_vtab_cursor base; /* Base class - must be first */
4618 u32 iRowid; /* The rowid */
4619 u32 i; /* Index in sParse.aBlob[] of current row */
4620 u32 iEnd; /* EOF when i equals or exceeds this value */
4621 u32 nRoot; /* Size of the root path in bytes */
4622 u8 eType; /* Type of the container for element i */
4623 u8 bRecursive; /* True for json_tree(). False for json_each() */
4624 u32 nParent; /* Current nesting depth */
4625 u32 nParentAlloc; /* Space allocated for aParent[] */
4626 JsonParent *aParent; /* Parent elements of i */
4627 sqlite3 *db; /* Database connection */
4628 JsonString path; /* Current path */
4629 JsonParse sParse; /* Parse of the input JSON */
4631 typedef struct JsonEachConnection JsonEachConnection;
4632 struct JsonEachConnection {
4633 sqlite3_vtab base; /* Base class - must be first */
4634 sqlite3 *db; /* Database connection */
4638 /* Constructor for the json_each virtual table */
4639 static int jsonEachConnect(
4640 sqlite3 *db,
4641 void *pAux,
4642 int argc, const char *const*argv,
4643 sqlite3_vtab **ppVtab,
4644 char **pzErr
4646 JsonEachConnection *pNew;
4647 int rc;
4649 /* Column numbers */
4650 #define JEACH_KEY 0
4651 #define JEACH_VALUE 1
4652 #define JEACH_TYPE 2
4653 #define JEACH_ATOM 3
4654 #define JEACH_ID 4
4655 #define JEACH_PARENT 5
4656 #define JEACH_FULLKEY 6
4657 #define JEACH_PATH 7
4658 /* The xBestIndex method assumes that the JSON and ROOT columns are
4659 ** the last two columns in the table. Should this ever changes, be
4660 ** sure to update the xBestIndex method. */
4661 #define JEACH_JSON 8
4662 #define JEACH_ROOT 9
4664 UNUSED_PARAMETER(pzErr);
4665 UNUSED_PARAMETER(argv);
4666 UNUSED_PARAMETER(argc);
4667 UNUSED_PARAMETER(pAux);
4668 rc = sqlite3_declare_vtab(db,
4669 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
4670 "json HIDDEN,root HIDDEN)");
4671 if( rc==SQLITE_OK ){
4672 pNew = (JsonEachConnection*)sqlite3DbMallocZero(db, sizeof(*pNew));
4673 *ppVtab = (sqlite3_vtab*)pNew;
4674 if( pNew==0 ) return SQLITE_NOMEM;
4675 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
4676 pNew->db = db;
4678 return rc;
4681 /* destructor for json_each virtual table */
4682 static int jsonEachDisconnect(sqlite3_vtab *pVtab){
4683 JsonEachConnection *p = (JsonEachConnection*)pVtab;
4684 sqlite3DbFree(p->db, pVtab);
4685 return SQLITE_OK;
4688 /* constructor for a JsonEachCursor object for json_each(). */
4689 static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
4690 JsonEachConnection *pVtab = (JsonEachConnection*)p;
4691 JsonEachCursor *pCur;
4693 UNUSED_PARAMETER(p);
4694 pCur = sqlite3DbMallocZero(pVtab->db, sizeof(*pCur));
4695 if( pCur==0 ) return SQLITE_NOMEM;
4696 pCur->db = pVtab->db;
4697 jsonStringZero(&pCur->path);
4698 *ppCursor = &pCur->base;
4699 return SQLITE_OK;
4702 /* constructor for a JsonEachCursor object for json_tree(). */
4703 static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
4704 int rc = jsonEachOpenEach(p, ppCursor);
4705 if( rc==SQLITE_OK ){
4706 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
4707 pCur->bRecursive = 1;
4709 return rc;
4712 /* Reset a JsonEachCursor back to its original state. Free any memory
4713 ** held. */
4714 static void jsonEachCursorReset(JsonEachCursor *p){
4715 jsonParseReset(&p->sParse);
4716 jsonStringReset(&p->path);
4717 sqlite3DbFree(p->db, p->aParent);
4718 p->iRowid = 0;
4719 p->i = 0;
4720 p->aParent = 0;
4721 p->nParent = 0;
4722 p->nParentAlloc = 0;
4723 p->iEnd = 0;
4724 p->eType = 0;
4727 /* Destructor for a jsonEachCursor object */
4728 static int jsonEachClose(sqlite3_vtab_cursor *cur){
4729 JsonEachCursor *p = (JsonEachCursor*)cur;
4730 jsonEachCursorReset(p);
4732 sqlite3DbFree(p->db, cur);
4733 return SQLITE_OK;
4736 /* Return TRUE if the jsonEachCursor object has been advanced off the end
4737 ** of the JSON object */
4738 static int jsonEachEof(sqlite3_vtab_cursor *cur){
4739 JsonEachCursor *p = (JsonEachCursor*)cur;
4740 return p->i >= p->iEnd;
4744 ** If the cursor is currently pointing at the label of a object entry,
4745 ** then return the index of the value. For all other cases, return the
4746 ** current pointer position, which is the value.
4748 static int jsonSkipLabel(JsonEachCursor *p){
4749 if( p->eType==JSONB_OBJECT ){
4750 u32 sz = 0;
4751 u32 n = jsonbPayloadSize(&p->sParse, p->i, &sz);
4752 return p->i + n + sz;
4753 }else{
4754 return p->i;
4759 ** Append the path name for the current element.
4761 static void jsonAppendPathName(JsonEachCursor *p){
4762 assert( p->nParent>0 );
4763 assert( p->eType==JSONB_ARRAY || p->eType==JSONB_OBJECT );
4764 if( p->eType==JSONB_ARRAY ){
4765 jsonPrintf(30, &p->path, "[%lld]", p->aParent[p->nParent-1].iKey);
4766 }else{
4767 u32 n, sz = 0, k, i;
4768 const char *z;
4769 int needQuote = 0;
4770 n = jsonbPayloadSize(&p->sParse, p->i, &sz);
4771 k = p->i + n;
4772 z = (const char*)&p->sParse.aBlob[k];
4773 if( sz==0 || !sqlite3Isalpha(z[0]) ){
4774 needQuote = 1;
4775 }else{
4776 for(i=0; i<sz; i++){
4777 if( !sqlite3Isalnum(z[i]) ){
4778 needQuote = 1;
4779 break;
4783 if( needQuote ){
4784 jsonPrintf(sz+4,&p->path,".\"%.*s\"", sz, z);
4785 }else{
4786 jsonPrintf(sz+2,&p->path,".%.*s", sz, z);
4791 /* Advance the cursor to the next element for json_tree() */
4792 static int jsonEachNext(sqlite3_vtab_cursor *cur){
4793 JsonEachCursor *p = (JsonEachCursor*)cur;
4794 int rc = SQLITE_OK;
4795 if( p->bRecursive ){
4796 u8 x;
4797 u8 levelChange = 0;
4798 u32 n, sz = 0;
4799 u32 i = jsonSkipLabel(p);
4800 x = p->sParse.aBlob[i] & 0x0f;
4801 n = jsonbPayloadSize(&p->sParse, i, &sz);
4802 if( x==JSONB_OBJECT || x==JSONB_ARRAY ){
4803 JsonParent *pParent;
4804 if( p->nParent>=p->nParentAlloc ){
4805 JsonParent *pNew;
4806 u64 nNew;
4807 nNew = p->nParentAlloc*2 + 3;
4808 pNew = sqlite3DbRealloc(p->db, p->aParent, sizeof(JsonParent)*nNew);
4809 if( pNew==0 ) return SQLITE_NOMEM;
4810 p->nParentAlloc = (u32)nNew;
4811 p->aParent = pNew;
4813 levelChange = 1;
4814 pParent = &p->aParent[p->nParent];
4815 pParent->iHead = p->i;
4816 pParent->iValue = i;
4817 pParent->iEnd = i + n + sz;
4818 pParent->iKey = -1;
4819 pParent->nPath = (u32)p->path.nUsed;
4820 if( p->eType && p->nParent ){
4821 jsonAppendPathName(p);
4822 if( p->path.eErr ) rc = SQLITE_NOMEM;
4824 p->nParent++;
4825 p->i = i + n;
4826 }else{
4827 p->i = i + n + sz;
4829 while( p->nParent>0 && p->i >= p->aParent[p->nParent-1].iEnd ){
4830 p->nParent--;
4831 p->path.nUsed = p->aParent[p->nParent].nPath;
4832 levelChange = 1;
4834 if( levelChange ){
4835 if( p->nParent>0 ){
4836 JsonParent *pParent = &p->aParent[p->nParent-1];
4837 u32 iVal = pParent->iValue;
4838 p->eType = p->sParse.aBlob[iVal] & 0x0f;
4839 }else{
4840 p->eType = 0;
4843 }else{
4844 u32 n, sz = 0;
4845 u32 i = jsonSkipLabel(p);
4846 n = jsonbPayloadSize(&p->sParse, i, &sz);
4847 p->i = i + n + sz;
4849 if( p->eType==JSONB_ARRAY && p->nParent ){
4850 p->aParent[p->nParent-1].iKey++;
4852 p->iRowid++;
4853 return rc;
4856 /* Length of the path for rowid==0 in bRecursive mode.
4858 static int jsonEachPathLength(JsonEachCursor *p){
4859 u32 n = p->path.nUsed;
4860 char *z = p->path.zBuf;
4861 if( p->iRowid==0 && p->bRecursive && n>=2 ){
4862 while( n>1 ){
4863 n--;
4864 if( z[n]=='[' || z[n]=='.' ){
4865 u32 x, sz = 0;
4866 char cSaved = z[n];
4867 z[n] = 0;
4868 assert( p->sParse.eEdit==0 );
4869 x = jsonLookupStep(&p->sParse, 0, z+1, 0);
4870 z[n] = cSaved;
4871 if( JSON_LOOKUP_ISERROR(x) ) continue;
4872 if( x + jsonbPayloadSize(&p->sParse, x, &sz) == p->i ) break;
4876 return n;
4879 /* Return the value of a column */
4880 static int jsonEachColumn(
4881 sqlite3_vtab_cursor *cur, /* The cursor */
4882 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
4883 int iColumn /* Which column to return */
4885 JsonEachCursor *p = (JsonEachCursor*)cur;
4886 switch( iColumn ){
4887 case JEACH_KEY: {
4888 if( p->nParent==0 ){
4889 u32 n, j;
4890 if( p->nRoot==1 ) break;
4891 j = jsonEachPathLength(p);
4892 n = p->nRoot - j;
4893 if( n==0 ){
4894 break;
4895 }else if( p->path.zBuf[j]=='[' ){
4896 i64 x;
4897 sqlite3Atoi64(&p->path.zBuf[j+1], &x, n-1, SQLITE_UTF8);
4898 sqlite3_result_int64(ctx, x);
4899 }else if( p->path.zBuf[j+1]=='"' ){
4900 sqlite3_result_text(ctx, &p->path.zBuf[j+2], n-3, SQLITE_TRANSIENT);
4901 }else{
4902 sqlite3_result_text(ctx, &p->path.zBuf[j+1], n-1, SQLITE_TRANSIENT);
4904 break;
4906 if( p->eType==JSONB_OBJECT ){
4907 jsonReturnFromBlob(&p->sParse, p->i, ctx, 1);
4908 }else{
4909 assert( p->eType==JSONB_ARRAY );
4910 sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iKey);
4912 break;
4914 case JEACH_VALUE: {
4915 u32 i = jsonSkipLabel(p);
4916 jsonReturnFromBlob(&p->sParse, i, ctx, 1);
4917 if( (p->sParse.aBlob[i] & 0x0f)>=JSONB_ARRAY ){
4918 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
4920 break;
4922 case JEACH_TYPE: {
4923 u32 i = jsonSkipLabel(p);
4924 u8 eType = p->sParse.aBlob[i] & 0x0f;
4925 sqlite3_result_text(ctx, jsonbType[eType], -1, SQLITE_STATIC);
4926 break;
4928 case JEACH_ATOM: {
4929 u32 i = jsonSkipLabel(p);
4930 if( (p->sParse.aBlob[i] & 0x0f)<JSONB_ARRAY ){
4931 jsonReturnFromBlob(&p->sParse, i, ctx, 1);
4933 break;
4935 case JEACH_ID: {
4936 sqlite3_result_int64(ctx, (sqlite3_int64)p->i);
4937 break;
4939 case JEACH_PARENT: {
4940 if( p->nParent>0 && p->bRecursive ){
4941 sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iHead);
4943 break;
4945 case JEACH_FULLKEY: {
4946 u64 nBase = p->path.nUsed;
4947 if( p->nParent ) jsonAppendPathName(p);
4948 sqlite3_result_text64(ctx, p->path.zBuf, p->path.nUsed,
4949 SQLITE_TRANSIENT, SQLITE_UTF8);
4950 p->path.nUsed = nBase;
4951 break;
4953 case JEACH_PATH: {
4954 u32 n = jsonEachPathLength(p);
4955 sqlite3_result_text64(ctx, p->path.zBuf, n,
4956 SQLITE_TRANSIENT, SQLITE_UTF8);
4957 break;
4959 default: {
4960 sqlite3_result_text(ctx, p->path.zBuf, p->nRoot, SQLITE_STATIC);
4961 break;
4963 case JEACH_JSON: {
4964 if( p->sParse.zJson==0 ){
4965 sqlite3_result_blob(ctx, p->sParse.aBlob, p->sParse.nBlob,
4966 SQLITE_TRANSIENT);
4967 }else{
4968 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_TRANSIENT);
4970 break;
4973 return SQLITE_OK;
4976 /* Return the current rowid value */
4977 static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
4978 JsonEachCursor *p = (JsonEachCursor*)cur;
4979 *pRowid = p->iRowid;
4980 return SQLITE_OK;
4983 /* The query strategy is to look for an equality constraint on the json
4984 ** column. Without such a constraint, the table cannot operate. idxNum is
4985 ** 1 if the constraint is found, 3 if the constraint and zRoot are found,
4986 ** and 0 otherwise.
4988 static int jsonEachBestIndex(
4989 sqlite3_vtab *tab,
4990 sqlite3_index_info *pIdxInfo
4992 int i; /* Loop counter or computed array index */
4993 int aIdx[2]; /* Index of constraints for JSON and ROOT */
4994 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
4995 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
4996 const struct sqlite3_index_constraint *pConstraint;
4998 /* This implementation assumes that JSON and ROOT are the last two
4999 ** columns in the table */
5000 assert( JEACH_ROOT == JEACH_JSON+1 );
5001 UNUSED_PARAMETER(tab);
5002 aIdx[0] = aIdx[1] = -1;
5003 pConstraint = pIdxInfo->aConstraint;
5004 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
5005 int iCol;
5006 int iMask;
5007 if( pConstraint->iColumn < JEACH_JSON ) continue;
5008 iCol = pConstraint->iColumn - JEACH_JSON;
5009 assert( iCol==0 || iCol==1 );
5010 testcase( iCol==0 );
5011 iMask = 1 << iCol;
5012 if( pConstraint->usable==0 ){
5013 unusableMask |= iMask;
5014 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5015 aIdx[iCol] = i;
5016 idxMask |= iMask;
5019 if( pIdxInfo->nOrderBy>0
5020 && pIdxInfo->aOrderBy[0].iColumn<0
5021 && pIdxInfo->aOrderBy[0].desc==0
5023 pIdxInfo->orderByConsumed = 1;
5026 if( (unusableMask & ~idxMask)!=0 ){
5027 /* If there are any unusable constraints on JSON or ROOT, then reject
5028 ** this entire plan */
5029 return SQLITE_CONSTRAINT;
5031 if( aIdx[0]<0 ){
5032 /* No JSON input. Leave estimatedCost at the huge value that it was
5033 ** initialized to to discourage the query planner from selecting this
5034 ** plan. */
5035 pIdxInfo->idxNum = 0;
5036 }else{
5037 pIdxInfo->estimatedCost = 1.0;
5038 i = aIdx[0];
5039 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
5040 pIdxInfo->aConstraintUsage[i].omit = 1;
5041 if( aIdx[1]<0 ){
5042 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
5043 }else{
5044 i = aIdx[1];
5045 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
5046 pIdxInfo->aConstraintUsage[i].omit = 1;
5047 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
5050 return SQLITE_OK;
5053 /* Start a search on a new JSON string */
5054 static int jsonEachFilter(
5055 sqlite3_vtab_cursor *cur,
5056 int idxNum, const char *idxStr,
5057 int argc, sqlite3_value **argv
5059 JsonEachCursor *p = (JsonEachCursor*)cur;
5060 const char *zRoot = 0;
5061 u32 i, n, sz;
5063 UNUSED_PARAMETER(idxStr);
5064 UNUSED_PARAMETER(argc);
5065 jsonEachCursorReset(p);
5066 if( idxNum==0 ) return SQLITE_OK;
5067 memset(&p->sParse, 0, sizeof(p->sParse));
5068 p->sParse.nJPRef = 1;
5069 p->sParse.db = p->db;
5070 if( jsonFuncArgMightBeBinary(argv[0]) ){
5071 p->sParse.nBlob = sqlite3_value_bytes(argv[0]);
5072 p->sParse.aBlob = (u8*)sqlite3_value_blob(argv[0]);
5073 }else{
5074 p->sParse.zJson = (char*)sqlite3_value_text(argv[0]);
5075 p->sParse.nJson = sqlite3_value_bytes(argv[0]);
5076 if( p->sParse.zJson==0 ){
5077 p->i = p->iEnd = 0;
5078 return SQLITE_OK;
5080 if( jsonConvertTextToBlob(&p->sParse, 0) ){
5081 if( p->sParse.oom ){
5082 return SQLITE_NOMEM;
5084 goto json_each_malformed_input;
5087 if( idxNum==3 ){
5088 zRoot = (const char*)sqlite3_value_text(argv[1]);
5089 if( zRoot==0 ) return SQLITE_OK;
5090 if( zRoot[0]!='$' ){
5091 sqlite3_free(cur->pVtab->zErrMsg);
5092 cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot);
5093 jsonEachCursorReset(p);
5094 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
5096 p->nRoot = sqlite3Strlen30(zRoot);
5097 if( zRoot[1]==0 ){
5098 i = p->i = 0;
5099 p->eType = 0;
5100 }else{
5101 i = jsonLookupStep(&p->sParse, 0, zRoot+1, 0);
5102 if( JSON_LOOKUP_ISERROR(i) ){
5103 if( i==JSON_LOOKUP_NOTFOUND ){
5104 p->i = 0;
5105 p->eType = 0;
5106 p->iEnd = 0;
5107 return SQLITE_OK;
5109 sqlite3_free(cur->pVtab->zErrMsg);
5110 cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot);
5111 jsonEachCursorReset(p);
5112 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
5114 if( p->sParse.iLabel ){
5115 p->i = p->sParse.iLabel;
5116 p->eType = JSONB_OBJECT;
5117 }else{
5118 p->i = i;
5119 p->eType = JSONB_ARRAY;
5122 jsonAppendRaw(&p->path, zRoot, p->nRoot);
5123 }else{
5124 i = p->i = 0;
5125 p->eType = 0;
5126 p->nRoot = 1;
5127 jsonAppendRaw(&p->path, "$", 1);
5129 p->nParent = 0;
5130 n = jsonbPayloadSize(&p->sParse, i, &sz);
5131 p->iEnd = i+n+sz;
5132 if( (p->sParse.aBlob[i] & 0x0f)>=JSONB_ARRAY && !p->bRecursive ){
5133 p->i = i + n;
5134 p->eType = p->sParse.aBlob[i] & 0x0f;
5135 p->aParent = sqlite3DbMallocZero(p->db, sizeof(JsonParent));
5136 if( p->aParent==0 ) return SQLITE_NOMEM;
5137 p->nParent = 1;
5138 p->nParentAlloc = 1;
5139 p->aParent[0].iKey = 0;
5140 p->aParent[0].iEnd = p->iEnd;
5141 p->aParent[0].iHead = p->i;
5142 p->aParent[0].iValue = i;
5144 return SQLITE_OK;
5146 json_each_malformed_input:
5147 sqlite3_free(cur->pVtab->zErrMsg);
5148 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
5149 jsonEachCursorReset(p);
5150 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
5153 /* The methods of the json_each virtual table */
5154 static sqlite3_module jsonEachModule = {
5155 0, /* iVersion */
5156 0, /* xCreate */
5157 jsonEachConnect, /* xConnect */
5158 jsonEachBestIndex, /* xBestIndex */
5159 jsonEachDisconnect, /* xDisconnect */
5160 0, /* xDestroy */
5161 jsonEachOpenEach, /* xOpen - open a cursor */
5162 jsonEachClose, /* xClose - close a cursor */
5163 jsonEachFilter, /* xFilter - configure scan constraints */
5164 jsonEachNext, /* xNext - advance a cursor */
5165 jsonEachEof, /* xEof - check for end of scan */
5166 jsonEachColumn, /* xColumn - read data */
5167 jsonEachRowid, /* xRowid - read data */
5168 0, /* xUpdate */
5169 0, /* xBegin */
5170 0, /* xSync */
5171 0, /* xCommit */
5172 0, /* xRollback */
5173 0, /* xFindMethod */
5174 0, /* xRename */
5175 0, /* xSavepoint */
5176 0, /* xRelease */
5177 0, /* xRollbackTo */
5178 0, /* xShadowName */
5179 0 /* xIntegrity */
5182 /* The methods of the json_tree virtual table. */
5183 static sqlite3_module jsonTreeModule = {
5184 0, /* iVersion */
5185 0, /* xCreate */
5186 jsonEachConnect, /* xConnect */
5187 jsonEachBestIndex, /* xBestIndex */
5188 jsonEachDisconnect, /* xDisconnect */
5189 0, /* xDestroy */
5190 jsonEachOpenTree, /* xOpen - open a cursor */
5191 jsonEachClose, /* xClose - close a cursor */
5192 jsonEachFilter, /* xFilter - configure scan constraints */
5193 jsonEachNext, /* xNext - advance a cursor */
5194 jsonEachEof, /* xEof - check for end of scan */
5195 jsonEachColumn, /* xColumn - read data */
5196 jsonEachRowid, /* xRowid - read data */
5197 0, /* xUpdate */
5198 0, /* xBegin */
5199 0, /* xSync */
5200 0, /* xCommit */
5201 0, /* xRollback */
5202 0, /* xFindMethod */
5203 0, /* xRename */
5204 0, /* xSavepoint */
5205 0, /* xRelease */
5206 0, /* xRollbackTo */
5207 0, /* xShadowName */
5208 0 /* xIntegrity */
5210 #endif /* SQLITE_OMIT_VIRTUALTABLE */
5211 #endif /* !defined(SQLITE_OMIT_JSON) */
5214 ** Register JSON functions.
5216 void sqlite3RegisterJsonFunctions(void){
5217 #ifndef SQLITE_OMIT_JSON
5218 static FuncDef aJsonFunc[] = {
5219 /* sqlite3_result_subtype() ----, ,--- sqlite3_value_subtype() */
5220 /* | | */
5221 /* Uses cache ------, | | ,---- Returns JSONB */
5222 /* | | | | */
5223 /* Number of arguments ---, | | | | ,--- Flags */
5224 /* | | | | | | */
5225 JFUNCTION(json, 1,1,1, 0,0,0, jsonRemoveFunc),
5226 JFUNCTION(jsonb, 1,1,0, 0,1,0, jsonRemoveFunc),
5227 JFUNCTION(json_array, -1,0,1, 1,0,0, jsonArrayFunc),
5228 JFUNCTION(jsonb_array, -1,0,1, 1,1,0, jsonArrayFunc),
5229 JFUNCTION(json_array_length, 1,1,0, 0,0,0, jsonArrayLengthFunc),
5230 JFUNCTION(json_array_length, 2,1,0, 0,0,0, jsonArrayLengthFunc),
5231 JFUNCTION(json_error_position,1,1,0, 0,0,0, jsonErrorFunc),
5232 JFUNCTION(json_extract, -1,1,1, 0,0,0, jsonExtractFunc),
5233 JFUNCTION(jsonb_extract, -1,1,0, 0,1,0, jsonExtractFunc),
5234 JFUNCTION(->, 2,1,1, 0,0,JSON_JSON, jsonExtractFunc),
5235 JFUNCTION(->>, 2,1,0, 0,0,JSON_SQL, jsonExtractFunc),
5236 JFUNCTION(json_insert, -1,1,1, 1,0,0, jsonSetFunc),
5237 JFUNCTION(jsonb_insert, -1,1,0, 1,1,0, jsonSetFunc),
5238 JFUNCTION(json_object, -1,0,1, 1,0,0, jsonObjectFunc),
5239 JFUNCTION(jsonb_object, -1,0,1, 1,1,0, jsonObjectFunc),
5240 JFUNCTION(json_patch, 2,1,1, 0,0,0, jsonPatchFunc),
5241 JFUNCTION(jsonb_patch, 2,1,0, 0,1,0, jsonPatchFunc),
5242 JFUNCTION(json_quote, 1,0,1, 1,0,0, jsonQuoteFunc),
5243 JFUNCTION(json_remove, -1,1,1, 0,0,0, jsonRemoveFunc),
5244 JFUNCTION(jsonb_remove, -1,1,0, 0,1,0, jsonRemoveFunc),
5245 JFUNCTION(json_replace, -1,1,1, 1,0,0, jsonReplaceFunc),
5246 JFUNCTION(jsonb_replace, -1,1,0, 1,1,0, jsonReplaceFunc),
5247 JFUNCTION(json_set, -1,1,1, 1,0,JSON_ISSET, jsonSetFunc),
5248 JFUNCTION(jsonb_set, -1,1,0, 1,1,JSON_ISSET, jsonSetFunc),
5249 JFUNCTION(json_type, 1,1,0, 0,0,0, jsonTypeFunc),
5250 JFUNCTION(json_type, 2,1,0, 0,0,0, jsonTypeFunc),
5251 JFUNCTION(json_valid, 1,1,0, 0,0,0, jsonValidFunc),
5252 JFUNCTION(json_valid, 2,1,0, 0,0,0, jsonValidFunc),
5253 #if SQLITE_DEBUG
5254 JFUNCTION(json_parse, 1,1,0, 0,0,0, jsonParseFunc),
5255 #endif
5256 WAGGREGATE(json_group_array, 1, 0, 0,
5257 jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,
5258 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|
5259 SQLITE_DETERMINISTIC),
5260 WAGGREGATE(jsonb_group_array, 1, JSON_BLOB, 0,
5261 jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,
5262 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC),
5263 WAGGREGATE(json_group_object, 2, 0, 0,
5264 jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,
5265 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC),
5266 WAGGREGATE(jsonb_group_object,2, JSON_BLOB, 0,
5267 jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,
5268 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|
5269 SQLITE_DETERMINISTIC)
5271 sqlite3InsertBuiltinFuncs(aJsonFunc, ArraySize(aJsonFunc));
5272 #endif
5275 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
5277 ** Register the JSON table-valued functions
5279 int sqlite3JsonTableFunctions(sqlite3 *db){
5280 int rc = SQLITE_OK;
5281 static const struct {
5282 const char *zName;
5283 sqlite3_module *pModule;
5284 } aMod[] = {
5285 { "json_each", &jsonEachModule },
5286 { "json_tree", &jsonTreeModule },
5288 unsigned int i;
5289 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
5290 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
5292 return rc;
5294 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) */