Improvements to TCL9 support. Fixes to the Makefiles so that the "install"
[sqlite.git] / src / json.c
bloba0a075e66cd43708e0033fe42aeaaeba4e81c898
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;
566 /* Append formatted text (not to exceed N bytes) to the JsonString.
568 static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
569 va_list ap;
570 if( (p->nUsed + N >= p->nAlloc) && jsonStringGrow(p, N) ) return;
571 va_start(ap, zFormat);
572 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
573 va_end(ap);
574 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
577 /* Append a single character
579 static SQLITE_NOINLINE void jsonAppendCharExpand(JsonString *p, char c){
580 if( jsonStringGrow(p,1) ) return;
581 p->zBuf[p->nUsed++] = c;
583 static void jsonAppendChar(JsonString *p, char c){
584 if( p->nUsed>=p->nAlloc ){
585 jsonAppendCharExpand(p,c);
586 }else{
587 p->zBuf[p->nUsed++] = c;
591 /* Remove a single character from the end of the string
593 static void jsonStringTrimOneChar(JsonString *p){
594 if( p->eErr==0 ){
595 assert( p->nUsed>0 );
596 p->nUsed--;
601 /* Make sure there is a zero terminator on p->zBuf[]
603 ** Return true on success. Return false if an OOM prevents this
604 ** from happening.
606 static int jsonStringTerminate(JsonString *p){
607 jsonAppendChar(p, 0);
608 jsonStringTrimOneChar(p);
609 return p->eErr==0;
612 /* Append a comma separator to the output buffer, if the previous
613 ** character is not '[' or '{'.
615 static void jsonAppendSeparator(JsonString *p){
616 char c;
617 if( p->nUsed==0 ) return;
618 c = p->zBuf[p->nUsed-1];
619 if( c=='[' || c=='{' ) return;
620 jsonAppendChar(p, ',');
623 /* c is a control character. Append the canonical JSON representation
624 ** of that control character to p.
626 ** This routine assumes that the output buffer has already been enlarged
627 ** sufficiently to hold the worst-case encoding plus a nul terminator.
629 static void jsonAppendControlChar(JsonString *p, u8 c){
630 static const char aSpecial[] = {
631 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
632 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
634 assert( sizeof(aSpecial)==32 );
635 assert( aSpecial['\b']=='b' );
636 assert( aSpecial['\f']=='f' );
637 assert( aSpecial['\n']=='n' );
638 assert( aSpecial['\r']=='r' );
639 assert( aSpecial['\t']=='t' );
640 assert( c>=0 && c<sizeof(aSpecial) );
641 assert( p->nUsed+7 <= p->nAlloc );
642 if( aSpecial[c] ){
643 p->zBuf[p->nUsed] = '\\';
644 p->zBuf[p->nUsed+1] = aSpecial[c];
645 p->nUsed += 2;
646 }else{
647 p->zBuf[p->nUsed] = '\\';
648 p->zBuf[p->nUsed+1] = 'u';
649 p->zBuf[p->nUsed+2] = '0';
650 p->zBuf[p->nUsed+3] = '0';
651 p->zBuf[p->nUsed+4] = "0123456789abcdef"[c>>4];
652 p->zBuf[p->nUsed+5] = "0123456789abcdef"[c&0xf];
653 p->nUsed += 6;
657 /* Append the N-byte string in zIn to the end of the JsonString string
658 ** under construction. Enclose the string in double-quotes ("...") and
659 ** escape any double-quotes or backslash characters contained within the
660 ** string.
662 ** This routine is a high-runner. There is a measurable performance
663 ** increase associated with unwinding the jsonIsOk[] loop.
665 static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
666 u32 k;
667 u8 c;
668 const u8 *z = (const u8*)zIn;
669 if( z==0 ) return;
670 if( (N+p->nUsed+2 >= p->nAlloc) && jsonStringGrow(p,N+2)!=0 ) return;
671 p->zBuf[p->nUsed++] = '"';
672 while( 1 /*exit-by-break*/ ){
673 k = 0;
674 /* The following while() is the 4-way unwound equivalent of
676 ** while( k<N && jsonIsOk[z[k]] ){ k++; }
678 while( 1 /* Exit by break */ ){
679 if( k+3>=N ){
680 while( k<N && jsonIsOk[z[k]] ){ k++; }
681 break;
683 if( !jsonIsOk[z[k]] ){
684 break;
686 if( !jsonIsOk[z[k+1]] ){
687 k += 1;
688 break;
690 if( !jsonIsOk[z[k+2]] ){
691 k += 2;
692 break;
694 if( !jsonIsOk[z[k+3]] ){
695 k += 3;
696 break;
697 }else{
698 k += 4;
701 if( k>=N ){
702 if( k>0 ){
703 memcpy(&p->zBuf[p->nUsed], z, k);
704 p->nUsed += k;
706 break;
708 if( k>0 ){
709 memcpy(&p->zBuf[p->nUsed], z, k);
710 p->nUsed += k;
711 z += k;
712 N -= k;
714 c = z[0];
715 if( c=='"' || c=='\\' ){
716 if( (p->nUsed+N+3 > p->nAlloc) && jsonStringGrow(p,N+3)!=0 ) return;
717 p->zBuf[p->nUsed++] = '\\';
718 p->zBuf[p->nUsed++] = c;
719 }else if( c=='\'' ){
720 p->zBuf[p->nUsed++] = c;
721 }else{
722 if( (p->nUsed+N+7 > p->nAlloc) && jsonStringGrow(p,N+7)!=0 ) return;
723 jsonAppendControlChar(p, c);
725 z++;
726 N--;
728 p->zBuf[p->nUsed++] = '"';
729 assert( p->nUsed<p->nAlloc );
733 ** Append an sqlite3_value (such as a function parameter) to the JSON
734 ** string under construction in p.
736 static void jsonAppendSqlValue(
737 JsonString *p, /* Append to this JSON string */
738 sqlite3_value *pValue /* Value to append */
740 switch( sqlite3_value_type(pValue) ){
741 case SQLITE_NULL: {
742 jsonAppendRawNZ(p, "null", 4);
743 break;
745 case SQLITE_FLOAT: {
746 jsonPrintf(100, p, "%!0.15g", sqlite3_value_double(pValue));
747 break;
749 case SQLITE_INTEGER: {
750 const char *z = (const char*)sqlite3_value_text(pValue);
751 u32 n = (u32)sqlite3_value_bytes(pValue);
752 jsonAppendRaw(p, z, n);
753 break;
755 case SQLITE_TEXT: {
756 const char *z = (const char*)sqlite3_value_text(pValue);
757 u32 n = (u32)sqlite3_value_bytes(pValue);
758 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
759 jsonAppendRaw(p, z, n);
760 }else{
761 jsonAppendString(p, z, n);
763 break;
765 default: {
766 if( jsonFuncArgMightBeBinary(pValue) ){
767 JsonParse px;
768 memset(&px, 0, sizeof(px));
769 px.aBlob = (u8*)sqlite3_value_blob(pValue);
770 px.nBlob = sqlite3_value_bytes(pValue);
771 jsonTranslateBlobToText(&px, 0, p);
772 }else if( p->eErr==0 ){
773 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
774 p->eErr = JSTRING_ERR;
775 jsonStringReset(p);
777 break;
782 /* Make the text in p (which is probably a generated JSON text string)
783 ** the result of the SQL function.
785 ** The JsonString is reset.
787 ** If pParse and ctx are both non-NULL, then the SQL string in p is
788 ** loaded into the zJson field of the pParse object as a RCStr and the
789 ** pParse is added to the cache.
791 static void jsonReturnString(
792 JsonString *p, /* String to return */
793 JsonParse *pParse, /* JSONB source or NULL */
794 sqlite3_context *ctx /* Where to cache */
796 assert( (pParse!=0)==(ctx!=0) );
797 assert( ctx==0 || ctx==p->pCtx );
798 if( p->eErr==0 ){
799 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(p->pCtx));
800 if( flags & JSON_BLOB ){
801 jsonReturnStringAsBlob(p);
802 }else if( p->bStatic ){
803 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
804 SQLITE_TRANSIENT, SQLITE_UTF8);
805 }else if( jsonStringTerminate(p) ){
806 if( pParse && pParse->bJsonIsRCStr==0 && pParse->nBlobAlloc>0 ){
807 int rc;
808 pParse->zJson = sqlite3RCStrRef(p->zBuf);
809 pParse->nJson = p->nUsed;
810 pParse->bJsonIsRCStr = 1;
811 rc = jsonCacheInsert(ctx, pParse);
812 if( rc==SQLITE_NOMEM ){
813 sqlite3_result_error_nomem(ctx);
814 jsonStringReset(p);
815 return;
818 sqlite3_result_text64(p->pCtx, sqlite3RCStrRef(p->zBuf), p->nUsed,
819 sqlite3RCStrUnref,
820 SQLITE_UTF8);
821 }else{
822 sqlite3_result_error_nomem(p->pCtx);
824 }else if( p->eErr & JSTRING_OOM ){
825 sqlite3_result_error_nomem(p->pCtx);
826 }else if( p->eErr & JSTRING_MALFORMED ){
827 sqlite3_result_error(p->pCtx, "malformed JSON", -1);
829 jsonStringReset(p);
832 /**************************************************************************
833 ** Utility routines for dealing with JsonParse objects
834 **************************************************************************/
837 ** Reclaim all memory allocated by a JsonParse object. But do not
838 ** delete the JsonParse object itself.
840 static void jsonParseReset(JsonParse *pParse){
841 assert( pParse->nJPRef<=1 );
842 if( pParse->bJsonIsRCStr ){
843 sqlite3RCStrUnref(pParse->zJson);
844 pParse->zJson = 0;
845 pParse->nJson = 0;
846 pParse->bJsonIsRCStr = 0;
848 if( pParse->nBlobAlloc ){
849 sqlite3DbFree(pParse->db, pParse->aBlob);
850 pParse->aBlob = 0;
851 pParse->nBlob = 0;
852 pParse->nBlobAlloc = 0;
857 ** Decrement the reference count on the JsonParse object. When the
858 ** count reaches zero, free the object.
860 static void jsonParseFree(JsonParse *pParse){
861 if( pParse ){
862 if( pParse->nJPRef>1 ){
863 pParse->nJPRef--;
864 }else{
865 jsonParseReset(pParse);
866 sqlite3DbFree(pParse->db, pParse);
871 /**************************************************************************
872 ** Utility routines for the JSON text parser
873 **************************************************************************/
876 ** Translate a single byte of Hex into an integer.
877 ** This routine only gives a correct answer if h really is a valid hexadecimal
878 ** character: 0..9a..fA..F. But unlike sqlite3HexToInt(), it does not
879 ** assert() if the digit is not hex.
881 static u8 jsonHexToInt(int h){
882 #ifdef SQLITE_ASCII
883 h += 9*(1&(h>>6));
884 #endif
885 #ifdef SQLITE_EBCDIC
886 h += 9*(1&~(h>>4));
887 #endif
888 return (u8)(h & 0xf);
892 ** Convert a 4-byte hex string into an integer
894 static u32 jsonHexToInt4(const char *z){
895 u32 v;
896 v = (jsonHexToInt(z[0])<<12)
897 + (jsonHexToInt(z[1])<<8)
898 + (jsonHexToInt(z[2])<<4)
899 + jsonHexToInt(z[3]);
900 return v;
904 ** Return true if z[] begins with 2 (or more) hexadecimal digits
906 static int jsonIs2Hex(const char *z){
907 return sqlite3Isxdigit(z[0]) && sqlite3Isxdigit(z[1]);
911 ** Return true if z[] begins with 4 (or more) hexadecimal digits
913 static int jsonIs4Hex(const char *z){
914 return jsonIs2Hex(z) && jsonIs2Hex(&z[2]);
918 ** Return the number of bytes of JSON5 whitespace at the beginning of
919 ** the input string z[].
921 ** JSON5 whitespace consists of any of the following characters:
923 ** Unicode UTF-8 Name
924 ** U+0009 09 horizontal tab
925 ** U+000a 0a line feed
926 ** U+000b 0b vertical tab
927 ** U+000c 0c form feed
928 ** U+000d 0d carriage return
929 ** U+0020 20 space
930 ** U+00a0 c2 a0 non-breaking space
931 ** U+1680 e1 9a 80 ogham space mark
932 ** U+2000 e2 80 80 en quad
933 ** U+2001 e2 80 81 em quad
934 ** U+2002 e2 80 82 en space
935 ** U+2003 e2 80 83 em space
936 ** U+2004 e2 80 84 three-per-em space
937 ** U+2005 e2 80 85 four-per-em space
938 ** U+2006 e2 80 86 six-per-em space
939 ** U+2007 e2 80 87 figure space
940 ** U+2008 e2 80 88 punctuation space
941 ** U+2009 e2 80 89 thin space
942 ** U+200a e2 80 8a hair space
943 ** U+2028 e2 80 a8 line separator
944 ** U+2029 e2 80 a9 paragraph separator
945 ** U+202f e2 80 af narrow no-break space (NNBSP)
946 ** U+205f e2 81 9f medium mathematical space (MMSP)
947 ** U+3000 e3 80 80 ideographical space
948 ** U+FEFF ef bb bf byte order mark
950 ** In addition, comments between '/', '*' and '*', '/' and
951 ** from '/', '/' to end-of-line are also considered to be whitespace.
953 static int json5Whitespace(const char *zIn){
954 int n = 0;
955 const u8 *z = (u8*)zIn;
956 while( 1 /*exit by "goto whitespace_done"*/ ){
957 switch( z[n] ){
958 case 0x09:
959 case 0x0a:
960 case 0x0b:
961 case 0x0c:
962 case 0x0d:
963 case 0x20: {
964 n++;
965 break;
967 case '/': {
968 if( z[n+1]=='*' && z[n+2]!=0 ){
969 int j;
970 for(j=n+3; z[j]!='/' || z[j-1]!='*'; j++){
971 if( z[j]==0 ) goto whitespace_done;
973 n = j+1;
974 break;
975 }else if( z[n+1]=='/' ){
976 int j;
977 char c;
978 for(j=n+2; (c = z[j])!=0; j++){
979 if( c=='\n' || c=='\r' ) break;
980 if( 0xe2==(u8)c && 0x80==(u8)z[j+1]
981 && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2])
983 j += 2;
984 break;
987 n = j;
988 if( z[n] ) n++;
989 break;
991 goto whitespace_done;
993 case 0xc2: {
994 if( z[n+1]==0xa0 ){
995 n += 2;
996 break;
998 goto whitespace_done;
1000 case 0xe1: {
1001 if( z[n+1]==0x9a && z[n+2]==0x80 ){
1002 n += 3;
1003 break;
1005 goto whitespace_done;
1007 case 0xe2: {
1008 if( z[n+1]==0x80 ){
1009 u8 c = z[n+2];
1010 if( c<0x80 ) goto whitespace_done;
1011 if( c<=0x8a || c==0xa8 || c==0xa9 || c==0xaf ){
1012 n += 3;
1013 break;
1015 }else if( z[n+1]==0x81 && z[n+2]==0x9f ){
1016 n += 3;
1017 break;
1019 goto whitespace_done;
1021 case 0xe3: {
1022 if( z[n+1]==0x80 && z[n+2]==0x80 ){
1023 n += 3;
1024 break;
1026 goto whitespace_done;
1028 case 0xef: {
1029 if( z[n+1]==0xbb && z[n+2]==0xbf ){
1030 n += 3;
1031 break;
1033 goto whitespace_done;
1035 default: {
1036 goto whitespace_done;
1040 whitespace_done:
1041 return n;
1045 ** Extra floating-point literals to allow in JSON.
1047 static const struct NanInfName {
1048 char c1;
1049 char c2;
1050 char n;
1051 char eType;
1052 char nRepl;
1053 char *zMatch;
1054 char *zRepl;
1055 } aNanInfName[] = {
1056 { 'i', 'I', 3, JSONB_FLOAT, 7, "inf", "9.0e999" },
1057 { 'i', 'I', 8, JSONB_FLOAT, 7, "infinity", "9.0e999" },
1058 { 'n', 'N', 3, JSONB_NULL, 4, "NaN", "null" },
1059 { 'q', 'Q', 4, JSONB_NULL, 4, "QNaN", "null" },
1060 { 's', 'S', 4, JSONB_NULL, 4, "SNaN", "null" },
1065 ** Report the wrong number of arguments for json_insert(), json_replace()
1066 ** or json_set().
1068 static void jsonWrongNumArgs(
1069 sqlite3_context *pCtx,
1070 const char *zFuncName
1072 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
1073 zFuncName);
1074 sqlite3_result_error(pCtx, zMsg, -1);
1075 sqlite3_free(zMsg);
1078 /****************************************************************************
1079 ** Utility routines for dealing with the binary BLOB representation of JSON
1080 ****************************************************************************/
1083 ** Expand pParse->aBlob so that it holds at least N bytes.
1085 ** Return the number of errors.
1087 static int jsonBlobExpand(JsonParse *pParse, u32 N){
1088 u8 *aNew;
1089 u32 t;
1090 assert( N>pParse->nBlobAlloc );
1091 if( pParse->nBlobAlloc==0 ){
1092 t = 100;
1093 }else{
1094 t = pParse->nBlobAlloc*2;
1096 if( t<N ) t = N+100;
1097 aNew = sqlite3DbRealloc(pParse->db, pParse->aBlob, t);
1098 if( aNew==0 ){ pParse->oom = 1; return 1; }
1099 pParse->aBlob = aNew;
1100 pParse->nBlobAlloc = t;
1101 return 0;
1105 ** If pParse->aBlob is not previously editable (because it is taken
1106 ** from sqlite3_value_blob(), as indicated by the fact that
1107 ** pParse->nBlobAlloc==0 and pParse->nBlob>0) then make it editable
1108 ** by making a copy into space obtained from malloc.
1110 ** Return true on success. Return false on OOM.
1112 static int jsonBlobMakeEditable(JsonParse *pParse, u32 nExtra){
1113 u8 *aOld;
1114 u32 nSize;
1115 assert( !pParse->bReadOnly );
1116 if( pParse->oom ) return 0;
1117 if( pParse->nBlobAlloc>0 ) return 1;
1118 aOld = pParse->aBlob;
1119 nSize = pParse->nBlob + nExtra;
1120 pParse->aBlob = 0;
1121 if( jsonBlobExpand(pParse, nSize) ){
1122 return 0;
1124 assert( pParse->nBlobAlloc >= pParse->nBlob + nExtra );
1125 memcpy(pParse->aBlob, aOld, pParse->nBlob);
1126 return 1;
1129 /* Expand pParse->aBlob and append one bytes.
1131 static SQLITE_NOINLINE void jsonBlobExpandAndAppendOneByte(
1132 JsonParse *pParse,
1133 u8 c
1135 jsonBlobExpand(pParse, pParse->nBlob+1);
1136 if( pParse->oom==0 ){
1137 assert( pParse->nBlob+1<=pParse->nBlobAlloc );
1138 pParse->aBlob[pParse->nBlob++] = c;
1142 /* Append a single character.
1144 static void jsonBlobAppendOneByte(JsonParse *pParse, u8 c){
1145 if( pParse->nBlob >= pParse->nBlobAlloc ){
1146 jsonBlobExpandAndAppendOneByte(pParse, c);
1147 }else{
1148 pParse->aBlob[pParse->nBlob++] = c;
1152 /* Slow version of jsonBlobAppendNode() that first resizes the
1153 ** pParse->aBlob structure.
1155 static void jsonBlobAppendNode(JsonParse*,u8,u32,const void*);
1156 static SQLITE_NOINLINE void jsonBlobExpandAndAppendNode(
1157 JsonParse *pParse,
1158 u8 eType,
1159 u32 szPayload,
1160 const void *aPayload
1162 if( jsonBlobExpand(pParse, pParse->nBlob+szPayload+9) ) return;
1163 jsonBlobAppendNode(pParse, eType, szPayload, aPayload);
1167 /* Append an node type byte together with the payload size and
1168 ** possibly also the payload.
1170 ** If aPayload is not NULL, then it is a pointer to the payload which
1171 ** is also appended. If aPayload is NULL, the pParse->aBlob[] array
1172 ** is resized (if necessary) so that it is big enough to hold the
1173 ** payload, but the payload is not appended and pParse->nBlob is left
1174 ** pointing to where the first byte of payload will eventually be.
1176 static void jsonBlobAppendNode(
1177 JsonParse *pParse, /* The JsonParse object under construction */
1178 u8 eType, /* Node type. One of JSONB_* */
1179 u32 szPayload, /* Number of bytes of payload */
1180 const void *aPayload /* The payload. Might be NULL */
1182 u8 *a;
1183 if( pParse->nBlob+szPayload+9 > pParse->nBlobAlloc ){
1184 jsonBlobExpandAndAppendNode(pParse,eType,szPayload,aPayload);
1185 return;
1187 assert( pParse->aBlob!=0 );
1188 a = &pParse->aBlob[pParse->nBlob];
1189 if( szPayload<=11 ){
1190 a[0] = eType | (szPayload<<4);
1191 pParse->nBlob += 1;
1192 }else if( szPayload<=0xff ){
1193 a[0] = eType | 0xc0;
1194 a[1] = szPayload & 0xff;
1195 pParse->nBlob += 2;
1196 }else if( szPayload<=0xffff ){
1197 a[0] = eType | 0xd0;
1198 a[1] = (szPayload >> 8) & 0xff;
1199 a[2] = szPayload & 0xff;
1200 pParse->nBlob += 3;
1201 }else{
1202 a[0] = eType | 0xe0;
1203 a[1] = (szPayload >> 24) & 0xff;
1204 a[2] = (szPayload >> 16) & 0xff;
1205 a[3] = (szPayload >> 8) & 0xff;
1206 a[4] = szPayload & 0xff;
1207 pParse->nBlob += 5;
1209 if( aPayload ){
1210 pParse->nBlob += szPayload;
1211 memcpy(&pParse->aBlob[pParse->nBlob-szPayload], aPayload, szPayload);
1215 /* Change the payload size for the node at index i to be szPayload.
1217 static int jsonBlobChangePayloadSize(
1218 JsonParse *pParse,
1219 u32 i,
1220 u32 szPayload
1222 u8 *a;
1223 u8 szType;
1224 u8 nExtra;
1225 u8 nNeeded;
1226 int delta;
1227 if( pParse->oom ) return 0;
1228 a = &pParse->aBlob[i];
1229 szType = a[0]>>4;
1230 if( szType<=11 ){
1231 nExtra = 0;
1232 }else if( szType==12 ){
1233 nExtra = 1;
1234 }else if( szType==13 ){
1235 nExtra = 2;
1236 }else{
1237 nExtra = 4;
1239 if( szPayload<=11 ){
1240 nNeeded = 0;
1241 }else if( szPayload<=0xff ){
1242 nNeeded = 1;
1243 }else if( szPayload<=0xffff ){
1244 nNeeded = 2;
1245 }else{
1246 nNeeded = 4;
1248 delta = nNeeded - nExtra;
1249 if( delta ){
1250 u32 newSize = pParse->nBlob + delta;
1251 if( delta>0 ){
1252 if( newSize>pParse->nBlobAlloc && jsonBlobExpand(pParse, newSize) ){
1253 return 0; /* OOM error. Error state recorded in pParse->oom. */
1255 a = &pParse->aBlob[i];
1256 memmove(&a[1+delta], &a[1], pParse->nBlob - (i+1));
1257 }else{
1258 memmove(&a[1], &a[1-delta], pParse->nBlob - (i+1-delta));
1260 pParse->nBlob = newSize;
1262 if( nNeeded==0 ){
1263 a[0] = (a[0] & 0x0f) | (szPayload<<4);
1264 }else if( nNeeded==1 ){
1265 a[0] = (a[0] & 0x0f) | 0xc0;
1266 a[1] = szPayload & 0xff;
1267 }else if( nNeeded==2 ){
1268 a[0] = (a[0] & 0x0f) | 0xd0;
1269 a[1] = (szPayload >> 8) & 0xff;
1270 a[2] = szPayload & 0xff;
1271 }else{
1272 a[0] = (a[0] & 0x0f) | 0xe0;
1273 a[1] = (szPayload >> 24) & 0xff;
1274 a[2] = (szPayload >> 16) & 0xff;
1275 a[3] = (szPayload >> 8) & 0xff;
1276 a[4] = szPayload & 0xff;
1278 return delta;
1282 ** If z[0] is 'u' and is followed by exactly 4 hexadecimal character,
1283 ** then set *pOp to JSONB_TEXTJ and return true. If not, do not make
1284 ** any changes to *pOp and return false.
1286 static int jsonIs4HexB(const char *z, int *pOp){
1287 if( z[0]!='u' ) return 0;
1288 if( !jsonIs4Hex(&z[1]) ) return 0;
1289 *pOp = JSONB_TEXTJ;
1290 return 1;
1294 ** Check a single element of the JSONB in pParse for validity.
1296 ** The element to be checked starts at offset i and must end at on the
1297 ** last byte before iEnd.
1299 ** Return 0 if everything is correct. Return the 1-based byte offset of the
1300 ** error if a problem is detected. (In other words, if the error is at offset
1301 ** 0, return 1).
1303 static u32 jsonbValidityCheck(
1304 const JsonParse *pParse, /* Input JSONB. Only aBlob and nBlob are used */
1305 u32 i, /* Start of element as pParse->aBlob[i] */
1306 u32 iEnd, /* One more than the last byte of the element */
1307 u32 iDepth /* Current nesting depth */
1309 u32 n, sz, j, k;
1310 const u8 *z;
1311 u8 x;
1312 if( iDepth>JSON_MAX_DEPTH ) return i+1;
1313 sz = 0;
1314 n = jsonbPayloadSize(pParse, i, &sz);
1315 if( NEVER(n==0) ) return i+1; /* Checked by caller */
1316 if( NEVER(i+n+sz!=iEnd) ) return i+1; /* Checked by caller */
1317 z = pParse->aBlob;
1318 x = z[i] & 0x0f;
1319 switch( x ){
1320 case JSONB_NULL:
1321 case JSONB_TRUE:
1322 case JSONB_FALSE: {
1323 return n+sz==1 ? 0 : i+1;
1325 case JSONB_INT: {
1326 if( sz<1 ) return i+1;
1327 j = i+n;
1328 if( z[j]=='-' ){
1329 j++;
1330 if( sz<2 ) return i+1;
1332 k = i+n+sz;
1333 while( j<k ){
1334 if( sqlite3Isdigit(z[j]) ){
1335 j++;
1336 }else{
1337 return j+1;
1340 return 0;
1342 case JSONB_INT5: {
1343 if( sz<3 ) return i+1;
1344 j = i+n;
1345 if( z[j]=='-' ){
1346 if( sz<4 ) return i+1;
1347 j++;
1349 if( z[j]!='0' ) return i+1;
1350 if( z[j+1]!='x' && z[j+1]!='X' ) return j+2;
1351 j += 2;
1352 k = i+n+sz;
1353 while( j<k ){
1354 if( sqlite3Isxdigit(z[j]) ){
1355 j++;
1356 }else{
1357 return j+1;
1360 return 0;
1362 case JSONB_FLOAT:
1363 case JSONB_FLOAT5: {
1364 u8 seen = 0; /* 0: initial. 1: '.' seen 2: 'e' seen */
1365 if( sz<2 ) return i+1;
1366 j = i+n;
1367 k = j+sz;
1368 if( z[j]=='-' ){
1369 j++;
1370 if( sz<3 ) return i+1;
1372 if( z[j]=='.' ){
1373 if( x==JSONB_FLOAT ) return j+1;
1374 if( !sqlite3Isdigit(z[j+1]) ) return j+1;
1375 j += 2;
1376 seen = 1;
1377 }else if( z[j]=='0' && x==JSONB_FLOAT ){
1378 if( j+3>k ) return j+1;
1379 if( z[j+1]!='.' && z[j+1]!='e' && z[j+1]!='E' ) return j+1;
1380 j++;
1382 for(; j<k; j++){
1383 if( sqlite3Isdigit(z[j]) ) continue;
1384 if( z[j]=='.' ){
1385 if( seen>0 ) return j+1;
1386 if( x==JSONB_FLOAT && (j==k-1 || !sqlite3Isdigit(z[j+1])) ){
1387 return j+1;
1389 seen = 1;
1390 continue;
1392 if( z[j]=='e' || z[j]=='E' ){
1393 if( seen==2 ) return j+1;
1394 if( j==k-1 ) return j+1;
1395 if( z[j+1]=='+' || z[j+1]=='-' ){
1396 j++;
1397 if( j==k-1 ) return j+1;
1399 seen = 2;
1400 continue;
1402 return j+1;
1404 if( seen==0 ) return i+1;
1405 return 0;
1407 case JSONB_TEXT: {
1408 j = i+n;
1409 k = j+sz;
1410 while( j<k ){
1411 if( !jsonIsOk[z[j]] && z[j]!='\'' ) return j+1;
1412 j++;
1414 return 0;
1416 case JSONB_TEXTJ:
1417 case JSONB_TEXT5: {
1418 j = i+n;
1419 k = j+sz;
1420 while( j<k ){
1421 if( !jsonIsOk[z[j]] && z[j]!='\'' ){
1422 if( z[j]=='"' ){
1423 if( x==JSONB_TEXTJ ) return j+1;
1424 }else if( z[j]<=0x1f ){
1425 /* Control characters in JSON5 string literals are ok */
1426 if( x==JSONB_TEXTJ ) return j+1;
1427 }else if( NEVER(z[j]!='\\') || j+1>=k ){
1428 return j+1;
1429 }else if( strchr("\"\\/bfnrt",z[j+1])!=0 ){
1430 j++;
1431 }else if( z[j+1]=='u' ){
1432 if( j+5>=k ) return j+1;
1433 if( !jsonIs4Hex((const char*)&z[j+2]) ) return j+1;
1434 j++;
1435 }else if( x!=JSONB_TEXT5 ){
1436 return j+1;
1437 }else{
1438 u32 c = 0;
1439 u32 szC = jsonUnescapeOneChar((const char*)&z[j], k-j, &c);
1440 if( c==JSON_INVALID_CHAR ) return j+1;
1441 j += szC - 1;
1444 j++;
1446 return 0;
1448 case JSONB_TEXTRAW: {
1449 return 0;
1451 case JSONB_ARRAY: {
1452 u32 sub;
1453 j = i+n;
1454 k = j+sz;
1455 while( j<k ){
1456 sz = 0;
1457 n = jsonbPayloadSize(pParse, j, &sz);
1458 if( n==0 ) return j+1;
1459 if( j+n+sz>k ) return j+1;
1460 sub = jsonbValidityCheck(pParse, j, j+n+sz, iDepth+1);
1461 if( sub ) return sub;
1462 j += n + sz;
1464 assert( j==k );
1465 return 0;
1467 case JSONB_OBJECT: {
1468 u32 cnt = 0;
1469 u32 sub;
1470 j = i+n;
1471 k = j+sz;
1472 while( j<k ){
1473 sz = 0;
1474 n = jsonbPayloadSize(pParse, j, &sz);
1475 if( n==0 ) return j+1;
1476 if( j+n+sz>k ) return j+1;
1477 if( (cnt & 1)==0 ){
1478 x = z[j] & 0x0f;
1479 if( x<JSONB_TEXT || x>JSONB_TEXTRAW ) return j+1;
1481 sub = jsonbValidityCheck(pParse, j, j+n+sz, iDepth+1);
1482 if( sub ) return sub;
1483 cnt++;
1484 j += n + sz;
1486 assert( j==k );
1487 if( (cnt & 1)!=0 ) return j+1;
1488 return 0;
1490 default: {
1491 return i+1;
1497 ** Translate a single element of JSON text at pParse->zJson[i] into
1498 ** its equivalent binary JSONB representation. Append the translation into
1499 ** pParse->aBlob[] beginning at pParse->nBlob. The size of
1500 ** pParse->aBlob[] is increased as necessary.
1502 ** Return the index of the first character past the end of the element parsed,
1503 ** or one of the following special result codes:
1505 ** 0 End of input
1506 ** -1 Syntax error or OOM
1507 ** -2 '}' seen \
1508 ** -3 ']' seen \___ For these returns, pParse->iErr is set to
1509 ** -4 ',' seen / the index in zJson[] of the seen character
1510 ** -5 ':' seen /
1512 static int jsonTranslateTextToBlob(JsonParse *pParse, u32 i){
1513 char c;
1514 u32 j;
1515 u32 iThis, iStart;
1516 int x;
1517 u8 t;
1518 const char *z = pParse->zJson;
1519 json_parse_restart:
1520 switch( (u8)z[i] ){
1521 case '{': {
1522 /* Parse object */
1523 iThis = pParse->nBlob;
1524 jsonBlobAppendNode(pParse, JSONB_OBJECT, pParse->nJson-i, 0);
1525 if( ++pParse->iDepth > JSON_MAX_DEPTH ){
1526 pParse->iErr = i;
1527 return -1;
1529 iStart = pParse->nBlob;
1530 for(j=i+1;;j++){
1531 u32 iBlob = pParse->nBlob;
1532 x = jsonTranslateTextToBlob(pParse, j);
1533 if( x<=0 ){
1534 int op;
1535 if( x==(-2) ){
1536 j = pParse->iErr;
1537 if( pParse->nBlob!=(u32)iStart ) pParse->hasNonstd = 1;
1538 break;
1540 j += json5Whitespace(&z[j]);
1541 op = JSONB_TEXT;
1542 if( sqlite3JsonId1(z[j])
1543 || (z[j]=='\\' && jsonIs4HexB(&z[j+1], &op))
1545 int k = j+1;
1546 while( (sqlite3JsonId2(z[k]) && json5Whitespace(&z[k])==0)
1547 || (z[k]=='\\' && jsonIs4HexB(&z[k+1], &op))
1549 k++;
1551 assert( iBlob==pParse->nBlob );
1552 jsonBlobAppendNode(pParse, op, k-j, &z[j]);
1553 pParse->hasNonstd = 1;
1554 x = k;
1555 }else{
1556 if( x!=-1 ) pParse->iErr = j;
1557 return -1;
1560 if( pParse->oom ) return -1;
1561 t = pParse->aBlob[iBlob] & 0x0f;
1562 if( t<JSONB_TEXT || t>JSONB_TEXTRAW ){
1563 pParse->iErr = j;
1564 return -1;
1566 j = x;
1567 if( z[j]==':' ){
1568 j++;
1569 }else{
1570 if( jsonIsspace(z[j]) ){
1571 /* strspn() is not helpful here */
1572 do{ j++; }while( jsonIsspace(z[j]) );
1573 if( z[j]==':' ){
1574 j++;
1575 goto parse_object_value;
1578 x = jsonTranslateTextToBlob(pParse, j);
1579 if( x!=(-5) ){
1580 if( x!=(-1) ) pParse->iErr = j;
1581 return -1;
1583 j = pParse->iErr+1;
1585 parse_object_value:
1586 x = jsonTranslateTextToBlob(pParse, j);
1587 if( x<=0 ){
1588 if( x!=(-1) ) pParse->iErr = j;
1589 return -1;
1591 j = x;
1592 if( z[j]==',' ){
1593 continue;
1594 }else if( z[j]=='}' ){
1595 break;
1596 }else{
1597 if( jsonIsspace(z[j]) ){
1598 j += 1 + (u32)strspn(&z[j+1], jsonSpaces);
1599 if( z[j]==',' ){
1600 continue;
1601 }else if( z[j]=='}' ){
1602 break;
1605 x = jsonTranslateTextToBlob(pParse, j);
1606 if( x==(-4) ){
1607 j = pParse->iErr;
1608 continue;
1610 if( x==(-2) ){
1611 j = pParse->iErr;
1612 break;
1615 pParse->iErr = j;
1616 return -1;
1618 jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart);
1619 pParse->iDepth--;
1620 return j+1;
1622 case '[': {
1623 /* Parse array */
1624 iThis = pParse->nBlob;
1625 assert( i<=(u32)pParse->nJson );
1626 jsonBlobAppendNode(pParse, JSONB_ARRAY, pParse->nJson - i, 0);
1627 iStart = pParse->nBlob;
1628 if( pParse->oom ) return -1;
1629 if( ++pParse->iDepth > JSON_MAX_DEPTH ){
1630 pParse->iErr = i;
1631 return -1;
1633 for(j=i+1;;j++){
1634 x = jsonTranslateTextToBlob(pParse, j);
1635 if( x<=0 ){
1636 if( x==(-3) ){
1637 j = pParse->iErr;
1638 if( pParse->nBlob!=iStart ) pParse->hasNonstd = 1;
1639 break;
1641 if( x!=(-1) ) pParse->iErr = j;
1642 return -1;
1644 j = x;
1645 if( z[j]==',' ){
1646 continue;
1647 }else if( z[j]==']' ){
1648 break;
1649 }else{
1650 if( jsonIsspace(z[j]) ){
1651 j += 1 + (u32)strspn(&z[j+1], jsonSpaces);
1652 if( z[j]==',' ){
1653 continue;
1654 }else if( z[j]==']' ){
1655 break;
1658 x = jsonTranslateTextToBlob(pParse, j);
1659 if( x==(-4) ){
1660 j = pParse->iErr;
1661 continue;
1663 if( x==(-3) ){
1664 j = pParse->iErr;
1665 break;
1668 pParse->iErr = j;
1669 return -1;
1671 jsonBlobChangePayloadSize(pParse, iThis, pParse->nBlob - iStart);
1672 pParse->iDepth--;
1673 return j+1;
1675 case '\'': {
1676 u8 opcode;
1677 char cDelim;
1678 pParse->hasNonstd = 1;
1679 opcode = JSONB_TEXT;
1680 goto parse_string;
1681 case '"':
1682 /* Parse string */
1683 opcode = JSONB_TEXT;
1684 parse_string:
1685 cDelim = z[i];
1686 j = i+1;
1687 while( 1 /*exit-by-break*/ ){
1688 if( jsonIsOk[(u8)z[j]] ){
1689 if( !jsonIsOk[(u8)z[j+1]] ){
1690 j += 1;
1691 }else if( !jsonIsOk[(u8)z[j+2]] ){
1692 j += 2;
1693 }else{
1694 j += 3;
1695 continue;
1698 c = z[j];
1699 if( c==cDelim ){
1700 break;
1701 }else if( c=='\\' ){
1702 c = z[++j];
1703 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
1704 || c=='n' || c=='r' || c=='t'
1705 || (c=='u' && jsonIs4Hex(&z[j+1])) ){
1706 if( opcode==JSONB_TEXT ) opcode = JSONB_TEXTJ;
1707 }else if( c=='\'' || c=='0' || c=='v' || c=='\n'
1708 || (0xe2==(u8)c && 0x80==(u8)z[j+1]
1709 && (0xa8==(u8)z[j+2] || 0xa9==(u8)z[j+2]))
1710 || (c=='x' && jsonIs2Hex(&z[j+1])) ){
1711 opcode = JSONB_TEXT5;
1712 pParse->hasNonstd = 1;
1713 }else if( c=='\r' ){
1714 if( z[j+1]=='\n' ) j++;
1715 opcode = JSONB_TEXT5;
1716 pParse->hasNonstd = 1;
1717 }else{
1718 pParse->iErr = j;
1719 return -1;
1721 }else if( c<=0x1f ){
1722 if( c==0 ){
1723 pParse->iErr = j;
1724 return -1;
1726 /* Control characters are not allowed in canonical JSON string
1727 ** literals, but are allowed in JSON5 string literals. */
1728 opcode = JSONB_TEXT5;
1729 pParse->hasNonstd = 1;
1730 }else if( c=='"' ){
1731 opcode = JSONB_TEXT5;
1733 j++;
1735 jsonBlobAppendNode(pParse, opcode, j-1-i, &z[i+1]);
1736 return j+1;
1738 case 't': {
1739 if( strncmp(z+i,"true",4)==0 && !sqlite3Isalnum(z[i+4]) ){
1740 jsonBlobAppendOneByte(pParse, JSONB_TRUE);
1741 return i+4;
1743 pParse->iErr = i;
1744 return -1;
1746 case 'f': {
1747 if( strncmp(z+i,"false",5)==0 && !sqlite3Isalnum(z[i+5]) ){
1748 jsonBlobAppendOneByte(pParse, JSONB_FALSE);
1749 return i+5;
1751 pParse->iErr = i;
1752 return -1;
1754 case '+': {
1755 u8 seenE;
1756 pParse->hasNonstd = 1;
1757 t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
1758 goto parse_number;
1759 case '.':
1760 if( sqlite3Isdigit(z[i+1]) ){
1761 pParse->hasNonstd = 1;
1762 t = 0x03; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
1763 seenE = 0;
1764 goto parse_number_2;
1766 pParse->iErr = i;
1767 return -1;
1768 case '-':
1769 case '0':
1770 case '1':
1771 case '2':
1772 case '3':
1773 case '4':
1774 case '5':
1775 case '6':
1776 case '7':
1777 case '8':
1778 case '9':
1779 /* Parse number */
1780 t = 0x00; /* Bit 0x01: JSON5. Bit 0x02: FLOAT */
1781 parse_number:
1782 seenE = 0;
1783 assert( '-' < '0' );
1784 assert( '+' < '0' );
1785 assert( '.' < '0' );
1786 c = z[i];
1788 if( c<='0' ){
1789 if( c=='0' ){
1790 if( (z[i+1]=='x' || z[i+1]=='X') && sqlite3Isxdigit(z[i+2]) ){
1791 assert( t==0x00 );
1792 pParse->hasNonstd = 1;
1793 t = 0x01;
1794 for(j=i+3; sqlite3Isxdigit(z[j]); j++){}
1795 goto parse_number_finish;
1796 }else if( sqlite3Isdigit(z[i+1]) ){
1797 pParse->iErr = i+1;
1798 return -1;
1800 }else{
1801 if( !sqlite3Isdigit(z[i+1]) ){
1802 /* JSON5 allows for "+Infinity" and "-Infinity" using exactly
1803 ** that case. SQLite also allows these in any case and it allows
1804 ** "+inf" and "-inf". */
1805 if( (z[i+1]=='I' || z[i+1]=='i')
1806 && sqlite3StrNICmp(&z[i+1], "inf",3)==0
1808 pParse->hasNonstd = 1;
1809 if( z[i]=='-' ){
1810 jsonBlobAppendNode(pParse, JSONB_FLOAT, 6, "-9e999");
1811 }else{
1812 jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999");
1814 return i + (sqlite3StrNICmp(&z[i+4],"inity",5)==0 ? 9 : 4);
1816 if( z[i+1]=='.' ){
1817 pParse->hasNonstd = 1;
1818 t |= 0x01;
1819 goto parse_number_2;
1821 pParse->iErr = i;
1822 return -1;
1824 if( z[i+1]=='0' ){
1825 if( sqlite3Isdigit(z[i+2]) ){
1826 pParse->iErr = i+1;
1827 return -1;
1828 }else if( (z[i+2]=='x' || z[i+2]=='X') && sqlite3Isxdigit(z[i+3]) ){
1829 pParse->hasNonstd = 1;
1830 t |= 0x01;
1831 for(j=i+4; sqlite3Isxdigit(z[j]); j++){}
1832 goto parse_number_finish;
1838 parse_number_2:
1839 for(j=i+1;; j++){
1840 c = z[j];
1841 if( sqlite3Isdigit(c) ) continue;
1842 if( c=='.' ){
1843 if( (t & 0x02)!=0 ){
1844 pParse->iErr = j;
1845 return -1;
1847 t |= 0x02;
1848 continue;
1850 if( c=='e' || c=='E' ){
1851 if( z[j-1]<'0' ){
1852 if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){
1853 pParse->hasNonstd = 1;
1854 t |= 0x01;
1855 }else{
1856 pParse->iErr = j;
1857 return -1;
1860 if( seenE ){
1861 pParse->iErr = j;
1862 return -1;
1864 t |= 0x02;
1865 seenE = 1;
1866 c = z[j+1];
1867 if( c=='+' || c=='-' ){
1868 j++;
1869 c = z[j+1];
1871 if( c<'0' || c>'9' ){
1872 pParse->iErr = j;
1873 return -1;
1875 continue;
1877 break;
1879 if( z[j-1]<'0' ){
1880 if( ALWAYS(z[j-1]=='.') && ALWAYS(j-2>=i) && sqlite3Isdigit(z[j-2]) ){
1881 pParse->hasNonstd = 1;
1882 t |= 0x01;
1883 }else{
1884 pParse->iErr = j;
1885 return -1;
1888 parse_number_finish:
1889 assert( JSONB_INT+0x01==JSONB_INT5 );
1890 assert( JSONB_FLOAT+0x01==JSONB_FLOAT5 );
1891 assert( JSONB_INT+0x02==JSONB_FLOAT );
1892 if( z[i]=='+' ) i++;
1893 jsonBlobAppendNode(pParse, JSONB_INT+t, j-i, &z[i]);
1894 return j;
1896 case '}': {
1897 pParse->iErr = i;
1898 return -2; /* End of {...} */
1900 case ']': {
1901 pParse->iErr = i;
1902 return -3; /* End of [...] */
1904 case ',': {
1905 pParse->iErr = i;
1906 return -4; /* List separator */
1908 case ':': {
1909 pParse->iErr = i;
1910 return -5; /* Object label/value separator */
1912 case 0: {
1913 return 0; /* End of file */
1915 case 0x09:
1916 case 0x0a:
1917 case 0x0d:
1918 case 0x20: {
1919 i += 1 + (u32)strspn(&z[i+1], jsonSpaces);
1920 goto json_parse_restart;
1922 case 0x0b:
1923 case 0x0c:
1924 case '/':
1925 case 0xc2:
1926 case 0xe1:
1927 case 0xe2:
1928 case 0xe3:
1929 case 0xef: {
1930 j = json5Whitespace(&z[i]);
1931 if( j>0 ){
1932 i += j;
1933 pParse->hasNonstd = 1;
1934 goto json_parse_restart;
1936 pParse->iErr = i;
1937 return -1;
1939 case 'n': {
1940 if( strncmp(z+i,"null",4)==0 && !sqlite3Isalnum(z[i+4]) ){
1941 jsonBlobAppendOneByte(pParse, JSONB_NULL);
1942 return i+4;
1944 /* fall-through into the default case that checks for NaN */
1945 /* no break */ deliberate_fall_through
1947 default: {
1948 u32 k;
1949 int nn;
1950 c = z[i];
1951 for(k=0; k<sizeof(aNanInfName)/sizeof(aNanInfName[0]); k++){
1952 if( c!=aNanInfName[k].c1 && c!=aNanInfName[k].c2 ) continue;
1953 nn = aNanInfName[k].n;
1954 if( sqlite3StrNICmp(&z[i], aNanInfName[k].zMatch, nn)!=0 ){
1955 continue;
1957 if( sqlite3Isalnum(z[i+nn]) ) continue;
1958 if( aNanInfName[k].eType==JSONB_FLOAT ){
1959 jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999");
1960 }else{
1961 jsonBlobAppendOneByte(pParse, JSONB_NULL);
1963 pParse->hasNonstd = 1;
1964 return i + nn;
1966 pParse->iErr = i;
1967 return -1; /* Syntax error */
1969 } /* End switch(z[i]) */
1974 ** Parse a complete JSON string. Return 0 on success or non-zero if there
1975 ** are any errors. If an error occurs, free all memory held by pParse,
1976 ** but not pParse itself.
1978 ** pParse must be initialized to an empty parse object prior to calling
1979 ** this routine.
1981 static int jsonConvertTextToBlob(
1982 JsonParse *pParse, /* Initialize and fill this JsonParse object */
1983 sqlite3_context *pCtx /* Report errors here */
1985 int i;
1986 const char *zJson = pParse->zJson;
1987 i = jsonTranslateTextToBlob(pParse, 0);
1988 if( pParse->oom ) i = -1;
1989 if( i>0 ){
1990 #ifdef SQLITE_DEBUG
1991 assert( pParse->iDepth==0 );
1992 if( sqlite3Config.bJsonSelfcheck ){
1993 assert( jsonbValidityCheck(pParse, 0, pParse->nBlob, 0)==0 );
1995 #endif
1996 while( jsonIsspace(zJson[i]) ) i++;
1997 if( zJson[i] ){
1998 i += json5Whitespace(&zJson[i]);
1999 if( zJson[i] ){
2000 if( pCtx ) sqlite3_result_error(pCtx, "malformed JSON", -1);
2001 jsonParseReset(pParse);
2002 return 1;
2004 pParse->hasNonstd = 1;
2007 if( i<=0 ){
2008 if( pCtx!=0 ){
2009 if( pParse->oom ){
2010 sqlite3_result_error_nomem(pCtx);
2011 }else{
2012 sqlite3_result_error(pCtx, "malformed JSON", -1);
2015 jsonParseReset(pParse);
2016 return 1;
2018 return 0;
2022 ** The input string pStr is a well-formed JSON text string. Convert
2023 ** this into the JSONB format and make it the return value of the
2024 ** SQL function.
2026 static void jsonReturnStringAsBlob(JsonString *pStr){
2027 JsonParse px;
2028 memset(&px, 0, sizeof(px));
2029 jsonStringTerminate(pStr);
2030 if( pStr->eErr ){
2031 sqlite3_result_error_nomem(pStr->pCtx);
2032 return;
2034 px.zJson = pStr->zBuf;
2035 px.nJson = pStr->nUsed;
2036 px.db = sqlite3_context_db_handle(pStr->pCtx);
2037 (void)jsonTranslateTextToBlob(&px, 0);
2038 if( px.oom ){
2039 sqlite3DbFree(px.db, px.aBlob);
2040 sqlite3_result_error_nomem(pStr->pCtx);
2041 }else{
2042 assert( px.nBlobAlloc>0 );
2043 assert( !px.bReadOnly );
2044 sqlite3_result_blob(pStr->pCtx, px.aBlob, px.nBlob, SQLITE_DYNAMIC);
2048 /* The byte at index i is a node type-code. This routine
2049 ** determines the payload size for that node and writes that
2050 ** payload size in to *pSz. It returns the offset from i to the
2051 ** beginning of the payload. Return 0 on error.
2053 static u32 jsonbPayloadSize(const JsonParse *pParse, u32 i, u32 *pSz){
2054 u8 x;
2055 u32 sz;
2056 u32 n;
2057 if( NEVER(i>pParse->nBlob) ){
2058 *pSz = 0;
2059 return 0;
2061 x = pParse->aBlob[i]>>4;
2062 if( x<=11 ){
2063 sz = x;
2064 n = 1;
2065 }else if( x==12 ){
2066 if( i+1>=pParse->nBlob ){
2067 *pSz = 0;
2068 return 0;
2070 sz = pParse->aBlob[i+1];
2071 n = 2;
2072 }else if( x==13 ){
2073 if( i+2>=pParse->nBlob ){
2074 *pSz = 0;
2075 return 0;
2077 sz = (pParse->aBlob[i+1]<<8) + pParse->aBlob[i+2];
2078 n = 3;
2079 }else if( x==14 ){
2080 if( i+4>=pParse->nBlob ){
2081 *pSz = 0;
2082 return 0;
2084 sz = ((u32)pParse->aBlob[i+1]<<24) + (pParse->aBlob[i+2]<<16) +
2085 (pParse->aBlob[i+3]<<8) + pParse->aBlob[i+4];
2086 n = 5;
2087 }else{
2088 if( i+8>=pParse->nBlob
2089 || pParse->aBlob[i+1]!=0
2090 || pParse->aBlob[i+2]!=0
2091 || pParse->aBlob[i+3]!=0
2092 || pParse->aBlob[i+4]!=0
2094 *pSz = 0;
2095 return 0;
2097 sz = (pParse->aBlob[i+5]<<24) + (pParse->aBlob[i+6]<<16) +
2098 (pParse->aBlob[i+7]<<8) + pParse->aBlob[i+8];
2099 n = 9;
2101 if( (i64)i+sz+n > pParse->nBlob
2102 && (i64)i+sz+n > pParse->nBlob-pParse->delta
2104 sz = 0;
2105 n = 0;
2107 *pSz = sz;
2108 return n;
2113 ** Translate the binary JSONB representation of JSON beginning at
2114 ** pParse->aBlob[i] into a JSON text string. Append the JSON
2115 ** text onto the end of pOut. Return the index in pParse->aBlob[]
2116 ** of the first byte past the end of the element that is translated.
2118 ** If an error is detected in the BLOB input, the pOut->eErr flag
2119 ** might get set to JSTRING_MALFORMED. But not all BLOB input errors
2120 ** are detected. So a malformed JSONB input might either result
2121 ** in an error, or in incorrect JSON.
2123 ** The pOut->eErr JSTRING_OOM flag is set on a OOM.
2125 static u32 jsonTranslateBlobToText(
2126 const JsonParse *pParse, /* the complete parse of the JSON */
2127 u32 i, /* Start rendering at this index */
2128 JsonString *pOut /* Write JSON here */
2130 u32 sz, n, j, iEnd;
2132 n = jsonbPayloadSize(pParse, i, &sz);
2133 if( n==0 ){
2134 pOut->eErr |= JSTRING_MALFORMED;
2135 return pParse->nBlob+1;
2137 switch( pParse->aBlob[i] & 0x0f ){
2138 case JSONB_NULL: {
2139 jsonAppendRawNZ(pOut, "null", 4);
2140 return i+1;
2142 case JSONB_TRUE: {
2143 jsonAppendRawNZ(pOut, "true", 4);
2144 return i+1;
2146 case JSONB_FALSE: {
2147 jsonAppendRawNZ(pOut, "false", 5);
2148 return i+1;
2150 case JSONB_INT:
2151 case JSONB_FLOAT: {
2152 if( sz==0 ) goto malformed_jsonb;
2153 jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n], sz);
2154 break;
2156 case JSONB_INT5: { /* Integer literal in hexadecimal notation */
2157 u32 k = 2;
2158 sqlite3_uint64 u = 0;
2159 const char *zIn = (const char*)&pParse->aBlob[i+n];
2160 int bOverflow = 0;
2161 if( sz==0 ) goto malformed_jsonb;
2162 if( zIn[0]=='-' ){
2163 jsonAppendChar(pOut, '-');
2164 k++;
2165 }else if( zIn[0]=='+' ){
2166 k++;
2168 for(; k<sz; k++){
2169 if( !sqlite3Isxdigit(zIn[k]) ){
2170 pOut->eErr |= JSTRING_MALFORMED;
2171 break;
2172 }else if( (u>>60)!=0 ){
2173 bOverflow = 1;
2174 }else{
2175 u = u*16 + sqlite3HexToInt(zIn[k]);
2178 jsonPrintf(100,pOut,bOverflow?"9.0e999":"%llu", u);
2179 break;
2181 case JSONB_FLOAT5: { /* Float literal missing digits beside "." */
2182 u32 k = 0;
2183 const char *zIn = (const char*)&pParse->aBlob[i+n];
2184 if( sz==0 ) goto malformed_jsonb;
2185 if( zIn[0]=='-' ){
2186 jsonAppendChar(pOut, '-');
2187 k++;
2189 if( zIn[k]=='.' ){
2190 jsonAppendChar(pOut, '0');
2192 for(; k<sz; k++){
2193 jsonAppendChar(pOut, zIn[k]);
2194 if( zIn[k]=='.' && (k+1==sz || !sqlite3Isdigit(zIn[k+1])) ){
2195 jsonAppendChar(pOut, '0');
2198 break;
2200 case JSONB_TEXT:
2201 case JSONB_TEXTJ: {
2202 jsonAppendChar(pOut, '"');
2203 jsonAppendRaw(pOut, (const char*)&pParse->aBlob[i+n], sz);
2204 jsonAppendChar(pOut, '"');
2205 break;
2207 case JSONB_TEXT5: {
2208 const char *zIn;
2209 u32 k;
2210 u32 sz2 = sz;
2211 zIn = (const char*)&pParse->aBlob[i+n];
2212 jsonAppendChar(pOut, '"');
2213 while( sz2>0 ){
2214 for(k=0; k<sz2 && (jsonIsOk[(u8)zIn[k]] || zIn[k]=='\''); k++){}
2215 if( k>0 ){
2216 jsonAppendRawNZ(pOut, zIn, k);
2217 if( k>=sz2 ){
2218 break;
2220 zIn += k;
2221 sz2 -= k;
2223 if( zIn[0]=='"' ){
2224 jsonAppendRawNZ(pOut, "\\\"", 2);
2225 zIn++;
2226 sz2--;
2227 continue;
2229 if( zIn[0]<=0x1f ){
2230 if( pOut->nUsed+7>pOut->nAlloc && jsonStringGrow(pOut,7) ) break;
2231 jsonAppendControlChar(pOut, zIn[0]);
2232 zIn++;
2233 sz2--;
2234 continue;
2236 assert( zIn[0]=='\\' );
2237 assert( sz2>=1 );
2238 if( sz2<2 ){
2239 pOut->eErr |= JSTRING_MALFORMED;
2240 break;
2242 switch( (u8)zIn[1] ){
2243 case '\'':
2244 jsonAppendChar(pOut, '\'');
2245 break;
2246 case 'v':
2247 jsonAppendRawNZ(pOut, "\\u0009", 6);
2248 break;
2249 case 'x':
2250 if( sz2<4 ){
2251 pOut->eErr |= JSTRING_MALFORMED;
2252 sz2 = 2;
2253 break;
2255 jsonAppendRawNZ(pOut, "\\u00", 4);
2256 jsonAppendRawNZ(pOut, &zIn[2], 2);
2257 zIn += 2;
2258 sz2 -= 2;
2259 break;
2260 case '0':
2261 jsonAppendRawNZ(pOut, "\\u0000", 6);
2262 break;
2263 case '\r':
2264 if( sz2>2 && zIn[2]=='\n' ){
2265 zIn++;
2266 sz2--;
2268 break;
2269 case '\n':
2270 break;
2271 case 0xe2:
2272 /* '\' followed by either U+2028 or U+2029 is ignored as
2273 ** whitespace. Not that in UTF8, U+2028 is 0xe2 0x80 0x29.
2274 ** U+2029 is the same except for the last byte */
2275 if( sz2<4
2276 || 0x80!=(u8)zIn[2]
2277 || (0xa8!=(u8)zIn[3] && 0xa9!=(u8)zIn[3])
2279 pOut->eErr |= JSTRING_MALFORMED;
2280 sz2 = 2;
2281 break;
2283 zIn += 2;
2284 sz2 -= 2;
2285 break;
2286 default:
2287 jsonAppendRawNZ(pOut, zIn, 2);
2288 break;
2290 assert( sz2>=2 );
2291 zIn += 2;
2292 sz2 -= 2;
2294 jsonAppendChar(pOut, '"');
2295 break;
2297 case JSONB_TEXTRAW: {
2298 jsonAppendString(pOut, (const char*)&pParse->aBlob[i+n], sz);
2299 break;
2301 case JSONB_ARRAY: {
2302 jsonAppendChar(pOut, '[');
2303 j = i+n;
2304 iEnd = j+sz;
2305 while( j<iEnd && pOut->eErr==0 ){
2306 j = jsonTranslateBlobToText(pParse, j, pOut);
2307 jsonAppendChar(pOut, ',');
2309 if( j>iEnd ) pOut->eErr |= JSTRING_MALFORMED;
2310 if( sz>0 ) jsonStringTrimOneChar(pOut);
2311 jsonAppendChar(pOut, ']');
2312 break;
2314 case JSONB_OBJECT: {
2315 int x = 0;
2316 jsonAppendChar(pOut, '{');
2317 j = i+n;
2318 iEnd = j+sz;
2319 while( j<iEnd && pOut->eErr==0 ){
2320 j = jsonTranslateBlobToText(pParse, j, pOut);
2321 jsonAppendChar(pOut, (x++ & 1) ? ',' : ':');
2323 if( (x & 1)!=0 || j>iEnd ) pOut->eErr |= JSTRING_MALFORMED;
2324 if( sz>0 ) jsonStringTrimOneChar(pOut);
2325 jsonAppendChar(pOut, '}');
2326 break;
2329 default: {
2330 malformed_jsonb:
2331 pOut->eErr |= JSTRING_MALFORMED;
2332 break;
2335 return i+n+sz;
2338 /* Context for recursion of json_pretty()
2340 typedef struct JsonPretty JsonPretty;
2341 struct JsonPretty {
2342 JsonParse *pParse; /* The BLOB being rendered */
2343 JsonString *pOut; /* Generate pretty output into this string */
2344 const char *zIndent; /* Use this text for indentation */
2345 u32 szIndent; /* Bytes in zIndent[] */
2346 u32 nIndent; /* Current level of indentation */
2349 /* Append indentation to the pretty JSON under construction */
2350 static void jsonPrettyIndent(JsonPretty *pPretty){
2351 u32 jj;
2352 for(jj=0; jj<pPretty->nIndent; jj++){
2353 jsonAppendRaw(pPretty->pOut, pPretty->zIndent, pPretty->szIndent);
2358 ** Translate the binary JSONB representation of JSON beginning at
2359 ** pParse->aBlob[i] into a JSON text string. Append the JSON
2360 ** text onto the end of pOut. Return the index in pParse->aBlob[]
2361 ** of the first byte past the end of the element that is translated.
2363 ** This is a variant of jsonTranslateBlobToText() that "pretty-prints"
2364 ** the output. Extra whitespace is inserted to make the JSON easier
2365 ** for humans to read.
2367 ** If an error is detected in the BLOB input, the pOut->eErr flag
2368 ** might get set to JSTRING_MALFORMED. But not all BLOB input errors
2369 ** are detected. So a malformed JSONB input might either result
2370 ** in an error, or in incorrect JSON.
2372 ** The pOut->eErr JSTRING_OOM flag is set on a OOM.
2374 static u32 jsonTranslateBlobToPrettyText(
2375 JsonPretty *pPretty, /* Pretty-printing context */
2376 u32 i /* Start rendering at this index */
2378 u32 sz, n, j, iEnd;
2379 const JsonParse *pParse = pPretty->pParse;
2380 JsonString *pOut = pPretty->pOut;
2381 n = jsonbPayloadSize(pParse, i, &sz);
2382 if( n==0 ){
2383 pOut->eErr |= JSTRING_MALFORMED;
2384 return pParse->nBlob+1;
2386 switch( pParse->aBlob[i] & 0x0f ){
2387 case JSONB_ARRAY: {
2388 j = i+n;
2389 iEnd = j+sz;
2390 jsonAppendChar(pOut, '[');
2391 if( j<iEnd ){
2392 jsonAppendChar(pOut, '\n');
2393 pPretty->nIndent++;
2394 while( pOut->eErr==0 ){
2395 jsonPrettyIndent(pPretty);
2396 j = jsonTranslateBlobToPrettyText(pPretty, j);
2397 if( j>=iEnd ) break;
2398 jsonAppendRawNZ(pOut, ",\n", 2);
2400 jsonAppendChar(pOut, '\n');
2401 pPretty->nIndent--;
2402 jsonPrettyIndent(pPretty);
2404 jsonAppendChar(pOut, ']');
2405 i = iEnd;
2406 break;
2408 case JSONB_OBJECT: {
2409 j = i+n;
2410 iEnd = j+sz;
2411 jsonAppendChar(pOut, '{');
2412 if( j<iEnd ){
2413 jsonAppendChar(pOut, '\n');
2414 pPretty->nIndent++;
2415 while( pOut->eErr==0 ){
2416 jsonPrettyIndent(pPretty);
2417 j = jsonTranslateBlobToText(pParse, j, pOut);
2418 if( j>iEnd ){
2419 pOut->eErr |= JSTRING_MALFORMED;
2420 break;
2422 jsonAppendRawNZ(pOut, ": ", 2);
2423 j = jsonTranslateBlobToPrettyText(pPretty, j);
2424 if( j>=iEnd ) break;
2425 jsonAppendRawNZ(pOut, ",\n", 2);
2427 jsonAppendChar(pOut, '\n');
2428 pPretty->nIndent--;
2429 jsonPrettyIndent(pPretty);
2431 jsonAppendChar(pOut, '}');
2432 i = iEnd;
2433 break;
2435 default: {
2436 i = jsonTranslateBlobToText(pParse, i, pOut);
2437 break;
2440 return i;
2444 /* Return true if the input pJson
2446 ** For performance reasons, this routine does not do a detailed check of the
2447 ** input BLOB to ensure that it is well-formed. Hence, false positives are
2448 ** possible. False negatives should never occur, however.
2450 static int jsonFuncArgMightBeBinary(sqlite3_value *pJson){
2451 u32 sz, n;
2452 const u8 *aBlob;
2453 int nBlob;
2454 JsonParse s;
2455 if( sqlite3_value_type(pJson)!=SQLITE_BLOB ) return 0;
2456 aBlob = sqlite3_value_blob(pJson);
2457 nBlob = sqlite3_value_bytes(pJson);
2458 if( nBlob<1 ) return 0;
2459 if( NEVER(aBlob==0) || (aBlob[0] & 0x0f)>JSONB_OBJECT ) return 0;
2460 memset(&s, 0, sizeof(s));
2461 s.aBlob = (u8*)aBlob;
2462 s.nBlob = nBlob;
2463 n = jsonbPayloadSize(&s, 0, &sz);
2464 if( n==0 ) return 0;
2465 if( sz+n!=(u32)nBlob ) return 0;
2466 if( (aBlob[0] & 0x0f)<=JSONB_FALSE && sz>0 ) return 0;
2467 return sz+n==(u32)nBlob;
2471 ** Given that a JSONB_ARRAY object starts at offset i, return
2472 ** the number of entries in that array.
2474 static u32 jsonbArrayCount(JsonParse *pParse, u32 iRoot){
2475 u32 n, sz, i, iEnd;
2476 u32 k = 0;
2477 n = jsonbPayloadSize(pParse, iRoot, &sz);
2478 iEnd = iRoot+n+sz;
2479 for(i=iRoot+n; n>0 && i<iEnd; i+=sz+n, k++){
2480 n = jsonbPayloadSize(pParse, i, &sz);
2482 return k;
2486 ** Edit the payload size of the element at iRoot by the amount in
2487 ** pParse->delta.
2489 static void jsonAfterEditSizeAdjust(JsonParse *pParse, u32 iRoot){
2490 u32 sz = 0;
2491 u32 nBlob;
2492 assert( pParse->delta!=0 );
2493 assert( pParse->nBlobAlloc >= pParse->nBlob );
2494 nBlob = pParse->nBlob;
2495 pParse->nBlob = pParse->nBlobAlloc;
2496 (void)jsonbPayloadSize(pParse, iRoot, &sz);
2497 pParse->nBlob = nBlob;
2498 sz += pParse->delta;
2499 pParse->delta += jsonBlobChangePayloadSize(pParse, iRoot, sz);
2503 ** Modify the JSONB blob at pParse->aBlob by removing nDel bytes of
2504 ** content beginning at iDel, and replacing them with nIns bytes of
2505 ** content given by aIns.
2507 ** nDel may be zero, in which case no bytes are removed. But iDel is
2508 ** still important as new bytes will be insert beginning at iDel.
2510 ** aIns may be zero, in which case space is created to hold nIns bytes
2511 ** beginning at iDel, but that space is uninitialized.
2513 ** Set pParse->oom if an OOM occurs.
2515 static void jsonBlobEdit(
2516 JsonParse *pParse, /* The JSONB to be modified is in pParse->aBlob */
2517 u32 iDel, /* First byte to be removed */
2518 u32 nDel, /* Number of bytes to remove */
2519 const u8 *aIns, /* Content to insert */
2520 u32 nIns /* Bytes of content to insert */
2522 i64 d = (i64)nIns - (i64)nDel;
2523 if( d!=0 ){
2524 if( pParse->nBlob + d > pParse->nBlobAlloc ){
2525 jsonBlobExpand(pParse, pParse->nBlob+d);
2526 if( pParse->oom ) return;
2528 memmove(&pParse->aBlob[iDel+nIns],
2529 &pParse->aBlob[iDel+nDel],
2530 pParse->nBlob - (iDel+nDel));
2531 pParse->nBlob += d;
2532 pParse->delta += d;
2534 if( nIns && aIns ) memcpy(&pParse->aBlob[iDel], aIns, nIns);
2538 ** Return the number of escaped newlines to be ignored.
2539 ** An escaped newline is a one of the following byte sequences:
2541 ** 0x5c 0x0a
2542 ** 0x5c 0x0d
2543 ** 0x5c 0x0d 0x0a
2544 ** 0x5c 0xe2 0x80 0xa8
2545 ** 0x5c 0xe2 0x80 0xa9
2547 static u32 jsonBytesToBypass(const char *z, u32 n){
2548 u32 i = 0;
2549 while( i+1<n ){
2550 if( z[i]!='\\' ) return i;
2551 if( z[i+1]=='\n' ){
2552 i += 2;
2553 continue;
2555 if( z[i+1]=='\r' ){
2556 if( i+2<n && z[i+2]=='\n' ){
2557 i += 3;
2558 }else{
2559 i += 2;
2561 continue;
2563 if( 0xe2==(u8)z[i+1]
2564 && i+3<n
2565 && 0x80==(u8)z[i+2]
2566 && (0xa8==(u8)z[i+3] || 0xa9==(u8)z[i+3])
2568 i += 4;
2569 continue;
2571 break;
2573 return i;
2577 ** Input z[0..n] defines JSON escape sequence including the leading '\\'.
2578 ** Decode that escape sequence into a single character. Write that
2579 ** character into *piOut. Return the number of bytes in the escape sequence.
2581 ** If there is a syntax error of some kind (for example too few characters
2582 ** after the '\\' to complete the encoding) then *piOut is set to
2583 ** JSON_INVALID_CHAR.
2585 static u32 jsonUnescapeOneChar(const char *z, u32 n, u32 *piOut){
2586 assert( n>0 );
2587 assert( z[0]=='\\' );
2588 if( n<2 ){
2589 *piOut = JSON_INVALID_CHAR;
2590 return n;
2592 switch( (u8)z[1] ){
2593 case 'u': {
2594 u32 v, vlo;
2595 if( n<6 ){
2596 *piOut = JSON_INVALID_CHAR;
2597 return n;
2599 v = jsonHexToInt4(&z[2]);
2600 if( (v & 0xfc00)==0xd800
2601 && n>=12
2602 && z[6]=='\\'
2603 && z[7]=='u'
2604 && ((vlo = jsonHexToInt4(&z[8]))&0xfc00)==0xdc00
2606 *piOut = ((v&0x3ff)<<10) + (vlo&0x3ff) + 0x10000;
2607 return 12;
2608 }else{
2609 *piOut = v;
2610 return 6;
2613 case 'b': { *piOut = '\b'; return 2; }
2614 case 'f': { *piOut = '\f'; return 2; }
2615 case 'n': { *piOut = '\n'; return 2; }
2616 case 'r': { *piOut = '\r'; return 2; }
2617 case 't': { *piOut = '\t'; return 2; }
2618 case 'v': { *piOut = '\v'; return 2; }
2619 case '0': { *piOut = 0; return 2; }
2620 case '\'':
2621 case '"':
2622 case '/':
2623 case '\\':{ *piOut = z[1]; return 2; }
2624 case 'x': {
2625 if( n<4 ){
2626 *piOut = JSON_INVALID_CHAR;
2627 return n;
2629 *piOut = (jsonHexToInt(z[2])<<4) | jsonHexToInt(z[3]);
2630 return 4;
2632 case 0xe2:
2633 case '\r':
2634 case '\n': {
2635 u32 nSkip = jsonBytesToBypass(z, n);
2636 if( nSkip==0 ){
2637 *piOut = JSON_INVALID_CHAR;
2638 return n;
2639 }else if( nSkip==n ){
2640 *piOut = 0;
2641 return n;
2642 }else if( z[nSkip]=='\\' ){
2643 return nSkip + jsonUnescapeOneChar(&z[nSkip], n-nSkip, piOut);
2644 }else{
2645 int sz = sqlite3Utf8ReadLimited((u8*)&z[nSkip], n-nSkip, piOut);
2646 return nSkip + sz;
2649 default: {
2650 *piOut = JSON_INVALID_CHAR;
2651 return 2;
2658 ** Compare two object labels. Return 1 if they are equal and
2659 ** 0 if they differ.
2661 ** In this version, we know that one or the other or both of the
2662 ** two comparands contains an escape sequence.
2664 static SQLITE_NOINLINE int jsonLabelCompareEscaped(
2665 const char *zLeft, /* The left label */
2666 u32 nLeft, /* Size of the left label in bytes */
2667 int rawLeft, /* True if zLeft contains no escapes */
2668 const char *zRight, /* The right label */
2669 u32 nRight, /* Size of the right label in bytes */
2670 int rawRight /* True if zRight is escape-free */
2672 u32 cLeft, cRight;
2673 assert( rawLeft==0 || rawRight==0 );
2674 while( 1 /*exit-by-return*/ ){
2675 if( nLeft==0 ){
2676 cLeft = 0;
2677 }else if( rawLeft || zLeft[0]!='\\' ){
2678 cLeft = ((u8*)zLeft)[0];
2679 if( cLeft>=0xc0 ){
2680 int sz = sqlite3Utf8ReadLimited((u8*)zLeft, nLeft, &cLeft);
2681 zLeft += sz;
2682 nLeft -= sz;
2683 }else{
2684 zLeft++;
2685 nLeft--;
2687 }else{
2688 u32 n = jsonUnescapeOneChar(zLeft, nLeft, &cLeft);
2689 zLeft += n;
2690 assert( n<=nLeft );
2691 nLeft -= n;
2693 if( nRight==0 ){
2694 cRight = 0;
2695 }else if( rawRight || zRight[0]!='\\' ){
2696 cRight = ((u8*)zRight)[0];
2697 if( cRight>=0xc0 ){
2698 int sz = sqlite3Utf8ReadLimited((u8*)zRight, nRight, &cRight);
2699 zRight += sz;
2700 nRight -= sz;
2701 }else{
2702 zRight++;
2703 nRight--;
2705 }else{
2706 u32 n = jsonUnescapeOneChar(zRight, nRight, &cRight);
2707 zRight += n;
2708 assert( n<=nRight );
2709 nRight -= n;
2711 if( cLeft!=cRight ) return 0;
2712 if( cLeft==0 ) return 1;
2717 ** Compare two object labels. Return 1 if they are equal and
2718 ** 0 if they differ. Return -1 if an OOM occurs.
2720 static int jsonLabelCompare(
2721 const char *zLeft, /* The left label */
2722 u32 nLeft, /* Size of the left label in bytes */
2723 int rawLeft, /* True if zLeft contains no escapes */
2724 const char *zRight, /* The right label */
2725 u32 nRight, /* Size of the right label in bytes */
2726 int rawRight /* True if zRight is escape-free */
2728 if( rawLeft && rawRight ){
2729 /* Simpliest case: Neither label contains escapes. A simple
2730 ** memcmp() is sufficient. */
2731 if( nLeft!=nRight ) return 0;
2732 return memcmp(zLeft, zRight, nLeft)==0;
2733 }else{
2734 return jsonLabelCompareEscaped(zLeft, nLeft, rawLeft,
2735 zRight, nRight, rawRight);
2740 ** Error returns from jsonLookupStep()
2742 #define JSON_LOOKUP_ERROR 0xffffffff
2743 #define JSON_LOOKUP_NOTFOUND 0xfffffffe
2744 #define JSON_LOOKUP_PATHERROR 0xfffffffd
2745 #define JSON_LOOKUP_ISERROR(x) ((x)>=JSON_LOOKUP_PATHERROR)
2747 /* Forward declaration */
2748 static u32 jsonLookupStep(JsonParse*,u32,const char*,u32);
2751 /* This helper routine for jsonLookupStep() populates pIns with
2752 ** binary data that is to be inserted into pParse.
2754 ** In the common case, pIns just points to pParse->aIns and pParse->nIns.
2755 ** But if the zPath of the original edit operation includes path elements
2756 ** that go deeper, additional substructure must be created.
2758 ** For example:
2760 ** json_insert('{}', '$.a.b.c', 123);
2762 ** The search stops at '$.a' But additional substructure must be
2763 ** created for the ".b.c" part of the patch so that the final result
2764 ** is: {"a":{"b":{"c"::123}}}. This routine populates pIns with
2765 ** the binary equivalent of {"b":{"c":123}} so that it can be inserted.
2767 ** The caller is responsible for resetting pIns when it has finished
2768 ** using the substructure.
2770 static u32 jsonCreateEditSubstructure(
2771 JsonParse *pParse, /* The original JSONB that is being edited */
2772 JsonParse *pIns, /* Populate this with the blob data to insert */
2773 const char *zTail /* Tail of the path that determins substructure */
2775 static const u8 emptyObject[] = { JSONB_ARRAY, JSONB_OBJECT };
2776 int rc;
2777 memset(pIns, 0, sizeof(*pIns));
2778 pIns->db = pParse->db;
2779 if( zTail[0]==0 ){
2780 /* No substructure. Just insert what is given in pParse. */
2781 pIns->aBlob = pParse->aIns;
2782 pIns->nBlob = pParse->nIns;
2783 rc = 0;
2784 }else{
2785 /* Construct the binary substructure */
2786 pIns->nBlob = 1;
2787 pIns->aBlob = (u8*)&emptyObject[zTail[0]=='.'];
2788 pIns->eEdit = pParse->eEdit;
2789 pIns->nIns = pParse->nIns;
2790 pIns->aIns = pParse->aIns;
2791 rc = jsonLookupStep(pIns, 0, zTail, 0);
2792 pParse->oom |= pIns->oom;
2794 return rc; /* Error code only */
2798 ** Search along zPath to find the Json element specified. Return an
2799 ** index into pParse->aBlob[] for the start of that element's value.
2801 ** If the value found by this routine is the value half of label/value pair
2802 ** within an object, then set pPath->iLabel to the start of the corresponding
2803 ** label, before returning.
2805 ** Return one of the JSON_LOOKUP error codes if problems are seen.
2807 ** This routine will also modify the blob. If pParse->eEdit is one of
2808 ** JEDIT_DEL, JEDIT_REPL, JEDIT_INS, or JEDIT_SET, then changes might be
2809 ** made to the selected value. If an edit is performed, then the return
2810 ** value does not necessarily point to the select element. If an edit
2811 ** is performed, the return value is only useful for detecting error
2812 ** conditions.
2814 static u32 jsonLookupStep(
2815 JsonParse *pParse, /* The JSON to search */
2816 u32 iRoot, /* Begin the search at this element of aBlob[] */
2817 const char *zPath, /* The path to search */
2818 u32 iLabel /* Label if iRoot is a value of in an object */
2820 u32 i, j, k, nKey, sz, n, iEnd, rc;
2821 const char *zKey;
2822 u8 x;
2824 if( zPath[0]==0 ){
2825 if( pParse->eEdit && jsonBlobMakeEditable(pParse, pParse->nIns) ){
2826 n = jsonbPayloadSize(pParse, iRoot, &sz);
2827 sz += n;
2828 if( pParse->eEdit==JEDIT_DEL ){
2829 if( iLabel>0 ){
2830 sz += iRoot - iLabel;
2831 iRoot = iLabel;
2833 jsonBlobEdit(pParse, iRoot, sz, 0, 0);
2834 }else if( pParse->eEdit==JEDIT_INS ){
2835 /* Already exists, so json_insert() is a no-op */
2836 }else{
2837 /* json_set() or json_replace() */
2838 jsonBlobEdit(pParse, iRoot, sz, pParse->aIns, pParse->nIns);
2841 pParse->iLabel = iLabel;
2842 return iRoot;
2844 if( zPath[0]=='.' ){
2845 int rawKey = 1;
2846 x = pParse->aBlob[iRoot];
2847 zPath++;
2848 if( zPath[0]=='"' ){
2849 zKey = zPath + 1;
2850 for(i=1; zPath[i] && zPath[i]!='"'; i++){
2851 if( zPath[i]=='\\' && zPath[i+1]!=0 ) i++;
2853 nKey = i-1;
2854 if( zPath[i] ){
2855 i++;
2856 }else{
2857 return JSON_LOOKUP_PATHERROR;
2859 testcase( nKey==0 );
2860 rawKey = memchr(zKey, '\\', nKey)==0;
2861 }else{
2862 zKey = zPath;
2863 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
2864 nKey = i;
2865 if( nKey==0 ){
2866 return JSON_LOOKUP_PATHERROR;
2869 if( (x & 0x0f)!=JSONB_OBJECT ) return JSON_LOOKUP_NOTFOUND;
2870 n = jsonbPayloadSize(pParse, iRoot, &sz);
2871 j = iRoot + n; /* j is the index of a label */
2872 iEnd = j+sz;
2873 while( j<iEnd ){
2874 int rawLabel;
2875 const char *zLabel;
2876 x = pParse->aBlob[j] & 0x0f;
2877 if( x<JSONB_TEXT || x>JSONB_TEXTRAW ) return JSON_LOOKUP_ERROR;
2878 n = jsonbPayloadSize(pParse, j, &sz);
2879 if( n==0 ) return JSON_LOOKUP_ERROR;
2880 k = j+n; /* k is the index of the label text */
2881 if( k+sz>=iEnd ) return JSON_LOOKUP_ERROR;
2882 zLabel = (const char*)&pParse->aBlob[k];
2883 rawLabel = x==JSONB_TEXT || x==JSONB_TEXTRAW;
2884 if( jsonLabelCompare(zKey, nKey, rawKey, zLabel, sz, rawLabel) ){
2885 u32 v = k+sz; /* v is the index of the value */
2886 if( ((pParse->aBlob[v])&0x0f)>JSONB_OBJECT ) return JSON_LOOKUP_ERROR;
2887 n = jsonbPayloadSize(pParse, v, &sz);
2888 if( n==0 || v+n+sz>iEnd ) return JSON_LOOKUP_ERROR;
2889 assert( j>0 );
2890 rc = jsonLookupStep(pParse, v, &zPath[i], j);
2891 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
2892 return rc;
2894 j = k+sz;
2895 if( ((pParse->aBlob[j])&0x0f)>JSONB_OBJECT ) return JSON_LOOKUP_ERROR;
2896 n = jsonbPayloadSize(pParse, j, &sz);
2897 if( n==0 ) return JSON_LOOKUP_ERROR;
2898 j += n+sz;
2900 if( j>iEnd ) return JSON_LOOKUP_ERROR;
2901 if( pParse->eEdit>=JEDIT_INS ){
2902 u32 nIns; /* Total bytes to insert (label+value) */
2903 JsonParse v; /* BLOB encoding of the value to be inserted */
2904 JsonParse ix; /* Header of the label to be inserted */
2905 testcase( pParse->eEdit==JEDIT_INS );
2906 testcase( pParse->eEdit==JEDIT_SET );
2907 memset(&ix, 0, sizeof(ix));
2908 ix.db = pParse->db;
2909 jsonBlobAppendNode(&ix, rawKey?JSONB_TEXTRAW:JSONB_TEXT5, nKey, 0);
2910 pParse->oom |= ix.oom;
2911 rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i]);
2912 if( !JSON_LOOKUP_ISERROR(rc)
2913 && jsonBlobMakeEditable(pParse, ix.nBlob+nKey+v.nBlob)
2915 assert( !pParse->oom );
2916 nIns = ix.nBlob + nKey + v.nBlob;
2917 jsonBlobEdit(pParse, j, 0, 0, nIns);
2918 if( !pParse->oom ){
2919 assert( pParse->aBlob!=0 ); /* Because pParse->oom!=0 */
2920 assert( ix.aBlob!=0 ); /* Because pPasre->oom!=0 */
2921 memcpy(&pParse->aBlob[j], ix.aBlob, ix.nBlob);
2922 k = j + ix.nBlob;
2923 memcpy(&pParse->aBlob[k], zKey, nKey);
2924 k += nKey;
2925 memcpy(&pParse->aBlob[k], v.aBlob, v.nBlob);
2926 if( ALWAYS(pParse->delta) ) jsonAfterEditSizeAdjust(pParse, iRoot);
2929 jsonParseReset(&v);
2930 jsonParseReset(&ix);
2931 return rc;
2933 }else if( zPath[0]=='[' ){
2934 x = pParse->aBlob[iRoot] & 0x0f;
2935 if( x!=JSONB_ARRAY ) return JSON_LOOKUP_NOTFOUND;
2936 n = jsonbPayloadSize(pParse, iRoot, &sz);
2937 k = 0;
2938 i = 1;
2939 while( sqlite3Isdigit(zPath[i]) ){
2940 k = k*10 + zPath[i] - '0';
2941 i++;
2943 if( i<2 || zPath[i]!=']' ){
2944 if( zPath[1]=='#' ){
2945 k = jsonbArrayCount(pParse, iRoot);
2946 i = 2;
2947 if( zPath[2]=='-' && sqlite3Isdigit(zPath[3]) ){
2948 unsigned int nn = 0;
2949 i = 3;
2951 nn = nn*10 + zPath[i] - '0';
2952 i++;
2953 }while( sqlite3Isdigit(zPath[i]) );
2954 if( nn>k ) return JSON_LOOKUP_NOTFOUND;
2955 k -= nn;
2957 if( zPath[i]!=']' ){
2958 return JSON_LOOKUP_PATHERROR;
2960 }else{
2961 return JSON_LOOKUP_PATHERROR;
2964 j = iRoot+n;
2965 iEnd = j+sz;
2966 while( j<iEnd ){
2967 if( k==0 ){
2968 rc = jsonLookupStep(pParse, j, &zPath[i+1], 0);
2969 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
2970 return rc;
2972 k--;
2973 n = jsonbPayloadSize(pParse, j, &sz);
2974 if( n==0 ) return JSON_LOOKUP_ERROR;
2975 j += n+sz;
2977 if( j>iEnd ) return JSON_LOOKUP_ERROR;
2978 if( k>0 ) return JSON_LOOKUP_NOTFOUND;
2979 if( pParse->eEdit>=JEDIT_INS ){
2980 JsonParse v;
2981 testcase( pParse->eEdit==JEDIT_INS );
2982 testcase( pParse->eEdit==JEDIT_SET );
2983 rc = jsonCreateEditSubstructure(pParse, &v, &zPath[i+1]);
2984 if( !JSON_LOOKUP_ISERROR(rc)
2985 && jsonBlobMakeEditable(pParse, v.nBlob)
2987 assert( !pParse->oom );
2988 jsonBlobEdit(pParse, j, 0, v.aBlob, v.nBlob);
2990 jsonParseReset(&v);
2991 if( pParse->delta ) jsonAfterEditSizeAdjust(pParse, iRoot);
2992 return rc;
2994 }else{
2995 return JSON_LOOKUP_PATHERROR;
2997 return JSON_LOOKUP_NOTFOUND;
3001 ** Convert a JSON BLOB into text and make that text the return value
3002 ** of an SQL function.
3004 static void jsonReturnTextJsonFromBlob(
3005 sqlite3_context *ctx,
3006 const u8 *aBlob,
3007 u32 nBlob
3009 JsonParse x;
3010 JsonString s;
3012 if( NEVER(aBlob==0) ) return;
3013 memset(&x, 0, sizeof(x));
3014 x.aBlob = (u8*)aBlob;
3015 x.nBlob = nBlob;
3016 jsonStringInit(&s, ctx);
3017 jsonTranslateBlobToText(&x, 0, &s);
3018 jsonReturnString(&s, 0, 0);
3023 ** Return the value of the BLOB node at index i.
3025 ** If the value is a primitive, return it as an SQL value.
3026 ** If the value is an array or object, return it as either
3027 ** JSON text or the BLOB encoding, depending on the JSON_B flag
3028 ** on the userdata.
3030 static void jsonReturnFromBlob(
3031 JsonParse *pParse, /* Complete JSON parse tree */
3032 u32 i, /* Index of the node */
3033 sqlite3_context *pCtx, /* Return value for this function */
3034 int textOnly /* return text JSON. Disregard user-data */
3036 u32 n, sz;
3037 int rc;
3038 sqlite3 *db = sqlite3_context_db_handle(pCtx);
3040 n = jsonbPayloadSize(pParse, i, &sz);
3041 if( n==0 ){
3042 sqlite3_result_error(pCtx, "malformed JSON", -1);
3043 return;
3045 switch( pParse->aBlob[i] & 0x0f ){
3046 case JSONB_NULL: {
3047 if( sz ) goto returnfromblob_malformed;
3048 sqlite3_result_null(pCtx);
3049 break;
3051 case JSONB_TRUE: {
3052 if( sz ) goto returnfromblob_malformed;
3053 sqlite3_result_int(pCtx, 1);
3054 break;
3056 case JSONB_FALSE: {
3057 if( sz ) goto returnfromblob_malformed;
3058 sqlite3_result_int(pCtx, 0);
3059 break;
3061 case JSONB_INT5:
3062 case JSONB_INT: {
3063 sqlite3_int64 iRes = 0;
3064 char *z;
3065 int bNeg = 0;
3066 char x;
3067 if( sz==0 ) goto returnfromblob_malformed;
3068 x = (char)pParse->aBlob[i+n];
3069 if( x=='-' ){
3070 if( sz<2 ) goto returnfromblob_malformed;
3071 n++;
3072 sz--;
3073 bNeg = 1;
3075 z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz);
3076 if( z==0 ) goto returnfromblob_oom;
3077 rc = sqlite3DecOrHexToI64(z, &iRes);
3078 sqlite3DbFree(db, z);
3079 if( rc==0 ){
3080 sqlite3_result_int64(pCtx, bNeg ? -iRes : iRes);
3081 }else if( rc==3 && bNeg ){
3082 sqlite3_result_int64(pCtx, SMALLEST_INT64);
3083 }else if( rc==1 ){
3084 goto returnfromblob_malformed;
3085 }else{
3086 if( bNeg ){ n--; sz++; }
3087 goto to_double;
3089 break;
3091 case JSONB_FLOAT5:
3092 case JSONB_FLOAT: {
3093 double r;
3094 char *z;
3095 if( sz==0 ) goto returnfromblob_malformed;
3096 to_double:
3097 z = sqlite3DbStrNDup(db, (const char*)&pParse->aBlob[i+n], (int)sz);
3098 if( z==0 ) goto returnfromblob_oom;
3099 rc = sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
3100 sqlite3DbFree(db, z);
3101 if( rc<=0 ) goto returnfromblob_malformed;
3102 sqlite3_result_double(pCtx, r);
3103 break;
3105 case JSONB_TEXTRAW:
3106 case JSONB_TEXT: {
3107 sqlite3_result_text(pCtx, (char*)&pParse->aBlob[i+n], sz,
3108 SQLITE_TRANSIENT);
3109 break;
3111 case JSONB_TEXT5:
3112 case JSONB_TEXTJ: {
3113 /* Translate JSON formatted string into raw text */
3114 u32 iIn, iOut;
3115 const char *z;
3116 char *zOut;
3117 u32 nOut = sz;
3118 z = (const char*)&pParse->aBlob[i+n];
3119 zOut = sqlite3DbMallocRaw(db, nOut+1);
3120 if( zOut==0 ) goto returnfromblob_oom;
3121 for(iIn=iOut=0; iIn<sz; iIn++){
3122 char c = z[iIn];
3123 if( c=='\\' ){
3124 u32 v;
3125 u32 szEscape = jsonUnescapeOneChar(&z[iIn], sz-iIn, &v);
3126 if( v<=0x7f ){
3127 zOut[iOut++] = (char)v;
3128 }else if( v<=0x7ff ){
3129 assert( szEscape>=2 );
3130 zOut[iOut++] = (char)(0xc0 | (v>>6));
3131 zOut[iOut++] = 0x80 | (v&0x3f);
3132 }else if( v<0x10000 ){
3133 assert( szEscape>=3 );
3134 zOut[iOut++] = 0xe0 | (v>>12);
3135 zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
3136 zOut[iOut++] = 0x80 | (v&0x3f);
3137 }else if( v==JSON_INVALID_CHAR ){
3138 /* Silently ignore illegal unicode */
3139 }else{
3140 assert( szEscape>=4 );
3141 zOut[iOut++] = 0xf0 | (v>>18);
3142 zOut[iOut++] = 0x80 | ((v>>12)&0x3f);
3143 zOut[iOut++] = 0x80 | ((v>>6)&0x3f);
3144 zOut[iOut++] = 0x80 | (v&0x3f);
3146 iIn += szEscape - 1;
3147 }else{
3148 zOut[iOut++] = c;
3150 } /* end for() */
3151 assert( iOut<=nOut );
3152 zOut[iOut] = 0;
3153 sqlite3_result_text(pCtx, zOut, iOut, SQLITE_DYNAMIC);
3154 break;
3156 case JSONB_ARRAY:
3157 case JSONB_OBJECT: {
3158 int flags = textOnly ? 0 : SQLITE_PTR_TO_INT(sqlite3_user_data(pCtx));
3159 if( flags & JSON_BLOB ){
3160 sqlite3_result_blob(pCtx, &pParse->aBlob[i], sz+n, SQLITE_TRANSIENT);
3161 }else{
3162 jsonReturnTextJsonFromBlob(pCtx, &pParse->aBlob[i], sz+n);
3164 break;
3166 default: {
3167 goto returnfromblob_malformed;
3170 return;
3172 returnfromblob_oom:
3173 sqlite3_result_error_nomem(pCtx);
3174 return;
3176 returnfromblob_malformed:
3177 sqlite3_result_error(pCtx, "malformed JSON", -1);
3178 return;
3182 ** pArg is a function argument that might be an SQL value or a JSON
3183 ** value. Figure out what it is and encode it as a JSONB blob.
3184 ** Return the results in pParse.
3186 ** pParse is uninitialized upon entry. This routine will handle the
3187 ** initialization of pParse. The result will be contained in
3188 ** pParse->aBlob and pParse->nBlob. pParse->aBlob might be dynamically
3189 ** allocated (if pParse->nBlobAlloc is greater than zero) in which case
3190 ** the caller is responsible for freeing the space allocated to pParse->aBlob
3191 ** when it has finished with it. Or pParse->aBlob might be a static string
3192 ** or a value obtained from sqlite3_value_blob(pArg).
3194 ** If the argument is a BLOB that is clearly not a JSONB, then this
3195 ** function might set an error message in ctx and return non-zero.
3196 ** It might also set an error message and return non-zero on an OOM error.
3198 static int jsonFunctionArgToBlob(
3199 sqlite3_context *ctx,
3200 sqlite3_value *pArg,
3201 JsonParse *pParse
3203 int eType = sqlite3_value_type(pArg);
3204 static u8 aNull[] = { 0x00 };
3205 memset(pParse, 0, sizeof(pParse[0]));
3206 pParse->db = sqlite3_context_db_handle(ctx);
3207 switch( eType ){
3208 default: {
3209 pParse->aBlob = aNull;
3210 pParse->nBlob = 1;
3211 return 0;
3213 case SQLITE_BLOB: {
3214 if( jsonFuncArgMightBeBinary(pArg) ){
3215 pParse->aBlob = (u8*)sqlite3_value_blob(pArg);
3216 pParse->nBlob = sqlite3_value_bytes(pArg);
3217 }else{
3218 sqlite3_result_error(ctx, "JSON cannot hold BLOB values", -1);
3219 return 1;
3221 break;
3223 case SQLITE_TEXT: {
3224 const char *zJson = (const char*)sqlite3_value_text(pArg);
3225 int nJson = sqlite3_value_bytes(pArg);
3226 if( zJson==0 ) return 1;
3227 if( sqlite3_value_subtype(pArg)==JSON_SUBTYPE ){
3228 pParse->zJson = (char*)zJson;
3229 pParse->nJson = nJson;
3230 if( jsonConvertTextToBlob(pParse, ctx) ){
3231 sqlite3_result_error(ctx, "malformed JSON", -1);
3232 sqlite3DbFree(pParse->db, pParse->aBlob);
3233 memset(pParse, 0, sizeof(pParse[0]));
3234 return 1;
3236 }else{
3237 jsonBlobAppendNode(pParse, JSONB_TEXTRAW, nJson, zJson);
3239 break;
3241 case SQLITE_FLOAT: {
3242 double r = sqlite3_value_double(pArg);
3243 if( NEVER(sqlite3IsNaN(r)) ){
3244 jsonBlobAppendNode(pParse, JSONB_NULL, 0, 0);
3245 }else{
3246 int n = sqlite3_value_bytes(pArg);
3247 const char *z = (const char*)sqlite3_value_text(pArg);
3248 if( z==0 ) return 1;
3249 if( z[0]=='I' ){
3250 jsonBlobAppendNode(pParse, JSONB_FLOAT, 5, "9e999");
3251 }else if( z[0]=='-' && z[1]=='I' ){
3252 jsonBlobAppendNode(pParse, JSONB_FLOAT, 6, "-9e999");
3253 }else{
3254 jsonBlobAppendNode(pParse, JSONB_FLOAT, n, z);
3257 break;
3259 case SQLITE_INTEGER: {
3260 int n = sqlite3_value_bytes(pArg);
3261 const char *z = (const char*)sqlite3_value_text(pArg);
3262 if( z==0 ) return 1;
3263 jsonBlobAppendNode(pParse, JSONB_INT, n, z);
3264 break;
3267 if( pParse->oom ){
3268 sqlite3_result_error_nomem(ctx);
3269 return 1;
3270 }else{
3271 return 0;
3276 ** Generate a bad path error.
3278 ** If ctx is not NULL then push the error message into ctx and return NULL.
3279 ** If ctx is NULL, then return the text of the error message.
3281 static char *jsonBadPathError(
3282 sqlite3_context *ctx, /* The function call containing the error */
3283 const char *zPath /* The path with the problem */
3285 char *zMsg = sqlite3_mprintf("bad JSON path: %Q", zPath);
3286 if( ctx==0 ) return zMsg;
3287 if( zMsg ){
3288 sqlite3_result_error(ctx, zMsg, -1);
3289 sqlite3_free(zMsg);
3290 }else{
3291 sqlite3_result_error_nomem(ctx);
3293 return 0;
3296 /* argv[0] is a BLOB that seems likely to be a JSONB. Subsequent
3297 ** arguments come in parse where each pair contains a JSON path and
3298 ** content to insert or set at that patch. Do the updates
3299 ** and return the result.
3301 ** The specific operation is determined by eEdit, which can be one
3302 ** of JEDIT_INS, JEDIT_REPL, or JEDIT_SET.
3304 static void jsonInsertIntoBlob(
3305 sqlite3_context *ctx,
3306 int argc,
3307 sqlite3_value **argv,
3308 int eEdit /* JEDIT_INS, JEDIT_REPL, or JEDIT_SET */
3310 int i;
3311 u32 rc = 0;
3312 const char *zPath = 0;
3313 int flgs;
3314 JsonParse *p;
3315 JsonParse ax;
3317 assert( (argc&1)==1 );
3318 flgs = argc==1 ? 0 : JSON_EDITABLE;
3319 p = jsonParseFuncArg(ctx, argv[0], flgs);
3320 if( p==0 ) return;
3321 for(i=1; i<argc-1; i+=2){
3322 if( sqlite3_value_type(argv[i])==SQLITE_NULL ) continue;
3323 zPath = (const char*)sqlite3_value_text(argv[i]);
3324 if( zPath==0 ){
3325 sqlite3_result_error_nomem(ctx);
3326 jsonParseFree(p);
3327 return;
3329 if( zPath[0]!='$' ) goto jsonInsertIntoBlob_patherror;
3330 if( jsonFunctionArgToBlob(ctx, argv[i+1], &ax) ){
3331 jsonParseReset(&ax);
3332 jsonParseFree(p);
3333 return;
3335 if( zPath[1]==0 ){
3336 if( eEdit==JEDIT_REPL || eEdit==JEDIT_SET ){
3337 jsonBlobEdit(p, 0, p->nBlob, ax.aBlob, ax.nBlob);
3339 rc = 0;
3340 }else{
3341 p->eEdit = eEdit;
3342 p->nIns = ax.nBlob;
3343 p->aIns = ax.aBlob;
3344 p->delta = 0;
3345 rc = jsonLookupStep(p, 0, zPath+1, 0);
3347 jsonParseReset(&ax);
3348 if( rc==JSON_LOOKUP_NOTFOUND ) continue;
3349 if( JSON_LOOKUP_ISERROR(rc) ) goto jsonInsertIntoBlob_patherror;
3351 jsonReturnParse(ctx, p);
3352 jsonParseFree(p);
3353 return;
3355 jsonInsertIntoBlob_patherror:
3356 jsonParseFree(p);
3357 if( rc==JSON_LOOKUP_ERROR ){
3358 sqlite3_result_error(ctx, "malformed JSON", -1);
3359 }else{
3360 jsonBadPathError(ctx, zPath);
3362 return;
3366 ** If pArg is a blob that seems like a JSONB blob, then initialize
3367 ** p to point to that JSONB and return TRUE. If pArg does not seem like
3368 ** a JSONB blob, then return FALSE;
3370 ** This routine is only called if it is already known that pArg is a
3371 ** blob. The only open question is whether or not the blob appears
3372 ** to be a JSONB blob.
3374 static int jsonArgIsJsonb(sqlite3_value *pArg, JsonParse *p){
3375 u32 n, sz = 0;
3376 p->aBlob = (u8*)sqlite3_value_blob(pArg);
3377 p->nBlob = (u32)sqlite3_value_bytes(pArg);
3378 if( p->nBlob==0 ){
3379 p->aBlob = 0;
3380 return 0;
3382 if( NEVER(p->aBlob==0) ){
3383 return 0;
3385 if( (p->aBlob[0] & 0x0f)<=JSONB_OBJECT
3386 && (n = jsonbPayloadSize(p, 0, &sz))>0
3387 && sz+n==p->nBlob
3388 && ((p->aBlob[0] & 0x0f)>JSONB_FALSE || sz==0)
3390 return 1;
3392 p->aBlob = 0;
3393 p->nBlob = 0;
3394 return 0;
3398 ** Generate a JsonParse object, containing valid JSONB in aBlob and nBlob,
3399 ** from the SQL function argument pArg. Return a pointer to the new
3400 ** JsonParse object.
3402 ** Ownership of the new JsonParse object is passed to the caller. The
3403 ** caller should invoke jsonParseFree() on the return value when it
3404 ** has finished using it.
3406 ** If any errors are detected, an appropriate error messages is set
3407 ** using sqlite3_result_error() or the equivalent and this routine
3408 ** returns NULL. This routine also returns NULL if the pArg argument
3409 ** is an SQL NULL value, but no error message is set in that case. This
3410 ** is so that SQL functions that are given NULL arguments will return
3411 ** a NULL value.
3413 static JsonParse *jsonParseFuncArg(
3414 sqlite3_context *ctx,
3415 sqlite3_value *pArg,
3416 u32 flgs
3418 int eType; /* Datatype of pArg */
3419 JsonParse *p = 0; /* Value to be returned */
3420 JsonParse *pFromCache = 0; /* Value taken from cache */
3421 sqlite3 *db; /* The database connection */
3423 assert( ctx!=0 );
3424 eType = sqlite3_value_type(pArg);
3425 if( eType==SQLITE_NULL ){
3426 return 0;
3428 pFromCache = jsonCacheSearch(ctx, pArg);
3429 if( pFromCache ){
3430 pFromCache->nJPRef++;
3431 if( (flgs & JSON_EDITABLE)==0 ){
3432 return pFromCache;
3435 db = sqlite3_context_db_handle(ctx);
3436 rebuild_from_cache:
3437 p = sqlite3DbMallocZero(db, sizeof(*p));
3438 if( p==0 ) goto json_pfa_oom;
3439 memset(p, 0, sizeof(*p));
3440 p->db = db;
3441 p->nJPRef = 1;
3442 if( pFromCache!=0 ){
3443 u32 nBlob = pFromCache->nBlob;
3444 p->aBlob = sqlite3DbMallocRaw(db, nBlob);
3445 if( p->aBlob==0 ) goto json_pfa_oom;
3446 memcpy(p->aBlob, pFromCache->aBlob, nBlob);
3447 p->nBlobAlloc = p->nBlob = nBlob;
3448 p->hasNonstd = pFromCache->hasNonstd;
3449 jsonParseFree(pFromCache);
3450 return p;
3452 if( eType==SQLITE_BLOB ){
3453 if( jsonArgIsJsonb(pArg,p) ){
3454 if( (flgs & JSON_EDITABLE)!=0 && jsonBlobMakeEditable(p, 0)==0 ){
3455 goto json_pfa_oom;
3457 return p;
3459 /* If the blob is not valid JSONB, fall through into trying to cast
3460 ** the blob into text which is then interpreted as JSON. (tag-20240123-a)
3462 ** This goes against all historical documentation about how the SQLite
3463 ** JSON functions were suppose to work. From the beginning, blob was
3464 ** reserved for expansion and a blob value should have raised an error.
3465 ** But it did not, due to a bug. And many applications came to depend
3466 ** upon this buggy behavior, espeically when using the CLI and reading
3467 ** JSON text using readfile(), which returns a blob. For this reason
3468 ** we will continue to support the bug moving forward.
3469 ** See for example https://sqlite.org/forum/forumpost/012136abd5292b8d
3472 p->zJson = (char*)sqlite3_value_text(pArg);
3473 p->nJson = sqlite3_value_bytes(pArg);
3474 if( db->mallocFailed ) goto json_pfa_oom;
3475 if( p->nJson==0 ) goto json_pfa_malformed;
3476 assert( p->zJson!=0 );
3477 if( jsonConvertTextToBlob(p, (flgs & JSON_KEEPERROR) ? 0 : ctx) ){
3478 if( flgs & JSON_KEEPERROR ){
3479 p->nErr = 1;
3480 return p;
3481 }else{
3482 jsonParseFree(p);
3483 return 0;
3485 }else{
3486 int isRCStr = sqlite3ValueIsOfClass(pArg, sqlite3RCStrUnref);
3487 int rc;
3488 if( !isRCStr ){
3489 char *zNew = sqlite3RCStrNew( p->nJson );
3490 if( zNew==0 ) goto json_pfa_oom;
3491 memcpy(zNew, p->zJson, p->nJson);
3492 p->zJson = zNew;
3493 p->zJson[p->nJson] = 0;
3494 }else{
3495 sqlite3RCStrRef(p->zJson);
3497 p->bJsonIsRCStr = 1;
3498 rc = jsonCacheInsert(ctx, p);
3499 if( rc==SQLITE_NOMEM ) goto json_pfa_oom;
3500 if( flgs & JSON_EDITABLE ){
3501 pFromCache = p;
3502 p = 0;
3503 goto rebuild_from_cache;
3506 return p;
3508 json_pfa_malformed:
3509 if( flgs & JSON_KEEPERROR ){
3510 p->nErr = 1;
3511 return p;
3512 }else{
3513 jsonParseFree(p);
3514 sqlite3_result_error(ctx, "malformed JSON", -1);
3515 return 0;
3518 json_pfa_oom:
3519 jsonParseFree(pFromCache);
3520 jsonParseFree(p);
3521 sqlite3_result_error_nomem(ctx);
3522 return 0;
3526 ** Make the return value of a JSON function either the raw JSONB blob
3527 ** or make it JSON text, depending on whether the JSON_BLOB flag is
3528 ** set on the function.
3530 static void jsonReturnParse(
3531 sqlite3_context *ctx,
3532 JsonParse *p
3534 int flgs;
3535 if( p->oom ){
3536 sqlite3_result_error_nomem(ctx);
3537 return;
3539 flgs = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
3540 if( flgs & JSON_BLOB ){
3541 if( p->nBlobAlloc>0 && !p->bReadOnly ){
3542 sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_DYNAMIC);
3543 p->nBlobAlloc = 0;
3544 }else{
3545 sqlite3_result_blob(ctx, p->aBlob, p->nBlob, SQLITE_TRANSIENT);
3547 }else{
3548 JsonString s;
3549 jsonStringInit(&s, ctx);
3550 p->delta = 0;
3551 jsonTranslateBlobToText(p, 0, &s);
3552 jsonReturnString(&s, p, ctx);
3553 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3557 /****************************************************************************
3558 ** SQL functions used for testing and debugging
3559 ****************************************************************************/
3561 #if SQLITE_DEBUG
3563 ** Decode JSONB bytes in aBlob[] starting at iStart through but not
3564 ** including iEnd. Indent the
3565 ** content by nIndent spaces.
3567 static void jsonDebugPrintBlob(
3568 JsonParse *pParse, /* JSON content */
3569 u32 iStart, /* Start rendering here */
3570 u32 iEnd, /* Do not render this byte or any byte after this one */
3571 int nIndent, /* Indent by this many spaces */
3572 sqlite3_str *pOut /* Generate output into this sqlite3_str object */
3574 while( iStart<iEnd ){
3575 u32 i, n, nn, sz = 0;
3576 int showContent = 1;
3577 u8 x = pParse->aBlob[iStart] & 0x0f;
3578 u32 savedNBlob = pParse->nBlob;
3579 sqlite3_str_appendf(pOut, "%5d:%*s", iStart, nIndent, "");
3580 if( pParse->nBlobAlloc>pParse->nBlob ){
3581 pParse->nBlob = pParse->nBlobAlloc;
3583 nn = n = jsonbPayloadSize(pParse, iStart, &sz);
3584 if( nn==0 ) nn = 1;
3585 if( sz>0 && x<JSONB_ARRAY ){
3586 nn += sz;
3588 for(i=0; i<nn; i++){
3589 sqlite3_str_appendf(pOut, " %02x", pParse->aBlob[iStart+i]);
3591 if( n==0 ){
3592 sqlite3_str_appendf(pOut, " ERROR invalid node size\n");
3593 iStart = n==0 ? iStart+1 : iEnd;
3594 continue;
3596 pParse->nBlob = savedNBlob;
3597 if( iStart+n+sz>iEnd ){
3598 iEnd = iStart+n+sz;
3599 if( iEnd>pParse->nBlob ){
3600 if( pParse->nBlobAlloc>0 && iEnd>pParse->nBlobAlloc ){
3601 iEnd = pParse->nBlobAlloc;
3602 }else{
3603 iEnd = pParse->nBlob;
3607 sqlite3_str_appendall(pOut," <-- ");
3608 switch( x ){
3609 case JSONB_NULL: sqlite3_str_appendall(pOut,"null"); break;
3610 case JSONB_TRUE: sqlite3_str_appendall(pOut,"true"); break;
3611 case JSONB_FALSE: sqlite3_str_appendall(pOut,"false"); break;
3612 case JSONB_INT: sqlite3_str_appendall(pOut,"int"); break;
3613 case JSONB_INT5: sqlite3_str_appendall(pOut,"int5"); break;
3614 case JSONB_FLOAT: sqlite3_str_appendall(pOut,"float"); break;
3615 case JSONB_FLOAT5: sqlite3_str_appendall(pOut,"float5"); break;
3616 case JSONB_TEXT: sqlite3_str_appendall(pOut,"text"); break;
3617 case JSONB_TEXTJ: sqlite3_str_appendall(pOut,"textj"); break;
3618 case JSONB_TEXT5: sqlite3_str_appendall(pOut,"text5"); break;
3619 case JSONB_TEXTRAW: sqlite3_str_appendall(pOut,"textraw"); break;
3620 case JSONB_ARRAY: {
3621 sqlite3_str_appendf(pOut,"array, %u bytes\n", sz);
3622 jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut);
3623 showContent = 0;
3624 break;
3626 case JSONB_OBJECT: {
3627 sqlite3_str_appendf(pOut, "object, %u bytes\n", sz);
3628 jsonDebugPrintBlob(pParse, iStart+n, iStart+n+sz, nIndent+2, pOut);
3629 showContent = 0;
3630 break;
3632 default: {
3633 sqlite3_str_appendall(pOut, "ERROR: unknown node type\n");
3634 showContent = 0;
3635 break;
3638 if( showContent ){
3639 if( sz==0 && x<=JSONB_FALSE ){
3640 sqlite3_str_append(pOut, "\n", 1);
3641 }else{
3642 u32 j;
3643 sqlite3_str_appendall(pOut, ": \"");
3644 for(j=iStart+n; j<iStart+n+sz; j++){
3645 u8 c = pParse->aBlob[j];
3646 if( c<0x20 || c>=0x7f ) c = '.';
3647 sqlite3_str_append(pOut, (char*)&c, 1);
3649 sqlite3_str_append(pOut, "\"\n", 2);
3652 iStart += n + sz;
3655 static void jsonShowParse(JsonParse *pParse){
3656 sqlite3_str out;
3657 char zBuf[1000];
3658 if( pParse==0 ){
3659 printf("NULL pointer\n");
3660 return;
3661 }else{
3662 printf("nBlobAlloc = %u\n", pParse->nBlobAlloc);
3663 printf("nBlob = %u\n", pParse->nBlob);
3664 printf("delta = %d\n", pParse->delta);
3665 if( pParse->nBlob==0 ) return;
3666 printf("content (bytes 0..%u):\n", pParse->nBlob-1);
3668 sqlite3StrAccumInit(&out, 0, zBuf, sizeof(zBuf), 1000000);
3669 jsonDebugPrintBlob(pParse, 0, pParse->nBlob, 0, &out);
3670 printf("%s", sqlite3_str_value(&out));
3671 sqlite3_str_reset(&out);
3673 #endif /* SQLITE_DEBUG */
3675 #ifdef SQLITE_DEBUG
3677 ** SQL function: json_parse(JSON)
3679 ** Parse JSON using jsonParseFuncArg(). Return text that is a
3680 ** human-readable dump of the binary JSONB for the input parameter.
3682 static void jsonParseFunc(
3683 sqlite3_context *ctx,
3684 int argc,
3685 sqlite3_value **argv
3687 JsonParse *p; /* The parse */
3688 sqlite3_str out;
3690 assert( argc>=1 );
3691 sqlite3StrAccumInit(&out, 0, 0, 0, 1000000);
3692 p = jsonParseFuncArg(ctx, argv[0], 0);
3693 if( p==0 ) return;
3694 if( argc==1 ){
3695 jsonDebugPrintBlob(p, 0, p->nBlob, 0, &out);
3696 sqlite3_result_text64(ctx,out.zText,out.nChar,SQLITE_TRANSIENT,SQLITE_UTF8);
3697 }else{
3698 jsonShowParse(p);
3700 jsonParseFree(p);
3701 sqlite3_str_reset(&out);
3703 #endif /* SQLITE_DEBUG */
3705 /****************************************************************************
3706 ** Scalar SQL function implementations
3707 ****************************************************************************/
3710 ** Implementation of the json_quote(VALUE) function. Return a JSON value
3711 ** corresponding to the SQL value input. Mostly this means putting
3712 ** double-quotes around strings and returning the unquoted string "null"
3713 ** when given a NULL input.
3715 static void jsonQuoteFunc(
3716 sqlite3_context *ctx,
3717 int argc,
3718 sqlite3_value **argv
3720 JsonString jx;
3721 UNUSED_PARAMETER(argc);
3723 jsonStringInit(&jx, ctx);
3724 jsonAppendSqlValue(&jx, argv[0]);
3725 jsonReturnString(&jx, 0, 0);
3726 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3730 ** Implementation of the json_array(VALUE,...) function. Return a JSON
3731 ** array that contains all values given in arguments. Or if any argument
3732 ** is a BLOB, throw an error.
3734 static void jsonArrayFunc(
3735 sqlite3_context *ctx,
3736 int argc,
3737 sqlite3_value **argv
3739 int i;
3740 JsonString jx;
3742 jsonStringInit(&jx, ctx);
3743 jsonAppendChar(&jx, '[');
3744 for(i=0; i<argc; i++){
3745 jsonAppendSeparator(&jx);
3746 jsonAppendSqlValue(&jx, argv[i]);
3748 jsonAppendChar(&jx, ']');
3749 jsonReturnString(&jx, 0, 0);
3750 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3754 ** json_array_length(JSON)
3755 ** json_array_length(JSON, PATH)
3757 ** Return the number of elements in the top-level JSON array.
3758 ** Return 0 if the input is not a well-formed JSON array.
3760 static void jsonArrayLengthFunc(
3761 sqlite3_context *ctx,
3762 int argc,
3763 sqlite3_value **argv
3765 JsonParse *p; /* The parse */
3766 sqlite3_int64 cnt = 0;
3767 u32 i;
3768 u8 eErr = 0;
3770 p = jsonParseFuncArg(ctx, argv[0], 0);
3771 if( p==0 ) return;
3772 if( argc==2 ){
3773 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
3774 if( zPath==0 ){
3775 jsonParseFree(p);
3776 return;
3778 i = jsonLookupStep(p, 0, zPath[0]=='$' ? zPath+1 : "@", 0);
3779 if( JSON_LOOKUP_ISERROR(i) ){
3780 if( i==JSON_LOOKUP_NOTFOUND ){
3781 /* no-op */
3782 }else if( i==JSON_LOOKUP_PATHERROR ){
3783 jsonBadPathError(ctx, zPath);
3784 }else{
3785 sqlite3_result_error(ctx, "malformed JSON", -1);
3787 eErr = 1;
3788 i = 0;
3790 }else{
3791 i = 0;
3793 if( (p->aBlob[i] & 0x0f)==JSONB_ARRAY ){
3794 cnt = jsonbArrayCount(p, i);
3796 if( !eErr ) sqlite3_result_int64(ctx, cnt);
3797 jsonParseFree(p);
3800 /* True if the string is all alphanumerics and underscores */
3801 static int jsonAllAlphanum(const char *z, int n){
3802 int i;
3803 for(i=0; i<n && (sqlite3Isalnum(z[i]) || z[i]=='_'); i++){}
3804 return i==n;
3808 ** json_extract(JSON, PATH, ...)
3809 ** "->"(JSON,PATH)
3810 ** "->>"(JSON,PATH)
3812 ** Return the element described by PATH. Return NULL if that PATH element
3813 ** is not found.
3815 ** If JSON_JSON is set or if more that one PATH argument is supplied then
3816 ** always return a JSON representation of the result. If JSON_SQL is set,
3817 ** then always return an SQL representation of the result. If neither flag
3818 ** is present and argc==2, then return JSON for objects and arrays and SQL
3819 ** for all other values.
3821 ** When multiple PATH arguments are supplied, the result is a JSON array
3822 ** containing the result of each PATH.
3824 ** Abbreviated JSON path expressions are allows if JSON_ABPATH, for
3825 ** compatibility with PG.
3827 static void jsonExtractFunc(
3828 sqlite3_context *ctx,
3829 int argc,
3830 sqlite3_value **argv
3832 JsonParse *p = 0; /* The parse */
3833 int flags; /* Flags associated with the function */
3834 int i; /* Loop counter */
3835 JsonString jx; /* String for array result */
3837 if( argc<2 ) return;
3838 p = jsonParseFuncArg(ctx, argv[0], 0);
3839 if( p==0 ) return;
3840 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
3841 jsonStringInit(&jx, ctx);
3842 if( argc>2 ){
3843 jsonAppendChar(&jx, '[');
3845 for(i=1; i<argc; i++){
3846 /* With a single PATH argument */
3847 const char *zPath = (const char*)sqlite3_value_text(argv[i]);
3848 int nPath;
3849 u32 j;
3850 if( zPath==0 ) goto json_extract_error;
3851 nPath = sqlite3Strlen30(zPath);
3852 if( zPath[0]=='$' ){
3853 j = jsonLookupStep(p, 0, zPath+1, 0);
3854 }else if( (flags & JSON_ABPATH) ){
3855 /* The -> and ->> operators accept abbreviated PATH arguments. This
3856 ** is mostly for compatibility with PostgreSQL, but also for
3857 ** convenience.
3859 ** NUMBER ==> $[NUMBER] // PG compatible
3860 ** LABEL ==> $.LABEL // PG compatible
3861 ** [NUMBER] ==> $[NUMBER] // Not PG. Purely for convenience
3863 ** Updated 2024-05-27: If the NUMBER is negative, then PG counts from
3864 ** the right of the array. Hence for negative NUMBER:
3866 ** NUMBER ==> $[#NUMBER] // PG compatible
3868 jsonStringInit(&jx, ctx);
3869 if( sqlite3_value_type(argv[i])==SQLITE_INTEGER ){
3870 jsonAppendRawNZ(&jx, "[", 1);
3871 if( zPath[0]=='-' ) jsonAppendRawNZ(&jx,"#",1);
3872 jsonAppendRaw(&jx, zPath, nPath);
3873 jsonAppendRawNZ(&jx, "]", 2);
3874 }else if( jsonAllAlphanum(zPath, nPath) ){
3875 jsonAppendRawNZ(&jx, ".", 1);
3876 jsonAppendRaw(&jx, zPath, nPath);
3877 }else if( zPath[0]=='[' && nPath>=3 && zPath[nPath-1]==']' ){
3878 jsonAppendRaw(&jx, zPath, nPath);
3879 }else{
3880 jsonAppendRawNZ(&jx, ".\"", 2);
3881 jsonAppendRaw(&jx, zPath, nPath);
3882 jsonAppendRawNZ(&jx, "\"", 1);
3884 jsonStringTerminate(&jx);
3885 j = jsonLookupStep(p, 0, jx.zBuf, 0);
3886 jsonStringReset(&jx);
3887 }else{
3888 jsonBadPathError(ctx, zPath);
3889 goto json_extract_error;
3891 if( j<p->nBlob ){
3892 if( argc==2 ){
3893 if( flags & JSON_JSON ){
3894 jsonStringInit(&jx, ctx);
3895 jsonTranslateBlobToText(p, j, &jx);
3896 jsonReturnString(&jx, 0, 0);
3897 jsonStringReset(&jx);
3898 assert( (flags & JSON_BLOB)==0 );
3899 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3900 }else{
3901 jsonReturnFromBlob(p, j, ctx, 0);
3902 if( (flags & (JSON_SQL|JSON_BLOB))==0
3903 && (p->aBlob[j]&0x0f)>=JSONB_ARRAY
3905 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3908 }else{
3909 jsonAppendSeparator(&jx);
3910 jsonTranslateBlobToText(p, j, &jx);
3912 }else if( j==JSON_LOOKUP_NOTFOUND ){
3913 if( argc==2 ){
3914 goto json_extract_error; /* Return NULL if not found */
3915 }else{
3916 jsonAppendSeparator(&jx);
3917 jsonAppendRawNZ(&jx, "null", 4);
3919 }else if( j==JSON_LOOKUP_ERROR ){
3920 sqlite3_result_error(ctx, "malformed JSON", -1);
3921 goto json_extract_error;
3922 }else{
3923 jsonBadPathError(ctx, zPath);
3924 goto json_extract_error;
3927 if( argc>2 ){
3928 jsonAppendChar(&jx, ']');
3929 jsonReturnString(&jx, 0, 0);
3930 if( (flags & JSON_BLOB)==0 ){
3931 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
3934 json_extract_error:
3935 jsonStringReset(&jx);
3936 jsonParseFree(p);
3937 return;
3941 ** Return codes for jsonMergePatch()
3943 #define JSON_MERGE_OK 0 /* Success */
3944 #define JSON_MERGE_BADTARGET 1 /* Malformed TARGET blob */
3945 #define JSON_MERGE_BADPATCH 2 /* Malformed PATCH blob */
3946 #define JSON_MERGE_OOM 3 /* Out-of-memory condition */
3949 ** RFC-7396 MergePatch for two JSONB blobs.
3951 ** pTarget is the target. pPatch is the patch. The target is updated
3952 ** in place. The patch is read-only.
3954 ** The original RFC-7396 algorithm is this:
3956 ** define MergePatch(Target, Patch):
3957 ** if Patch is an Object:
3958 ** if Target is not an Object:
3959 ** Target = {} # Ignore the contents and set it to an empty Object
3960 ** for each Name/Value pair in Patch:
3961 ** if Value is null:
3962 ** if Name exists in Target:
3963 ** remove the Name/Value pair from Target
3964 ** else:
3965 ** Target[Name] = MergePatch(Target[Name], Value)
3966 ** return Target
3967 ** else:
3968 ** return Patch
3970 ** Here is an equivalent algorithm restructured to show the actual
3971 ** implementation:
3973 ** 01 define MergePatch(Target, Patch):
3974 ** 02 if Patch is not an Object:
3975 ** 03 return Patch
3976 ** 04 else: // if Patch is an Object
3977 ** 05 if Target is not an Object:
3978 ** 06 Target = {}
3979 ** 07 for each Name/Value pair in Patch:
3980 ** 08 if Name exists in Target:
3981 ** 09 if Value is null:
3982 ** 10 remove the Name/Value pair from Target
3983 ** 11 else
3984 ** 12 Target[name] = MergePatch(Target[Name], Value)
3985 ** 13 else if Value is not NULL:
3986 ** 14 if Value is not an Object:
3987 ** 15 Target[name] = Value
3988 ** 16 else:
3989 ** 17 Target[name] = MergePatch('{}',value)
3990 ** 18 return Target
3991 ** |
3992 ** ^---- Line numbers referenced in comments in the implementation
3994 static int jsonMergePatch(
3995 JsonParse *pTarget, /* The JSON parser that contains the TARGET */
3996 u32 iTarget, /* Index of TARGET in pTarget->aBlob[] */
3997 const JsonParse *pPatch, /* The PATCH */
3998 u32 iPatch /* Index of PATCH in pPatch->aBlob[] */
4000 u8 x; /* Type of a single node */
4001 u32 n, sz=0; /* Return values from jsonbPayloadSize() */
4002 u32 iTCursor; /* Cursor position while scanning the target object */
4003 u32 iTStart; /* First label in the target object */
4004 u32 iTEndBE; /* Original first byte past end of target, before edit */
4005 u32 iTEnd; /* Current first byte past end of target */
4006 u8 eTLabel; /* Node type of the target label */
4007 u32 iTLabel = 0; /* Index of the label */
4008 u32 nTLabel = 0; /* Header size in bytes for the target label */
4009 u32 szTLabel = 0; /* Size of the target label payload */
4010 u32 iTValue = 0; /* Index of the target value */
4011 u32 nTValue = 0; /* Header size of the target value */
4012 u32 szTValue = 0; /* Payload size for the target value */
4014 u32 iPCursor; /* Cursor position while scanning the patch */
4015 u32 iPEnd; /* First byte past the end of the patch */
4016 u8 ePLabel; /* Node type of the patch label */
4017 u32 iPLabel; /* Start of patch label */
4018 u32 nPLabel; /* Size of header on the patch label */
4019 u32 szPLabel; /* Payload size of the patch label */
4020 u32 iPValue; /* Start of patch value */
4021 u32 nPValue; /* Header size for the patch value */
4022 u32 szPValue; /* Payload size of the patch value */
4024 assert( iTarget>=0 && iTarget<pTarget->nBlob );
4025 assert( iPatch>=0 && iPatch<pPatch->nBlob );
4026 x = pPatch->aBlob[iPatch] & 0x0f;
4027 if( x!=JSONB_OBJECT ){ /* Algorithm line 02 */
4028 u32 szPatch; /* Total size of the patch, header+payload */
4029 u32 szTarget; /* Total size of the target, header+payload */
4030 n = jsonbPayloadSize(pPatch, iPatch, &sz);
4031 szPatch = n+sz;
4032 sz = 0;
4033 n = jsonbPayloadSize(pTarget, iTarget, &sz);
4034 szTarget = n+sz;
4035 jsonBlobEdit(pTarget, iTarget, szTarget, pPatch->aBlob+iPatch, szPatch);
4036 return pTarget->oom ? JSON_MERGE_OOM : JSON_MERGE_OK; /* Line 03 */
4038 x = pTarget->aBlob[iTarget] & 0x0f;
4039 if( x!=JSONB_OBJECT ){ /* Algorithm line 05 */
4040 n = jsonbPayloadSize(pTarget, iTarget, &sz);
4041 jsonBlobEdit(pTarget, iTarget+n, sz, 0, 0);
4042 x = pTarget->aBlob[iTarget];
4043 pTarget->aBlob[iTarget] = (x & 0xf0) | JSONB_OBJECT;
4045 n = jsonbPayloadSize(pPatch, iPatch, &sz);
4046 if( NEVER(n==0) ) return JSON_MERGE_BADPATCH;
4047 iPCursor = iPatch+n;
4048 iPEnd = iPCursor+sz;
4049 n = jsonbPayloadSize(pTarget, iTarget, &sz);
4050 if( NEVER(n==0) ) return JSON_MERGE_BADTARGET;
4051 iTStart = iTarget+n;
4052 iTEndBE = iTStart+sz;
4054 while( iPCursor<iPEnd ){ /* Algorithm line 07 */
4055 iPLabel = iPCursor;
4056 ePLabel = pPatch->aBlob[iPCursor] & 0x0f;
4057 if( ePLabel<JSONB_TEXT || ePLabel>JSONB_TEXTRAW ){
4058 return JSON_MERGE_BADPATCH;
4060 nPLabel = jsonbPayloadSize(pPatch, iPCursor, &szPLabel);
4061 if( nPLabel==0 ) return JSON_MERGE_BADPATCH;
4062 iPValue = iPCursor + nPLabel + szPLabel;
4063 if( iPValue>=iPEnd ) return JSON_MERGE_BADPATCH;
4064 nPValue = jsonbPayloadSize(pPatch, iPValue, &szPValue);
4065 if( nPValue==0 ) return JSON_MERGE_BADPATCH;
4066 iPCursor = iPValue + nPValue + szPValue;
4067 if( iPCursor>iPEnd ) return JSON_MERGE_BADPATCH;
4069 iTCursor = iTStart;
4070 iTEnd = iTEndBE + pTarget->delta;
4071 while( iTCursor<iTEnd ){
4072 int isEqual; /* true if the patch and target labels match */
4073 iTLabel = iTCursor;
4074 eTLabel = pTarget->aBlob[iTCursor] & 0x0f;
4075 if( eTLabel<JSONB_TEXT || eTLabel>JSONB_TEXTRAW ){
4076 return JSON_MERGE_BADTARGET;
4078 nTLabel = jsonbPayloadSize(pTarget, iTCursor, &szTLabel);
4079 if( nTLabel==0 ) return JSON_MERGE_BADTARGET;
4080 iTValue = iTLabel + nTLabel + szTLabel;
4081 if( iTValue>=iTEnd ) return JSON_MERGE_BADTARGET;
4082 nTValue = jsonbPayloadSize(pTarget, iTValue, &szTValue);
4083 if( nTValue==0 ) return JSON_MERGE_BADTARGET;
4084 if( iTValue + nTValue + szTValue > iTEnd ) return JSON_MERGE_BADTARGET;
4085 isEqual = jsonLabelCompare(
4086 (const char*)&pPatch->aBlob[iPLabel+nPLabel],
4087 szPLabel,
4088 (ePLabel==JSONB_TEXT || ePLabel==JSONB_TEXTRAW),
4089 (const char*)&pTarget->aBlob[iTLabel+nTLabel],
4090 szTLabel,
4091 (eTLabel==JSONB_TEXT || eTLabel==JSONB_TEXTRAW));
4092 if( isEqual ) break;
4093 iTCursor = iTValue + nTValue + szTValue;
4095 x = pPatch->aBlob[iPValue] & 0x0f;
4096 if( iTCursor<iTEnd ){
4097 /* A match was found. Algorithm line 08 */
4098 if( x==0 ){
4099 /* Patch value is NULL. Algorithm line 09 */
4100 jsonBlobEdit(pTarget, iTLabel, nTLabel+szTLabel+nTValue+szTValue, 0,0);
4101 /* vvvvvv----- No OOM on a delete-only edit */
4102 if( NEVER(pTarget->oom) ) return JSON_MERGE_OOM;
4103 }else{
4104 /* Algorithm line 12 */
4105 int rc, savedDelta = pTarget->delta;
4106 pTarget->delta = 0;
4107 rc = jsonMergePatch(pTarget, iTValue, pPatch, iPValue);
4108 if( rc ) return rc;
4109 pTarget->delta += savedDelta;
4111 }else if( x>0 ){ /* Algorithm line 13 */
4112 /* No match and patch value is not NULL */
4113 u32 szNew = szPLabel+nPLabel;
4114 if( (pPatch->aBlob[iPValue] & 0x0f)!=JSONB_OBJECT ){ /* Line 14 */
4115 jsonBlobEdit(pTarget, iTEnd, 0, 0, szPValue+nPValue+szNew);
4116 if( pTarget->oom ) return JSON_MERGE_OOM;
4117 memcpy(&pTarget->aBlob[iTEnd], &pPatch->aBlob[iPLabel], szNew);
4118 memcpy(&pTarget->aBlob[iTEnd+szNew],
4119 &pPatch->aBlob[iPValue], szPValue+nPValue);
4120 }else{
4121 int rc, savedDelta;
4122 jsonBlobEdit(pTarget, iTEnd, 0, 0, szNew+1);
4123 if( pTarget->oom ) return JSON_MERGE_OOM;
4124 memcpy(&pTarget->aBlob[iTEnd], &pPatch->aBlob[iPLabel], szNew);
4125 pTarget->aBlob[iTEnd+szNew] = 0x00;
4126 savedDelta = pTarget->delta;
4127 pTarget->delta = 0;
4128 rc = jsonMergePatch(pTarget, iTEnd+szNew,pPatch,iPValue);
4129 if( rc ) return rc;
4130 pTarget->delta += savedDelta;
4134 if( pTarget->delta ) jsonAfterEditSizeAdjust(pTarget, iTarget);
4135 return pTarget->oom ? JSON_MERGE_OOM : JSON_MERGE_OK;
4140 ** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
4141 ** object that is the result of running the RFC 7396 MergePatch() algorithm
4142 ** on the two arguments.
4144 static void jsonPatchFunc(
4145 sqlite3_context *ctx,
4146 int argc,
4147 sqlite3_value **argv
4149 JsonParse *pTarget; /* The TARGET */
4150 JsonParse *pPatch; /* The PATCH */
4151 int rc; /* Result code */
4153 UNUSED_PARAMETER(argc);
4154 assert( argc==2 );
4155 pTarget = jsonParseFuncArg(ctx, argv[0], JSON_EDITABLE);
4156 if( pTarget==0 ) return;
4157 pPatch = jsonParseFuncArg(ctx, argv[1], 0);
4158 if( pPatch ){
4159 rc = jsonMergePatch(pTarget, 0, pPatch, 0);
4160 if( rc==JSON_MERGE_OK ){
4161 jsonReturnParse(ctx, pTarget);
4162 }else if( rc==JSON_MERGE_OOM ){
4163 sqlite3_result_error_nomem(ctx);
4164 }else{
4165 sqlite3_result_error(ctx, "malformed JSON", -1);
4167 jsonParseFree(pPatch);
4169 jsonParseFree(pTarget);
4174 ** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
4175 ** object that contains all name/value given in arguments. Or if any name
4176 ** is not a string or if any value is a BLOB, throw an error.
4178 static void jsonObjectFunc(
4179 sqlite3_context *ctx,
4180 int argc,
4181 sqlite3_value **argv
4183 int i;
4184 JsonString jx;
4185 const char *z;
4186 u32 n;
4188 if( argc&1 ){
4189 sqlite3_result_error(ctx, "json_object() requires an even number "
4190 "of arguments", -1);
4191 return;
4193 jsonStringInit(&jx, ctx);
4194 jsonAppendChar(&jx, '{');
4195 for(i=0; i<argc; i+=2){
4196 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
4197 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
4198 jsonStringReset(&jx);
4199 return;
4201 jsonAppendSeparator(&jx);
4202 z = (const char*)sqlite3_value_text(argv[i]);
4203 n = sqlite3_value_bytes(argv[i]);
4204 jsonAppendString(&jx, z, n);
4205 jsonAppendChar(&jx, ':');
4206 jsonAppendSqlValue(&jx, argv[i+1]);
4208 jsonAppendChar(&jx, '}');
4209 jsonReturnString(&jx, 0, 0);
4210 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
4215 ** json_remove(JSON, PATH, ...)
4217 ** Remove the named elements from JSON and return the result. malformed
4218 ** JSON or PATH arguments result in an error.
4220 static void jsonRemoveFunc(
4221 sqlite3_context *ctx,
4222 int argc,
4223 sqlite3_value **argv
4225 JsonParse *p; /* The parse */
4226 const char *zPath = 0; /* Path of element to be removed */
4227 int i; /* Loop counter */
4228 u32 rc; /* Subroutine return code */
4230 if( argc<1 ) return;
4231 p = jsonParseFuncArg(ctx, argv[0], argc>1 ? JSON_EDITABLE : 0);
4232 if( p==0 ) return;
4233 for(i=1; i<argc; i++){
4234 zPath = (const char*)sqlite3_value_text(argv[i]);
4235 if( zPath==0 ){
4236 goto json_remove_done;
4238 if( zPath[0]!='$' ){
4239 goto json_remove_patherror;
4241 if( zPath[1]==0 ){
4242 /* json_remove(j,'$') returns NULL */
4243 goto json_remove_done;
4245 p->eEdit = JEDIT_DEL;
4246 p->delta = 0;
4247 rc = jsonLookupStep(p, 0, zPath+1, 0);
4248 if( JSON_LOOKUP_ISERROR(rc) ){
4249 if( rc==JSON_LOOKUP_NOTFOUND ){
4250 continue; /* No-op */
4251 }else if( rc==JSON_LOOKUP_PATHERROR ){
4252 jsonBadPathError(ctx, zPath);
4253 }else{
4254 sqlite3_result_error(ctx, "malformed JSON", -1);
4256 goto json_remove_done;
4259 jsonReturnParse(ctx, p);
4260 jsonParseFree(p);
4261 return;
4263 json_remove_patherror:
4264 jsonBadPathError(ctx, zPath);
4266 json_remove_done:
4267 jsonParseFree(p);
4268 return;
4272 ** json_replace(JSON, PATH, VALUE, ...)
4274 ** Replace the value at PATH with VALUE. If PATH does not already exist,
4275 ** this routine is a no-op. If JSON or PATH is malformed, throw an error.
4277 static void jsonReplaceFunc(
4278 sqlite3_context *ctx,
4279 int argc,
4280 sqlite3_value **argv
4282 if( argc<1 ) return;
4283 if( (argc&1)==0 ) {
4284 jsonWrongNumArgs(ctx, "replace");
4285 return;
4287 jsonInsertIntoBlob(ctx, argc, argv, JEDIT_REPL);
4292 ** json_set(JSON, PATH, VALUE, ...)
4294 ** Set the value at PATH to VALUE. Create the PATH if it does not already
4295 ** exist. Overwrite existing values that do exist.
4296 ** If JSON or PATH is malformed, throw an error.
4298 ** json_insert(JSON, PATH, VALUE, ...)
4300 ** Create PATH and initialize it to VALUE. If PATH already exists, this
4301 ** routine is a no-op. If JSON or PATH is malformed, throw an error.
4303 static void jsonSetFunc(
4304 sqlite3_context *ctx,
4305 int argc,
4306 sqlite3_value **argv
4309 int flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
4310 int bIsSet = (flags&JSON_ISSET)!=0;
4312 if( argc<1 ) return;
4313 if( (argc&1)==0 ) {
4314 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
4315 return;
4317 jsonInsertIntoBlob(ctx, argc, argv, bIsSet ? JEDIT_SET : JEDIT_INS);
4321 ** json_type(JSON)
4322 ** json_type(JSON, PATH)
4324 ** Return the top-level "type" of a JSON string. json_type() raises an
4325 ** error if either the JSON or PATH inputs are not well-formed.
4327 static void jsonTypeFunc(
4328 sqlite3_context *ctx,
4329 int argc,
4330 sqlite3_value **argv
4332 JsonParse *p; /* The parse */
4333 const char *zPath = 0;
4334 u32 i;
4336 p = jsonParseFuncArg(ctx, argv[0], 0);
4337 if( p==0 ) return;
4338 if( argc==2 ){
4339 zPath = (const char*)sqlite3_value_text(argv[1]);
4340 if( zPath==0 ) goto json_type_done;
4341 if( zPath[0]!='$' ){
4342 jsonBadPathError(ctx, zPath);
4343 goto json_type_done;
4345 i = jsonLookupStep(p, 0, zPath+1, 0);
4346 if( JSON_LOOKUP_ISERROR(i) ){
4347 if( i==JSON_LOOKUP_NOTFOUND ){
4348 /* no-op */
4349 }else if( i==JSON_LOOKUP_PATHERROR ){
4350 jsonBadPathError(ctx, zPath);
4351 }else{
4352 sqlite3_result_error(ctx, "malformed JSON", -1);
4354 goto json_type_done;
4356 }else{
4357 i = 0;
4359 sqlite3_result_text(ctx, jsonbType[p->aBlob[i]&0x0f], -1, SQLITE_STATIC);
4360 json_type_done:
4361 jsonParseFree(p);
4365 ** json_pretty(JSON)
4366 ** json_pretty(JSON, INDENT)
4368 ** Return text that is a pretty-printed rendering of the input JSON.
4369 ** If the argument is not valid JSON, return NULL.
4371 ** The INDENT argument is text that is used for indentation. If omitted,
4372 ** it defaults to four spaces (the same as PostgreSQL).
4374 static void jsonPrettyFunc(
4375 sqlite3_context *ctx,
4376 int argc,
4377 sqlite3_value **argv
4379 JsonString s; /* The output string */
4380 JsonPretty x; /* Pretty printing context */
4382 memset(&x, 0, sizeof(x));
4383 x.pParse = jsonParseFuncArg(ctx, argv[0], 0);
4384 if( x.pParse==0 ) return;
4385 x.pOut = &s;
4386 jsonStringInit(&s, ctx);
4387 if( argc==1 || (x.zIndent = (const char*)sqlite3_value_text(argv[1]))==0 ){
4388 x.zIndent = " ";
4389 x.szIndent = 4;
4390 }else{
4391 x.szIndent = (u32)strlen(x.zIndent);
4393 jsonTranslateBlobToPrettyText(&x, 0);
4394 jsonReturnString(&s, 0, 0);
4395 jsonParseFree(x.pParse);
4399 ** json_valid(JSON)
4400 ** json_valid(JSON, FLAGS)
4402 ** Check the JSON argument to see if it is well-formed. The FLAGS argument
4403 ** encodes the various constraints on what is meant by "well-formed":
4405 ** 0x01 Canonical RFC-8259 JSON text
4406 ** 0x02 JSON text with optional JSON-5 extensions
4407 ** 0x04 Superficially appears to be JSONB
4408 ** 0x08 Strictly well-formed JSONB
4410 ** If the FLAGS argument is omitted, it defaults to 1. Useful values for
4411 ** FLAGS include:
4413 ** 1 Strict canonical JSON text
4414 ** 2 JSON text perhaps with JSON-5 extensions
4415 ** 4 Superficially appears to be JSONB
4416 ** 5 Canonical JSON text or superficial JSONB
4417 ** 6 JSON-5 text or superficial JSONB
4418 ** 8 Strict JSONB
4419 ** 9 Canonical JSON text or strict JSONB
4420 ** 10 JSON-5 text or strict JSONB
4422 ** Other flag combinations are redundant. For example, every canonical
4423 ** JSON text is also well-formed JSON-5 text, so FLAG values 2 and 3
4424 ** are the same. Similarly, any input that passes a strict JSONB validation
4425 ** will also pass the superficial validation so 12 through 15 are the same
4426 ** as 8 through 11 respectively.
4428 ** This routine runs in linear time to validate text and when doing strict
4429 ** JSONB validation. Superficial JSONB validation is constant time,
4430 ** assuming the BLOB is already in memory. The performance advantage
4431 ** of superficial JSONB validation is why that option is provided.
4432 ** Application developers can choose to do fast superficial validation or
4433 ** slower strict validation, according to their specific needs.
4435 ** Only the lower four bits of the FLAGS argument are currently used.
4436 ** Higher bits are reserved for future expansion. To facilitate
4437 ** compatibility, the current implementation raises an error if any bit
4438 ** in FLAGS is set other than the lower four bits.
4440 ** The original circa 2015 implementation of the JSON routines in
4441 ** SQLite only supported canonical RFC-8259 JSON text and the json_valid()
4442 ** function only accepted one argument. That is why the default value
4443 ** for the FLAGS argument is 1, since FLAGS=1 causes this routine to only
4444 ** recognize canonical RFC-8259 JSON text as valid. The extra FLAGS
4445 ** argument was added when the JSON routines were extended to support
4446 ** JSON5-like extensions and binary JSONB stored in BLOBs.
4448 ** Return Values:
4450 ** * Raise an error if FLAGS is outside the range of 1 to 15.
4451 ** * Return NULL if the input is NULL
4452 ** * Return 1 if the input is well-formed.
4453 ** * Return 0 if the input is not well-formed.
4455 static void jsonValidFunc(
4456 sqlite3_context *ctx,
4457 int argc,
4458 sqlite3_value **argv
4460 JsonParse *p; /* The parse */
4461 u8 flags = 1;
4462 u8 res = 0;
4463 if( argc==2 ){
4464 i64 f = sqlite3_value_int64(argv[1]);
4465 if( f<1 || f>15 ){
4466 sqlite3_result_error(ctx, "FLAGS parameter to json_valid() must be"
4467 " between 1 and 15", -1);
4468 return;
4470 flags = f & 0x0f;
4472 switch( sqlite3_value_type(argv[0]) ){
4473 case SQLITE_NULL: {
4474 #ifdef SQLITE_LEGACY_JSON_VALID
4475 /* Incorrect legacy behavior was to return FALSE for a NULL input */
4476 sqlite3_result_int(ctx, 0);
4477 #endif
4478 return;
4480 case SQLITE_BLOB: {
4481 if( jsonFuncArgMightBeBinary(argv[0]) ){
4482 if( flags & 0x04 ){
4483 /* Superficial checking only - accomplished by the
4484 ** jsonFuncArgMightBeBinary() call above. */
4485 res = 1;
4486 }else if( flags & 0x08 ){
4487 /* Strict checking. Check by translating BLOB->TEXT->BLOB. If
4488 ** no errors occur, call that a "strict check". */
4489 JsonParse px;
4490 u32 iErr;
4491 memset(&px, 0, sizeof(px));
4492 px.aBlob = (u8*)sqlite3_value_blob(argv[0]);
4493 px.nBlob = sqlite3_value_bytes(argv[0]);
4494 iErr = jsonbValidityCheck(&px, 0, px.nBlob, 1);
4495 res = iErr==0;
4497 break;
4499 /* Fall through into interpreting the input as text. See note
4500 ** above at tag-20240123-a. */
4501 /* no break */ deliberate_fall_through
4503 default: {
4504 JsonParse px;
4505 if( (flags & 0x3)==0 ) break;
4506 memset(&px, 0, sizeof(px));
4508 p = jsonParseFuncArg(ctx, argv[0], JSON_KEEPERROR);
4509 if( p ){
4510 if( p->oom ){
4511 sqlite3_result_error_nomem(ctx);
4512 }else if( p->nErr ){
4513 /* no-op */
4514 }else if( (flags & 0x02)!=0 || p->hasNonstd==0 ){
4515 res = 1;
4517 jsonParseFree(p);
4518 }else{
4519 sqlite3_result_error_nomem(ctx);
4521 break;
4524 sqlite3_result_int(ctx, res);
4528 ** json_error_position(JSON)
4530 ** If the argument is NULL, return NULL
4532 ** If the argument is BLOB, do a full validity check and return non-zero
4533 ** if the check fails. The return value is the approximate 1-based offset
4534 ** to the byte of the element that contains the first error.
4536 ** Otherwise interpret the argument is TEXT (even if it is numeric) and
4537 ** return the 1-based character position for where the parser first recognized
4538 ** that the input was not valid JSON, or return 0 if the input text looks
4539 ** ok. JSON-5 extensions are accepted.
4541 static void jsonErrorFunc(
4542 sqlite3_context *ctx,
4543 int argc,
4544 sqlite3_value **argv
4546 i64 iErrPos = 0; /* Error position to be returned */
4547 JsonParse s;
4549 assert( argc==1 );
4550 UNUSED_PARAMETER(argc);
4551 memset(&s, 0, sizeof(s));
4552 s.db = sqlite3_context_db_handle(ctx);
4553 if( jsonFuncArgMightBeBinary(argv[0]) ){
4554 s.aBlob = (u8*)sqlite3_value_blob(argv[0]);
4555 s.nBlob = sqlite3_value_bytes(argv[0]);
4556 iErrPos = (i64)jsonbValidityCheck(&s, 0, s.nBlob, 1);
4557 }else{
4558 s.zJson = (char*)sqlite3_value_text(argv[0]);
4559 if( s.zJson==0 ) return; /* NULL input or OOM */
4560 s.nJson = sqlite3_value_bytes(argv[0]);
4561 if( jsonConvertTextToBlob(&s,0) ){
4562 if( s.oom ){
4563 iErrPos = -1;
4564 }else{
4565 /* Convert byte-offset s.iErr into a character offset */
4566 u32 k;
4567 assert( s.zJson!=0 ); /* Because s.oom is false */
4568 for(k=0; k<s.iErr && ALWAYS(s.zJson[k]); k++){
4569 if( (s.zJson[k] & 0xc0)!=0x80 ) iErrPos++;
4571 iErrPos++;
4575 jsonParseReset(&s);
4576 if( iErrPos<0 ){
4577 sqlite3_result_error_nomem(ctx);
4578 }else{
4579 sqlite3_result_int64(ctx, iErrPos);
4583 /****************************************************************************
4584 ** Aggregate SQL function implementations
4585 ****************************************************************************/
4587 ** json_group_array(VALUE)
4589 ** Return a JSON array composed of all values in the aggregate.
4591 static void jsonArrayStep(
4592 sqlite3_context *ctx,
4593 int argc,
4594 sqlite3_value **argv
4596 JsonString *pStr;
4597 UNUSED_PARAMETER(argc);
4598 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
4599 if( pStr ){
4600 if( pStr->zBuf==0 ){
4601 jsonStringInit(pStr, ctx);
4602 jsonAppendChar(pStr, '[');
4603 }else if( pStr->nUsed>1 ){
4604 jsonAppendChar(pStr, ',');
4606 pStr->pCtx = ctx;
4607 jsonAppendSqlValue(pStr, argv[0]);
4610 static void jsonArrayCompute(sqlite3_context *ctx, int isFinal){
4611 JsonString *pStr;
4612 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
4613 if( pStr ){
4614 int flags;
4615 pStr->pCtx = ctx;
4616 jsonAppendChar(pStr, ']');
4617 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
4618 if( pStr->eErr ){
4619 jsonReturnString(pStr, 0, 0);
4620 return;
4621 }else if( flags & JSON_BLOB ){
4622 jsonReturnStringAsBlob(pStr);
4623 if( isFinal ){
4624 if( !pStr->bStatic ) sqlite3RCStrUnref(pStr->zBuf);
4625 }else{
4626 jsonStringTrimOneChar(pStr);
4628 return;
4629 }else if( isFinal ){
4630 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
4631 pStr->bStatic ? SQLITE_TRANSIENT :
4632 sqlite3RCStrUnref);
4633 pStr->bStatic = 1;
4634 }else{
4635 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
4636 jsonStringTrimOneChar(pStr);
4638 }else{
4639 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
4641 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
4643 static void jsonArrayValue(sqlite3_context *ctx){
4644 jsonArrayCompute(ctx, 0);
4646 static void jsonArrayFinal(sqlite3_context *ctx){
4647 jsonArrayCompute(ctx, 1);
4650 #ifndef SQLITE_OMIT_WINDOWFUNC
4652 ** This method works for both json_group_array() and json_group_object().
4653 ** It works by removing the first element of the group by searching forward
4654 ** to the first comma (",") that is not within a string and deleting all
4655 ** text through that comma.
4657 static void jsonGroupInverse(
4658 sqlite3_context *ctx,
4659 int argc,
4660 sqlite3_value **argv
4662 unsigned int i;
4663 int inStr = 0;
4664 int nNest = 0;
4665 char *z;
4666 char c;
4667 JsonString *pStr;
4668 UNUSED_PARAMETER(argc);
4669 UNUSED_PARAMETER(argv);
4670 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
4671 #ifdef NEVER
4672 /* pStr is always non-NULL since jsonArrayStep() or jsonObjectStep() will
4673 ** always have been called to initialize it */
4674 if( NEVER(!pStr) ) return;
4675 #endif
4676 z = pStr->zBuf;
4677 for(i=1; i<pStr->nUsed && ((c = z[i])!=',' || inStr || nNest); i++){
4678 if( c=='"' ){
4679 inStr = !inStr;
4680 }else if( c=='\\' ){
4681 i++;
4682 }else if( !inStr ){
4683 if( c=='{' || c=='[' ) nNest++;
4684 if( c=='}' || c==']' ) nNest--;
4687 if( i<pStr->nUsed ){
4688 pStr->nUsed -= i;
4689 memmove(&z[1], &z[i+1], (size_t)pStr->nUsed-1);
4690 z[pStr->nUsed] = 0;
4691 }else{
4692 pStr->nUsed = 1;
4695 #else
4696 # define jsonGroupInverse 0
4697 #endif
4701 ** json_group_obj(NAME,VALUE)
4703 ** Return a JSON object composed of all names and values in the aggregate.
4705 static void jsonObjectStep(
4706 sqlite3_context *ctx,
4707 int argc,
4708 sqlite3_value **argv
4710 JsonString *pStr;
4711 const char *z;
4712 u32 n;
4713 UNUSED_PARAMETER(argc);
4714 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
4715 if( pStr ){
4716 if( pStr->zBuf==0 ){
4717 jsonStringInit(pStr, ctx);
4718 jsonAppendChar(pStr, '{');
4719 }else if( pStr->nUsed>1 ){
4720 jsonAppendChar(pStr, ',');
4722 pStr->pCtx = ctx;
4723 z = (const char*)sqlite3_value_text(argv[0]);
4724 n = sqlite3Strlen30(z);
4725 jsonAppendString(pStr, z, n);
4726 jsonAppendChar(pStr, ':');
4727 jsonAppendSqlValue(pStr, argv[1]);
4730 static void jsonObjectCompute(sqlite3_context *ctx, int isFinal){
4731 JsonString *pStr;
4732 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
4733 if( pStr ){
4734 int flags;
4735 jsonAppendChar(pStr, '}');
4736 pStr->pCtx = ctx;
4737 flags = SQLITE_PTR_TO_INT(sqlite3_user_data(ctx));
4738 if( pStr->eErr ){
4739 jsonReturnString(pStr, 0, 0);
4740 return;
4741 }else if( flags & JSON_BLOB ){
4742 jsonReturnStringAsBlob(pStr);
4743 if( isFinal ){
4744 if( !pStr->bStatic ) sqlite3RCStrUnref(pStr->zBuf);
4745 }else{
4746 jsonStringTrimOneChar(pStr);
4748 return;
4749 }else if( isFinal ){
4750 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed,
4751 pStr->bStatic ? SQLITE_TRANSIENT :
4752 sqlite3RCStrUnref);
4753 pStr->bStatic = 1;
4754 }else{
4755 sqlite3_result_text(ctx, pStr->zBuf, (int)pStr->nUsed, SQLITE_TRANSIENT);
4756 jsonStringTrimOneChar(pStr);
4758 }else{
4759 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
4761 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
4763 static void jsonObjectValue(sqlite3_context *ctx){
4764 jsonObjectCompute(ctx, 0);
4766 static void jsonObjectFinal(sqlite3_context *ctx){
4767 jsonObjectCompute(ctx, 1);
4772 #ifndef SQLITE_OMIT_VIRTUALTABLE
4773 /****************************************************************************
4774 ** The json_each virtual table
4775 ****************************************************************************/
4776 typedef struct JsonParent JsonParent;
4777 struct JsonParent {
4778 u32 iHead; /* Start of object or array */
4779 u32 iValue; /* Start of the value */
4780 u32 iEnd; /* First byte past the end */
4781 u32 nPath; /* Length of path */
4782 i64 iKey; /* Key for JSONB_ARRAY */
4785 typedef struct JsonEachCursor JsonEachCursor;
4786 struct JsonEachCursor {
4787 sqlite3_vtab_cursor base; /* Base class - must be first */
4788 u32 iRowid; /* The rowid */
4789 u32 i; /* Index in sParse.aBlob[] of current row */
4790 u32 iEnd; /* EOF when i equals or exceeds this value */
4791 u32 nRoot; /* Size of the root path in bytes */
4792 u8 eType; /* Type of the container for element i */
4793 u8 bRecursive; /* True for json_tree(). False for json_each() */
4794 u32 nParent; /* Current nesting depth */
4795 u32 nParentAlloc; /* Space allocated for aParent[] */
4796 JsonParent *aParent; /* Parent elements of i */
4797 sqlite3 *db; /* Database connection */
4798 JsonString path; /* Current path */
4799 JsonParse sParse; /* Parse of the input JSON */
4801 typedef struct JsonEachConnection JsonEachConnection;
4802 struct JsonEachConnection {
4803 sqlite3_vtab base; /* Base class - must be first */
4804 sqlite3 *db; /* Database connection */
4808 /* Constructor for the json_each virtual table */
4809 static int jsonEachConnect(
4810 sqlite3 *db,
4811 void *pAux,
4812 int argc, const char *const*argv,
4813 sqlite3_vtab **ppVtab,
4814 char **pzErr
4816 JsonEachConnection *pNew;
4817 int rc;
4819 /* Column numbers */
4820 #define JEACH_KEY 0
4821 #define JEACH_VALUE 1
4822 #define JEACH_TYPE 2
4823 #define JEACH_ATOM 3
4824 #define JEACH_ID 4
4825 #define JEACH_PARENT 5
4826 #define JEACH_FULLKEY 6
4827 #define JEACH_PATH 7
4828 /* The xBestIndex method assumes that the JSON and ROOT columns are
4829 ** the last two columns in the table. Should this ever changes, be
4830 ** sure to update the xBestIndex method. */
4831 #define JEACH_JSON 8
4832 #define JEACH_ROOT 9
4834 UNUSED_PARAMETER(pzErr);
4835 UNUSED_PARAMETER(argv);
4836 UNUSED_PARAMETER(argc);
4837 UNUSED_PARAMETER(pAux);
4838 rc = sqlite3_declare_vtab(db,
4839 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
4840 "json HIDDEN,root HIDDEN)");
4841 if( rc==SQLITE_OK ){
4842 pNew = (JsonEachConnection*)sqlite3DbMallocZero(db, sizeof(*pNew));
4843 *ppVtab = (sqlite3_vtab*)pNew;
4844 if( pNew==0 ) return SQLITE_NOMEM;
4845 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
4846 pNew->db = db;
4848 return rc;
4851 /* destructor for json_each virtual table */
4852 static int jsonEachDisconnect(sqlite3_vtab *pVtab){
4853 JsonEachConnection *p = (JsonEachConnection*)pVtab;
4854 sqlite3DbFree(p->db, pVtab);
4855 return SQLITE_OK;
4858 /* constructor for a JsonEachCursor object for json_each(). */
4859 static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
4860 JsonEachConnection *pVtab = (JsonEachConnection*)p;
4861 JsonEachCursor *pCur;
4863 UNUSED_PARAMETER(p);
4864 pCur = sqlite3DbMallocZero(pVtab->db, sizeof(*pCur));
4865 if( pCur==0 ) return SQLITE_NOMEM;
4866 pCur->db = pVtab->db;
4867 jsonStringZero(&pCur->path);
4868 *ppCursor = &pCur->base;
4869 return SQLITE_OK;
4872 /* constructor for a JsonEachCursor object for json_tree(). */
4873 static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
4874 int rc = jsonEachOpenEach(p, ppCursor);
4875 if( rc==SQLITE_OK ){
4876 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
4877 pCur->bRecursive = 1;
4879 return rc;
4882 /* Reset a JsonEachCursor back to its original state. Free any memory
4883 ** held. */
4884 static void jsonEachCursorReset(JsonEachCursor *p){
4885 jsonParseReset(&p->sParse);
4886 jsonStringReset(&p->path);
4887 sqlite3DbFree(p->db, p->aParent);
4888 p->iRowid = 0;
4889 p->i = 0;
4890 p->aParent = 0;
4891 p->nParent = 0;
4892 p->nParentAlloc = 0;
4893 p->iEnd = 0;
4894 p->eType = 0;
4897 /* Destructor for a jsonEachCursor object */
4898 static int jsonEachClose(sqlite3_vtab_cursor *cur){
4899 JsonEachCursor *p = (JsonEachCursor*)cur;
4900 jsonEachCursorReset(p);
4902 sqlite3DbFree(p->db, cur);
4903 return SQLITE_OK;
4906 /* Return TRUE if the jsonEachCursor object has been advanced off the end
4907 ** of the JSON object */
4908 static int jsonEachEof(sqlite3_vtab_cursor *cur){
4909 JsonEachCursor *p = (JsonEachCursor*)cur;
4910 return p->i >= p->iEnd;
4914 ** If the cursor is currently pointing at the label of a object entry,
4915 ** then return the index of the value. For all other cases, return the
4916 ** current pointer position, which is the value.
4918 static int jsonSkipLabel(JsonEachCursor *p){
4919 if( p->eType==JSONB_OBJECT ){
4920 u32 sz = 0;
4921 u32 n = jsonbPayloadSize(&p->sParse, p->i, &sz);
4922 return p->i + n + sz;
4923 }else{
4924 return p->i;
4929 ** Append the path name for the current element.
4931 static void jsonAppendPathName(JsonEachCursor *p){
4932 assert( p->nParent>0 );
4933 assert( p->eType==JSONB_ARRAY || p->eType==JSONB_OBJECT );
4934 if( p->eType==JSONB_ARRAY ){
4935 jsonPrintf(30, &p->path, "[%lld]", p->aParent[p->nParent-1].iKey);
4936 }else{
4937 u32 n, sz = 0, k, i;
4938 const char *z;
4939 int needQuote = 0;
4940 n = jsonbPayloadSize(&p->sParse, p->i, &sz);
4941 k = p->i + n;
4942 z = (const char*)&p->sParse.aBlob[k];
4943 if( sz==0 || !sqlite3Isalpha(z[0]) ){
4944 needQuote = 1;
4945 }else{
4946 for(i=0; i<sz; i++){
4947 if( !sqlite3Isalnum(z[i]) ){
4948 needQuote = 1;
4949 break;
4953 if( needQuote ){
4954 jsonPrintf(sz+4,&p->path,".\"%.*s\"", sz, z);
4955 }else{
4956 jsonPrintf(sz+2,&p->path,".%.*s", sz, z);
4961 /* Advance the cursor to the next element for json_tree() */
4962 static int jsonEachNext(sqlite3_vtab_cursor *cur){
4963 JsonEachCursor *p = (JsonEachCursor*)cur;
4964 int rc = SQLITE_OK;
4965 if( p->bRecursive ){
4966 u8 x;
4967 u8 levelChange = 0;
4968 u32 n, sz = 0;
4969 u32 i = jsonSkipLabel(p);
4970 x = p->sParse.aBlob[i] & 0x0f;
4971 n = jsonbPayloadSize(&p->sParse, i, &sz);
4972 if( x==JSONB_OBJECT || x==JSONB_ARRAY ){
4973 JsonParent *pParent;
4974 if( p->nParent>=p->nParentAlloc ){
4975 JsonParent *pNew;
4976 u64 nNew;
4977 nNew = p->nParentAlloc*2 + 3;
4978 pNew = sqlite3DbRealloc(p->db, p->aParent, sizeof(JsonParent)*nNew);
4979 if( pNew==0 ) return SQLITE_NOMEM;
4980 p->nParentAlloc = (u32)nNew;
4981 p->aParent = pNew;
4983 levelChange = 1;
4984 pParent = &p->aParent[p->nParent];
4985 pParent->iHead = p->i;
4986 pParent->iValue = i;
4987 pParent->iEnd = i + n + sz;
4988 pParent->iKey = -1;
4989 pParent->nPath = (u32)p->path.nUsed;
4990 if( p->eType && p->nParent ){
4991 jsonAppendPathName(p);
4992 if( p->path.eErr ) rc = SQLITE_NOMEM;
4994 p->nParent++;
4995 p->i = i + n;
4996 }else{
4997 p->i = i + n + sz;
4999 while( p->nParent>0 && p->i >= p->aParent[p->nParent-1].iEnd ){
5000 p->nParent--;
5001 p->path.nUsed = p->aParent[p->nParent].nPath;
5002 levelChange = 1;
5004 if( levelChange ){
5005 if( p->nParent>0 ){
5006 JsonParent *pParent = &p->aParent[p->nParent-1];
5007 u32 iVal = pParent->iValue;
5008 p->eType = p->sParse.aBlob[iVal] & 0x0f;
5009 }else{
5010 p->eType = 0;
5013 }else{
5014 u32 n, sz = 0;
5015 u32 i = jsonSkipLabel(p);
5016 n = jsonbPayloadSize(&p->sParse, i, &sz);
5017 p->i = i + n + sz;
5019 if( p->eType==JSONB_ARRAY && p->nParent ){
5020 p->aParent[p->nParent-1].iKey++;
5022 p->iRowid++;
5023 return rc;
5026 /* Length of the path for rowid==0 in bRecursive mode.
5028 static int jsonEachPathLength(JsonEachCursor *p){
5029 u32 n = p->path.nUsed;
5030 char *z = p->path.zBuf;
5031 if( p->iRowid==0 && p->bRecursive && n>=2 ){
5032 while( n>1 ){
5033 n--;
5034 if( z[n]=='[' || z[n]=='.' ){
5035 u32 x, sz = 0;
5036 char cSaved = z[n];
5037 z[n] = 0;
5038 assert( p->sParse.eEdit==0 );
5039 x = jsonLookupStep(&p->sParse, 0, z+1, 0);
5040 z[n] = cSaved;
5041 if( JSON_LOOKUP_ISERROR(x) ) continue;
5042 if( x + jsonbPayloadSize(&p->sParse, x, &sz) == p->i ) break;
5046 return n;
5049 /* Return the value of a column */
5050 static int jsonEachColumn(
5051 sqlite3_vtab_cursor *cur, /* The cursor */
5052 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
5053 int iColumn /* Which column to return */
5055 JsonEachCursor *p = (JsonEachCursor*)cur;
5056 switch( iColumn ){
5057 case JEACH_KEY: {
5058 if( p->nParent==0 ){
5059 u32 n, j;
5060 if( p->nRoot==1 ) break;
5061 j = jsonEachPathLength(p);
5062 n = p->nRoot - j;
5063 if( n==0 ){
5064 break;
5065 }else if( p->path.zBuf[j]=='[' ){
5066 i64 x;
5067 sqlite3Atoi64(&p->path.zBuf[j+1], &x, n-1, SQLITE_UTF8);
5068 sqlite3_result_int64(ctx, x);
5069 }else if( p->path.zBuf[j+1]=='"' ){
5070 sqlite3_result_text(ctx, &p->path.zBuf[j+2], n-3, SQLITE_TRANSIENT);
5071 }else{
5072 sqlite3_result_text(ctx, &p->path.zBuf[j+1], n-1, SQLITE_TRANSIENT);
5074 break;
5076 if( p->eType==JSONB_OBJECT ){
5077 jsonReturnFromBlob(&p->sParse, p->i, ctx, 1);
5078 }else{
5079 assert( p->eType==JSONB_ARRAY );
5080 sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iKey);
5082 break;
5084 case JEACH_VALUE: {
5085 u32 i = jsonSkipLabel(p);
5086 jsonReturnFromBlob(&p->sParse, i, ctx, 1);
5087 if( (p->sParse.aBlob[i] & 0x0f)>=JSONB_ARRAY ){
5088 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
5090 break;
5092 case JEACH_TYPE: {
5093 u32 i = jsonSkipLabel(p);
5094 u8 eType = p->sParse.aBlob[i] & 0x0f;
5095 sqlite3_result_text(ctx, jsonbType[eType], -1, SQLITE_STATIC);
5096 break;
5098 case JEACH_ATOM: {
5099 u32 i = jsonSkipLabel(p);
5100 if( (p->sParse.aBlob[i] & 0x0f)<JSONB_ARRAY ){
5101 jsonReturnFromBlob(&p->sParse, i, ctx, 1);
5103 break;
5105 case JEACH_ID: {
5106 sqlite3_result_int64(ctx, (sqlite3_int64)p->i);
5107 break;
5109 case JEACH_PARENT: {
5110 if( p->nParent>0 && p->bRecursive ){
5111 sqlite3_result_int64(ctx, p->aParent[p->nParent-1].iHead);
5113 break;
5115 case JEACH_FULLKEY: {
5116 u64 nBase = p->path.nUsed;
5117 if( p->nParent ) jsonAppendPathName(p);
5118 sqlite3_result_text64(ctx, p->path.zBuf, p->path.nUsed,
5119 SQLITE_TRANSIENT, SQLITE_UTF8);
5120 p->path.nUsed = nBase;
5121 break;
5123 case JEACH_PATH: {
5124 u32 n = jsonEachPathLength(p);
5125 sqlite3_result_text64(ctx, p->path.zBuf, n,
5126 SQLITE_TRANSIENT, SQLITE_UTF8);
5127 break;
5129 default: {
5130 sqlite3_result_text(ctx, p->path.zBuf, p->nRoot, SQLITE_STATIC);
5131 break;
5133 case JEACH_JSON: {
5134 if( p->sParse.zJson==0 ){
5135 sqlite3_result_blob(ctx, p->sParse.aBlob, p->sParse.nBlob,
5136 SQLITE_TRANSIENT);
5137 }else{
5138 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_TRANSIENT);
5140 break;
5143 return SQLITE_OK;
5146 /* Return the current rowid value */
5147 static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5148 JsonEachCursor *p = (JsonEachCursor*)cur;
5149 *pRowid = p->iRowid;
5150 return SQLITE_OK;
5153 /* The query strategy is to look for an equality constraint on the json
5154 ** column. Without such a constraint, the table cannot operate. idxNum is
5155 ** 1 if the constraint is found, 3 if the constraint and zRoot are found,
5156 ** and 0 otherwise.
5158 static int jsonEachBestIndex(
5159 sqlite3_vtab *tab,
5160 sqlite3_index_info *pIdxInfo
5162 int i; /* Loop counter or computed array index */
5163 int aIdx[2]; /* Index of constraints for JSON and ROOT */
5164 int unusableMask = 0; /* Mask of unusable JSON and ROOT constraints */
5165 int idxMask = 0; /* Mask of usable == constraints JSON and ROOT */
5166 const struct sqlite3_index_constraint *pConstraint;
5168 /* This implementation assumes that JSON and ROOT are the last two
5169 ** columns in the table */
5170 assert( JEACH_ROOT == JEACH_JSON+1 );
5171 UNUSED_PARAMETER(tab);
5172 aIdx[0] = aIdx[1] = -1;
5173 pConstraint = pIdxInfo->aConstraint;
5174 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
5175 int iCol;
5176 int iMask;
5177 if( pConstraint->iColumn < JEACH_JSON ) continue;
5178 iCol = pConstraint->iColumn - JEACH_JSON;
5179 assert( iCol==0 || iCol==1 );
5180 testcase( iCol==0 );
5181 iMask = 1 << iCol;
5182 if( pConstraint->usable==0 ){
5183 unusableMask |= iMask;
5184 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5185 aIdx[iCol] = i;
5186 idxMask |= iMask;
5189 if( pIdxInfo->nOrderBy>0
5190 && pIdxInfo->aOrderBy[0].iColumn<0
5191 && pIdxInfo->aOrderBy[0].desc==0
5193 pIdxInfo->orderByConsumed = 1;
5196 if( (unusableMask & ~idxMask)!=0 ){
5197 /* If there are any unusable constraints on JSON or ROOT, then reject
5198 ** this entire plan */
5199 return SQLITE_CONSTRAINT;
5201 if( aIdx[0]<0 ){
5202 /* No JSON input. Leave estimatedCost at the huge value that it was
5203 ** initialized to to discourage the query planner from selecting this
5204 ** plan. */
5205 pIdxInfo->idxNum = 0;
5206 }else{
5207 pIdxInfo->estimatedCost = 1.0;
5208 i = aIdx[0];
5209 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
5210 pIdxInfo->aConstraintUsage[i].omit = 1;
5211 if( aIdx[1]<0 ){
5212 pIdxInfo->idxNum = 1; /* Only JSON supplied. Plan 1 */
5213 }else{
5214 i = aIdx[1];
5215 pIdxInfo->aConstraintUsage[i].argvIndex = 2;
5216 pIdxInfo->aConstraintUsage[i].omit = 1;
5217 pIdxInfo->idxNum = 3; /* Both JSON and ROOT are supplied. Plan 3 */
5220 return SQLITE_OK;
5223 /* Start a search on a new JSON string */
5224 static int jsonEachFilter(
5225 sqlite3_vtab_cursor *cur,
5226 int idxNum, const char *idxStr,
5227 int argc, sqlite3_value **argv
5229 JsonEachCursor *p = (JsonEachCursor*)cur;
5230 const char *zRoot = 0;
5231 u32 i, n, sz;
5233 UNUSED_PARAMETER(idxStr);
5234 UNUSED_PARAMETER(argc);
5235 jsonEachCursorReset(p);
5236 if( idxNum==0 ) return SQLITE_OK;
5237 memset(&p->sParse, 0, sizeof(p->sParse));
5238 p->sParse.nJPRef = 1;
5239 p->sParse.db = p->db;
5240 if( jsonFuncArgMightBeBinary(argv[0]) ){
5241 p->sParse.nBlob = sqlite3_value_bytes(argv[0]);
5242 p->sParse.aBlob = (u8*)sqlite3_value_blob(argv[0]);
5243 }else{
5244 p->sParse.zJson = (char*)sqlite3_value_text(argv[0]);
5245 p->sParse.nJson = sqlite3_value_bytes(argv[0]);
5246 if( p->sParse.zJson==0 ){
5247 p->i = p->iEnd = 0;
5248 return SQLITE_OK;
5250 if( jsonConvertTextToBlob(&p->sParse, 0) ){
5251 if( p->sParse.oom ){
5252 return SQLITE_NOMEM;
5254 goto json_each_malformed_input;
5257 if( idxNum==3 ){
5258 zRoot = (const char*)sqlite3_value_text(argv[1]);
5259 if( zRoot==0 ) return SQLITE_OK;
5260 if( zRoot[0]!='$' ){
5261 sqlite3_free(cur->pVtab->zErrMsg);
5262 cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot);
5263 jsonEachCursorReset(p);
5264 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
5266 p->nRoot = sqlite3Strlen30(zRoot);
5267 if( zRoot[1]==0 ){
5268 i = p->i = 0;
5269 p->eType = 0;
5270 }else{
5271 i = jsonLookupStep(&p->sParse, 0, zRoot+1, 0);
5272 if( JSON_LOOKUP_ISERROR(i) ){
5273 if( i==JSON_LOOKUP_NOTFOUND ){
5274 p->i = 0;
5275 p->eType = 0;
5276 p->iEnd = 0;
5277 return SQLITE_OK;
5279 sqlite3_free(cur->pVtab->zErrMsg);
5280 cur->pVtab->zErrMsg = jsonBadPathError(0, zRoot);
5281 jsonEachCursorReset(p);
5282 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
5284 if( p->sParse.iLabel ){
5285 p->i = p->sParse.iLabel;
5286 p->eType = JSONB_OBJECT;
5287 }else{
5288 p->i = i;
5289 p->eType = JSONB_ARRAY;
5292 jsonAppendRaw(&p->path, zRoot, p->nRoot);
5293 }else{
5294 i = p->i = 0;
5295 p->eType = 0;
5296 p->nRoot = 1;
5297 jsonAppendRaw(&p->path, "$", 1);
5299 p->nParent = 0;
5300 n = jsonbPayloadSize(&p->sParse, i, &sz);
5301 p->iEnd = i+n+sz;
5302 if( (p->sParse.aBlob[i] & 0x0f)>=JSONB_ARRAY && !p->bRecursive ){
5303 p->i = i + n;
5304 p->eType = p->sParse.aBlob[i] & 0x0f;
5305 p->aParent = sqlite3DbMallocZero(p->db, sizeof(JsonParent));
5306 if( p->aParent==0 ) return SQLITE_NOMEM;
5307 p->nParent = 1;
5308 p->nParentAlloc = 1;
5309 p->aParent[0].iKey = 0;
5310 p->aParent[0].iEnd = p->iEnd;
5311 p->aParent[0].iHead = p->i;
5312 p->aParent[0].iValue = i;
5314 return SQLITE_OK;
5316 json_each_malformed_input:
5317 sqlite3_free(cur->pVtab->zErrMsg);
5318 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
5319 jsonEachCursorReset(p);
5320 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
5323 /* The methods of the json_each virtual table */
5324 static sqlite3_module jsonEachModule = {
5325 0, /* iVersion */
5326 0, /* xCreate */
5327 jsonEachConnect, /* xConnect */
5328 jsonEachBestIndex, /* xBestIndex */
5329 jsonEachDisconnect, /* xDisconnect */
5330 0, /* xDestroy */
5331 jsonEachOpenEach, /* xOpen - open a cursor */
5332 jsonEachClose, /* xClose - close a cursor */
5333 jsonEachFilter, /* xFilter - configure scan constraints */
5334 jsonEachNext, /* xNext - advance a cursor */
5335 jsonEachEof, /* xEof - check for end of scan */
5336 jsonEachColumn, /* xColumn - read data */
5337 jsonEachRowid, /* xRowid - read data */
5338 0, /* xUpdate */
5339 0, /* xBegin */
5340 0, /* xSync */
5341 0, /* xCommit */
5342 0, /* xRollback */
5343 0, /* xFindMethod */
5344 0, /* xRename */
5345 0, /* xSavepoint */
5346 0, /* xRelease */
5347 0, /* xRollbackTo */
5348 0, /* xShadowName */
5349 0 /* xIntegrity */
5352 /* The methods of the json_tree virtual table. */
5353 static sqlite3_module jsonTreeModule = {
5354 0, /* iVersion */
5355 0, /* xCreate */
5356 jsonEachConnect, /* xConnect */
5357 jsonEachBestIndex, /* xBestIndex */
5358 jsonEachDisconnect, /* xDisconnect */
5359 0, /* xDestroy */
5360 jsonEachOpenTree, /* xOpen - open a cursor */
5361 jsonEachClose, /* xClose - close a cursor */
5362 jsonEachFilter, /* xFilter - configure scan constraints */
5363 jsonEachNext, /* xNext - advance a cursor */
5364 jsonEachEof, /* xEof - check for end of scan */
5365 jsonEachColumn, /* xColumn - read data */
5366 jsonEachRowid, /* xRowid - read data */
5367 0, /* xUpdate */
5368 0, /* xBegin */
5369 0, /* xSync */
5370 0, /* xCommit */
5371 0, /* xRollback */
5372 0, /* xFindMethod */
5373 0, /* xRename */
5374 0, /* xSavepoint */
5375 0, /* xRelease */
5376 0, /* xRollbackTo */
5377 0, /* xShadowName */
5378 0 /* xIntegrity */
5380 #endif /* SQLITE_OMIT_VIRTUALTABLE */
5381 #endif /* !defined(SQLITE_OMIT_JSON) */
5384 ** Register JSON functions.
5386 void sqlite3RegisterJsonFunctions(void){
5387 #ifndef SQLITE_OMIT_JSON
5388 static FuncDef aJsonFunc[] = {
5389 /* sqlite3_result_subtype() ----, ,--- sqlite3_value_subtype() */
5390 /* | | */
5391 /* Uses cache ------, | | ,---- Returns JSONB */
5392 /* | | | | */
5393 /* Number of arguments ---, | | | | ,--- Flags */
5394 /* | | | | | | */
5395 JFUNCTION(json, 1,1,1, 0,0,0, jsonRemoveFunc),
5396 JFUNCTION(jsonb, 1,1,0, 0,1,0, jsonRemoveFunc),
5397 JFUNCTION(json_array, -1,0,1, 1,0,0, jsonArrayFunc),
5398 JFUNCTION(jsonb_array, -1,0,1, 1,1,0, jsonArrayFunc),
5399 JFUNCTION(json_array_length, 1,1,0, 0,0,0, jsonArrayLengthFunc),
5400 JFUNCTION(json_array_length, 2,1,0, 0,0,0, jsonArrayLengthFunc),
5401 JFUNCTION(json_error_position,1,1,0, 0,0,0, jsonErrorFunc),
5402 JFUNCTION(json_extract, -1,1,1, 0,0,0, jsonExtractFunc),
5403 JFUNCTION(jsonb_extract, -1,1,0, 0,1,0, jsonExtractFunc),
5404 JFUNCTION(->, 2,1,1, 0,0,JSON_JSON, jsonExtractFunc),
5405 JFUNCTION(->>, 2,1,0, 0,0,JSON_SQL, jsonExtractFunc),
5406 JFUNCTION(json_insert, -1,1,1, 1,0,0, jsonSetFunc),
5407 JFUNCTION(jsonb_insert, -1,1,0, 1,1,0, jsonSetFunc),
5408 JFUNCTION(json_object, -1,0,1, 1,0,0, jsonObjectFunc),
5409 JFUNCTION(jsonb_object, -1,0,1, 1,1,0, jsonObjectFunc),
5410 JFUNCTION(json_patch, 2,1,1, 0,0,0, jsonPatchFunc),
5411 JFUNCTION(jsonb_patch, 2,1,0, 0,1,0, jsonPatchFunc),
5412 JFUNCTION(json_pretty, 1,1,0, 0,0,0, jsonPrettyFunc),
5413 JFUNCTION(json_pretty, 2,1,0, 0,0,0, jsonPrettyFunc),
5414 JFUNCTION(json_quote, 1,0,1, 1,0,0, jsonQuoteFunc),
5415 JFUNCTION(json_remove, -1,1,1, 0,0,0, jsonRemoveFunc),
5416 JFUNCTION(jsonb_remove, -1,1,0, 0,1,0, jsonRemoveFunc),
5417 JFUNCTION(json_replace, -1,1,1, 1,0,0, jsonReplaceFunc),
5418 JFUNCTION(jsonb_replace, -1,1,0, 1,1,0, jsonReplaceFunc),
5419 JFUNCTION(json_set, -1,1,1, 1,0,JSON_ISSET, jsonSetFunc),
5420 JFUNCTION(jsonb_set, -1,1,0, 1,1,JSON_ISSET, jsonSetFunc),
5421 JFUNCTION(json_type, 1,1,0, 0,0,0, jsonTypeFunc),
5422 JFUNCTION(json_type, 2,1,0, 0,0,0, jsonTypeFunc),
5423 JFUNCTION(json_valid, 1,1,0, 0,0,0, jsonValidFunc),
5424 JFUNCTION(json_valid, 2,1,0, 0,0,0, jsonValidFunc),
5425 #if SQLITE_DEBUG
5426 JFUNCTION(json_parse, 1,1,0, 0,0,0, jsonParseFunc),
5427 #endif
5428 WAGGREGATE(json_group_array, 1, 0, 0,
5429 jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,
5430 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|
5431 SQLITE_DETERMINISTIC),
5432 WAGGREGATE(jsonb_group_array, 1, JSON_BLOB, 0,
5433 jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,
5434 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC),
5435 WAGGREGATE(json_group_object, 2, 0, 0,
5436 jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,
5437 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC),
5438 WAGGREGATE(jsonb_group_object,2, JSON_BLOB, 0,
5439 jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,
5440 SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|
5441 SQLITE_DETERMINISTIC)
5443 sqlite3InsertBuiltinFuncs(aJsonFunc, ArraySize(aJsonFunc));
5444 #endif
5447 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
5449 ** Register the JSON table-valued functions
5451 int sqlite3JsonTableFunctions(sqlite3 *db){
5452 int rc = SQLITE_OK;
5453 static const struct {
5454 const char *zName;
5455 sqlite3_module *pModule;
5456 } aMod[] = {
5457 { "json_each", &jsonEachModule },
5458 { "json_tree", &jsonTreeModule },
5460 unsigned int i;
5461 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
5462 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
5464 return rc;
5466 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON) */