blob: bad5cd9b534481e869b3e3d3e62ab2b3f45ae9f1 [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)
drh666d34c2017-01-25 13:54:27 +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 */
drhad875e72016-11-07 13:37:28 +000052# define safe_isdigit(x) sqlite3Isdigit(x)
53# define safe_isalnum(x) sqlite3Isalnum(x)
54# define safe_isxdigit(x) sqlite3Isxdigit(x)
drh49472652015-10-16 15:35:39 +000055#else
56 /* Use the standard library for separate compilation */
57#include <ctype.h> /* amalgamator: keep */
drhad875e72016-11-07 13:37:28 +000058# define safe_isdigit(x) isdigit((unsigned char)(x))
59# define safe_isalnum(x) isalnum((unsigned char)(x))
60# define safe_isxdigit(x) isxdigit((unsigned char)(x))
drh49472652015-10-16 15:35:39 +000061#endif
dan2e8f5512015-09-17 17:21:09 +000062
drh95677942015-09-24 01:06:37 +000063/*
64** Growing our own isspace() routine this way is twice as fast as
65** the library isspace() function, resulting in a 7% overall performance
66** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
67*/
68static const char jsonIsSpace[] = {
drhb9e8f592015-10-16 15:16:06 +000069 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
drh95677942015-09-24 01:06:37 +000070 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 1, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85};
86#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
87
drh9a4718f2015-10-10 14:00:37 +000088#ifndef SQLITE_AMALGAMATION
89 /* Unsigned integer types. These are already defined in the sqliteInt.h,
90 ** but the definitions need to be repeated for separate compilation. */
91 typedef sqlite3_uint64 u64;
92 typedef unsigned int u32;
93 typedef unsigned char u8;
94#endif
drh5fa5c102015-08-12 16:49:40 +000095
drh52216ad2015-08-18 02:28:03 +000096/* Objects */
drh505ad2c2015-08-21 17:33:11 +000097typedef struct JsonString JsonString;
drh52216ad2015-08-18 02:28:03 +000098typedef struct JsonNode JsonNode;
99typedef struct JsonParse JsonParse;
100
drh5634cc02015-08-17 11:28:03 +0000101/* An instance of this object represents a JSON string
102** under construction. Really, this is a generic string accumulator
103** that can be and is used to create strings other than JSON.
drh5fa5c102015-08-12 16:49:40 +0000104*/
drh505ad2c2015-08-21 17:33:11 +0000105struct JsonString {
drh5fa5c102015-08-12 16:49:40 +0000106 sqlite3_context *pCtx; /* Function context - put error messages here */
drh5634cc02015-08-17 11:28:03 +0000107 char *zBuf; /* Append JSON content here */
drh5fa5c102015-08-12 16:49:40 +0000108 u64 nAlloc; /* Bytes of storage available in zBuf[] */
109 u64 nUsed; /* Bytes of zBuf[] currently used */
110 u8 bStatic; /* True if zBuf is static space */
drhd0960592015-08-17 21:22:32 +0000111 u8 bErr; /* True if an error has been encountered */
drh5fa5c102015-08-12 16:49:40 +0000112 char zSpace[100]; /* Initial static space */
113};
114
drhe9c37f32015-08-15 21:25:36 +0000115/* JSON type values
drhbd0621b2015-08-13 13:54:59 +0000116*/
drhe9c37f32015-08-15 21:25:36 +0000117#define JSON_NULL 0
118#define JSON_TRUE 1
119#define JSON_FALSE 2
120#define JSON_INT 3
121#define JSON_REAL 4
122#define JSON_STRING 5
123#define JSON_ARRAY 6
124#define JSON_OBJECT 7
125
drhf5ddb9c2015-09-11 00:06:41 +0000126/* The "subtype" set for JSON values */
127#define JSON_SUBTYPE 74 /* Ascii for "J" */
128
drh987eb1f2015-08-17 15:17:37 +0000129/*
130** Names of the various JSON types:
131*/
132static const char * const jsonType[] = {
133 "null", "true", "false", "integer", "real", "text", "array", "object"
134};
135
drh301eecc2015-08-17 20:14:19 +0000136/* Bit values for the JsonNode.jnFlag field
137*/
138#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
139#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
140#define JNODE_REMOVE 0x04 /* Do not output */
drh633647a2017-03-22 21:24:31 +0000141#define JNODE_REPLACE 0x08 /* Replace with JsonNode.u.iReplace */
142#define JNODE_PATCH 0x10 /* Patch with JsonNode.u.pPatch */
143#define JNODE_APPEND 0x20 /* More ARRAY/OBJECT entries at u.iAppend */
144#define JNODE_LABEL 0x40 /* Is a label of an object */
drh301eecc2015-08-17 20:14:19 +0000145
drh987eb1f2015-08-17 15:17:37 +0000146
drhe9c37f32015-08-15 21:25:36 +0000147/* A single node of parsed JSON
148*/
drhe9c37f32015-08-15 21:25:36 +0000149struct JsonNode {
drh5634cc02015-08-17 11:28:03 +0000150 u8 eType; /* One of the JSON_ type values */
drh301eecc2015-08-17 20:14:19 +0000151 u8 jnFlags; /* JNODE flags */
drhe9c37f32015-08-15 21:25:36 +0000152 u32 n; /* Bytes of content, or number of sub-nodes */
drh52216ad2015-08-18 02:28:03 +0000153 union {
drh0042a972015-08-18 12:59:58 +0000154 const char *zJContent; /* Content for INT, REAL, and STRING */
155 u32 iAppend; /* More terms for ARRAY and OBJECT */
drh505ad2c2015-08-21 17:33:11 +0000156 u32 iKey; /* Key for ARRAY objects in json_tree() */
drh633647a2017-03-22 21:24:31 +0000157 u32 iReplace; /* Replacement content for JNODE_REPLACE */
158 JsonNode *pPatch; /* Node chain of patch for JNODE_PATCH */
drh52216ad2015-08-18 02:28:03 +0000159 } u;
drhe9c37f32015-08-15 21:25:36 +0000160};
161
162/* A completely parsed JSON string
163*/
drhe9c37f32015-08-15 21:25:36 +0000164struct JsonParse {
165 u32 nNode; /* Number of slots of aNode[] used */
166 u32 nAlloc; /* Number of slots of aNode[] allocated */
167 JsonNode *aNode; /* Array of nodes containing the parse */
168 const char *zJson; /* Original JSON string */
drh505ad2c2015-08-21 17:33:11 +0000169 u32 *aUp; /* Index of parent of each node */
drhe9c37f32015-08-15 21:25:36 +0000170 u8 oom; /* Set to true if out of memory */
drha7714022015-08-29 00:54:49 +0000171 u8 nErr; /* Number of errors seen */
drhe9c37f32015-08-15 21:25:36 +0000172};
173
drh505ad2c2015-08-21 17:33:11 +0000174/**************************************************************************
175** Utility routines for dealing with JsonString objects
176**************************************************************************/
drh301eecc2015-08-17 20:14:19 +0000177
drh505ad2c2015-08-21 17:33:11 +0000178/* Set the JsonString object to an empty string
drh5fa5c102015-08-12 16:49:40 +0000179*/
drh505ad2c2015-08-21 17:33:11 +0000180static void jsonZero(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000181 p->zBuf = p->zSpace;
182 p->nAlloc = sizeof(p->zSpace);
183 p->nUsed = 0;
184 p->bStatic = 1;
185}
186
drh505ad2c2015-08-21 17:33:11 +0000187/* Initialize the JsonString object
drh5fa5c102015-08-12 16:49:40 +0000188*/
drh505ad2c2015-08-21 17:33:11 +0000189static void jsonInit(JsonString *p, sqlite3_context *pCtx){
drh5fa5c102015-08-12 16:49:40 +0000190 p->pCtx = pCtx;
drhd0960592015-08-17 21:22:32 +0000191 p->bErr = 0;
drh5fa5c102015-08-12 16:49:40 +0000192 jsonZero(p);
193}
194
195
drh505ad2c2015-08-21 17:33:11 +0000196/* Free all allocated memory and reset the JsonString object back to its
drh5fa5c102015-08-12 16:49:40 +0000197** initial state.
198*/
drh505ad2c2015-08-21 17:33:11 +0000199static void jsonReset(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000200 if( !p->bStatic ) sqlite3_free(p->zBuf);
201 jsonZero(p);
202}
203
204
205/* Report an out-of-memory (OOM) condition
206*/
drh505ad2c2015-08-21 17:33:11 +0000207static void jsonOom(JsonString *p){
drh3d1d2a92015-09-22 01:15:49 +0000208 p->bErr = 1;
209 sqlite3_result_error_nomem(p->pCtx);
210 jsonReset(p);
drh5fa5c102015-08-12 16:49:40 +0000211}
212
213/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
214** Return zero on success. Return non-zero on an OOM error
215*/
drh505ad2c2015-08-21 17:33:11 +0000216static int jsonGrow(JsonString *p, u32 N){
drh301eecc2015-08-17 20:14:19 +0000217 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
drh5fa5c102015-08-12 16:49:40 +0000218 char *zNew;
219 if( p->bStatic ){
drhd0960592015-08-17 21:22:32 +0000220 if( p->bErr ) return 1;
drh5fa5c102015-08-12 16:49:40 +0000221 zNew = sqlite3_malloc64(nTotal);
222 if( zNew==0 ){
223 jsonOom(p);
224 return SQLITE_NOMEM;
225 }
drh6fd5c1e2015-08-21 20:37:12 +0000226 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
drh5fa5c102015-08-12 16:49:40 +0000227 p->zBuf = zNew;
228 p->bStatic = 0;
229 }else{
230 zNew = sqlite3_realloc64(p->zBuf, nTotal);
231 if( zNew==0 ){
232 jsonOom(p);
233 return SQLITE_NOMEM;
234 }
235 p->zBuf = zNew;
236 }
237 p->nAlloc = nTotal;
238 return SQLITE_OK;
239}
240
drh505ad2c2015-08-21 17:33:11 +0000241/* Append N bytes from zIn onto the end of the JsonString string.
drh5fa5c102015-08-12 16:49:40 +0000242*/
drh505ad2c2015-08-21 17:33:11 +0000243static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000244 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
245 memcpy(p->zBuf+p->nUsed, zIn, N);
246 p->nUsed += N;
247}
248
drh4af352d2015-08-21 20:02:48 +0000249/* Append formatted text (not to exceed N bytes) to the JsonString.
250*/
251static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
252 va_list ap;
253 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
254 va_start(ap, zFormat);
255 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
256 va_end(ap);
257 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
258}
259
drh5634cc02015-08-17 11:28:03 +0000260/* Append a single character
261*/
drh505ad2c2015-08-21 17:33:11 +0000262static void jsonAppendChar(JsonString *p, char c){
drh5634cc02015-08-17 11:28:03 +0000263 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
264 p->zBuf[p->nUsed++] = c;
265}
266
drh301eecc2015-08-17 20:14:19 +0000267/* Append a comma separator to the output buffer, if the previous
268** character is not '[' or '{'.
269*/
drh505ad2c2015-08-21 17:33:11 +0000270static void jsonAppendSeparator(JsonString *p){
drh301eecc2015-08-17 20:14:19 +0000271 char c;
272 if( p->nUsed==0 ) return;
273 c = p->zBuf[p->nUsed-1];
274 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
275}
276
drh505ad2c2015-08-21 17:33:11 +0000277/* Append the N-byte string in zIn to the end of the JsonString string
drh5fa5c102015-08-12 16:49:40 +0000278** under construction. Enclose the string in "..." and escape
279** any double-quotes or backslash characters contained within the
280** string.
281*/
drh505ad2c2015-08-21 17:33:11 +0000282static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000283 u32 i;
284 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
285 p->zBuf[p->nUsed++] = '"';
286 for(i=0; i<N; i++){
drh3b7f9a62016-02-04 10:28:57 +0000287 unsigned char c = ((unsigned const char*)zIn)[i];
drh5fa5c102015-08-12 16:49:40 +0000288 if( c=='"' || c=='\\' ){
drh3b7f9a62016-02-04 10:28:57 +0000289 json_simple_escape:
drh4977ccf2015-09-19 11:57:26 +0000290 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
drh5fa5c102015-08-12 16:49:40 +0000291 p->zBuf[p->nUsed++] = '\\';
drh3b7f9a62016-02-04 10:28:57 +0000292 }else if( c<=0x1f ){
293 static const char aSpecial[] = {
294 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
295 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
296 };
297 assert( sizeof(aSpecial)==32 );
298 assert( aSpecial['\b']=='b' );
299 assert( aSpecial['\f']=='f' );
300 assert( aSpecial['\n']=='n' );
301 assert( aSpecial['\r']=='r' );
302 assert( aSpecial['\t']=='t' );
303 if( aSpecial[c] ){
304 c = aSpecial[c];
305 goto json_simple_escape;
306 }
307 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
308 p->zBuf[p->nUsed++] = '\\';
309 p->zBuf[p->nUsed++] = 'u';
310 p->zBuf[p->nUsed++] = '0';
311 p->zBuf[p->nUsed++] = '0';
312 p->zBuf[p->nUsed++] = '0' + (c>>4);
313 c = "0123456789abcdef"[c&0xf];
drh5fa5c102015-08-12 16:49:40 +0000314 }
315 p->zBuf[p->nUsed++] = c;
316 }
317 p->zBuf[p->nUsed++] = '"';
drh4977ccf2015-09-19 11:57:26 +0000318 assert( p->nUsed<p->nAlloc );
drh5fa5c102015-08-12 16:49:40 +0000319}
320
drhd0960592015-08-17 21:22:32 +0000321/*
322** Append a function parameter value to the JSON string under
323** construction.
324*/
325static void jsonAppendValue(
drh505ad2c2015-08-21 17:33:11 +0000326 JsonString *p, /* Append to this JSON string */
drhf5ddb9c2015-09-11 00:06:41 +0000327 sqlite3_value *pValue /* Value to append */
drhd0960592015-08-17 21:22:32 +0000328){
329 switch( sqlite3_value_type(pValue) ){
330 case SQLITE_NULL: {
331 jsonAppendRaw(p, "null", 4);
332 break;
333 }
334 case SQLITE_INTEGER:
335 case SQLITE_FLOAT: {
336 const char *z = (const char*)sqlite3_value_text(pValue);
337 u32 n = (u32)sqlite3_value_bytes(pValue);
338 jsonAppendRaw(p, z, n);
339 break;
340 }
341 case SQLITE_TEXT: {
342 const char *z = (const char*)sqlite3_value_text(pValue);
343 u32 n = (u32)sqlite3_value_bytes(pValue);
drhf5ddb9c2015-09-11 00:06:41 +0000344 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
drhecb5fed2015-08-28 03:33:50 +0000345 jsonAppendRaw(p, z, n);
346 }else{
347 jsonAppendString(p, z, n);
348 }
drhd0960592015-08-17 21:22:32 +0000349 break;
350 }
351 default: {
352 if( p->bErr==0 ){
353 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
drh4a642b62016-02-05 01:55:27 +0000354 p->bErr = 2;
drhd0960592015-08-17 21:22:32 +0000355 jsonReset(p);
356 }
357 break;
358 }
359 }
360}
361
362
drhbd0621b2015-08-13 13:54:59 +0000363/* Make the JSON in p the result of the SQL function.
drh5fa5c102015-08-12 16:49:40 +0000364*/
drh505ad2c2015-08-21 17:33:11 +0000365static void jsonResult(JsonString *p){
drhd0960592015-08-17 21:22:32 +0000366 if( p->bErr==0 ){
drh5fa5c102015-08-12 16:49:40 +0000367 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
368 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
369 SQLITE_UTF8);
370 jsonZero(p);
371 }
372 assert( p->bStatic );
373}
374
drh505ad2c2015-08-21 17:33:11 +0000375/**************************************************************************
376** Utility routines for dealing with JsonNode and JsonParse objects
377**************************************************************************/
378
379/*
380** Return the number of consecutive JsonNode slots need to represent
381** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
382** OBJECT types, the number might be larger.
383**
384** Appended elements are not counted. The value returned is the number
385** by which the JsonNode counter should increment in order to go to the
386** next peer value.
387*/
388static u32 jsonNodeSize(JsonNode *pNode){
389 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
390}
391
392/*
393** Reclaim all memory allocated by a JsonParse object. But do not
394** delete the JsonParse object itself.
395*/
396static void jsonParseReset(JsonParse *pParse){
397 sqlite3_free(pParse->aNode);
398 pParse->aNode = 0;
399 pParse->nNode = 0;
400 pParse->nAlloc = 0;
401 sqlite3_free(pParse->aUp);
402 pParse->aUp = 0;
403}
404
drh5634cc02015-08-17 11:28:03 +0000405/*
406** Convert the JsonNode pNode into a pure JSON string and
407** append to pOut. Subsubstructure is also included. Return
408** the number of JsonNode objects that are encoded.
drhbd0621b2015-08-13 13:54:59 +0000409*/
drh52216ad2015-08-18 02:28:03 +0000410static void jsonRenderNode(
drhd0960592015-08-17 21:22:32 +0000411 JsonNode *pNode, /* The node to render */
drh505ad2c2015-08-21 17:33:11 +0000412 JsonString *pOut, /* Write JSON here */
drhd0960592015-08-17 21:22:32 +0000413 sqlite3_value **aReplace /* Replacement values */
414){
drh633647a2017-03-22 21:24:31 +0000415 if( pNode->jnFlags & (JNODE_REPLACE|JNODE_PATCH) ){
416 if( pNode->jnFlags & JNODE_REPLACE ){
417 jsonAppendValue(pOut, aReplace[pNode->u.iReplace]);
418 return;
419 }
420 pNode = pNode->u.pPatch;
421 }
drh5634cc02015-08-17 11:28:03 +0000422 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000423 default: {
424 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000425 jsonAppendRaw(pOut, "null", 4);
426 break;
427 }
428 case JSON_TRUE: {
429 jsonAppendRaw(pOut, "true", 4);
430 break;
431 }
432 case JSON_FALSE: {
433 jsonAppendRaw(pOut, "false", 5);
434 break;
435 }
436 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000437 if( pNode->jnFlags & JNODE_RAW ){
drh52216ad2015-08-18 02:28:03 +0000438 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000439 break;
440 }
441 /* Fall through into the next case */
442 }
443 case JSON_REAL:
444 case JSON_INT: {
drh52216ad2015-08-18 02:28:03 +0000445 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000446 break;
447 }
448 case JSON_ARRAY: {
drh52216ad2015-08-18 02:28:03 +0000449 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000450 jsonAppendChar(pOut, '[');
drh52216ad2015-08-18 02:28:03 +0000451 for(;;){
452 while( j<=pNode->n ){
drh633647a2017-03-22 21:24:31 +0000453 if( (pNode[j].jnFlags & JNODE_REMOVE)==0 ){
drhd0960592015-08-17 21:22:32 +0000454 jsonAppendSeparator(pOut);
drh52216ad2015-08-18 02:28:03 +0000455 jsonRenderNode(&pNode[j], pOut, aReplace);
drhd0960592015-08-17 21:22:32 +0000456 }
drh505ad2c2015-08-21 17:33:11 +0000457 j += jsonNodeSize(&pNode[j]);
drh301eecc2015-08-17 20:14:19 +0000458 }
drh52216ad2015-08-18 02:28:03 +0000459 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
460 pNode = &pNode[pNode->u.iAppend];
461 j = 1;
drh5634cc02015-08-17 11:28:03 +0000462 }
463 jsonAppendChar(pOut, ']');
464 break;
465 }
466 case JSON_OBJECT: {
drh52216ad2015-08-18 02:28:03 +0000467 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000468 jsonAppendChar(pOut, '{');
drh52216ad2015-08-18 02:28:03 +0000469 for(;;){
470 while( j<=pNode->n ){
471 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
472 jsonAppendSeparator(pOut);
473 jsonRenderNode(&pNode[j], pOut, aReplace);
474 jsonAppendChar(pOut, ':');
drh633647a2017-03-22 21:24:31 +0000475 jsonRenderNode(&pNode[j+1], pOut, aReplace);
drhd0960592015-08-17 21:22:32 +0000476 }
drh505ad2c2015-08-21 17:33:11 +0000477 j += 1 + jsonNodeSize(&pNode[j+1]);
drh301eecc2015-08-17 20:14:19 +0000478 }
drh52216ad2015-08-18 02:28:03 +0000479 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
480 pNode = &pNode[pNode->u.iAppend];
481 j = 1;
drh5634cc02015-08-17 11:28:03 +0000482 }
483 jsonAppendChar(pOut, '}');
484 break;
485 }
drhbd0621b2015-08-13 13:54:59 +0000486 }
drh5634cc02015-08-17 11:28:03 +0000487}
488
489/*
drhf2df7e72015-08-28 20:07:40 +0000490** Return a JsonNode and all its descendents as a JSON string.
491*/
492static void jsonReturnJson(
493 JsonNode *pNode, /* Node to return */
494 sqlite3_context *pCtx, /* Return value for this function */
495 sqlite3_value **aReplace /* Array of replacement values */
496){
497 JsonString s;
498 jsonInit(&s, pCtx);
499 jsonRenderNode(pNode, &s, aReplace);
500 jsonResult(&s);
drhf5ddb9c2015-09-11 00:06:41 +0000501 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
drhf2df7e72015-08-28 20:07:40 +0000502}
503
504/*
drh5634cc02015-08-17 11:28:03 +0000505** Make the JsonNode the return value of the function.
506*/
drhd0960592015-08-17 21:22:32 +0000507static void jsonReturn(
508 JsonNode *pNode, /* Node to return */
509 sqlite3_context *pCtx, /* Return value for this function */
510 sqlite3_value **aReplace /* Array of replacement values */
511){
drh5634cc02015-08-17 11:28:03 +0000512 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000513 default: {
514 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000515 sqlite3_result_null(pCtx);
516 break;
517 }
518 case JSON_TRUE: {
519 sqlite3_result_int(pCtx, 1);
520 break;
521 }
522 case JSON_FALSE: {
523 sqlite3_result_int(pCtx, 0);
524 break;
525 }
drh987eb1f2015-08-17 15:17:37 +0000526 case JSON_INT: {
527 sqlite3_int64 i = 0;
drh52216ad2015-08-18 02:28:03 +0000528 const char *z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000529 if( z[0]=='-' ){ z++; }
drh8deb4b82015-10-09 18:21:43 +0000530 while( z[0]>='0' && z[0]<='9' ){
531 unsigned v = *(z++) - '0';
532 if( i>=LARGEST_INT64/10 ){
drha0882fa2015-10-09 20:40:44 +0000533 if( i>LARGEST_INT64/10 ) goto int_as_real;
drh8deb4b82015-10-09 18:21:43 +0000534 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
535 if( v==9 ) goto int_as_real;
536 if( v==8 ){
537 if( pNode->u.zJContent[0]=='-' ){
538 sqlite3_result_int64(pCtx, SMALLEST_INT64);
539 goto int_done;
540 }else{
541 goto int_as_real;
542 }
543 }
544 }
545 i = i*10 + v;
546 }
drh52216ad2015-08-18 02:28:03 +0000547 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
drh987eb1f2015-08-17 15:17:37 +0000548 sqlite3_result_int64(pCtx, i);
drh8deb4b82015-10-09 18:21:43 +0000549 int_done:
550 break;
551 int_as_real: /* fall through to real */;
552 }
553 case JSON_REAL: {
drh49472652015-10-16 15:35:39 +0000554 double r;
555#ifdef SQLITE_AMALGAMATION
556 const char *z = pNode->u.zJContent;
557 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
558#else
559 r = strtod(pNode->u.zJContent, 0);
560#endif
drh8deb4b82015-10-09 18:21:43 +0000561 sqlite3_result_double(pCtx, r);
drh987eb1f2015-08-17 15:17:37 +0000562 break;
563 }
drh5634cc02015-08-17 11:28:03 +0000564 case JSON_STRING: {
drha8f39a92015-09-21 22:53:16 +0000565#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
566 ** json_insert() and json_replace() and those routines do not
567 ** call jsonReturn() */
drh301eecc2015-08-17 20:14:19 +0000568 if( pNode->jnFlags & JNODE_RAW ){
drh52216ad2015-08-18 02:28:03 +0000569 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
570 SQLITE_TRANSIENT);
drha8f39a92015-09-21 22:53:16 +0000571 }else
572#endif
573 assert( (pNode->jnFlags & JNODE_RAW)==0 );
574 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
drh987eb1f2015-08-17 15:17:37 +0000575 /* JSON formatted without any backslash-escapes */
drh52216ad2015-08-18 02:28:03 +0000576 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
drh987eb1f2015-08-17 15:17:37 +0000577 SQLITE_TRANSIENT);
drh5634cc02015-08-17 11:28:03 +0000578 }else{
579 /* Translate JSON formatted string into raw text */
drh987eb1f2015-08-17 15:17:37 +0000580 u32 i;
581 u32 n = pNode->n;
drh52216ad2015-08-18 02:28:03 +0000582 const char *z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000583 char *zOut;
584 u32 j;
585 zOut = sqlite3_malloc( n+1 );
586 if( zOut==0 ){
587 sqlite3_result_error_nomem(pCtx);
588 break;
589 }
590 for(i=1, j=0; i<n-1; i++){
591 char c = z[i];
drh80d87402015-08-24 12:42:41 +0000592 if( c!='\\' ){
drh987eb1f2015-08-17 15:17:37 +0000593 zOut[j++] = c;
594 }else{
595 c = z[++i];
drh80d87402015-08-24 12:42:41 +0000596 if( c=='u' ){
drh987eb1f2015-08-17 15:17:37 +0000597 u32 v = 0, k;
drh27b2d1b2016-11-07 15:15:42 +0000598 for(k=0; k<4; i++, k++){
599 assert( i<n-2 );
drh8784eca2015-08-23 02:42:30 +0000600 c = z[i+1];
drh27b2d1b2016-11-07 15:15:42 +0000601 assert( safe_isxdigit(c) );
602 if( c<='9' ) v = v*16 + c - '0';
603 else if( c<='F' ) v = v*16 + c - 'A' + 10;
604 else v = v*16 + c - 'a' + 10;
drh987eb1f2015-08-17 15:17:37 +0000605 }
drh80d87402015-08-24 12:42:41 +0000606 if( v==0 ) break;
drh987eb1f2015-08-17 15:17:37 +0000607 if( v<=0x7f ){
mistachkin16a93122015-09-11 18:05:01 +0000608 zOut[j++] = (char)v;
drh987eb1f2015-08-17 15:17:37 +0000609 }else if( v<=0x7ff ){
mistachkin16a93122015-09-11 18:05:01 +0000610 zOut[j++] = (char)(0xc0 | (v>>6));
drh987eb1f2015-08-17 15:17:37 +0000611 zOut[j++] = 0x80 | (v&0x3f);
drh80d87402015-08-24 12:42:41 +0000612 }else{
mistachkin16a93122015-09-11 18:05:01 +0000613 zOut[j++] = (char)(0xe0 | (v>>12));
drh987eb1f2015-08-17 15:17:37 +0000614 zOut[j++] = 0x80 | ((v>>6)&0x3f);
615 zOut[j++] = 0x80 | (v&0x3f);
drh987eb1f2015-08-17 15:17:37 +0000616 }
617 }else{
618 if( c=='b' ){
619 c = '\b';
620 }else if( c=='f' ){
621 c = '\f';
622 }else if( c=='n' ){
623 c = '\n';
624 }else if( c=='r' ){
625 c = '\r';
626 }else if( c=='t' ){
627 c = '\t';
628 }
629 zOut[j++] = c;
630 }
631 }
632 }
633 zOut[j] = 0;
634 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
drh5634cc02015-08-17 11:28:03 +0000635 }
636 break;
637 }
638 case JSON_ARRAY:
639 case JSON_OBJECT: {
drhf2df7e72015-08-28 20:07:40 +0000640 jsonReturnJson(pNode, pCtx, aReplace);
drh5634cc02015-08-17 11:28:03 +0000641 break;
642 }
643 }
drhbd0621b2015-08-13 13:54:59 +0000644}
645
drh95677942015-09-24 01:06:37 +0000646/* Forward reference */
647static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
648
649/*
650** A macro to hint to the compiler that a function should not be
651** inlined.
652*/
653#if defined(__GNUC__)
654# define JSON_NOINLINE __attribute__((noinline))
655#elif defined(_MSC_VER) && _MSC_VER>=1310
656# define JSON_NOINLINE __declspec(noinline)
657#else
658# define JSON_NOINLINE
659#endif
660
661
662static JSON_NOINLINE int jsonParseAddNodeExpand(
663 JsonParse *pParse, /* Append the node to this object */
664 u32 eType, /* Node type */
665 u32 n, /* Content size or sub-node count */
666 const char *zContent /* Content */
667){
668 u32 nNew;
669 JsonNode *pNew;
670 assert( pParse->nNode>=pParse->nAlloc );
671 if( pParse->oom ) return -1;
672 nNew = pParse->nAlloc*2 + 10;
673 pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew);
674 if( pNew==0 ){
675 pParse->oom = 1;
676 return -1;
677 }
678 pParse->nAlloc = nNew;
679 pParse->aNode = pNew;
680 assert( pParse->nNode<pParse->nAlloc );
681 return jsonParseAddNode(pParse, eType, n, zContent);
682}
683
drh5fa5c102015-08-12 16:49:40 +0000684/*
drhe9c37f32015-08-15 21:25:36 +0000685** Create a new JsonNode instance based on the arguments and append that
686** instance to the JsonParse. Return the index in pParse->aNode[] of the
687** new node, or -1 if a memory allocation fails.
688*/
689static int jsonParseAddNode(
690 JsonParse *pParse, /* Append the node to this object */
691 u32 eType, /* Node type */
692 u32 n, /* Content size or sub-node count */
693 const char *zContent /* Content */
694){
695 JsonNode *p;
696 if( pParse->nNode>=pParse->nAlloc ){
drh95677942015-09-24 01:06:37 +0000697 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
drhe9c37f32015-08-15 21:25:36 +0000698 }
699 p = &pParse->aNode[pParse->nNode];
drh5634cc02015-08-17 11:28:03 +0000700 p->eType = (u8)eType;
drh301eecc2015-08-17 20:14:19 +0000701 p->jnFlags = 0;
drhe9c37f32015-08-15 21:25:36 +0000702 p->n = n;
drh52216ad2015-08-18 02:28:03 +0000703 p->u.zJContent = zContent;
drhe9c37f32015-08-15 21:25:36 +0000704 return pParse->nNode++;
705}
706
707/*
drhad875e72016-11-07 13:37:28 +0000708** Return true if z[] begins with 4 (or more) hexadecimal digits
709*/
710static int jsonIs4Hex(const char *z){
711 int i;
712 for(i=0; i<4; i++) if( !safe_isxdigit(z[i]) ) return 0;
713 return 1;
714}
715
716/*
drhe9c37f32015-08-15 21:25:36 +0000717** Parse a single JSON value which begins at pParse->zJson[i]. Return the
718** index of the first character past the end of the value parsed.
719**
720** Return negative for a syntax error. Special cases: return -2 if the
721** first non-whitespace character is '}' and return -3 if the first
722** non-whitespace character is ']'.
723*/
724static int jsonParseValue(JsonParse *pParse, u32 i){
725 char c;
726 u32 j;
drhbc8f0922015-08-22 19:39:04 +0000727 int iThis;
drhe9c37f32015-08-15 21:25:36 +0000728 int x;
drh852944e2015-09-10 03:29:11 +0000729 JsonNode *pNode;
dan2e8f5512015-09-17 17:21:09 +0000730 while( safe_isspace(pParse->zJson[i]) ){ i++; }
drh8cb15cc2015-09-24 01:40:45 +0000731 if( (c = pParse->zJson[i])=='{' ){
drhe9c37f32015-08-15 21:25:36 +0000732 /* Parse object */
733 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000734 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000735 for(j=i+1;;j++){
dan2e8f5512015-09-17 17:21:09 +0000736 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000737 x = jsonParseValue(pParse, j);
738 if( x<0 ){
drhf27cd1f2015-09-23 01:10:29 +0000739 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000740 return -1;
741 }
drhbe9474e2015-08-22 03:05:54 +0000742 if( pParse->oom ) return -1;
drh852944e2015-09-10 03:29:11 +0000743 pNode = &pParse->aNode[pParse->nNode-1];
744 if( pNode->eType!=JSON_STRING ) return -1;
745 pNode->jnFlags |= JNODE_LABEL;
drhe9c37f32015-08-15 21:25:36 +0000746 j = x;
dan2e8f5512015-09-17 17:21:09 +0000747 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000748 if( pParse->zJson[j]!=':' ) return -1;
749 j++;
750 x = jsonParseValue(pParse, j);
751 if( x<0 ) return -1;
752 j = x;
dan2e8f5512015-09-17 17:21:09 +0000753 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000754 c = pParse->zJson[j];
755 if( c==',' ) continue;
756 if( c!='}' ) return -1;
757 break;
758 }
drhbc8f0922015-08-22 19:39:04 +0000759 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000760 return j+1;
761 }else if( c=='[' ){
762 /* Parse array */
763 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000764 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000765 for(j=i+1;;j++){
dan2e8f5512015-09-17 17:21:09 +0000766 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000767 x = jsonParseValue(pParse, j);
768 if( x<0 ){
drhf27cd1f2015-09-23 01:10:29 +0000769 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000770 return -1;
771 }
772 j = x;
dan2e8f5512015-09-17 17:21:09 +0000773 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000774 c = pParse->zJson[j];
775 if( c==',' ) continue;
776 if( c!=']' ) return -1;
777 break;
778 }
drhbc8f0922015-08-22 19:39:04 +0000779 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000780 return j+1;
781 }else if( c=='"' ){
782 /* Parse string */
drh301eecc2015-08-17 20:14:19 +0000783 u8 jnFlags = 0;
drhe9c37f32015-08-15 21:25:36 +0000784 j = i+1;
785 for(;;){
786 c = pParse->zJson[j];
787 if( c==0 ) return -1;
788 if( c=='\\' ){
789 c = pParse->zJson[++j];
drhad875e72016-11-07 13:37:28 +0000790 if( c=='"' || c=='\\' || c=='/' || c=='b' || c=='f'
791 || c=='n' || c=='r' || c=='t'
792 || (c=='u' && jsonIs4Hex(pParse->zJson+j+1)) ){
793 jnFlags = JNODE_ESCAPE;
794 }else{
795 return -1;
796 }
drhe9c37f32015-08-15 21:25:36 +0000797 }else if( c=='"' ){
798 break;
799 }
800 j++;
801 }
802 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]);
drhbe9474e2015-08-22 03:05:54 +0000803 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
drhe9c37f32015-08-15 21:25:36 +0000804 return j+1;
805 }else if( c=='n'
806 && strncmp(pParse->zJson+i,"null",4)==0
dan2e8f5512015-09-17 17:21:09 +0000807 && !safe_isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000808 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
809 return i+4;
810 }else if( c=='t'
811 && strncmp(pParse->zJson+i,"true",4)==0
dan2e8f5512015-09-17 17:21:09 +0000812 && !safe_isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000813 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
814 return i+4;
815 }else if( c=='f'
816 && strncmp(pParse->zJson+i,"false",5)==0
dan2e8f5512015-09-17 17:21:09 +0000817 && !safe_isalnum(pParse->zJson[i+5]) ){
drhe9c37f32015-08-15 21:25:36 +0000818 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
819 return i+5;
820 }else if( c=='-' || (c>='0' && c<='9') ){
821 /* Parse number */
822 u8 seenDP = 0;
823 u8 seenE = 0;
824 j = i+1;
825 for(;; j++){
826 c = pParse->zJson[j];
827 if( c>='0' && c<='9' ) continue;
828 if( c=='.' ){
829 if( pParse->zJson[j-1]=='-' ) return -1;
830 if( seenDP ) return -1;
831 seenDP = 1;
832 continue;
833 }
834 if( c=='e' || c=='E' ){
835 if( pParse->zJson[j-1]<'0' ) return -1;
836 if( seenE ) return -1;
837 seenDP = seenE = 1;
838 c = pParse->zJson[j+1];
drh8784eca2015-08-23 02:42:30 +0000839 if( c=='+' || c=='-' ){
840 j++;
841 c = pParse->zJson[j+1];
842 }
drhd1f00682015-08-29 16:02:37 +0000843 if( c<'0' || c>'9' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000844 continue;
845 }
846 break;
847 }
848 if( pParse->zJson[j-1]<'0' ) return -1;
849 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
850 j - i, &pParse->zJson[i]);
851 return j;
852 }else if( c=='}' ){
853 return -2; /* End of {...} */
854 }else if( c==']' ){
855 return -3; /* End of [...] */
drh8cb15cc2015-09-24 01:40:45 +0000856 }else if( c==0 ){
857 return 0; /* End of file */
drhe9c37f32015-08-15 21:25:36 +0000858 }else{
859 return -1; /* Syntax error */
860 }
861}
862
863/*
864** Parse a complete JSON string. Return 0 on success or non-zero if there
865** are any errors. If an error occurs, free all memory associated with
866** pParse.
867**
868** pParse is uninitialized when this routine is called.
869*/
drhbc8f0922015-08-22 19:39:04 +0000870static int jsonParse(
871 JsonParse *pParse, /* Initialize and fill this JsonParse object */
872 sqlite3_context *pCtx, /* Report errors here */
873 const char *zJson /* Input JSON text to be parsed */
874){
drhe9c37f32015-08-15 21:25:36 +0000875 int i;
drhe9c37f32015-08-15 21:25:36 +0000876 memset(pParse, 0, sizeof(*pParse));
drhc3722b22015-08-23 20:44:59 +0000877 if( zJson==0 ) return 1;
drhe9c37f32015-08-15 21:25:36 +0000878 pParse->zJson = zJson;
879 i = jsonParseValue(pParse, 0);
drhc3722b22015-08-23 20:44:59 +0000880 if( pParse->oom ) i = -1;
drhe9c37f32015-08-15 21:25:36 +0000881 if( i>0 ){
dan2e8f5512015-09-17 17:21:09 +0000882 while( safe_isspace(zJson[i]) ) i++;
drhe9c37f32015-08-15 21:25:36 +0000883 if( zJson[i] ) i = -1;
884 }
drhd1f00682015-08-29 16:02:37 +0000885 if( i<=0 ){
drhf2df7e72015-08-28 20:07:40 +0000886 if( pCtx!=0 ){
887 if( pParse->oom ){
888 sqlite3_result_error_nomem(pCtx);
889 }else{
890 sqlite3_result_error(pCtx, "malformed JSON", -1);
891 }
892 }
drh505ad2c2015-08-21 17:33:11 +0000893 jsonParseReset(pParse);
drhe9c37f32015-08-15 21:25:36 +0000894 return 1;
895 }
896 return 0;
897}
drh301eecc2015-08-17 20:14:19 +0000898
drh505ad2c2015-08-21 17:33:11 +0000899/* Mark node i of pParse as being a child of iParent. Call recursively
900** to fill in all the descendants of node i.
901*/
902static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
903 JsonNode *pNode = &pParse->aNode[i];
904 u32 j;
905 pParse->aUp[i] = iParent;
906 switch( pNode->eType ){
907 case JSON_ARRAY: {
908 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
909 jsonParseFillInParentage(pParse, i+j, i);
910 }
911 break;
912 }
913 case JSON_OBJECT: {
914 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
915 pParse->aUp[i+j] = i;
916 jsonParseFillInParentage(pParse, i+j+1, i);
917 }
918 break;
919 }
920 default: {
921 break;
922 }
923 }
924}
925
926/*
927** Compute the parentage of all nodes in a completed parse.
928*/
929static int jsonParseFindParents(JsonParse *pParse){
930 u32 *aUp;
931 assert( pParse->aUp==0 );
932 aUp = pParse->aUp = sqlite3_malloc( sizeof(u32)*pParse->nNode );
drhc3722b22015-08-23 20:44:59 +0000933 if( aUp==0 ){
934 pParse->oom = 1;
935 return SQLITE_NOMEM;
936 }
drh505ad2c2015-08-21 17:33:11 +0000937 jsonParseFillInParentage(pParse, 0, 0);
938 return SQLITE_OK;
939}
940
drh8cb0c832015-09-22 00:21:03 +0000941/*
942** Compare the OBJECT label at pNode against zKey,nKey. Return true on
943** a match.
944*/
mistachkinf2c26ed2015-10-12 22:20:29 +0000945static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
drh8cb0c832015-09-22 00:21:03 +0000946 if( pNode->jnFlags & JNODE_RAW ){
947 if( pNode->n!=nKey ) return 0;
948 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
949 }else{
950 if( pNode->n!=nKey+2 ) return 0;
951 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
952 }
953}
954
drh52216ad2015-08-18 02:28:03 +0000955/* forward declaration */
drha7714022015-08-29 00:54:49 +0000956static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
drh52216ad2015-08-18 02:28:03 +0000957
drh987eb1f2015-08-17 15:17:37 +0000958/*
959** Search along zPath to find the node specified. Return a pointer
960** to that node, or NULL if zPath is malformed or if there is no such
961** node.
drh52216ad2015-08-18 02:28:03 +0000962**
963** If pApnd!=0, then try to append new nodes to complete zPath if it is
964** possible to do so and if no existing node corresponds to zPath. If
965** new nodes are appended *pApnd is set to 1.
drh987eb1f2015-08-17 15:17:37 +0000966*/
drha7714022015-08-29 00:54:49 +0000967static JsonNode *jsonLookupStep(
drh52216ad2015-08-18 02:28:03 +0000968 JsonParse *pParse, /* The JSON to search */
969 u32 iRoot, /* Begin the search at this node */
970 const char *zPath, /* The path to search */
drha7714022015-08-29 00:54:49 +0000971 int *pApnd, /* Append nodes to complete path if not NULL */
972 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
drh52216ad2015-08-18 02:28:03 +0000973){
drhbc8f0922015-08-22 19:39:04 +0000974 u32 i, j, nKey;
drh6b43cc82015-08-19 23:02:49 +0000975 const char *zKey;
drh52216ad2015-08-18 02:28:03 +0000976 JsonNode *pRoot = &pParse->aNode[iRoot];
drh987eb1f2015-08-17 15:17:37 +0000977 if( zPath[0]==0 ) return pRoot;
978 if( zPath[0]=='.' ){
979 if( pRoot->eType!=JSON_OBJECT ) return 0;
980 zPath++;
drh6b43cc82015-08-19 23:02:49 +0000981 if( zPath[0]=='"' ){
982 zKey = zPath + 1;
983 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
984 nKey = i-1;
drha8f39a92015-09-21 22:53:16 +0000985 if( zPath[i] ){
986 i++;
987 }else{
988 *pzErr = zPath;
989 return 0;
990 }
drh6b43cc82015-08-19 23:02:49 +0000991 }else{
992 zKey = zPath;
993 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
994 nKey = i;
995 }
drha7714022015-08-29 00:54:49 +0000996 if( nKey==0 ){
997 *pzErr = zPath;
998 return 0;
999 }
drh987eb1f2015-08-17 15:17:37 +00001000 j = 1;
drh52216ad2015-08-18 02:28:03 +00001001 for(;;){
1002 while( j<=pRoot->n ){
drh8cb0c832015-09-22 00:21:03 +00001003 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
drha7714022015-08-29 00:54:49 +00001004 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001005 }
1006 j++;
drh505ad2c2015-08-21 17:33:11 +00001007 j += jsonNodeSize(&pRoot[j]);
drh987eb1f2015-08-17 15:17:37 +00001008 }
drh52216ad2015-08-18 02:28:03 +00001009 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
1010 iRoot += pRoot->u.iAppend;
1011 pRoot = &pParse->aNode[iRoot];
1012 j = 1;
1013 }
1014 if( pApnd ){
drhbc8f0922015-08-22 19:39:04 +00001015 u32 iStart, iLabel;
1016 JsonNode *pNode;
1017 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
1018 iLabel = jsonParseAddNode(pParse, JSON_STRING, i, zPath);
drh52216ad2015-08-18 02:28:03 +00001019 zPath += i;
drha7714022015-08-29 00:54:49 +00001020 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001021 if( pParse->oom ) return 0;
1022 if( pNode ){
1023 pRoot = &pParse->aNode[iRoot];
1024 pRoot->u.iAppend = iStart - iRoot;
1025 pRoot->jnFlags |= JNODE_APPEND;
1026 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
1027 }
1028 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001029 }
dan2e8f5512015-09-17 17:21:09 +00001030 }else if( zPath[0]=='[' && safe_isdigit(zPath[1]) ){
drh987eb1f2015-08-17 15:17:37 +00001031 if( pRoot->eType!=JSON_ARRAY ) return 0;
1032 i = 0;
drh3d1d2a92015-09-22 01:15:49 +00001033 j = 1;
1034 while( safe_isdigit(zPath[j]) ){
1035 i = i*10 + zPath[j] - '0';
1036 j++;
drh987eb1f2015-08-17 15:17:37 +00001037 }
drh3d1d2a92015-09-22 01:15:49 +00001038 if( zPath[j]!=']' ){
drha7714022015-08-29 00:54:49 +00001039 *pzErr = zPath;
1040 return 0;
1041 }
drh3d1d2a92015-09-22 01:15:49 +00001042 zPath += j + 1;
drh987eb1f2015-08-17 15:17:37 +00001043 j = 1;
drh52216ad2015-08-18 02:28:03 +00001044 for(;;){
drhbc8f0922015-08-22 19:39:04 +00001045 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
1046 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
drh505ad2c2015-08-21 17:33:11 +00001047 j += jsonNodeSize(&pRoot[j]);
drh52216ad2015-08-18 02:28:03 +00001048 }
1049 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
1050 iRoot += pRoot->u.iAppend;
1051 pRoot = &pParse->aNode[iRoot];
1052 j = 1;
drh987eb1f2015-08-17 15:17:37 +00001053 }
1054 if( j<=pRoot->n ){
drha7714022015-08-29 00:54:49 +00001055 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001056 }
1057 if( i==0 && pApnd ){
drhbc8f0922015-08-22 19:39:04 +00001058 u32 iStart;
1059 JsonNode *pNode;
1060 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
drha7714022015-08-29 00:54:49 +00001061 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001062 if( pParse->oom ) return 0;
1063 if( pNode ){
1064 pRoot = &pParse->aNode[iRoot];
1065 pRoot->u.iAppend = iStart - iRoot;
1066 pRoot->jnFlags |= JNODE_APPEND;
1067 }
1068 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001069 }
drh3d1d2a92015-09-22 01:15:49 +00001070 }else{
drha7714022015-08-29 00:54:49 +00001071 *pzErr = zPath;
drh987eb1f2015-08-17 15:17:37 +00001072 }
1073 return 0;
1074}
1075
drh52216ad2015-08-18 02:28:03 +00001076/*
drhbc8f0922015-08-22 19:39:04 +00001077** Append content to pParse that will complete zPath. Return a pointer
1078** to the inserted node, or return NULL if the append fails.
drh52216ad2015-08-18 02:28:03 +00001079*/
1080static JsonNode *jsonLookupAppend(
1081 JsonParse *pParse, /* Append content to the JSON parse */
1082 const char *zPath, /* Description of content to append */
drha7714022015-08-29 00:54:49 +00001083 int *pApnd, /* Set this flag to 1 */
1084 const char **pzErr /* Make this point to any syntax error */
drh52216ad2015-08-18 02:28:03 +00001085){
1086 *pApnd = 1;
1087 if( zPath[0]==0 ){
1088 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
1089 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
1090 }
1091 if( zPath[0]=='.' ){
1092 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
1093 }else if( strncmp(zPath,"[0]",3)==0 ){
1094 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
1095 }else{
1096 return 0;
1097 }
1098 if( pParse->oom ) return 0;
drha7714022015-08-29 00:54:49 +00001099 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001100}
1101
drhbc8f0922015-08-22 19:39:04 +00001102/*
drha7714022015-08-29 00:54:49 +00001103** Return the text of a syntax error message on a JSON path. Space is
1104** obtained from sqlite3_malloc().
1105*/
1106static char *jsonPathSyntaxError(const char *zErr){
1107 return sqlite3_mprintf("JSON path error near '%q'", zErr);
1108}
1109
1110/*
1111** Do a node lookup using zPath. Return a pointer to the node on success.
1112** Return NULL if not found or if there is an error.
1113**
1114** On an error, write an error message into pCtx and increment the
1115** pParse->nErr counter.
1116**
1117** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
1118** nodes are appended.
drha7714022015-08-29 00:54:49 +00001119*/
1120static JsonNode *jsonLookup(
1121 JsonParse *pParse, /* The JSON to search */
1122 const char *zPath, /* The path to search */
1123 int *pApnd, /* Append nodes to complete path if not NULL */
drhf5ddb9c2015-09-11 00:06:41 +00001124 sqlite3_context *pCtx /* Report errors here, if not NULL */
drha7714022015-08-29 00:54:49 +00001125){
1126 const char *zErr = 0;
1127 JsonNode *pNode = 0;
drha8f39a92015-09-21 22:53:16 +00001128 char *zMsg;
drha7714022015-08-29 00:54:49 +00001129
1130 if( zPath==0 ) return 0;
1131 if( zPath[0]!='$' ){
1132 zErr = zPath;
1133 goto lookup_err;
1134 }
1135 zPath++;
drha7714022015-08-29 00:54:49 +00001136 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
drha8f39a92015-09-21 22:53:16 +00001137 if( zErr==0 ) return pNode;
drha7714022015-08-29 00:54:49 +00001138
1139lookup_err:
1140 pParse->nErr++;
drha8f39a92015-09-21 22:53:16 +00001141 assert( zErr!=0 && pCtx!=0 );
1142 zMsg = jsonPathSyntaxError(zErr);
1143 if( zMsg ){
1144 sqlite3_result_error(pCtx, zMsg, -1);
1145 sqlite3_free(zMsg);
1146 }else{
1147 sqlite3_result_error_nomem(pCtx);
drha7714022015-08-29 00:54:49 +00001148 }
drha7714022015-08-29 00:54:49 +00001149 return 0;
1150}
1151
1152
1153/*
drhbc8f0922015-08-22 19:39:04 +00001154** Report the wrong number of arguments for json_insert(), json_replace()
1155** or json_set().
1156*/
1157static void jsonWrongNumArgs(
1158 sqlite3_context *pCtx,
1159 const char *zFuncName
1160){
1161 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
1162 zFuncName);
1163 sqlite3_result_error(pCtx, zMsg, -1);
1164 sqlite3_free(zMsg);
1165}
drh52216ad2015-08-18 02:28:03 +00001166
drh29c99692017-03-24 12:35:17 +00001167/*
1168** Mark all NULL entries in the Object passed in as JNODE_REMOVE.
1169*/
1170static void jsonRemoveAllNulls(JsonNode *pNode){
1171 int i, n;
1172 assert( pNode->eType==JSON_OBJECT );
1173 n = pNode->n;
1174 for(i=2; i<=n; i += jsonNodeSize(&pNode[i])+1){
1175 switch( pNode[i].eType ){
1176 case JSON_NULL:
1177 pNode[i].jnFlags |= JNODE_REMOVE;
1178 break;
1179 case JSON_OBJECT:
1180 jsonRemoveAllNulls(&pNode[i]);
1181 break;
1182 }
1183 }
1184}
1185
drha7714022015-08-29 00:54:49 +00001186
drh987eb1f2015-08-17 15:17:37 +00001187/****************************************************************************
1188** SQL functions used for testing and debugging
1189****************************************************************************/
drhe9c37f32015-08-15 21:25:36 +00001190
drh301eecc2015-08-17 20:14:19 +00001191#ifdef SQLITE_DEBUG
drhe9c37f32015-08-15 21:25:36 +00001192/*
drh5634cc02015-08-17 11:28:03 +00001193** The json_parse(JSON) function returns a string which describes
drhe9c37f32015-08-15 21:25:36 +00001194** a parse of the JSON provided. Or it returns NULL if JSON is not
1195** well-formed.
1196*/
drh5634cc02015-08-17 11:28:03 +00001197static void jsonParseFunc(
drhbc8f0922015-08-22 19:39:04 +00001198 sqlite3_context *ctx,
drhe9c37f32015-08-15 21:25:36 +00001199 int argc,
1200 sqlite3_value **argv
1201){
drh505ad2c2015-08-21 17:33:11 +00001202 JsonString s; /* Output string - not real JSON */
1203 JsonParse x; /* The parse */
drhe9c37f32015-08-15 21:25:36 +00001204 u32 i;
drhe9c37f32015-08-15 21:25:36 +00001205
1206 assert( argc==1 );
drhbc8f0922015-08-22 19:39:04 +00001207 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh8784eca2015-08-23 02:42:30 +00001208 jsonParseFindParents(&x);
drhbc8f0922015-08-22 19:39:04 +00001209 jsonInit(&s, ctx);
drhe9c37f32015-08-15 21:25:36 +00001210 for(i=0; i<x.nNode; i++){
drh852944e2015-09-10 03:29:11 +00001211 const char *zType;
1212 if( x.aNode[i].jnFlags & JNODE_LABEL ){
1213 assert( x.aNode[i].eType==JSON_STRING );
1214 zType = "label";
1215 }else{
1216 zType = jsonType[x.aNode[i].eType];
drhe9c37f32015-08-15 21:25:36 +00001217 }
drh852944e2015-09-10 03:29:11 +00001218 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
1219 i, zType, x.aNode[i].n, x.aUp[i]);
1220 if( x.aNode[i].u.zJContent!=0 ){
1221 jsonAppendRaw(&s, " ", 1);
1222 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
1223 }
1224 jsonAppendRaw(&s, "\n", 1);
drhe9c37f32015-08-15 21:25:36 +00001225 }
drh505ad2c2015-08-21 17:33:11 +00001226 jsonParseReset(&x);
drhe9c37f32015-08-15 21:25:36 +00001227 jsonResult(&s);
1228}
1229
drh5634cc02015-08-17 11:28:03 +00001230/*
drhf5ddb9c2015-09-11 00:06:41 +00001231** The json_test1(JSON) function return true (1) if the input is JSON
1232** text generated by another json function. It returns (0) if the input
1233** is not known to be JSON.
drh5634cc02015-08-17 11:28:03 +00001234*/
1235static void jsonTest1Func(
drhbc8f0922015-08-22 19:39:04 +00001236 sqlite3_context *ctx,
drh5634cc02015-08-17 11:28:03 +00001237 int argc,
1238 sqlite3_value **argv
1239){
mistachkin16a93122015-09-11 18:05:01 +00001240 UNUSED_PARAM(argc);
drhf5ddb9c2015-09-11 00:06:41 +00001241 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
drh5634cc02015-08-17 11:28:03 +00001242}
drh301eecc2015-08-17 20:14:19 +00001243#endif /* SQLITE_DEBUG */
drh5634cc02015-08-17 11:28:03 +00001244
drh987eb1f2015-08-17 15:17:37 +00001245/****************************************************************************
drhff135ae2015-12-30 01:07:02 +00001246** Scalar SQL function implementations
drh987eb1f2015-08-17 15:17:37 +00001247****************************************************************************/
1248
1249/*
drh2ad96f52016-06-17 13:01:51 +00001250** Implementation of the json_QUOTE(VALUE) function. Return a JSON value
1251** corresponding to the SQL value input. Mostly this means putting
1252** double-quotes around strings and returning the unquoted string "null"
1253** when given a NULL input.
1254*/
1255static void jsonQuoteFunc(
1256 sqlite3_context *ctx,
1257 int argc,
1258 sqlite3_value **argv
1259){
1260 JsonString jx;
drhb0df5402016-08-01 17:06:44 +00001261 UNUSED_PARAM(argc);
drh2ad96f52016-06-17 13:01:51 +00001262
1263 jsonInit(&jx, ctx);
1264 jsonAppendValue(&jx, argv[0]);
1265 jsonResult(&jx);
1266 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1267}
1268
1269/*
drh987eb1f2015-08-17 15:17:37 +00001270** Implementation of the json_array(VALUE,...) function. Return a JSON
1271** array that contains all values given in arguments. Or if any argument
1272** is a BLOB, throw an error.
1273*/
1274static void jsonArrayFunc(
drhbc8f0922015-08-22 19:39:04 +00001275 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001276 int argc,
1277 sqlite3_value **argv
1278){
1279 int i;
drh505ad2c2015-08-21 17:33:11 +00001280 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001281
drhbc8f0922015-08-22 19:39:04 +00001282 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001283 jsonAppendChar(&jx, '[');
drh987eb1f2015-08-17 15:17:37 +00001284 for(i=0; i<argc; i++){
drhd0960592015-08-17 21:22:32 +00001285 jsonAppendSeparator(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001286 jsonAppendValue(&jx, argv[i]);
drh987eb1f2015-08-17 15:17:37 +00001287 }
drhd0960592015-08-17 21:22:32 +00001288 jsonAppendChar(&jx, ']');
drh987eb1f2015-08-17 15:17:37 +00001289 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001290 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001291}
1292
1293
1294/*
1295** json_array_length(JSON)
1296** json_array_length(JSON, PATH)
1297**
1298** Return the number of elements in the top-level JSON array.
1299** Return 0 if the input is not a well-formed JSON array.
1300*/
1301static void jsonArrayLengthFunc(
drhbc8f0922015-08-22 19:39:04 +00001302 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001303 int argc,
1304 sqlite3_value **argv
1305){
1306 JsonParse x; /* The parse */
1307 sqlite3_int64 n = 0;
1308 u32 i;
drha8f39a92015-09-21 22:53:16 +00001309 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001310
drhf2df7e72015-08-28 20:07:40 +00001311 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001312 assert( x.nNode );
1313 if( argc==2 ){
1314 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
1315 pNode = jsonLookup(&x, zPath, 0, ctx);
1316 }else{
1317 pNode = x.aNode;
1318 }
1319 if( pNode==0 ){
1320 x.nErr = 1;
1321 }else if( pNode->eType==JSON_ARRAY ){
1322 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
1323 for(i=1; i<=pNode->n; n++){
1324 i += jsonNodeSize(&pNode[i]);
drh987eb1f2015-08-17 15:17:37 +00001325 }
drh987eb1f2015-08-17 15:17:37 +00001326 }
drha7714022015-08-29 00:54:49 +00001327 if( x.nErr==0 ) sqlite3_result_int64(ctx, n);
drhf6ec8d42015-08-28 03:48:04 +00001328 jsonParseReset(&x);
1329}
1330
1331/*
drh3ad93bb2015-08-29 19:41:45 +00001332** json_extract(JSON, PATH, ...)
drh987eb1f2015-08-17 15:17:37 +00001333**
drh3ad93bb2015-08-29 19:41:45 +00001334** Return the element described by PATH. Return NULL if there is no
1335** PATH element. If there are multiple PATHs, then return a JSON array
1336** with the result from each path. Throw an error if the JSON or any PATH
1337** is malformed.
drh987eb1f2015-08-17 15:17:37 +00001338*/
1339static void jsonExtractFunc(
drhbc8f0922015-08-22 19:39:04 +00001340 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001341 int argc,
1342 sqlite3_value **argv
1343){
1344 JsonParse x; /* The parse */
1345 JsonNode *pNode;
1346 const char *zPath;
drh3ad93bb2015-08-29 19:41:45 +00001347 JsonString jx;
1348 int i;
1349
1350 if( argc<2 ) return;
drhbc8f0922015-08-22 19:39:04 +00001351 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh3ad93bb2015-08-29 19:41:45 +00001352 jsonInit(&jx, ctx);
1353 jsonAppendChar(&jx, '[');
1354 for(i=1; i<argc; i++){
1355 zPath = (const char*)sqlite3_value_text(argv[i]);
drhf5ddb9c2015-09-11 00:06:41 +00001356 pNode = jsonLookup(&x, zPath, 0, ctx);
drh3ad93bb2015-08-29 19:41:45 +00001357 if( x.nErr ) break;
1358 if( argc>2 ){
1359 jsonAppendSeparator(&jx);
1360 if( pNode ){
1361 jsonRenderNode(pNode, &jx, 0);
1362 }else{
1363 jsonAppendRaw(&jx, "null", 4);
1364 }
1365 }else if( pNode ){
1366 jsonReturn(pNode, ctx, 0);
1367 }
drh987eb1f2015-08-17 15:17:37 +00001368 }
drh3ad93bb2015-08-29 19:41:45 +00001369 if( argc>2 && i==argc ){
1370 jsonAppendChar(&jx, ']');
1371 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001372 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh3ad93bb2015-08-29 19:41:45 +00001373 }
1374 jsonReset(&jx);
drh505ad2c2015-08-21 17:33:11 +00001375 jsonParseReset(&x);
drh987eb1f2015-08-17 15:17:37 +00001376}
1377
drh633647a2017-03-22 21:24:31 +00001378/* This is the RFC 7396 MergePatch algorithm.
1379*/
1380static JsonNode *jsonMergePatch(
1381 JsonParse *pParse, /* The JSON parser that contains the TARGET */
1382 int iTarget, /* Node of the TARGET in pParse */
1383 JsonNode *pPatch /* The PATCH */
1384){
drh0002d242017-03-23 00:46:15 +00001385 u32 i, j;
1386 u32 iRoot;
drh633647a2017-03-22 21:24:31 +00001387 JsonNode *pTarget;
1388 if( pPatch->eType!=JSON_OBJECT ){
1389 return pPatch;
1390 }
1391 assert( iTarget>=0 && iTarget<pParse->nNode );
1392 pTarget = &pParse->aNode[iTarget];
1393 assert( (pPatch->jnFlags & JNODE_APPEND)==0 );
1394 if( pTarget->eType!=JSON_OBJECT ){
drh29c99692017-03-24 12:35:17 +00001395 jsonRemoveAllNulls(pPatch);
drh633647a2017-03-22 21:24:31 +00001396 return pPatch;
1397 }
drhbb7aa2d2017-03-23 00:13:52 +00001398 iRoot = iTarget;
drh633647a2017-03-22 21:24:31 +00001399 for(i=1; i<pPatch->n; i += jsonNodeSize(&pPatch[i+1])+1){
drhba7cce32017-03-24 19:45:05 +00001400 u32 nKey;
drh633647a2017-03-22 21:24:31 +00001401 const char *zKey;
1402 assert( pPatch[i].eType==JSON_STRING );
1403 assert( pPatch[i].jnFlags & JNODE_LABEL );
1404 nKey = pPatch[i].n;
1405 zKey = pPatch[i].u.zJContent;
drhbb7aa2d2017-03-23 00:13:52 +00001406 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
drh633647a2017-03-22 21:24:31 +00001407 for(j=1; j<pTarget->n; j += jsonNodeSize(&pTarget[j+1])+1 ){
1408 assert( pTarget[j].eType==JSON_STRING );
1409 assert( pTarget[j].jnFlags & JNODE_LABEL );
drhbb7aa2d2017-03-23 00:13:52 +00001410 assert( (pPatch[i].jnFlags & JNODE_RAW)==0 );
drh1fe162f2017-03-23 12:56:44 +00001411 if( pTarget[j].n==nKey && strncmp(pTarget[j].u.zJContent,zKey,nKey)==0 ){
1412 if( pTarget[j+1].jnFlags & (JNODE_REMOVE|JNODE_PATCH) ) break;
drh633647a2017-03-22 21:24:31 +00001413 if( pPatch[i+1].eType==JSON_NULL ){
1414 pTarget[j+1].jnFlags |= JNODE_REMOVE;
1415 }else{
drhbb7aa2d2017-03-23 00:13:52 +00001416 JsonNode *pNew = jsonMergePatch(pParse, iTarget+j+1, &pPatch[i+1]);
drh633647a2017-03-22 21:24:31 +00001417 if( pNew==0 ) return 0;
1418 pTarget = &pParse->aNode[iTarget];
1419 if( pNew!=&pTarget[j+1] ){
1420 pTarget[j+1].u.pPatch = pNew;
1421 pTarget[j+1].jnFlags |= JNODE_PATCH;
1422 }
1423 }
1424 break;
1425 }
1426 }
drhbb7aa2d2017-03-23 00:13:52 +00001427 if( j>=pTarget->n && pPatch[i+1].eType!=JSON_NULL ){
drh633647a2017-03-22 21:24:31 +00001428 int iStart, iPatch;
1429 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
1430 jsonParseAddNode(pParse, JSON_STRING, nKey, zKey);
1431 iPatch = jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
1432 if( pParse->oom ) return 0;
drh29c99692017-03-24 12:35:17 +00001433 jsonRemoveAllNulls(pPatch);
drh633647a2017-03-22 21:24:31 +00001434 pTarget = &pParse->aNode[iTarget];
drhbb7aa2d2017-03-23 00:13:52 +00001435 pParse->aNode[iRoot].jnFlags |= JNODE_APPEND;
1436 pParse->aNode[iRoot].u.iAppend = iStart - iRoot;
1437 iRoot = iStart;
drh633647a2017-03-22 21:24:31 +00001438 pParse->aNode[iPatch].jnFlags |= JNODE_PATCH;
1439 pParse->aNode[iPatch].u.pPatch = &pPatch[i+1];
1440 }
1441 }
1442 return pTarget;
1443}
1444
1445/*
1446** Implementation of the json_mergepatch(JSON1,JSON2) function. Return a JSON
1447** object that is the result of running the RFC 7396 MergePatch() algorithm
1448** on the two arguments.
1449*/
drh37f03df2017-03-23 20:33:49 +00001450static void jsonPatchFunc(
drh633647a2017-03-22 21:24:31 +00001451 sqlite3_context *ctx,
1452 int argc,
1453 sqlite3_value **argv
1454){
1455 JsonParse x; /* The JSON that is being patched */
1456 JsonParse y; /* The patch */
drhbb7aa2d2017-03-23 00:13:52 +00001457 JsonNode *pResult; /* The result of the merge */
drh633647a2017-03-22 21:24:31 +00001458
drh2fb79e92017-03-25 12:08:11 +00001459 UNUSED_PARAM(argc);
drh633647a2017-03-22 21:24:31 +00001460 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
1461 if( jsonParse(&y, ctx, (const char*)sqlite3_value_text(argv[1])) ){
1462 jsonParseReset(&x);
1463 return;
1464 }
drhbb7aa2d2017-03-23 00:13:52 +00001465 pResult = jsonMergePatch(&x, 0, y.aNode);
1466 assert( pResult!=0 || x.oom );
1467 if( pResult ){
1468 jsonReturnJson(pResult, ctx, 0);
1469 }else{
1470 sqlite3_result_error_nomem(ctx);
1471 }
drh633647a2017-03-22 21:24:31 +00001472 jsonParseReset(&x);
1473 jsonParseReset(&y);
1474}
1475
1476
drh987eb1f2015-08-17 15:17:37 +00001477/*
1478** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
1479** object that contains all name/value given in arguments. Or if any name
1480** is not a string or if any value is a BLOB, throw an error.
1481*/
1482static void jsonObjectFunc(
drhbc8f0922015-08-22 19:39:04 +00001483 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001484 int argc,
1485 sqlite3_value **argv
1486){
1487 int i;
drh505ad2c2015-08-21 17:33:11 +00001488 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001489 const char *z;
1490 u32 n;
1491
1492 if( argc&1 ){
drhbc8f0922015-08-22 19:39:04 +00001493 sqlite3_result_error(ctx, "json_object() requires an even number "
drh987eb1f2015-08-17 15:17:37 +00001494 "of arguments", -1);
1495 return;
1496 }
drhbc8f0922015-08-22 19:39:04 +00001497 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001498 jsonAppendChar(&jx, '{');
drh987eb1f2015-08-17 15:17:37 +00001499 for(i=0; i<argc; i+=2){
drh987eb1f2015-08-17 15:17:37 +00001500 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
drhbc8f0922015-08-22 19:39:04 +00001501 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
drhdc384952015-09-19 18:54:39 +00001502 jsonReset(&jx);
drh987eb1f2015-08-17 15:17:37 +00001503 return;
1504 }
drhd0960592015-08-17 21:22:32 +00001505 jsonAppendSeparator(&jx);
drh987eb1f2015-08-17 15:17:37 +00001506 z = (const char*)sqlite3_value_text(argv[i]);
1507 n = (u32)sqlite3_value_bytes(argv[i]);
1508 jsonAppendString(&jx, z, n);
drhd0960592015-08-17 21:22:32 +00001509 jsonAppendChar(&jx, ':');
drhf5ddb9c2015-09-11 00:06:41 +00001510 jsonAppendValue(&jx, argv[i+1]);
drh987eb1f2015-08-17 15:17:37 +00001511 }
drhd0960592015-08-17 21:22:32 +00001512 jsonAppendChar(&jx, '}');
drh987eb1f2015-08-17 15:17:37 +00001513 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001514 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001515}
1516
1517
1518/*
drh301eecc2015-08-17 20:14:19 +00001519** json_remove(JSON, PATH, ...)
1520**
drh3ad93bb2015-08-29 19:41:45 +00001521** Remove the named elements from JSON and return the result. malformed
1522** JSON or PATH arguments result in an error.
drh301eecc2015-08-17 20:14:19 +00001523*/
1524static void jsonRemoveFunc(
drhbc8f0922015-08-22 19:39:04 +00001525 sqlite3_context *ctx,
drh301eecc2015-08-17 20:14:19 +00001526 int argc,
1527 sqlite3_value **argv
1528){
1529 JsonParse x; /* The parse */
1530 JsonNode *pNode;
1531 const char *zPath;
1532 u32 i;
1533
1534 if( argc<1 ) return;
drhbc8f0922015-08-22 19:39:04 +00001535 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001536 assert( x.nNode );
1537 for(i=1; i<(u32)argc; i++){
1538 zPath = (const char*)sqlite3_value_text(argv[i]);
1539 if( zPath==0 ) goto remove_done;
1540 pNode = jsonLookup(&x, zPath, 0, ctx);
1541 if( x.nErr ) goto remove_done;
1542 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
1543 }
1544 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
1545 jsonReturnJson(x.aNode, ctx, 0);
drhd0960592015-08-17 21:22:32 +00001546 }
drha7714022015-08-29 00:54:49 +00001547remove_done:
drh505ad2c2015-08-21 17:33:11 +00001548 jsonParseReset(&x);
drhd0960592015-08-17 21:22:32 +00001549}
1550
1551/*
1552** json_replace(JSON, PATH, VALUE, ...)
1553**
1554** Replace the value at PATH with VALUE. If PATH does not already exist,
drh3ad93bb2015-08-29 19:41:45 +00001555** this routine is a no-op. If JSON or PATH is malformed, throw an error.
drhd0960592015-08-17 21:22:32 +00001556*/
1557static void jsonReplaceFunc(
drhbc8f0922015-08-22 19:39:04 +00001558 sqlite3_context *ctx,
drhd0960592015-08-17 21:22:32 +00001559 int argc,
1560 sqlite3_value **argv
1561){
1562 JsonParse x; /* The parse */
1563 JsonNode *pNode;
1564 const char *zPath;
1565 u32 i;
1566
1567 if( argc<1 ) return;
1568 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001569 jsonWrongNumArgs(ctx, "replace");
drhd0960592015-08-17 21:22:32 +00001570 return;
1571 }
drhbc8f0922015-08-22 19:39:04 +00001572 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001573 assert( x.nNode );
1574 for(i=1; i<(u32)argc; i+=2){
1575 zPath = (const char*)sqlite3_value_text(argv[i]);
1576 pNode = jsonLookup(&x, zPath, 0, ctx);
1577 if( x.nErr ) goto replace_err;
1578 if( pNode ){
1579 pNode->jnFlags |= (u8)JNODE_REPLACE;
drh633647a2017-03-22 21:24:31 +00001580 pNode->u.iReplace = i + 1;
drhd0960592015-08-17 21:22:32 +00001581 }
drha8f39a92015-09-21 22:53:16 +00001582 }
1583 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
drh633647a2017-03-22 21:24:31 +00001584 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
drha8f39a92015-09-21 22:53:16 +00001585 }else{
1586 jsonReturnJson(x.aNode, ctx, argv);
drh301eecc2015-08-17 20:14:19 +00001587 }
drha7714022015-08-29 00:54:49 +00001588replace_err:
drh505ad2c2015-08-21 17:33:11 +00001589 jsonParseReset(&x);
drh301eecc2015-08-17 20:14:19 +00001590}
drh505ad2c2015-08-21 17:33:11 +00001591
drh52216ad2015-08-18 02:28:03 +00001592/*
1593** json_set(JSON, PATH, VALUE, ...)
1594**
1595** Set the value at PATH to VALUE. Create the PATH if it does not already
1596** exist. Overwrite existing values that do exist.
drh3ad93bb2015-08-29 19:41:45 +00001597** If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001598**
1599** json_insert(JSON, PATH, VALUE, ...)
1600**
1601** Create PATH and initialize it to VALUE. If PATH already exists, this
drh3ad93bb2015-08-29 19:41:45 +00001602** routine is a no-op. If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001603*/
1604static void jsonSetFunc(
drhbc8f0922015-08-22 19:39:04 +00001605 sqlite3_context *ctx,
drh52216ad2015-08-18 02:28:03 +00001606 int argc,
1607 sqlite3_value **argv
1608){
1609 JsonParse x; /* The parse */
1610 JsonNode *pNode;
1611 const char *zPath;
1612 u32 i;
1613 int bApnd;
drhbc8f0922015-08-22 19:39:04 +00001614 int bIsSet = *(int*)sqlite3_user_data(ctx);
drh52216ad2015-08-18 02:28:03 +00001615
1616 if( argc<1 ) return;
1617 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001618 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
drh52216ad2015-08-18 02:28:03 +00001619 return;
1620 }
drhbc8f0922015-08-22 19:39:04 +00001621 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001622 assert( x.nNode );
1623 for(i=1; i<(u32)argc; i+=2){
1624 zPath = (const char*)sqlite3_value_text(argv[i]);
1625 bApnd = 0;
1626 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
1627 if( x.oom ){
1628 sqlite3_result_error_nomem(ctx);
1629 goto jsonSetDone;
1630 }else if( x.nErr ){
1631 goto jsonSetDone;
1632 }else if( pNode && (bApnd || bIsSet) ){
1633 pNode->jnFlags |= (u8)JNODE_REPLACE;
drh633647a2017-03-22 21:24:31 +00001634 pNode->u.iReplace = i + 1;
drh52216ad2015-08-18 02:28:03 +00001635 }
drha8f39a92015-09-21 22:53:16 +00001636 }
1637 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
drh633647a2017-03-22 21:24:31 +00001638 sqlite3_result_value(ctx, argv[x.aNode[0].u.iReplace]);
drha8f39a92015-09-21 22:53:16 +00001639 }else{
1640 jsonReturnJson(x.aNode, ctx, argv);
drh52216ad2015-08-18 02:28:03 +00001641 }
drhbc8f0922015-08-22 19:39:04 +00001642jsonSetDone:
drh505ad2c2015-08-21 17:33:11 +00001643 jsonParseReset(&x);
drh52216ad2015-08-18 02:28:03 +00001644}
drh301eecc2015-08-17 20:14:19 +00001645
1646/*
drh987eb1f2015-08-17 15:17:37 +00001647** json_type(JSON)
1648** json_type(JSON, PATH)
1649**
drh3ad93bb2015-08-29 19:41:45 +00001650** Return the top-level "type" of a JSON string. Throw an error if
1651** either the JSON or PATH inputs are not well-formed.
drh987eb1f2015-08-17 15:17:37 +00001652*/
1653static void jsonTypeFunc(
drhbc8f0922015-08-22 19:39:04 +00001654 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001655 int argc,
1656 sqlite3_value **argv
1657){
1658 JsonParse x; /* The parse */
1659 const char *zPath;
drha8f39a92015-09-21 22:53:16 +00001660 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001661
drhbc8f0922015-08-22 19:39:04 +00001662 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001663 assert( x.nNode );
1664 if( argc==2 ){
1665 zPath = (const char*)sqlite3_value_text(argv[1]);
1666 pNode = jsonLookup(&x, zPath, 0, ctx);
1667 }else{
1668 pNode = x.aNode;
1669 }
1670 if( pNode ){
1671 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
drh987eb1f2015-08-17 15:17:37 +00001672 }
drh505ad2c2015-08-21 17:33:11 +00001673 jsonParseReset(&x);
drh987eb1f2015-08-17 15:17:37 +00001674}
drh5634cc02015-08-17 11:28:03 +00001675
drhbc8f0922015-08-22 19:39:04 +00001676/*
1677** json_valid(JSON)
1678**
drh3ad93bb2015-08-29 19:41:45 +00001679** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
1680** Return 0 otherwise.
drhbc8f0922015-08-22 19:39:04 +00001681*/
1682static void jsonValidFunc(
1683 sqlite3_context *ctx,
1684 int argc,
1685 sqlite3_value **argv
1686){
1687 JsonParse x; /* The parse */
1688 int rc = 0;
1689
mistachkin16a93122015-09-11 18:05:01 +00001690 UNUSED_PARAM(argc);
drha8f39a92015-09-21 22:53:16 +00001691 if( jsonParse(&x, 0, (const char*)sqlite3_value_text(argv[0]))==0 ){
drhbc8f0922015-08-22 19:39:04 +00001692 rc = 1;
1693 }
1694 jsonParseReset(&x);
1695 sqlite3_result_int(ctx, rc);
1696}
1697
drhff135ae2015-12-30 01:07:02 +00001698
1699/****************************************************************************
1700** Aggregate SQL function implementations
1701****************************************************************************/
1702/*
1703** json_group_array(VALUE)
1704**
1705** Return a JSON array composed of all values in the aggregate.
1706*/
1707static void jsonArrayStep(
1708 sqlite3_context *ctx,
1709 int argc,
1710 sqlite3_value **argv
1711){
1712 JsonString *pStr;
drhdf3a9072016-02-11 15:37:18 +00001713 UNUSED_PARAM(argc);
drhff135ae2015-12-30 01:07:02 +00001714 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
1715 if( pStr ){
1716 if( pStr->zBuf==0 ){
1717 jsonInit(pStr, ctx);
1718 jsonAppendChar(pStr, '[');
1719 }else{
1720 jsonAppendChar(pStr, ',');
1721 pStr->pCtx = ctx;
1722 }
1723 jsonAppendValue(pStr, argv[0]);
1724 }
1725}
1726static void jsonArrayFinal(sqlite3_context *ctx){
1727 JsonString *pStr;
1728 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
1729 if( pStr ){
1730 pStr->pCtx = ctx;
1731 jsonAppendChar(pStr, ']');
1732 if( pStr->bErr ){
drh4a642b62016-02-05 01:55:27 +00001733 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00001734 assert( pStr->bStatic );
drhff135ae2015-12-30 01:07:02 +00001735 }else{
1736 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
1737 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
1738 pStr->bStatic = 1;
1739 }
1740 }else{
1741 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
1742 }
1743 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1744}
1745
1746/*
1747** json_group_obj(NAME,VALUE)
1748**
1749** Return a JSON object composed of all names and values in the aggregate.
1750*/
1751static void jsonObjectStep(
1752 sqlite3_context *ctx,
1753 int argc,
1754 sqlite3_value **argv
1755){
1756 JsonString *pStr;
1757 const char *z;
1758 u32 n;
drhdf3a9072016-02-11 15:37:18 +00001759 UNUSED_PARAM(argc);
drhff135ae2015-12-30 01:07:02 +00001760 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
1761 if( pStr ){
1762 if( pStr->zBuf==0 ){
1763 jsonInit(pStr, ctx);
1764 jsonAppendChar(pStr, '{');
1765 }else{
1766 jsonAppendChar(pStr, ',');
1767 pStr->pCtx = ctx;
1768 }
1769 z = (const char*)sqlite3_value_text(argv[0]);
1770 n = (u32)sqlite3_value_bytes(argv[0]);
1771 jsonAppendString(pStr, z, n);
1772 jsonAppendChar(pStr, ':');
1773 jsonAppendValue(pStr, argv[1]);
1774 }
1775}
1776static void jsonObjectFinal(sqlite3_context *ctx){
1777 JsonString *pStr;
1778 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
1779 if( pStr ){
1780 jsonAppendChar(pStr, '}');
1781 if( pStr->bErr ){
drh6850a632016-11-07 18:18:08 +00001782 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00001783 assert( pStr->bStatic );
drhff135ae2015-12-30 01:07:02 +00001784 }else{
1785 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
1786 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
1787 pStr->bStatic = 1;
1788 }
1789 }else{
1790 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
1791 }
1792 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1793}
1794
1795
drhd2975922015-08-29 17:22:33 +00001796#ifndef SQLITE_OMIT_VIRTUALTABLE
drhcb6c6c62015-08-19 22:47:17 +00001797/****************************************************************************
1798** The json_each virtual table
1799****************************************************************************/
1800typedef struct JsonEachCursor JsonEachCursor;
1801struct JsonEachCursor {
1802 sqlite3_vtab_cursor base; /* Base class - must be first */
drh505ad2c2015-08-21 17:33:11 +00001803 u32 iRowid; /* The rowid */
drh852944e2015-09-10 03:29:11 +00001804 u32 iBegin; /* The first node of the scan */
drh505ad2c2015-08-21 17:33:11 +00001805 u32 i; /* Index in sParse.aNode[] of current row */
1806 u32 iEnd; /* EOF when i equals or exceeds this value */
1807 u8 eType; /* Type of top-level element */
1808 u8 bRecursive; /* True for json_tree(). False for json_each() */
1809 char *zJson; /* Input JSON */
drh383de692015-09-10 17:20:57 +00001810 char *zRoot; /* Path by which to filter zJson */
drh505ad2c2015-08-21 17:33:11 +00001811 JsonParse sParse; /* Parse of the input JSON */
drhcb6c6c62015-08-19 22:47:17 +00001812};
1813
1814/* Constructor for the json_each virtual table */
1815static int jsonEachConnect(
1816 sqlite3 *db,
1817 void *pAux,
1818 int argc, const char *const*argv,
1819 sqlite3_vtab **ppVtab,
1820 char **pzErr
1821){
1822 sqlite3_vtab *pNew;
drh505ad2c2015-08-21 17:33:11 +00001823 int rc;
drhcb6c6c62015-08-19 22:47:17 +00001824
1825/* Column numbers */
drh4af352d2015-08-21 20:02:48 +00001826#define JEACH_KEY 0
1827#define JEACH_VALUE 1
1828#define JEACH_TYPE 2
1829#define JEACH_ATOM 3
1830#define JEACH_ID 4
1831#define JEACH_PARENT 5
1832#define JEACH_FULLKEY 6
drh383de692015-09-10 17:20:57 +00001833#define JEACH_PATH 7
1834#define JEACH_JSON 8
1835#define JEACH_ROOT 9
drhcb6c6c62015-08-19 22:47:17 +00001836
drh6fd5c1e2015-08-21 20:37:12 +00001837 UNUSED_PARAM(pzErr);
1838 UNUSED_PARAM(argv);
1839 UNUSED_PARAM(argc);
1840 UNUSED_PARAM(pAux);
drh505ad2c2015-08-21 17:33:11 +00001841 rc = sqlite3_declare_vtab(db,
drh383de692015-09-10 17:20:57 +00001842 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
1843 "json HIDDEN,root HIDDEN)");
drh505ad2c2015-08-21 17:33:11 +00001844 if( rc==SQLITE_OK ){
1845 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
1846 if( pNew==0 ) return SQLITE_NOMEM;
1847 memset(pNew, 0, sizeof(*pNew));
1848 }
1849 return rc;
drhcb6c6c62015-08-19 22:47:17 +00001850}
1851
1852/* destructor for json_each virtual table */
1853static int jsonEachDisconnect(sqlite3_vtab *pVtab){
1854 sqlite3_free(pVtab);
1855 return SQLITE_OK;
1856}
1857
drh505ad2c2015-08-21 17:33:11 +00001858/* constructor for a JsonEachCursor object for json_each(). */
1859static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
drhcb6c6c62015-08-19 22:47:17 +00001860 JsonEachCursor *pCur;
drh6fd5c1e2015-08-21 20:37:12 +00001861
1862 UNUSED_PARAM(p);
drhcb6c6c62015-08-19 22:47:17 +00001863 pCur = sqlite3_malloc( sizeof(*pCur) );
1864 if( pCur==0 ) return SQLITE_NOMEM;
1865 memset(pCur, 0, sizeof(*pCur));
1866 *ppCursor = &pCur->base;
1867 return SQLITE_OK;
1868}
1869
drh505ad2c2015-08-21 17:33:11 +00001870/* constructor for a JsonEachCursor object for json_tree(). */
1871static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
1872 int rc = jsonEachOpenEach(p, ppCursor);
1873 if( rc==SQLITE_OK ){
1874 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
1875 pCur->bRecursive = 1;
1876 }
1877 return rc;
1878}
1879
drhcb6c6c62015-08-19 22:47:17 +00001880/* Reset a JsonEachCursor back to its original state. Free any memory
1881** held. */
1882static void jsonEachCursorReset(JsonEachCursor *p){
1883 sqlite3_free(p->zJson);
drh383de692015-09-10 17:20:57 +00001884 sqlite3_free(p->zRoot);
drh505ad2c2015-08-21 17:33:11 +00001885 jsonParseReset(&p->sParse);
drhcb6c6c62015-08-19 22:47:17 +00001886 p->iRowid = 0;
1887 p->i = 0;
1888 p->iEnd = 0;
1889 p->eType = 0;
drhcb6c6c62015-08-19 22:47:17 +00001890 p->zJson = 0;
drh383de692015-09-10 17:20:57 +00001891 p->zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00001892}
1893
1894/* Destructor for a jsonEachCursor object */
1895static int jsonEachClose(sqlite3_vtab_cursor *cur){
1896 JsonEachCursor *p = (JsonEachCursor*)cur;
1897 jsonEachCursorReset(p);
1898 sqlite3_free(cur);
1899 return SQLITE_OK;
1900}
1901
1902/* Return TRUE if the jsonEachCursor object has been advanced off the end
1903** of the JSON object */
1904static int jsonEachEof(sqlite3_vtab_cursor *cur){
1905 JsonEachCursor *p = (JsonEachCursor*)cur;
1906 return p->i >= p->iEnd;
1907}
1908
drh505ad2c2015-08-21 17:33:11 +00001909/* Advance the cursor to the next element for json_tree() */
drh4af352d2015-08-21 20:02:48 +00001910static int jsonEachNext(sqlite3_vtab_cursor *cur){
drh505ad2c2015-08-21 17:33:11 +00001911 JsonEachCursor *p = (JsonEachCursor*)cur;
drh4af352d2015-08-21 20:02:48 +00001912 if( p->bRecursive ){
drh852944e2015-09-10 03:29:11 +00001913 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
1914 p->i++;
drh4af352d2015-08-21 20:02:48 +00001915 p->iRowid++;
drh852944e2015-09-10 03:29:11 +00001916 if( p->i<p->iEnd ){
drh8784eca2015-08-23 02:42:30 +00001917 u32 iUp = p->sParse.aUp[p->i];
1918 JsonNode *pUp = &p->sParse.aNode[iUp];
drh4af352d2015-08-21 20:02:48 +00001919 p->eType = pUp->eType;
drh8784eca2015-08-23 02:42:30 +00001920 if( pUp->eType==JSON_ARRAY ){
1921 if( iUp==p->i-1 ){
1922 pUp->u.iKey = 0;
1923 }else{
1924 pUp->u.iKey++;
1925 }
drh4af352d2015-08-21 20:02:48 +00001926 }
1927 }
drh505ad2c2015-08-21 17:33:11 +00001928 }else{
drh4af352d2015-08-21 20:02:48 +00001929 switch( p->eType ){
1930 case JSON_ARRAY: {
1931 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
1932 p->iRowid++;
1933 break;
1934 }
1935 case JSON_OBJECT: {
1936 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
1937 p->iRowid++;
1938 break;
1939 }
1940 default: {
1941 p->i = p->iEnd;
1942 break;
1943 }
drh505ad2c2015-08-21 17:33:11 +00001944 }
1945 }
1946 return SQLITE_OK;
1947}
1948
drh4af352d2015-08-21 20:02:48 +00001949/* Append the name of the path for element i to pStr
1950*/
1951static void jsonEachComputePath(
1952 JsonEachCursor *p, /* The cursor */
1953 JsonString *pStr, /* Write the path here */
1954 u32 i /* Path to this element */
1955){
1956 JsonNode *pNode, *pUp;
1957 u32 iUp;
1958 if( i==0 ){
1959 jsonAppendChar(pStr, '$');
1960 return;
drhcb6c6c62015-08-19 22:47:17 +00001961 }
drh4af352d2015-08-21 20:02:48 +00001962 iUp = p->sParse.aUp[i];
1963 jsonEachComputePath(p, pStr, iUp);
1964 pNode = &p->sParse.aNode[i];
1965 pUp = &p->sParse.aNode[iUp];
1966 if( pUp->eType==JSON_ARRAY ){
1967 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
1968 }else{
1969 assert( pUp->eType==JSON_OBJECT );
drh852944e2015-09-10 03:29:11 +00001970 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
drh4af352d2015-08-21 20:02:48 +00001971 assert( pNode->eType==JSON_STRING );
drh852944e2015-09-10 03:29:11 +00001972 assert( pNode->jnFlags & JNODE_LABEL );
drh4af352d2015-08-21 20:02:48 +00001973 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
1974 }
drhcb6c6c62015-08-19 22:47:17 +00001975}
1976
1977/* Return the value of a column */
1978static int jsonEachColumn(
1979 sqlite3_vtab_cursor *cur, /* The cursor */
1980 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
1981 int i /* Which column to return */
1982){
1983 JsonEachCursor *p = (JsonEachCursor*)cur;
drh505ad2c2015-08-21 17:33:11 +00001984 JsonNode *pThis = &p->sParse.aNode[p->i];
drhcb6c6c62015-08-19 22:47:17 +00001985 switch( i ){
1986 case JEACH_KEY: {
drh8784eca2015-08-23 02:42:30 +00001987 if( p->i==0 ) break;
drhcb6c6c62015-08-19 22:47:17 +00001988 if( p->eType==JSON_OBJECT ){
drh505ad2c2015-08-21 17:33:11 +00001989 jsonReturn(pThis, ctx, 0);
1990 }else if( p->eType==JSON_ARRAY ){
1991 u32 iKey;
1992 if( p->bRecursive ){
1993 if( p->iRowid==0 ) break;
drh8784eca2015-08-23 02:42:30 +00001994 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
drh505ad2c2015-08-21 17:33:11 +00001995 }else{
1996 iKey = p->iRowid;
1997 }
drh6fd5c1e2015-08-21 20:37:12 +00001998 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
drhcb6c6c62015-08-19 22:47:17 +00001999 }
2000 break;
2001 }
2002 case JEACH_VALUE: {
drh852944e2015-09-10 03:29:11 +00002003 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002004 jsonReturn(pThis, ctx, 0);
2005 break;
2006 }
2007 case JEACH_TYPE: {
drh852944e2015-09-10 03:29:11 +00002008 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002009 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
2010 break;
2011 }
2012 case JEACH_ATOM: {
drh852944e2015-09-10 03:29:11 +00002013 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00002014 if( pThis->eType>=JSON_ARRAY ) break;
2015 jsonReturn(pThis, ctx, 0);
2016 break;
2017 }
2018 case JEACH_ID: {
drh852944e2015-09-10 03:29:11 +00002019 sqlite3_result_int64(ctx,
2020 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
drh505ad2c2015-08-21 17:33:11 +00002021 break;
2022 }
2023 case JEACH_PARENT: {
drh852944e2015-09-10 03:29:11 +00002024 if( p->i>p->iBegin && p->bRecursive ){
drh6fd5c1e2015-08-21 20:37:12 +00002025 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
drhcb6c6c62015-08-19 22:47:17 +00002026 }
2027 break;
2028 }
drh4af352d2015-08-21 20:02:48 +00002029 case JEACH_FULLKEY: {
2030 JsonString x;
2031 jsonInit(&x, ctx);
2032 if( p->bRecursive ){
2033 jsonEachComputePath(p, &x, p->i);
2034 }else{
drh383de692015-09-10 17:20:57 +00002035 if( p->zRoot ){
2036 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
drh4af352d2015-08-21 20:02:48 +00002037 }else{
2038 jsonAppendChar(&x, '$');
2039 }
2040 if( p->eType==JSON_ARRAY ){
2041 jsonPrintf(30, &x, "[%d]", p->iRowid);
2042 }else{
2043 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
2044 }
2045 }
2046 jsonResult(&x);
2047 break;
2048 }
drhcb6c6c62015-08-19 22:47:17 +00002049 case JEACH_PATH: {
drh383de692015-09-10 17:20:57 +00002050 if( p->bRecursive ){
2051 JsonString x;
2052 jsonInit(&x, ctx);
2053 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
2054 jsonResult(&x);
2055 break;
drh4af352d2015-08-21 20:02:48 +00002056 }
drh383de692015-09-10 17:20:57 +00002057 /* For json_each() path and root are the same so fall through
2058 ** into the root case */
2059 }
drh6850a632016-11-07 18:18:08 +00002060 default: {
drh383de692015-09-10 17:20:57 +00002061 const char *zRoot = p->zRoot;
drh6850a632016-11-07 18:18:08 +00002062 if( zRoot==0 ) zRoot = "$";
drh383de692015-09-10 17:20:57 +00002063 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
drhcb6c6c62015-08-19 22:47:17 +00002064 break;
2065 }
drh3d1d2a92015-09-22 01:15:49 +00002066 case JEACH_JSON: {
drh505ad2c2015-08-21 17:33:11 +00002067 assert( i==JEACH_JSON );
drhcb6c6c62015-08-19 22:47:17 +00002068 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
2069 break;
2070 }
2071 }
2072 return SQLITE_OK;
2073}
2074
2075/* Return the current rowid value */
2076static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2077 JsonEachCursor *p = (JsonEachCursor*)cur;
2078 *pRowid = p->iRowid;
2079 return SQLITE_OK;
2080}
2081
2082/* The query strategy is to look for an equality constraint on the json
2083** column. Without such a constraint, the table cannot operate. idxNum is
drh383de692015-09-10 17:20:57 +00002084** 1 if the constraint is found, 3 if the constraint and zRoot are found,
drhcb6c6c62015-08-19 22:47:17 +00002085** and 0 otherwise.
2086*/
2087static int jsonEachBestIndex(
2088 sqlite3_vtab *tab,
2089 sqlite3_index_info *pIdxInfo
2090){
2091 int i;
2092 int jsonIdx = -1;
drh383de692015-09-10 17:20:57 +00002093 int rootIdx = -1;
drhcb6c6c62015-08-19 22:47:17 +00002094 const struct sqlite3_index_constraint *pConstraint;
drh6fd5c1e2015-08-21 20:37:12 +00002095
2096 UNUSED_PARAM(tab);
drhcb6c6c62015-08-19 22:47:17 +00002097 pConstraint = pIdxInfo->aConstraint;
2098 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2099 if( pConstraint->usable==0 ) continue;
2100 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2101 switch( pConstraint->iColumn ){
2102 case JEACH_JSON: jsonIdx = i; break;
drh383de692015-09-10 17:20:57 +00002103 case JEACH_ROOT: rootIdx = i; break;
drhcb6c6c62015-08-19 22:47:17 +00002104 default: /* no-op */ break;
2105 }
2106 }
2107 if( jsonIdx<0 ){
2108 pIdxInfo->idxNum = 0;
drh505ad2c2015-08-21 17:33:11 +00002109 pIdxInfo->estimatedCost = 1e99;
drhcb6c6c62015-08-19 22:47:17 +00002110 }else{
drh505ad2c2015-08-21 17:33:11 +00002111 pIdxInfo->estimatedCost = 1.0;
drhcb6c6c62015-08-19 22:47:17 +00002112 pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1;
2113 pIdxInfo->aConstraintUsage[jsonIdx].omit = 1;
drh383de692015-09-10 17:20:57 +00002114 if( rootIdx<0 ){
drhcb6c6c62015-08-19 22:47:17 +00002115 pIdxInfo->idxNum = 1;
2116 }else{
drh383de692015-09-10 17:20:57 +00002117 pIdxInfo->aConstraintUsage[rootIdx].argvIndex = 2;
2118 pIdxInfo->aConstraintUsage[rootIdx].omit = 1;
drhcb6c6c62015-08-19 22:47:17 +00002119 pIdxInfo->idxNum = 3;
2120 }
2121 }
2122 return SQLITE_OK;
2123}
2124
2125/* Start a search on a new JSON string */
2126static int jsonEachFilter(
2127 sqlite3_vtab_cursor *cur,
2128 int idxNum, const char *idxStr,
2129 int argc, sqlite3_value **argv
2130){
2131 JsonEachCursor *p = (JsonEachCursor*)cur;
2132 const char *z;
mistachkin16a93122015-09-11 18:05:01 +00002133 const char *zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00002134 sqlite3_int64 n;
2135
drh6fd5c1e2015-08-21 20:37:12 +00002136 UNUSED_PARAM(idxStr);
2137 UNUSED_PARAM(argc);
drhcb6c6c62015-08-19 22:47:17 +00002138 jsonEachCursorReset(p);
2139 if( idxNum==0 ) return SQLITE_OK;
2140 z = (const char*)sqlite3_value_text(argv[0]);
2141 if( z==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002142 n = sqlite3_value_bytes(argv[0]);
drh6fd5c1e2015-08-21 20:37:12 +00002143 p->zJson = sqlite3_malloc64( n+1 );
drhcb6c6c62015-08-19 22:47:17 +00002144 if( p->zJson==0 ) return SQLITE_NOMEM;
drh6fd5c1e2015-08-21 20:37:12 +00002145 memcpy(p->zJson, z, (size_t)n+1);
drha7714022015-08-29 00:54:49 +00002146 if( jsonParse(&p->sParse, 0, p->zJson) ){
2147 int rc = SQLITE_NOMEM;
2148 if( p->sParse.oom==0 ){
2149 sqlite3_free(cur->pVtab->zErrMsg);
2150 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
2151 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
2152 }
drhcb6c6c62015-08-19 22:47:17 +00002153 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002154 return rc;
2155 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
2156 jsonEachCursorReset(p);
2157 return SQLITE_NOMEM;
drhcb6c6c62015-08-19 22:47:17 +00002158 }else{
drh95677942015-09-24 01:06:37 +00002159 JsonNode *pNode = 0;
drhcb6c6c62015-08-19 22:47:17 +00002160 if( idxNum==3 ){
drha7714022015-08-29 00:54:49 +00002161 const char *zErr = 0;
drha8f39a92015-09-21 22:53:16 +00002162 zRoot = (const char*)sqlite3_value_text(argv[1]);
2163 if( zRoot==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002164 n = sqlite3_value_bytes(argv[1]);
drh383de692015-09-10 17:20:57 +00002165 p->zRoot = sqlite3_malloc64( n+1 );
2166 if( p->zRoot==0 ) return SQLITE_NOMEM;
2167 memcpy(p->zRoot, zRoot, (size_t)n+1);
drha8f39a92015-09-21 22:53:16 +00002168 if( zRoot[0]!='$' ){
2169 zErr = zRoot;
2170 }else{
2171 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
2172 }
2173 if( zErr ){
drha7714022015-08-29 00:54:49 +00002174 sqlite3_free(cur->pVtab->zErrMsg);
2175 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
drhcb6c6c62015-08-19 22:47:17 +00002176 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002177 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
2178 }else if( pNode==0 ){
drhcb6c6c62015-08-19 22:47:17 +00002179 return SQLITE_OK;
2180 }
2181 }else{
2182 pNode = p->sParse.aNode;
2183 }
drh852944e2015-09-10 03:29:11 +00002184 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
drhcb6c6c62015-08-19 22:47:17 +00002185 p->eType = pNode->eType;
2186 if( p->eType>=JSON_ARRAY ){
drh8784eca2015-08-23 02:42:30 +00002187 pNode->u.iKey = 0;
drhc3722b22015-08-23 20:44:59 +00002188 p->iEnd = p->i + pNode->n + 1;
drh852944e2015-09-10 03:29:11 +00002189 if( p->bRecursive ){
drh3d1d2a92015-09-22 01:15:49 +00002190 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
drh852944e2015-09-10 03:29:11 +00002191 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
2192 p->i--;
2193 }
2194 }else{
2195 p->i++;
2196 }
drhcb6c6c62015-08-19 22:47:17 +00002197 }else{
2198 p->iEnd = p->i+1;
2199 }
2200 }
drha8f39a92015-09-21 22:53:16 +00002201 return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002202}
2203
2204/* The methods of the json_each virtual table */
2205static sqlite3_module jsonEachModule = {
2206 0, /* iVersion */
2207 0, /* xCreate */
2208 jsonEachConnect, /* xConnect */
2209 jsonEachBestIndex, /* xBestIndex */
2210 jsonEachDisconnect, /* xDisconnect */
2211 0, /* xDestroy */
drh505ad2c2015-08-21 17:33:11 +00002212 jsonEachOpenEach, /* xOpen - open a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002213 jsonEachClose, /* xClose - close a cursor */
2214 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002215 jsonEachNext, /* xNext - advance a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002216 jsonEachEof, /* xEof - check for end of scan */
2217 jsonEachColumn, /* xColumn - read data */
2218 jsonEachRowid, /* xRowid - read data */
2219 0, /* xUpdate */
2220 0, /* xBegin */
2221 0, /* xSync */
2222 0, /* xCommit */
2223 0, /* xRollback */
2224 0, /* xFindMethod */
2225 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002226 0, /* xSavepoint */
2227 0, /* xRelease */
2228 0 /* xRollbackTo */
drhcb6c6c62015-08-19 22:47:17 +00002229};
2230
drh505ad2c2015-08-21 17:33:11 +00002231/* The methods of the json_tree virtual table. */
2232static sqlite3_module jsonTreeModule = {
2233 0, /* iVersion */
2234 0, /* xCreate */
2235 jsonEachConnect, /* xConnect */
2236 jsonEachBestIndex, /* xBestIndex */
2237 jsonEachDisconnect, /* xDisconnect */
2238 0, /* xDestroy */
2239 jsonEachOpenTree, /* xOpen - open a cursor */
2240 jsonEachClose, /* xClose - close a cursor */
2241 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002242 jsonEachNext, /* xNext - advance a cursor */
drh505ad2c2015-08-21 17:33:11 +00002243 jsonEachEof, /* xEof - check for end of scan */
2244 jsonEachColumn, /* xColumn - read data */
2245 jsonEachRowid, /* xRowid - read data */
2246 0, /* xUpdate */
2247 0, /* xBegin */
2248 0, /* xSync */
2249 0, /* xCommit */
2250 0, /* xRollback */
2251 0, /* xFindMethod */
2252 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002253 0, /* xSavepoint */
2254 0, /* xRelease */
2255 0 /* xRollbackTo */
drh505ad2c2015-08-21 17:33:11 +00002256};
drhd2975922015-08-29 17:22:33 +00002257#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh505ad2c2015-08-21 17:33:11 +00002258
2259/****************************************************************************
drh2f20e132015-09-26 17:44:59 +00002260** The following routines are the only publically visible identifiers in this
2261** file. Call the following routines in order to register the various SQL
drh505ad2c2015-08-21 17:33:11 +00002262** functions and the virtual table implemented by this file.
2263****************************************************************************/
drhcb6c6c62015-08-19 22:47:17 +00002264
drh2f20e132015-09-26 17:44:59 +00002265int sqlite3Json1Init(sqlite3 *db){
drh5fa5c102015-08-12 16:49:40 +00002266 int rc = SQLITE_OK;
drh6fd5c1e2015-08-21 20:37:12 +00002267 unsigned int i;
drh5fa5c102015-08-12 16:49:40 +00002268 static const struct {
2269 const char *zName;
2270 int nArg;
drh52216ad2015-08-18 02:28:03 +00002271 int flag;
drh5fa5c102015-08-12 16:49:40 +00002272 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
2273 } aFunc[] = {
drhf5ddb9c2015-09-11 00:06:41 +00002274 { "json", 1, 0, jsonRemoveFunc },
drh52216ad2015-08-18 02:28:03 +00002275 { "json_array", -1, 0, jsonArrayFunc },
2276 { "json_array_length", 1, 0, jsonArrayLengthFunc },
2277 { "json_array_length", 2, 0, jsonArrayLengthFunc },
drh3ad93bb2015-08-29 19:41:45 +00002278 { "json_extract", -1, 0, jsonExtractFunc },
drh52216ad2015-08-18 02:28:03 +00002279 { "json_insert", -1, 0, jsonSetFunc },
2280 { "json_object", -1, 0, jsonObjectFunc },
drh37f03df2017-03-23 20:33:49 +00002281 { "json_patch", 2, 0, jsonPatchFunc },
drh2ad96f52016-06-17 13:01:51 +00002282 { "json_quote", 1, 0, jsonQuoteFunc },
drh52216ad2015-08-18 02:28:03 +00002283 { "json_remove", -1, 0, jsonRemoveFunc },
2284 { "json_replace", -1, 0, jsonReplaceFunc },
2285 { "json_set", -1, 1, jsonSetFunc },
2286 { "json_type", 1, 0, jsonTypeFunc },
2287 { "json_type", 2, 0, jsonTypeFunc },
drhbc8f0922015-08-22 19:39:04 +00002288 { "json_valid", 1, 0, jsonValidFunc },
drh987eb1f2015-08-17 15:17:37 +00002289
drh301eecc2015-08-17 20:14:19 +00002290#if SQLITE_DEBUG
drh987eb1f2015-08-17 15:17:37 +00002291 /* DEBUG and TESTING functions */
drh52216ad2015-08-18 02:28:03 +00002292 { "json_parse", 1, 0, jsonParseFunc },
2293 { "json_test1", 1, 0, jsonTest1Func },
drh301eecc2015-08-17 20:14:19 +00002294#endif
drh5fa5c102015-08-12 16:49:40 +00002295 };
drhff135ae2015-12-30 01:07:02 +00002296 static const struct {
2297 const char *zName;
2298 int nArg;
2299 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
2300 void (*xFinal)(sqlite3_context*);
2301 } aAgg[] = {
2302 { "json_group_array", 1, jsonArrayStep, jsonArrayFinal },
2303 { "json_group_object", 2, jsonObjectStep, jsonObjectFinal },
2304 };
drhd2975922015-08-29 17:22:33 +00002305#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002306 static const struct {
2307 const char *zName;
2308 sqlite3_module *pModule;
2309 } aMod[] = {
2310 { "json_each", &jsonEachModule },
2311 { "json_tree", &jsonTreeModule },
2312 };
drhd2975922015-08-29 17:22:33 +00002313#endif
drh5fa5c102015-08-12 16:49:40 +00002314 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
2315 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
drh52216ad2015-08-18 02:28:03 +00002316 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
2317 (void*)&aFunc[i].flag,
drh5fa5c102015-08-12 16:49:40 +00002318 aFunc[i].xFunc, 0, 0);
2319 }
drhff135ae2015-12-30 01:07:02 +00002320 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
2321 rc = sqlite3_create_function(db, aAgg[i].zName, aAgg[i].nArg,
2322 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
2323 0, aAgg[i].xStep, aAgg[i].xFinal);
2324 }
drhd2975922015-08-29 17:22:33 +00002325#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002326 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
2327 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
drhcb6c6c62015-08-19 22:47:17 +00002328 }
drhd2975922015-08-29 17:22:33 +00002329#endif
drh5fa5c102015-08-12 16:49:40 +00002330 return rc;
2331}
drh2f20e132015-09-26 17:44:59 +00002332
2333
dan8d32e802015-10-14 18:45:42 +00002334#ifndef SQLITE_CORE
drh2f20e132015-09-26 17:44:59 +00002335#ifdef _WIN32
2336__declspec(dllexport)
2337#endif
2338int sqlite3_json_init(
2339 sqlite3 *db,
2340 char **pzErrMsg,
2341 const sqlite3_api_routines *pApi
2342){
2343 SQLITE_EXTENSION_INIT2(pApi);
2344 (void)pzErrMsg; /* Unused parameter */
2345 return sqlite3Json1Init(db);
2346}
dan8d32e802015-10-14 18:45:42 +00002347#endif
drh50065652015-10-08 19:29:18 +00002348#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */