Simplifications to PRAGMA optimize to make it easier to use. It always
[sqlite.git] / src / json.c
blobc33b63556ac83dab68f4f5aa81b3032e5f878996
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 /* c is a control character. Append the canonical JSON representation
625 ** of that control character to p.
627 ** This routine assumes that the output buffer has already been enlarged
628 ** sufficiently to hold the worst-case encoding plus a nul terminator.
630 static void jsonAppendControlChar(JsonString *p, u8 c){
631 static const char aSpecial[] = {
632 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
633 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
635 assert( sizeof(aSpecial)==32 );
636 assert( aSpecial['\b']=='b' );
637 assert( aSpecial['\f']=='f' );
638 assert( aSpecial['\n']=='n' );
639 assert( aSpecial['\r']=='r' );
640 assert( aSpecial['\t']=='t' );
641 assert( c>=0 && c<sizeof(aSpecial) );
642 assert( p->nUsed+7 <= p->nAlloc );
643 if( aSpecial[c] ){
644 p->zBuf[p->nUsed] = '\\';
645 p->zBuf[p->nUsed+1] = aSpecial[c];
646 p->nUsed += 2;
647 }else{
648 p->zBuf[p->nUsed] = '\\';
649 p->zBuf[p->nUsed+1] = 'u';
650 p->zBuf[p->nUsed+2] = '0';
651 p->zBuf[p->nUsed+3] = '0';
652 p->zBuf[p->nUsed+4] = "0123456789abcdef"[c>>4];
653 p->zBuf[p->nUsed+5] = "0123456789abcdef"[c&0xf];
654 p->nUsed += 6;
658 /* Append the N-byte string in zIn to the end of the JsonString string
659 ** under construction. Enclose the string in double-quotes ("...") and
660 ** escape any double-quotes or backslash characters contained within the
661 ** string.
663 ** This routine is a high-runner. There is a measurable performance
664 ** increase associated with unwinding the jsonIsOk[] loop.
666 static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
667 u32 k;
668 u8 c;
669 const u8 *z = (const u8*)zIn;
670 if( z==0 ) return;
671 if( (N+p->nUsed+2 >= p->nAlloc) && jsonStringGrow(p,N+2)!=0 ) return;
672 p->zBuf[p->nUsed++] = '"';
673 while( 1 /*exit-by-break*/ ){
674 k = 0;
675 /* The following while() is the 4-way unwound equivalent of
677 ** while( k<N && jsonIsOk[z[k]] ){ k++; }
679 while( 1 /* Exit by break */ ){
680 if( k+3>=N ){
681 while( k<N && jsonIsOk[z[k]] ){ k++; }
682 break;
684 if( !jsonIsOk[z[k]] ){
685 break;
687 if( !jsonIsOk[z[k+1]] ){
688 k += 1;
689 break;
691 if( !jsonIsOk[z[k+2]] ){
692 k += 2;
693 break;
695 if( !jsonIsOk[z[k+3]] ){
696 k += 3;
697 break;
698 }else{
699 k += 4;
702 if( k>=N ){
703 if( k>0 ){
704 memcpy(&p->zBuf[p->nUsed], z, k);
705 p->nUsed += k;
707 break;
709 if( k>0 ){
710 memcpy(&p->zBuf[p->nUsed], z, k);
711 p->nUsed += k;
712 z += k;
713 N -= k;
715 c = z[0];
716 if( c=='"' || c=='\\' ){
717 if( (p->nUsed+N+3 > p->nAlloc) && jsonStringGrow(p,N+3)!=0 ) return;
718 p->zBuf[p->nUsed++] = '\\';
719 p->zBuf[p->nUsed++] = c;
720 }else if( c=='\'' ){
721 p->zBuf[p->nUsed++] = c;
722 }else{
723 if( (p->nUsed+N+7 > p->nAlloc) && jsonStringGrow(p,N+7)!=0 ) return;
724 jsonAppendControlChar(p, c);
726 z++;
727 N--;
729 p->zBuf[p->nUsed++] = '"';
730 assert( p->nUsed<p->nAlloc );
734 ** Append an sqlite3_value (such as a function parameter) to the JSON
735 ** string under construction in p.
737 static void jsonAppendSqlValue(
738 JsonString *p, /* Append to this JSON string */
739 sqlite3_value *pValue /* Value to append */
741 switch( sqlite3_value_type(pValue) ){
742 case SQLITE_NULL: {
743 jsonAppendRawNZ(p, "null", 4);
744 break;
746 case SQLITE_FLOAT: {
747 jsonPrintf(100, p, "%!0.15g", sqlite3_value_double(pValue));
748 break;
750 case SQLITE_INTEGER: {
751 const char *z = (const char*)sqlite3_value_text(pValue);
752 u32 n = (u32)sqlite3_value_bytes(pValue);
753 jsonAppendRaw(p, z, n);
754 break;
756 case SQLITE_TEXT: {
757 const char *z = (const char*)sqlite3_value_text(pValue);
758 u32 n = (u32)sqlite3_value_bytes(pValue);
759 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
760 jsonAppendRaw(p, z, n);
761 }else{
762 jsonAppendString(p, z, n);
764 break;
766 default: {
767 if( jsonFuncArgMightBeBinary(pValue) ){
768 JsonParse px;
769 memset(&px, 0, sizeof(px));
770 px.aBlob = (u8*)sqlite3_value_blob(pValue);
771 px.nBlob = sqlite3_value_bytes(pValue);
772 jsonTranslateBlobToText(&px, 0, p);
773 }else if( p->eErr==0 ){
774 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
775 p->eErr = JSTRING_ERR;
776 jsonStringReset(p);
778 break;
783 /* Make the text in p (which is probably a generated JSON text string)
784 ** the result of the SQL function.
786 ** The JsonString is reset.
788 ** If pParse and ctx are both non-NULL, then the SQL string in p is
789 ** loaded into the zJson field of the pParse object as a RCStr and the
790 ** pParse is added to the cache.
792 static void jsonReturnString(
793 JsonString *p, /* String to return */
794 JsonParse *pParse, /* JSONB source or NULL */
795 sqlite3_context *ctx /* Where to cache */
797 assert( (pParse!=0)==(ctx!=0) );
798 assert( ctx==0 || ctx==p->pCtx );
799 if( p->eErr==0 ){
800 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(p->pCtx));
801 if( flags & JSON_BLOB ){
802 jsonReturnStringAsBlob(p);
803 }else if( p->bStatic ){
804 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
805 SQLITE_TRANSIENT, SQLITE_UTF8);
806 }else if( jsonStringTerminate(p) ){
807 if( pParse && pParse->bJsonIsRCStr==0 && pParse->nBlobAlloc>0 ){
808 int rc;
809 pParse->zJson = sqlite3RCStrRef(p->zBuf);
810 pParse->nJson = p->nUsed;
811 pParse->bJsonIsRCStr = 1;
812 rc = jsonCacheInsert(ctx, pParse);
813 if( rc==SQLITE_NOMEM ){
814 sqlite3_result_error_nomem(ctx);
815 jsonStringReset(p);
816 return;
819 sqlite3_result_text64(p->pCtx, sqlite3RCStrRef(p->zBuf), p->nUsed,
820 sqlite3RCStrUnref,
821 SQLITE_UTF8);
822 }else{
823 sqlite3_result_error_nomem(p->pCtx);
825 }else if( p->eErr & JSTRING_OOM ){
826 sqlite3_result_error_nomem(p->pCtx);
827 }else if( p->eErr & JSTRING_MALFORMED ){
828 sqlite3_result_error(p->pCtx, "malformed JSON", -1);
830 jsonStringReset(p);
833 /**************************************************************************
834 ** Utility routines for dealing with JsonParse objects
835 **************************************************************************/
838 ** Reclaim all memory allocated by a JsonParse object. But do not
839 ** delete the JsonParse object itself.
841 static void jsonParseReset(JsonParse *pParse){
842 assert( pParse->nJPRef<=1 );
843 if( pParse->bJsonIsRCStr ){
844 sqlite3RCStrUnref(pParse->zJson);
845 pParse->zJson = 0;
846 pParse->nJson = 0;
847 pParse->bJsonIsRCStr = 0;
849 if( pParse->nBlobAlloc ){
850 sqlite3DbFree(pParse->db, pParse->aBlob);
851 pParse->aBlob = 0;
852 pParse->nBlob = 0;
853 pParse->nBlobAlloc = 0;
858 ** Decrement the reference count on the JsonParse object. When the
859 ** count reaches zero, free the object.
861 static void jsonParseFree(JsonParse *pParse){
862 if( pParse ){
863 if( pParse->nJPRef>1 ){
864 pParse->nJPRef--;
865 }else{
866 jsonParseReset(pParse);
867 sqlite3DbFree(pParse->db, pParse);
872 /**************************************************************************
873 ** Utility routines for the JSON text parser
874 **************************************************************************/
877 ** Translate a single byte of Hex into an integer.
878 ** This routine only gives a correct answer if h really is a valid hexadecimal
879 ** character: 0..9a..fA..F. But unlike sqlite3HexToInt(), it does not
880 ** assert() if the digit is not hex.
882 static u8 jsonHexToInt(int h){
883 #ifdef SQLITE_ASCII
884 h += 9*(1&(h>>6));
885 #endif
886 #ifdef SQLITE_EBCDIC
887 h += 9*(1&~(h>>4));
888 #endif
889 return (u8)(h & 0xf);
893 ** Convert a 4-byte hex string into an integer
895 static u32 jsonHexToInt4(const char *z){
896 u32 v;
897 v = (jsonHexToInt(z[0])<<12)
898 + (jsonHexToInt(z[1])<<8)
899 + (jsonHexToInt(z[2])<<4)
900 + jsonHexToInt(z[3]);
901 return v;
905 ** Return true if z[] begins with 2 (or more) hexadecimal digits
907 static int jsonIs2Hex(const char *z){
908 return sqlite3Isxdigit(z[0]) && sqlite3Isxdigit(z[1]);
912 ** Return true if z[] begins with 4 (or more) hexadecimal digits
914 static int jsonIs4Hex(const char *z){
915 return jsonIs2Hex(z) && jsonIs2Hex(&z[2]);
919 ** Return the number of bytes of JSON5 whitespace at the beginning of
920 ** the input string z[].
922 ** JSON5 whitespace consists of any of the following characters:
924 ** Unicode UTF-8 Name
925 ** U+0009 09 horizontal tab
926 ** U+000a 0a line feed
927 ** U+000b 0b vertical tab
928 ** U+000c 0c form feed
929 ** U+000d 0d carriage return
930 ** U+0020 20 space
931 ** U+00a0 c2 a0 non-breaking space
932 ** U+1680 e1 9a 80 ogham space mark
933 ** U+2000 e2 80 80 en quad
934 ** U+2001 e2 80 81 em quad
935 ** U+2002 e2 80 82 en space
936 ** U+2003 e2 80 83 em space
937 ** U+2004 e2 80 84 three-per-em space
938 ** U+2005 e2 80 85 four-per-em space
939 ** U+2006 e2 80 86 six-per-em space
940 ** U+2007 e2 80 87 figure space
941 ** U+2008 e2 80 88 punctuation space
942 ** U+2009 e2 80 89 thin space
943 ** U+200a e2 80 8a hair space
944 ** U+2028 e2 80 a8 line separator
945 ** U+2029 e2 80 a9 paragraph separator
946 ** U+202f e2 80 af narrow no-break space (NNBSP)
947 ** U+205f e2 81 9f medium mathematical space (MMSP)
948 ** U+3000 e3 80 80 ideographical space
949 ** U+FEFF ef bb bf byte order mark
951 ** In addition, comments between '/', '*' and '*', '/' and
952 ** from '/', '/' to end-of-line are also considered to be whitespace.
954 static int json5Whitespace(const char *zIn){
955 int n = 0;
956 const u8 *z = (u8*)zIn;
957 while( 1 /*exit by "goto whitespace_done"*/ ){
958 switch( z[n] ){
959 case 0x09:
960 case 0x0a:
961 case 0x0b:
962 case 0x0c:
963 case 0x0d:
964 case 0x20: {
965 n++;
966 break;
968 case '/': {
969 if( z[n+1]=='*' && z[n+2]!=0 ){
970 int j;
971 for(j=n+3; z[j]!='/' || z[j-1]!='*'; j++){
972 if( z[j]==0 ) goto whitespace_done;
974 n = j+1;
975 break;
976 }else if( z[n+1]=='/' ){
977 int j;
978 char c;
979 for(j=n+2; (c = z[j])!=0; j++){
980 if( c=='\n' || c=='\r' ) break;
981 if( 0xe2==(u8)c && 0x80==(u8)z[j+1]
982 && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2])
984 j += 2;
985 break;
988 n = j;
989 if( z[n] ) n++;
990 break;
992 goto whitespace_done;
994 case 0xc2: {
995 if( z[n+1]==0xa0 ){
996 n += 2;
997 break;
999 goto whitespace_done;
1001 case 0xe1: {
1002 if( z[n+1]==0x9a && z[n+2]==0x80 ){
1003 n += 3;
1004 break;
1006 goto whitespace_done;
1008 case 0xe2: {
1009 if( z[n+1]==0x80 ){
1010 u8 c = z[n+2];
1011 if( c<0x80 ) goto whitespace_done;
1012 if( c<=0x8a || c==0xa8 || c==0xa9 || c==0xaf ){
1013 n += 3;
1014 break;
1016 }else if( z[n+1]==0x81 && z[n+2]==0x9f ){
1017 n += 3;
1018 break;
1020 goto whitespace_done;
1022 case 0xe3: {
1023 if( z[n+1]==0x80 && z[n+2]==0x80 ){
1024 n += 3;
1025 break;
1027 goto whitespace_done;
1029 case 0xef: {
1030 if( z[n+1]==0xbb && z[n+2]==0xbf ){
1031 n += 3;
1032 break;
1034 goto whitespace_done;
1036 default: {
1037 goto whitespace_done;
1041 whitespace_done:
1042 return n;
1046 ** Extra floating-point literals to allow in JSON.
1048 static const struct NanInfName {
1049 char c1;
1050 char c2;
1051 char n;
1052 char eType;
1053 char nRepl;
1054 char *zMatch;
1055 char *zRepl;
1056 } aNanInfName[] = {
1057 { 'i', 'I', 3, JSONB_FLOAT, 7, "inf", "9.0e999" },
1058 { 'i', 'I', 8, JSONB_FLOAT, 7, "infinity", "9.0e999" },
1059 { 'n', 'N', 3, JSONB_NULL, 4, "NaN", "null" },
1060 { 'q', 'Q', 4, JSONB_NULL, 4, "QNaN", "null" },
1061 { 's', 'S', 4, JSONB_NULL, 4, "SNaN", "null" },
1066 ** Report the wrong number of arguments for json_insert(), json_replace()
1067 ** or json_set().
1069 static void jsonWrongNumArgs(
1070 sqlite3_context *pCtx,
1071 const char *zFuncName
1073 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
1074 zFuncName);
1075 sqlite3_result_error(pCtx, zMsg, -1);
1076 sqlite3_free(zMsg);
1079 /****************************************************************************
1080 ** Utility routines for dealing with the binary BLOB representation of JSON
1081 ****************************************************************************/
1084 ** Expand pParse->aBlob so that it holds at least N bytes.
1086 ** Return the number of errors.
1088 static int jsonBlobExpand(JsonParse *pParse, u32 N){
1089 u8 *aNew;
1090 u32 t;
1091 assert( N>pParse->nBlobAlloc );
1092 if( pParse->nBlobAlloc==0 ){
1093 t = 100;
1094 }else{
1095 t = pParse->nBlobAlloc*2;
1097 if( t<N ) t = N+100;
1098 aNew = sqlite3DbRealloc(pParse->db, pParse->aBlob, t);
1099 if( aNew==0 ){ pParse->oom = 1; return 1; }
1100 pParse->aBlob = aNew;
1101 pParse->nBlobAlloc = t;
1102 return 0;
1106 ** If pParse->aBlob is not previously editable (because it is taken
1107 ** from sqlite3_value_blob(), as indicated by the fact that
1108 ** pParse->nBlobAlloc==0 and pParse->nBlob>0) then make it editable
1109 ** by making a copy into space obtained from malloc.
1111 ** Return true on success. Return false on OOM.
1113 static int jsonBlobMakeEditable(JsonParse *pParse, u32 nExtra){
1114 u8 *aOld;
1115 u32 nSize;
1116 assert( !pParse->bReadOnly );
1117 if( pParse->oom ) return 0;
1118 if( pParse->nBlobAlloc>0 ) return 1;
1119 aOld = pParse->aBlob;
1120 nSize = pParse->nBlob + nExtra;
1121 pParse->aBlob = 0;
1122 if( jsonBlobExpand(pParse, nSize) ){
1123 return 0;
1125 assert( pParse->nBlobAlloc >= pParse->nBlob + nExtra );
1126 memcpy(pParse->aBlob, aOld, pParse->nBlob);
1127 return 1;
1130 /* Expand pParse->aBlob and append one bytes.
1132 static SQLITE_NOINLINE void jsonBlobExpandAndAppendOneByte(
1133 JsonParse *pParse,
1134 u8 c
1136 jsonBlobExpand(pParse, pParse->nBlob+1);
1137 if( pParse->oom==0 ){
1138 assert( pParse->nBlob+1<=pParse->nBlobAlloc );
1139 pParse->aBlob[pParse->nBlob++] = c;
1143 /* Append a single character.
1145 static void jsonBlobAppendOneByte(JsonParse *pParse, u8 c){
1146 if( pParse->nBlob >= pParse->nBlobAlloc ){
1147 jsonBlobExpandAndAppendOneByte(pParse, c);
1148 }else{
1149 pParse->aBlob[pParse->nBlob++] = c;
1153 /* Slow version of jsonBlobAppendNode() that first resizes the
1154 ** pParse->aBlob structure.
1156 static void jsonBlobAppendNode(JsonParse*,u8,u32,const void*);
1157 static SQLITE_NOINLINE void jsonBlobExpandAndAppendNode(
1158 JsonParse *pParse,
1159 u8 eType,
1160 u32 szPayload,
1161 const void *aPayload
1163 if( jsonBlobExpand(pParse, pParse->nBlob+szPayload+9) ) return;
1164 jsonBlobAppendNode(pParse, eType, szPayload, aPayload);
1168 /* Append an node type byte together with the payload size and
1169 ** possibly also the payload.
1171 ** If aPayload is not NULL, then it is a pointer to the payload which
1172 ** is also appended. If aPayload is NULL, the pParse->aBlob[] array
1173 ** is resized (if necessary) so that it is big enough to hold the
1174 ** payload, but the payload is not appended and pParse->nBlob is left
1175 ** pointing to where the first byte of payload will eventually be.
1177 static void jsonBlobAppendNode(
1178 JsonParse *pParse, /* The JsonParse object under construction */
1179 u8 eType, /* Node type. One of JSONB_* */
1180 u32 szPayload, /* Number of bytes of payload */
1181 const void *aPayload /* The payload. Might be NULL */
1183 u8 *a;
1184 if( pParse->nBlob+szPayload+9 > pParse->nBlobAlloc ){
1185 jsonBlobExpandAndAppendNode(pParse,eType,szPayload,aPayload);
1186 return;
1188 assert( pParse->aBlob!=0 );
1189 a = &pParse->aBlob[pParse->nBlob];
1190 if( szPayload<=11 ){
1191 a[0] = eType | (szPayload<<4);
1192 pParse->nBlob += 1;
1193 }else if( szPayload<=0xff ){
1194 a[0] = eType | 0xc0;
1195 a[1] = szPayload & 0xff;
1196 pParse->nBlob += 2;
1197 }else if( szPayload<=0xffff ){
1198 a[0] = eType | 0xd0;
1199 a[1] = (szPayload >> 8) & 0xff;
1200 a[2] = szPayload & 0xff;
1201 pParse->nBlob += 3;
1202 }else{
1203 a[0] = eType | 0xe0;
1204 a[1] = (szPayload >> 24) & 0xff;
1205 a[2] = (szPayload >> 16) & 0xff;
1206 a[3] = (szPayload >> 8) & 0xff;
1207 a[4] = szPayload & 0xff;
1208 pParse->nBlob += 5;
1210 if( aPayload ){
1211 pParse->nBlob += szPayload;
1212 memcpy(&pParse->aBlob[pParse->nBlob-szPayload], aPayload, szPayload);
1216 /* Change the payload size for the node at index i to be szPayload.
1218 static int jsonBlobChangePayloadSize(
1219 JsonParse *pParse,
1220 u32 i,
1221 u32 szPayload
1223 u8 *a;
1224 u8 szType;
1225 u8 nExtra;
1226 u8 nNeeded;
1227 int delta;
1228 if( pParse->oom ) return 0;
1229 a = &pParse->aBlob[i];
1230 szType = a[0]>>4;
1231 if( szType<=11 ){
1232 nExtra = 0;
1233 }else if( szType==12 ){
1234 nExtra = 1;
1235 }else if( szType==13 ){
1236 nExtra = 2;
1237 }else{
1238 nExtra = 4;
1240 if( szPayload<=11 ){
1241 nNeeded = 0;
1242 }else if( szPayload<=0xff ){
1243 nNeeded = 1;
1244 }else if( szPayload<=0xffff ){
1245 nNeeded = 2;
1246 }else{
1247 nNeeded = 4;
1249 delta = nNeeded - nExtra;
1250 if( delta ){
1251 u32 newSize = pParse->nBlob + delta;
1252 if( delta>0 ){
1253 if( newSize>pParse->nBlobAlloc && jsonBlobExpand(pParse, newSize) ){
1254 return 0; /* OOM error. Error state recorded in pParse->oom. */
1256 a = &pParse->aBlob[i];
1257 memmove(&a[1+delta], &a[1], pParse->nBlob - (i+1));
1258 }else{
1259 memmove(&a[1], &a[1-delta], pParse->nBlob - (i+1-delta));
1261 pParse->nBlob = newSize;
1263 if( nNeeded==0 ){
1264 a[0] = (a[0] & 0x0f) | (szPayload<<4);
1265 }else if( nNeeded==1 ){
1266 a[0] = (a[0] & 0x0f) | 0xc0;
1267 a[1] = szPayload & 0xff;
1268 }else if( nNeeded==2 ){
1269 a[0] = (a[0] & 0x0f) | 0xd0;
1270 a[1] = (szPayload >> 8) & 0xff;
1271 a[2] = szPayload & 0xff;
1272 }else{
1273 a[0] = (a[0] & 0x0f) | 0xe0;
1274 a[1] = (szPayload >> 24) & 0xff;
1275 a[2] = (szPayload >> 16) & 0xff;
1276 a[3] = (szPayload >> 8) & 0xff;
1277 a[4] = szPayload & 0xff;
1279 return delta;
1283 ** If z[0] is 'u' and is followed by exactly 4 hexadecimal character,
1284 ** then set *pOp to JSONB_TEXTJ and return true. If not, do not make
1285 ** any changes to *pOp and return false.
1287 static int jsonIs4HexB(const char *z, int *pOp){
1288 if( z[0]!='u' ) return 0;
1289 if( !jsonIs4Hex(&z[1]) ) return 0;
1290 *pOp = JSONB_TEXTJ;
1291 return 1;
1295 ** Check a single element of the JSONB in pParse for validity.
1297 ** The element to be checked starts at offset i and must end at on the
1298 ** last byte before iEnd.
1300 ** Return 0 if everything is correct. Return the 1-based byte offset of the
1301 ** error if a problem is detected. (In other words, if the error is at offset
1302 ** 0, return 1).
1304 static u32 jsonbValidityCheck(
1305 const JsonParse *pParse, /* Input JSONB. Only aBlob and nBlob are used */
1306 u32 i, /* Start of element as pParse->aBlob[i] */
1307 u32 iEnd, /* One more than the last byte of the element */
1308 u32 iDepth /* Current nesting depth */
1310 u32 n, sz, j, k;
1311 const u8 *z;
1312 u8 x;
1313 if( iDepth>JSON_MAX_DEPTH ) return i+1;
1314 sz = 0;
1315 n = jsonbPayloadSize(pParse, i, &sz);
1316 if( NEVER(n==0) ) return i+1; /* Checked by caller */
1317 if( NEVER(i+n+sz!=iEnd) ) return i+1; /* Checked by caller */
1318 z = pParse->aBlob;
1319 x = z[i] & 0x0f;
1320 switch( x ){
1321 case JSONB_NULL:
1322 case JSONB_TRUE:
1323 case JSONB_FALSE: {
1324 return n+sz==1 ? 0 : i+1;
1326 case JSONB_INT: {
1327 if( sz<1 ) return i+1;
1328 j = i+n;
1329 if( z[j]=='-' ){
1330 j++;
1331 if( sz<2 ) return i+1;
1333 k = i+n+sz;
1334 while( j<k ){
1335 if( sqlite3Isdigit(z[j]) ){
1336 j++;
1337 }else{
1338 return j+1;
1341 return 0;
1343 case JSONB_INT5: {
1344 if( sz<3 ) return i+1;
1345 j = i+n;
1346 if( z[j]=='-' ){
1347 if( sz<4 ) return i+1;
1348 j++;
1350 if( z[j]!='0' ) return i+1;
1351 if( z[j+1]!='x' && z[j+1]!='X' ) return j+2;
1352 j += 2;
1353 k = i+n+sz;
1354 while( j<k ){
1355 if( sqlite3Isxdigit(z[j]) ){
1356 j++;
1357 }else{
1358 return j+1;
1361 return 0;
1363 case JSONB_FLOAT:
1364 case JSONB_FLOAT5: {
1365 u8 seen = 0; /* 0: initial. 1: '.' seen 2: 'e' seen */
1366 if( sz<2 ) return i+1;
1367 j = i+n;
1368 k = j+sz;
1369 if( z[j]=='-' ){
1370 j++;
1371 if( sz<3 ) return i+1;
1373 if( z[j]=='.' ){
1374 if( x==JSONB_FLOAT ) return j+1;
1375 if( !sqlite3Isdigit(z[j+1]) ) return j+1;
1376 j += 2;
1377 seen = 1;
1378 }else if( z[j]=='0' && x==JSONB_FLOAT ){
1379 if( j+3>k ) return j+1;
1380 if( z[j+1]!='.' && z[j+1]!='e' && z[j+1]!='E' ) return j+1;
1381 j++;
1383 for(; j<k; j++){
1384 if( sqlite3Isdigit(z[j]) ) continue;
1385 if( z[j]=='.' ){
1386 if( seen>0 ) return j+1;
1387 if( x==JSONB_FLOAT && (j==k-1 || !sqlite3Isdigit(z[j+1])) ){
1388 return j+1;
1390 seen = 1;
1391 continue;
1393 if( z[j]=='e' || z[j]=='E' ){
1394 if( seen==2 ) return j+1;
1395 if( j==k-1 ) return j+1;
1396 if( z[j+1]=='+' || z[j+1]=='-' ){
1397 j++;
1398 if( j==k-1 ) return j+1;
1400 seen = 2;
1401 continue;
1403 return j+1;
1405 if( seen==0 ) return i+1;
1406 return 0;
1408 case JSONB_TEXT: {
1409 j = i+n;
1410 k = j+sz;
1411 while( j<k ){
1412 if( !jsonIsOk[z[j]] && z[j]!='\'' ) return j+1;
1413 j++;
1415 return 0;
1417 case JSONB_TEXTJ:
1418 case JSONB_TEXT5: {
1419 j = i+n;
1420 k = j+sz;
1421 while( j<k ){
1422 if( !jsonIsOk[z[j]] && z[j]!='\'' ){
1423 if( z[j]=='"' ){
1424 if( x==JSONB_TEXTJ ) return j+1;
1425 }else if( z[j]<=0x1f ){
1426 /* Control characters in JSON5 string literals are ok */
1427 if( x==JSONB_TEXTJ ) return j+1;
1428 }else if( NEVER(z[j]!='\\') || j+1>=k ){
1429 return j+1;
1430 }else if( strchr("\"\\/bfnrt",z[j+1])!=0 ){
1431 j++;
1432 }else if( z[j+1]=='u' ){
1433 if( j+5>=k ) return j+1;
1434 if( !jsonIs4Hex((const char*)&z[j+2]) ) return j+1;
1435 j++;
1436 }else if( x!=JSONB_TEXT5 ){
1437 return j+1;
1438 }else{
1439 u32 c = 0;
1440 u32 szC = jsonUnescapeOneChar((const char*)&z[j], k-j, &c);
1441 if( c==JSON_INVALID_CHAR ) return j+1;
1442 j += szC - 1;
1445 j++;
1447 return 0;
1449 case JSONB_TEXTRAW: {
1450 return 0;
1452 case JSONB_ARRAY: {
1453 u32 sub;
1454 j = i+n;
1455 k = j+sz;
1456 while( j<k ){
1457 sz = 0;
1458 n = jsonbPayloadSize(pParse, j, &sz);
1459 if( n==0 ) return j+1;
1460 if( j+n+sz>k ) return j+1;
1461 sub = jsonbValidityCheck(pParse, j, j+n+sz, iDepth+1);
1462 if( sub ) return sub;
1463 j += n + sz;
1465 assert( j==k );
1466 return 0;
1468 case JSONB_OBJECT: {
1469 u32 cnt = 0;
1470 u32 sub;
1471 j = i+n;
1472 k = j+sz;
1473 while( j<k ){
1474 sz = 0;
1475 n = jsonbPayloadSize(pParse, j, &sz);
1476 if( n==0 ) return j+1;
1477 if( j+n+sz>k ) return j+1;
1478 if( (cnt & 1)==0 ){
1479 x = z[j] & 0x0f;
1480 if( x<JSONB_TEXT || x>JSONB_TEXTRAW ) return j+1;
1482 sub = jsonbValidityCheck(pParse, j, j+n+sz, iDepth+1);
1483 if( sub ) return sub;
1484 cnt++;
1485 j += n + sz;
1487 assert( j==k );
1488 if( (cnt & 1)!=0 ) return j+1;
1489 return 0;
1491 default: {
1492 return i+1;
1498 ** Translate a single element of JSON text at pParse->zJson[i] into
1499 ** its equivalent binary JSONB representation. Append the translation into
1500 ** pParse->aBlob[] beginning at pParse->nBlob. The size of
1501 ** pParse->aBlob[] is increased as necessary.
1503 ** Return the index of the first character past the end of the element parsed,
1504 ** or one of the following special result codes:
1506 ** 0 End of input
1507 ** -1 Syntax error or OOM
1508 ** -2 '}' seen \
1509 ** -3 ']' seen \___ For these returns, pParse->iErr is set to
1510 ** -4 ',' seen / the index in zJson[] of the seen character
1511 ** -5 ':' seen /
1513 static int jsonTranslateTextToBlob(JsonParse *pParse, u32 i){
1514 char c;
1515 u32 j;
1516 u32 iThis, iStart;
1517 int x;
1518 u8 t;
1519 const char *z = pParse->zJson;
1520 json_parse_restart:
1521 switch( (u8)z[i] ){
1522 case '{': {
1523 /* Parse object */
1524 iThis = pParse->nBlob;
1525 jsonBlobAppendNode(pParse, JSONB_OBJECT, pParse->nJson-i, 0);
1526 if( ++pParse->iDepth > JSON_MAX_DEPTH ){
1527 pParse->iErr = i;
1528 return -1;
1530 iStart = pParse->nBlob;
1531 for(j=i+1;;j++){
1532 u32 iBlob = pParse->nBlob;
1533 x = jsonTranslateTextToBlob(pParse, j);
1534 if( x<=0 ){
1535 int op;
1536 if( x==(-2) ){
1537 j = pParse->iErr;
1538 if( pParse->nBlob!=(u32)iStart ) pParse->hasNonstd = 1;
1539 break;
1541 j += json5Whitespace(&z[j]);
1542 op = JSONB_TEXT;
1543 if( sqlite3JsonId1(z[j])
1544 || (z[j]=='\\' && jsonIs4HexB(&z[j+1], &op))
1546 int k = j+1;
1547 while( (sqlite3JsonId2(z[k]) && json5Whitespace(&z[k])==0)
1548 || (z[k]=='\\' && jsonIs4HexB(&z[k+1], &op))
1550 k++;
1552 assert( iBlob==pParse->nBlob );
1553 jsonBlobAppendNode(pParse, op, k-j, &z[j]);
1554 pParse->hasNonstd = 1;
1555 x = k;
1556 }else{
1557 if( x!=-1 ) pParse->iErr = j;
1558 return -1;
1561 if( pParse->oom ) return -1;
1562 t = pParse->aBlob[iBlob] & 0x0f;
1563 if( t<JSONB_TEXT || t>JSONB_TEXTRAW ){
1564 pParse->iErr = j;
1565 return -1;
1567 j = x;
1568 if( z[j]==':' ){
1569 j++;
1570 }else{
1571 if( jsonIsspace(z[j]) ){
1572 /* strspn() is not helpful here */
1573 do{ j++; }while( jsonIsspace(z[j]) );
1574 if( z[j]==':' ){
1575 j++;
1576 goto parse_object_value;
1579 x = jsonTranslateTextToBlob(pParse, j);
1580 if( x!=(-5) ){
1581 if( x!=(-1) ) pParse->iErr = j;
1582 return -1;
1584 j = pParse->iErr+1;
1586 parse_object_value:
1587 x = jsonTranslateTextToBlob(pParse, j);
1588 if( x<=0 ){
1589 if( x!=(-1) ) pParse->iErr = j;
1590 return -1;
1592 j = x;
1593 if( z[j]==',' ){
1594 continue;
1595 }else if( z[j]=='}' ){
1596 break;
1597 }else{
1598 if( jsonIsspace(z[j]) ){
1599 j += 1 + (u32)strspn(&z[j+1], jsonSpaces);
1600 if( z[j]==',' ){
1601 continue;
1602 }else if( z[j]=='}' ){
1603 break;
1606 x = jsonTranslateTextToBlob(pParse, j);
1607 if( x==(-4) ){
1608 j = pParse->iErr;
1609 continue;
1611 if( x==(-2) ){
1612 j = pParse->iErr;
1613 break;
1616 pParse->iErr = j;
1617 return -1;
1619 jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart);
1620 pParse->iDepth--;
1621 return j+1;
1623 case '[': {
1624 /* Parse array */
1625 iThis = pParse->nBlob;
1626 assert( i<=pParse->nJson );
1627 jsonBlobAppendNode(pParse, JSONB_ARRAY, pParse->nJson - i, 0);
1628 iStart = pParse->nBlob;
1629 if( pParse->oom ) return -1;
1630 if( ++pParse->iDepth > JSON_MAX_DEPTH ){
1631 pParse->iErr = i;
1632 return -1;
1634 for(j=i+1;;j++){
1635 x = jsonTranslateTextToBlob(pParse, j);
1636 if( x<=0 ){
1637 if( x==(-3) ){
1638 j = pParse->iErr;
1639 if( pParse->nBlob!=iStart ) pParse->hasNonstd = 1;
1640 break;
1642 if( x!=(-1) ) pParse->iErr = j;
1643 return -1;
1645 j = x;
1646 if( z[j]==',' ){
1647 continue;
1648 }else if( z[j]==']' ){
1649 break;
1650 }else{
1651 if( jsonIsspace(z[j]) ){
1652 j += 1 + (u32)strspn(&z[j+1], jsonSpaces);
1653 if( z[j]==',' ){
1654 continue;
1655 }else if( z[j]==']' ){
1656 break;
1659 x = jsonTranslateTextToBlob(pParse, j);
1660 if( x==(-4) ){
1661 j = pParse->iErr;
1662 continue;
1664 if( x==(-3) ){
1665 j = pParse->iErr;
1666 break;
1669 pParse->iErr = j;
1670 return -1;
1672 jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart);
1673 pParse->iDepth--;
1674 return j+1;
1676 case '\'': {
1677 u8 opcode;
1678 char cDelim;
1679 pParse->hasNonstd = 1;
1680 opcode = JSONB_TEXT;
1681 goto parse_string;
1682 case '"':
1683 /* Parse string */
1684 opcode = JSONB_TEXT;
1685 parse_string:
1686 cDelim = z[i];
1687 j = i+1;
1688 while( 1 /*exit-by-break*/ ){
1689 if( jsonIsOk[(u8)z[j]] ){
1690 if( !jsonIsOk[(u8)z[j+1]] ){
1691 j += 1;
1692 }else if( !jsonIsOk[(u8)z[j+2]] ){
1693 j += 2;
1694 }else{
1695 j += 3;
1696 continue;
1699 c = z[j];
1700 if( c==cDelim ){
1701 break;
1702 }else if( c=='\\' ){
1703 c = z[++j];
1704 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
1705 || c=='n' || c=='r' || c=='t'
1706 || (c=='u' && jsonIs4Hex(&z[j+1])) ){
1707 if( opcode==JSONB_TEXT ) opcode = JSONB_TEXTJ;
1708 }else if( c=='\'' || c=='0' || c=='v' || c=='\n'
1709 || (0xe2==(u8)c && 0x80==(u8)z[j+1]
1710 && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2]))
1711 || (c=='x' && jsonIs2Hex(&z[j+1])) ){
1712 opcode = JSONB_TEXT5;
1713 pParse->hasNonstd = 1;
1714 }else if( c=='\r' ){
1715 if( z[j+1]=='\n' ) j++;
1716 opcode = JSONB_TEXT5;
1717 pParse->hasNonstd = 1;
1718 }else{
1719 pParse->iErr = j;
1720 return -1;
1722 }else if( c<=0x1f ){
1723 if( c==0 ){
1724 pParse->iErr = j;
1725 return -1;
1727 /* Control characters are not allowed in canonical JSON string
1728 ** literals, but are allowed in JSON5 string literals. */
1729 opcode = JSONB_TEXT5;
1730 pParse->hasNonstd = 1;
1731 }else if( c=='"' ){
1732 opcode = JSONB_TEXT5;
1734 j++;
1736 jsonBlobAppendNode(pParse, opcode, j-1-i, &z[i+1]);
1737 return j+1;
1739 case 't': {
1740 if( strncmp(z+i,"true",4)==0 && !sqlite3Isalnum(z[i+4]) ){
1741 jsonBlobAppendOneByte(pParse, JSONB_TRUE);
1742 return i+4;
1744 pParse->iErr = i;
1745 return -1;
1747 case 'f': {
1748 if( strncmp(z+i,"false",5)==0 && !sqlite3Isalnum(z[i+5]) ){
1749 jsonBlobAppendOneByte(pParse, JSONB_FALSE);
1750 return i+5;
1752 pParse->iErr = i;
1753 return -1;
1755 case '+': {
1756 u8 seenE;
1757 pParse->hasNonstd = 1;
1758 t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
1759 goto parse_number;
1760 case '.':
1761 if( sqlite3Isdigit(z[i+1]) ){
1762 pParse->hasNonstd = 1;
1763 t = 0x03; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
1764 seenE = 0;
1765 goto parse_number_2;
1767 pParse->iErr = i;
1768 return -1;
1769 case '-':
1770 case '0':
1771 case '1':
1772 case '2':
1773 case '3':
1774 case '4':
1775 case '5':
1776 case '6':
1777 case '7':
1778 case '8':
1779 case '9':
1780 /* Parse number */
1781 t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
1782 parse_number:
1783 seenE = 0;
1784 assert( '-' < '0' );
1785 assert( '+' < '0' );
1786 assert( '.' < '0' );
1787 c = z[i];
1789 if( c<='0' ){
1790 if( c=='0' ){
1791 if( (z[i+1]=='x' || z[i+1]=='X') && sqlite3Isxdigit(z[i+2]) ){
1792 assert( t==0x00 );
1793 pParse->hasNonstd = 1;
1794 t = 0x01;
1795 for(j=i+3; sqlite3Isxdigit(z[j]); j++){}
1796 goto parse_number_finish;
1797 }else if( sqlite3Isdigit(z[i+1]) ){
1798 pParse->iErr = i+1;
1799 return -1;
1801 }else{
1802 if( !sqlite3Isdigit(z[i+1]) ){
1803 /* JSON5 allows for "+Infinity" and "-Infinity" using exactly
1804 ** that case. SQLite also allows these in any case and it allows
1805 ** "+inf" and "-inf". */
1806 if( (z[i+1]=='I' || z[i+1]=='i')
1807 && sqlite3StrNICmp(&z[i+1], "inf",3)==0
1809 pParse->hasNonstd = 1;
1810 if( z[i]=='-' ){
1811 jsonBlobAppendNode(pParse, JSONB_FLOAT, 6, "-9e999");
1812 }else{
1813 jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999");
1815 return i + (sqlite3StrNICmp(&z[i+4],"inity",5)==0 ? 9 : 4);
1817 if( z[i+1]=='.' ){
1818 pParse->hasNonstd = 1;
1819 t |= 0x01;
1820 goto parse_number_2;
1822 pParse->iErr = i;
1823 return -1;
1825 if( z[i+1]=='0' ){
1826 if( sqlite3Isdigit(z[i+2]) ){
1827 pParse->iErr = i+1;
1828 return -1;
1829 }else if( (z[i+2]=='x' || z[i+2]=='X') && sqlite3Isxdigit(z[i+3]) ){
1830 pParse->hasNonstd = 1;
1831 t |= 0x01;
1832 for(j=i+4; sqlite3Isxdigit(z[j]); j++){}
1833 goto parse_number_finish;
1839 parse_number_2:
1840 for(j=i+1;; j++){
1841 c = z[j];
1842 if( sqlite3Isdigit(c) ) continue;
1843 if( c=='.' ){
1844 if( (t & 0x02)!=0 ){
1845 pParse->iErr = j;
1846 return -1;
1848 t |= 0x02;
1849 continue;
1851 if( c=='e' || c=='E' ){
1852 if( z[j-1]<'0' ){
1853 if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){
1854 pParse->hasNonstd = 1;
1855 t |= 0x01;
1856 }else{
1857 pParse->iErr = j;
1858 return -1;
1861 if( seenE ){
1862 pParse->iErr = j;
1863 return -1;
1865 t |= 0x02;
1866 seenE = 1;
1867 c = z[j+1];
1868 if( c=='+' || c=='-' ){
1869 j++;
1870 c = z[j+1];
1872 if( c<'0' || c>'9' ){
1873 pParse->iErr = j;
1874 return -1;
1876 continue;
1878 break;
1880 if( z[j-1]<'0' ){
1881 if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){
1882 pParse->hasNonstd = 1;
1883 t |= 0x01;
1884 }else{
1885 pParse->iErr = j;
1886 return -1;
1889 parse_number_finish:
1890 assert( JSONB_INT+0x01==JSONB_INT5 );
1891 assert( JSONB_FLOAT+0x01==JSONB_FLOAT5 );
1892 assert( JSONB_INT+0x02==JSONB_FLOAT );
1893 if( z[i]=='+' ) i++;
1894 jsonBlobAppendNode(pParse, JSONB_INT+t, j-i, &z[i]);
1895 return j;
1897 case '}': {
1898 pParse->iErr = i;
1899 return -2; /* End of {...} */
1901 case ']': {
1902 pParse->iErr = i;
1903 return -3; /* End of [...] */
1905 case ',': {
1906 pParse->iErr = i;
1907 return -4; /* List separator */
1909 case ':': {
1910 pParse->iErr = i;
1911 return -5; /* Object label/value separator */
1913 case 0: {
1914 return 0; /* End of file */
1916 case 0x09:
1917 case 0x0a:
1918 case 0x0d:
1919 case 0x20: {
1920 i += 1 + (u32)strspn(&z[i+1], jsonSpaces);
1921 goto json_parse_restart;
1923 case 0x0b:
1924 case 0x0c:
1925 case '/':
1926 case 0xc2:
1927 case 0xe1:
1928 case 0xe2:
1929 case 0xe3:
1930 case 0xef: {
1931 j = json5Whitespace(&z[i]);
1932 if( j>0 ){
1933 i += j;
1934 pParse->hasNonstd = 1;
1935 goto json_parse_restart;
1937 pParse->iErr = i;
1938 return -1;
1940 case 'n': {
1941 if( strncmp(z+i,"null",4)==0 && !sqlite3Isalnum(z[i+4]) ){
1942 jsonBlobAppendOneByte(pParse, JSONB_NULL);
1943 return i+4;
1945 /* fall-through into the default case that checks for NaN */
1946 /* no break */ deliberate_fall_through
1948 default: {
1949 u32 k;
1950 int nn;
1951 c = z[i];
1952 for(k=0; k<sizeof(aNanInfName)/sizeof(aNanInfName[0]); k++){
1953 if( c!=aNanInfName[k].c1 && c!=aNanInfName[k].c2 ) continue;
1954 nn = aNanInfName[k].n;
1955 if( sqlite3StrNICmp(&z[i], aNanInfName[k].zMatch, nn)!=0 ){
1956 continue;
1958 if( sqlite3Isalnum(z[i+nn]) ) continue;
1959 if( aNanInfName[k].eType==JSONB_FLOAT ){
1960 jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999");
1961 }else{
1962 jsonBlobAppendOneByte(pParse, JSONB_NULL);
1964 pParse->hasNonstd = 1;
1965 return i + nn;
1967 pParse->iErr = i;
1968 return -1; /* Syntax error */
1970 } /* End switch(z[i]) */
1975 ** Parse a complete JSON string. Return 0 on success or non-zero if there
1976 ** are any errors. If an error occurs, free all memory held by pParse,
1977 ** but not pParse itself.
1979 ** pParse must be initialized to an empty parse object prior to calling
1980 ** this routine.
1982 static int jsonConvertTextToBlob(
1983 JsonParse *pParse, /* Initialize and fill this JsonParse object */
1984 sqlite3_context *pCtx /* Report errors here */
1986 int i;
1987 const char *zJson = pParse->zJson;
1988 i = jsonTranslateTextToBlob(pParse, 0);
1989 if( pParse->oom ) i = -1;
1990 if( i>0 ){
1991 #ifdef SQLITE_DEBUG
1992 assert( pParse->iDepth==0 );
1993 if( sqlite3Config.bJsonSelfcheck ){
1994 assert( jsonbValidityCheck(pParse, 0, pParse->nBlob, 0)==0 );
1996 #endif
1997 while( jsonIsspace(zJson[i]) ) i++;
1998 if( zJson[i] ){
1999 i += json5Whitespace(&zJson[i]);
2000 if( zJson[i] ){
2001 if( pCtx ) sqlite3_result_error(pCtx, "malformed JSON", -1);
2002 jsonParseReset(pParse);
2003 return 1;
2005 pParse->hasNonstd = 1;
2008 if( i<=0 ){
2009 if( pCtx!=0 ){
2010 if( pParse->oom ){
2011 sqlite3_result_error_nomem(pCtx);
2012 }else{
2013 sqlite3_result_error(pCtx, "malformed JSON", -1);
2016 jsonParseReset(pParse);
2017 return 1;
2019 return 0;
2023 ** The input string pStr is a well-formed JSON text string. Convert
2024 ** this into the JSONB format and make it the return value of the
2025 ** SQL function.
2027 static void jsonReturnStringAsBlob(JsonString *pStr){
2028 JsonParse px;
2029 memset(&px, 0, sizeof(px));
2030 jsonStringTerminate(pStr);
2031 if( pStr->eErr ){
2032 sqlite3_result_error_nomem(pStr->pCtx);
2033 return;
2035 px.zJson = pStr->zBuf;
2036 px.nJson = pStr->nUsed;
2037 px.db = sqlite3_context_db_handle(pStr->pCtx);
2038 (void)jsonTranslateTextToBlob(&px, 0);
2039 if( px.oom ){
2040 sqlite3DbFree(px.db, px.aBlob);
2041 sqlite3_result_error_nomem(pStr->pCtx);
2042 }else{
2043 assert( px.nBlobAlloc>0 );
2044 assert( !px.bReadOnly );
2045 sqlite3_result_blob(pStr->pCtx, px.aBlob, px.nBlob, SQLITE_DYNAMIC);
2049 /* The byte at index i is a node type-code. This routine
2050 ** determines the payload size for that node and writes that
2051 ** payload size in to *pSz. It returns the offset from i to the
2052 ** beginning of the payload. Return 0 on error.
2054 static u32 jsonbPayloadSize(const JsonParse *pParse, u32 i, u32 *pSz){
2055 u8 x;
2056 u32 sz;
2057 u32 n;
2058 if( NEVER(i>pParse->nBlob) ){
2059 *pSz = 0;
2060 return 0;
2062 x = pParse->aBlob[i]>>4;
2063 if( x<=11 ){
2064 sz = x;
2065 n = 1;
2066 }else if( x==12 ){
2067 if( i+1>=pParse->nBlob ){
2068 *pSz = 0;
2069 return 0;
2071 sz = pParse->aBlob[i+1];
2072 n = 2;
2073 }else if( x==13 ){
2074 if( i+2>=pParse->nBlob ){
2075 *pSz = 0;
2076 return 0;
2078 sz = (pParse->aBlob[i+1]<<8) + pParse->aBlob[i+2];
2079 n = 3;
2080 }else if( x==14 ){
2081 if( i+4>=pParse->nBlob ){
2082 *pSz = 0;
2083 return 0;
2085 sz = ((u32)pParse->aBlob[i+1]<<24) + (pParse->aBlob[i+2]<<16) +
2086 (pParse->aBlob[i+3]<<8) + pParse->aBlob[i+4];
2087 n = 5;
2088 }else{
2089 if( i+8>=pParse->nBlob
2090 || pParse->aBlob[i+1]!=0
2091 || pParse->aBlob[i+2]!=0
2092 || pParse->aBlob[i+3]!=0
2093 || pParse->aBlob[i+4]!=0
2095 *pSz = 0;
2096 return 0;
2098 sz = (pParse->aBlob[i+5]<<24) + (pParse->aBlob[i+6]<<16) +
2099 (pParse->aBlob[i+7]<<8) + pParse->aBlob[i+8];
2100 n = 9;
2102 if( (i64)i+sz+n > pParse->nBlob
2103 && (i64)i+sz+n > pParse->nBlob-pParse->delta
2105 sz = 0;
2106 n = 0;
2108 *pSz = sz;
2109 return n;
2114 ** Translate the binary JSONB representation of JSON beginning at
2115 ** pParse->aBlob[i] into a JSON text string. Append the JSON
2116 ** text onto the end of pOut. Return the index in pParse->aBlob[]
2117 ** of the first byte past the end of the element that is translated.
2119 ** If an error is detected in the BLOB input, the pOut->eErr flag
2120 ** might get set to JSTRING_MALFORMED. But not all BLOB input errors
2121 ** are detected. So a malformed JSONB input might either result
2122 ** in an error, or in incorrect JSON.
2124 ** The pOut->eErr JSTRING_OOM flag is set on a OOM.
2126 static u32 jsonTranslateBlobToText(
2127 const JsonParse *pParse, /* the complete parse of the JSON */
2128 u32 i, /* Start rendering at this index */
2129 JsonString *pOut /* Write JSON here */
2131 u32 sz, n, j, iEnd;
2133 n = jsonbPayloadSize(pParse, i, &sz);
2134 if( n==0 ){
2135 pOut->eErr |= JSTRING_MALFORMED;
2136 return pParse->nBlob+1;
2138 switch( pParse->aBlob[i] & 0x0f ){
2139 case JSONB_NULL: {
2140 jsonAppendRawNZ(pOut, "null", 4);
2141 return i+1;
2143 case JSONB_TRUE: {
2144 jsonAppendRawNZ(pOut, "true", 4);
2145 return i+1;
2147 case JSONB_FALSE: {
2148 jsonAppendRawNZ(pOut, "false", 5);
2149 return i+1;
2151 case JSONB_INT:
2152 case JSONB_FLOAT: {
2153 if( sz==0 ) goto malformed_jsonb;
2154 jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n], sz);
2155 break;
2157 case JSONB_INT5: { /* Integer literal in hexadecimal notation */
2158 u32 k = 2;
2159 sqlite3_uint64 u = 0;
2160 const char *zIn = (const char*)&pParse->aBlob[i+n];
2161 int bOverflow = 0;
2162 if( sz==0 ) goto malformed_jsonb;
2163 if( zIn[0]=='-' ){
2164 jsonAppendChar(pOut, '-');
2165 k++;
2166 }else if( zIn[0]=='+' ){
2167 k++;
2169 for(; k<sz; k++){
2170 if( !sqlite3Isxdigit(zIn[k]) ){
2171 pOut->eErr |= JSTRING_MALFORMED;
2172 break;
2173 }else if( (u>>60)!=0 ){
2174 bOverflow = 1;
2175 }else{
2176 u = u*16 + sqlite3HexToInt(zIn[k]);
2179 jsonPrintf(100,pOut,bOverflow?"9.0e999":"%llu", u);
2180 break;
2182 case JSONB_FLOAT5: { /* Float literal missing digits beside "." */
2183 u32 k = 0;
2184 const char *zIn = (const char*)&pParse->aBlob[i+n];
2185 if( sz==0 ) goto malformed_jsonb;
2186 if( zIn[0]=='-' ){
2187 jsonAppendChar(pOut, '-');
2188 k++;
2190 if( zIn[k]=='.' ){
2191 jsonAppendChar(pOut, '0');
2193 for(; k<sz; k++){
2194 jsonAppendChar(pOut, zIn[k]);
2195 if( zIn[k]=='.' && (k+1==sz || !sqlite3Isdigit(zIn[k+1])) ){
2196 jsonAppendChar(pOut, '0');
2199 break;
2201 case JSONB_TEXT:
2202 case JSONB_TEXTJ: {
2203 jsonAppendChar(pOut, '"');
2204 jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n], sz);
2205 jsonAppendChar(pOut, '"');
2206 break;
2208 case JSONB_TEXT5: {
2209 const char *zIn;
2210 u32 k;
2211 u32 sz2 = sz;
2212 zIn = (const char*)&pParse->aBlob[i+n];
2213 jsonAppendChar(pOut, '"');
2214 while( sz2>0 ){
2215 for(k=0; k<sz2 && (jsonIsOk[(u8)zIn[k]] || zIn[k]=='\''); k++){}
2216 if( k>0 ){
2217 jsonAppendRawNZ(pOut, zIn, k);
2218 if( k>=sz2 ){
2219 break;
2221 zIn += k;
2222 sz2 -= k;
2224 if( zIn[0]=='"' ){
2225 jsonAppendRawNZ(pOut, "\\\"", 2);
2226 zIn++;
2227 sz2--;
2228 continue;
2230 if( zIn[0]<=0x1f ){
2231 if( pOut->nUsed+7>pOut->nAlloc && jsonStringGrow(pOut,7) ) break;
2232 jsonAppendControlChar(pOut, zIn[0]);
2233 zIn++;
2234 sz2--;
2235 continue;
2237 assert( zIn[0]=='\\' );
2238 assert( sz2>=1 );
2239 if( sz2<2 ){
2240 pOut->eErr |= JSTRING_MALFORMED;
2241 break;
2243 switch( (u8)zIn[1] ){
2244 case '\'':
2245 jsonAppendChar(pOut, '\'');
2246 break;
2247 case 'v':
2248 jsonAppendRawNZ(pOut, "\\u0009", 6);
2249 break;
2250 case 'x':
2251 if( sz2<4 ){
2252 pOut->eErr |= JSTRING_MALFORMED;
2253 sz2 = 2;
2254 break;
2256 jsonAppendRawNZ(pOut, "\\u00", 4);
2257 jsonAppendRawNZ(pOut, &zIn[2], 2);
2258 zIn += 2;
2259 sz2 -= 2;
2260 break;
2261 case '0':
2262 jsonAppendRawNZ(pOut, "\\u0000", 6);
2263 break;
2264 case '\r':
2265 if( sz2>2 && zIn[2]=='\n' ){
2266 zIn++;
2267 sz2--;
2269 break;
2270 case '\n':
2271 break;
2272 case 0xe2:
2273 /* '\' followed by either U+2028 or U+2029 is ignored as
2274 ** whitespace. Not that in UTF8, U+2028 is 0xe2 0x80 0x29.
2275 ** U+2029 is the same except for the last byte */
2276 if( sz2<4
2277 || 0x80!=(u8)zIn[2]
2278 || (0xa8!=(u8)zIn[3] && 0xa9!=(u8)zIn[3])
2280 pOut->eErr |= JSTRING_MALFORMED;
2281 sz2 = 2;
2282 break;
2284 zIn += 2;
2285 sz2 -= 2;
2286 break;
2287 default:
2288 jsonAppendRawNZ(pOut, zIn, 2);
2289 break;
2291 assert( sz2>=2 );
2292 zIn += 2;
2293 sz2 -= 2;
2295 jsonAppendChar(pOut, '"');
2296 break;
2298 case JSONB_TEXTRAW: {
2299 jsonAppendString(pOut, (const char*)&pParse->aBlob[i+n], sz);
2300 break;
2302 case JSONB_ARRAY: {
2303 jsonAppendChar(pOut, '[');
2304 j = i+n;
2305 iEnd = j+sz;
2306 while( j<iEnd && pOut->eErr==0 ){
2307 j = jsonTranslateBlobToText(pParse, j, pOut);
2308 jsonAppendChar(pOut, ',');
2310 if( j>iEnd ) pOut->eErr |= JSTRING_MALFORMED;
2311 if( sz>0 ) jsonStringTrimOneChar(pOut);
2312 jsonAppendChar(pOut, ']');
2313 break;
2315 case JSONB_OBJECT: {
2316 int x = 0;
2317 jsonAppendChar(pOut, '{');
2318 j = i+n;
2319 iEnd = j+sz;
2320 while( j<iEnd && pOut->eErr==0 ){
2321 j = jsonTranslateBlobToText(pParse, j, pOut);
2322 jsonAppendChar(pOut, (x++ & 1) ? ',' : ':');
2324 if( (x & 1)!=0 || j>iEnd ) pOut->eErr |= JSTRING_MALFORMED;
2325 if( sz>0 ) jsonStringTrimOneChar(pOut);
2326 jsonAppendChar(pOut, '}');
2327 break;
2330 default: {
2331 malformed_jsonb:
2332 pOut->eErr |= JSTRING_MALFORMED;
2333 break;
2336 return i+n+sz;
2339 /* Return true if the input pJson
2341 ** For performance reasons, this routine does not do a detailed check of the
2342 ** input BLOB to ensure that it is well-formed. Hence, false positives are
2343 ** possible. False negatives should never occur, however.
2345 static int jsonFuncArgMightBeBinary(sqlite3_value *pJson){
2346 u32 sz, n;
2347 const u8 *aBlob;
2348 int nBlob;
2349 JsonParse s;
2350 if( sqlite3_value_type(pJson)!=SQLITE_BLOB ) return 0;
2351 aBlob = sqlite3_value_blob(pJson);
2352 nBlob = sqlite3_value_bytes(pJson);
2353 if( nBlob<1 ) return 0;
2354 if( NEVER(aBlob==0) || (aBlob[0] & 0x0f)>JSONB_OBJECT ) return 0;
2355 memset(&s, 0, sizeof(s));
2356 s.aBlob = (u8*)aBlob;
2357 s.nBlob = nBlob;
2358 n = jsonbPayloadSize(&s, 0, &sz);
2359 if( n==0 ) return 0;
2360 if( sz+n!=(u32)nBlob ) return 0;
2361 if( (aBlob[0] & 0x0f)<=JSONB_FALSE && sz>0 ) return 0;
2362 return sz+n==(u32)nBlob;
2366 ** Given that a JSONB_ARRAY object starts at offset i, return
2367 ** the number of entries in that array.
2369 static u32 jsonbArrayCount(JsonParse *pParse, u32 iRoot){
2370 u32 n, sz, i, iEnd;
2371 u32 k = 0;
2372 n = jsonbPayloadSize(pParse, iRoot, &sz);
2373 iEnd = iRoot+n+sz;
2374 for(i=iRoot+n; n>0 && i<iEnd; i+=sz+n, k++){
2375 n = jsonbPayloadSize(pParse, i, &sz);
2377 return k;
2381 ** Edit the payload size of the element at iRoot by the amount in
2382 ** pParse->delta.
2384 static void jsonAfterEditSizeAdjust(JsonParse *pParse, u32 iRoot){
2385 u32 sz = 0;
2386 u32 nBlob;
2387 assert( pParse->delta!=0 );
2388 assert( pParse->nBlobAlloc >= pParse->nBlob );
2389 nBlob = pParse->nBlob;
2390 pParse->nBlob = pParse->nBlobAlloc;
2391 (void)jsonbPayloadSize(pParse, iRoot, &sz);
2392 pParse->nBlob = nBlob;
2393 sz += pParse->delta;
2394 pParse->delta += jsonBlobChangePayloadSize(pParse, iRoot, sz);
2398 ** Modify the JSONB blob at pParse->aBlob by removing nDel bytes of
2399 ** content beginning at iDel, and replacing them with nIns bytes of
2400 ** content given by aIns.
2402 ** nDel may be zero, in which case no bytes are removed. But iDel is
2403 ** still important as new bytes will be insert beginning at iDel.
2405 ** aIns may be zero, in which case space is created to hold nIns bytes
2406 ** beginning at iDel, but that space is uninitialized.
2408 ** Set pParse->oom if an OOM occurs.
2410 static void jsonBlobEdit(
2411 JsonParse *pParse, /* The JSONB to be modified is in pParse->aBlob */
2412 u32 iDel, /* First byte to be removed */
2413 u32 nDel, /* Number of bytes to remove */
2414 const u8 *aIns, /* Content to insert */
2415 u32 nIns /* Bytes of content to insert */
2417 i64 d = (i64)nIns - (i64)nDel;
2418 if( d!=0 ){
2419 if( pParse->nBlob + d > pParse->nBlobAlloc ){
2420 jsonBlobExpand(pParse, pParse->nBlob+d);
2421 if( pParse->oom ) return;
2423 memmove(&pParse->aBlob[iDel+nIns],
2424 &pParse->aBlob[iDel+nDel],
2425 pParse->nBlob - (iDel+nDel));
2426 pParse->nBlob += d;
2427 pParse->delta += d;
2429 if( nIns && aIns ) memcpy(&pParse->aBlob[iDel], aIns, nIns);
2433 ** Return the number of escaped newlines to be ignored.
2434 ** An escaped newline is a one of the following byte sequences:
2436 ** 0x5c 0x0a
2437 ** 0x5c 0x0d
2438 ** 0x5c 0x0d 0x0a
2439 ** 0x5c 0xe2 0x80 0xa8
2440 ** 0x5c 0xe2 0x80 0xa9
2442 static u32 jsonBytesToBypass(const char *z, u32 n){
2443 u32 i = 0;
2444 while( i+1<n ){
2445 if( z[i]!='\\' ) return i;
2446 if( z[i+1]=='\n' ){
2447 i += 2;
2448 continue;
2450 if( z[i+1]=='\r' ){
2451 if( i+2<n && z[i+2]=='\n' ){
2452 i += 3;
2453 }else{
2454 i += 2;
2456 continue;
2458 if( 0xe2==(u8)z[i+1]
2459 && i+3<n
2460 && 0x80==(u8)z[i+2]
2461 && (0xa8==(u8)z[i+3] || 0xa9==(u8)z[i+3])
2463 i += 4;
2464 continue;
2466 break;
2468 return i;
2472 ** Input z[0..n] defines JSON escape sequence including the leading '\\'.
2473 ** Decode that escape sequence into a single character. Write that
2474 ** character into *piOut. Return the number of bytes in the escape sequence.
2476 ** If there is a syntax error of some kind (for example too few characters
2477 ** after the '\\' to complete the encoding) then *piOut is set to
2478 ** JSON_INVALID_CHAR.
2480 static u32 jsonUnescapeOneChar(const char *z, u32 n, u32 *piOut){
2481 assert( n>0 );
2482 assert( z[0]=='\\' );
2483 if( n<2 ){
2484 *piOut = JSON_INVALID_CHAR;
2485 return n;
2487 switch( (u8)z[1] ){
2488 case 'u': {
2489 u32 v, vlo;
2490 if( n<6 ){
2491 *piOut = JSON_INVALID_CHAR;
2492 return n;
2494 v = jsonHexToInt4(&z[2]);
2495 if( (v & 0xfc00)==0xd800
2496 && n>=12
2497 && z[6]=='\\'
2498 && z[7]=='u'
2499 && ((vlo = jsonHexToInt4(&z[8]))&0xfc00)==0xdc00
2501 *piOut = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
2502 return 12;
2503 }else{
2504 *piOut = v;
2505 return 6;
2508 case 'b': { *piOut = '\b'; return 2; }
2509 case 'f': { *piOut = '\f'; return 2; }
2510 case 'n': { *piOut = '\n'; return 2; }
2511 case 'r': { *piOut = '\r'; return 2; }
2512 case 't': { *piOut = '\t'; return 2; }
2513 case 'v': { *piOut = '\v'; return 2; }
2514 case '0': { *piOut = 0; return 2; }
2515 case '\'':
2516 case '"':
2517 case '/':
2518 case '\\':{ *piOut = z[1]; return 2; }
2519 case 'x': {
2520 if( n<4 ){
2521 *piOut = JSON_INVALID_CHAR;
2522 return n;
2524 *piOut = (jsonHexToInt(z[2])<<4) | jsonHexToInt(z[3]);
2525 return 4;
2527 case 0xe2:
2528 case '\r':
2529 case '\n': {
2530 u32 nSkip = jsonBytesToBypass(z, n);
2531 if( nSkip==0 ){
2532 *piOut = JSON_INVALID_CHAR;
2533 return n;
2534 }else if( nSkip==n ){
2535 *piOut = 0;
2536 return n;
2537 }else if( z[nSkip]=='\\' ){
2538 return nSkip + jsonUnescapeOneChar(&z[nSkip], n-nSkip, piOut);
2539 }else{
2540 int sz = sqlite3Utf8ReadLimited((u8*)&z[nSkip], n-nSkip, piOut);
2541 return nSkip + sz;
2544 default: {
2545 *piOut = JSON_INVALID_CHAR;
2546 return 2;
2553 ** Compare two object labels. Return 1 if they are equal and
2554 ** 0 if they differ.
2556 ** In this version, we know that one or the other or both of the
2557 ** two comparands contains an escape sequence.
2559 static SQLITE_NOINLINE int jsonLabelCompareEscaped(
2560 const char *zLeft, /* The left label */
2561 u32 nLeft, /* Size of the left label in bytes */
2562 int rawLeft, /* True if zLeft contains no escapes */
2563 const char *zRight, /* The right label */
2564 u32 nRight, /* Size of the right label in bytes */
2565 int rawRight /* True if zRight is escape-free */
2567 u32 cLeft, cRight;
2568 assert( rawLeft==0 || rawRight==0 );
2569 while( 1 /*exit-by-return*/ ){
2570 if( nLeft==0 ){
2571 cLeft = 0;
2572 }else if( rawLeft || zLeft[0]!='\\' ){
2573 cLeft = ((u8*)zLeft)[0];
2574 if( cLeft>=0xc0 ){
2575 int sz = sqlite3Utf8ReadLimited((u8*)zLeft, nLeft, &cLeft);
2576 zLeft += sz;
2577 nLeft -= sz;
2578 }else{
2579 zLeft++;
2580 nLeft--;
2582 }else{
2583 u32 n = jsonUnescapeOneChar(zLeft, nLeft, &cLeft);
2584 zLeft += n;
2585 assert( n<=nLeft );
2586 nLeft -= n;
2588 if( nRight==0 ){
2589 cRight = 0;
2590 }else if( rawRight || zRight[0]!='\\' ){
2591 cRight = ((u8*)zRight)[0];
2592 if( cRight>=0xc0 ){
2593 int sz = sqlite3Utf8ReadLimited((u8*)zRight, nRight, &cRight);
2594 zRight += sz;
2595 nRight -= sz;
2596 }else{
2597 zRight++;
2598 nRight--;
2600 }else{
2601 u32 n = jsonUnescapeOneChar(zRight, nRight, &cRight);
2602 zRight += n;
2603 assert( n<=nRight );
2604 nRight -= n;
2606 if( cLeft!=cRight ) return 0;
2607 if( cLeft==0 ) return 1;
2612 ** Compare two object labels. Return 1 if they are equal and
2613 ** 0 if they differ. Return -1 if an OOM occurs.
2615 static int jsonLabelCompare(
2616 const char *zLeft, /* The left label */
2617 u32 nLeft, /* Size of the left label in bytes */
2618 int rawLeft, /* True if zLeft contains no escapes */
2619 const char *zRight, /* The right label */
2620 u32 nRight, /* Size of the right label in bytes */
2621 int rawRight /* True if zRight is escape-free */
2623 if( rawLeft && rawRight ){
2624 /* Simpliest case: Neither label contains escapes. A simple
2625 ** memcmp() is sufficient. */
2626 if( nLeft!=nRight ) return 0;
2627 return memcmp(zLeft, zRight, nLeft)==0;
2628 }else{
2629 return jsonLabelCompareEscaped(zLeft, nLeft, rawLeft,
2630 zRight, nRight, rawRight);
2635 ** Error returns from jsonLookupStep()
2637 #define JSON_LOOKUP_ERROR 0xffffffff
2638 #define JSON_LOOKUP_NOTFOUND 0xfffffffe
2639 #define JSON_LOOKUP_PATHERROR 0xfffffffd
2640 #define JSON_LOOKUP_ISERROR(x) ((x)>=JSON_LOOKUP_PATHERROR)
2642 /* Forward declaration */
2643 static u32 jsonLookupStep(JsonParse*,u32,const char*,u32);
2646 /* This helper routine for jsonLookupStep() populates pIns with
2647 ** binary data that is to be inserted into pParse.
2649 ** In the common case, pIns just points to pParse->aIns and pParse->nIns.
2650 ** But if the zPath of the original edit operation includes path elements
2651 ** that go deeper, additional substructure must be created.
2653 ** For example:
2655 ** json_insert('{}', '$.a.b.c', 123);
2657 ** The search stops at '$.a' But additional substructure must be
2658 ** created for the ".b.c" part of the patch so that the final result
2659 ** is: {"a":{"b":{"c"::123}}}. This routine populates pIns with
2660 ** the binary equivalent of {"b":{"c":123}} so that it can be inserted.
2662 ** The caller is responsible for resetting pIns when it has finished
2663 ** using the substructure.
2665 static u32 jsonCreateEditSubstructure(
2666 JsonParse *pParse, /* The original JSONB that is being edited */
2667 JsonParse *pIns, /* Populate this with the blob data to insert */
2668 const char *zTail /* Tail of the path that determins substructure */
2670 static const u8 emptyObject[] = { JSONB_ARRAY, JSONB_OBJECT };
2671 int rc;
2672 memset(pIns, 0, sizeof(*pIns));
2673 pIns->db = pParse->db;
2674 if( zTail[0]==0 ){
2675 /* No substructure. Just insert what is given in pParse. */
2676 pIns->aBlob = pParse->aIns;
2677 pIns->nBlob = pParse->nIns;
2678 rc = 0;
2679 }else{
2680 /* Construct the binary substructure */
2681 pIns->nBlob = 1;
2682 pIns->aBlob = (u8*)&emptyObject[zTail[0]=='.'];
2683 pIns->eEdit = pParse->eEdit;
2684 pIns->nIns = pParse->nIns;
2685 pIns->aIns = pParse->aIns;
2686 rc = jsonLookupStep(pIns, 0, zTail, 0);
2687 pParse->oom |= pIns->oom;
2689 return rc; /* Error code only */
2693 ** Search along zPath to find the Json element specified. Return an
2694 ** index into pParse->aBlob[] for the start of that element's value.
2696 ** If the value found by this routine is the value half of label/value pair
2697 ** within an object, then set pPath->iLabel to the start of the corresponding
2698 ** label, before returning.
2700 ** Return one of the JSON_LOOKUP error codes if problems are seen.
2702 ** This routine will also modify the blob. If pParse->eEdit is one of
2703 ** JEDIT_DEL, JEDIT_REPL, JEDIT_INS, or JEDIT_SET, then changes might be
2704 ** made to the selected value. If an edit is performed, then the return
2705 ** value does not necessarily point to the select element. If an edit
2706 ** is performed, the return value is only useful for detecting error
2707 ** conditions.
2709 static u32 jsonLookupStep(
2710 JsonParse *pParse, /* The JSON to search */
2711 u32 iRoot, /* Begin the search at this element of aBlob[] */
2712 const char *zPath, /* The path to search */
2713 u32 iLabel /* Label if iRoot is a value of in an object */
2715 u32 i, j, k, nKey, sz, n, iEnd, rc;
2716 const char *zKey;
2717 u8 x;
2719 if( zPath[0]==0 ){
2720 if( pParse->eEdit && jsonBlobMakeEditable(pParse, pParse->nIns) ){
2721 n = jsonbPayloadSize(pParse, iRoot, &sz);
2722 sz += n;
2723 if( pParse->eEdit==JEDIT_DEL ){
2724 if( iLabel>0 ){
2725 sz += iRoot - iLabel;
2726 iRoot = iLabel;
2728 jsonBlobEdit(pParse, iRoot, sz, 0, 0);
2729 }else if( pParse->eEdit==JEDIT_INS ){
2730 /* Already exists, so json_insert() is a no-op */
2731 }else{
2732 /* json_set() or json_replace() */
2733 jsonBlobEdit(pParse, iRoot, sz, pParse->aIns, pParse->nIns);
2736 pParse->iLabel = iLabel;
2737 return iRoot;
2739 if( zPath[0]=='.' ){
2740 int rawKey = 1;
2741 x = pParse->aBlob[iRoot];
2742 zPath++;
2743 if( zPath[0]=='"' ){
2744 zKey = zPath + 1;
2745 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
2746 nKey = i-1;
2747 if( zPath[i] ){
2748 i++;
2749 }else{
2750 return JSON_LOOKUP_PATHERROR;
2752 testcase( nKey==0 );
2753 rawKey = memchr(zKey, '\\', nKey)==0;
2754 }else{
2755 zKey = zPath;
2756 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
2757 nKey = i;
2758 if( nKey==0 ){
2759 return JSON_LOOKUP_PATHERROR;
2762 if( (x & 0x0f)!=JSONB_OBJECT ) return JSON_LOOKUP_NOTFOUND;
2763 n = jsonbPayloadSize(pParse, iRoot, &sz);
2764 j = iRoot + n; /* j is the index of a label */
2765 iEnd = j+sz;
2766 while( j<iEnd ){
2767 int rawLabel;
2768 const char *zLabel;
2769 x = pParse->aBlob[j] & 0x0f;
2770 if( x<JSONB_TEXT || x>JSONB_TEXTRAW ) return JSON_LOOKUP_ERROR;
2771 n = jsonbPayloadSize(pParse, j, &sz);
2772 if( n==0 ) return JSON_LOOKUP_ERROR;
2773 k = j+n; /* k is the index of the label text */
2774 if( k+sz>=iEnd ) return JSON_LOOKUP_ERROR;
2775 zLabel = (const char*)&pParse->aBlob[k];
2776 rawLabel = x==JSONB_TEXT || x==JSONB_TEXTRAW;
2777 if( jsonLabelCompare(zKey, nKey, rawKey, zLabel, sz, rawLabel) ){
2778 u32 v = k+sz; /* v is the index of the value */
2779 if( ((pParse->aBlob[v])&0x0f)>JSONB_OBJECT ) return JSON_LOOKUP_ERROR;
2780 n = jsonbPayloadSize(pParse, v, &sz);
2781 if( n==0 || v+n+sz>iEnd ) return JSON_LOOKUP_ERROR;
2782 assert( j>0 );
2783 rc = jsonLookupStep(pParse, v, &zPath[i], j);
2784 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
2785 return rc;
2787 j = k+sz;
2788 if( ((pParse->aBlob[j])&0x0f)>JSONB_OBJECT ) return JSON_LOOKUP_ERROR;
2789 n = jsonbPayloadSize(pParse, j, &sz);
2790 if( n==0 ) return JSON_LOOKUP_ERROR;
2791 j += n+sz;
2793 if( j>iEnd ) return JSON_LOOKUP_ERROR;
2794 if( pParse->eEdit>=JEDIT_INS ){
2795 u32 nIns; /* Total bytes to insert (label+value) */
2796 JsonParse v; /* BLOB encoding of the value to be inserted */
2797 JsonParse ix; /* Header of the label to be inserted */
2798 testcase( pParse->eEdit==JEDIT_INS );
2799 testcase( pParse->eEdit==JEDIT_SET );
2800 memset(&ix, 0, sizeof(ix));
2801 ix.db = pParse->db;
2802 jsonBlobAppendNode(&ix, rawKey?JSONB_TEXTRAW:JSONB_TEXT5, nKey, 0);
2803 pParse->oom |= ix.oom;
2804 rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i]);
2805 if( !JSON_LOOKUP_ISERROR(rc)
2806 && jsonBlobMakeEditable(pParse, ix.nBlob+nKey+v.nBlob)
2808 assert( !pParse->oom );
2809 nIns = ix.nBlob + nKey + v.nBlob;
2810 jsonBlobEdit(pParse, j, 0, 0, nIns);
2811 if( !pParse->oom ){
2812 assert( pParse->aBlob!=0 ); /* Because pParse->oom!=0 */
2813 assert( ix.aBlob!=0 ); /* Because pPasre->oom!=0 */
2814 memcpy(&pParse->aBlob[j], ix.aBlob, ix.nBlob);
2815 k = j + ix.nBlob;
2816 memcpy(&pParse->aBlob[k], zKey, nKey);
2817 k += nKey;
2818 memcpy(&pParse->aBlob[k], v.aBlob, v.nBlob);
2819 if( ALWAYS(pParse->delta) ) jsonAfterEditSizeAdjust(pParse, iRoot);
2822 jsonParseReset(&v);
2823 jsonParseReset(&ix);
2824 return rc;
2826 }else if( zPath[0]=='[' ){
2827 x = pParse->aBlob[iRoot] & 0x0f;
2828 if( x!=JSONB_ARRAY ) return JSON_LOOKUP_NOTFOUND;
2829 n = jsonbPayloadSize(pParse, iRoot, &sz);
2830 k = 0;
2831 i = 1;
2832 while( sqlite3Isdigit(zPath[i]) ){
2833 k = k*10 + zPath[i] - '0';
2834 i++;
2836 if( i<2 || zPath[i]!=']' ){
2837 if( zPath[1]=='#' ){
2838 k = jsonbArrayCount(pParse, iRoot);
2839 i = 2;
2840 if( zPath[2]=='-' && sqlite3Isdigit(zPath[3]) ){
2841 unsigned int nn = 0;
2842 i = 3;
2844 nn = nn*10 + zPath[i] - '0';
2845 i++;
2846 }while( sqlite3Isdigit(zPath[i]) );
2847 if( nn>k ) return JSON_LOOKUP_NOTFOUND;
2848 k -= nn;
2850 if( zPath[i]!=']' ){
2851 return JSON_LOOKUP_PATHERROR;
2853 }else{
2854 return JSON_LOOKUP_PATHERROR;
2857 j = iRoot+n;
2858 iEnd = j+sz;
2859 while( j<iEnd ){
2860 if( k==0 ){
2861 rc = jsonLookupStep(pParse, j, &zPath[i+1], 0);
2862 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
2863 return rc;
2865 k--;
2866 n = jsonbPayloadSize(pParse, j, &sz);
2867 if( n==0 ) return JSON_LOOKUP_ERROR;
2868 j += n+sz;
2870 if( j>iEnd ) return JSON_LOOKUP_ERROR;
2871 if( k>0 ) return JSON_LOOKUP_NOTFOUND;
2872 if( pParse->eEdit>=JEDIT_INS ){
2873 JsonParse v;
2874 testcase( pParse->eEdit==JEDIT_INS );
2875 testcase( pParse->eEdit==JEDIT_SET );
2876 rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i+1]);
2877 if( !JSON_LOOKUP_ISERROR(rc)
2878 && jsonBlobMakeEditable(pParse, v.nBlob)
2880 assert( !pParse->oom );
2881 jsonBlobEdit(pParse, j, 0, v.aBlob, v.nBlob);
2883 jsonParseReset(&v);
2884 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
2885 return rc;
2887 }else{
2888 return JSON_LOOKUP_PATHERROR;
2890 return JSON_LOOKUP_NOTFOUND;
2894 ** Convert a JSON BLOB into text and make that text the return value
2895 ** of an SQL function.
2897 static void jsonReturnTextJsonFromBlob(
2898 sqlite3_context *ctx,
2899 const u8 *aBlob,
2900 u32 nBlob
2902 JsonParse x;
2903 JsonString s;
2905 if( NEVER(aBlob==0) ) return;
2906 memset(&x, 0, sizeof(x));
2907 x.aBlob = (u8*)aBlob;
2908 x.nBlob = nBlob;
2909 jsonStringInit(&s, ctx);
2910 jsonTranslateBlobToText(&x, 0, &s);
2911 jsonReturnString(&s, 0, 0);
2916 ** Return the value of the BLOB node at index i.
2918 ** If the value is a primitive, return it as an SQL value.
2919 ** If the value is an array or object, return it as either
2920 ** JSON text or the BLOB encoding, depending on the JSON_B flag
2921 ** on the userdata.
2923 static void jsonReturnFromBlob(
2924 JsonParse *pParse, /* Complete JSON parse tree */
2925 u32 i, /* Index of the node */
2926 sqlite3_context *pCtx, /* Return value for this function */
2927 int textOnly /* return text JSON. Disregard user-data */
2929 u32 n, sz;
2930 int rc;
2931 sqlite3 *db = sqlite3_context_db_handle(pCtx);
2933 n = jsonbPayloadSize(pParse, i, &sz);
2934 if( n==0 ){
2935 sqlite3_result_error(pCtx, "malformed JSON", -1);
2936 return;
2938 switch( pParse->aBlob[i] & 0x0f ){
2939 case JSONB_NULL: {
2940 if( sz ) goto returnfromblob_malformed;
2941 sqlite3_result_null(pCtx);
2942 break;
2944 case JSONB_TRUE: {
2945 if( sz ) goto returnfromblob_malformed;
2946 sqlite3_result_int(pCtx, 1);
2947 break;
2949 case JSONB_FALSE: {
2950 if( sz ) goto returnfromblob_malformed;
2951 sqlite3_result_int(pCtx, 0);
2952 break;
2954 case JSONB_INT5:
2955 case JSONB_INT: {
2956 sqlite3_int64 iRes = 0;
2957 char *z;
2958 int bNeg = 0;
2959 char x;
2960 if( sz==0 ) goto returnfromblob_malformed;
2961 x = (char)pParse->aBlob[i+n];
2962 if( x=='-' ){
2963 if( sz<2 ) goto returnfromblob_malformed;
2964 n++;
2965 sz--;
2966 bNeg = 1;
2968 z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz);
2969 if( z==0 ) goto returnfromblob_oom;
2970 rc = sqlite3DecOrHexToI64(z, &iRes);
2971 sqlite3DbFree(db, z);
2972 if( rc==0 ){
2973 sqlite3_result_int64(pCtx, bNeg ? -iRes : iRes);
2974 }else if( rc==3 && bNeg ){
2975 sqlite3_result_int64(pCtx, SMALLEST_INT64);
2976 }else if( rc==1 ){
2977 goto returnfromblob_malformed;
2978 }else{
2979 if( bNeg ){ n--; sz++; }
2980 goto to_double;
2982 break;
2984 case JSONB_FLOAT5:
2985 case JSONB_FLOAT: {
2986 double r;
2987 char *z;
2988 if( sz==0 ) goto returnfromblob_malformed;
2989 to_double:
2990 z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz);
2991 if( z==0 ) goto returnfromblob_oom;
2992 rc = sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
2993 sqlite3DbFree(db, z);
2994 if( rc<=0 ) goto returnfromblob_malformed;
2995 sqlite3_result_double(pCtx, r);
2996 break;
2998 case JSONB_TEXTRAW:
2999 case JSONB_TEXT: {
3000 sqlite3_result_text(pCtx, (char*)&pParse->aBlob[i+n], sz,
3001 SQLITE_TRANSIENT);
3002 break;
3004 case JSONB_TEXT5:
3005 case JSONB_TEXTJ: {
3006 /* Translate JSON formatted string into raw text */
3007 u32 iIn, iOut;
3008 const char *z;
3009 char *zOut;
3010 u32 nOut = sz;
3011 z = (const char*)&pParse->aBlob[i+n];
3012 zOut = sqlite3DbMallocRaw(db, nOut+1);
3013 if( zOut==0 ) goto returnfromblob_oom;
3014 for(iIn=iOut=0; iIn<sz; iIn++){
3015 char c = z[iIn];
3016 if( c=='\\' ){
3017 u32 v;
3018 u32 szEscape = jsonUnescapeOneChar(&z[iIn], sz-iIn, &v);
3019 if( v<=0x7f ){
3020 zOut[iOut++] = (char)v;
3021 }else if( v<=0x7ff ){
3022 assert( szEscape>=2 );
3023 zOut[iOut++] = (char)(0xc0 | (v>>6));
3024 zOut[iOut++] = 0x80 | (v&0x3f);
3025 }else if( v<0x10000 ){
3026 assert( szEscape>=3 );
3027 zOut[iOut++] = 0xe0 | (v>>12);
3028 zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
3029 zOut[iOut++] = 0x80 | (v&0x3f);
3030 }else if( v==JSON_INVALID_CHAR ){
3031 /* Silently ignore illegal unicode */
3032 }else{
3033 assert( szEscape>=4 );
3034 zOut[iOut++] = 0xf0 | (v>>18);
3035 zOut[iOut++] = 0x80 | ((v>>12)&0x3f);
3036 zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
3037 zOut[iOut++] = 0x80 | (v&0x3f);
3039 iIn += szEscape - 1;
3040 }else{
3041 zOut[iOut++] = c;
3043 } /* end for() */
3044 assert( iOut<=nOut );
3045 zOut[iOut] = 0;
3046 sqlite3_result_text(pCtx, zOut, iOut, SQLITE_DYNAMIC);
3047 break;
3049 case JSONB_ARRAY:
3050 case JSONB_OBJECT: {
3051 int flags = textOnly ? 0 : SQLITE_PTR_TO_INT(sqlite3_user_data(pCtx));
3052 if( flags & JSON_BLOB ){
3053 sqlite3_result_blob(pCtx, &pParse->aBlob[i], sz+n, SQLITE_TRANSIENT);
3054 }else{
3055 jsonReturnTextJsonFromBlob(pCtx, &pParse->aBlob[i], sz+n);
3057 break;
3059 default: {
3060 goto returnfromblob_malformed;
3063 return;
3065 returnfromblob_oom:
3066 sqlite3_result_error_nomem(pCtx);
3067 return;
3069 returnfromblob_malformed:
3070 sqlite3_result_error(pCtx, "malformed JSON", -1);
3071 return;
3075 ** pArg is a function argument that might be an SQL value or a JSON
3076 ** value. Figure out what it is and encode it as a JSONB blob.
3077 ** Return the results in pParse.
3079 ** pParse is uninitialized upon entry. This routine will handle the
3080 ** initialization of pParse. The result will be contained in
3081 ** pParse->aBlob and pParse->nBlob. pParse->aBlob might be dynamically
3082 ** allocated (if pParse->nBlobAlloc is greater than zero) in which case
3083 ** the caller is responsible for freeing the space allocated to pParse->aBlob
3084 ** when it has finished with it. Or pParse->aBlob might be a static string
3085 ** or a value obtained from sqlite3_value_blob(pArg).
3087 ** If the argument is a BLOB that is clearly not a JSONB, then this
3088 ** function might set an error message in ctx and return non-zero.
3089 ** It might also set an error message and return non-zero on an OOM error.
3091 static int jsonFunctionArgToBlob(
3092 sqlite3_context *ctx,
3093 sqlite3_value *pArg,
3094 JsonParse *pParse
3096 int eType = sqlite3_value_type(pArg);
3097 static u8 aNull[] = { 0x00 };
3098 memset(pParse, 0, sizeof(pParse[0]));
3099 pParse->db = sqlite3_context_db_handle(ctx);
3100 switch( eType ){
3101 default: {
3102 pParse->aBlob = aNull;
3103 pParse->nBlob = 1;
3104 return 0;
3106 case SQLITE_BLOB: {
3107 if( jsonFuncArgMightBeBinary(pArg) ){
3108 pParse->aBlob = (u8*)sqlite3_value_blob(pArg);
3109 pParse->nBlob = sqlite3_value_bytes(pArg);
3110 }else{
3111 sqlite3_result_error(ctx, "JSON cannot hold BLOB values", -1);
3112 return 1;
3114 break;
3116 case SQLITE_TEXT: {
3117 const char *zJson = (const char*)sqlite3_value_text(pArg);
3118 int nJson = sqlite3_value_bytes(pArg);
3119 if( zJson==0 ) return 1;
3120 if( sqlite3_value_subtype(pArg)==JSON_SUBTYPE ){
3121 pParse->zJson = (char*)zJson;
3122 pParse->nJson = nJson;
3123 if( jsonConvertTextToBlob(pParse, ctx) ){
3124 sqlite3_result_error(ctx, "malformed JSON", -1);
3125 sqlite3DbFree(pParse->db, pParse->aBlob);
3126 memset(pParse, 0, sizeof(pParse[0]));
3127 return 1;
3129 }else{
3130 jsonBlobAppendNode(pParse, JSONB_TEXTRAW, nJson, zJson);
3132 break;
3134 case SQLITE_FLOAT: {
3135 double r = sqlite3_value_double(pArg);
3136 if( NEVER(sqlite3IsNaN(r)) ){
3137 jsonBlobAppendNode(pParse, JSONB_NULL, 0, 0);
3138 }else{
3139 int n = sqlite3_value_bytes(pArg);
3140 const char *z = (const char*)sqlite3_value_text(pArg);
3141 if( z==0 ) return 1;
3142 if( z[0]=='I' ){
3143 jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999");
3144 }else if( z[0]=='-' && z[1]=='I' ){
3145 jsonBlobAppendNode(pParse, JSONB_FLOAT, 6, "-9e999");
3146 }else{
3147 jsonBlobAppendNode(pParse, JSONB_FLOAT, n, z);
3150 break;
3152 case SQLITE_INTEGER: {
3153 int n = sqlite3_value_bytes(pArg);
3154 const char *z = (const char*)sqlite3_value_text(pArg);
3155 if( z==0 ) return 1;
3156 jsonBlobAppendNode(pParse, JSONB_INT, n, z);
3157 break;
3160 if( pParse->oom ){
3161 sqlite3_result_error_nomem(ctx);
3162 return 1;
3163 }else{
3164 return 0;
3169 ** Generate a bad path error.
3171 ** If ctx is not NULL then push the error message into ctx and return NULL.
3172 ** If ctx is NULL, then return the text of the error message.
3174 static char *jsonBadPathError(
3175 sqlite3_context *ctx, /* The function call containing the error */
3176 const char *zPath /* The path with the problem */
3178 char *zMsg = sqlite3_mprintf("bad JSON path: %Q", zPath);
3179 if( ctx==0 ) return zMsg;
3180 if( zMsg ){
3181 sqlite3_result_error(ctx, zMsg, -1);
3182 sqlite3_free(zMsg);
3183 }else{
3184 sqlite3_result_error_nomem(ctx);
3186 return 0;
3189 /* argv[0] is a BLOB that seems likely to be a JSONB. Subsequent
3190 ** arguments come in parse where each pair contains a JSON path and
3191 ** content to insert or set at that patch. Do the updates
3192 ** and return the result.
3194 ** The specific operation is determined by eEdit, which can be one
3195 ** of JEDIT_INS, JEDIT_REPL, or JEDIT_SET.
3197 static void jsonInsertIntoBlob(
3198 sqlite3_context *ctx,
3199 int argc,
3200 sqlite3_value **argv,
3201 int eEdit /* JEDIT_INS, JEDIT_REPL, or JEDIT_SET */
3203 int i;
3204 u32 rc = 0;
3205 const char *zPath = 0;
3206 int flgs;
3207 JsonParse *p;
3208 JsonParse ax;
3210 assert( (argc&1)==1 );
3211 flgs = argc==1 ? 0 : JSON_EDITABLE;
3212 p = jsonParseFuncArg(ctx, argv[0], flgs);
3213 if( p==0 ) return;
3214 for(i=1; i<argc-1; i+=2){
3215 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) continue;
3216 zPath = (const char*)sqlite3_value_text(argv[i]);
3217 if( zPath==0 ){
3218 sqlite3_result_error_nomem(ctx);
3219 jsonParseFree(p);
3220 return;
3222 if( zPath[0]!='$' ) goto jsonInsertIntoBlob_patherror;
3223 if( jsonFunctionArgToBlob(ctx, argv[i+1], &ax) ){
3224 jsonParseReset(&ax);
3225 jsonParseFree(p);
3226 return;
3228 if( zPath[1]==0 ){
3229 if( eEdit==JEDIT_REPL || eEdit==JEDIT_SET ){
3230 jsonBlobEdit(p, 0, p->nBlob, ax.aBlob, ax.nBlob);
3232 rc = 0;
3233 }else{
3234 p->eEdit = eEdit;
3235 p->nIns = ax.nBlob;
3236 p->aIns = ax.aBlob;
3237 p->delta = 0;
3238 rc = jsonLookupStep(p, 0, zPath+1, 0);
3240 jsonParseReset(&ax);
3241 if( rc==JSON_LOOKUP_NOTFOUND ) continue;
3242 if( JSON_LOOKUP_ISERROR(rc) ) goto jsonInsertIntoBlob_patherror;
3244 jsonReturnParse(ctx, p);
3245 jsonParseFree(p);
3246 return;
3248 jsonInsertIntoBlob_patherror:
3249 jsonParseFree(p);
3250 if( rc==JSON_LOOKUP_ERROR ){
3251 sqlite3_result_error(ctx, "malformed JSON", -1);
3252 }else{
3253 jsonBadPathError(ctx, zPath);
3255 return;
3259 ** If pArg is a blob that seems like a JSONB blob, then initialize
3260 ** p to point to that JSONB and return TRUE. If pArg does not seem like
3261 ** a JSONB blob, then return FALSE;
3263 ** This routine is only called if it is already known that pArg is a
3264 ** blob. The only open question is whether or not the blob appears
3265 ** to be a JSONB blob.
3267 static int jsonArgIsJsonb(sqlite3_value *pArg, JsonParse *p){
3268 u32 n, sz = 0;
3269 p->aBlob = (u8*)sqlite3_value_blob(pArg);
3270 p->nBlob = (u32)sqlite3_value_bytes(pArg);
3271 if( p->nBlob==0 ){
3272 p->aBlob = 0;
3273 return 0;
3275 if( NEVER(p->aBlob==0) ){
3276 return 0;
3278 if( (p->aBlob[0] & 0x0f)<=JSONB_OBJECT
3279 && (n = jsonbPayloadSize(p, 0, &sz))>0
3280 && sz+n==p->nBlob
3281 && ((p->aBlob[0] & 0x0f)>JSONB_FALSE || sz==0)
3283 return 1;
3285 p->aBlob = 0;
3286 p->nBlob = 0;
3287 return 0;
3291 ** Generate a JsonParse object, containing valid JSONB in aBlob and nBlob,
3292 ** from the SQL function argument pArg. Return a pointer to the new
3293 ** JsonParse object.
3295 ** Ownership of the new JsonParse object is passed to the caller. The
3296 ** caller should invoke jsonParseFree() on the return value when it
3297 ** has finished using it.
3299 ** If any errors are detected, an appropriate error messages is set
3300 ** using sqlite3_result_error() or the equivalent and this routine
3301 ** returns NULL. This routine also returns NULL if the pArg argument
3302 ** is an SQL NULL value, but no error message is set in that case. This
3303 ** is so that SQL functions that are given NULL arguments will return
3304 ** a NULL value.
3306 static JsonParse *jsonParseFuncArg(
3307 sqlite3_context *ctx,
3308 sqlite3_value *pArg,
3309 u32 flgs
3311 int eType; /* Datatype of pArg */
3312 JsonParse *p = 0; /* Value to be returned */
3313 JsonParse *pFromCache = 0; /* Value taken from cache */
3314 sqlite3 *db; /* The database connection */
3316 assert( ctx!=0 );
3317 eType = sqlite3_value_type(pArg);
3318 if( eType==SQLITE_NULL ){
3319 return 0;
3321 pFromCache = jsonCacheSearch(ctx, pArg);
3322 if( pFromCache ){
3323 pFromCache->nJPRef++;
3324 if( (flgs & JSON_EDITABLE)==0 ){
3325 return pFromCache;
3328 db = sqlite3_context_db_handle(ctx);
3329 rebuild_from_cache:
3330 p = sqlite3DbMallocZero(db, sizeof(*p));
3331 if( p==0 ) goto json_pfa_oom;
3332 memset(p, 0, sizeof(*p));
3333 p->db = db;
3334 p->nJPRef = 1;
3335 if( pFromCache!=0 ){
3336 u32 nBlob = pFromCache->nBlob;
3337 p->aBlob = sqlite3DbMallocRaw(db, nBlob);
3338 if( p->aBlob==0 ) goto json_pfa_oom;
3339 memcpy(p->aBlob, pFromCache->aBlob, nBlob);
3340 p->nBlobAlloc = p->nBlob = nBlob;
3341 p->hasNonstd = pFromCache->hasNonstd;
3342 jsonParseFree(pFromCache);
3343 return p;
3345 if( eType==SQLITE_BLOB ){
3346 if( jsonArgIsJsonb(pArg,p) ){
3347 if( (flgs & JSON_EDITABLE)!=0 && jsonBlobMakeEditable(p, 0)==0 ){
3348 goto json_pfa_oom;
3350 return p;
3352 /* If the blob is not valid JSONB, fall through into trying to cast
3353 ** the blob into text which is then interpreted as JSON. (tag-20240123-a)
3355 ** This goes against all historical documentation about how the SQLite
3356 ** JSON functions were suppose to work. From the beginning, blob was
3357 ** reserved for expansion and a blob value should have raised an error.
3358 ** But it did not, due to a bug. And many applications came to depend
3359 ** upon this buggy behavior, espeically when using the CLI and reading
3360 ** JSON text using readfile(), which returns a blob. For this reason
3361 ** we will continue to support the bug moving forward.
3362 ** See for example https://sqlite.org/forum/forumpost/012136abd5292b8d
3365 p->zJson = (char*)sqlite3_value_text(pArg);
3366 p->nJson = sqlite3_value_bytes(pArg);
3367 if( db->mallocFailed ) goto json_pfa_oom;
3368 if( p->nJson==0 ) goto json_pfa_malformed;
3369 assert( p->zJson!=0 );
3370 if( jsonConvertTextToBlob(p, (flgs & JSON_KEEPERROR) ? 0 : ctx) ){
3371 if( flgs & JSON_KEEPERROR ){
3372 p->nErr = 1;
3373 return p;
3374 }else{
3375 jsonParseFree(p);
3376 return 0;
3378 }else{
3379 int isRCStr = sqlite3ValueIsOfClass(pArg, sqlite3RCStrUnref);
3380 int rc;
3381 if( !isRCStr ){
3382 char *zNew = sqlite3RCStrNew( p->nJson );
3383 if( zNew==0 ) goto json_pfa_oom;
3384 memcpy(zNew, p->zJson, p->nJson);
3385 p->zJson = zNew;
3386 p->zJson[p->nJson] = 0;
3387 }else{
3388 sqlite3RCStrRef(p->zJson);
3390 p->bJsonIsRCStr = 1;
3391 rc = jsonCacheInsert(ctx, p);
3392 if( rc==SQLITE_NOMEM ) goto json_pfa_oom;
3393 if( flgs & JSON_EDITABLE ){
3394 pFromCache = p;
3395 p = 0;
3396 goto rebuild_from_cache;
3399 return p;
3401 json_pfa_malformed:
3402 if( flgs & JSON_KEEPERROR ){
3403 p->nErr = 1;
3404 return p;
3405 }else{
3406 jsonParseFree(p);
3407 sqlite3_result_error(ctx, "malformed JSON", -1);
3408 return 0;
3411 json_pfa_oom:
3412 jsonParseFree(pFromCache);
3413 jsonParseFree(p);
3414 sqlite3_result_error_nomem(ctx);
3415 return 0;
3419 ** Make the return value of a JSON function either the raw JSONB blob
3420 ** or make it JSON text, depending on whether the JSON_BLOB flag is
3421 ** set on the function.
3423 static void jsonReturnParse(
3424 sqlite3_context *ctx,
3425 JsonParse *p
3427 int flgs;
3428 if( p->oom ){
3429 sqlite3_result_error_nomem(ctx);
3430 return;
3432 flgs = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
3433 if( flgs & JSON_BLOB ){
3434 if( p->nBlobAlloc>0 && !p->bReadOnly ){
3435 sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_DYNAMIC);
3436 p->nBlobAlloc = 0;
3437 }else{
3438 sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_TRANSIENT);
3440 }else{
3441 JsonString s;
3442 jsonStringInit(&s, ctx);
3443 p->delta = 0;
3444 jsonTranslateBlobToText(p, 0, &s);
3445 jsonReturnString(&s, p, ctx);
3446 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3450 /****************************************************************************
3451 ** SQL functions used for testing and debugging
3452 ****************************************************************************/
3454 #if SQLITE_DEBUG
3456 ** Decode JSONB bytes in aBlob[] starting at iStart through but not
3457 ** including iEnd. Indent the
3458 ** content by nIndent spaces.
3460 static void jsonDebugPrintBlob(
3461 JsonParse *pParse, /* JSON content */
3462 u32 iStart, /* Start rendering here */
3463 u32 iEnd, /* Do not render this byte or any byte after this one */
3464 int nIndent, /* Indent by this many spaces */
3465 sqlite3_str *pOut /* Generate output into this sqlite3_str object */
3467 while( iStart<iEnd ){
3468 u32 i, n, nn, sz = 0;
3469 int showContent = 1;
3470 u8 x = pParse->aBlob[iStart] & 0x0f;
3471 u32 savedNBlob = pParse->nBlob;
3472 sqlite3_str_appendf(pOut, "%5d:%*s", iStart, nIndent, "");
3473 if( pParse->nBlobAlloc>pParse->nBlob ){
3474 pParse->nBlob = pParse->nBlobAlloc;
3476 nn = n = jsonbPayloadSize(pParse, iStart, &sz);
3477 if( nn==0 ) nn = 1;
3478 if( sz>0 && x<JSONB_ARRAY ){
3479 nn += sz;
3481 for(i=0; i<nn; i++){
3482 sqlite3_str_appendf(pOut, " %02x", pParse->aBlob[iStart+i]);
3484 if( n==0 ){
3485 sqlite3_str_appendf(pOut, " ERROR invalid node size\n");
3486 iStart = n==0 ? iStart+1 : iEnd;
3487 continue;
3489 pParse->nBlob = savedNBlob;
3490 if( iStart+n+sz>iEnd ){
3491 iEnd = iStart+n+sz;
3492 if( iEnd>pParse->nBlob ){
3493 if( pParse->nBlobAlloc>0 && iEnd>pParse->nBlobAlloc ){
3494 iEnd = pParse->nBlobAlloc;
3495 }else{
3496 iEnd = pParse->nBlob;
3500 sqlite3_str_appendall(pOut," <-- ");
3501 switch( x ){
3502 case JSONB_NULL: sqlite3_str_appendall(pOut,"null"); break;
3503 case JSONB_TRUE: sqlite3_str_appendall(pOut,"true"); break;
3504 case JSONB_FALSE: sqlite3_str_appendall(pOut,"false"); break;
3505 case JSONB_INT: sqlite3_str_appendall(pOut,"int"); break;
3506 case JSONB_INT5: sqlite3_str_appendall(pOut,"int5"); break;
3507 case JSONB_FLOAT: sqlite3_str_appendall(pOut,"float"); break;
3508 case JSONB_FLOAT5: sqlite3_str_appendall(pOut,"float5"); break;
3509 case JSONB_TEXT: sqlite3_str_appendall(pOut,"text"); break;
3510 case JSONB_TEXTJ: sqlite3_str_appendall(pOut,"textj"); break;
3511 case JSONB_TEXT5: sqlite3_str_appendall(pOut,"text5"); break;
3512 case JSONB_TEXTRAW: sqlite3_str_appendall(pOut,"textraw"); break;
3513 case JSONB_ARRAY: {
3514 sqlite3_str_appendf(pOut,"array, %u bytes\n", sz);
3515 jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut);
3516 showContent = 0;
3517 break;
3519 case JSONB_OBJECT: {
3520 sqlite3_str_appendf(pOut, "object, %u bytes\n", sz);
3521 jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut);
3522 showContent = 0;
3523 break;
3525 default: {
3526 sqlite3_str_appendall(pOut, "ERROR: unknown node type\n");
3527 showContent = 0;
3528 break;
3531 if( showContent ){
3532 if( sz==0 && x<=JSONB_FALSE ){
3533 sqlite3_str_append(pOut, "\n", 1);
3534 }else{
3535 u32 j;
3536 sqlite3_str_appendall(pOut, ": \"");
3537 for(j=iStart+n; j<iStart+n+sz; j++){
3538 u8 c = pParse->aBlob[j];
3539 if( c<0x20 || c>=0x7f ) c = '.';
3540 sqlite3_str_append(pOut, (char*)&c, 1);
3542 sqlite3_str_append(pOut, "\"\n", 2);
3545 iStart += n + sz;
3548 static void jsonShowParse(JsonParse *pParse){
3549 sqlite3_str out;
3550 char zBuf[1000];
3551 if( pParse==0 ){
3552 printf("NULL pointer\n");
3553 return;
3554 }else{
3555 printf("nBlobAlloc = %u\n", pParse->nBlobAlloc);
3556 printf("nBlob = %u\n", pParse->nBlob);
3557 printf("delta = %d\n", pParse->delta);
3558 if( pParse->nBlob==0 ) return;
3559 printf("content (bytes 0..%u):\n", pParse->nBlob-1);
3561 sqlite3StrAccumInit(&out, 0, zBuf, sizeof(zBuf), 1000000);
3562 jsonDebugPrintBlob(pParse, 0, pParse->nBlob, 0, &out);
3563 printf("%s", sqlite3_str_value(&out));
3564 sqlite3_str_reset(&out);
3566 #endif /* SQLITE_DEBUG */
3568 #ifdef SQLITE_DEBUG
3570 ** SQL function: json_parse(JSON)
3572 ** Parse JSON using jsonParseFuncArg(). Return text that is a
3573 ** human-readable dump of the binary JSONB for the input parameter.
3575 static void jsonParseFunc(
3576 sqlite3_context *ctx,
3577 int argc,
3578 sqlite3_value **argv
3580 JsonParse *p; /* The parse */
3581 sqlite3_str out;
3583 assert( argc>=1 );
3584 sqlite3StrAccumInit(&out, 0, 0, 0, 1000000);
3585 p = jsonParseFuncArg(ctx, argv[0], 0);
3586 if( p==0 ) return;
3587 if( argc==1 ){
3588 jsonDebugPrintBlob(p, 0, p->nBlob, 0, &out);
3589 sqlite3_result_text64(ctx,out.zText,out.nChar,SQLITE_TRANSIENT,SQLITE_UTF8);
3590 }else{
3591 jsonShowParse(p);
3593 jsonParseFree(p);
3594 sqlite3_str_reset(&out);
3596 #endif /* SQLITE_DEBUG */
3598 /****************************************************************************
3599 ** Scalar SQL function implementations
3600 ****************************************************************************/
3603 ** Implementation of the json_quote(VALUE) function. Return a JSON value
3604 ** corresponding to the SQL value input. Mostly this means putting
3605 ** double-quotes around strings and returning the unquoted string "null"
3606 ** when given a NULL input.
3608 static void jsonQuoteFunc(
3609 sqlite3_context *ctx,
3610 int argc,
3611 sqlite3_value **argv
3613 JsonString jx;
3614 UNUSED_PARAMETER(argc);
3616 jsonStringInit(&jx, ctx);
3617 jsonAppendSqlValue(&jx, argv[0]);
3618 jsonReturnString(&jx, 0, 0);
3619 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3623 ** Implementation of the json_array(VALUE,...) function. Return a JSON
3624 ** array that contains all values given in arguments. Or if any argument
3625 ** is a BLOB, throw an error.
3627 static void jsonArrayFunc(
3628 sqlite3_context *ctx,
3629 int argc,
3630 sqlite3_value **argv
3632 int i;
3633 JsonString jx;
3635 jsonStringInit(&jx, ctx);
3636 jsonAppendChar(&jx, '[');
3637 for(i=0; i<argc; i++){
3638 jsonAppendSeparator(&jx);
3639 jsonAppendSqlValue(&jx, argv[i]);
3641 jsonAppendChar(&jx, ']');
3642 jsonReturnString(&jx, 0, 0);
3643 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3647 ** json_array_length(JSON)
3648 ** json_array_length(JSON, PATH)
3650 ** Return the number of elements in the top-level JSON array.
3651 ** Return 0 if the input is not a well-formed JSON array.
3653 static void jsonArrayLengthFunc(
3654 sqlite3_context *ctx,
3655 int argc,
3656 sqlite3_value **argv
3658 JsonParse *p; /* The parse */
3659 sqlite3_int64 cnt = 0;
3660 u32 i;
3661 u8 eErr = 0;
3663 p = jsonParseFuncArg(ctx, argv[0], 0);
3664 if( p==0 ) return;
3665 if( argc==2 ){
3666 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
3667 if( zPath==0 ){
3668 jsonParseFree(p);
3669 return;
3671 i = jsonLookupStep(p, 0, zPath[0]=='$' ? zPath+1 : "@", 0);
3672 if( JSON_LOOKUP_ISERROR(i) ){
3673 if( i==JSON_LOOKUP_NOTFOUND ){
3674 /* no-op */
3675 }else if( i==JSON_LOOKUP_PATHERROR ){
3676 jsonBadPathError(ctx, zPath);
3677 }else{
3678 sqlite3_result_error(ctx, "malformed JSON", -1);
3680 eErr = 1;
3681 i = 0;
3683 }else{
3684 i = 0;
3686 if( (p->aBlob[i] & 0x0f)==JSONB_ARRAY ){
3687 cnt = jsonbArrayCount(p, i);
3689 if( !eErr ) sqlite3_result_int64(ctx, cnt);
3690 jsonParseFree(p);
3693 /* True if the string is all digits */
3694 static int jsonAllDigits(const char *z, int n){
3695 int i;
3696 for(i=0; i<n && sqlite3Isdigit(z[i]); i++){}
3697 return i==n;
3700 /* True if the string is all alphanumerics and underscores */
3701 static int jsonAllAlphanum(const char *z, int n){
3702 int i;
3703 for(i=0; i<n && (sqlite3Isalnum(z[i]) || z[i]=='_'); i++){}
3704 return i==n;
3708 ** json_extract(JSON, PATH, ...)
3709 ** "->"(JSON,PATH)
3710 ** "->>"(JSON,PATH)
3712 ** Return the element described by PATH. Return NULL if that PATH element
3713 ** is not found.
3715 ** If JSON_JSON is set or if more that one PATH argument is supplied then
3716 ** always return a JSON representation of the result. If JSON_SQL is set,
3717 ** then always return an SQL representation of the result. If neither flag
3718 ** is present and argc==2, then return JSON for objects and arrays and SQL
3719 ** for all other values.
3721 ** When multiple PATH arguments are supplied, the result is a JSON array
3722 ** containing the result of each PATH.
3724 ** Abbreviated JSON path expressions are allows if JSON_ABPATH, for
3725 ** compatibility with PG.
3727 static void jsonExtractFunc(
3728 sqlite3_context *ctx,
3729 int argc,
3730 sqlite3_value **argv
3732 JsonParse *p = 0; /* The parse */
3733 int flags; /* Flags associated with the function */
3734 int i; /* Loop counter */
3735 JsonString jx; /* String for array result */
3737 if( argc<2 ) return;
3738 p = jsonParseFuncArg(ctx, argv[0], 0);
3739 if( p==0 ) return;
3740 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
3741 jsonStringInit(&jx, ctx);
3742 if( argc>2 ){
3743 jsonAppendChar(&jx, '[');
3745 for(i=1; i<argc; i++){
3746 /* With a single PATH argument */
3747 const char *zPath = (const char*)sqlite3_value_text(argv[i]);
3748 int nPath;
3749 u32 j;
3750 if( zPath==0 ) goto json_extract_error;
3751 nPath = sqlite3Strlen30(zPath);
3752 if( zPath[0]=='$' ){
3753 j = jsonLookupStep(p, 0, zPath+1, 0);
3754 }else if( (flags & JSON_ABPATH) ){
3755 /* The -> and ->> operators accept abbreviated PATH arguments. This
3756 ** is mostly for compatibility with PostgreSQL, but also for
3757 ** convenience.
3759 ** NUMBER ==> $[NUMBER] // PG compatible
3760 ** LABEL ==> $.LABEL // PG compatible
3761 ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience
3763 jsonStringInit(&jx, ctx);
3764 if( jsonAllDigits(zPath, nPath) ){
3765 jsonAppendRawNZ(&jx, "[", 1);
3766 jsonAppendRaw(&jx, zPath, nPath);
3767 jsonAppendRawNZ(&jx, "]", 2);
3768 }else if( jsonAllAlphanum(zPath, nPath) ){
3769 jsonAppendRawNZ(&jx, ".", 1);
3770 jsonAppendRaw(&jx, zPath, nPath);
3771 }else if( zPath[0]=='[' && nPath>=3 && zPath[nPath-1]==']' ){
3772 jsonAppendRaw(&jx, zPath, nPath);
3773 }else{
3774 jsonAppendRawNZ(&jx, ".\"", 2);
3775 jsonAppendRaw(&jx, zPath, nPath);
3776 jsonAppendRawNZ(&jx, "\"", 1);
3778 jsonStringTerminate(&jx);
3779 j = jsonLookupStep(p, 0, jx.zBuf, 0);
3780 jsonStringReset(&jx);
3781 }else{
3782 jsonBadPathError(ctx, zPath);
3783 goto json_extract_error;
3785 if( j<p->nBlob ){
3786 if( argc==2 ){
3787 if( flags & JSON_JSON ){
3788 jsonStringInit(&jx, ctx);
3789 jsonTranslateBlobToText(p, j, &jx);
3790 jsonReturnString(&jx, 0, 0);
3791 jsonStringReset(&jx);
3792 assert( (flags & JSON_BLOB)==0 );
3793 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3794 }else{
3795 jsonReturnFromBlob(p, j, ctx, 0);
3796 if( (flags & (JSON_SQL|JSON_BLOB))==0
3797 && (p->aBlob[j]&0x0f)>=JSONB_ARRAY
3799 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3802 }else{
3803 jsonAppendSeparator(&jx);
3804 jsonTranslateBlobToText(p, j, &jx);
3806 }else if( j==JSON_LOOKUP_NOTFOUND ){
3807 if( argc==2 ){
3808 goto json_extract_error; /* Return NULL if not found */
3809 }else{
3810 jsonAppendSeparator(&jx);
3811 jsonAppendRawNZ(&jx, "null", 4);
3813 }else if( j==JSON_LOOKUP_ERROR ){
3814 sqlite3_result_error(ctx, "malformed JSON", -1);
3815 goto json_extract_error;
3816 }else{
3817 jsonBadPathError(ctx, zPath);
3818 goto json_extract_error;
3821 if( argc>2 ){
3822 jsonAppendChar(&jx, ']');
3823 jsonReturnString(&jx, 0, 0);
3824 if( (flags & JSON_BLOB)==0 ){
3825 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3828 json_extract_error:
3829 jsonStringReset(&jx);
3830 jsonParseFree(p);
3831 return;
3835 ** Return codes for jsonMergePatch()
3837 #define JSON_MERGE_OK 0 /* Success */
3838 #define JSON_MERGE_BADTARGET 1 /* Malformed TARGET blob */
3839 #define JSON_MERGE_BADPATCH 2 /* Malformed PATCH blob */
3840 #define JSON_MERGE_OOM 3 /* Out-of-memory condition */
3843 ** RFC-7396 MergePatch for two JSONB blobs.
3845 ** pTarget is the target. pPatch is the patch. The target is updated
3846 ** in place. The patch is read-only.
3848 ** The original RFC-7396 algorithm is this:
3850 ** define MergePatch(Target, Patch):
3851 ** if Patch is an Object:
3852 ** if Target is not an Object:
3853 ** Target = {} # Ignore the contents and set it to an empty Object
3854 ** for each Name/Value pair in Patch:
3855 ** if Value is null:
3856 ** if Name exists in Target:
3857 ** remove the Name/Value pair from Target
3858 ** else:
3859 ** Target[Name] = MergePatch(Target[Name], Value)
3860 ** return Target
3861 ** else:
3862 ** return Patch
3864 ** Here is an equivalent algorithm restructured to show the actual
3865 ** implementation:
3867 ** 01 define MergePatch(Target, Patch):
3868 ** 02 if Patch is not an Object:
3869 ** 03 return Patch
3870 ** 04 else: // if Patch is an Object
3871 ** 05 if Target is not an Object:
3872 ** 06 Target = {}
3873 ** 07 for each Name/Value pair in Patch:
3874 ** 08 if Name exists in Target:
3875 ** 09 if Value is null:
3876 ** 10 remove the Name/Value pair from Target
3877 ** 11 else
3878 ** 12 Target[name] = MergePatch(Target[Name], Value)
3879 ** 13 else if Value is not NULL:
3880 ** 14 if Value is not an Object:
3881 ** 15 Target[name] = Value
3882 ** 16 else:
3883 ** 17 Target[name] = MergePatch('{}',value)
3884 ** 18 return Target
3885 ** |
3886 ** ^---- Line numbers referenced in comments in the implementation
3888 static int jsonMergePatch(
3889 JsonParse *pTarget, /* The JSON parser that contains the TARGET */
3890 u32 iTarget, /* Index of TARGET in pTarget->aBlob[] */
3891 const JsonParse *pPatch, /* The PATCH */
3892 u32 iPatch /* Index of PATCH in pPatch->aBlob[] */
3894 u8 x; /* Type of a single node */
3895 u32 n, sz=0; /* Return values from jsonbPayloadSize() */
3896 u32 iTCursor; /* Cursor position while scanning the target object */
3897 u32 iTStart; /* First label in the target object */
3898 u32 iTEndBE; /* Original first byte past end of target, before edit */
3899 u32 iTEnd; /* Current first byte past end of target */
3900 u8 eTLabel; /* Node type of the target label */
3901 u32 iTLabel = 0; /* Index of the label */
3902 u32 nTLabel = 0; /* Header size in bytes for the target label */
3903 u32 szTLabel = 0; /* Size of the target label payload */
3904 u32 iTValue = 0; /* Index of the target value */
3905 u32 nTValue = 0; /* Header size of the target value */
3906 u32 szTValue = 0; /* Payload size for the target value */
3908 u32 iPCursor; /* Cursor position while scanning the patch */
3909 u32 iPEnd; /* First byte past the end of the patch */
3910 u8 ePLabel; /* Node type of the patch label */
3911 u32 iPLabel; /* Start of patch label */
3912 u32 nPLabel; /* Size of header on the patch label */
3913 u32 szPLabel; /* Payload size of the patch label */
3914 u32 iPValue; /* Start of patch value */
3915 u32 nPValue; /* Header size for the patch value */
3916 u32 szPValue; /* Payload size of the patch value */
3918 assert( iTarget>=0 && iTarget<pTarget->nBlob );
3919 assert( iPatch>=0 && iPatch<pPatch->nBlob );
3920 x = pPatch->aBlob[iPatch] & 0x0f;
3921 if( x!=JSONB_OBJECT ){ /* Algorithm line 02 */
3922 u32 szPatch; /* Total size of the patch, header+payload */
3923 u32 szTarget; /* Total size of the target, header+payload */
3924 n = jsonbPayloadSize(pPatch, iPatch, &sz);
3925 szPatch = n+sz;
3926 sz = 0;
3927 n = jsonbPayloadSize(pTarget, iTarget, &sz);
3928 szTarget = n+sz;
3929 jsonBlobEdit(pTarget, iTarget, szTarget, pPatch->aBlob+iPatch, szPatch);
3930 return pTarget->oom ? JSON_MERGE_OOM : JSON_MERGE_OK; /* Line 03 */
3932 x = pTarget->aBlob[iTarget] & 0x0f;
3933 if( x!=JSONB_OBJECT ){ /* Algorithm line 05 */
3934 n = jsonbPayloadSize(pTarget, iTarget, &sz);
3935 jsonBlobEdit(pTarget, iTarget+n, sz, 0, 0);
3936 x = pTarget->aBlob[iTarget];
3937 pTarget->aBlob[iTarget] = (x & 0xf0) | JSONB_OBJECT;
3939 n = jsonbPayloadSize(pPatch, iPatch, &sz);
3940 if( NEVER(n==0) ) return JSON_MERGE_BADPATCH;
3941 iPCursor = iPatch+n;
3942 iPEnd = iPCursor+sz;
3943 n = jsonbPayloadSize(pTarget, iTarget, &sz);
3944 if( NEVER(n==0) ) return JSON_MERGE_BADTARGET;
3945 iTStart = iTarget+n;
3946 iTEndBE = iTStart+sz;
3948 while( iPCursor<iPEnd ){ /* Algorithm line 07 */
3949 iPLabel = iPCursor;
3950 ePLabel = pPatch->aBlob[iPCursor] & 0x0f;
3951 if( ePLabel<JSONB_TEXT || ePLabel>JSONB_TEXTRAW ){
3952 return JSON_MERGE_BADPATCH;
3954 nPLabel = jsonbPayloadSize(pPatch, iPCursor, &szPLabel);
3955 if( nPLabel==0 ) return JSON_MERGE_BADPATCH;
3956 iPValue = iPCursor + nPLabel + szPLabel;
3957 if( iPValue>=iPEnd ) return JSON_MERGE_BADPATCH;
3958 nPValue = jsonbPayloadSize(pPatch, iPValue, &szPValue);
3959 if( nPValue==0 ) return JSON_MERGE_BADPATCH;
3960 iPCursor = iPValue + nPValue + szPValue;
3961 if( iPCursor>iPEnd ) return JSON_MERGE_BADPATCH;
3963 iTCursor = iTStart;
3964 iTEnd = iTEndBE + pTarget->delta;
3965 while( iTCursor<iTEnd ){
3966 int isEqual; /* true if the patch and target labels match */
3967 iTLabel = iTCursor;
3968 eTLabel = pTarget->aBlob[iTCursor] & 0x0f;
3969 if( eTLabel<JSONB_TEXT || eTLabel>JSONB_TEXTRAW ){
3970 return JSON_MERGE_BADTARGET;
3972 nTLabel = jsonbPayloadSize(pTarget, iTCursor, &szTLabel);
3973 if( nTLabel==0 ) return JSON_MERGE_BADTARGET;
3974 iTValue = iTLabel + nTLabel + szTLabel;
3975 if( iTValue>=iTEnd ) return JSON_MERGE_BADTARGET;
3976 nTValue = jsonbPayloadSize(pTarget, iTValue, &szTValue);
3977 if( nTValue==0 ) return JSON_MERGE_BADTARGET;
3978 if( iTValue + nTValue + szTValue > iTEnd ) return JSON_MERGE_BADTARGET;
3979 isEqual = jsonLabelCompare(
3980 (const char*)&pPatch->aBlob[iPLabel+nPLabel],
3981 szPLabel,
3982 (ePLabel==JSONB_TEXT || ePLabel==JSONB_TEXTRAW),
3983 (const char*)&pTarget->aBlob[iTLabel+nTLabel],
3984 szTLabel,
3985 (eTLabel==JSONB_TEXT || eTLabel==JSONB_TEXTRAW));
3986 if( isEqual ) break;
3987 iTCursor = iTValue + nTValue + szTValue;
3989 x = pPatch->aBlob[iPValue] & 0x0f;
3990 if( iTCursor<iTEnd ){
3991 /* A match was found. Algorithm line 08 */
3992 if( x==0 ){
3993 /* Patch value is NULL. Algorithm line 09 */
3994 jsonBlobEdit(pTarget, iTLabel, nTLabel+szTLabel+nTValue+szTValue, 0,0);
3995 /* vvvvvv----- No OOM on a delete-only edit */
3996 if( NEVER(pTarget->oom) ) return JSON_MERGE_OOM;
3997 }else{
3998 /* Algorithm line 12 */
3999 int rc, savedDelta = pTarget->delta;
4000 pTarget->delta = 0;
4001 rc = jsonMergePatch(pTarget, iTValue, pPatch, iPValue);
4002 if( rc ) return rc;
4003 pTarget->delta += savedDelta;
4005 }else if( x>0 ){ /* Algorithm line 13 */
4006 /* No match and patch value is not NULL */
4007 u32 szNew = szPLabel+nPLabel;
4008 if( (pPatch->aBlob[iPValue] & 0x0f)!=JSONB_OBJECT ){ /* Line 14 */
4009 jsonBlobEdit(pTarget, iTEnd, 0, 0, szPValue+nPValue+szNew);
4010 if( pTarget->oom ) return JSON_MERGE_OOM;
4011 memcpy(&pTarget->aBlob[iTEnd], &pPatch->aBlob[iPLabel], szNew);
4012 memcpy(&pTarget->aBlob[iTEnd+szNew],
4013 &pPatch->aBlob[iPValue], szPValue+nPValue);
4014 }else{
4015 int rc, savedDelta;
4016 jsonBlobEdit(pTarget, iTEnd, 0, 0, szNew+1);
4017 if( pTarget->oom ) return JSON_MERGE_OOM;
4018 memcpy(&pTarget->aBlob[iTEnd], &pPatch->aBlob[iPLabel], szNew);
4019 pTarget->aBlob[iTEnd+szNew] = 0x00;
4020 savedDelta = pTarget->delta;
4021 pTarget->delta = 0;
4022 rc = jsonMergePatch(pTarget, iTEnd+szNew,pPatch,iPValue);
4023 if( rc ) return rc;
4024 pTarget->delta += savedDelta;
4028 if( pTarget->delta ) jsonAfterEditSizeAdjust(pTarget, iTarget);
4029 return pTarget->oom ? JSON_MERGE_OOM : JSON_MERGE_OK;
4034 ** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
4035 ** object that is the result of running the RFC 7396 MergePatch() algorithm
4036 ** on the two arguments.
4038 static void jsonPatchFunc(
4039 sqlite3_context *ctx,
4040 int argc,
4041 sqlite3_value **argv
4043 JsonParse *pTarget; /* The TARGET */
4044 JsonParse *pPatch; /* The PATCH */
4045 int rc; /* Result code */
4047 UNUSED_PARAMETER(argc);
4048 assert( argc==2 );
4049 pTarget = jsonParseFuncArg(ctx, argv[0], JSON_EDITABLE);
4050 if( pTarget==0 ) return;
4051 pPatch = jsonParseFuncArg(ctx, argv[1], 0);
4052 if( pPatch ){
4053 rc = jsonMergePatch(pTarget, 0, pPatch, 0);
4054 if( rc==JSON_MERGE_OK ){
4055 jsonReturnParse(ctx, pTarget);
4056 }else if( rc==JSON_MERGE_OOM ){
4057 sqlite3_result_error_nomem(ctx);
4058 }else{
4059 sqlite3_result_error(ctx, "malformed JSON", -1);
4061 jsonParseFree(pPatch);
4063 jsonParseFree(pTarget);
4068 ** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
4069 ** object that contains all name/value given in arguments. Or if any name
4070 ** is not a string or if any value is a BLOB, throw an error.
4072 static void jsonObjectFunc(
4073 sqlite3_context *ctx,
4074 int argc,
4075 sqlite3_value **argv
4077 int i;
4078 JsonString jx;
4079 const char *z;
4080 u32 n;
4082 if( argc&1 ){
4083 sqlite3_result_error(ctx, "json_object() requires an even number "
4084 "of arguments", -1);
4085 return;
4087 jsonStringInit(&jx, ctx);
4088 jsonAppendChar(&jx, '{');
4089 for(i=0; i<argc; i+=2){
4090 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
4091 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
4092 jsonStringReset(&jx);
4093 return;
4095 jsonAppendSeparator(&jx);
4096 z = (const char*)sqlite3_value_text(argv[i]);
4097 n = sqlite3_value_bytes(argv[i]);
4098 jsonAppendString(&jx, z, n);
4099 jsonAppendChar(&jx, ':');
4100 jsonAppendSqlValue(&jx, argv[i+1]);
4102 jsonAppendChar(&jx, '}');
4103 jsonReturnString(&jx, 0, 0);
4104 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
4109 ** json_remove(JSON, PATH, ...)
4111 ** Remove the named elements from JSON and return the result. malformed
4112 ** JSON or PATH arguments result in an error.
4114 static void jsonRemoveFunc(
4115 sqlite3_context *ctx,
4116 int argc,
4117 sqlite3_value **argv
4119 JsonParse *p; /* The parse */
4120 const char *zPath = 0; /* Path of element to be removed */
4121 int i; /* Loop counter */
4122 u32 rc; /* Subroutine return code */
4124 if( argc<1 ) return;
4125 p = jsonParseFuncArg(ctx, argv[0], argc>1 ? JSON_EDITABLE : 0);
4126 if( p==0 ) return;
4127 for(i=1; i<argc; i++){
4128 zPath = (const char*)sqlite3_value_text(argv[i]);
4129 if( zPath==0 ){
4130 goto json_remove_done;
4132 if( zPath[0]!='$' ){
4133 goto json_remove_patherror;
4135 if( zPath[1]==0 ){
4136 /* json_remove(j,'$') returns NULL */
4137 goto json_remove_done;
4139 p->eEdit = JEDIT_DEL;
4140 p->delta = 0;
4141 rc = jsonLookupStep(p, 0, zPath+1, 0);
4142 if( JSON_LOOKUP_ISERROR(rc) ){
4143 if( rc==JSON_LOOKUP_NOTFOUND ){
4144 continue; /* No-op */
4145 }else if( rc==JSON_LOOKUP_PATHERROR ){
4146 jsonBadPathError(ctx, zPath);
4147 }else{
4148 sqlite3_result_error(ctx, "malformed JSON", -1);
4150 goto json_remove_done;
4153 jsonReturnParse(ctx, p);
4154 jsonParseFree(p);
4155 return;
4157 json_remove_patherror:
4158 jsonBadPathError(ctx, zPath);
4160 json_remove_done:
4161 jsonParseFree(p);
4162 return;
4166 ** json_replace(JSON, PATH, VALUE, ...)
4168 ** Replace the value at PATH with VALUE. If PATH does not already exist,
4169 ** this routine is a no-op. If JSON or PATH is malformed, throw an error.
4171 static void jsonReplaceFunc(
4172 sqlite3_context *ctx,
4173 int argc,
4174 sqlite3_value **argv
4176 if( argc<1 ) return;
4177 if( (argc&1)==0 ) {
4178 jsonWrongNumArgs(ctx, "replace");
4179 return;
4181 jsonInsertIntoBlob(ctx, argc, argv, JEDIT_REPL);
4186 ** json_set(JSON, PATH, VALUE, ...)
4188 ** Set the value at PATH to VALUE. Create the PATH if it does not already
4189 ** exist. Overwrite existing values that do exist.
4190 ** If JSON or PATH is malformed, throw an error.
4192 ** json_insert(JSON, PATH, VALUE, ...)
4194 ** Create PATH and initialize it to VALUE. If PATH already exists, this
4195 ** routine is a no-op. If JSON or PATH is malformed, throw an error.
4197 static void jsonSetFunc(
4198 sqlite3_context *ctx,
4199 int argc,
4200 sqlite3_value **argv
4203 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
4204 int bIsSet = (flags&JSON_ISSET)!=0;
4206 if( argc<1 ) return;
4207 if( (argc&1)==0 ) {
4208 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
4209 return;
4211 jsonInsertIntoBlob(ctx, argc, argv, bIsSet ? JEDIT_SET : JEDIT_INS);
4215 ** json_type(JSON)
4216 ** json_type(JSON, PATH)
4218 ** Return the top-level "type" of a JSON string. json_type() raises an
4219 ** error if either the JSON or PATH inputs are not well-formed.
4221 static void jsonTypeFunc(
4222 sqlite3_context *ctx,
4223 int argc,
4224 sqlite3_value **argv
4226 JsonParse *p; /* The parse */
4227 const char *zPath = 0;
4228 u32 i;
4230 p = jsonParseFuncArg(ctx, argv[0], 0);
4231 if( p==0 ) return;
4232 if( argc==2 ){
4233 zPath = (const char*)sqlite3_value_text(argv[1]);
4234 if( zPath==0 ) goto json_type_done;
4235 if( zPath[0]!='$' ){
4236 jsonBadPathError(ctx, zPath);
4237 goto json_type_done;
4239 i = jsonLookupStep(p, 0, zPath+1, 0);
4240 if( JSON_LOOKUP_ISERROR(i) ){
4241 if( i==JSON_LOOKUP_NOTFOUND ){
4242 /* no-op */
4243 }else if( i==JSON_LOOKUP_PATHERROR ){
4244 jsonBadPathError(ctx, zPath);
4245 }else{
4246 sqlite3_result_error(ctx, "malformed JSON", -1);
4248 goto json_type_done;
4250 }else{
4251 i = 0;
4253 sqlite3_result_text(ctx, jsonbType[p->aBlob[i]&0x0f], -1, SQLITE_STATIC);
4254 json_type_done:
4255 jsonParseFree(p);
4259 ** json_valid(JSON)
4260 ** json_valid(JSON, FLAGS)
4262 ** Check the JSON argument to see if it is well-formed. The FLAGS argument
4263 ** encodes the various constraints on what is meant by "well-formed":
4265 ** 0x01 Canonical RFC-8259 JSON text
4266 ** 0x02 JSON text with optional JSON-5 extensions
4267 ** 0x04 Superficially appears to be JSONB
4268 ** 0x08 Strictly well-formed JSONB
4270 ** If the FLAGS argument is omitted, it defaults to 1. Useful values for
4271 ** FLAGS include:
4273 ** 1 Strict canonical JSON text
4274 ** 2 JSON text perhaps with JSON-5 extensions
4275 ** 4 Superficially appears to be JSONB
4276 ** 5 Canonical JSON text or superficial JSONB
4277 ** 6 JSON-5 text or superficial JSONB
4278 ** 8 Strict JSONB
4279 ** 9 Canonical JSON text or strict JSONB
4280 ** 10 JSON-5 text or strict JSONB
4282 ** Other flag combinations are redundant. For example, every canonical
4283 ** JSON text is also well-formed JSON-5 text, so FLAG values 2 and 3
4284 ** are the same. Similarly, any input that passes a strict JSONB validation
4285 ** will also pass the superficial validation so 12 through 15 are the same
4286 ** as 8 through 11 respectively.
4288 ** This routine runs in linear time to validate text and when doing strict
4289 ** JSONB validation. Superficial JSONB validation is constant time,
4290 ** assuming the BLOB is already in memory. The performance advantage
4291 ** of superficial JSONB validation is why that option is provided.
4292 ** Application developers can choose to do fast superficial validation or
4293 ** slower strict validation, according to their specific needs.
4295 ** Only the lower four bits of the FLAGS argument are currently used.
4296 ** Higher bits are reserved for future expansion. To facilitate
4297 ** compatibility, the current implementation raises an error if any bit
4298 ** in FLAGS is set other than the lower four bits.
4300 ** The original circa 2015 implementation of the JSON routines in
4301 ** SQLite only supported canonical RFC-8259 JSON text and the json_valid()
4302 ** function only accepted one argument. That is why the default value
4303 ** for the FLAGS argument is 1, since FLAGS=1 causes this routine to only
4304 ** recognize canonical RFC-8259 JSON text as valid. The extra FLAGS
4305 ** argument was added when the JSON routines were extended to support
4306 ** JSON5-like extensions and binary JSONB stored in BLOBs.
4308 ** Return Values:
4310 ** * Raise an error if FLAGS is outside the range of 1 to 15.
4311 ** * Return NULL if the input is NULL
4312 ** * Return 1 if the input is well-formed.
4313 ** * Return 0 if the input is not well-formed.
4315 static void jsonValidFunc(
4316 sqlite3_context *ctx,
4317 int argc,
4318 sqlite3_value **argv
4320 JsonParse *p; /* The parse */
4321 u8 flags = 1;
4322 u8 res = 0;
4323 if( argc==2 ){
4324 i64 f = sqlite3_value_int64(argv[1]);
4325 if( f<1 || f>15 ){
4326 sqlite3_result_error(ctx, "FLAGS parameter to json_valid() must be"
4327 " between 1 and 15", -1);
4328 return;
4330 flags = f & 0x0f;
4332 switch( sqlite3_value_type(argv[0]) ){
4333 case SQLITE_NULL: {
4334 #ifdef SQLITE_LEGACY_JSON_VALID
4335 /* Incorrect legacy behavior was to return FALSE for a NULL input */
4336 sqlite3_result_int(ctx, 0);
4337 #endif
4338 return;
4340 case SQLITE_BLOB: {
4341 if( jsonFuncArgMightBeBinary(argv[0]) ){
4342 if( flags & 0x04 ){
4343 /* Superficial checking only - accomplished by the
4344 ** jsonFuncArgMightBeBinary() call above. */
4345 res = 1;
4346 }else if( flags & 0x08 ){
4347 /* Strict checking. Check by translating BLOB->TEXT->BLOB. If
4348 ** no errors occur, call that a "strict check". */
4349 JsonParse px;
4350 u32 iErr;
4351 memset(&px, 0, sizeof(px));
4352 px.aBlob = (u8*)sqlite3_value_blob(argv[0]);
4353 px.nBlob = sqlite3_value_bytes(argv[0]);
4354 iErr = jsonbValidityCheck(&px, 0, px.nBlob, 1);
4355 res = iErr==0;
4357 break;
4359 /* Fall through into interpreting the input as text. See note
4360 ** above at tag-20240123-a. */
4361 /* no break */ deliberate_fall_through
4363 default: {
4364 JsonParse px;
4365 if( (flags & 0x3)==0 ) break;
4366 memset(&px, 0, sizeof(px));
4368 p = jsonParseFuncArg(ctx, argv[0], JSON_KEEPERROR);
4369 if( p ){
4370 if( p->oom ){
4371 sqlite3_result_error_nomem(ctx);
4372 }else if( p->nErr ){
4373 /* no-op */
4374 }else if( (flags & 0x02)!=0 || p->hasNonstd==0 ){
4375 res = 1;
4377 jsonParseFree(p);
4378 }else{
4379 sqlite3_result_error_nomem(ctx);
4381 break;
4384 sqlite3_result_int(ctx, res);
4388 ** json_error_position(JSON)
4390 ** If the argument is NULL, return NULL
4392 ** If the argument is BLOB, do a full validity check and return non-zero
4393 ** if the check fails. The return value is the approximate 1-based offset
4394 ** to the byte of the element that contains the first error.
4396 ** Otherwise interpret the argument is TEXT (even if it is numeric) and
4397 ** return the 1-based character position for where the parser first recognized
4398 ** that the input was not valid JSON, or return 0 if the input text looks
4399 ** ok. JSON-5 extensions are accepted.
4401 static void jsonErrorFunc(
4402 sqlite3_context *ctx,
4403 int argc,
4404 sqlite3_value **argv
4406 i64 iErrPos = 0; /* Error position to be returned */
4407 JsonParse s;
4409 assert( argc==1 );
4410 UNUSED_PARAMETER(argc);
4411 memset(&s, 0, sizeof(s));
4412 s.db = sqlite3_context_db_handle(ctx);
4413 if( jsonFuncArgMightBeBinary(argv[0]) ){
4414 s.aBlob = (u8*)sqlite3_value_blob(argv[0]);
4415 s.nBlob = sqlite3_value_bytes(argv[0]);
4416 iErrPos = (i64)jsonbValidityCheck(&s, 0, s.nBlob, 1);
4417 }else{
4418 s.zJson = (char*)sqlite3_value_text(argv[0]);
4419 if( s.zJson==0 ) return; /* NULL input or OOM */
4420 s.nJson = sqlite3_value_bytes(argv[0]);
4421 if( jsonConvertTextToBlob(&s,0) ){
4422 if( s.oom ){
4423 iErrPos = -1;
4424 }else{
4425 /* Convert byte-offset s.iErr into a character offset */
4426 u32 k;
4427 assert( s.zJson!=0 ); /* Because s.oom is false */
4428 for(k=0; k<s.iErr && ALWAYS(s.zJson[k]); k++){
4429 if( (s.zJson[k] & 0xc0)!=0x80 ) iErrPos++;
4431 iErrPos++;
4435 jsonParseReset(&s);
4436 if( iErrPos<0 ){
4437 sqlite3_result_error_nomem(ctx);
4438 }else{
4439 sqlite3_result_int64(ctx, iErrPos);
4443 /****************************************************************************
4444 ** Aggregate SQL function implementations
4445 ****************************************************************************/
4447 ** json_group_array(VALUE)
4449 ** Return a JSON array composed of all values in the aggregate.
4451 static void jsonArrayStep(
4452 sqlite3_context *ctx,
4453 int argc,
4454 sqlite3_value **argv
4456 JsonString *pStr;
4457 UNUSED_PARAMETER(argc);
4458 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
4459 if( pStr ){
4460 if( pStr->zBuf==0 ){
4461 jsonStringInit(pStr, ctx);
4462 jsonAppendChar(pStr, '[');
4463 }else if( pStr->nUsed>1 ){
4464 jsonAppendChar(pStr, ',');
4466 pStr->pCtx = ctx;
4467 jsonAppendSqlValue(pStr, argv[0]);
4470 static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
4471 JsonString *pStr;
4472 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
4473 if( pStr ){
4474 int flags;
4475 pStr->pCtx = ctx;
4476 jsonAppendChar(pStr, ']');
4477 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
4478 if( pStr->eErr ){
4479 jsonReturnString(pStr, 0, 0);
4480 return;
4481 }else if( flags & JSON_BLOB ){
4482 jsonReturnStringAsBlob(pStr);
4483 if( isFinal ){
4484 if( !pStr->bStatic ) sqlite3RCStrUnref(pStr->zBuf);
4485 }else{
4486 jsonStringTrimOneChar(pStr);
4488 return;
4489 }else if( isFinal ){
4490 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
4491 pStr->bStatic ? SQLITE_TRANSIENT :
4492 sqlite3RCStrUnref);
4493 pStr->bStatic = 1;
4494 }else{
4495 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
4496 jsonStringTrimOneChar(pStr);
4498 }else{
4499 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
4501 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
4503 static void jsonArrayValue(sqlite3_context *ctx){
4504 jsonArrayCompute(ctx, 0);
4506 static void jsonArrayFinal(sqlite3_context *ctx){
4507 jsonArrayCompute(ctx, 1);
4510 #ifndef SQLITE_OMIT_WINDOWFUNC
4512 ** This method works for both json_group_array() and json_group_object().
4513 ** It works by removing the first element of the group by searching forward
4514 ** to the first comma (",") that is not within a string and deleting all
4515 ** text through that comma.
4517 static void jsonGroupInverse(
4518 sqlite3_context *ctx,
4519 int argc,
4520 sqlite3_value **argv
4522 unsigned int i;
4523 int inStr = 0;
4524 int nNest = 0;
4525 char *z;
4526 char c;
4527 JsonString *pStr;
4528 UNUSED_PARAMETER(argc);
4529 UNUSED_PARAMETER(argv);
4530 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
4531 #ifdef NEVER
4532 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
4533 ** always have been called to initialize it */
4534 if( NEVER(!pStr) ) return;
4535 #endif
4536 z = pStr->zBuf;
4537 for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){
4538 if( c=='"' ){
4539 inStr = !inStr;
4540 }else if( c=='\\' ){
4541 i++;
4542 }else if( !inStr ){
4543 if( c=='{' || c=='[' ) nNest++;
4544 if( c=='}' || c==']' ) nNest--;
4547 if( i<pStr->nUsed ){
4548 pStr->nUsed -= i;
4549 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
4550 z[pStr->nUsed] = 0;
4551 }else{
4552 pStr->nUsed = 1;
4555 #else
4556 # define jsonGroupInverse 0
4557 #endif
4561 ** json_group_obj(NAME,VALUE)
4563 ** Return a JSON object composed of all names and values in the aggregate.
4565 static void jsonObjectStep(
4566 sqlite3_context *ctx,
4567 int argc,
4568 sqlite3_value **argv
4570 JsonString *pStr;
4571 const char *z;
4572 u32 n;
4573 UNUSED_PARAMETER(argc);
4574 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
4575 if( pStr ){
4576 if( pStr->zBuf==0 ){
4577 jsonStringInit(pStr, ctx);
4578 jsonAppendChar(pStr, '{');
4579 }else if( pStr->nUsed>1 ){
4580 jsonAppendChar(pStr, ',');
4582 pStr->pCtx = ctx;
4583 z = (const char*)sqlite3_value_text(argv[0]);
4584 n = sqlite3Strlen30(z);
4585 jsonAppendString(pStr, z, n);
4586 jsonAppendChar(pStr, ':');
4587 jsonAppendSqlValue(pStr, argv[1]);
4590 static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
4591 JsonString *pStr;
4592 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
4593 if( pStr ){
4594 int flags;
4595 jsonAppendChar(pStr, '}');
4596 pStr->pCtx = ctx;
4597 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
4598 if( pStr->eErr ){
4599 jsonReturnString(pStr, 0, 0);
4600 return;
4601 }else if( flags & JSON_BLOB ){
4602 jsonReturnStringAsBlob(pStr);
4603 if( isFinal ){
4604 if( !pStr->bStatic ) sqlite3RCStrUnref(pStr->zBuf);
4605 }else{
4606 jsonStringTrimOneChar(pStr);
4608 return;
4609 }else if( isFinal ){
4610 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
4611 pStr->bStatic ? SQLITE_TRANSIENT :
4612 sqlite3RCStrUnref);
4613 pStr->bStatic = 1;
4614 }else{
4615 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
4616 jsonStringTrimOneChar(pStr);
4618 }else{
4619 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
4621 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
4623 static void jsonObjectValue(sqlite3_context *ctx){
4624 jsonObjectCompute(ctx, 0);
4626 static void jsonObjectFinal(sqlite3_context *ctx){
4627 jsonObjectCompute(ctx, 1);
4632 #ifndef SQLITE_OMIT_VIRTUALTABLE
4633 /****************************************************************************
4634 ** The json_each virtual table
4635 ****************************************************************************/
4636 typedef struct JsonParent JsonParent;
4637 struct JsonParent {
4638 u32 iHead; /* Start of object or array */
4639 u32 iValue; /* Start of the value */
4640 u32 iEnd; /* First byte past the end */
4641 u32 nPath; /* Length of path */
4642 i64 iKey; /* Key for JSONB_ARRAY */
4645 typedef struct JsonEachCursor JsonEachCursor;
4646 struct JsonEachCursor {
4647 sqlite3_vtab_cursor base; /* Base class - must be first */
4648 u32 iRowid; /* The rowid */
4649 u32 i; /* Index in sParse.aBlob[] of current row */
4650 u32 iEnd; /* EOF when i equals or exceeds this value */
4651 u32 nRoot; /* Size of the root path in bytes */
4652 u8 eType; /* Type of the container for element i */
4653 u8 bRecursive; /* True for json_tree(). False for json_each() */
4654 u32 nParent; /* Current nesting depth */
4655 u32 nParentAlloc; /* Space allocated for aParent[] */
4656 JsonParent *aParent; /* Parent elements of i */
4657 sqlite3 *db; /* Database connection */
4658 JsonString path; /* Current path */
4659 JsonParse sParse; /* Parse of the input JSON */
4661 typedef struct JsonEachConnection JsonEachConnection;
4662 struct JsonEachConnection {
4663 sqlite3_vtab base; /* Base class - must be first */
4664 sqlite3 *db; /* Database connection */
4668 /* Constructor for the json_each virtual table */
4669 static int jsonEachConnect(
4670 sqlite3 *db,
4671 void *pAux,
4672 int argc, const char *const*argv,
4673 sqlite3_vtab **ppVtab,
4674 char **pzErr
4676 JsonEachConnection *pNew;
4677 int rc;
4679 /* Column numbers */
4680 #define JEACH_KEY 0
4681 #define JEACH_VALUE 1
4682 #define JEACH_TYPE 2
4683 #define JEACH_ATOM 3
4684 #define JEACH_ID 4
4685 #define JEACH_PARENT 5
4686 #define JEACH_FULLKEY 6
4687 #define JEACH_PATH 7
4688 /* The xBestIndex method assumes that the JSON and ROOT columns are
4689 ** the last two columns in the table. Should this ever changes, be
4690 ** sure to update the xBestIndex method. */
4691 #define JEACH_JSON 8
4692 #define JEACH_ROOT 9
4694 UNUSED_PARAMETER(pzErr);
4695 UNUSED_PARAMETER(argv);
4696 UNUSED_PARAMETER(argc);
4697 UNUSED_PARAMETER(pAux);
4698 rc = sqlite3_declare_vtab(db,
4699 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
4700 "json HIDDEN,root HIDDEN)");
4701 if( rc==SQLITE_OK ){
4702 pNew = (JsonEachConnection*)sqlite3DbMallocZero(db, sizeof(*pNew));
4703 *ppVtab = (sqlite3_vtab*)pNew;
4704 if( pNew==0 ) return SQLITE_NOMEM;
4705 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
4706 pNew->db = db;
4708 return rc;
4711 /* destructor for json_each virtual table */
4712 static int jsonEachDisconnect(sqlite3_vtab *pVtab){
4713 JsonEachConnection *p = (JsonEachConnection*)pVtab;
4714 sqlite3DbFree(p->db, pVtab);
4715 return SQLITE_OK;
4718 /* constructor for a JsonEachCursor object for json_each(). */
4719 static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
4720 JsonEachConnection *pVtab = (JsonEachConnection*)p;
4721 JsonEachCursor *pCur;
4723 UNUSED_PARAMETER(p);
4724 pCur = sqlite3DbMallocZero(pVtab->db, sizeof(*pCur));
4725 if( pCur==0 ) return SQLITE_NOMEM;
4726 pCur->db = pVtab->db;
4727 jsonStringZero(&pCur->path);
4728 *ppCursor = &pCur->base;
4729 return SQLITE_OK;
4732 /* constructor for a JsonEachCursor object for json_tree(). */
4733 static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
4734 int rc = jsonEachOpenEach(p, ppCursor);
4735 if( rc==SQLITE_OK ){
4736 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
4737 pCur->bRecursive = 1;
4739 return rc;
4742 /* Reset a JsonEachCursor back to its original state. Free any memory
4743 ** held. */
4744 static void jsonEachCursorReset(JsonEachCursor *p){
4745 jsonParseReset(&p->sParse);
4746 jsonStringReset(&p->path);
4747 sqlite3DbFree(p->db, p->aParent);
4748 p->iRowid = 0;
4749 p->i = 0;
4750 p->aParent = 0;
4751 p->nParent = 0;
4752 p->nParentAlloc = 0;
4753 p->iEnd = 0;
4754 p->eType = 0;
4757 /* Destructor for a jsonEachCursor object */
4758 static int jsonEachClose(sqlite3_vtab_cursor *cur){
4759 JsonEachCursor *p = (JsonEachCursor*)cur;
4760 jsonEachCursorReset(p);
4762 sqlite3DbFree(p->db, cur);
4763 return SQLITE_OK;
4766 /* Return TRUE if the jsonEachCursor object has been advanced off the end
4767 ** of the JSON object */
4768 static int jsonEachEof(sqlite3_vtab_cursor *cur){
4769 JsonEachCursor *p = (JsonEachCursor*)cur;
4770 return p->i >= p->iEnd;
4774 ** If the cursor is currently pointing at the label of a object entry,
4775 ** then return the index of the value. For all other cases, return the
4776 ** current pointer position, which is the value.
4778 static int jsonSkipLabel(JsonEachCursor *p){
4779 if( p->eType==JSONB_OBJECT ){
4780 u32 sz = 0;
4781 u32 n = jsonbPayloadSize(&p->sParse, p->i, &sz);
4782 return p->i + n + sz;
4783 }else{
4784 return p->i;
4789 ** Append the path name for the current element.
4791 static void jsonAppendPathName(JsonEachCursor *p){
4792 assert( p->nParent>0 );
4793 assert( p->eType==JSONB_ARRAY || p->eType==JSONB_OBJECT );
4794 if( p->eType==JSONB_ARRAY ){
4795 jsonPrintf(30, &p->path, "[%lld]", p->aParent[p->nParent-1].iKey);
4796 }else{
4797 u32 n, sz = 0, k, i;
4798 const char *z;
4799 int needQuote = 0;
4800 n = jsonbPayloadSize(&p->sParse, p->i, &sz);
4801 k = p->i + n;
4802 z = (const char*)&p->sParse.aBlob[k];
4803 if( sz==0 || !sqlite3Isalpha(z[0]) ){
4804 needQuote = 1;
4805 }else{
4806 for(i=0; i<sz; i++){
4807 if( !sqlite3Isalnum(z[i]) ){
4808 needQuote = 1;
4809 break;
4813 if( needQuote ){
4814 jsonPrintf(sz+4,&p->path,".\"%.*s\"", sz, z);
4815 }else{
4816 jsonPrintf(sz+2,&p->path,".%.*s", sz, z);
4821 /* Advance the cursor to the next element for json_tree() */
4822 static int jsonEachNext(sqlite3_vtab_cursor *cur){
4823 JsonEachCursor *p = (JsonEachCursor*)cur;
4824 int rc = SQLITE_OK;
4825 if( p->bRecursive ){
4826 u8 x;
4827 u8 levelChange = 0;
4828 u32 n, sz = 0;
4829 u32 i = jsonSkipLabel(p);
4830 x = p->sParse.aBlob[i] & 0x0f;
4831 n = jsonbPayloadSize(&p->sParse, i, &sz);
4832 if( x==JSONB_OBJECT || x==JSONB_ARRAY ){
4833 JsonParent *pParent;
4834 if( p->nParent>=p->nParentAlloc ){
4835 JsonParent *pNew;
4836 u64 nNew;
4837 nNew = p->nParentAlloc*2 + 3;
4838 pNew = sqlite3DbRealloc(p->db, p->aParent, sizeof(JsonParent)*nNew);
4839 if( pNew==0 ) return SQLITE_NOMEM;
4840 p->nParentAlloc = (u32)nNew;
4841 p->aParent = pNew;
4843 levelChange = 1;
4844 pParent = &p->aParent[p->nParent];
4845 pParent->iHead = p->i;
4846 pParent->iValue = i;
4847 pParent->iEnd = i + n + sz;
4848 pParent->iKey = -1;
4849 pParent->nPath = (u32)p->path.nUsed;
4850 if( p->eType && p->nParent ){
4851 jsonAppendPathName(p);
4852 if( p->path.eErr ) rc = SQLITE_NOMEM;
4854 p->nParent++;
4855 p->i = i + n;
4856 }else{
4857 p->i = i + n + sz;
4859 while( p->nParent>0 && p->i >= p->aParent[p->nParent-1].iEnd ){
4860 p->nParent--;
4861 p->path.nUsed = p->aParent[p->nParent].nPath;
4862 levelChange = 1;
4864 if( levelChange ){
4865 if( p->nParent>0 ){
4866 JsonParent *pParent = &p->aParent[p->nParent-1];
4867 u32 iVal = pParent->iValue;
4868 p->eType = p->sParse.aBlob[iVal] & 0x0f;
4869 }else{
4870 p->eType = 0;
4873 }else{
4874 u32 n, sz = 0;
4875 u32 i = jsonSkipLabel(p);
4876 n = jsonbPayloadSize(&p->sParse, i, &sz);
4877 p->i = i + n + sz;
4879 if( p->eType==JSONB_ARRAY && p->nParent ){
4880 p->aParent[p->nParent-1].iKey++;
4882 p->iRowid++;
4883 return rc;
4886 /* Length of the path for rowid==0 in bRecursive mode.
4888 static int jsonEachPathLength(JsonEachCursor *p){
4889 u32 n = p->path.nUsed;
4890 char *z = p->path.zBuf;
4891 if( p->iRowid==0 && p->bRecursive && n>=2 ){
4892 while( n>1 ){
4893 n--;
4894 if( z[n]=='[' || z[n]=='.' ){
4895 u32 x, sz = 0;
4896 char cSaved = z[n];
4897 z[n] = 0;
4898 assert( p->sParse.eEdit==0 );
4899 x = jsonLookupStep(&p->sParse, 0, z+1, 0);
4900 z[n] = cSaved;
4901 if( JSON_LOOKUP_ISERROR(x) ) continue;
4902 if( x + jsonbPayloadSize(&p->sParse, x, &sz) == p->i ) break;
4906 return n;
4909 /* Return the value of a column */
4910 static int jsonEachColumn(
4911 sqlite3_vtab_cursor *cur, /* The cursor */
4912 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
4913 int iColumn /* Which column to return */
4915 JsonEachCursor *p = (JsonEachCursor*)cur;
4916 switch( iColumn ){
4917 case JEACH_KEY: {
4918 if( p->nParent==0 ){
4919 u32 n, j;
4920 if( p->nRoot==1 ) break;
4921 j = jsonEachPathLength(p);
4922 n = p->nRoot - j;
4923 if( n==0 ){
4924 break;
4925 }else if( p->path.zBuf[j]=='[' ){
4926 i64 x;
4927 sqlite3Atoi64(&p->path.zBuf[j+1], &x, n-1, SQLITE_UTF8);
4928 sqlite3_result_int64(ctx, x);
4929 }else if( p->path.zBuf[j+1]=='"' ){
4930 sqlite3_result_text(ctx, &p->path.zBuf[j+2], n-3, SQLITE_TRANSIENT);
4931 }else{
4932 sqlite3_result_text(ctx, &p->path.zBuf[j+1], n-1, SQLITE_TRANSIENT);
4934 break;
4936 if( p->eType==JSONB_OBJECT ){
4937 jsonReturnFromBlob(&p->sParse, p->i, ctx, 1);
4938 }else{
4939 assert( p->eType==JSONB_ARRAY );
4940 sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iKey);
4942 break;
4944 case JEACH_VALUE: {
4945 u32 i = jsonSkipLabel(p);
4946 jsonReturnFromBlob(&p->sParse, i, ctx, 1);
4947 if( (p->sParse.aBlob[i] & 0x0f)>=JSONB_ARRAY ){
4948 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
4950 break;
4952 case JEACH_TYPE: {
4953 u32 i = jsonSkipLabel(p);
4954 u8 eType = p->sParse.aBlob[i] & 0x0f;
4955 sqlite3_result_text(ctx, jsonbType[eType], -1, SQLITE_STATIC);
4956 break;
4958 case JEACH_ATOM: {
4959 u32 i = jsonSkipLabel(p);
4960 if( (p->sParse.aBlob[i] & 0x0f)<JSONB_ARRAY ){
4961 jsonReturnFromBlob(&p->sParse, i, ctx, 1);
4963 break;
4965 case JEACH_ID: {
4966 sqlite3_result_int64(ctx, (sqlite3_int64)p->i);
4967 break;
4969 case JEACH_PARENT: {
4970 if( p->nParent>0 && p->bRecursive ){
4971 sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iHead);
4973 break;
4975 case JEACH_FULLKEY: {
4976 u64 nBase = p->path.nUsed;
4977 if( p->nParent ) jsonAppendPathName(p);
4978 sqlite3_result_text64(ctx, p->path.zBuf, p->path.nUsed,
4979 SQLITE_TRANSIENT, SQLITE_UTF8);
4980 p->path.nUsed = nBase;
4981 break;
4983 case JEACH_PATH: {
4984 u32 n = jsonEachPathLength(p);
4985 sqlite3_result_text64(ctx, p->path.zBuf, n,
4986 SQLITE_TRANSIENT, SQLITE_UTF8);
4987 break;
4989 default: {
4990 sqlite3_result_text(ctx, p->path.zBuf, p->nRoot, SQLITE_STATIC);
4991 break;
4993 case JEACH_JSON: {
4994 if( p->sParse.zJson==0 ){
4995 sqlite3_result_blob(ctx, p->sParse.aBlob, p->sParse.nBlob,
4996 SQLITE_STATIC);
4997 }else{
4998 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
5000 break;
5003 return SQLITE_OK;
5006 /* Return the current rowid value */
5007 static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5008 JsonEachCursor *p = (JsonEachCursor*)cur;
5009 *pRowid = p->iRowid;
5010 return SQLITE_OK;
5013 /* The query strategy is to look for an equality constraint on the json
5014 ** column. Without such a constraint, the table cannot operate. idxNum is
5015 ** 1 if the constraint is found, 3 if the constraint and zRoot are found,
5016 ** and 0 otherwise.
5018 static int jsonEachBestIndex(
5019 sqlite3_vtab *tab,
5020 sqlite3_index_info *pIdxInfo
5022 int i; /* Loop counter or computed array index */
5023 int aIdx[2]; /* Index of constraints for JSON and ROOT */
5024 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
5025 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
5026 const struct sqlite3_index_constraint *pConstraint;
5028 /* This implementation assumes that JSON and ROOT are the last two
5029 ** columns in the table */
5030 assert( JEACH_ROOT == JEACH_JSON+1 );
5031 UNUSED_PARAMETER(tab);
5032 aIdx[0] = aIdx[1] = -1;
5033 pConstraint = pIdxInfo->aConstraint;
5034 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
5035 int iCol;
5036 int iMask;
5037 if( pConstraint->iColumn < JEACH_JSON ) continue;
5038 iCol = pConstraint->iColumn - JEACH_JSON;
5039 assert( iCol==0 || iCol==1 );
5040 testcase( iCol==0 );
5041 iMask = 1 << iCol;
5042 if( pConstraint->usable==0 ){
5043 unusableMask |= iMask;
5044 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5045 aIdx[iCol] = i;
5046 idxMask |= iMask;
5049 if( pIdxInfo->nOrderBy>0
5050 && pIdxInfo->aOrderBy[0].iColumn<0
5051 && pIdxInfo->aOrderBy[0].desc==0
5053 pIdxInfo->orderByConsumed = 1;
5056 if( (unusableMask & ~idxMask)!=0 ){
5057 /* If there are any unusable constraints on JSON or ROOT, then reject
5058 ** this entire plan */
5059 return SQLITE_CONSTRAINT;
5061 if( aIdx[0]<0 ){
5062 /* No JSON input. Leave estimatedCost at the huge value that it was
5063 ** initialized to to discourage the query planner from selecting this
5064 ** plan. */
5065 pIdxInfo->idxNum = 0;
5066 }else{
5067 pIdxInfo->estimatedCost = 1.0;
5068 i = aIdx[0];
5069 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
5070 pIdxInfo->aConstraintUsage[i].omit = 1;
5071 if( aIdx[1]<0 ){
5072 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
5073 }else{
5074 i = aIdx[1];
5075 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
5076 pIdxInfo->aConstraintUsage[i].omit = 1;
5077 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
5080 return SQLITE_OK;
5083 /* Start a search on a new JSON string */
5084 static int jsonEachFilter(
5085 sqlite3_vtab_cursor *cur,
5086 int idxNum, const char *idxStr,
5087 int argc, sqlite3_value **argv
5089 JsonEachCursor *p = (JsonEachCursor*)cur;
5090 const char *zRoot = 0;
5091 u32 i, n, sz;
5093 UNUSED_PARAMETER(idxStr);
5094 UNUSED_PARAMETER(argc);
5095 jsonEachCursorReset(p);
5096 if( idxNum==0 ) return SQLITE_OK;
5097 memset(&p->sParse, 0, sizeof(p->sParse));
5098 p->sParse.nJPRef = 1;
5099 p->sParse.db = p->db;
5100 if( jsonFuncArgMightBeBinary(argv[0]) ){
5101 p->sParse.nBlob = sqlite3_value_bytes(argv[0]);
5102 p->sParse.aBlob = (u8*)sqlite3_value_blob(argv[0]);
5103 }else{
5104 p->sParse.zJson = (char*)sqlite3_value_text(argv[0]);
5105 p->sParse.nJson = sqlite3_value_bytes(argv[0]);
5106 if( p->sParse.zJson==0 ){
5107 p->i = p->iEnd = 0;
5108 return SQLITE_OK;
5110 if( jsonConvertTextToBlob(&p->sParse, 0) ){
5111 if( p->sParse.oom ){
5112 return SQLITE_NOMEM;
5114 goto json_each_malformed_input;
5117 if( idxNum==3 ){
5118 zRoot = (const char*)sqlite3_value_text(argv[1]);
5119 if( zRoot==0 ) return SQLITE_OK;
5120 if( zRoot[0]!='$' ){
5121 sqlite3_free(cur->pVtab->zErrMsg);
5122 cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot);
5123 jsonEachCursorReset(p);
5124 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
5126 p->nRoot = sqlite3Strlen30(zRoot);
5127 if( zRoot[1]==0 ){
5128 i = p->i = 0;
5129 p->eType = 0;
5130 }else{
5131 i = jsonLookupStep(&p->sParse, 0, zRoot+1, 0);
5132 if( JSON_LOOKUP_ISERROR(i) ){
5133 if( i==JSON_LOOKUP_NOTFOUND ){
5134 p->i = 0;
5135 p->eType = 0;
5136 p->iEnd = 0;
5137 return SQLITE_OK;
5139 sqlite3_free(cur->pVtab->zErrMsg);
5140 cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot);
5141 jsonEachCursorReset(p);
5142 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
5144 if( p->sParse.iLabel ){
5145 p->i = p->sParse.iLabel;
5146 p->eType = JSONB_OBJECT;
5147 }else{
5148 p->i = i;
5149 p->eType = JSONB_ARRAY;
5152 jsonAppendRaw(&p->path, zRoot, p->nRoot);
5153 }else{
5154 i = p->i = 0;
5155 p->eType = 0;
5156 p->nRoot = 1;
5157 jsonAppendRaw(&p->path, "$", 1);
5159 p->nParent = 0;
5160 n = jsonbPayloadSize(&p->sParse, i, &sz);
5161 p->iEnd = i+n+sz;
5162 if( (p->sParse.aBlob[i] & 0x0f)>=JSONB_ARRAY && !p->bRecursive ){
5163 p->i = i + n;
5164 p->eType = p->sParse.aBlob[i] & 0x0f;
5165 p->aParent = sqlite3DbMallocZero(p->db, sizeof(JsonParent));
5166 if( p->aParent==0 ) return SQLITE_NOMEM;
5167 p->nParent = 1;
5168 p->nParentAlloc = 1;
5169 p->aParent[0].iKey = 0;
5170 p->aParent[0].iEnd = p->iEnd;
5171 p->aParent[0].iHead = p->i;
5172 p->aParent[0].iValue = i;
5174 return SQLITE_OK;
5176 json_each_malformed_input:
5177 sqlite3_free(cur->pVtab->zErrMsg);
5178 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
5179 jsonEachCursorReset(p);
5180 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
5183 /* The methods of the json_each virtual table */
5184 static sqlite3_module jsonEachModule = {
5185 0, /* iVersion */
5186 0, /* xCreate */
5187 jsonEachConnect, /* xConnect */
5188 jsonEachBestIndex, /* xBestIndex */
5189 jsonEachDisconnect, /* xDisconnect */
5190 0, /* xDestroy */
5191 jsonEachOpenEach, /* xOpen - open a cursor */
5192 jsonEachClose, /* xClose - close a cursor */
5193 jsonEachFilter, /* xFilter - configure scan constraints */
5194 jsonEachNext, /* xNext - advance a cursor */
5195 jsonEachEof, /* xEof - check for end of scan */
5196 jsonEachColumn, /* xColumn - read data */
5197 jsonEachRowid, /* xRowid - read data */
5198 0, /* xUpdate */
5199 0, /* xBegin */
5200 0, /* xSync */
5201 0, /* xCommit */
5202 0, /* xRollback */
5203 0, /* xFindMethod */
5204 0, /* xRename */
5205 0, /* xSavepoint */
5206 0, /* xRelease */
5207 0, /* xRollbackTo */
5208 0, /* xShadowName */
5209 0 /* xIntegrity */
5212 /* The methods of the json_tree virtual table. */
5213 static sqlite3_module jsonTreeModule = {
5214 0, /* iVersion */
5215 0, /* xCreate */
5216 jsonEachConnect, /* xConnect */
5217 jsonEachBestIndex, /* xBestIndex */
5218 jsonEachDisconnect, /* xDisconnect */
5219 0, /* xDestroy */
5220 jsonEachOpenTree, /* xOpen - open a cursor */
5221 jsonEachClose, /* xClose - close a cursor */
5222 jsonEachFilter, /* xFilter - configure scan constraints */
5223 jsonEachNext, /* xNext - advance a cursor */
5224 jsonEachEof, /* xEof - check for end of scan */
5225 jsonEachColumn, /* xColumn - read data */
5226 jsonEachRowid, /* xRowid - read data */
5227 0, /* xUpdate */
5228 0, /* xBegin */
5229 0, /* xSync */
5230 0, /* xCommit */
5231 0, /* xRollback */
5232 0, /* xFindMethod */
5233 0, /* xRename */
5234 0, /* xSavepoint */
5235 0, /* xRelease */
5236 0, /* xRollbackTo */
5237 0, /* xShadowName */
5238 0 /* xIntegrity */
5240 #endif /* SQLITE_OMIT_VIRTUALTABLE */
5241 #endif /* !defined(SQLITE_OMIT_JSON) */
5244 ** Register JSON functions.
5246 void sqlite3RegisterJsonFunctions(void){
5247 #ifndef SQLITE_OMIT_JSON
5248 static FuncDef aJsonFunc[] = {
5249 /* sqlite3_result_subtype() ----, ,--- sqlite3_value_subtype() */
5250 /* | | */
5251 /* Uses cache ------, | | ,---- Returns JSONB */
5252 /* | | | | */
5253 /* Number of arguments ---, | | | | ,--- Flags */
5254 /* | | | | | | */
5255 JFUNCTION(json, 1,1,1, 0,0,0, jsonRemoveFunc),
5256 JFUNCTION(jsonb, 1,1,0, 0,1,0, jsonRemoveFunc),
5257 JFUNCTION(json_array, -1,0,1, 1,0,0, jsonArrayFunc),
5258 JFUNCTION(jsonb_array, -1,0,1, 1,1,0, jsonArrayFunc),
5259 JFUNCTION(json_array_length, 1,1,0, 0,0,0, jsonArrayLengthFunc),
5260 JFUNCTION(json_array_length, 2,1,0, 0,0,0, jsonArrayLengthFunc),
5261 JFUNCTION(json_error_position,1,1,0, 0,0,0, jsonErrorFunc),
5262 JFUNCTION(json_extract, -1,1,1, 0,0,0, jsonExtractFunc),
5263 JFUNCTION(jsonb_extract, -1,1,0, 0,1,0, jsonExtractFunc),
5264 JFUNCTION(->, 2,1,1, 0,0,JSON_JSON, jsonExtractFunc),
5265 JFUNCTION(->>, 2,1,0, 0,0,JSON_SQL, jsonExtractFunc),
5266 JFUNCTION(json_insert, -1,1,1, 1,0,0, jsonSetFunc),
5267 JFUNCTION(jsonb_insert, -1,1,0, 1,1,0, jsonSetFunc),
5268 JFUNCTION(json_object, -1,0,1, 1,0,0, jsonObjectFunc),
5269 JFUNCTION(jsonb_object, -1,0,1, 1,1,0, jsonObjectFunc),
5270 JFUNCTION(json_patch, 2,1,1, 0,0,0, jsonPatchFunc),
5271 JFUNCTION(jsonb_patch, 2,1,0, 0,1,0, jsonPatchFunc),
5272 JFUNCTION(json_quote, 1,0,1, 1,0,0, jsonQuoteFunc),
5273 JFUNCTION(json_remove, -1,1,1, 0,0,0, jsonRemoveFunc),
5274 JFUNCTION(jsonb_remove, -1,1,0, 0,1,0, jsonRemoveFunc),
5275 JFUNCTION(json_replace, -1,1,1, 1,0,0, jsonReplaceFunc),
5276 JFUNCTION(jsonb_replace, -1,1,0, 1,1,0, jsonReplaceFunc),
5277 JFUNCTION(json_set, -1,1,1, 1,0,JSON_ISSET, jsonSetFunc),
5278 JFUNCTION(jsonb_set, -1,1,0, 1,1,JSON_ISSET, jsonSetFunc),
5279 JFUNCTION(json_type, 1,1,0, 0,0,0, jsonTypeFunc),
5280 JFUNCTION(json_type, 2,1,0, 0,0,0, jsonTypeFunc),
5281 JFUNCTION(json_valid, 1,1,0, 0,0,0, jsonValidFunc),
5282 JFUNCTION(json_valid, 2,1,0, 0,0,0, jsonValidFunc),
5283 #if SQLITE_DEBUG
5284 JFUNCTION(json_parse, 1,1,0, 0,0,0, jsonParseFunc),
5285 #endif
5286 WAGGREGATE(json_group_array, 1, 0, 0,
5287 jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,
5288 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|
5289 SQLITE_DETERMINISTIC),
5290 WAGGREGATE(jsonb_group_array, 1, JSON_BLOB, 0,
5291 jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,
5292 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC),
5293 WAGGREGATE(json_group_object, 2, 0, 0,
5294 jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,
5295 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC),
5296 WAGGREGATE(jsonb_group_object,2, JSON_BLOB, 0,
5297 jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,
5298 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|
5299 SQLITE_DETERMINISTIC)
5301 sqlite3InsertBuiltinFuncs(aJsonFunc, ArraySize(aJsonFunc));
5302 #endif
5305 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
5307 ** Register the JSON table-valued functions
5309 int sqlite3JsonTableFunctions(sqlite3 *db){
5310 int rc = SQLITE_OK;
5311 static const struct {
5312 const char *zName;
5313 sqlite3_module *pModule;
5314 } aMod[] = {
5315 { "json_each", &jsonEachModule },
5316 { "json_tree", &jsonTreeModule },
5318 unsigned int i;
5319 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
5320 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
5322 return rc;
5324 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) */