blob: 3c63b6e9469471fd1aa79ac6801850f7b2efaee8 [file] [log] [blame]
drh5fa5c102015-08-12 16:49:40 +00001/*
2** 2015-08-12
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11******************************************************************************
12**
13** This SQLite extension implements JSON functions. The interface is
14** modeled after MySQL JSON functions:
15**
16** https://dev.mysql.com/doc/refman/5.7/en/json.html
17**
drh5634cc02015-08-17 11:28:03 +000018** For the time being, all JSON is stored as pure text. (We might add
19** a JSONB type in the future which stores a binary encoding of JSON in
drhcb6c6c62015-08-19 22:47:17 +000020** a BLOB, but there is no support for JSONB in the current implementation.
21** This implementation parses JSON text at 250 MB/s, so it is hard to see
22** how JSONB might improve on that.)
drh5fa5c102015-08-12 16:49:40 +000023*/
drh50065652015-10-08 19:29:18 +000024#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1)
drhf2df7e72015-08-28 20:07:40 +000025#if !defined(_SQLITEINT_H_)
drh5fa5c102015-08-12 16:49:40 +000026#include "sqlite3ext.h"
drhf2df7e72015-08-28 20:07:40 +000027#endif
drh5fa5c102015-08-12 16:49:40 +000028SQLITE_EXTENSION_INIT1
29#include <assert.h>
30#include <string.h>
drh987eb1f2015-08-17 15:17:37 +000031#include <stdlib.h>
drh4af352d2015-08-21 20:02:48 +000032#include <stdarg.h>
drh5fa5c102015-08-12 16:49:40 +000033
drh6fd5c1e2015-08-21 20:37:12 +000034#define UNUSED_PARAM(X) (void)(X)
35
drh8deb4b82015-10-09 18:21:43 +000036#ifndef LARGEST_INT64
37# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
38# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
39#endif
40
dan2e8f5512015-09-17 17:21:09 +000041/*
42** Versions of isspace(), isalnum() and isdigit() to which it is safe
43** to pass signed char values.
44*/
drh49472652015-10-16 15:35:39 +000045#ifdef sqlite3Isdigit
46 /* Use the SQLite core versions if this routine is part of the
47 ** SQLite amalgamation */
48# define safe_isdigit(x) sqlite3Isdigit(x)
49# define safe_isalnum(x) sqlite3Isalnum(x)
50#else
51 /* Use the standard library for separate compilation */
52#include <ctype.h> /* amalgamator: keep */
53# define safe_isdigit(x) isdigit((unsigned char)(x))
54# define safe_isalnum(x) isalnum((unsigned char)(x))
55#endif
dan2e8f5512015-09-17 17:21:09 +000056
drh95677942015-09-24 01:06:37 +000057/*
58** Growing our own isspace() routine this way is twice as fast as
59** the library isspace() function, resulting in a 7% overall performance
60** increase for the parser. (Ubuntu14.10 gcc 4.8.4 x64 with -Os).
61*/
62static const char jsonIsSpace[] = {
drhb9e8f592015-10-16 15:16:06 +000063 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
drh95677942015-09-24 01:06:37 +000064 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79};
80#define safe_isspace(x) (jsonIsSpace[(unsigned char)x])
81
drh9a4718f2015-10-10 14:00:37 +000082#ifndef SQLITE_AMALGAMATION
83 /* Unsigned integer types. These are already defined in the sqliteInt.h,
84 ** but the definitions need to be repeated for separate compilation. */
85 typedef sqlite3_uint64 u64;
86 typedef unsigned int u32;
87 typedef unsigned char u8;
88#endif
drh5fa5c102015-08-12 16:49:40 +000089
drh52216ad2015-08-18 02:28:03 +000090/* Objects */
drh505ad2c2015-08-21 17:33:11 +000091typedef struct JsonString JsonString;
drh52216ad2015-08-18 02:28:03 +000092typedef struct JsonNode JsonNode;
93typedef struct JsonParse JsonParse;
94
drh5634cc02015-08-17 11:28:03 +000095/* An instance of this object represents a JSON string
96** under construction. Really, this is a generic string accumulator
97** that can be and is used to create strings other than JSON.
drh5fa5c102015-08-12 16:49:40 +000098*/
drh505ad2c2015-08-21 17:33:11 +000099struct JsonString {
drh5fa5c102015-08-12 16:49:40 +0000100 sqlite3_context *pCtx; /* Function context - put error messages here */
drh5634cc02015-08-17 11:28:03 +0000101 char *zBuf; /* Append JSON content here */
drh5fa5c102015-08-12 16:49:40 +0000102 u64 nAlloc; /* Bytes of storage available in zBuf[] */
103 u64 nUsed; /* Bytes of zBuf[] currently used */
104 u8 bStatic; /* True if zBuf is static space */
drhd0960592015-08-17 21:22:32 +0000105 u8 bErr; /* True if an error has been encountered */
drh5fa5c102015-08-12 16:49:40 +0000106 char zSpace[100]; /* Initial static space */
107};
108
drhe9c37f32015-08-15 21:25:36 +0000109/* JSON type values
drhbd0621b2015-08-13 13:54:59 +0000110*/
drhe9c37f32015-08-15 21:25:36 +0000111#define JSON_NULL 0
112#define JSON_TRUE 1
113#define JSON_FALSE 2
114#define JSON_INT 3
115#define JSON_REAL 4
116#define JSON_STRING 5
117#define JSON_ARRAY 6
118#define JSON_OBJECT 7
119
drhf5ddb9c2015-09-11 00:06:41 +0000120/* The "subtype" set for JSON values */
121#define JSON_SUBTYPE 74 /* Ascii for "J" */
122
drh987eb1f2015-08-17 15:17:37 +0000123/*
124** Names of the various JSON types:
125*/
126static const char * const jsonType[] = {
127 "null", "true", "false", "integer", "real", "text", "array", "object"
128};
129
drh301eecc2015-08-17 20:14:19 +0000130/* Bit values for the JsonNode.jnFlag field
131*/
132#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
133#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
134#define JNODE_REMOVE 0x04 /* Do not output */
drhd0960592015-08-17 21:22:32 +0000135#define JNODE_REPLACE 0x08 /* Replace with JsonNode.iVal */
drh52216ad2015-08-18 02:28:03 +0000136#define JNODE_APPEND 0x10 /* More ARRAY/OBJECT entries at u.iAppend */
drhf5ddb9c2015-09-11 00:06:41 +0000137#define JNODE_LABEL 0x20 /* Is a label of an object */
drh301eecc2015-08-17 20:14:19 +0000138
drh987eb1f2015-08-17 15:17:37 +0000139
drhe9c37f32015-08-15 21:25:36 +0000140/* A single node of parsed JSON
141*/
drhe9c37f32015-08-15 21:25:36 +0000142struct JsonNode {
drh5634cc02015-08-17 11:28:03 +0000143 u8 eType; /* One of the JSON_ type values */
drh301eecc2015-08-17 20:14:19 +0000144 u8 jnFlags; /* JNODE flags */
drhd0960592015-08-17 21:22:32 +0000145 u8 iVal; /* Replacement value when JNODE_REPLACE */
drhe9c37f32015-08-15 21:25:36 +0000146 u32 n; /* Bytes of content, or number of sub-nodes */
drh52216ad2015-08-18 02:28:03 +0000147 union {
drh0042a972015-08-18 12:59:58 +0000148 const char *zJContent; /* Content for INT, REAL, and STRING */
149 u32 iAppend; /* More terms for ARRAY and OBJECT */
drh505ad2c2015-08-21 17:33:11 +0000150 u32 iKey; /* Key for ARRAY objects in json_tree() */
drh52216ad2015-08-18 02:28:03 +0000151 } u;
drhe9c37f32015-08-15 21:25:36 +0000152};
153
154/* A completely parsed JSON string
155*/
drhe9c37f32015-08-15 21:25:36 +0000156struct JsonParse {
157 u32 nNode; /* Number of slots of aNode[] used */
158 u32 nAlloc; /* Number of slots of aNode[] allocated */
159 JsonNode *aNode; /* Array of nodes containing the parse */
160 const char *zJson; /* Original JSON string */
drh505ad2c2015-08-21 17:33:11 +0000161 u32 *aUp; /* Index of parent of each node */
drhe9c37f32015-08-15 21:25:36 +0000162 u8 oom; /* Set to true if out of memory */
drha7714022015-08-29 00:54:49 +0000163 u8 nErr; /* Number of errors seen */
drhe9c37f32015-08-15 21:25:36 +0000164};
165
drh505ad2c2015-08-21 17:33:11 +0000166/**************************************************************************
167** Utility routines for dealing with JsonString objects
168**************************************************************************/
drh301eecc2015-08-17 20:14:19 +0000169
drh505ad2c2015-08-21 17:33:11 +0000170/* Set the JsonString object to an empty string
drh5fa5c102015-08-12 16:49:40 +0000171*/
drh505ad2c2015-08-21 17:33:11 +0000172static void jsonZero(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000173 p->zBuf = p->zSpace;
174 p->nAlloc = sizeof(p->zSpace);
175 p->nUsed = 0;
176 p->bStatic = 1;
177}
178
drh505ad2c2015-08-21 17:33:11 +0000179/* Initialize the JsonString object
drh5fa5c102015-08-12 16:49:40 +0000180*/
drh505ad2c2015-08-21 17:33:11 +0000181static void jsonInit(JsonString *p, sqlite3_context *pCtx){
drh5fa5c102015-08-12 16:49:40 +0000182 p->pCtx = pCtx;
drhd0960592015-08-17 21:22:32 +0000183 p->bErr = 0;
drh5fa5c102015-08-12 16:49:40 +0000184 jsonZero(p);
185}
186
187
drh505ad2c2015-08-21 17:33:11 +0000188/* Free all allocated memory and reset the JsonString object back to its
drh5fa5c102015-08-12 16:49:40 +0000189** initial state.
190*/
drh505ad2c2015-08-21 17:33:11 +0000191static void jsonReset(JsonString *p){
drh5fa5c102015-08-12 16:49:40 +0000192 if( !p->bStatic ) sqlite3_free(p->zBuf);
193 jsonZero(p);
194}
195
196
197/* Report an out-of-memory (OOM) condition
198*/
drh505ad2c2015-08-21 17:33:11 +0000199static void jsonOom(JsonString *p){
drh3d1d2a92015-09-22 01:15:49 +0000200 p->bErr = 1;
201 sqlite3_result_error_nomem(p->pCtx);
202 jsonReset(p);
drh5fa5c102015-08-12 16:49:40 +0000203}
204
205/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
206** Return zero on success. Return non-zero on an OOM error
207*/
drh505ad2c2015-08-21 17:33:11 +0000208static int jsonGrow(JsonString *p, u32 N){
drh301eecc2015-08-17 20:14:19 +0000209 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
drh5fa5c102015-08-12 16:49:40 +0000210 char *zNew;
211 if( p->bStatic ){
drhd0960592015-08-17 21:22:32 +0000212 if( p->bErr ) return 1;
drh5fa5c102015-08-12 16:49:40 +0000213 zNew = sqlite3_malloc64(nTotal);
214 if( zNew==0 ){
215 jsonOom(p);
216 return SQLITE_NOMEM;
217 }
drh6fd5c1e2015-08-21 20:37:12 +0000218 memcpy(zNew, p->zBuf, (size_t)p->nUsed);
drh5fa5c102015-08-12 16:49:40 +0000219 p->zBuf = zNew;
220 p->bStatic = 0;
221 }else{
222 zNew = sqlite3_realloc64(p->zBuf, nTotal);
223 if( zNew==0 ){
224 jsonOom(p);
225 return SQLITE_NOMEM;
226 }
227 p->zBuf = zNew;
228 }
229 p->nAlloc = nTotal;
230 return SQLITE_OK;
231}
232
drh505ad2c2015-08-21 17:33:11 +0000233/* Append N bytes from zIn onto the end of the JsonString string.
drh5fa5c102015-08-12 16:49:40 +0000234*/
drh505ad2c2015-08-21 17:33:11 +0000235static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000236 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
237 memcpy(p->zBuf+p->nUsed, zIn, N);
238 p->nUsed += N;
239}
240
drh4af352d2015-08-21 20:02:48 +0000241/* Append formatted text (not to exceed N bytes) to the JsonString.
242*/
243static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
244 va_list ap;
245 if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
246 va_start(ap, zFormat);
247 sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
248 va_end(ap);
249 p->nUsed += (int)strlen(p->zBuf+p->nUsed);
250}
251
drh5634cc02015-08-17 11:28:03 +0000252/* Append a single character
253*/
drh505ad2c2015-08-21 17:33:11 +0000254static void jsonAppendChar(JsonString *p, char c){
drh5634cc02015-08-17 11:28:03 +0000255 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
256 p->zBuf[p->nUsed++] = c;
257}
258
drh301eecc2015-08-17 20:14:19 +0000259/* Append a comma separator to the output buffer, if the previous
260** character is not '[' or '{'.
261*/
drh505ad2c2015-08-21 17:33:11 +0000262static void jsonAppendSeparator(JsonString *p){
drh301eecc2015-08-17 20:14:19 +0000263 char c;
264 if( p->nUsed==0 ) return;
265 c = p->zBuf[p->nUsed-1];
266 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
267}
268
drh505ad2c2015-08-21 17:33:11 +0000269/* Append the N-byte string in zIn to the end of the JsonString string
drh5fa5c102015-08-12 16:49:40 +0000270** under construction. Enclose the string in "..." and escape
271** any double-quotes or backslash characters contained within the
272** string.
273*/
drh505ad2c2015-08-21 17:33:11 +0000274static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
drh5fa5c102015-08-12 16:49:40 +0000275 u32 i;
276 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
277 p->zBuf[p->nUsed++] = '"';
278 for(i=0; i<N; i++){
drh3b7f9a62016-02-04 10:28:57 +0000279 unsigned char c = ((unsigned const char*)zIn)[i];
drh5fa5c102015-08-12 16:49:40 +0000280 if( c=='"' || c=='\\' ){
drh3b7f9a62016-02-04 10:28:57 +0000281 json_simple_escape:
drh4977ccf2015-09-19 11:57:26 +0000282 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
drh5fa5c102015-08-12 16:49:40 +0000283 p->zBuf[p->nUsed++] = '\\';
drh3b7f9a62016-02-04 10:28:57 +0000284 }else if( c<=0x1f ){
285 static const char aSpecial[] = {
286 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
287 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
288 };
289 assert( sizeof(aSpecial)==32 );
290 assert( aSpecial['\b']=='b' );
291 assert( aSpecial['\f']=='f' );
292 assert( aSpecial['\n']=='n' );
293 assert( aSpecial['\r']=='r' );
294 assert( aSpecial['\t']=='t' );
295 if( aSpecial[c] ){
296 c = aSpecial[c];
297 goto json_simple_escape;
298 }
299 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
300 p->zBuf[p->nUsed++] = '\\';
301 p->zBuf[p->nUsed++] = 'u';
302 p->zBuf[p->nUsed++] = '0';
303 p->zBuf[p->nUsed++] = '0';
304 p->zBuf[p->nUsed++] = '0' + (c>>4);
305 c = "0123456789abcdef"[c&0xf];
drh5fa5c102015-08-12 16:49:40 +0000306 }
307 p->zBuf[p->nUsed++] = c;
308 }
309 p->zBuf[p->nUsed++] = '"';
drh4977ccf2015-09-19 11:57:26 +0000310 assert( p->nUsed<p->nAlloc );
drh5fa5c102015-08-12 16:49:40 +0000311}
312
drhd0960592015-08-17 21:22:32 +0000313/*
314** Append a function parameter value to the JSON string under
315** construction.
316*/
317static void jsonAppendValue(
drh505ad2c2015-08-21 17:33:11 +0000318 JsonString *p, /* Append to this JSON string */
drhf5ddb9c2015-09-11 00:06:41 +0000319 sqlite3_value *pValue /* Value to append */
drhd0960592015-08-17 21:22:32 +0000320){
321 switch( sqlite3_value_type(pValue) ){
322 case SQLITE_NULL: {
323 jsonAppendRaw(p, "null", 4);
324 break;
325 }
326 case SQLITE_INTEGER:
327 case SQLITE_FLOAT: {
328 const char *z = (const char*)sqlite3_value_text(pValue);
329 u32 n = (u32)sqlite3_value_bytes(pValue);
330 jsonAppendRaw(p, z, n);
331 break;
332 }
333 case SQLITE_TEXT: {
334 const char *z = (const char*)sqlite3_value_text(pValue);
335 u32 n = (u32)sqlite3_value_bytes(pValue);
drhf5ddb9c2015-09-11 00:06:41 +0000336 if( sqlite3_value_subtype(pValue)==JSON_SUBTYPE ){
drhecb5fed2015-08-28 03:33:50 +0000337 jsonAppendRaw(p, z, n);
338 }else{
339 jsonAppendString(p, z, n);
340 }
drhd0960592015-08-17 21:22:32 +0000341 break;
342 }
343 default: {
344 if( p->bErr==0 ){
345 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
drh4a642b62016-02-05 01:55:27 +0000346 p->bErr = 2;
drhd0960592015-08-17 21:22:32 +0000347 jsonReset(p);
348 }
349 break;
350 }
351 }
352}
353
354
drhbd0621b2015-08-13 13:54:59 +0000355/* Make the JSON in p the result of the SQL function.
drh5fa5c102015-08-12 16:49:40 +0000356*/
drh505ad2c2015-08-21 17:33:11 +0000357static void jsonResult(JsonString *p){
drhd0960592015-08-17 21:22:32 +0000358 if( p->bErr==0 ){
drh5fa5c102015-08-12 16:49:40 +0000359 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
360 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
361 SQLITE_UTF8);
362 jsonZero(p);
363 }
364 assert( p->bStatic );
365}
366
drh505ad2c2015-08-21 17:33:11 +0000367/**************************************************************************
368** Utility routines for dealing with JsonNode and JsonParse objects
369**************************************************************************/
370
371/*
372** Return the number of consecutive JsonNode slots need to represent
373** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
374** OBJECT types, the number might be larger.
375**
376** Appended elements are not counted. The value returned is the number
377** by which the JsonNode counter should increment in order to go to the
378** next peer value.
379*/
380static u32 jsonNodeSize(JsonNode *pNode){
381 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
382}
383
384/*
385** Reclaim all memory allocated by a JsonParse object. But do not
386** delete the JsonParse object itself.
387*/
388static void jsonParseReset(JsonParse *pParse){
389 sqlite3_free(pParse->aNode);
390 pParse->aNode = 0;
391 pParse->nNode = 0;
392 pParse->nAlloc = 0;
393 sqlite3_free(pParse->aUp);
394 pParse->aUp = 0;
395}
396
drh5634cc02015-08-17 11:28:03 +0000397/*
398** Convert the JsonNode pNode into a pure JSON string and
399** append to pOut. Subsubstructure is also included. Return
400** the number of JsonNode objects that are encoded.
drhbd0621b2015-08-13 13:54:59 +0000401*/
drh52216ad2015-08-18 02:28:03 +0000402static void jsonRenderNode(
drhd0960592015-08-17 21:22:32 +0000403 JsonNode *pNode, /* The node to render */
drh505ad2c2015-08-21 17:33:11 +0000404 JsonString *pOut, /* Write JSON here */
drhd0960592015-08-17 21:22:32 +0000405 sqlite3_value **aReplace /* Replacement values */
406){
drh5634cc02015-08-17 11:28:03 +0000407 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000408 default: {
409 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000410 jsonAppendRaw(pOut, "null", 4);
411 break;
412 }
413 case JSON_TRUE: {
414 jsonAppendRaw(pOut, "true", 4);
415 break;
416 }
417 case JSON_FALSE: {
418 jsonAppendRaw(pOut, "false", 5);
419 break;
420 }
421 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000422 if( pNode->jnFlags & JNODE_RAW ){
drh52216ad2015-08-18 02:28:03 +0000423 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000424 break;
425 }
426 /* Fall through into the next case */
427 }
428 case JSON_REAL:
429 case JSON_INT: {
drh52216ad2015-08-18 02:28:03 +0000430 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000431 break;
432 }
433 case JSON_ARRAY: {
drh52216ad2015-08-18 02:28:03 +0000434 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000435 jsonAppendChar(pOut, '[');
drh52216ad2015-08-18 02:28:03 +0000436 for(;;){
437 while( j<=pNode->n ){
438 if( pNode[j].jnFlags & (JNODE_REMOVE|JNODE_REPLACE) ){
439 if( pNode[j].jnFlags & JNODE_REPLACE ){
440 jsonAppendSeparator(pOut);
drhf5ddb9c2015-09-11 00:06:41 +0000441 jsonAppendValue(pOut, aReplace[pNode[j].iVal]);
drh52216ad2015-08-18 02:28:03 +0000442 }
443 }else{
drhd0960592015-08-17 21:22:32 +0000444 jsonAppendSeparator(pOut);
drh52216ad2015-08-18 02:28:03 +0000445 jsonRenderNode(&pNode[j], pOut, aReplace);
drhd0960592015-08-17 21:22:32 +0000446 }
drh505ad2c2015-08-21 17:33:11 +0000447 j += jsonNodeSize(&pNode[j]);
drh301eecc2015-08-17 20:14:19 +0000448 }
drh52216ad2015-08-18 02:28:03 +0000449 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
450 pNode = &pNode[pNode->u.iAppend];
451 j = 1;
drh5634cc02015-08-17 11:28:03 +0000452 }
453 jsonAppendChar(pOut, ']');
454 break;
455 }
456 case JSON_OBJECT: {
drh52216ad2015-08-18 02:28:03 +0000457 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000458 jsonAppendChar(pOut, '{');
drh52216ad2015-08-18 02:28:03 +0000459 for(;;){
460 while( j<=pNode->n ){
461 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
462 jsonAppendSeparator(pOut);
463 jsonRenderNode(&pNode[j], pOut, aReplace);
464 jsonAppendChar(pOut, ':');
465 if( pNode[j+1].jnFlags & JNODE_REPLACE ){
drhf5ddb9c2015-09-11 00:06:41 +0000466 jsonAppendValue(pOut, aReplace[pNode[j+1].iVal]);
drh52216ad2015-08-18 02:28:03 +0000467 }else{
468 jsonRenderNode(&pNode[j+1], pOut, aReplace);
469 }
drhd0960592015-08-17 21:22:32 +0000470 }
drh505ad2c2015-08-21 17:33:11 +0000471 j += 1 + jsonNodeSize(&pNode[j+1]);
drh301eecc2015-08-17 20:14:19 +0000472 }
drh52216ad2015-08-18 02:28:03 +0000473 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
474 pNode = &pNode[pNode->u.iAppend];
475 j = 1;
drh5634cc02015-08-17 11:28:03 +0000476 }
477 jsonAppendChar(pOut, '}');
478 break;
479 }
drhbd0621b2015-08-13 13:54:59 +0000480 }
drh5634cc02015-08-17 11:28:03 +0000481}
482
483/*
drhf2df7e72015-08-28 20:07:40 +0000484** Return a JsonNode and all its descendents as a JSON string.
485*/
486static void jsonReturnJson(
487 JsonNode *pNode, /* Node to return */
488 sqlite3_context *pCtx, /* Return value for this function */
489 sqlite3_value **aReplace /* Array of replacement values */
490){
491 JsonString s;
492 jsonInit(&s, pCtx);
493 jsonRenderNode(pNode, &s, aReplace);
494 jsonResult(&s);
drhf5ddb9c2015-09-11 00:06:41 +0000495 sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
drhf2df7e72015-08-28 20:07:40 +0000496}
497
498/*
drh5634cc02015-08-17 11:28:03 +0000499** Make the JsonNode the return value of the function.
500*/
drhd0960592015-08-17 21:22:32 +0000501static void jsonReturn(
502 JsonNode *pNode, /* Node to return */
503 sqlite3_context *pCtx, /* Return value for this function */
504 sqlite3_value **aReplace /* Array of replacement values */
505){
drh5634cc02015-08-17 11:28:03 +0000506 switch( pNode->eType ){
drha8f39a92015-09-21 22:53:16 +0000507 default: {
508 assert( pNode->eType==JSON_NULL );
drh5634cc02015-08-17 11:28:03 +0000509 sqlite3_result_null(pCtx);
510 break;
511 }
512 case JSON_TRUE: {
513 sqlite3_result_int(pCtx, 1);
514 break;
515 }
516 case JSON_FALSE: {
517 sqlite3_result_int(pCtx, 0);
518 break;
519 }
drh987eb1f2015-08-17 15:17:37 +0000520 case JSON_INT: {
521 sqlite3_int64 i = 0;
drh52216ad2015-08-18 02:28:03 +0000522 const char *z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000523 if( z[0]=='-' ){ z++; }
drh8deb4b82015-10-09 18:21:43 +0000524 while( z[0]>='0' && z[0]<='9' ){
525 unsigned v = *(z++) - '0';
526 if( i>=LARGEST_INT64/10 ){
drha0882fa2015-10-09 20:40:44 +0000527 if( i>LARGEST_INT64/10 ) goto int_as_real;
drh8deb4b82015-10-09 18:21:43 +0000528 if( z[0]>='0' && z[0]<='9' ) goto int_as_real;
529 if( v==9 ) goto int_as_real;
530 if( v==8 ){
531 if( pNode->u.zJContent[0]=='-' ){
532 sqlite3_result_int64(pCtx, SMALLEST_INT64);
533 goto int_done;
534 }else{
535 goto int_as_real;
536 }
537 }
538 }
539 i = i*10 + v;
540 }
drh52216ad2015-08-18 02:28:03 +0000541 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
drh987eb1f2015-08-17 15:17:37 +0000542 sqlite3_result_int64(pCtx, i);
drh8deb4b82015-10-09 18:21:43 +0000543 int_done:
544 break;
545 int_as_real: /* fall through to real */;
546 }
547 case JSON_REAL: {
drh49472652015-10-16 15:35:39 +0000548 double r;
549#ifdef SQLITE_AMALGAMATION
550 const char *z = pNode->u.zJContent;
551 sqlite3AtoF(z, &r, sqlite3Strlen30(z), SQLITE_UTF8);
552#else
553 r = strtod(pNode->u.zJContent, 0);
554#endif
drh8deb4b82015-10-09 18:21:43 +0000555 sqlite3_result_double(pCtx, r);
drh987eb1f2015-08-17 15:17:37 +0000556 break;
557 }
drh5634cc02015-08-17 11:28:03 +0000558 case JSON_STRING: {
drha8f39a92015-09-21 22:53:16 +0000559#if 0 /* Never happens because JNODE_RAW is only set by json_set(),
560 ** json_insert() and json_replace() and those routines do not
561 ** call jsonReturn() */
drh301eecc2015-08-17 20:14:19 +0000562 if( pNode->jnFlags & JNODE_RAW ){
drh52216ad2015-08-18 02:28:03 +0000563 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
564 SQLITE_TRANSIENT);
drha8f39a92015-09-21 22:53:16 +0000565 }else
566#endif
567 assert( (pNode->jnFlags & JNODE_RAW)==0 );
568 if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
drh987eb1f2015-08-17 15:17:37 +0000569 /* JSON formatted without any backslash-escapes */
drh52216ad2015-08-18 02:28:03 +0000570 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
drh987eb1f2015-08-17 15:17:37 +0000571 SQLITE_TRANSIENT);
drh5634cc02015-08-17 11:28:03 +0000572 }else{
573 /* Translate JSON formatted string into raw text */
drh987eb1f2015-08-17 15:17:37 +0000574 u32 i;
575 u32 n = pNode->n;
drh52216ad2015-08-18 02:28:03 +0000576 const char *z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000577 char *zOut;
578 u32 j;
579 zOut = sqlite3_malloc( n+1 );
580 if( zOut==0 ){
581 sqlite3_result_error_nomem(pCtx);
582 break;
583 }
584 for(i=1, j=0; i<n-1; i++){
585 char c = z[i];
drh80d87402015-08-24 12:42:41 +0000586 if( c!='\\' ){
drh987eb1f2015-08-17 15:17:37 +0000587 zOut[j++] = c;
588 }else{
589 c = z[++i];
drh80d87402015-08-24 12:42:41 +0000590 if( c=='u' ){
drh987eb1f2015-08-17 15:17:37 +0000591 u32 v = 0, k;
drh80d87402015-08-24 12:42:41 +0000592 for(k=0; k<4 && i<n-2; i++, k++){
drh8784eca2015-08-23 02:42:30 +0000593 c = z[i+1];
drh987eb1f2015-08-17 15:17:37 +0000594 if( c>='0' && c<='9' ) v = v*16 + c - '0';
595 else if( c>='A' && c<='F' ) v = v*16 + c - 'A' + 10;
596 else if( c>='a' && c<='f' ) v = v*16 + c - 'a' + 10;
597 else break;
drh987eb1f2015-08-17 15:17:37 +0000598 }
drh80d87402015-08-24 12:42:41 +0000599 if( v==0 ) break;
drh987eb1f2015-08-17 15:17:37 +0000600 if( v<=0x7f ){
mistachkin16a93122015-09-11 18:05:01 +0000601 zOut[j++] = (char)v;
drh987eb1f2015-08-17 15:17:37 +0000602 }else if( v<=0x7ff ){
mistachkin16a93122015-09-11 18:05:01 +0000603 zOut[j++] = (char)(0xc0 | (v>>6));
drh987eb1f2015-08-17 15:17:37 +0000604 zOut[j++] = 0x80 | (v&0x3f);
drh80d87402015-08-24 12:42:41 +0000605 }else{
mistachkin16a93122015-09-11 18:05:01 +0000606 zOut[j++] = (char)(0xe0 | (v>>12));
drh987eb1f2015-08-17 15:17:37 +0000607 zOut[j++] = 0x80 | ((v>>6)&0x3f);
608 zOut[j++] = 0x80 | (v&0x3f);
drh987eb1f2015-08-17 15:17:37 +0000609 }
610 }else{
611 if( c=='b' ){
612 c = '\b';
613 }else if( c=='f' ){
614 c = '\f';
615 }else if( c=='n' ){
616 c = '\n';
617 }else if( c=='r' ){
618 c = '\r';
619 }else if( c=='t' ){
620 c = '\t';
621 }
622 zOut[j++] = c;
623 }
624 }
625 }
626 zOut[j] = 0;
627 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
drh5634cc02015-08-17 11:28:03 +0000628 }
629 break;
630 }
631 case JSON_ARRAY:
632 case JSON_OBJECT: {
drhf2df7e72015-08-28 20:07:40 +0000633 jsonReturnJson(pNode, pCtx, aReplace);
drh5634cc02015-08-17 11:28:03 +0000634 break;
635 }
636 }
drhbd0621b2015-08-13 13:54:59 +0000637}
638
drh95677942015-09-24 01:06:37 +0000639/* Forward reference */
640static int jsonParseAddNode(JsonParse*,u32,u32,const char*);
641
642/*
643** A macro to hint to the compiler that a function should not be
644** inlined.
645*/
646#if defined(__GNUC__)
647# define JSON_NOINLINE __attribute__((noinline))
648#elif defined(_MSC_VER) && _MSC_VER>=1310
649# define JSON_NOINLINE __declspec(noinline)
650#else
651# define JSON_NOINLINE
652#endif
653
654
655static JSON_NOINLINE int jsonParseAddNodeExpand(
656 JsonParse *pParse, /* Append the node to this object */
657 u32 eType, /* Node type */
658 u32 n, /* Content size or sub-node count */
659 const char *zContent /* Content */
660){
661 u32 nNew;
662 JsonNode *pNew;
663 assert( pParse->nNode>=pParse->nAlloc );
664 if( pParse->oom ) return -1;
665 nNew = pParse->nAlloc*2 + 10;
666 pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew);
667 if( pNew==0 ){
668 pParse->oom = 1;
669 return -1;
670 }
671 pParse->nAlloc = nNew;
672 pParse->aNode = pNew;
673 assert( pParse->nNode<pParse->nAlloc );
674 return jsonParseAddNode(pParse, eType, n, zContent);
675}
676
drh5fa5c102015-08-12 16:49:40 +0000677/*
drhe9c37f32015-08-15 21:25:36 +0000678** Create a new JsonNode instance based on the arguments and append that
679** instance to the JsonParse. Return the index in pParse->aNode[] of the
680** new node, or -1 if a memory allocation fails.
681*/
682static int jsonParseAddNode(
683 JsonParse *pParse, /* Append the node to this object */
684 u32 eType, /* Node type */
685 u32 n, /* Content size or sub-node count */
686 const char *zContent /* Content */
687){
688 JsonNode *p;
689 if( pParse->nNode>=pParse->nAlloc ){
drh95677942015-09-24 01:06:37 +0000690 return jsonParseAddNodeExpand(pParse, eType, n, zContent);
drhe9c37f32015-08-15 21:25:36 +0000691 }
692 p = &pParse->aNode[pParse->nNode];
drh5634cc02015-08-17 11:28:03 +0000693 p->eType = (u8)eType;
drh301eecc2015-08-17 20:14:19 +0000694 p->jnFlags = 0;
drhd0960592015-08-17 21:22:32 +0000695 p->iVal = 0;
drhe9c37f32015-08-15 21:25:36 +0000696 p->n = n;
drh52216ad2015-08-18 02:28:03 +0000697 p->u.zJContent = zContent;
drhe9c37f32015-08-15 21:25:36 +0000698 return pParse->nNode++;
699}
700
701/*
702** Parse a single JSON value which begins at pParse->zJson[i]. Return the
703** index of the first character past the end of the value parsed.
704**
705** Return negative for a syntax error. Special cases: return -2 if the
706** first non-whitespace character is '}' and return -3 if the first
707** non-whitespace character is ']'.
708*/
709static int jsonParseValue(JsonParse *pParse, u32 i){
710 char c;
711 u32 j;
drhbc8f0922015-08-22 19:39:04 +0000712 int iThis;
drhe9c37f32015-08-15 21:25:36 +0000713 int x;
drh852944e2015-09-10 03:29:11 +0000714 JsonNode *pNode;
dan2e8f5512015-09-17 17:21:09 +0000715 while( safe_isspace(pParse->zJson[i]) ){ i++; }
drh8cb15cc2015-09-24 01:40:45 +0000716 if( (c = pParse->zJson[i])=='{' ){
drhe9c37f32015-08-15 21:25:36 +0000717 /* Parse object */
718 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000719 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000720 for(j=i+1;;j++){
dan2e8f5512015-09-17 17:21:09 +0000721 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000722 x = jsonParseValue(pParse, j);
723 if( x<0 ){
drhf27cd1f2015-09-23 01:10:29 +0000724 if( x==(-2) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000725 return -1;
726 }
drhbe9474e2015-08-22 03:05:54 +0000727 if( pParse->oom ) return -1;
drh852944e2015-09-10 03:29:11 +0000728 pNode = &pParse->aNode[pParse->nNode-1];
729 if( pNode->eType!=JSON_STRING ) return -1;
730 pNode->jnFlags |= JNODE_LABEL;
drhe9c37f32015-08-15 21:25:36 +0000731 j = x;
dan2e8f5512015-09-17 17:21:09 +0000732 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000733 if( pParse->zJson[j]!=':' ) return -1;
734 j++;
735 x = jsonParseValue(pParse, j);
736 if( x<0 ) return -1;
737 j = x;
dan2e8f5512015-09-17 17:21:09 +0000738 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000739 c = pParse->zJson[j];
740 if( c==',' ) continue;
741 if( c!='}' ) return -1;
742 break;
743 }
drhbc8f0922015-08-22 19:39:04 +0000744 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000745 return j+1;
746 }else if( c=='[' ){
747 /* Parse array */
748 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
drhbc8f0922015-08-22 19:39:04 +0000749 if( iThis<0 ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000750 for(j=i+1;;j++){
dan2e8f5512015-09-17 17:21:09 +0000751 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000752 x = jsonParseValue(pParse, j);
753 if( x<0 ){
drhf27cd1f2015-09-23 01:10:29 +0000754 if( x==(-3) && pParse->nNode==(u32)iThis+1 ) return j+1;
drhe9c37f32015-08-15 21:25:36 +0000755 return -1;
756 }
757 j = x;
dan2e8f5512015-09-17 17:21:09 +0000758 while( safe_isspace(pParse->zJson[j]) ){ j++; }
drhe9c37f32015-08-15 21:25:36 +0000759 c = pParse->zJson[j];
760 if( c==',' ) continue;
761 if( c!=']' ) return -1;
762 break;
763 }
drhbc8f0922015-08-22 19:39:04 +0000764 pParse->aNode[iThis].n = pParse->nNode - (u32)iThis - 1;
drhe9c37f32015-08-15 21:25:36 +0000765 return j+1;
766 }else if( c=='"' ){
767 /* Parse string */
drh301eecc2015-08-17 20:14:19 +0000768 u8 jnFlags = 0;
drhe9c37f32015-08-15 21:25:36 +0000769 j = i+1;
770 for(;;){
771 c = pParse->zJson[j];
772 if( c==0 ) return -1;
773 if( c=='\\' ){
774 c = pParse->zJson[++j];
775 if( c==0 ) return -1;
drh301eecc2015-08-17 20:14:19 +0000776 jnFlags = JNODE_ESCAPE;
drhe9c37f32015-08-15 21:25:36 +0000777 }else if( c=='"' ){
778 break;
779 }
780 j++;
781 }
782 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]);
drhbe9474e2015-08-22 03:05:54 +0000783 if( !pParse->oom ) pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
drhe9c37f32015-08-15 21:25:36 +0000784 return j+1;
785 }else if( c=='n'
786 && strncmp(pParse->zJson+i,"null",4)==0
dan2e8f5512015-09-17 17:21:09 +0000787 && !safe_isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000788 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
789 return i+4;
790 }else if( c=='t'
791 && strncmp(pParse->zJson+i,"true",4)==0
dan2e8f5512015-09-17 17:21:09 +0000792 && !safe_isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000793 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
794 return i+4;
795 }else if( c=='f'
796 && strncmp(pParse->zJson+i,"false",5)==0
dan2e8f5512015-09-17 17:21:09 +0000797 && !safe_isalnum(pParse->zJson[i+5]) ){
drhe9c37f32015-08-15 21:25:36 +0000798 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
799 return i+5;
800 }else if( c=='-' || (c>='0' && c<='9') ){
801 /* Parse number */
802 u8 seenDP = 0;
803 u8 seenE = 0;
804 j = i+1;
805 for(;; j++){
806 c = pParse->zJson[j];
807 if( c>='0' && c<='9' ) continue;
808 if( c=='.' ){
809 if( pParse->zJson[j-1]=='-' ) return -1;
810 if( seenDP ) return -1;
811 seenDP = 1;
812 continue;
813 }
814 if( c=='e' || c=='E' ){
815 if( pParse->zJson[j-1]<'0' ) return -1;
816 if( seenE ) return -1;
817 seenDP = seenE = 1;
818 c = pParse->zJson[j+1];
drh8784eca2015-08-23 02:42:30 +0000819 if( c=='+' || c=='-' ){
820 j++;
821 c = pParse->zJson[j+1];
822 }
drhd1f00682015-08-29 16:02:37 +0000823 if( c<'0' || c>'9' ) return -1;
drhe9c37f32015-08-15 21:25:36 +0000824 continue;
825 }
826 break;
827 }
828 if( pParse->zJson[j-1]<'0' ) return -1;
829 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
830 j - i, &pParse->zJson[i]);
831 return j;
832 }else if( c=='}' ){
833 return -2; /* End of {...} */
834 }else if( c==']' ){
835 return -3; /* End of [...] */
drh8cb15cc2015-09-24 01:40:45 +0000836 }else if( c==0 ){
837 return 0; /* End of file */
drhe9c37f32015-08-15 21:25:36 +0000838 }else{
839 return -1; /* Syntax error */
840 }
841}
842
843/*
844** Parse a complete JSON string. Return 0 on success or non-zero if there
845** are any errors. If an error occurs, free all memory associated with
846** pParse.
847**
848** pParse is uninitialized when this routine is called.
849*/
drhbc8f0922015-08-22 19:39:04 +0000850static int jsonParse(
851 JsonParse *pParse, /* Initialize and fill this JsonParse object */
852 sqlite3_context *pCtx, /* Report errors here */
853 const char *zJson /* Input JSON text to be parsed */
854){
drhe9c37f32015-08-15 21:25:36 +0000855 int i;
drhe9c37f32015-08-15 21:25:36 +0000856 memset(pParse, 0, sizeof(*pParse));
drhc3722b22015-08-23 20:44:59 +0000857 if( zJson==0 ) return 1;
drhe9c37f32015-08-15 21:25:36 +0000858 pParse->zJson = zJson;
859 i = jsonParseValue(pParse, 0);
drhc3722b22015-08-23 20:44:59 +0000860 if( pParse->oom ) i = -1;
drhe9c37f32015-08-15 21:25:36 +0000861 if( i>0 ){
dan2e8f5512015-09-17 17:21:09 +0000862 while( safe_isspace(zJson[i]) ) i++;
drhe9c37f32015-08-15 21:25:36 +0000863 if( zJson[i] ) i = -1;
864 }
drhd1f00682015-08-29 16:02:37 +0000865 if( i<=0 ){
drhf2df7e72015-08-28 20:07:40 +0000866 if( pCtx!=0 ){
867 if( pParse->oom ){
868 sqlite3_result_error_nomem(pCtx);
869 }else{
870 sqlite3_result_error(pCtx, "malformed JSON", -1);
871 }
872 }
drh505ad2c2015-08-21 17:33:11 +0000873 jsonParseReset(pParse);
drhe9c37f32015-08-15 21:25:36 +0000874 return 1;
875 }
876 return 0;
877}
drh301eecc2015-08-17 20:14:19 +0000878
drh505ad2c2015-08-21 17:33:11 +0000879/* Mark node i of pParse as being a child of iParent. Call recursively
880** to fill in all the descendants of node i.
881*/
882static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
883 JsonNode *pNode = &pParse->aNode[i];
884 u32 j;
885 pParse->aUp[i] = iParent;
886 switch( pNode->eType ){
887 case JSON_ARRAY: {
888 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
889 jsonParseFillInParentage(pParse, i+j, i);
890 }
891 break;
892 }
893 case JSON_OBJECT: {
894 for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
895 pParse->aUp[i+j] = i;
896 jsonParseFillInParentage(pParse, i+j+1, i);
897 }
898 break;
899 }
900 default: {
901 break;
902 }
903 }
904}
905
906/*
907** Compute the parentage of all nodes in a completed parse.
908*/
909static int jsonParseFindParents(JsonParse *pParse){
910 u32 *aUp;
911 assert( pParse->aUp==0 );
912 aUp = pParse->aUp = sqlite3_malloc( sizeof(u32)*pParse->nNode );
drhc3722b22015-08-23 20:44:59 +0000913 if( aUp==0 ){
914 pParse->oom = 1;
915 return SQLITE_NOMEM;
916 }
drh505ad2c2015-08-21 17:33:11 +0000917 jsonParseFillInParentage(pParse, 0, 0);
918 return SQLITE_OK;
919}
920
drh8cb0c832015-09-22 00:21:03 +0000921/*
922** Compare the OBJECT label at pNode against zKey,nKey. Return true on
923** a match.
924*/
mistachkinf2c26ed2015-10-12 22:20:29 +0000925static int jsonLabelCompare(JsonNode *pNode, const char *zKey, u32 nKey){
drh8cb0c832015-09-22 00:21:03 +0000926 if( pNode->jnFlags & JNODE_RAW ){
927 if( pNode->n!=nKey ) return 0;
928 return strncmp(pNode->u.zJContent, zKey, nKey)==0;
929 }else{
930 if( pNode->n!=nKey+2 ) return 0;
931 return strncmp(pNode->u.zJContent+1, zKey, nKey)==0;
932 }
933}
934
drh52216ad2015-08-18 02:28:03 +0000935/* forward declaration */
drha7714022015-08-29 00:54:49 +0000936static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*,const char**);
drh52216ad2015-08-18 02:28:03 +0000937
drh987eb1f2015-08-17 15:17:37 +0000938/*
939** Search along zPath to find the node specified. Return a pointer
940** to that node, or NULL if zPath is malformed or if there is no such
941** node.
drh52216ad2015-08-18 02:28:03 +0000942**
943** If pApnd!=0, then try to append new nodes to complete zPath if it is
944** possible to do so and if no existing node corresponds to zPath. If
945** new nodes are appended *pApnd is set to 1.
drh987eb1f2015-08-17 15:17:37 +0000946*/
drha7714022015-08-29 00:54:49 +0000947static JsonNode *jsonLookupStep(
drh52216ad2015-08-18 02:28:03 +0000948 JsonParse *pParse, /* The JSON to search */
949 u32 iRoot, /* Begin the search at this node */
950 const char *zPath, /* The path to search */
drha7714022015-08-29 00:54:49 +0000951 int *pApnd, /* Append nodes to complete path if not NULL */
952 const char **pzErr /* Make *pzErr point to any syntax error in zPath */
drh52216ad2015-08-18 02:28:03 +0000953){
drhbc8f0922015-08-22 19:39:04 +0000954 u32 i, j, nKey;
drh6b43cc82015-08-19 23:02:49 +0000955 const char *zKey;
drh52216ad2015-08-18 02:28:03 +0000956 JsonNode *pRoot = &pParse->aNode[iRoot];
drh987eb1f2015-08-17 15:17:37 +0000957 if( zPath[0]==0 ) return pRoot;
958 if( zPath[0]=='.' ){
959 if( pRoot->eType!=JSON_OBJECT ) return 0;
960 zPath++;
drh6b43cc82015-08-19 23:02:49 +0000961 if( zPath[0]=='"' ){
962 zKey = zPath + 1;
963 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
964 nKey = i-1;
drha8f39a92015-09-21 22:53:16 +0000965 if( zPath[i] ){
966 i++;
967 }else{
968 *pzErr = zPath;
969 return 0;
970 }
drh6b43cc82015-08-19 23:02:49 +0000971 }else{
972 zKey = zPath;
973 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
974 nKey = i;
975 }
drha7714022015-08-29 00:54:49 +0000976 if( nKey==0 ){
977 *pzErr = zPath;
978 return 0;
979 }
drh987eb1f2015-08-17 15:17:37 +0000980 j = 1;
drh52216ad2015-08-18 02:28:03 +0000981 for(;;){
982 while( j<=pRoot->n ){
drh8cb0c832015-09-22 00:21:03 +0000983 if( jsonLabelCompare(pRoot+j, zKey, nKey) ){
drha7714022015-08-29 00:54:49 +0000984 return jsonLookupStep(pParse, iRoot+j+1, &zPath[i], pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +0000985 }
986 j++;
drh505ad2c2015-08-21 17:33:11 +0000987 j += jsonNodeSize(&pRoot[j]);
drh987eb1f2015-08-17 15:17:37 +0000988 }
drh52216ad2015-08-18 02:28:03 +0000989 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
990 iRoot += pRoot->u.iAppend;
991 pRoot = &pParse->aNode[iRoot];
992 j = 1;
993 }
994 if( pApnd ){
drhbc8f0922015-08-22 19:39:04 +0000995 u32 iStart, iLabel;
996 JsonNode *pNode;
997 iStart = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
998 iLabel = jsonParseAddNode(pParse, JSON_STRING, i, zPath);
drh52216ad2015-08-18 02:28:03 +0000999 zPath += i;
drha7714022015-08-29 00:54:49 +00001000 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001001 if( pParse->oom ) return 0;
1002 if( pNode ){
1003 pRoot = &pParse->aNode[iRoot];
1004 pRoot->u.iAppend = iStart - iRoot;
1005 pRoot->jnFlags |= JNODE_APPEND;
1006 pParse->aNode[iLabel].jnFlags |= JNODE_RAW;
1007 }
1008 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001009 }
dan2e8f5512015-09-17 17:21:09 +00001010 }else if( zPath[0]=='[' && safe_isdigit(zPath[1]) ){
drh987eb1f2015-08-17 15:17:37 +00001011 if( pRoot->eType!=JSON_ARRAY ) return 0;
1012 i = 0;
drh3d1d2a92015-09-22 01:15:49 +00001013 j = 1;
1014 while( safe_isdigit(zPath[j]) ){
1015 i = i*10 + zPath[j] - '0';
1016 j++;
drh987eb1f2015-08-17 15:17:37 +00001017 }
drh3d1d2a92015-09-22 01:15:49 +00001018 if( zPath[j]!=']' ){
drha7714022015-08-29 00:54:49 +00001019 *pzErr = zPath;
1020 return 0;
1021 }
drh3d1d2a92015-09-22 01:15:49 +00001022 zPath += j + 1;
drh987eb1f2015-08-17 15:17:37 +00001023 j = 1;
drh52216ad2015-08-18 02:28:03 +00001024 for(;;){
drhbc8f0922015-08-22 19:39:04 +00001025 while( j<=pRoot->n && (i>0 || (pRoot[j].jnFlags & JNODE_REMOVE)!=0) ){
1026 if( (pRoot[j].jnFlags & JNODE_REMOVE)==0 ) i--;
drh505ad2c2015-08-21 17:33:11 +00001027 j += jsonNodeSize(&pRoot[j]);
drh52216ad2015-08-18 02:28:03 +00001028 }
1029 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
1030 iRoot += pRoot->u.iAppend;
1031 pRoot = &pParse->aNode[iRoot];
1032 j = 1;
drh987eb1f2015-08-17 15:17:37 +00001033 }
1034 if( j<=pRoot->n ){
drha7714022015-08-29 00:54:49 +00001035 return jsonLookupStep(pParse, iRoot+j, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001036 }
1037 if( i==0 && pApnd ){
drhbc8f0922015-08-22 19:39:04 +00001038 u32 iStart;
1039 JsonNode *pNode;
1040 iStart = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
drha7714022015-08-29 00:54:49 +00001041 pNode = jsonLookupAppend(pParse, zPath, pApnd, pzErr);
drhbc8f0922015-08-22 19:39:04 +00001042 if( pParse->oom ) return 0;
1043 if( pNode ){
1044 pRoot = &pParse->aNode[iRoot];
1045 pRoot->u.iAppend = iStart - iRoot;
1046 pRoot->jnFlags |= JNODE_APPEND;
1047 }
1048 return pNode;
drh987eb1f2015-08-17 15:17:37 +00001049 }
drh3d1d2a92015-09-22 01:15:49 +00001050 }else{
drha7714022015-08-29 00:54:49 +00001051 *pzErr = zPath;
drh987eb1f2015-08-17 15:17:37 +00001052 }
1053 return 0;
1054}
1055
drh52216ad2015-08-18 02:28:03 +00001056/*
drhbc8f0922015-08-22 19:39:04 +00001057** Append content to pParse that will complete zPath. Return a pointer
1058** to the inserted node, or return NULL if the append fails.
drh52216ad2015-08-18 02:28:03 +00001059*/
1060static JsonNode *jsonLookupAppend(
1061 JsonParse *pParse, /* Append content to the JSON parse */
1062 const char *zPath, /* Description of content to append */
drha7714022015-08-29 00:54:49 +00001063 int *pApnd, /* Set this flag to 1 */
1064 const char **pzErr /* Make this point to any syntax error */
drh52216ad2015-08-18 02:28:03 +00001065){
1066 *pApnd = 1;
1067 if( zPath[0]==0 ){
1068 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
1069 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
1070 }
1071 if( zPath[0]=='.' ){
1072 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
1073 }else if( strncmp(zPath,"[0]",3)==0 ){
1074 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
1075 }else{
1076 return 0;
1077 }
1078 if( pParse->oom ) return 0;
drha7714022015-08-29 00:54:49 +00001079 return jsonLookupStep(pParse, pParse->nNode-1, zPath, pApnd, pzErr);
drh52216ad2015-08-18 02:28:03 +00001080}
1081
drhbc8f0922015-08-22 19:39:04 +00001082/*
drha7714022015-08-29 00:54:49 +00001083** Return the text of a syntax error message on a JSON path. Space is
1084** obtained from sqlite3_malloc().
1085*/
1086static char *jsonPathSyntaxError(const char *zErr){
1087 return sqlite3_mprintf("JSON path error near '%q'", zErr);
1088}
1089
1090/*
1091** Do a node lookup using zPath. Return a pointer to the node on success.
1092** Return NULL if not found or if there is an error.
1093**
1094** On an error, write an error message into pCtx and increment the
1095** pParse->nErr counter.
1096**
1097** If pApnd!=NULL then try to append missing nodes and set *pApnd = 1 if
1098** nodes are appended.
drha7714022015-08-29 00:54:49 +00001099*/
1100static JsonNode *jsonLookup(
1101 JsonParse *pParse, /* The JSON to search */
1102 const char *zPath, /* The path to search */
1103 int *pApnd, /* Append nodes to complete path if not NULL */
drhf5ddb9c2015-09-11 00:06:41 +00001104 sqlite3_context *pCtx /* Report errors here, if not NULL */
drha7714022015-08-29 00:54:49 +00001105){
1106 const char *zErr = 0;
1107 JsonNode *pNode = 0;
drha8f39a92015-09-21 22:53:16 +00001108 char *zMsg;
drha7714022015-08-29 00:54:49 +00001109
1110 if( zPath==0 ) return 0;
1111 if( zPath[0]!='$' ){
1112 zErr = zPath;
1113 goto lookup_err;
1114 }
1115 zPath++;
drha7714022015-08-29 00:54:49 +00001116 pNode = jsonLookupStep(pParse, 0, zPath, pApnd, &zErr);
drha8f39a92015-09-21 22:53:16 +00001117 if( zErr==0 ) return pNode;
drha7714022015-08-29 00:54:49 +00001118
1119lookup_err:
1120 pParse->nErr++;
drha8f39a92015-09-21 22:53:16 +00001121 assert( zErr!=0 && pCtx!=0 );
1122 zMsg = jsonPathSyntaxError(zErr);
1123 if( zMsg ){
1124 sqlite3_result_error(pCtx, zMsg, -1);
1125 sqlite3_free(zMsg);
1126 }else{
1127 sqlite3_result_error_nomem(pCtx);
drha7714022015-08-29 00:54:49 +00001128 }
drha7714022015-08-29 00:54:49 +00001129 return 0;
1130}
1131
1132
1133/*
drhbc8f0922015-08-22 19:39:04 +00001134** Report the wrong number of arguments for json_insert(), json_replace()
1135** or json_set().
1136*/
1137static void jsonWrongNumArgs(
1138 sqlite3_context *pCtx,
1139 const char *zFuncName
1140){
1141 char *zMsg = sqlite3_mprintf("json_%s() needs an odd number of arguments",
1142 zFuncName);
1143 sqlite3_result_error(pCtx, zMsg, -1);
1144 sqlite3_free(zMsg);
1145}
drh52216ad2015-08-18 02:28:03 +00001146
drha7714022015-08-29 00:54:49 +00001147
drh987eb1f2015-08-17 15:17:37 +00001148/****************************************************************************
1149** SQL functions used for testing and debugging
1150****************************************************************************/
drhe9c37f32015-08-15 21:25:36 +00001151
drh301eecc2015-08-17 20:14:19 +00001152#ifdef SQLITE_DEBUG
drhe9c37f32015-08-15 21:25:36 +00001153/*
drh5634cc02015-08-17 11:28:03 +00001154** The json_parse(JSON) function returns a string which describes
drhe9c37f32015-08-15 21:25:36 +00001155** a parse of the JSON provided. Or it returns NULL if JSON is not
1156** well-formed.
1157*/
drh5634cc02015-08-17 11:28:03 +00001158static void jsonParseFunc(
drhbc8f0922015-08-22 19:39:04 +00001159 sqlite3_context *ctx,
drhe9c37f32015-08-15 21:25:36 +00001160 int argc,
1161 sqlite3_value **argv
1162){
drh505ad2c2015-08-21 17:33:11 +00001163 JsonString s; /* Output string - not real JSON */
1164 JsonParse x; /* The parse */
drhe9c37f32015-08-15 21:25:36 +00001165 u32 i;
drhe9c37f32015-08-15 21:25:36 +00001166
1167 assert( argc==1 );
drhbc8f0922015-08-22 19:39:04 +00001168 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh8784eca2015-08-23 02:42:30 +00001169 jsonParseFindParents(&x);
drhbc8f0922015-08-22 19:39:04 +00001170 jsonInit(&s, ctx);
drhe9c37f32015-08-15 21:25:36 +00001171 for(i=0; i<x.nNode; i++){
drh852944e2015-09-10 03:29:11 +00001172 const char *zType;
1173 if( x.aNode[i].jnFlags & JNODE_LABEL ){
1174 assert( x.aNode[i].eType==JSON_STRING );
1175 zType = "label";
1176 }else{
1177 zType = jsonType[x.aNode[i].eType];
drhe9c37f32015-08-15 21:25:36 +00001178 }
drh852944e2015-09-10 03:29:11 +00001179 jsonPrintf(100, &s,"node %3u: %7s n=%-4d up=%-4d",
1180 i, zType, x.aNode[i].n, x.aUp[i]);
1181 if( x.aNode[i].u.zJContent!=0 ){
1182 jsonAppendRaw(&s, " ", 1);
1183 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
1184 }
1185 jsonAppendRaw(&s, "\n", 1);
drhe9c37f32015-08-15 21:25:36 +00001186 }
drh505ad2c2015-08-21 17:33:11 +00001187 jsonParseReset(&x);
drhe9c37f32015-08-15 21:25:36 +00001188 jsonResult(&s);
1189}
1190
drh5634cc02015-08-17 11:28:03 +00001191/*
drhf5ddb9c2015-09-11 00:06:41 +00001192** The json_test1(JSON) function return true (1) if the input is JSON
1193** text generated by another json function. It returns (0) if the input
1194** is not known to be JSON.
drh5634cc02015-08-17 11:28:03 +00001195*/
1196static void jsonTest1Func(
drhbc8f0922015-08-22 19:39:04 +00001197 sqlite3_context *ctx,
drh5634cc02015-08-17 11:28:03 +00001198 int argc,
1199 sqlite3_value **argv
1200){
mistachkin16a93122015-09-11 18:05:01 +00001201 UNUSED_PARAM(argc);
drhf5ddb9c2015-09-11 00:06:41 +00001202 sqlite3_result_int(ctx, sqlite3_value_subtype(argv[0])==JSON_SUBTYPE);
drh5634cc02015-08-17 11:28:03 +00001203}
drh301eecc2015-08-17 20:14:19 +00001204#endif /* SQLITE_DEBUG */
drh5634cc02015-08-17 11:28:03 +00001205
drh987eb1f2015-08-17 15:17:37 +00001206/****************************************************************************
drhff135ae2015-12-30 01:07:02 +00001207** Scalar SQL function implementations
drh987eb1f2015-08-17 15:17:37 +00001208****************************************************************************/
1209
1210/*
1211** Implementation of the json_array(VALUE,...) function. Return a JSON
1212** array that contains all values given in arguments. Or if any argument
1213** is a BLOB, throw an error.
1214*/
1215static void jsonArrayFunc(
drhbc8f0922015-08-22 19:39:04 +00001216 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001217 int argc,
1218 sqlite3_value **argv
1219){
1220 int i;
drh505ad2c2015-08-21 17:33:11 +00001221 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001222
drhbc8f0922015-08-22 19:39:04 +00001223 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001224 jsonAppendChar(&jx, '[');
drh987eb1f2015-08-17 15:17:37 +00001225 for(i=0; i<argc; i++){
drhd0960592015-08-17 21:22:32 +00001226 jsonAppendSeparator(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001227 jsonAppendValue(&jx, argv[i]);
drh987eb1f2015-08-17 15:17:37 +00001228 }
drhd0960592015-08-17 21:22:32 +00001229 jsonAppendChar(&jx, ']');
drh987eb1f2015-08-17 15:17:37 +00001230 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001231 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001232}
1233
1234
1235/*
1236** json_array_length(JSON)
1237** json_array_length(JSON, PATH)
1238**
1239** Return the number of elements in the top-level JSON array.
1240** Return 0 if the input is not a well-formed JSON array.
1241*/
1242static void jsonArrayLengthFunc(
drhbc8f0922015-08-22 19:39:04 +00001243 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001244 int argc,
1245 sqlite3_value **argv
1246){
1247 JsonParse x; /* The parse */
1248 sqlite3_int64 n = 0;
1249 u32 i;
drha8f39a92015-09-21 22:53:16 +00001250 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001251
drhf2df7e72015-08-28 20:07:40 +00001252 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001253 assert( x.nNode );
1254 if( argc==2 ){
1255 const char *zPath = (const char*)sqlite3_value_text(argv[1]);
1256 pNode = jsonLookup(&x, zPath, 0, ctx);
1257 }else{
1258 pNode = x.aNode;
1259 }
1260 if( pNode==0 ){
1261 x.nErr = 1;
1262 }else if( pNode->eType==JSON_ARRAY ){
1263 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
1264 for(i=1; i<=pNode->n; n++){
1265 i += jsonNodeSize(&pNode[i]);
drh987eb1f2015-08-17 15:17:37 +00001266 }
drh987eb1f2015-08-17 15:17:37 +00001267 }
drha7714022015-08-29 00:54:49 +00001268 if( x.nErr==0 ) sqlite3_result_int64(ctx, n);
drhf6ec8d42015-08-28 03:48:04 +00001269 jsonParseReset(&x);
1270}
1271
1272/*
drh3ad93bb2015-08-29 19:41:45 +00001273** json_extract(JSON, PATH, ...)
drh987eb1f2015-08-17 15:17:37 +00001274**
drh3ad93bb2015-08-29 19:41:45 +00001275** Return the element described by PATH. Return NULL if there is no
1276** PATH element. If there are multiple PATHs, then return a JSON array
1277** with the result from each path. Throw an error if the JSON or any PATH
1278** is malformed.
drh987eb1f2015-08-17 15:17:37 +00001279*/
1280static void jsonExtractFunc(
drhbc8f0922015-08-22 19:39:04 +00001281 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001282 int argc,
1283 sqlite3_value **argv
1284){
1285 JsonParse x; /* The parse */
1286 JsonNode *pNode;
1287 const char *zPath;
drh3ad93bb2015-08-29 19:41:45 +00001288 JsonString jx;
1289 int i;
1290
1291 if( argc<2 ) return;
drhbc8f0922015-08-22 19:39:04 +00001292 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drh3ad93bb2015-08-29 19:41:45 +00001293 jsonInit(&jx, ctx);
1294 jsonAppendChar(&jx, '[');
1295 for(i=1; i<argc; i++){
1296 zPath = (const char*)sqlite3_value_text(argv[i]);
drhf5ddb9c2015-09-11 00:06:41 +00001297 pNode = jsonLookup(&x, zPath, 0, ctx);
drh3ad93bb2015-08-29 19:41:45 +00001298 if( x.nErr ) break;
1299 if( argc>2 ){
1300 jsonAppendSeparator(&jx);
1301 if( pNode ){
1302 jsonRenderNode(pNode, &jx, 0);
1303 }else{
1304 jsonAppendRaw(&jx, "null", 4);
1305 }
1306 }else if( pNode ){
1307 jsonReturn(pNode, ctx, 0);
1308 }
drh987eb1f2015-08-17 15:17:37 +00001309 }
drh3ad93bb2015-08-29 19:41:45 +00001310 if( argc>2 && i==argc ){
1311 jsonAppendChar(&jx, ']');
1312 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001313 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh3ad93bb2015-08-29 19:41:45 +00001314 }
1315 jsonReset(&jx);
drh505ad2c2015-08-21 17:33:11 +00001316 jsonParseReset(&x);
drh987eb1f2015-08-17 15:17:37 +00001317}
1318
1319/*
1320** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
1321** object that contains all name/value given in arguments. Or if any name
1322** is not a string or if any value is a BLOB, throw an error.
1323*/
1324static void jsonObjectFunc(
drhbc8f0922015-08-22 19:39:04 +00001325 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001326 int argc,
1327 sqlite3_value **argv
1328){
1329 int i;
drh505ad2c2015-08-21 17:33:11 +00001330 JsonString jx;
drh987eb1f2015-08-17 15:17:37 +00001331 const char *z;
1332 u32 n;
1333
1334 if( argc&1 ){
drhbc8f0922015-08-22 19:39:04 +00001335 sqlite3_result_error(ctx, "json_object() requires an even number "
drh987eb1f2015-08-17 15:17:37 +00001336 "of arguments", -1);
1337 return;
1338 }
drhbc8f0922015-08-22 19:39:04 +00001339 jsonInit(&jx, ctx);
drhd0960592015-08-17 21:22:32 +00001340 jsonAppendChar(&jx, '{');
drh987eb1f2015-08-17 15:17:37 +00001341 for(i=0; i<argc; i+=2){
drh987eb1f2015-08-17 15:17:37 +00001342 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
drhbc8f0922015-08-22 19:39:04 +00001343 sqlite3_result_error(ctx, "json_object() labels must be TEXT", -1);
drhdc384952015-09-19 18:54:39 +00001344 jsonReset(&jx);
drh987eb1f2015-08-17 15:17:37 +00001345 return;
1346 }
drhd0960592015-08-17 21:22:32 +00001347 jsonAppendSeparator(&jx);
drh987eb1f2015-08-17 15:17:37 +00001348 z = (const char*)sqlite3_value_text(argv[i]);
1349 n = (u32)sqlite3_value_bytes(argv[i]);
1350 jsonAppendString(&jx, z, n);
drhd0960592015-08-17 21:22:32 +00001351 jsonAppendChar(&jx, ':');
drhf5ddb9c2015-09-11 00:06:41 +00001352 jsonAppendValue(&jx, argv[i+1]);
drh987eb1f2015-08-17 15:17:37 +00001353 }
drhd0960592015-08-17 21:22:32 +00001354 jsonAppendChar(&jx, '}');
drh987eb1f2015-08-17 15:17:37 +00001355 jsonResult(&jx);
drhf5ddb9c2015-09-11 00:06:41 +00001356 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
drh987eb1f2015-08-17 15:17:37 +00001357}
1358
1359
1360/*
drh301eecc2015-08-17 20:14:19 +00001361** json_remove(JSON, PATH, ...)
1362**
drh3ad93bb2015-08-29 19:41:45 +00001363** Remove the named elements from JSON and return the result. malformed
1364** JSON or PATH arguments result in an error.
drh301eecc2015-08-17 20:14:19 +00001365*/
1366static void jsonRemoveFunc(
drhbc8f0922015-08-22 19:39:04 +00001367 sqlite3_context *ctx,
drh301eecc2015-08-17 20:14:19 +00001368 int argc,
1369 sqlite3_value **argv
1370){
1371 JsonParse x; /* The parse */
1372 JsonNode *pNode;
1373 const char *zPath;
1374 u32 i;
1375
1376 if( argc<1 ) return;
drhbc8f0922015-08-22 19:39:04 +00001377 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001378 assert( x.nNode );
1379 for(i=1; i<(u32)argc; i++){
1380 zPath = (const char*)sqlite3_value_text(argv[i]);
1381 if( zPath==0 ) goto remove_done;
1382 pNode = jsonLookup(&x, zPath, 0, ctx);
1383 if( x.nErr ) goto remove_done;
1384 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
1385 }
1386 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
1387 jsonReturnJson(x.aNode, ctx, 0);
drhd0960592015-08-17 21:22:32 +00001388 }
drha7714022015-08-29 00:54:49 +00001389remove_done:
drh505ad2c2015-08-21 17:33:11 +00001390 jsonParseReset(&x);
drhd0960592015-08-17 21:22:32 +00001391}
1392
1393/*
1394** json_replace(JSON, PATH, VALUE, ...)
1395**
1396** Replace the value at PATH with VALUE. If PATH does not already exist,
drh3ad93bb2015-08-29 19:41:45 +00001397** this routine is a no-op. If JSON or PATH is malformed, throw an error.
drhd0960592015-08-17 21:22:32 +00001398*/
1399static void jsonReplaceFunc(
drhbc8f0922015-08-22 19:39:04 +00001400 sqlite3_context *ctx,
drhd0960592015-08-17 21:22:32 +00001401 int argc,
1402 sqlite3_value **argv
1403){
1404 JsonParse x; /* The parse */
1405 JsonNode *pNode;
1406 const char *zPath;
1407 u32 i;
1408
1409 if( argc<1 ) return;
1410 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001411 jsonWrongNumArgs(ctx, "replace");
drhd0960592015-08-17 21:22:32 +00001412 return;
1413 }
drhbc8f0922015-08-22 19:39:04 +00001414 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001415 assert( x.nNode );
1416 for(i=1; i<(u32)argc; i+=2){
1417 zPath = (const char*)sqlite3_value_text(argv[i]);
1418 pNode = jsonLookup(&x, zPath, 0, ctx);
1419 if( x.nErr ) goto replace_err;
1420 if( pNode ){
1421 pNode->jnFlags |= (u8)JNODE_REPLACE;
1422 pNode->iVal = (u8)(i+1);
drhd0960592015-08-17 21:22:32 +00001423 }
drha8f39a92015-09-21 22:53:16 +00001424 }
1425 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
1426 sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
1427 }else{
1428 jsonReturnJson(x.aNode, ctx, argv);
drh301eecc2015-08-17 20:14:19 +00001429 }
drha7714022015-08-29 00:54:49 +00001430replace_err:
drh505ad2c2015-08-21 17:33:11 +00001431 jsonParseReset(&x);
drh301eecc2015-08-17 20:14:19 +00001432}
drh505ad2c2015-08-21 17:33:11 +00001433
drh52216ad2015-08-18 02:28:03 +00001434/*
1435** json_set(JSON, PATH, VALUE, ...)
1436**
1437** Set the value at PATH to VALUE. Create the PATH if it does not already
1438** exist. Overwrite existing values that do exist.
drh3ad93bb2015-08-29 19:41:45 +00001439** If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001440**
1441** json_insert(JSON, PATH, VALUE, ...)
1442**
1443** Create PATH and initialize it to VALUE. If PATH already exists, this
drh3ad93bb2015-08-29 19:41:45 +00001444** routine is a no-op. If JSON or PATH is malformed, throw an error.
drh52216ad2015-08-18 02:28:03 +00001445*/
1446static void jsonSetFunc(
drhbc8f0922015-08-22 19:39:04 +00001447 sqlite3_context *ctx,
drh52216ad2015-08-18 02:28:03 +00001448 int argc,
1449 sqlite3_value **argv
1450){
1451 JsonParse x; /* The parse */
1452 JsonNode *pNode;
1453 const char *zPath;
1454 u32 i;
1455 int bApnd;
drhbc8f0922015-08-22 19:39:04 +00001456 int bIsSet = *(int*)sqlite3_user_data(ctx);
drh52216ad2015-08-18 02:28:03 +00001457
1458 if( argc<1 ) return;
1459 if( (argc&1)==0 ) {
drhbc8f0922015-08-22 19:39:04 +00001460 jsonWrongNumArgs(ctx, bIsSet ? "set" : "insert");
drh52216ad2015-08-18 02:28:03 +00001461 return;
1462 }
drhbc8f0922015-08-22 19:39:04 +00001463 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001464 assert( x.nNode );
1465 for(i=1; i<(u32)argc; i+=2){
1466 zPath = (const char*)sqlite3_value_text(argv[i]);
1467 bApnd = 0;
1468 pNode = jsonLookup(&x, zPath, &bApnd, ctx);
1469 if( x.oom ){
1470 sqlite3_result_error_nomem(ctx);
1471 goto jsonSetDone;
1472 }else if( x.nErr ){
1473 goto jsonSetDone;
1474 }else if( pNode && (bApnd || bIsSet) ){
1475 pNode->jnFlags |= (u8)JNODE_REPLACE;
1476 pNode->iVal = (u8)(i+1);
drh52216ad2015-08-18 02:28:03 +00001477 }
drha8f39a92015-09-21 22:53:16 +00001478 }
1479 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
1480 sqlite3_result_value(ctx, argv[x.aNode[0].iVal]);
1481 }else{
1482 jsonReturnJson(x.aNode, ctx, argv);
drh52216ad2015-08-18 02:28:03 +00001483 }
drhbc8f0922015-08-22 19:39:04 +00001484jsonSetDone:
drh505ad2c2015-08-21 17:33:11 +00001485 jsonParseReset(&x);
drh52216ad2015-08-18 02:28:03 +00001486}
drh301eecc2015-08-17 20:14:19 +00001487
1488/*
drh987eb1f2015-08-17 15:17:37 +00001489** json_type(JSON)
1490** json_type(JSON, PATH)
1491**
drh3ad93bb2015-08-29 19:41:45 +00001492** Return the top-level "type" of a JSON string. Throw an error if
1493** either the JSON or PATH inputs are not well-formed.
drh987eb1f2015-08-17 15:17:37 +00001494*/
1495static void jsonTypeFunc(
drhbc8f0922015-08-22 19:39:04 +00001496 sqlite3_context *ctx,
drh987eb1f2015-08-17 15:17:37 +00001497 int argc,
1498 sqlite3_value **argv
1499){
1500 JsonParse x; /* The parse */
1501 const char *zPath;
drha8f39a92015-09-21 22:53:16 +00001502 JsonNode *pNode;
drh987eb1f2015-08-17 15:17:37 +00001503
drhbc8f0922015-08-22 19:39:04 +00001504 if( jsonParse(&x, ctx, (const char*)sqlite3_value_text(argv[0])) ) return;
drha8f39a92015-09-21 22:53:16 +00001505 assert( x.nNode );
1506 if( argc==2 ){
1507 zPath = (const char*)sqlite3_value_text(argv[1]);
1508 pNode = jsonLookup(&x, zPath, 0, ctx);
1509 }else{
1510 pNode = x.aNode;
1511 }
1512 if( pNode ){
1513 sqlite3_result_text(ctx, jsonType[pNode->eType], -1, SQLITE_STATIC);
drh987eb1f2015-08-17 15:17:37 +00001514 }
drh505ad2c2015-08-21 17:33:11 +00001515 jsonParseReset(&x);
drh987eb1f2015-08-17 15:17:37 +00001516}
drh5634cc02015-08-17 11:28:03 +00001517
drhbc8f0922015-08-22 19:39:04 +00001518/*
1519** json_valid(JSON)
1520**
drh3ad93bb2015-08-29 19:41:45 +00001521** Return 1 if JSON is a well-formed JSON string according to RFC-7159.
1522** Return 0 otherwise.
drhbc8f0922015-08-22 19:39:04 +00001523*/
1524static void jsonValidFunc(
1525 sqlite3_context *ctx,
1526 int argc,
1527 sqlite3_value **argv
1528){
1529 JsonParse x; /* The parse */
1530 int rc = 0;
1531
mistachkin16a93122015-09-11 18:05:01 +00001532 UNUSED_PARAM(argc);
drha8f39a92015-09-21 22:53:16 +00001533 if( jsonParse(&x, 0, (const char*)sqlite3_value_text(argv[0]))==0 ){
drhbc8f0922015-08-22 19:39:04 +00001534 rc = 1;
1535 }
1536 jsonParseReset(&x);
1537 sqlite3_result_int(ctx, rc);
1538}
1539
drhff135ae2015-12-30 01:07:02 +00001540
1541/****************************************************************************
1542** Aggregate SQL function implementations
1543****************************************************************************/
1544/*
1545** json_group_array(VALUE)
1546**
1547** Return a JSON array composed of all values in the aggregate.
1548*/
1549static void jsonArrayStep(
1550 sqlite3_context *ctx,
1551 int argc,
1552 sqlite3_value **argv
1553){
1554 JsonString *pStr;
1555 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
1556 if( pStr ){
1557 if( pStr->zBuf==0 ){
1558 jsonInit(pStr, ctx);
1559 jsonAppendChar(pStr, '[');
1560 }else{
1561 jsonAppendChar(pStr, ',');
1562 pStr->pCtx = ctx;
1563 }
1564 jsonAppendValue(pStr, argv[0]);
1565 }
1566}
1567static void jsonArrayFinal(sqlite3_context *ctx){
1568 JsonString *pStr;
1569 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
1570 if( pStr ){
1571 pStr->pCtx = ctx;
1572 jsonAppendChar(pStr, ']');
1573 if( pStr->bErr ){
drh4a642b62016-02-05 01:55:27 +00001574 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00001575 assert( pStr->bStatic );
drhff135ae2015-12-30 01:07:02 +00001576 }else{
1577 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
1578 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
1579 pStr->bStatic = 1;
1580 }
1581 }else{
1582 sqlite3_result_text(ctx, "[]", 2, SQLITE_STATIC);
1583 }
1584 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1585}
1586
1587/*
1588** json_group_obj(NAME,VALUE)
1589**
1590** Return a JSON object composed of all names and values in the aggregate.
1591*/
1592static void jsonObjectStep(
1593 sqlite3_context *ctx,
1594 int argc,
1595 sqlite3_value **argv
1596){
1597 JsonString *pStr;
1598 const char *z;
1599 u32 n;
1600 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
1601 if( pStr ){
1602 if( pStr->zBuf==0 ){
1603 jsonInit(pStr, ctx);
1604 jsonAppendChar(pStr, '{');
1605 }else{
1606 jsonAppendChar(pStr, ',');
1607 pStr->pCtx = ctx;
1608 }
1609 z = (const char*)sqlite3_value_text(argv[0]);
1610 n = (u32)sqlite3_value_bytes(argv[0]);
1611 jsonAppendString(pStr, z, n);
1612 jsonAppendChar(pStr, ':');
1613 jsonAppendValue(pStr, argv[1]);
1614 }
1615}
1616static void jsonObjectFinal(sqlite3_context *ctx){
1617 JsonString *pStr;
1618 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
1619 if( pStr ){
1620 jsonAppendChar(pStr, '}');
1621 if( pStr->bErr ){
drh4a642b62016-02-05 01:55:27 +00001622 if( pStr->bErr==0 ) sqlite3_result_error_nomem(ctx);
drh23079262016-01-01 00:15:59 +00001623 assert( pStr->bStatic );
drhff135ae2015-12-30 01:07:02 +00001624 }else{
1625 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
1626 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
1627 pStr->bStatic = 1;
1628 }
1629 }else{
1630 sqlite3_result_text(ctx, "{}", 2, SQLITE_STATIC);
1631 }
1632 sqlite3_result_subtype(ctx, JSON_SUBTYPE);
1633}
1634
1635
drhd2975922015-08-29 17:22:33 +00001636#ifndef SQLITE_OMIT_VIRTUALTABLE
drhcb6c6c62015-08-19 22:47:17 +00001637/****************************************************************************
1638** The json_each virtual table
1639****************************************************************************/
1640typedef struct JsonEachCursor JsonEachCursor;
1641struct JsonEachCursor {
1642 sqlite3_vtab_cursor base; /* Base class - must be first */
drh505ad2c2015-08-21 17:33:11 +00001643 u32 iRowid; /* The rowid */
drh852944e2015-09-10 03:29:11 +00001644 u32 iBegin; /* The first node of the scan */
drh505ad2c2015-08-21 17:33:11 +00001645 u32 i; /* Index in sParse.aNode[] of current row */
1646 u32 iEnd; /* EOF when i equals or exceeds this value */
1647 u8 eType; /* Type of top-level element */
1648 u8 bRecursive; /* True for json_tree(). False for json_each() */
1649 char *zJson; /* Input JSON */
drh383de692015-09-10 17:20:57 +00001650 char *zRoot; /* Path by which to filter zJson */
drh505ad2c2015-08-21 17:33:11 +00001651 JsonParse sParse; /* Parse of the input JSON */
drhcb6c6c62015-08-19 22:47:17 +00001652};
1653
1654/* Constructor for the json_each virtual table */
1655static int jsonEachConnect(
1656 sqlite3 *db,
1657 void *pAux,
1658 int argc, const char *const*argv,
1659 sqlite3_vtab **ppVtab,
1660 char **pzErr
1661){
1662 sqlite3_vtab *pNew;
drh505ad2c2015-08-21 17:33:11 +00001663 int rc;
drhcb6c6c62015-08-19 22:47:17 +00001664
1665/* Column numbers */
drh4af352d2015-08-21 20:02:48 +00001666#define JEACH_KEY 0
1667#define JEACH_VALUE 1
1668#define JEACH_TYPE 2
1669#define JEACH_ATOM 3
1670#define JEACH_ID 4
1671#define JEACH_PARENT 5
1672#define JEACH_FULLKEY 6
drh383de692015-09-10 17:20:57 +00001673#define JEACH_PATH 7
1674#define JEACH_JSON 8
1675#define JEACH_ROOT 9
drhcb6c6c62015-08-19 22:47:17 +00001676
drh6fd5c1e2015-08-21 20:37:12 +00001677 UNUSED_PARAM(pzErr);
1678 UNUSED_PARAM(argv);
1679 UNUSED_PARAM(argc);
1680 UNUSED_PARAM(pAux);
drh505ad2c2015-08-21 17:33:11 +00001681 rc = sqlite3_declare_vtab(db,
drh383de692015-09-10 17:20:57 +00001682 "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,path,"
1683 "json HIDDEN,root HIDDEN)");
drh505ad2c2015-08-21 17:33:11 +00001684 if( rc==SQLITE_OK ){
1685 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
1686 if( pNew==0 ) return SQLITE_NOMEM;
1687 memset(pNew, 0, sizeof(*pNew));
1688 }
1689 return rc;
drhcb6c6c62015-08-19 22:47:17 +00001690}
1691
1692/* destructor for json_each virtual table */
1693static int jsonEachDisconnect(sqlite3_vtab *pVtab){
1694 sqlite3_free(pVtab);
1695 return SQLITE_OK;
1696}
1697
drh505ad2c2015-08-21 17:33:11 +00001698/* constructor for a JsonEachCursor object for json_each(). */
1699static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
drhcb6c6c62015-08-19 22:47:17 +00001700 JsonEachCursor *pCur;
drh6fd5c1e2015-08-21 20:37:12 +00001701
1702 UNUSED_PARAM(p);
drhcb6c6c62015-08-19 22:47:17 +00001703 pCur = sqlite3_malloc( sizeof(*pCur) );
1704 if( pCur==0 ) return SQLITE_NOMEM;
1705 memset(pCur, 0, sizeof(*pCur));
1706 *ppCursor = &pCur->base;
1707 return SQLITE_OK;
1708}
1709
drh505ad2c2015-08-21 17:33:11 +00001710/* constructor for a JsonEachCursor object for json_tree(). */
1711static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
1712 int rc = jsonEachOpenEach(p, ppCursor);
1713 if( rc==SQLITE_OK ){
1714 JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
1715 pCur->bRecursive = 1;
1716 }
1717 return rc;
1718}
1719
drhcb6c6c62015-08-19 22:47:17 +00001720/* Reset a JsonEachCursor back to its original state. Free any memory
1721** held. */
1722static void jsonEachCursorReset(JsonEachCursor *p){
1723 sqlite3_free(p->zJson);
drh383de692015-09-10 17:20:57 +00001724 sqlite3_free(p->zRoot);
drh505ad2c2015-08-21 17:33:11 +00001725 jsonParseReset(&p->sParse);
drhcb6c6c62015-08-19 22:47:17 +00001726 p->iRowid = 0;
1727 p->i = 0;
1728 p->iEnd = 0;
1729 p->eType = 0;
drhcb6c6c62015-08-19 22:47:17 +00001730 p->zJson = 0;
drh383de692015-09-10 17:20:57 +00001731 p->zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00001732}
1733
1734/* Destructor for a jsonEachCursor object */
1735static int jsonEachClose(sqlite3_vtab_cursor *cur){
1736 JsonEachCursor *p = (JsonEachCursor*)cur;
1737 jsonEachCursorReset(p);
1738 sqlite3_free(cur);
1739 return SQLITE_OK;
1740}
1741
1742/* Return TRUE if the jsonEachCursor object has been advanced off the end
1743** of the JSON object */
1744static int jsonEachEof(sqlite3_vtab_cursor *cur){
1745 JsonEachCursor *p = (JsonEachCursor*)cur;
1746 return p->i >= p->iEnd;
1747}
1748
drh505ad2c2015-08-21 17:33:11 +00001749/* Advance the cursor to the next element for json_tree() */
drh4af352d2015-08-21 20:02:48 +00001750static int jsonEachNext(sqlite3_vtab_cursor *cur){
drh505ad2c2015-08-21 17:33:11 +00001751 JsonEachCursor *p = (JsonEachCursor*)cur;
drh4af352d2015-08-21 20:02:48 +00001752 if( p->bRecursive ){
drh852944e2015-09-10 03:29:11 +00001753 if( p->sParse.aNode[p->i].jnFlags & JNODE_LABEL ) p->i++;
1754 p->i++;
drh4af352d2015-08-21 20:02:48 +00001755 p->iRowid++;
drh852944e2015-09-10 03:29:11 +00001756 if( p->i<p->iEnd ){
drh8784eca2015-08-23 02:42:30 +00001757 u32 iUp = p->sParse.aUp[p->i];
1758 JsonNode *pUp = &p->sParse.aNode[iUp];
drh4af352d2015-08-21 20:02:48 +00001759 p->eType = pUp->eType;
drh8784eca2015-08-23 02:42:30 +00001760 if( pUp->eType==JSON_ARRAY ){
1761 if( iUp==p->i-1 ){
1762 pUp->u.iKey = 0;
1763 }else{
1764 pUp->u.iKey++;
1765 }
drh4af352d2015-08-21 20:02:48 +00001766 }
1767 }
drh505ad2c2015-08-21 17:33:11 +00001768 }else{
drh4af352d2015-08-21 20:02:48 +00001769 switch( p->eType ){
1770 case JSON_ARRAY: {
1771 p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
1772 p->iRowid++;
1773 break;
1774 }
1775 case JSON_OBJECT: {
1776 p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
1777 p->iRowid++;
1778 break;
1779 }
1780 default: {
1781 p->i = p->iEnd;
1782 break;
1783 }
drh505ad2c2015-08-21 17:33:11 +00001784 }
1785 }
1786 return SQLITE_OK;
1787}
1788
drh4af352d2015-08-21 20:02:48 +00001789/* Append the name of the path for element i to pStr
1790*/
1791static void jsonEachComputePath(
1792 JsonEachCursor *p, /* The cursor */
1793 JsonString *pStr, /* Write the path here */
1794 u32 i /* Path to this element */
1795){
1796 JsonNode *pNode, *pUp;
1797 u32 iUp;
1798 if( i==0 ){
1799 jsonAppendChar(pStr, '$');
1800 return;
drhcb6c6c62015-08-19 22:47:17 +00001801 }
drh4af352d2015-08-21 20:02:48 +00001802 iUp = p->sParse.aUp[i];
1803 jsonEachComputePath(p, pStr, iUp);
1804 pNode = &p->sParse.aNode[i];
1805 pUp = &p->sParse.aNode[iUp];
1806 if( pUp->eType==JSON_ARRAY ){
1807 jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
1808 }else{
1809 assert( pUp->eType==JSON_OBJECT );
drh852944e2015-09-10 03:29:11 +00001810 if( (pNode->jnFlags & JNODE_LABEL)==0 ) pNode--;
drh4af352d2015-08-21 20:02:48 +00001811 assert( pNode->eType==JSON_STRING );
drh852944e2015-09-10 03:29:11 +00001812 assert( pNode->jnFlags & JNODE_LABEL );
drh4af352d2015-08-21 20:02:48 +00001813 jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
1814 }
drhcb6c6c62015-08-19 22:47:17 +00001815}
1816
1817/* Return the value of a column */
1818static int jsonEachColumn(
1819 sqlite3_vtab_cursor *cur, /* The cursor */
1820 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
1821 int i /* Which column to return */
1822){
1823 JsonEachCursor *p = (JsonEachCursor*)cur;
drh505ad2c2015-08-21 17:33:11 +00001824 JsonNode *pThis = &p->sParse.aNode[p->i];
drhcb6c6c62015-08-19 22:47:17 +00001825 switch( i ){
1826 case JEACH_KEY: {
drh8784eca2015-08-23 02:42:30 +00001827 if( p->i==0 ) break;
drhcb6c6c62015-08-19 22:47:17 +00001828 if( p->eType==JSON_OBJECT ){
drh505ad2c2015-08-21 17:33:11 +00001829 jsonReturn(pThis, ctx, 0);
1830 }else if( p->eType==JSON_ARRAY ){
1831 u32 iKey;
1832 if( p->bRecursive ){
1833 if( p->iRowid==0 ) break;
drh8784eca2015-08-23 02:42:30 +00001834 iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey;
drh505ad2c2015-08-21 17:33:11 +00001835 }else{
1836 iKey = p->iRowid;
1837 }
drh6fd5c1e2015-08-21 20:37:12 +00001838 sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
drhcb6c6c62015-08-19 22:47:17 +00001839 }
1840 break;
1841 }
1842 case JEACH_VALUE: {
drh852944e2015-09-10 03:29:11 +00001843 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00001844 jsonReturn(pThis, ctx, 0);
1845 break;
1846 }
1847 case JEACH_TYPE: {
drh852944e2015-09-10 03:29:11 +00001848 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00001849 sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
1850 break;
1851 }
1852 case JEACH_ATOM: {
drh852944e2015-09-10 03:29:11 +00001853 if( pThis->jnFlags & JNODE_LABEL ) pThis++;
drh505ad2c2015-08-21 17:33:11 +00001854 if( pThis->eType>=JSON_ARRAY ) break;
1855 jsonReturn(pThis, ctx, 0);
1856 break;
1857 }
1858 case JEACH_ID: {
drh852944e2015-09-10 03:29:11 +00001859 sqlite3_result_int64(ctx,
1860 (sqlite3_int64)p->i + ((pThis->jnFlags & JNODE_LABEL)!=0));
drh505ad2c2015-08-21 17:33:11 +00001861 break;
1862 }
1863 case JEACH_PARENT: {
drh852944e2015-09-10 03:29:11 +00001864 if( p->i>p->iBegin && p->bRecursive ){
drh6fd5c1e2015-08-21 20:37:12 +00001865 sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
drhcb6c6c62015-08-19 22:47:17 +00001866 }
1867 break;
1868 }
drh4af352d2015-08-21 20:02:48 +00001869 case JEACH_FULLKEY: {
1870 JsonString x;
1871 jsonInit(&x, ctx);
1872 if( p->bRecursive ){
1873 jsonEachComputePath(p, &x, p->i);
1874 }else{
drh383de692015-09-10 17:20:57 +00001875 if( p->zRoot ){
1876 jsonAppendRaw(&x, p->zRoot, (int)strlen(p->zRoot));
drh4af352d2015-08-21 20:02:48 +00001877 }else{
1878 jsonAppendChar(&x, '$');
1879 }
1880 if( p->eType==JSON_ARRAY ){
1881 jsonPrintf(30, &x, "[%d]", p->iRowid);
1882 }else{
1883 jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
1884 }
1885 }
1886 jsonResult(&x);
1887 break;
1888 }
drhcb6c6c62015-08-19 22:47:17 +00001889 case JEACH_PATH: {
drh383de692015-09-10 17:20:57 +00001890 if( p->bRecursive ){
1891 JsonString x;
1892 jsonInit(&x, ctx);
1893 jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
1894 jsonResult(&x);
1895 break;
drh4af352d2015-08-21 20:02:48 +00001896 }
drh383de692015-09-10 17:20:57 +00001897 /* For json_each() path and root are the same so fall through
1898 ** into the root case */
1899 }
1900 case JEACH_ROOT: {
1901 const char *zRoot = p->zRoot;
1902 if( zRoot==0 ) zRoot = "$";
1903 sqlite3_result_text(ctx, zRoot, -1, SQLITE_STATIC);
drhcb6c6c62015-08-19 22:47:17 +00001904 break;
1905 }
drh3d1d2a92015-09-22 01:15:49 +00001906 case JEACH_JSON: {
drh505ad2c2015-08-21 17:33:11 +00001907 assert( i==JEACH_JSON );
drhcb6c6c62015-08-19 22:47:17 +00001908 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
1909 break;
1910 }
1911 }
1912 return SQLITE_OK;
1913}
1914
1915/* Return the current rowid value */
1916static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
1917 JsonEachCursor *p = (JsonEachCursor*)cur;
1918 *pRowid = p->iRowid;
1919 return SQLITE_OK;
1920}
1921
1922/* The query strategy is to look for an equality constraint on the json
1923** column. Without such a constraint, the table cannot operate. idxNum is
drh383de692015-09-10 17:20:57 +00001924** 1 if the constraint is found, 3 if the constraint and zRoot are found,
drhcb6c6c62015-08-19 22:47:17 +00001925** and 0 otherwise.
1926*/
1927static int jsonEachBestIndex(
1928 sqlite3_vtab *tab,
1929 sqlite3_index_info *pIdxInfo
1930){
1931 int i;
1932 int jsonIdx = -1;
drh383de692015-09-10 17:20:57 +00001933 int rootIdx = -1;
drhcb6c6c62015-08-19 22:47:17 +00001934 const struct sqlite3_index_constraint *pConstraint;
drh6fd5c1e2015-08-21 20:37:12 +00001935
1936 UNUSED_PARAM(tab);
drhcb6c6c62015-08-19 22:47:17 +00001937 pConstraint = pIdxInfo->aConstraint;
1938 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
1939 if( pConstraint->usable==0 ) continue;
1940 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
1941 switch( pConstraint->iColumn ){
1942 case JEACH_JSON: jsonIdx = i; break;
drh383de692015-09-10 17:20:57 +00001943 case JEACH_ROOT: rootIdx = i; break;
drhcb6c6c62015-08-19 22:47:17 +00001944 default: /* no-op */ break;
1945 }
1946 }
1947 if( jsonIdx<0 ){
1948 pIdxInfo->idxNum = 0;
drh505ad2c2015-08-21 17:33:11 +00001949 pIdxInfo->estimatedCost = 1e99;
drhcb6c6c62015-08-19 22:47:17 +00001950 }else{
drh505ad2c2015-08-21 17:33:11 +00001951 pIdxInfo->estimatedCost = 1.0;
drhcb6c6c62015-08-19 22:47:17 +00001952 pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1;
1953 pIdxInfo->aConstraintUsage[jsonIdx].omit = 1;
drh383de692015-09-10 17:20:57 +00001954 if( rootIdx<0 ){
drhcb6c6c62015-08-19 22:47:17 +00001955 pIdxInfo->idxNum = 1;
1956 }else{
drh383de692015-09-10 17:20:57 +00001957 pIdxInfo->aConstraintUsage[rootIdx].argvIndex = 2;
1958 pIdxInfo->aConstraintUsage[rootIdx].omit = 1;
drhcb6c6c62015-08-19 22:47:17 +00001959 pIdxInfo->idxNum = 3;
1960 }
1961 }
1962 return SQLITE_OK;
1963}
1964
1965/* Start a search on a new JSON string */
1966static int jsonEachFilter(
1967 sqlite3_vtab_cursor *cur,
1968 int idxNum, const char *idxStr,
1969 int argc, sqlite3_value **argv
1970){
1971 JsonEachCursor *p = (JsonEachCursor*)cur;
1972 const char *z;
mistachkin16a93122015-09-11 18:05:01 +00001973 const char *zRoot = 0;
drhcb6c6c62015-08-19 22:47:17 +00001974 sqlite3_int64 n;
1975
drh6fd5c1e2015-08-21 20:37:12 +00001976 UNUSED_PARAM(idxStr);
1977 UNUSED_PARAM(argc);
drhcb6c6c62015-08-19 22:47:17 +00001978 jsonEachCursorReset(p);
1979 if( idxNum==0 ) return SQLITE_OK;
1980 z = (const char*)sqlite3_value_text(argv[0]);
1981 if( z==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00001982 n = sqlite3_value_bytes(argv[0]);
drh6fd5c1e2015-08-21 20:37:12 +00001983 p->zJson = sqlite3_malloc64( n+1 );
drhcb6c6c62015-08-19 22:47:17 +00001984 if( p->zJson==0 ) return SQLITE_NOMEM;
drh6fd5c1e2015-08-21 20:37:12 +00001985 memcpy(p->zJson, z, (size_t)n+1);
drha7714022015-08-29 00:54:49 +00001986 if( jsonParse(&p->sParse, 0, p->zJson) ){
1987 int rc = SQLITE_NOMEM;
1988 if( p->sParse.oom==0 ){
1989 sqlite3_free(cur->pVtab->zErrMsg);
1990 cur->pVtab->zErrMsg = sqlite3_mprintf("malformed JSON");
1991 if( cur->pVtab->zErrMsg ) rc = SQLITE_ERROR;
1992 }
drhcb6c6c62015-08-19 22:47:17 +00001993 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00001994 return rc;
1995 }else if( p->bRecursive && jsonParseFindParents(&p->sParse) ){
1996 jsonEachCursorReset(p);
1997 return SQLITE_NOMEM;
drhcb6c6c62015-08-19 22:47:17 +00001998 }else{
drh95677942015-09-24 01:06:37 +00001999 JsonNode *pNode = 0;
drhcb6c6c62015-08-19 22:47:17 +00002000 if( idxNum==3 ){
drha7714022015-08-29 00:54:49 +00002001 const char *zErr = 0;
drha8f39a92015-09-21 22:53:16 +00002002 zRoot = (const char*)sqlite3_value_text(argv[1]);
2003 if( zRoot==0 ) return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002004 n = sqlite3_value_bytes(argv[1]);
drh383de692015-09-10 17:20:57 +00002005 p->zRoot = sqlite3_malloc64( n+1 );
2006 if( p->zRoot==0 ) return SQLITE_NOMEM;
2007 memcpy(p->zRoot, zRoot, (size_t)n+1);
drha8f39a92015-09-21 22:53:16 +00002008 if( zRoot[0]!='$' ){
2009 zErr = zRoot;
2010 }else{
2011 pNode = jsonLookupStep(&p->sParse, 0, p->zRoot+1, 0, &zErr);
2012 }
2013 if( zErr ){
drha7714022015-08-29 00:54:49 +00002014 sqlite3_free(cur->pVtab->zErrMsg);
2015 cur->pVtab->zErrMsg = jsonPathSyntaxError(zErr);
drhcb6c6c62015-08-19 22:47:17 +00002016 jsonEachCursorReset(p);
drha7714022015-08-29 00:54:49 +00002017 return cur->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
2018 }else if( pNode==0 ){
drhcb6c6c62015-08-19 22:47:17 +00002019 return SQLITE_OK;
2020 }
2021 }else{
2022 pNode = p->sParse.aNode;
2023 }
drh852944e2015-09-10 03:29:11 +00002024 p->iBegin = p->i = (int)(pNode - p->sParse.aNode);
drhcb6c6c62015-08-19 22:47:17 +00002025 p->eType = pNode->eType;
2026 if( p->eType>=JSON_ARRAY ){
drh8784eca2015-08-23 02:42:30 +00002027 pNode->u.iKey = 0;
drhc3722b22015-08-23 20:44:59 +00002028 p->iEnd = p->i + pNode->n + 1;
drh852944e2015-09-10 03:29:11 +00002029 if( p->bRecursive ){
drh3d1d2a92015-09-22 01:15:49 +00002030 p->eType = p->sParse.aNode[p->sParse.aUp[p->i]].eType;
drh852944e2015-09-10 03:29:11 +00002031 if( p->i>0 && (p->sParse.aNode[p->i-1].jnFlags & JNODE_LABEL)!=0 ){
2032 p->i--;
2033 }
2034 }else{
2035 p->i++;
2036 }
drhcb6c6c62015-08-19 22:47:17 +00002037 }else{
2038 p->iEnd = p->i+1;
2039 }
2040 }
drha8f39a92015-09-21 22:53:16 +00002041 return SQLITE_OK;
drhcb6c6c62015-08-19 22:47:17 +00002042}
2043
2044/* The methods of the json_each virtual table */
2045static sqlite3_module jsonEachModule = {
2046 0, /* iVersion */
2047 0, /* xCreate */
2048 jsonEachConnect, /* xConnect */
2049 jsonEachBestIndex, /* xBestIndex */
2050 jsonEachDisconnect, /* xDisconnect */
2051 0, /* xDestroy */
drh505ad2c2015-08-21 17:33:11 +00002052 jsonEachOpenEach, /* xOpen - open a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002053 jsonEachClose, /* xClose - close a cursor */
2054 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002055 jsonEachNext, /* xNext - advance a cursor */
drhcb6c6c62015-08-19 22:47:17 +00002056 jsonEachEof, /* xEof - check for end of scan */
2057 jsonEachColumn, /* xColumn - read data */
2058 jsonEachRowid, /* xRowid - read data */
2059 0, /* xUpdate */
2060 0, /* xBegin */
2061 0, /* xSync */
2062 0, /* xCommit */
2063 0, /* xRollback */
2064 0, /* xFindMethod */
2065 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002066 0, /* xSavepoint */
2067 0, /* xRelease */
2068 0 /* xRollbackTo */
drhcb6c6c62015-08-19 22:47:17 +00002069};
2070
drh505ad2c2015-08-21 17:33:11 +00002071/* The methods of the json_tree virtual table. */
2072static sqlite3_module jsonTreeModule = {
2073 0, /* iVersion */
2074 0, /* xCreate */
2075 jsonEachConnect, /* xConnect */
2076 jsonEachBestIndex, /* xBestIndex */
2077 jsonEachDisconnect, /* xDisconnect */
2078 0, /* xDestroy */
2079 jsonEachOpenTree, /* xOpen - open a cursor */
2080 jsonEachClose, /* xClose - close a cursor */
2081 jsonEachFilter, /* xFilter - configure scan constraints */
drh4af352d2015-08-21 20:02:48 +00002082 jsonEachNext, /* xNext - advance a cursor */
drh505ad2c2015-08-21 17:33:11 +00002083 jsonEachEof, /* xEof - check for end of scan */
2084 jsonEachColumn, /* xColumn - read data */
2085 jsonEachRowid, /* xRowid - read data */
2086 0, /* xUpdate */
2087 0, /* xBegin */
2088 0, /* xSync */
2089 0, /* xCommit */
2090 0, /* xRollback */
2091 0, /* xFindMethod */
2092 0, /* xRename */
drh6fd5c1e2015-08-21 20:37:12 +00002093 0, /* xSavepoint */
2094 0, /* xRelease */
2095 0 /* xRollbackTo */
drh505ad2c2015-08-21 17:33:11 +00002096};
drhd2975922015-08-29 17:22:33 +00002097#endif /* SQLITE_OMIT_VIRTUALTABLE */
drh505ad2c2015-08-21 17:33:11 +00002098
2099/****************************************************************************
drh2f20e132015-09-26 17:44:59 +00002100** The following routines are the only publically visible identifiers in this
2101** file. Call the following routines in order to register the various SQL
drh505ad2c2015-08-21 17:33:11 +00002102** functions and the virtual table implemented by this file.
2103****************************************************************************/
drhcb6c6c62015-08-19 22:47:17 +00002104
drh2f20e132015-09-26 17:44:59 +00002105int sqlite3Json1Init(sqlite3 *db){
drh5fa5c102015-08-12 16:49:40 +00002106 int rc = SQLITE_OK;
drh6fd5c1e2015-08-21 20:37:12 +00002107 unsigned int i;
drh5fa5c102015-08-12 16:49:40 +00002108 static const struct {
2109 const char *zName;
2110 int nArg;
drh52216ad2015-08-18 02:28:03 +00002111 int flag;
drh5fa5c102015-08-12 16:49:40 +00002112 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
2113 } aFunc[] = {
drhf5ddb9c2015-09-11 00:06:41 +00002114 { "json", 1, 0, jsonRemoveFunc },
drh52216ad2015-08-18 02:28:03 +00002115 { "json_array", -1, 0, jsonArrayFunc },
2116 { "json_array_length", 1, 0, jsonArrayLengthFunc },
2117 { "json_array_length", 2, 0, jsonArrayLengthFunc },
drh3ad93bb2015-08-29 19:41:45 +00002118 { "json_extract", -1, 0, jsonExtractFunc },
drh52216ad2015-08-18 02:28:03 +00002119 { "json_insert", -1, 0, jsonSetFunc },
2120 { "json_object", -1, 0, jsonObjectFunc },
2121 { "json_remove", -1, 0, jsonRemoveFunc },
2122 { "json_replace", -1, 0, jsonReplaceFunc },
2123 { "json_set", -1, 1, jsonSetFunc },
2124 { "json_type", 1, 0, jsonTypeFunc },
2125 { "json_type", 2, 0, jsonTypeFunc },
drhbc8f0922015-08-22 19:39:04 +00002126 { "json_valid", 1, 0, jsonValidFunc },
drh987eb1f2015-08-17 15:17:37 +00002127
drh301eecc2015-08-17 20:14:19 +00002128#if SQLITE_DEBUG
drh987eb1f2015-08-17 15:17:37 +00002129 /* DEBUG and TESTING functions */
drh52216ad2015-08-18 02:28:03 +00002130 { "json_parse", 1, 0, jsonParseFunc },
2131 { "json_test1", 1, 0, jsonTest1Func },
drh301eecc2015-08-17 20:14:19 +00002132#endif
drh5fa5c102015-08-12 16:49:40 +00002133 };
drhff135ae2015-12-30 01:07:02 +00002134 static const struct {
2135 const char *zName;
2136 int nArg;
2137 void (*xStep)(sqlite3_context*,int,sqlite3_value**);
2138 void (*xFinal)(sqlite3_context*);
2139 } aAgg[] = {
2140 { "json_group_array", 1, jsonArrayStep, jsonArrayFinal },
2141 { "json_group_object", 2, jsonObjectStep, jsonObjectFinal },
2142 };
drhd2975922015-08-29 17:22:33 +00002143#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002144 static const struct {
2145 const char *zName;
2146 sqlite3_module *pModule;
2147 } aMod[] = {
2148 { "json_each", &jsonEachModule },
2149 { "json_tree", &jsonTreeModule },
2150 };
drhd2975922015-08-29 17:22:33 +00002151#endif
drh5fa5c102015-08-12 16:49:40 +00002152 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
2153 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
drh52216ad2015-08-18 02:28:03 +00002154 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
2155 (void*)&aFunc[i].flag,
drh5fa5c102015-08-12 16:49:40 +00002156 aFunc[i].xFunc, 0, 0);
2157 }
drhff135ae2015-12-30 01:07:02 +00002158 for(i=0; i<sizeof(aAgg)/sizeof(aAgg[0]) && rc==SQLITE_OK; i++){
2159 rc = sqlite3_create_function(db, aAgg[i].zName, aAgg[i].nArg,
2160 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
2161 0, aAgg[i].xStep, aAgg[i].xFinal);
2162 }
drhd2975922015-08-29 17:22:33 +00002163#ifndef SQLITE_OMIT_VIRTUALTABLE
drh505ad2c2015-08-21 17:33:11 +00002164 for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
2165 rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
drhcb6c6c62015-08-19 22:47:17 +00002166 }
drhd2975922015-08-29 17:22:33 +00002167#endif
drh5fa5c102015-08-12 16:49:40 +00002168 return rc;
2169}
drh2f20e132015-09-26 17:44:59 +00002170
2171
dan8d32e802015-10-14 18:45:42 +00002172#ifndef SQLITE_CORE
drh2f20e132015-09-26 17:44:59 +00002173#ifdef _WIN32
2174__declspec(dllexport)
2175#endif
2176int sqlite3_json_init(
2177 sqlite3 *db,
2178 char **pzErrMsg,
2179 const sqlite3_api_routines *pApi
2180){
2181 SQLITE_EXTENSION_INIT2(pApi);
2182 (void)pzErrMsg; /* Unused parameter */
2183 return sqlite3Json1Init(db);
2184}
dan8d32e802015-10-14 18:45:42 +00002185#endif
drh50065652015-10-08 19:29:18 +00002186#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_JSON1) */