blob: 99d299c29c7842a56b9fef9cf166a81b4d29393b [file] [log] [blame]
drh5fa5c102015-08-12 16:49:40 +00001/*
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.
10**
11******************************************************************************
12**
13** This SQLite extension implements JSON functions. The interface is
14** modeled after MySQL JSON functions:
15**
16** https://dev.mysql.com/doc/refman/5.7/en/json.html
17**
drh5634cc02015-08-17 11:28:03 +000018** For the time being, all JSON is stored as pure text. (We might add
19** a JSONB type in the future which stores a binary encoding of JSON in
drhcb6c6c62015-08-19 22:47:17 +000020** a BLOB, but there is no support for JSONB in the current implementation.
21** This implementation parses JSON text at 250 MB/s, so it is hard to see
22** how JSONB might improve on that.)
drh5fa5c102015-08-12 16:49:40 +000023*/
drh50065652015-10-08 19:29:18 +000024#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
drhf2df7e72015-08-28 20:07:40 +000025#if !defined(_SQLITEINT_H_)
drh5fa5c102015-08-12 16:49:40 +000026#include "sqlite3ext.h"
drhf2df7e72015-08-28 20:07:40 +000027#endif
drh5fa5c102015-08-12 16:49:40 +000028SQLITE_EXTENSION_INIT1
29#include <assert.h>
30#include <string.h>
drh987eb1f2015-08-17 15:17:37 +000031#include <stdlib.h>
drh4af352d2015-08-21 20:02:48 +000032#include <stdarg.h>
drh5fa5c102015-08-12 16:49:40 +000033
drhdf3a9072016-02-11 15:37:18 +000034/* Mark a function parameter as unused, to suppress nuisance compiler
35** warnings. */
36#ifndef UNUSED_PARAM
37# define UNUSED_PARAM(X) (void)(X)
38#endif
drh6fd5c1e2015-08-21 20:37:12 +000039
drh8deb4b82015-10-09 18:21:43 +000040#ifndef LARGEST_INT64
41# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
42# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
43#endif
44
dan2e8f5512015-09-17 17:21:09 +000045/*
46** Versions of isspace(), isalnum() and isdigit() to which it is safe
47** to pass signed char values.
48*/
drh49472652015-10-16 15:35:39 +000049#ifdef sqlite3Isdigit
50 /* Use the SQLite core versions if this routine is part of the
51 ** SQLite amalgamation */
52# define safe_isdigit(x) sqlite3Isdigit(x)
53# define safe_isalnum(x) sqlite3Isalnum(x)
54#else
55 /* Use the standard library for separate compilation */
56#include <ctype.h> /* amalgamator: keep */
57# define safe_isdigit(x) isdigit((unsigned char)(x))
58# define safe_isalnum(x) isalnum((unsigned char)(x))
59#endif
dan2e8f5512015-09-17 17:21:09 +000060
drh95677942015-09-24 01:06:37 +000061/*
62** Growing our own isspace() routine this way is twice as fast as
63** the library isspace() function, resulting in a 7% overall performance
64** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
65*/
66static const char jsonIsSpace[] = {
drhb9e8f592015-10-16 15:16:06 +000067 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
drh95677942015-09-24 01:06:37 +000068 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83};
84#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
85
drh9a4718f2015-10-10 14:00:37 +000086#ifndef SQLITE_AMALGAMATION
87 /* Unsigned integer types. These are already defined in the sqliteInt.h,
88 ** but the definitions need to be repeated for separate compilation. */
89 typedef sqlite3_uint64 u64;
90 typedef unsigned int u32;
91 typedef unsigned char u8;
92#endif
drh5fa5c102015-08-12 16:49:40 +000093
drh52216ad2015-08-18 02:28:03 +000094/* Objects */
drh505ad2c2015-08-21 17:33:11 +000095typedef struct JsonString JsonString;
drh52216ad2015-08-18 02:28:03 +000096typedef struct JsonNode JsonNode;
97typedef struct JsonParse JsonParse;
98
drh5634cc02015-08-17 11:28:03 +000099/* An instance of this object represents a JSON string
100** under construction. Really, this is a generic string accumulator
101** that can be and is used to create strings other than JSON.
drh5fa5c102015-08-12 16:49:40 +0000102*/
drh505ad2c2015-08-21 17:33:11 +0000103struct JsonString {
drh5fa5c102015-08-12 16:49:40 +0000104 sqlite3_context *pCtx; /* Function context - put error messages here */
drh5634cc02015-08-17 11:28:03 +0000105 char *zBuf; /* Append JSON content here */
drh5fa5c102015-08-12 16:49:40 +0000106 u64 nAlloc; /* Bytes of storage available in zBuf[] */
107 u64 nUsed; /* Bytes of zBuf[] currently used */
108 u8 bStatic; /* True if zBuf is static space */
drhd0960592015-08-17 21:22:32 +0000109 u8 bErr; /* True if an error has been encountered */
drh5fa5c102015-08-12 16:49:40 +0000110 char zSpace[100]; /* Initial static space */
111};
112
drhe9c37f32015-08-15 21:25:36 +0000113/* JSON type values
drhbd0621b2015-08-13 13:54:59 +0000114*/
drhe9c37f32015-08-15 21:25:36 +0000115#define JSON_NULL 0
116#define JSON_TRUE 1
117#define JSON_FALSE 2
118#define JSON_INT 3
119#define JSON_REAL 4
120#define JSON_STRING 5
121#define JSON_ARRAY 6
122#define JSON_OBJECT 7
123
drhf5ddb9c2015-09-11 00:06:41 +0000124/* The "subtype" set for JSON values */
125#define JSON_SUBTYPE 74 /* Ascii for "J" */
126
drh987eb1f2015-08-17 15:17:37 +0000127/*
128** Names of the various JSON types:
129*/
130static const char * const jsonType[] = {
131 "null", "true", "false", "integer", "real", "text", "array", "object"
132};
133
drh301eecc2015-08-17 20:14:19 +0000134/* Bit values for the JsonNode.jnFlag field
135*/
136#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
137#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
138#define JNODE_REMOVE 0x04 /* Do not output */
drhd0960592015-08-17 21:22:32 +0000139#define JNODE_REPLACE 0x08 /* Replace with JsonNode.iVal */
drh52216ad2015-08-18 02:28:03 +0000140#define JNODE_APPEND 0x10 /* More ARRAY/OBJECT entries at u.iAppend */
drhf5ddb9c2015-09-11 00:06:41 +0000141#define JNODE_LABEL 0x20 /* Is a label of an object */
drh301eecc2015-08-17 20:14:19 +0000142
drh987eb1f2015-08-17 15:17:37 +0000143
drhe9c37f32015-08-15 21:25:36 +0000144/* A single node of parsed JSON
145*/
drhe9c37f32015-08-15 21:25:36 +0000146struct JsonNode {
drh5634cc02015-08-17 11:28:03 +0000147 u8 eType; /* One of the JSON_ type values */
drh301eecc2015-08-17 20:14:19 +0000148 u8 jnFlags; /* JNODE flags */
drhd0960592015-08-17 21:22:32 +0000149 u8 iVal; /* Replacement value when JNODE_REPLACE */
drhe9c37f32015-08-15 21:25:36 +0000150 u32 n; /* Bytes of content, or number of sub-nodes */
drh52216ad2015-08-18 02:28:03 +0000151 union {
drh0042a972015-08-18 12:59:58 +0000152 const char *zJContent; /* Content for INT, REAL, and STRING */
153 u32 iAppend; /* More terms for ARRAY and OBJECT */
drh505ad2c2015-08-21 17:33:11 +0000154 u32 iKey; /* Key for ARRAY objects in json_tree() */
drh52216ad2015-08-18 02:28:03 +0000155 } u;
drhe9c37f32015-08-15 21:25:36 +0000156};
157
158/* A completely parsed JSON string
159*/
drhe9c37f32015-08-15 21:25:36 +0000160struct JsonParse {
161 u32 nNode; /* Number of slots of aNode[] used */
162 u32 nAlloc; /* Number of slots of aNode[] allocated */
163 JsonNode *aNode; /* Array of nodes containing the parse */
164 const char *zJson; /* Original JSON string */
drh505ad2c2015-08-21 17:33:11 +0000165 u32 *aUp; /* Index of parent of each node */
drhe9c37f32015-08-15 21:25:36 +0000166 u8 oom; /* Set to true if out of memory */
drha7714022015-08-29 00:54:49 +0000167 u8 nErr; /* Number of errors seen */
drhe9c37f32015-08-15 21:25:36 +0000168};
169
drh505ad2c2015-08-21 17:33:11 +0000170/**************************************************************************
171** Utility routines for dealing with JsonString objects
172**************************************************************************/
drh301eecc2015-08-17 20:14:19 +0000173
drh505ad2c2015-08-21 17:33:11 +0000174/* Set the JsonString object to an empty string
drh5fa5c102015-08-12 16:49:40 +0000175*/
drh505ad2c2015-08-21 17:33:11 +0000176static void jsonZero(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000177 p->zBuf = p->zSpace;
178 p->nAlloc = sizeof(p->zSpace);
179 p->nUsed = 0;
180 p->bStatic = 1;
181}
182
drh505ad2c2015-08-21 17:33:11 +0000183/* Initialize the JsonString object
drh5fa5c102015-08-12 16:49:40 +0000184*/
drh505ad2c2015-08-21 17:33:11 +0000185static void jsonInit(JsonString *p, sqlite3_context *pCtx){
drh5fa5c102015-08-12 16:49:40 +0000186 p->pCtx = pCtx;
drhd0960592015-08-17 21:22:32 +0000187 p->bErr = 0;
drh5fa5c102015-08-12 16:49:40 +0000188 jsonZero(p);
189}
190
191
drh505ad2c2015-08-21 17:33:11 +0000192/* Free all allocated memory and reset the JsonString object back to its
drh5fa5c102015-08-12 16:49:40 +0000193** initial state.
194*/
drh505ad2c2015-08-21 17:33:11 +0000195static void jsonReset(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000196 if( !p->bStatic ) sqlite3_free(p->zBuf);
197 jsonZero(p);
198}
199
200
201/* Report an out-of-memory (OOM) condition
202*/
drh505ad2c2015-08-21 17:33:11 +0000203static void jsonOom(JsonString *p){
drh3d1d2a92015-09-22 01:15:49 +0000204 p->bErr = 1;
205 sqlite3_result_error_nomem(p->pCtx);
206 jsonReset(p);
drh5fa5c102015-08-12 16:49:40 +0000207}
208
209/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
210** Return zero on success. Return non-zero on an OOM error
211*/
drh505ad2c2015-08-21 17:33:11 +0000212static int jsonGrow(JsonString *p, u32 N){
drh301eecc2015-08-17 20:14:19 +0000213 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
drh5fa5c102015-08-12 16:49:40 +0000214 char *zNew;
215 if( p->bStatic ){
drhd0960592015-08-17 21:22:32 +0000216 if( p->bErr ) return 1;
drh5fa5c102015-08-12 16:49:40 +0000217 zNew = sqlite3_malloc64(nTotal);
218 if( zNew==0 ){
219 jsonOom(p);
220 return SQLITE_NOMEM;
221 }
drh6fd5c1e2015-08-21 20:37:12 +0000222 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
drh5fa5c102015-08-12 16:49:40 +0000223 p->zBuf = zNew;
224 p->bStatic = 0;
225 }else{
226 zNew = sqlite3_realloc64(p->zBuf, nTotal);
227 if( zNew==0 ){
228 jsonOom(p);
229 return SQLITE_NOMEM;
230 }
231 p->zBuf = zNew;
232 }
233 p->nAlloc = nTotal;
234 return SQLITE_OK;
235}
236
drh505ad2c2015-08-21 17:33:11 +0000237/* Append N bytes from zIn onto the end of the JsonString string.
drh5fa5c102015-08-12 16:49:40 +0000238*/
drh505ad2c2015-08-21 17:33:11 +0000239static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000240 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
241 memcpy(p->zBuf+p->nUsed, zIn, N);
242 p->nUsed += N;
243}
244
drh4af352d2015-08-21 20:02:48 +0000245/* Append formatted text (not to exceed N bytes) to the JsonString.
246*/
247static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
248 va_list ap;
249 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
250 va_start(ap, zFormat);
251 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
252 va_end(ap);
253 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
254}
255
drh5634cc02015-08-17 11:28:03 +0000256/* Append a single character
257*/
drh505ad2c2015-08-21 17:33:11 +0000258static void jsonAppendChar(JsonString *p, char c){
drh5634cc02015-08-17 11:28:03 +0000259 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
260 p->zBuf[p->nUsed++] = c;
261}
262
drh301eecc2015-08-17 20:14:19 +0000263/* Append a comma separator to the output buffer, if the previous
264** character is not '[' or '{'.
265*/
drh505ad2c2015-08-21 17:33:11 +0000266static void jsonAppendSeparator(JsonString *p){
drh301eecc2015-08-17 20:14:19 +0000267 char c;
268 if( p->nUsed==0 ) return;
269 c = p->zBuf[p->nUsed-1];
270 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
271}
272
drh505ad2c2015-08-21 17:33:11 +0000273/* Append the N-byte string in zIn to the end of the JsonString string
drh5fa5c102015-08-12 16:49:40 +0000274** under construction. Enclose the string in "..." and escape
275** any double-quotes or backslash characters contained within the
276** string.
277*/
drh505ad2c2015-08-21 17:33:11 +0000278static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000279 u32 i;
280 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
281 p->zBuf[p->nUsed++] = '"';
282 for(i=0; i<N; i++){
drh3b7f9a62016-02-04 10:28:57 +0000283 unsigned char c = ((unsigned const char*)zIn)[i];
drh5fa5c102015-08-12 16:49:40 +0000284 if( c=='"' || c=='\\' ){
drh3b7f9a62016-02-04 10:28:57 +0000285 json_simple_escape:
drh4977ccf2015-09-19 11:57:26 +0000286 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
drh5fa5c102015-08-12 16:49:40 +0000287 p->zBuf[p->nUsed++] = '\\';
drh3b7f9a62016-02-04 10:28:57 +0000288 }else if( c<=0x1f ){
289 static const char aSpecial[] = {
290 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
291 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
292 };
293 assert( sizeof(aSpecial)==32 );
294 assert( aSpecial['\b']=='b' );
295 assert( aSpecial['\f']=='f' );
296 assert( aSpecial['\n']=='n' );
297 assert( aSpecial['\r']=='r' );
298 assert( aSpecial['\t']=='t' );
299 if( aSpecial[c] ){
300 c = aSpecial[c];
301 goto json_simple_escape;
302 }
303 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
304 p->zBuf[p->nUsed++] = '\\';
305 p->zBuf[p->nUsed++] = 'u';
306 p->zBuf[p->nUsed++] = '0';
307 p->zBuf[p->nUsed++] = '0';
308 p->zBuf[p->nUsed++] = '0' + (c>>4);
309 c = "0123456789abcdef"[c&0xf];
drh5fa5c102015-08-12 16:49:40 +0000310 }
311 p->zBuf[p->nUsed++] = c;
312 }
313 p->zBuf[p->nUsed++] = '"';
drh4977ccf2015-09-19 11:57:26 +0000314 assert( p->nUsed<p->nAlloc );
drh5fa5c102015-08-12 16:49:40 +0000315}
316
drhd0960592015-08-17 21:22:32 +0000317/*
318** Append a function parameter value to the JSON string under
319** construction.
320*/
321static void jsonAppendValue(
drh505ad2c2015-08-21 17:33:11 +0000322 JsonString *p, /* Append to this JSON string */
drhf5ddb9c2015-09-11 00:06:41 +0000323 sqlite3_value *pValue /* Value to append */
drhd0960592015-08-17 21:22:32 +0000324){
325 switch( sqlite3_value_type(pValue) ){
326 case SQLITE_NULL: {
327 jsonAppendRaw(p, "null", 4);
328 break;
329 }
330 case SQLITE_INTEGER:
331 case SQLITE_FLOAT: {
332 const char *z = (const char*)sqlite3_value_text(pValue);
333 u32 n = (u32)sqlite3_value_bytes(pValue);
334 jsonAppendRaw(p, z, n);
335 break;
336 }
337 case SQLITE_TEXT: {
338 const char *z = (const char*)sqlite3_value_text(pValue);
339 u32 n = (u32)sqlite3_value_bytes(pValue);
drhf5ddb9c2015-09-11 00:06:41 +0000340 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
drhecb5fed2015-08-28 03:33:50 +0000341 jsonAppendRaw(p, z, n);
342 }else{
343 jsonAppendString(p, z, n);
344 }
drhd0960592015-08-17 21:22:32 +0000345 break;
346 }
347 default: {
348 if( p->bErr==0 ){
349 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
drh4a642b62016-02-05 01:55:27 +0000350 p->bErr = 2;
drhd0960592015-08-17 21:22:32 +0000351 jsonReset(p);
352 }
353 break;
354 }
355 }
356}
357
358
drhbd0621b2015-08-13 13:54:59 +0000359/* Make the JSON in p the result of the SQL function.
drh5fa5c102015-08-12 16:49:40 +0000360*/
drh505ad2c2015-08-21 17:33:11 +0000361static void jsonResult(JsonString *p){
drhd0960592015-08-17 21:22:32 +0000362 if( p->bErr==0 ){
drh5fa5c102015-08-12 16:49:40 +0000363 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
364 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
365 SQLITE_UTF8);
366 jsonZero(p);
367 }
368 assert( p->bStatic );
369}
370
drh505ad2c2015-08-21 17:33:11 +0000371/**************************************************************************
372** Utility routines for dealing with JsonNode and JsonParse objects
373**************************************************************************/
374
375/*
376** Return the number of consecutive JsonNode slots need to represent
377** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
378** OBJECT types, the number might be larger.
379**
380** Appended elements are not counted. The value returned is the number
381** by which the JsonNode counter should increment in order to go to the
382** next peer value.
383*/
384static u32 jsonNodeSize(JsonNode *pNode){
385 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
386}
387
388/*
389** Reclaim all memory allocated by a JsonParse object. But do not
390** delete the JsonParse object itself.
391*/
392static void jsonParseReset(JsonParse *pParse){
393 sqlite3_free(pParse->aNode);
394 pParse->aNode = 0;
395 pParse->nNode = 0;
396 pParse->nAlloc = 0;
397 sqlite3_free(pParse->aUp);
398 pParse->aUp = 0;
399}
400
drh5634cc02015-08-17 11:28:03 +0000401/*
402** Convert the JsonNode pNode into a pure JSON string and
403** append to pOut. Subsubstructure is also included. Return
404** the number of JsonNode objects that are encoded.
drhbd0621b2015-08-13 13:54:59 +0000405*/
drh52216ad2015-08-18 02:28:03 +0000406static void jsonRenderNode(
drhd0960592015-08-17 21:22:32 +0000407 JsonNode *pNode, /* The node to render */
drh505ad2c2015-08-21 17:33:11 +0000408 JsonString *pOut, /* Write JSON here */
drhd0960592015-08-17 21:22:32 +0000409 sqlite3_value **aReplace /* Replacement values */
410){
drh5634cc02015-08-17 11:28:03 +0000411 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000412 default: {
413 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000414 jsonAppendRaw(pOut, "null", 4);
415 break;
416 }
417 case JSON_TRUE: {
418 jsonAppendRaw(pOut, "true", 4);
419 break;
420 }
421 case JSON_FALSE: {
422 jsonAppendRaw(pOut, "false", 5);
423 break;
424 }
425 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000426 if( pNode->jnFlags & JNODE_RAW ){
drh52216ad2015-08-18 02:28:03 +0000427 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000428 break;
429 }
430 /* Fall through into the next case */
431 }
432 case JSON_REAL:
433 case JSON_INT: {
drh52216ad2015-08-18 02:28:03 +0000434 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000435 break;
436 }
437 case JSON_ARRAY: {
drh52216ad2015-08-18 02:28:03 +0000438 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000439 jsonAppendChar(pOut, '[');
drh52216ad2015-08-18 02:28:03 +0000440 for(;;){
441 while( j<=pNode->n ){
442 if( pNode[j].jnFlags & (JNODE_REMOVE|JNODE_REPLACE) ){
443 if( pNode[j].jnFlags & JNODE_REPLACE ){
444 jsonAppendSeparator(pOut);
drhf5ddb9c2015-09-11 00:06:41 +0000445 jsonAppendValue(pOut, aReplace[pNode[j].iVal]);
drh52216ad2015-08-18 02:28:03 +0000446 }
447 }else{
drhd0960592015-08-17 21:22:32 +0000448 jsonAppendSeparator(pOut);
drh52216ad2015-08-18 02:28:03 +0000449 jsonRenderNode(&pNode[j], pOut, aReplace);
drhd0960592015-08-17 21:22:32 +0000450 }
drh505ad2c2015-08-21 17:33:11 +0000451 j += jsonNodeSize(&pNode[j]);
drh301eecc2015-08-17 20:14:19 +0000452 }
drh52216ad2015-08-18 02:28:03 +0000453 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
454 pNode = &pNode[pNode->u.iAppend];
455 j = 1;
drh5634cc02015-08-17 11:28:03 +0000456 }
457 jsonAppendChar(pOut, ']');
458 break;
459 }
460 case JSON_OBJECT: {
drh52216ad2015-08-18 02:28:03 +0000461 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000462 jsonAppendChar(pOut, '{');
drh52216ad2015-08-18 02:28:03 +0000463 for(;;){
464 while( j<=pNode->n ){
465 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
466 jsonAppendSeparator(pOut);
467 jsonRenderNode(&pNode[j], pOut, aReplace);
468 jsonAppendChar(pOut, ':');
469 if( pNode[j+1].jnFlags & JNODE_REPLACE ){
drhf5ddb9c2015-09-11 00:06:41 +0000470 jsonAppendValue(pOut, aReplace[pNode[j+1].iVal]);
drh52216ad2015-08-18 02:28:03 +0000471 }else{
472 jsonRenderNode(&pNode[j+1], pOut, aReplace);
473 }
drhd0960592015-08-17 21:22:32 +0000474 }
drh505ad2c2015-08-21 17:33:11 +0000475 j += 1 + jsonNodeSize(&pNode[j+1]);
drh301eecc2015-08-17 20:14:19 +0000476 }
drh52216ad2015-08-18 02:28:03 +0000477 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
478 pNode = &pNode[pNode->u.iAppend];
479 j = 1;
drh5634cc02015-08-17 11:28:03 +0000480 }
481 jsonAppendChar(pOut, '}');
482 break;
483 }
drhbd0621b2015-08-13 13:54:59 +0000484 }
drh5634cc02015-08-17 11:28:03 +0000485}
486
487/*
drhf2df7e72015-08-28 20:07:40 +0000488** Return a JsonNode and all its descendents as a JSON string.
489*/
490static void jsonReturnJson(
491 JsonNode *pNode, /* Node to return */
492 sqlite3_context *pCtx, /* Return value for this function */
493 sqlite3_value **aReplace /* Array of replacement values */
494){
495 JsonString s;
496 jsonInit(&s, pCtx);
497 jsonRenderNode(pNode, &s, aReplace);
498 jsonResult(&s);
drhf5ddb9c2015-09-11 00:06:41 +0000499 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
drhf2df7e72015-08-28 20:07:40 +0000500}
501
502/*
drh5634cc02015-08-17 11:28:03 +0000503** Make the JsonNode the return value of the function.
504*/
drhd0960592015-08-17 21:22:32 +0000505static void jsonReturn(
506 JsonNode *pNode, /* Node to return */
507 sqlite3_context *pCtx, /* Return value for this function */
508 sqlite3_value **aReplace /* Array of replacement values */
509){
drh5634cc02015-08-17 11:28:03 +0000510 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000511 default: {
512 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000513 sqlite3_result_null(pCtx);
514 break;
515 }
516 case JSON_TRUE: {
517 sqlite3_result_int(pCtx, 1);
518 break;
519 }
520 case JSON_FALSE: {
521 sqlite3_result_int(pCtx, 0);
522 break;
523 }
drh987eb1f2015-08-17 15:17:37 +0000524 case JSON_INT: {
525 sqlite3_int64 i = 0;
drh52216ad2015-08-18 02:28:03 +0000526 const char *z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000527 if( z[0]=='-' ){ z++; }
drh8deb4b82015-10-09 18:21:43 +0000528 while( z[0]>='0' && z[0]<='9' ){
529 unsigned v = *(z++) - '0';
530 if( i>=LARGEST_INT64/10 ){
drha0882fa2015-10-09 20:40:44 +0000531 if( i>LARGEST_INT64/10 ) goto int_as_real;
drh8deb4b82015-10-09 18:21:43 +0000532 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
533 if( v==9 ) goto int_as_real;
534 if( v==8 ){
535 if( pNode->u.zJContent[0]=='-' ){
536 sqlite3_result_int64(pCtx, SMALLEST_INT64);
537 goto int_done;
538 }else{
539 goto int_as_real;
540 }
541 }
542 }
543 i = i*10 + v;
544 }
drh52216ad2015-08-18 02:28:03 +0000545 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
drh987eb1f2015-08-17 15:17:37 +0000546 sqlite3_result_int64(pCtx, i);
drh8deb4b82015-10-09 18:21:43 +0000547 int_done:
548 break;
549 int_as_real: /* fall through to real */;
550 }
551 case JSON_REAL: {
drh49472652015-10-16 15:35:39 +0000552 double r;
553#ifdef SQLITE_AMALGAMATION
554 const char *z = pNode->u.zJContent;
555 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
556#else
557 r = strtod(pNode->u.zJContent, 0);
558#endif
drh8deb4b82015-10-09 18:21:43 +0000559 sqlite3_result_double(pCtx, r);
drh987eb1f2015-08-17 15:17:37 +0000560 break;
561 }
drh5634cc02015-08-17 11:28:03 +0000562 case JSON_STRING: {
drha8f39a92015-09-21 22:53:16 +0000563#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
564 ** json_insert() and json_replace() and those routines do not
565 ** call jsonReturn() */
drh301eecc2015-08-17 20:14:19 +0000566 if( pNode->jnFlags & JNODE_RAW ){
drh52216ad2015-08-18 02:28:03 +0000567 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
568 SQLITE_TRANSIENT);
drha8f39a92015-09-21 22:53:16 +0000569 }else
570#endif
571 assert( (pNode->jnFlags & JNODE_RAW)==0 );
572 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
drh987eb1f2015-08-17 15:17:37 +0000573 /* JSON formatted without any backslash-escapes */
drh52216ad2015-08-18 02:28:03 +0000574 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
drh987eb1f2015-08-17 15:17:37 +0000575 SQLITE_TRANSIENT);
drh5634cc02015-08-17 11:28:03 +0000576 }else{
577 /* Translate JSON formatted string into raw text */
drh987eb1f2015-08-17 15:17:37 +0000578 u32 i;
579 u32 n = pNode->n;
drh52216ad2015-08-18 02:28:03 +0000580 const char *z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000581 char *zOut;
582 u32 j;
583 zOut = sqlite3_malloc( n+1 );
584 if( zOut==0 ){
585 sqlite3_result_error_nomem(pCtx);
586 break;
587 }
588 for(i=1, j=0; i<n-1; i++){
589 char c = z[i];
drh80d87402015-08-24 12:42:41 +0000590 if( c!='\\' ){
drh987eb1f2015-08-17 15:17:37 +0000591 zOut[j++] = c;
592 }else{
593 c = z[++i];
drh80d87402015-08-24 12:42:41 +0000594 if( c=='u' ){
drh987eb1f2015-08-17 15:17:37 +0000595 u32 v = 0, k;
drh80d87402015-08-24 12:42:41 +0000596 for(k=0; k<4 && i<n-2; i++, k++){
drh8784eca2015-08-23 02:42:30 +0000597 c = z[i+1];
drh987eb1f2015-08-17 15:17:37 +0000598 if( c>='0' && c<='9' ) v = v*16 + c - '0';
599 else if( c>='A' && c<='F' ) v = v*16 + c - 'A' + 10;
600 else if( c>='a' && c<='f' ) v = v*16 + c - 'a' + 10;
601 else break;
drh987eb1f2015-08-17 15:17:37 +0000602 }
drh80d87402015-08-24 12:42:41 +0000603 if( v==0 ) break;
drh987eb1f2015-08-17 15:17:37 +0000604 if( v<=0x7f ){
mistachkin16a93122015-09-11 18:05:01 +0000605 zOut[j++] = (char)v;
drh987eb1f2015-08-17 15:17:37 +0000606 }else if( v<=0x7ff ){
mistachkin16a93122015-09-11 18:05:01 +0000607 zOut[j++] = (char)(0xc0 | (v>>6));
drh987eb1f2015-08-17 15:17:37 +0000608 zOut[j++] = 0x80 | (v&0x3f);
drh80d87402015-08-24 12:42:41 +0000609 }else{
mistachkin16a93122015-09-11 18:05:01 +0000610 zOut[j++] = (char)(0xe0 | (v>>12));
drh987eb1f2015-08-17 15:17:37 +0000611 zOut[j++] = 0x80 | ((v>>6)&0x3f);
612 zOut[j++] = 0x80 | (v&0x3f);
drh987eb1f2015-08-17 15:17:37 +0000613 }
614 }else{
615 if( c=='b' ){
616 c = '\b';
617 }else if( c=='f' ){
618 c = '\f';
619 }else if( c=='n' ){
620 c = '\n';
621 }else if( c=='r' ){
622 c = '\r';
623 }else if( c=='t' ){
624 c = '\t';
625 }
626 zOut[j++] = c;
627 }
628 }
629 }
630 zOut[j] = 0;
631 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
drh5634cc02015-08-17 11:28:03 +0000632 }
633 break;
634 }
635 case JSON_ARRAY:
636 case JSON_OBJECT: {
drhf2df7e72015-08-28 20:07:40 +0000637 jsonReturnJson(pNode, pCtx, aReplace);
drh5634cc02015-08-17 11:28:03 +0000638 break;
639 }
640 }
drhbd0621b2015-08-13 13:54:59 +0000641}
642
drh95677942015-09-24 01:06:37 +0000643/* Forward reference */
644static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
645
646/*
647** A macro to hint to the compiler that a function should not be
648** inlined.
649*/
650#if defined(__GNUC__)
651# define JSON_NOINLINE __attribute__((noinline))
652#elif defined(_MSC_VER) && _MSC_VER>=1310
653# define JSON_NOINLINE __declspec(noinline)
654#else
655# define JSON_NOINLINE
656#endif
657
658
659static JSON_NOINLINE int jsonParseAddNodeExpand(
660 JsonParse *pParse, /* Append the node to this object */
661 u32 eType, /* Node type */
662 u32 n, /* Content size or sub-node count */
663 const char *zContent /* Content */
664){
665 u32 nNew;
666 JsonNode *pNew;
667 assert( pParse->nNode>=pParse->nAlloc );
668 if( pParse->oom ) return -1;
669 nNew = pParse->nAlloc*2 + 10;
670 pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew);
671 if( pNew==0 ){
672 pParse->oom = 1;
673 return -1;
674 }
675 pParse->nAlloc = nNew;
676 pParse->aNode = pNew;
677 assert( pParse->nNode<pParse->nAlloc );
678 return jsonParseAddNode(pParse, eType, n, zContent);
679}
680
drh5fa5c102015-08-12 16:49:40 +0000681/*
drhe9c37f32015-08-15 21:25:36 +0000682** Create a new JsonNode instance based on the arguments and append that
683** instance to the JsonParse. Return the index in pParse->aNode[] of the
684** new node, or -1 if a memory allocation fails.
685*/
686static int jsonParseAddNode(
687 JsonParse *pParse, /* Append the node to this object */
688 u32 eType, /* Node type */
689 u32 n, /* Content size or sub-node count */
690 const char *zContent /* Content */
691){
692 JsonNode *p;
693 if( pParse->nNode>=pParse->nAlloc ){
drh95677942015-09-24 01:06:37 +0000694 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
drhe9c37f32015-08-15 21:25:36 +0000695 }
696 p = &pParse->aNode[pParse->nNode];
drh5634cc02015-08-17 11:28:03 +0000697 p->eType = (u8)eType;
drh301eecc2015-08-17 20:14:19 +0000698 p->jnFlags = 0;
drhd0960592015-08-17 21:22:32 +0000699 p->iVal = 0;
drhe9c37f32015-08-15 21:25:36 +0000700 p->n = n;
drh52216ad2015-08-18 02:28:03 +0000701 p->u.zJContent = zContent;
drhe9c37f32015-08-15 21:25:36 +0000702 return pParse->nNode++;
703}
704
705/*
706** Parse a single JSON value which begins at pParse->zJson[i]. Return the
707** index of the first character past the end of the value parsed.
708**
709** Return negative for a syntax error. Special cases: return -2 if the
710** first non-whitespace character is '}' and return -3 if the first
711** non-whitespace character is ']'.
712*/
713static int jsonParseValue(JsonParse *pParse, u32 i){
714 char c;
715 u32 j;
drhbc8f0922015-08-22 19:39:04 +0000716 int iThis;
drhe9c37f32015-08-15 21:25:36 +0000717 int x;
drh852944e2015-09-10 03:29:11 +0000718 JsonNode *pNode;
dan2e8f5512015-09-17 17:21:09 +0000719 while( safe_isspace(pParse->zJson[i]) ){ i++; }
drh8cb15cc2015-09-24 01:40:45 +0000720 if( (c = pParse->zJson[i])=='{' ){
drhe9c37f32015-08-15 21:25:36 +0000721 /* Parse object */
722 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000723 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000724 for(j=i+1;;j++){
dan2e8f5512015-09-17 17:21:09 +0000725 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000726 x = jsonParseValue(pParse, j);
727 if( x<0 ){
drhf27cd1f2015-09-23 01:10:29 +0000728 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000729 return -1;
730 }
drhbe9474e2015-08-22 03:05:54 +0000731 if( pParse->oom ) return -1;
drh852944e2015-09-10 03:29:11 +0000732 pNode = &pParse->aNode[pParse->nNode-1];
733 if( pNode->eType!=JSON_STRING ) return -1;
734 pNode->jnFlags |= JNODE_LABEL;
drhe9c37f32015-08-15 21:25:36 +0000735 j = x;
dan2e8f5512015-09-17 17:21:09 +0000736 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000737 if( pParse->zJson[j]!=':' ) return -1;
738 j++;
739 x = jsonParseValue(pParse, j);
740 if( x<0 ) return -1;
741 j = x;
dan2e8f5512015-09-17 17:21:09 +0000742 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000743 c = pParse->zJson[j];
744 if( c==',' ) continue;
745 if( c!='}' ) return -1;
746 break;
747 }
drhbc8f0922015-08-22 19:39:04 +0000748 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000749 return j+1;
750 }else if( c=='[' ){
751 /* Parse array */
752 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000753 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000754 for(j=i+1;;j++){
dan2e8f5512015-09-17 17:21:09 +0000755 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000756 x = jsonParseValue(pParse, j);
757 if( x<0 ){
drhf27cd1f2015-09-23 01:10:29 +0000758 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000759 return -1;
760 }
761 j = x;
dan2e8f5512015-09-17 17:21:09 +0000762 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000763 c = pParse->zJson[j];
764 if( c==',' ) continue;
765 if( c!=']' ) return -1;
766 break;
767 }
drhbc8f0922015-08-22 19:39:04 +0000768 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000769 return j+1;
770 }else if( c=='"' ){
771 /* Parse string */
drh301eecc2015-08-17 20:14:19 +0000772 u8 jnFlags = 0;
drhe9c37f32015-08-15 21:25:36 +0000773 j = i+1;
774 for(;;){
775 c = pParse->zJson[j];
776 if( c==0 ) return -1;
777 if( c=='\\' ){
778 c = pParse->zJson[++j];
779 if( c==0 ) return -1;
drh301eecc2015-08-17 20:14:19 +0000780 jnFlags = JNODE_ESCAPE;
drhe9c37f32015-08-15 21:25:36 +0000781 }else if( c=='"' ){
782 break;
783 }
784 j++;
785 }
786 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]);
drhbe9474e2015-08-22 03:05:54 +0000787 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
drhe9c37f32015-08-15 21:25:36 +0000788 return j+1;
789 }else if( c=='n'
790 && strncmp(pParse->zJson+i,"null",4)==0
dan2e8f5512015-09-17 17:21:09 +0000791 && !safe_isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000792 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
793 return i+4;
794 }else if( c=='t'
795 && strncmp(pParse->zJson+i,"true",4)==0
dan2e8f5512015-09-17 17:21:09 +0000796 && !safe_isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000797 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
798 return i+4;
799 }else if( c=='f'
800 && strncmp(pParse->zJson+i,"false",5)==0
dan2e8f5512015-09-17 17:21:09 +0000801 && !safe_isalnum(pParse->zJson[i+5]) ){
drhe9c37f32015-08-15 21:25:36 +0000802 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
803 return i+5;
804 }else if( c=='-' || (c>='0' && c<='9') ){
805 /* Parse number */
806 u8 seenDP = 0;
807 u8 seenE = 0;
808 j = i+1;
809 for(;; j++){
810 c = pParse->zJson[j];
811 if( c>='0' && c<='9' ) continue;
812 if( c=='.' ){
813 if( pParse->zJson[j-1]=='-' ) return -1;
814 if( seenDP ) return -1;
815 seenDP = 1;
816 continue;
817 }
818 if( c=='e' || c=='E' ){
819 if( pParse->zJson[j-1]<'0' ) return -1;
820 if( seenE ) return -1;
821 seenDP = seenE = 1;
822 c = pParse->zJson[j+1];
drh8784eca2015-08-23 02:42:30 +0000823 if( c=='+' || c=='-' ){
824 j++;
825 c = pParse->zJson[j+1];
826 }
drhd1f00682015-08-29 16:02:37 +0000827 if( c<'0' || c>'9' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000828 continue;
829 }
830 break;
831 }
832 if( pParse->zJson[j-1]<'0' ) return -1;
833 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
834 j - i, &pParse->zJson[i]);
835 return j;
836 }else if( c=='}' ){
837 return -2; /* End of {...} */
838 }else if( c==']' ){
839 return -3; /* End of [...] */
drh8cb15cc2015-09-24 01:40:45 +0000840 }else if( c==0 ){
841 return 0; /* End of file */
drhe9c37f32015-08-15 21:25:36 +0000842 }else{
843 return -1; /* Syntax error */
844 }
845}
846
847/*
848** Parse a complete JSON string. Return 0 on success or non-zero if there
849** are any errors. If an error occurs, free all memory associated with
850** pParse.
851**
852** pParse is uninitialized when this routine is called.
853*/
drhbc8f0922015-08-22 19:39:04 +0000854static int jsonParse(
855 JsonParse *pParse, /* Initialize and fill this JsonParse object */
856 sqlite3_context *pCtx, /* Report errors here */
857 const char *zJson /* Input JSON text to be parsed */
858){
drhe9c37f32015-08-15 21:25:36 +0000859 int i;
drhe9c37f32015-08-15 21:25:36 +0000860 memset(pParse, 0, sizeof(*pParse));
drhc3722b22015-08-23 20:44:59 +0000861 if( zJson==0 ) return 1;
drhe9c37f32015-08-15 21:25:36 +0000862 pParse->zJson = zJson;
863 i = jsonParseValue(pParse, 0);
drhc3722b22015-08-23 20:44:59 +0000864 if( pParse->oom ) i = -1;
drhe9c37f32015-08-15 21:25:36 +0000865 if( i>0 ){
dan2e8f5512015-09-17 17:21:09 +0000866 while( safe_isspace(zJson[i]) ) i++;
drhe9c37f32015-08-15 21:25:36 +0000867 if( zJson[i] ) i = -1;
868 }
drhd1f00682015-08-29 16:02:37 +0000869 if( i<=0 ){
drhf2df7e72015-08-28 20:07:40 +0000870 if( pCtx!=0 ){
871 if( pParse->oom ){
872 sqlite3_result_error_nomem(pCtx);
873 }else{
874 sqlite3_result_error(pCtx, "malformed JSON", -1);
875 }
876 }
drh505ad2c2015-08-21 17:33:11 +0000877 jsonParseReset(pParse);
drhe9c37f32015-08-15 21:25:36 +0000878 return 1;
879 }
880 return 0;
881}
drh301eecc2015-08-17 20:14:19 +0000882
drh505ad2c2015-08-21 17:33:11 +0000883/* Mark node i of pParse as being a child of iParent. Call recursively
884** to fill in all the descendants of node i.
885*/
886static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
887 JsonNode *pNode = &pParse->aNode[i];
888 u32 j;
889 pParse->aUp[i] = iParent;
890 switch( pNode->eType ){
891 case JSON_ARRAY: {
892 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
893 jsonParseFillInParentage(pParse, i+j, i);
894 }
895 break;
896 }
897 case JSON_OBJECT: {
898 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
899 pParse->aUp[i+j] = i;
900 jsonParseFillInParentage(pParse, i+j+1, i);
901 }
902 break;
903 }
904 default: {
905 break;
906 }
907 }
908}
909
910/*
911** Compute the parentage of all nodes in a completed parse.
912*/
913static int jsonParseFindParents(JsonParse *pParse){
914 u32 *aUp;
915 assert( pParse->aUp==0 );
916 aUp = pParse->aUp = sqlite3_malloc( sizeof(u32)*pParse->nNode );
drhc3722b22015-08-23 20:44:59 +0000917 if( aUp==0 ){
918 pParse->oom = 1;
919 return SQLITE_NOMEM;
920 }
drh505ad2c2015-08-21 17:33:11 +0000921 jsonParseFillInParentage(pParse, 0, 0);
922 return SQLITE_OK;
923}
924
drh8cb0c832015-09-22 00:21:03 +0000925/*
926** Compare the OBJECT label at pNode against zKey,nKey. Return true on
927** a match.
928*/
mistachkinf2c26ed2015-10-12 22:20:29 +0000929static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
drh8cb0c832015-09-22 00:21:03 +0000930 if( pNode->jnFlags & JNODE_RAW ){
931 if( pNode->n!=nKey ) return 0;
932 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
933 }else{
934 if( pNode->n!=nKey+2 ) return 0;
935 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
936 }
937}
938
drh52216ad2015-08-18 02:28:03 +0000939/* forward declaration */
drha7714022015-08-29 00:54:49 +0000940static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
drh52216ad2015-08-18 02:28:03 +0000941
drh987eb1f2015-08-17 15:17:37 +0000942/*
943** Search along zPath to find the node specified. Return a pointer
944** to that node, or NULL if zPath is malformed or if there is no such
945** node.
drh52216ad2015-08-18 02:28:03 +0000946**
947** If pApnd!=0, then try to append new nodes to complete zPath if it is
948** possible to do so and if no existing node corresponds to zPath. If
949** new nodes are appended *pApnd is set to 1.
drh987eb1f2015-08-17 15:17:37 +0000950*/
drha7714022015-08-29 00:54:49 +0000951static JsonNode *jsonLookupStep(
drh52216ad2015-08-18 02:28:03 +0000952 JsonParse *pParse, /* The JSON to search */
953 u32 iRoot, /* Begin the search at this node */
954 const char *zPath, /* The path to search */
drha7714022015-08-29 00:54:49 +0000955 int *pApnd, /* Append nodes to complete path if not NULL */
956 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
drh52216ad2015-08-18 02:28:03 +0000957){
drhbc8f0922015-08-22 19:39:04 +0000958 u32 i, j, nKey;
drh6b43cc82015-08-19 23:02:49 +0000959 const char *zKey;
drh52216ad2015-08-18 02:28:03 +0000960 JsonNode *pRoot = &pParse->aNode[iRoot];
drh987eb1f2015-08-17 15:17:37 +0000961 if( zPath[0]==0 ) return pRoot;
962 if( zPath[0]=='.' ){
963 if( pRoot->eType!=JSON_OBJECT ) return 0;
964 zPath++;
drh6b43cc82015-08-19 23:02:49 +0000965 if( zPath[0]=='"' ){
966 zKey = zPath + 1;
967 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
968 nKey = i-1;
drha8f39a92015-09-21 22:53:16 +0000969 if( zPath[i] ){
970 i++;
971 }else{
972 *pzErr = zPath;
973 return 0;
974 }
drh6b43cc82015-08-19 23:02:49 +0000975 }else{
976 zKey = zPath;
977 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
978 nKey = i;
979 }
drha7714022015-08-29 00:54:49 +0000980 if( nKey==0 ){
981 *pzErr = zPath;
982 return 0;
983 }
drh987eb1f2015-08-17 15:17:37 +0000984 j = 1;
drh52216ad2015-08-18 02:28:03 +0000985 for(;;){
986 while( j<=pRoot->n ){
drh8cb0c832015-09-22 00:21:03 +0000987 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
drha7714022015-08-29 00:54:49 +0000988 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +0000989 }
990 j++;
drh505ad2c2015-08-21 17:33:11 +0000991 j += jsonNodeSize(&pRoot[j]);
drh987eb1f2015-08-17 15:17:37 +0000992 }
drh52216ad2015-08-18 02:28:03 +0000993 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
994 iRoot += pRoot->u.iAppend;
995 pRoot = &pParse->aNode[iRoot];
996 j = 1;
997 }
998 if( pApnd ){
drhbc8f0922015-08-22 19:39:04 +0000999 u32 iStart, iLabel;
1000 JsonNode *pNode;
1001 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
1002 iLabel = jsonParseAddNode(pParse, JSON_STRING, i, zPath);
drh52216ad2015-08-18 02:28:03 +00001003 zPath += i;
drha7714022015-08-29 00:54:49 +00001004 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001005 if( pParse->oom ) return 0;
1006 if( pNode ){
1007 pRoot = &pParse->aNode[iRoot];
1008 pRoot->u.iAppend = iStart - iRoot;
1009 pRoot->jnFlags |= JNODE_APPEND;
1010 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
1011 }
1012 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001013 }
dan2e8f5512015-09-17 17:21:09 +00001014 }else if( zPath[0]=='[' && safe_isdigit(zPath[1]) ){
drh987eb1f2015-08-17 15:17:37 +00001015 if( pRoot->eType!=JSON_ARRAY ) return 0;
1016 i = 0;
drh3d1d2a92015-09-22 01:15:49 +00001017 j = 1;
1018 while( safe_isdigit(zPath[j]) ){
1019 i = i*10 + zPath[j] - '0';
1020 j++;
drh987eb1f2015-08-17 15:17:37 +00001021 }
drh3d1d2a92015-09-22 01:15:49 +00001022 if( zPath[j]!=']' ){
drha7714022015-08-29 00:54:49 +00001023 *pzErr = zPath;
1024 return 0;
1025 }
drh3d1d2a92015-09-22 01:15:49 +00001026 zPath += j + 1;
drh987eb1f2015-08-17 15:17:37 +00001027 j = 1;
drh52216ad2015-08-18 02:28:03 +00001028 for(;;){
drhbc8f0922015-08-22 19:39:04 +00001029 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
1030 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
drh505ad2c2015-08-21 17:33:11 +00001031 j += jsonNodeSize(&pRoot[j]);
drh52216ad2015-08-18 02:28:03 +00001032 }
1033 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
1034 iRoot += pRoot->u.iAppend;
1035 pRoot = &pParse->aNode[iRoot];
1036 j = 1;
drh987eb1f2015-08-17 15:17:37 +00001037 }
1038 if( j<=pRoot->n ){
drha7714022015-08-29 00:54:49 +00001039 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001040 }
1041 if( i==0 && pApnd ){
drhbc8f0922015-08-22 19:39:04 +00001042 u32 iStart;
1043 JsonNode *pNode;
1044 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
drha7714022015-08-29 00:54:49 +00001045 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001046 if( pParse->oom ) return 0;
1047 if( pNode ){
1048 pRoot = &pParse->aNode[iRoot];
1049 pRoot->u.iAppend = iStart - iRoot;
1050 pRoot->jnFlags |= JNODE_APPEND;
1051 }
1052 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001053 }
drh3d1d2a92015-09-22 01:15:49 +00001054 }else{
drha7714022015-08-29 00:54:49 +00001055 *pzErr = zPath;
drh987eb1f2015-08-17 15:17:37 +00001056 }
1057 return 0;
1058}
1059
drh52216ad2015-08-18 02:28:03 +00001060/*
drhbc8f0922015-08-22 19:39:04 +00001061** Append content to pParse that will complete zPath. Return a pointer
1062** to the inserted node, or return NULL if the append fails.
drh52216ad2015-08-18 02:28:03 +00001063*/
1064static JsonNode *jsonLookupAppend(
1065 JsonParse *pParse, /* Append content to the JSON parse */
1066 const char *zPath, /* Description of content to append */
drha7714022015-08-29 00:54:49 +00001067 int *pApnd, /* Set this flag to 1 */
1068 const char **pzErr /* Make this point to any syntax error */
drh52216ad2015-08-18 02:28:03 +00001069){
1070 *pApnd = 1;
1071 if( zPath[0]==0 ){
1072 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
1073 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
1074 }
1075 if( zPath[0]=='.' ){
1076 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
1077 }else if( strncmp(zPath,"[0]",3)==0 ){
1078 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
1079 }else{
1080 return 0;
1081 }
1082 if( pParse->oom ) return 0;
drha7714022015-08-29 00:54:49 +00001083 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001084}
1085
drhbc8f0922015-08-22 19:39:04 +00001086/*
drha7714022015-08-29 00:54:49 +00001087** Return the text of a syntax error message on a JSON path. Space is
1088** obtained from sqlite3_malloc().
1089*/
1090static char *jsonPathSyntaxError(const char *zErr){
1091 return sqlite3_mprintf("JSON path error near '%q'", zErr);
1092}
1093
1094/*
1095** Do a node lookup using zPath. Return a pointer to the node on success.
1096** Return NULL if not found or if there is an error.
1097**
1098** On an error, write an error message into pCtx and increment the
1099** pParse->nErr counter.
1100**
1101** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
1102** nodes are appended.
drha7714022015-08-29 00:54:49 +00001103*/
1104static JsonNode *jsonLookup(
1105 JsonParse *pParse, /* The JSON to search */
1106 const char *zPath, /* The path to search */
1107 int *pApnd, /* Append nodes to complete path if not NULL */
drhf5ddb9c2015-09-11 00:06:41 +00001108 sqlite3_context *pCtx /* Report errors here, if not NULL */
drha7714022015-08-29 00:54:49 +00001109){
1110 const char *zErr = 0;
1111 JsonNode *pNode = 0;
drha8f39a92015-09-21 22:53:16 +00001112 char *zMsg;
drha7714022015-08-29 00:54:49 +00001113
1114 if( zPath==0 ) return 0;
1115 if( zPath[0]!='$' ){
1116 zErr = zPath;
1117 goto lookup_err;
1118 }
1119 zPath++;
drha7714022015-08-29 00:54:49 +00001120 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
drha8f39a92015-09-21 22:53:16 +00001121 if( zErr==0 ) return pNode;
drha7714022015-08-29 00:54:49 +00001122
1123lookup_err:
1124 pParse->nErr++;
drha8f39a92015-09-21 22:53:16 +00001125 assert( zErr!=0 && pCtx!=0 );
1126 zMsg = jsonPathSyntaxError(zErr);
1127 if( zMsg ){
1128 sqlite3_result_error(pCtx, zMsg, -1);
1129 sqlite3_free(zMsg);
1130 }else{
1131 sqlite3_result_error_nomem(pCtx);
drha7714022015-08-29 00:54:49 +00001132 }
drha7714022015-08-29 00:54:49 +00001133 return 0;
1134}
1135
1136
1137/*
drhbc8f0922015-08-22 19:39:04 +00001138** Report the wrong number of arguments for json_insert(), json_replace()
1139** or json_set().
1140*/
1141static void jsonWrongNumArgs(
1142 sqlite3_context *pCtx,
1143 const char *zFuncName
1144){
1145 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
1146 zFuncName);
1147 sqlite3_result_error(pCtx, zMsg, -1);
1148 sqlite3_free(zMsg);
1149}
drh52216ad2015-08-18 02:28:03 +00001150
drha7714022015-08-29 00:54:49 +00001151
drh987eb1f2015-08-17 15:17:37 +00001152/****************************************************************************
1153** SQL functions used for testing and debugging
1154****************************************************************************/
drhe9c37f32015-08-15 21:25:36 +00001155
drh301eecc2015-08-17 20:14:19 +00001156#ifdef SQLITE_DEBUG
drhe9c37f32015-08-15 21:25:36 +00001157/*
drh5634cc02015-08-17 11:28:03 +00001158** The json_parse(JSON) function returns a string which describes
drhe9c37f32015-08-15 21:25:36 +00001159** a parse of the JSON provided. Or it returns NULL if JSON is not
1160** well-formed.
1161*/
drh5634cc02015-08-17 11:28:03 +00001162static void jsonParseFunc(
drhbc8f0922015-08-22 19:39:04 +00001163 sqlite3_context *ctx,
drhe9c37f32015-08-15 21:25:36 +00001164 int argc,
1165 sqlite3_value **argv
1166){
drh505ad2c2015-08-21 17:33:11 +00001167 JsonString s; /* Output string - not real JSON */
1168 JsonParse x; /* The parse */
drhe9c37f32015-08-15 21:25:36 +00001169 u32 i;
drhe9c37f32015-08-15 21:25:36 +00001170
1171 assert( argc==1 );
drhbc8f0922015-08-22 19:39:04 +00001172 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh8784eca2015-08-23 02:42:30 +00001173 jsonParseFindParents(&x);
drhbc8f0922015-08-22 19:39:04 +00001174 jsonInit(&s, ctx);
drhe9c37f32015-08-15 21:25:36 +00001175 for(i=0; i<x.nNode; i++){
drh852944e2015-09-10 03:29:11 +00001176 const char *zType;
1177 if( x.aNode[i].jnFlags & JNODE_LABEL ){
1178 assert( x.aNode[i].eType==JSON_STRING );
1179 zType = "label";
1180 }else{
1181 zType = jsonType[x.aNode[i].eType];
drhe9c37f32015-08-15 21:25:36 +00001182 }
drh852944e2015-09-10 03:29:11 +00001183 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
1184 i, zType, x.aNode[i].n, x.aUp[i]);
1185 if( x.aNode[i].u.zJContent!=0 ){
1186 jsonAppendRaw(&s, " ", 1);
1187 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
1188 }
1189 jsonAppendRaw(&s, "\n", 1);
drhe9c37f32015-08-15 21:25:36 +00001190 }
drh505ad2c2015-08-21 17:33:11 +00001191 jsonParseReset(&x);
drhe9c37f32015-08-15 21:25:36 +00001192 jsonResult(&s);
1193}
1194
drh5634cc02015-08-17 11:28:03 +00001195/*
drhf5ddb9c2015-09-11 00:06:41 +00001196** The json_test1(JSON) function return true (1) if the input is JSON
1197** text generated by another json function. It returns (0) if the input
1198** is not known to be JSON.
drh5634cc02015-08-17 11:28:03 +00001199*/
1200static void jsonTest1Func(
drhbc8f0922015-08-22 19:39:04 +00001201 sqlite3_context *ctx,
drh5634cc02015-08-17 11:28:03 +00001202 int argc,
1203 sqlite3_value **argv
1204){
mistachkin16a93122015-09-11 18:05:01 +00001205 UNUSED_PARAM(argc);
drhf5ddb9c2015-09-11 00:06:41 +00001206 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
drh5634cc02015-08-17 11:28:03 +00001207}
drh301eecc2015-08-17 20:14:19 +00001208#endif /* SQLITE_DEBUG */
drh5634cc02015-08-17 11:28:03 +00001209
drh987eb1f2015-08-17 15:17:37 +00001210/****************************************************************************
drhff135ae2015-12-30 01:07:02 +00001211** Scalar SQL function implementations
drh987eb1f2015-08-17 15:17:37 +00001212****************************************************************************/
1213
1214/*
1215** Implementation of the json_array(VALUE,...) function. Return a JSON
1216** array that contains all values given in arguments. Or if any argument
1217** is a BLOB, throw an error.
1218*/
1219static void jsonArrayFunc(
drhbc8f0922015-08-22 19:39:04 +00001220 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001221 int argc,
1222 sqlite3_value **argv
1223){
1224 int i;
drh505ad2c2015-08-21 17:33:11 +00001225 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001226
drhbc8f0922015-08-22 19:39:04 +00001227 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001228 jsonAppendChar(&jx, '[');
drh987eb1f2015-08-17 15:17:37 +00001229 for(i=0; i<argc; i++){
drhd0960592015-08-17 21:22:32 +00001230 jsonAppendSeparator(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001231 jsonAppendValue(&jx, argv[i]);
drh987eb1f2015-08-17 15:17:37 +00001232 }
drhd0960592015-08-17 21:22:32 +00001233 jsonAppendChar(&jx, ']');
drh987eb1f2015-08-17 15:17:37 +00001234 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001235 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001236}
1237
1238
1239/*
1240** json_array_length(JSON)
1241** json_array_length(JSON, PATH)
1242**
1243** Return the number of elements in the top-level JSON array.
1244** Return 0 if the input is not a well-formed JSON array.
1245*/
1246static void jsonArrayLengthFunc(
drhbc8f0922015-08-22 19:39:04 +00001247 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001248 int argc,
1249 sqlite3_value **argv
1250){
1251 JsonParse x; /* The parse */
1252 sqlite3_int64 n = 0;
1253 u32 i;
drha8f39a92015-09-21 22:53:16 +00001254 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001255
drhf2df7e72015-08-28 20:07:40 +00001256 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001257 assert( x.nNode );
1258 if( argc==2 ){
1259 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
1260 pNode = jsonLookup(&x, zPath, 0, ctx);
1261 }else{
1262 pNode = x.aNode;
1263 }
1264 if( pNode==0 ){
1265 x.nErr = 1;
1266 }else if( pNode->eType==JSON_ARRAY ){
1267 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
1268 for(i=1; i<=pNode->n; n++){
1269 i += jsonNodeSize(&pNode[i]);
drh987eb1f2015-08-17 15:17:37 +00001270 }
drh987eb1f2015-08-17 15:17:37 +00001271 }
drha7714022015-08-29 00:54:49 +00001272 if( x.nErr==0 ) sqlite3_result_int64(ctx, n);
drhf6ec8d42015-08-28 03:48:04 +00001273 jsonParseReset(&x);
1274}
1275
1276/*
drh3ad93bb2015-08-29 19:41:45 +00001277** json_extract(JSON, PATH, ...)
drh987eb1f2015-08-17 15:17:37 +00001278**
drh3ad93bb2015-08-29 19:41:45 +00001279** Return the element described by PATH. Return NULL if there is no
1280** PATH element. If there are multiple PATHs, then return a JSON array
1281** with the result from each path. Throw an error if the JSON or any PATH
1282** is malformed.
drh987eb1f2015-08-17 15:17:37 +00001283*/
1284static void jsonExtractFunc(
drhbc8f0922015-08-22 19:39:04 +00001285 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001286 int argc,
1287 sqlite3_value **argv
1288){
1289 JsonParse x; /* The parse */
1290 JsonNode *pNode;
1291 const char *zPath;
drh3ad93bb2015-08-29 19:41:45 +00001292 JsonString jx;
1293 int i;
1294
1295 if( argc<2 ) return;
drhbc8f0922015-08-22 19:39:04 +00001296 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh3ad93bb2015-08-29 19:41:45 +00001297 jsonInit(&jx, ctx);
1298 jsonAppendChar(&jx, '[');
1299 for(i=1; i<argc; i++){
1300 zPath = (const char*)sqlite3_value_text(argv[i]);
drhf5ddb9c2015-09-11 00:06:41 +00001301 pNode = jsonLookup(&x, zPath, 0, ctx);
drh3ad93bb2015-08-29 19:41:45 +00001302 if( x.nErr ) break;
1303 if( argc>2 ){
1304 jsonAppendSeparator(&jx);
1305 if( pNode ){
1306 jsonRenderNode(pNode, &jx, 0);
1307 }else{
1308 jsonAppendRaw(&jx, "null", 4);
1309 }
1310 }else if( pNode ){
1311 jsonReturn(pNode, ctx, 0);
1312 }
drh987eb1f2015-08-17 15:17:37 +00001313 }
drh3ad93bb2015-08-29 19:41:45 +00001314 if( argc>2 && i==argc ){
1315 jsonAppendChar(&jx, ']');
1316 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001317 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh3ad93bb2015-08-29 19:41:45 +00001318 }
1319 jsonReset(&jx);
drh505ad2c2015-08-21 17:33:11 +00001320 jsonParseReset(&x);
drh987eb1f2015-08-17 15:17:37 +00001321}
1322
1323/*
1324** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
1325** object that contains all name/value given in arguments. Or if any name
1326** is not a string or if any value is a BLOB, throw an error.
1327*/
1328static void jsonObjectFunc(
drhbc8f0922015-08-22 19:39:04 +00001329 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001330 int argc,
1331 sqlite3_value **argv
1332){
1333 int i;
drh505ad2c2015-08-21 17:33:11 +00001334 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001335 const char *z;
1336 u32 n;
1337
1338 if( argc&1 ){
drhbc8f0922015-08-22 19:39:04 +00001339 sqlite3_result_error(ctx, "json_object() requires an even number "
drh987eb1f2015-08-17 15:17:37 +00001340 "of arguments", -1);
1341 return;
1342 }
drhbc8f0922015-08-22 19:39:04 +00001343 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001344 jsonAppendChar(&jx, '{');
drh987eb1f2015-08-17 15:17:37 +00001345 for(i=0; i<argc; i+=2){
drh987eb1f2015-08-17 15:17:37 +00001346 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
drhbc8f0922015-08-22 19:39:04 +00001347 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
drhdc384952015-09-19 18:54:39 +00001348 jsonReset(&jx);
drh987eb1f2015-08-17 15:17:37 +00001349 return;
1350 }
drhd0960592015-08-17 21:22:32 +00001351 jsonAppendSeparator(&jx);
drh987eb1f2015-08-17 15:17:37 +00001352 z = (const char*)sqlite3_value_text(argv[i]);
1353 n = (u32)sqlite3_value_bytes(argv[i]);
1354 jsonAppendString(&jx, z, n);
drhd0960592015-08-17 21:22:32 +00001355 jsonAppendChar(&jx, ':');
drhf5ddb9c2015-09-11 00:06:41 +00001356 jsonAppendValue(&jx, argv[i+1]);
drh987eb1f2015-08-17 15:17:37 +00001357 }
drhd0960592015-08-17 21:22:32 +00001358 jsonAppendChar(&jx, '}');
drh987eb1f2015-08-17 15:17:37 +00001359 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001360 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001361}
1362
1363
1364/*
drh301eecc2015-08-17 20:14:19 +00001365** json_remove(JSON, PATH, ...)
1366**
drh3ad93bb2015-08-29 19:41:45 +00001367** Remove the named elements from JSON and return the result. malformed
1368** JSON or PATH arguments result in an error.
drh301eecc2015-08-17 20:14:19 +00001369*/
1370static void jsonRemoveFunc(
drhbc8f0922015-08-22 19:39:04 +00001371 sqlite3_context *ctx,
drh301eecc2015-08-17 20:14:19 +00001372 int argc,
1373 sqlite3_value **argv
1374){
1375 JsonParse x; /* The parse */
1376 JsonNode *pNode;
1377 const char *zPath;
1378 u32 i;
1379
1380 if( argc<1 ) return;
drhbc8f0922015-08-22 19:39:04 +00001381 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001382 assert( x.nNode );
1383 for(i=1; i<(u32)argc; i++){
1384 zPath = (const char*)sqlite3_value_text(argv[i]);
1385 if( zPath==0 ) goto remove_done;
1386 pNode = jsonLookup(&x, zPath, 0, ctx);
1387 if( x.nErr ) goto remove_done;
1388 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
1389 }
1390 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
1391 jsonReturnJson(x.aNode, ctx, 0);
drhd0960592015-08-17 21:22:32 +00001392 }
drha7714022015-08-29 00:54:49 +00001393remove_done:
drh505ad2c2015-08-21 17:33:11 +00001394 jsonParseReset(&x);
drhd0960592015-08-17 21:22:32 +00001395}
1396
1397/*
1398** json_replace(JSON, PATH, VALUE, ...)
1399**
1400** Replace the value at PATH with VALUE. If PATH does not already exist,
drh3ad93bb2015-08-29 19:41:45 +00001401** this routine is a no-op. If JSON or PATH is malformed, throw an error.
drhd0960592015-08-17 21:22:32 +00001402*/
1403static void jsonReplaceFunc(
drhbc8f0922015-08-22 19:39:04 +00001404 sqlite3_context *ctx,
drhd0960592015-08-17 21:22:32 +00001405 int argc,
1406 sqlite3_value **argv
1407){
1408 JsonParse x; /* The parse */
1409 JsonNode *pNode;
1410 const char *zPath;
1411 u32 i;
1412
1413 if( argc<1 ) return;
1414 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001415 jsonWrongNumArgs(ctx, "replace");
drhd0960592015-08-17 21:22:32 +00001416 return;
1417 }
drhbc8f0922015-08-22 19:39:04 +00001418 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001419 assert( x.nNode );
1420 for(i=1; i<(u32)argc; i+=2){
1421 zPath = (const char*)sqlite3_value_text(argv[i]);
1422 pNode = jsonLookup(&x, zPath, 0, ctx);
1423 if( x.nErr ) goto replace_err;
1424 if( pNode ){
1425 pNode->jnFlags |= (u8)JNODE_REPLACE;
1426 pNode->iVal = (u8)(i+1);
drhd0960592015-08-17 21:22:32 +00001427 }
drha8f39a92015-09-21 22:53:16 +00001428 }
1429 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
1430 sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
1431 }else{
1432 jsonReturnJson(x.aNode, ctx, argv);
drh301eecc2015-08-17 20:14:19 +00001433 }
drha7714022015-08-29 00:54:49 +00001434replace_err:
drh505ad2c2015-08-21 17:33:11 +00001435 jsonParseReset(&x);
drh301eecc2015-08-17 20:14:19 +00001436}
drh505ad2c2015-08-21 17:33:11 +00001437
drh52216ad2015-08-18 02:28:03 +00001438/*
1439** json_set(JSON, PATH, VALUE, ...)
1440**
1441** Set the value at PATH to VALUE. Create the PATH if it does not already
1442** exist. Overwrite existing values that do exist.
drh3ad93bb2015-08-29 19:41:45 +00001443** If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001444**
1445** json_insert(JSON, PATH, VALUE, ...)
1446**
1447** Create PATH and initialize it to VALUE. If PATH already exists, this
drh3ad93bb2015-08-29 19:41:45 +00001448** routine is a no-op. If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001449*/
1450static void jsonSetFunc(
drhbc8f0922015-08-22 19:39:04 +00001451 sqlite3_context *ctx,
drh52216ad2015-08-18 02:28:03 +00001452 int argc,
1453 sqlite3_value **argv
1454){
1455 JsonParse x; /* The parse */
1456 JsonNode *pNode;
1457 const char *zPath;
1458 u32 i;
1459 int bApnd;
drhbc8f0922015-08-22 19:39:04 +00001460 int bIsSet = *(int*)sqlite3_user_data(ctx);
drh52216ad2015-08-18 02:28:03 +00001461
1462 if( argc<1 ) return;
1463 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001464 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
drh52216ad2015-08-18 02:28:03 +00001465 return;
1466 }
drhbc8f0922015-08-22 19:39:04 +00001467 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001468 assert( x.nNode );
1469 for(i=1; i<(u32)argc; i+=2){
1470 zPath = (const char*)sqlite3_value_text(argv[i]);
1471 bApnd = 0;
1472 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
1473 if( x.oom ){
1474 sqlite3_result_error_nomem(ctx);
1475 goto jsonSetDone;
1476 }else if( x.nErr ){
1477 goto jsonSetDone;
1478 }else if( pNode && (bApnd || bIsSet) ){
1479 pNode->jnFlags |= (u8)JNODE_REPLACE;
1480 pNode->iVal = (u8)(i+1);
drh52216ad2015-08-18 02:28:03 +00001481 }
drha8f39a92015-09-21 22:53:16 +00001482 }
1483 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
1484 sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
1485 }else{
1486 jsonReturnJson(x.aNode, ctx, argv);
drh52216ad2015-08-18 02:28:03 +00001487 }
drhbc8f0922015-08-22 19:39:04 +00001488jsonSetDone:
drh505ad2c2015-08-21 17:33:11 +00001489 jsonParseReset(&x);
drh52216ad2015-08-18 02:28:03 +00001490}
drh301eecc2015-08-17 20:14:19 +00001491
1492/*
drh987eb1f2015-08-17 15:17:37 +00001493** json_type(JSON)
1494** json_type(JSON, PATH)
1495**
drh3ad93bb2015-08-29 19:41:45 +00001496** Return the top-level "type" of a JSON string. Throw an error if
1497** either the JSON or PATH inputs are not well-formed.
drh987eb1f2015-08-17 15:17:37 +00001498*/
1499static void jsonTypeFunc(
drhbc8f0922015-08-22 19:39:04 +00001500 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001501 int argc,
1502 sqlite3_value **argv
1503){
1504 JsonParse x; /* The parse */
1505 const char *zPath;
drha8f39a92015-09-21 22:53:16 +00001506 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001507
drhbc8f0922015-08-22 19:39:04 +00001508 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001509 assert( x.nNode );
1510 if( argc==2 ){
1511 zPath = (const char*)sqlite3_value_text(argv[1]);
1512 pNode = jsonLookup(&x, zPath, 0, ctx);
1513 }else{
1514 pNode = x.aNode;
1515 }
1516 if( pNode ){
1517 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
drh987eb1f2015-08-17 15:17:37 +00001518 }
drh505ad2c2015-08-21 17:33:11 +00001519 jsonParseReset(&x);
drh987eb1f2015-08-17 15:17:37 +00001520}
drh5634cc02015-08-17 11:28:03 +00001521
drhbc8f0922015-08-22 19:39:04 +00001522/*
1523** json_valid(JSON)
1524**
drh3ad93bb2015-08-29 19:41:45 +00001525** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
1526** Return 0 otherwise.
drhbc8f0922015-08-22 19:39:04 +00001527*/
1528static void jsonValidFunc(
1529 sqlite3_context *ctx,
1530 int argc,
1531 sqlite3_value **argv
1532){
1533 JsonParse x; /* The parse */
1534 int rc = 0;
1535
mistachkin16a93122015-09-11 18:05:01 +00001536 UNUSED_PARAM(argc);
drha8f39a92015-09-21 22:53:16 +00001537 if( jsonParse(&x, 0, (const char*)sqlite3_value_text(argv[0]))==0 ){
drhbc8f0922015-08-22 19:39:04 +00001538 rc = 1;
1539 }
1540 jsonParseReset(&x);
1541 sqlite3_result_int(ctx, rc);
1542}
1543
drhff135ae2015-12-30 01:07:02 +00001544
1545/****************************************************************************
1546** Aggregate SQL function implementations
1547****************************************************************************/
1548/*
1549** json_group_array(VALUE)
1550**
1551** Return a JSON array composed of all values in the aggregate.
1552*/
1553static void jsonArrayStep(
1554 sqlite3_context *ctx,
1555 int argc,
1556 sqlite3_value **argv
1557){
1558 JsonString *pStr;
drhdf3a9072016-02-11 15:37:18 +00001559 UNUSED_PARAM(argc);
drhff135ae2015-12-30 01:07:02 +00001560 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
1561 if( pStr ){
1562 if( pStr->zBuf==0 ){
1563 jsonInit(pStr, ctx);
1564 jsonAppendChar(pStr, '[');
1565 }else{
1566 jsonAppendChar(pStr, ',');
1567 pStr->pCtx = ctx;
1568 }
1569 jsonAppendValue(pStr, argv[0]);
1570 }
1571}
1572static void jsonArrayFinal(sqlite3_context *ctx){
1573 JsonString *pStr;
1574 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
1575 if( pStr ){
1576 pStr->pCtx = ctx;
1577 jsonAppendChar(pStr, ']');
1578 if( pStr->bErr ){
drh4a642b62016-02-05 01:55:27 +00001579 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00001580 assert( pStr->bStatic );
drhff135ae2015-12-30 01:07:02 +00001581 }else{
1582 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
1583 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
1584 pStr->bStatic = 1;
1585 }
1586 }else{
1587 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
1588 }
1589 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1590}
1591
1592/*
1593** json_group_obj(NAME,VALUE)
1594**
1595** Return a JSON object composed of all names and values in the aggregate.
1596*/
1597static void jsonObjectStep(
1598 sqlite3_context *ctx,
1599 int argc,
1600 sqlite3_value **argv
1601){
1602 JsonString *pStr;
1603 const char *z;
1604 u32 n;
drhdf3a9072016-02-11 15:37:18 +00001605 UNUSED_PARAM(argc);
drhff135ae2015-12-30 01:07:02 +00001606 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
1607 if( pStr ){
1608 if( pStr->zBuf==0 ){
1609 jsonInit(pStr, ctx);
1610 jsonAppendChar(pStr, '{');
1611 }else{
1612 jsonAppendChar(pStr, ',');
1613 pStr->pCtx = ctx;
1614 }
1615 z = (const char*)sqlite3_value_text(argv[0]);
1616 n = (u32)sqlite3_value_bytes(argv[0]);
1617 jsonAppendString(pStr, z, n);
1618 jsonAppendChar(pStr, ':');
1619 jsonAppendValue(pStr, argv[1]);
1620 }
1621}
1622static void jsonObjectFinal(sqlite3_context *ctx){
1623 JsonString *pStr;
1624 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
1625 if( pStr ){
1626 jsonAppendChar(pStr, '}');
1627 if( pStr->bErr ){
drh4a642b62016-02-05 01:55:27 +00001628 if( pStr->bErr==0 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00001629 assert( pStr->bStatic );
drhff135ae2015-12-30 01:07:02 +00001630 }else{
1631 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
1632 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
1633 pStr->bStatic = 1;
1634 }
1635 }else{
1636 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
1637 }
1638 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1639}
1640
1641
drhd2975922015-08-29 17:22:33 +00001642#ifndef SQLITE_OMIT_VIRTUALTABLE
drhcb6c6c62015-08-19 22:47:17 +00001643/****************************************************************************
1644** The json_each virtual table
1645****************************************************************************/
1646typedef struct JsonEachCursor JsonEachCursor;
1647struct JsonEachCursor {
1648 sqlite3_vtab_cursor base; /* Base class - must be first */
drh505ad2c2015-08-21 17:33:11 +00001649 u32 iRowid; /* The rowid */
drh852944e2015-09-10 03:29:11 +00001650 u32 iBegin; /* The first node of the scan */
drh505ad2c2015-08-21 17:33:11 +00001651 u32 i; /* Index in sParse.aNode[] of current row */
1652 u32 iEnd; /* EOF when i equals or exceeds this value */
1653 u8 eType; /* Type of top-level element */
1654 u8 bRecursive; /* True for json_tree(). False for json_each() */
1655 char *zJson; /* Input JSON */
drh383de692015-09-10 17:20:57 +00001656 char *zRoot; /* Path by which to filter zJson */
drh505ad2c2015-08-21 17:33:11 +00001657 JsonParse sParse; /* Parse of the input JSON */
drhcb6c6c62015-08-19 22:47:17 +00001658};
1659
1660/* Constructor for the json_each virtual table */
1661static int jsonEachConnect(
1662 sqlite3 *db,
1663 void *pAux,
1664 int argc, const char *const*argv,
1665 sqlite3_vtab **ppVtab,
1666 char **pzErr
1667){
1668 sqlite3_vtab *pNew;
drh505ad2c2015-08-21 17:33:11 +00001669 int rc;
drhcb6c6c62015-08-19 22:47:17 +00001670
1671/* Column numbers */
drh4af352d2015-08-21 20:02:48 +00001672#define JEACH_KEY 0
1673#define JEACH_VALUE 1
1674#define JEACH_TYPE 2
1675#define JEACH_ATOM 3
1676#define JEACH_ID 4
1677#define JEACH_PARENT 5
1678#define JEACH_FULLKEY 6
drh383de692015-09-10 17:20:57 +00001679#define JEACH_PATH 7
1680#define JEACH_JSON 8
1681#define JEACH_ROOT 9
drhcb6c6c62015-08-19 22:47:17 +00001682
drh6fd5c1e2015-08-21 20:37:12 +00001683 UNUSED_PARAM(pzErr);
1684 UNUSED_PARAM(argv);
1685 UNUSED_PARAM(argc);
1686 UNUSED_PARAM(pAux);
drh505ad2c2015-08-21 17:33:11 +00001687 rc = sqlite3_declare_vtab(db,
drh383de692015-09-10 17:20:57 +00001688 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
1689 "json HIDDEN,root HIDDEN)");
drh505ad2c2015-08-21 17:33:11 +00001690 if( rc==SQLITE_OK ){
1691 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
1692 if( pNew==0 ) return SQLITE_NOMEM;
1693 memset(pNew, 0, sizeof(*pNew));
1694 }
1695 return rc;
drhcb6c6c62015-08-19 22:47:17 +00001696}
1697
1698/* destructor for json_each virtual table */
1699static int jsonEachDisconnect(sqlite3_vtab *pVtab){
1700 sqlite3_free(pVtab);
1701 return SQLITE_OK;
1702}
1703
drh505ad2c2015-08-21 17:33:11 +00001704/* constructor for a JsonEachCursor object for json_each(). */
1705static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
drhcb6c6c62015-08-19 22:47:17 +00001706 JsonEachCursor *pCur;
drh6fd5c1e2015-08-21 20:37:12 +00001707
1708 UNUSED_PARAM(p);
drhcb6c6c62015-08-19 22:47:17 +00001709 pCur = sqlite3_malloc( sizeof(*pCur) );
1710 if( pCur==0 ) return SQLITE_NOMEM;
1711 memset(pCur, 0, sizeof(*pCur));
1712 *ppCursor = &pCur->base;
1713 return SQLITE_OK;
1714}
1715
drh505ad2c2015-08-21 17:33:11 +00001716/* constructor for a JsonEachCursor object for json_tree(). */
1717static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
1718 int rc = jsonEachOpenEach(p, ppCursor);
1719 if( rc==SQLITE_OK ){
1720 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
1721 pCur->bRecursive = 1;
1722 }
1723 return rc;
1724}
1725
drhcb6c6c62015-08-19 22:47:17 +00001726/* Reset a JsonEachCursor back to its original state. Free any memory
1727** held. */
1728static void jsonEachCursorReset(JsonEachCursor *p){
1729 sqlite3_free(p->zJson);
drh383de692015-09-10 17:20:57 +00001730 sqlite3_free(p->zRoot);
drh505ad2c2015-08-21 17:33:11 +00001731 jsonParseReset(&p->sParse);
drhcb6c6c62015-08-19 22:47:17 +00001732 p->iRowid = 0;
1733 p->i = 0;
1734 p->iEnd = 0;
1735 p->eType = 0;
drhcb6c6c62015-08-19 22:47:17 +00001736 p->zJson = 0;
drh383de692015-09-10 17:20:57 +00001737 p->zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00001738}
1739
1740/* Destructor for a jsonEachCursor object */
1741static int jsonEachClose(sqlite3_vtab_cursor *cur){
1742 JsonEachCursor *p = (JsonEachCursor*)cur;
1743 jsonEachCursorReset(p);
1744 sqlite3_free(cur);
1745 return SQLITE_OK;
1746}
1747
1748/* Return TRUE if the jsonEachCursor object has been advanced off the end
1749** of the JSON object */
1750static int jsonEachEof(sqlite3_vtab_cursor *cur){
1751 JsonEachCursor *p = (JsonEachCursor*)cur;
1752 return p->i >= p->iEnd;
1753}
1754
drh505ad2c2015-08-21 17:33:11 +00001755/* Advance the cursor to the next element for json_tree() */
drh4af352d2015-08-21 20:02:48 +00001756static int jsonEachNext(sqlite3_vtab_cursor *cur){
drh505ad2c2015-08-21 17:33:11 +00001757 JsonEachCursor *p = (JsonEachCursor*)cur;
drh4af352d2015-08-21 20:02:48 +00001758 if( p->bRecursive ){
drh852944e2015-09-10 03:29:11 +00001759 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
1760 p->i++;
drh4af352d2015-08-21 20:02:48 +00001761 p->iRowid++;
drh852944e2015-09-10 03:29:11 +00001762 if( p->i<p->iEnd ){
drh8784eca2015-08-23 02:42:30 +00001763 u32 iUp = p->sParse.aUp[p->i];
1764 JsonNode *pUp = &p->sParse.aNode[iUp];
drh4af352d2015-08-21 20:02:48 +00001765 p->eType = pUp->eType;
drh8784eca2015-08-23 02:42:30 +00001766 if( pUp->eType==JSON_ARRAY ){
1767 if( iUp==p->i-1 ){
1768 pUp->u.iKey = 0;
1769 }else{
1770 pUp->u.iKey++;
1771 }
drh4af352d2015-08-21 20:02:48 +00001772 }
1773 }
drh505ad2c2015-08-21 17:33:11 +00001774 }else{
drh4af352d2015-08-21 20:02:48 +00001775 switch( p->eType ){
1776 case JSON_ARRAY: {
1777 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
1778 p->iRowid++;
1779 break;
1780 }
1781 case JSON_OBJECT: {
1782 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
1783 p->iRowid++;
1784 break;
1785 }
1786 default: {
1787 p->i = p->iEnd;
1788 break;
1789 }
drh505ad2c2015-08-21 17:33:11 +00001790 }
1791 }
1792 return SQLITE_OK;
1793}
1794
drh4af352d2015-08-21 20:02:48 +00001795/* Append the name of the path for element i to pStr
1796*/
1797static void jsonEachComputePath(
1798 JsonEachCursor *p, /* The cursor */
1799 JsonString *pStr, /* Write the path here */
1800 u32 i /* Path to this element */
1801){
1802 JsonNode *pNode, *pUp;
1803 u32 iUp;
1804 if( i==0 ){
1805 jsonAppendChar(pStr, '$');
1806 return;
drhcb6c6c62015-08-19 22:47:17 +00001807 }
drh4af352d2015-08-21 20:02:48 +00001808 iUp = p->sParse.aUp[i];
1809 jsonEachComputePath(p, pStr, iUp);
1810 pNode = &p->sParse.aNode[i];
1811 pUp = &p->sParse.aNode[iUp];
1812 if( pUp->eType==JSON_ARRAY ){
1813 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
1814 }else{
1815 assert( pUp->eType==JSON_OBJECT );
drh852944e2015-09-10 03:29:11 +00001816 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
drh4af352d2015-08-21 20:02:48 +00001817 assert( pNode->eType==JSON_STRING );
drh852944e2015-09-10 03:29:11 +00001818 assert( pNode->jnFlags & JNODE_LABEL );
drh4af352d2015-08-21 20:02:48 +00001819 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
1820 }
drhcb6c6c62015-08-19 22:47:17 +00001821}
1822
1823/* Return the value of a column */
1824static int jsonEachColumn(
1825 sqlite3_vtab_cursor *cur, /* The cursor */
1826 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
1827 int i /* Which column to return */
1828){
1829 JsonEachCursor *p = (JsonEachCursor*)cur;
drh505ad2c2015-08-21 17:33:11 +00001830 JsonNode *pThis = &p->sParse.aNode[p->i];
drhcb6c6c62015-08-19 22:47:17 +00001831 switch( i ){
1832 case JEACH_KEY: {
drh8784eca2015-08-23 02:42:30 +00001833 if( p->i==0 ) break;
drhcb6c6c62015-08-19 22:47:17 +00001834 if( p->eType==JSON_OBJECT ){
drh505ad2c2015-08-21 17:33:11 +00001835 jsonReturn(pThis, ctx, 0);
1836 }else if( p->eType==JSON_ARRAY ){
1837 u32 iKey;
1838 if( p->bRecursive ){
1839 if( p->iRowid==0 ) break;
drh8784eca2015-08-23 02:42:30 +00001840 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
drh505ad2c2015-08-21 17:33:11 +00001841 }else{
1842 iKey = p->iRowid;
1843 }
drh6fd5c1e2015-08-21 20:37:12 +00001844 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
drhcb6c6c62015-08-19 22:47:17 +00001845 }
1846 break;
1847 }
1848 case JEACH_VALUE: {
drh852944e2015-09-10 03:29:11 +00001849 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00001850 jsonReturn(pThis, ctx, 0);
1851 break;
1852 }
1853 case JEACH_TYPE: {
drh852944e2015-09-10 03:29:11 +00001854 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00001855 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
1856 break;
1857 }
1858 case JEACH_ATOM: {
drh852944e2015-09-10 03:29:11 +00001859 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00001860 if( pThis->eType>=JSON_ARRAY ) break;
1861 jsonReturn(pThis, ctx, 0);
1862 break;
1863 }
1864 case JEACH_ID: {
drh852944e2015-09-10 03:29:11 +00001865 sqlite3_result_int64(ctx,
1866 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
drh505ad2c2015-08-21 17:33:11 +00001867 break;
1868 }
1869 case JEACH_PARENT: {
drh852944e2015-09-10 03:29:11 +00001870 if( p->i>p->iBegin && p->bRecursive ){
drh6fd5c1e2015-08-21 20:37:12 +00001871 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
drhcb6c6c62015-08-19 22:47:17 +00001872 }
1873 break;
1874 }
drh4af352d2015-08-21 20:02:48 +00001875 case JEACH_FULLKEY: {
1876 JsonString x;
1877 jsonInit(&x, ctx);
1878 if( p->bRecursive ){
1879 jsonEachComputePath(p, &x, p->i);
1880 }else{
drh383de692015-09-10 17:20:57 +00001881 if( p->zRoot ){
1882 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
drh4af352d2015-08-21 20:02:48 +00001883 }else{
1884 jsonAppendChar(&x, '$');
1885 }
1886 if( p->eType==JSON_ARRAY ){
1887 jsonPrintf(30, &x, "[%d]", p->iRowid);
1888 }else{
1889 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
1890 }
1891 }
1892 jsonResult(&x);
1893 break;
1894 }
drhcb6c6c62015-08-19 22:47:17 +00001895 case JEACH_PATH: {
drh383de692015-09-10 17:20:57 +00001896 if( p->bRecursive ){
1897 JsonString x;
1898 jsonInit(&x, ctx);
1899 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
1900 jsonResult(&x);
1901 break;
drh4af352d2015-08-21 20:02:48 +00001902 }
drh383de692015-09-10 17:20:57 +00001903 /* For json_each() path and root are the same so fall through
1904 ** into the root case */
1905 }
1906 case JEACH_ROOT: {
1907 const char *zRoot = p->zRoot;
1908 if( zRoot==0 ) zRoot = "$";
1909 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
drhcb6c6c62015-08-19 22:47:17 +00001910 break;
1911 }
drh3d1d2a92015-09-22 01:15:49 +00001912 case JEACH_JSON: {
drh505ad2c2015-08-21 17:33:11 +00001913 assert( i==JEACH_JSON );
drhcb6c6c62015-08-19 22:47:17 +00001914 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
1915 break;
1916 }
1917 }
1918 return SQLITE_OK;
1919}
1920
1921/* Return the current rowid value */
1922static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
1923 JsonEachCursor *p = (JsonEachCursor*)cur;
1924 *pRowid = p->iRowid;
1925 return SQLITE_OK;
1926}
1927
1928/* The query strategy is to look for an equality constraint on the json
1929** column. Without such a constraint, the table cannot operate. idxNum is
drh383de692015-09-10 17:20:57 +00001930** 1 if the constraint is found, 3 if the constraint and zRoot are found,
drhcb6c6c62015-08-19 22:47:17 +00001931** and 0 otherwise.
1932*/
1933static int jsonEachBestIndex(
1934 sqlite3_vtab *tab,
1935 sqlite3_index_info *pIdxInfo
1936){
1937 int i;
1938 int jsonIdx = -1;
drh383de692015-09-10 17:20:57 +00001939 int rootIdx = -1;
drhcb6c6c62015-08-19 22:47:17 +00001940 const struct sqlite3_index_constraint *pConstraint;
drh6fd5c1e2015-08-21 20:37:12 +00001941
1942 UNUSED_PARAM(tab);
drhcb6c6c62015-08-19 22:47:17 +00001943 pConstraint = pIdxInfo->aConstraint;
1944 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
1945 if( pConstraint->usable==0 ) continue;
1946 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
1947 switch( pConstraint->iColumn ){
1948 case JEACH_JSON: jsonIdx = i; break;
drh383de692015-09-10 17:20:57 +00001949 case JEACH_ROOT: rootIdx = i; break;
drhcb6c6c62015-08-19 22:47:17 +00001950 default: /* no-op */ break;
1951 }
1952 }
1953 if( jsonIdx<0 ){
1954 pIdxInfo->idxNum = 0;
drh505ad2c2015-08-21 17:33:11 +00001955 pIdxInfo->estimatedCost = 1e99;
drhcb6c6c62015-08-19 22:47:17 +00001956 }else{
drh505ad2c2015-08-21 17:33:11 +00001957 pIdxInfo->estimatedCost = 1.0;
drhcb6c6c62015-08-19 22:47:17 +00001958 pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1;
1959 pIdxInfo->aConstraintUsage[jsonIdx].omit = 1;
drh383de692015-09-10 17:20:57 +00001960 if( rootIdx<0 ){
drhcb6c6c62015-08-19 22:47:17 +00001961 pIdxInfo->idxNum = 1;
1962 }else{
drh383de692015-09-10 17:20:57 +00001963 pIdxInfo->aConstraintUsage[rootIdx].argvIndex = 2;
1964 pIdxInfo->aConstraintUsage[rootIdx].omit = 1;
drhcb6c6c62015-08-19 22:47:17 +00001965 pIdxInfo->idxNum = 3;
1966 }
1967 }
1968 return SQLITE_OK;
1969}
1970
1971/* Start a search on a new JSON string */
1972static int jsonEachFilter(
1973 sqlite3_vtab_cursor *cur,
1974 int idxNum, const char *idxStr,
1975 int argc, sqlite3_value **argv
1976){
1977 JsonEachCursor *p = (JsonEachCursor*)cur;
1978 const char *z;
mistachkin16a93122015-09-11 18:05:01 +00001979 const char *zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00001980 sqlite3_int64 n;
1981
drh6fd5c1e2015-08-21 20:37:12 +00001982 UNUSED_PARAM(idxStr);
1983 UNUSED_PARAM(argc);
drhcb6c6c62015-08-19 22:47:17 +00001984 jsonEachCursorReset(p);
1985 if( idxNum==0 ) return SQLITE_OK;
1986 z = (const char*)sqlite3_value_text(argv[0]);
1987 if( z==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00001988 n = sqlite3_value_bytes(argv[0]);
drh6fd5c1e2015-08-21 20:37:12 +00001989 p->zJson = sqlite3_malloc64( n+1 );
drhcb6c6c62015-08-19 22:47:17 +00001990 if( p->zJson==0 ) return SQLITE_NOMEM;
drh6fd5c1e2015-08-21 20:37:12 +00001991 memcpy(p->zJson, z, (size_t)n+1);
drha7714022015-08-29 00:54:49 +00001992 if( jsonParse(&p->sParse, 0, p->zJson) ){
1993 int rc = SQLITE_NOMEM;
1994 if( p->sParse.oom==0 ){
1995 sqlite3_free(cur->pVtab->zErrMsg);
1996 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
1997 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
1998 }
drhcb6c6c62015-08-19 22:47:17 +00001999 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002000 return rc;
2001 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
2002 jsonEachCursorReset(p);
2003 return SQLITE_NOMEM;
drhcb6c6c62015-08-19 22:47:17 +00002004 }else{
drh95677942015-09-24 01:06:37 +00002005 JsonNode *pNode = 0;
drhcb6c6c62015-08-19 22:47:17 +00002006 if( idxNum==3 ){
drha7714022015-08-29 00:54:49 +00002007 const char *zErr = 0;
drha8f39a92015-09-21 22:53:16 +00002008 zRoot = (const char*)sqlite3_value_text(argv[1]);
2009 if( zRoot==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002010 n = sqlite3_value_bytes(argv[1]);
drh383de692015-09-10 17:20:57 +00002011 p->zRoot = sqlite3_malloc64( n+1 );
2012 if( p->zRoot==0 ) return SQLITE_NOMEM;
2013 memcpy(p->zRoot, zRoot, (size_t)n+1);
drha8f39a92015-09-21 22:53:16 +00002014 if( zRoot[0]!='$' ){
2015 zErr = zRoot;
2016 }else{
2017 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
2018 }
2019 if( zErr ){
drha7714022015-08-29 00:54:49 +00002020 sqlite3_free(cur->pVtab->zErrMsg);
2021 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
drhcb6c6c62015-08-19 22:47:17 +00002022 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002023 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
2024 }else if( pNode==0 ){
drhcb6c6c62015-08-19 22:47:17 +00002025 return SQLITE_OK;
2026 }
2027 }else{
2028 pNode = p->sParse.aNode;
2029 }
drh852944e2015-09-10 03:29:11 +00002030 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
drhcb6c6c62015-08-19 22:47:17 +00002031 p->eType = pNode->eType;
2032 if( p->eType>=JSON_ARRAY ){
drh8784eca2015-08-23 02:42:30 +00002033 pNode->u.iKey = 0;
drhc3722b22015-08-23 20:44:59 +00002034 p->iEnd = p->i + pNode->n + 1;
drh852944e2015-09-10 03:29:11 +00002035 if( p->bRecursive ){
drh3d1d2a92015-09-22 01:15:49 +00002036 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
drh852944e2015-09-10 03:29:11 +00002037 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
2038 p->i--;
2039 }
2040 }else{
2041 p->i++;
2042 }
drhcb6c6c62015-08-19 22:47:17 +00002043 }else{
2044 p->iEnd = p->i+1;
2045 }
2046 }
drha8f39a92015-09-21 22:53:16 +00002047 return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002048}
2049
2050/* The methods of the json_each virtual table */
2051static sqlite3_module jsonEachModule = {
2052 0, /* iVersion */
2053 0, /* xCreate */
2054 jsonEachConnect, /* xConnect */
2055 jsonEachBestIndex, /* xBestIndex */
2056 jsonEachDisconnect, /* xDisconnect */
2057 0, /* xDestroy */
drh505ad2c2015-08-21 17:33:11 +00002058 jsonEachOpenEach, /* xOpen - open a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002059 jsonEachClose, /* xClose - close a cursor */
2060 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002061 jsonEachNext, /* xNext - advance a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002062 jsonEachEof, /* xEof - check for end of scan */
2063 jsonEachColumn, /* xColumn - read data */
2064 jsonEachRowid, /* xRowid - read data */
2065 0, /* xUpdate */
2066 0, /* xBegin */
2067 0, /* xSync */
2068 0, /* xCommit */
2069 0, /* xRollback */
2070 0, /* xFindMethod */
2071 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002072 0, /* xSavepoint */
2073 0, /* xRelease */
2074 0 /* xRollbackTo */
drhcb6c6c62015-08-19 22:47:17 +00002075};
2076
drh505ad2c2015-08-21 17:33:11 +00002077/* The methods of the json_tree virtual table. */
2078static sqlite3_module jsonTreeModule = {
2079 0, /* iVersion */
2080 0, /* xCreate */
2081 jsonEachConnect, /* xConnect */
2082 jsonEachBestIndex, /* xBestIndex */
2083 jsonEachDisconnect, /* xDisconnect */
2084 0, /* xDestroy */
2085 jsonEachOpenTree, /* xOpen - open a cursor */
2086 jsonEachClose, /* xClose - close a cursor */
2087 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002088 jsonEachNext, /* xNext - advance a cursor */
drh505ad2c2015-08-21 17:33:11 +00002089 jsonEachEof, /* xEof - check for end of scan */
2090 jsonEachColumn, /* xColumn - read data */
2091 jsonEachRowid, /* xRowid - read data */
2092 0, /* xUpdate */
2093 0, /* xBegin */
2094 0, /* xSync */
2095 0, /* xCommit */
2096 0, /* xRollback */
2097 0, /* xFindMethod */
2098 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002099 0, /* xSavepoint */
2100 0, /* xRelease */
2101 0 /* xRollbackTo */
drh505ad2c2015-08-21 17:33:11 +00002102};
drhd2975922015-08-29 17:22:33 +00002103#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh505ad2c2015-08-21 17:33:11 +00002104
2105/****************************************************************************
drh2f20e132015-09-26 17:44:59 +00002106** The following routines are the only publically visible identifiers in this
2107** file. Call the following routines in order to register the various SQL
drh505ad2c2015-08-21 17:33:11 +00002108** functions and the virtual table implemented by this file.
2109****************************************************************************/
drhcb6c6c62015-08-19 22:47:17 +00002110
drh2f20e132015-09-26 17:44:59 +00002111int sqlite3Json1Init(sqlite3 *db){
drh5fa5c102015-08-12 16:49:40 +00002112 int rc = SQLITE_OK;
drh6fd5c1e2015-08-21 20:37:12 +00002113 unsigned int i;
drh5fa5c102015-08-12 16:49:40 +00002114 static const struct {
2115 const char *zName;
2116 int nArg;
drh52216ad2015-08-18 02:28:03 +00002117 int flag;
drh5fa5c102015-08-12 16:49:40 +00002118 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
2119 } aFunc[] = {
drhf5ddb9c2015-09-11 00:06:41 +00002120 { "json", 1, 0, jsonRemoveFunc },
drh52216ad2015-08-18 02:28:03 +00002121 { "json_array", -1, 0, jsonArrayFunc },
2122 { "json_array_length", 1, 0, jsonArrayLengthFunc },
2123 { "json_array_length", 2, 0, jsonArrayLengthFunc },
drh3ad93bb2015-08-29 19:41:45 +00002124 { "json_extract", -1, 0, jsonExtractFunc },
drh52216ad2015-08-18 02:28:03 +00002125 { "json_insert", -1, 0, jsonSetFunc },
2126 { "json_object", -1, 0, jsonObjectFunc },
2127 { "json_remove", -1, 0, jsonRemoveFunc },
2128 { "json_replace", -1, 0, jsonReplaceFunc },
2129 { "json_set", -1, 1, jsonSetFunc },
2130 { "json_type", 1, 0, jsonTypeFunc },
2131 { "json_type", 2, 0, jsonTypeFunc },
drhbc8f0922015-08-22 19:39:04 +00002132 { "json_valid", 1, 0, jsonValidFunc },
drh987eb1f2015-08-17 15:17:37 +00002133
drh301eecc2015-08-17 20:14:19 +00002134#if SQLITE_DEBUG
drh987eb1f2015-08-17 15:17:37 +00002135 /* DEBUG and TESTING functions */
drh52216ad2015-08-18 02:28:03 +00002136 { "json_parse", 1, 0, jsonParseFunc },
2137 { "json_test1", 1, 0, jsonTest1Func },
drh301eecc2015-08-17 20:14:19 +00002138#endif
drh5fa5c102015-08-12 16:49:40 +00002139 };
drhff135ae2015-12-30 01:07:02 +00002140 static const struct {
2141 const char *zName;
2142 int nArg;
2143 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
2144 void (*xFinal)(sqlite3_context*);
2145 } aAgg[] = {
2146 { "json_group_array", 1, jsonArrayStep, jsonArrayFinal },
2147 { "json_group_object", 2, jsonObjectStep, jsonObjectFinal },
2148 };
drhd2975922015-08-29 17:22:33 +00002149#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002150 static const struct {
2151 const char *zName;
2152 sqlite3_module *pModule;
2153 } aMod[] = {
2154 { "json_each", &jsonEachModule },
2155 { "json_tree", &jsonTreeModule },
2156 };
drhd2975922015-08-29 17:22:33 +00002157#endif
drh5fa5c102015-08-12 16:49:40 +00002158 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
2159 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
drh52216ad2015-08-18 02:28:03 +00002160 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
2161 (void*)&aFunc[i].flag,
drh5fa5c102015-08-12 16:49:40 +00002162 aFunc[i].xFunc, 0, 0);
2163 }
drhff135ae2015-12-30 01:07:02 +00002164 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
2165 rc = sqlite3_create_function(db, aAgg[i].zName, aAgg[i].nArg,
2166 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
2167 0, aAgg[i].xStep, aAgg[i].xFinal);
2168 }
drhd2975922015-08-29 17:22:33 +00002169#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002170 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
2171 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
drhcb6c6c62015-08-19 22:47:17 +00002172 }
drhd2975922015-08-29 17:22:33 +00002173#endif
drh5fa5c102015-08-12 16:49:40 +00002174 return rc;
2175}
drh2f20e132015-09-26 17:44:59 +00002176
2177
dan8d32e802015-10-14 18:45:42 +00002178#ifndef SQLITE_CORE
drh2f20e132015-09-26 17:44:59 +00002179#ifdef _WIN32
2180__declspec(dllexport)
2181#endif
2182int sqlite3_json_init(
2183 sqlite3 *db,
2184 char **pzErrMsg,
2185 const sqlite3_api_routines *pApi
2186){
2187 SQLITE_EXTENSION_INIT2(pApi);
2188 (void)pzErrMsg; /* Unused parameter */
2189 return sqlite3Json1Init(db);
2190}
dan8d32e802015-10-14 18:45:42 +00002191#endif
drh50065652015-10-08 19:29:18 +00002192#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */