blob: 77bc6d67941977aefcc4eaf8d5d531aefccb4fc0 [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*/
24#include "sqlite3ext.h"
25SQLITE_EXTENSION_INIT1
26#include <assert.h>
27#include <string.h>
drhe9c37f32015-08-15 21:25:36 +000028#include <ctype.h>
drh987eb1f2015-08-17 15:17:37 +000029#include <stdlib.h>
drh5fa5c102015-08-12 16:49:40 +000030
31/* Unsigned integer types */
32typedef sqlite3_uint64 u64;
33typedef unsigned int u32;
34typedef unsigned char u8;
35
drh52216ad2015-08-18 02:28:03 +000036/* Objects */
37typedef struct Json Json;
38typedef struct JsonNode JsonNode;
39typedef struct JsonParse JsonParse;
40
drh5634cc02015-08-17 11:28:03 +000041/* An instance of this object represents a JSON string
42** under construction. Really, this is a generic string accumulator
43** that can be and is used to create strings other than JSON.
drh5fa5c102015-08-12 16:49:40 +000044*/
drh5fa5c102015-08-12 16:49:40 +000045struct Json {
46 sqlite3_context *pCtx; /* Function context - put error messages here */
drh5634cc02015-08-17 11:28:03 +000047 char *zBuf; /* Append JSON content here */
drh5fa5c102015-08-12 16:49:40 +000048 u64 nAlloc; /* Bytes of storage available in zBuf[] */
49 u64 nUsed; /* Bytes of zBuf[] currently used */
50 u8 bStatic; /* True if zBuf is static space */
drhd0960592015-08-17 21:22:32 +000051 u8 bErr; /* True if an error has been encountered */
drh5fa5c102015-08-12 16:49:40 +000052 char zSpace[100]; /* Initial static space */
53};
54
drhe9c37f32015-08-15 21:25:36 +000055/* JSON type values
drhbd0621b2015-08-13 13:54:59 +000056*/
drhe9c37f32015-08-15 21:25:36 +000057#define JSON_NULL 0
58#define JSON_TRUE 1
59#define JSON_FALSE 2
60#define JSON_INT 3
61#define JSON_REAL 4
62#define JSON_STRING 5
63#define JSON_ARRAY 6
64#define JSON_OBJECT 7
65
drh987eb1f2015-08-17 15:17:37 +000066/*
67** Names of the various JSON types:
68*/
69static const char * const jsonType[] = {
70 "null", "true", "false", "integer", "real", "text", "array", "object"
71};
72
drh301eecc2015-08-17 20:14:19 +000073/* Bit values for the JsonNode.jnFlag field
74*/
75#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
76#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
77#define JNODE_REMOVE 0x04 /* Do not output */
drhd0960592015-08-17 21:22:32 +000078#define JNODE_REPLACE 0x08 /* Replace with JsonNode.iVal */
drh52216ad2015-08-18 02:28:03 +000079#define JNODE_APPEND 0x10 /* More ARRAY/OBJECT entries at u.iAppend */
drh301eecc2015-08-17 20:14:19 +000080
drh987eb1f2015-08-17 15:17:37 +000081
drhe9c37f32015-08-15 21:25:36 +000082/* A single node of parsed JSON
83*/
drhe9c37f32015-08-15 21:25:36 +000084struct JsonNode {
drh5634cc02015-08-17 11:28:03 +000085 u8 eType; /* One of the JSON_ type values */
drh301eecc2015-08-17 20:14:19 +000086 u8 jnFlags; /* JNODE flags */
drhd0960592015-08-17 21:22:32 +000087 u8 iVal; /* Replacement value when JNODE_REPLACE */
drhe9c37f32015-08-15 21:25:36 +000088 u32 n; /* Bytes of content, or number of sub-nodes */
drh52216ad2015-08-18 02:28:03 +000089 union {
drh0042a972015-08-18 12:59:58 +000090 const char *zJContent; /* Content for INT, REAL, and STRING */
91 u32 iAppend; /* More terms for ARRAY and OBJECT */
drh52216ad2015-08-18 02:28:03 +000092 } u;
drhe9c37f32015-08-15 21:25:36 +000093};
94
95/* A completely parsed JSON string
96*/
drhe9c37f32015-08-15 21:25:36 +000097struct JsonParse {
98 u32 nNode; /* Number of slots of aNode[] used */
99 u32 nAlloc; /* Number of slots of aNode[] allocated */
100 JsonNode *aNode; /* Array of nodes containing the parse */
101 const char *zJson; /* Original JSON string */
102 u8 oom; /* Set to true if out of memory */
103};
104
drh301eecc2015-08-17 20:14:19 +0000105/*
106** Return the number of consecutive JsonNode slots need to represent
107** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
108** OBJECT types, the number might be larger.
drh52216ad2015-08-18 02:28:03 +0000109**
110** Appended elements are not counted. The value returned is the number
111** by which the JsonNode counter should increment in order to go to the
112** next peer value.
drh301eecc2015-08-17 20:14:19 +0000113*/
drhd0960592015-08-17 21:22:32 +0000114static u32 jsonSize(JsonNode *pNode){
drh301eecc2015-08-17 20:14:19 +0000115 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
116}
117
drh5fa5c102015-08-12 16:49:40 +0000118/* Set the Json object to an empty string
119*/
120static void jsonZero(Json *p){
121 p->zBuf = p->zSpace;
122 p->nAlloc = sizeof(p->zSpace);
123 p->nUsed = 0;
124 p->bStatic = 1;
125}
126
127/* Initialize the Json object
128*/
129static void jsonInit(Json *p, sqlite3_context *pCtx){
130 p->pCtx = pCtx;
drhd0960592015-08-17 21:22:32 +0000131 p->bErr = 0;
drh5fa5c102015-08-12 16:49:40 +0000132 jsonZero(p);
133}
134
135
136/* Free all allocated memory and reset the Json object back to its
137** initial state.
138*/
139static void jsonReset(Json *p){
140 if( !p->bStatic ) sqlite3_free(p->zBuf);
141 jsonZero(p);
142}
143
144
145/* Report an out-of-memory (OOM) condition
146*/
147static void jsonOom(Json *p){
drhd0960592015-08-17 21:22:32 +0000148 if( !p->bErr ){
149 p->bErr = 1;
150 sqlite3_result_error_nomem(p->pCtx);
151 jsonReset(p);
152 }
drh5fa5c102015-08-12 16:49:40 +0000153}
154
155/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
156** Return zero on success. Return non-zero on an OOM error
157*/
158static int jsonGrow(Json *p, u32 N){
drh301eecc2015-08-17 20:14:19 +0000159 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
drh5fa5c102015-08-12 16:49:40 +0000160 char *zNew;
161 if( p->bStatic ){
drhd0960592015-08-17 21:22:32 +0000162 if( p->bErr ) return 1;
drh5fa5c102015-08-12 16:49:40 +0000163 zNew = sqlite3_malloc64(nTotal);
164 if( zNew==0 ){
165 jsonOom(p);
166 return SQLITE_NOMEM;
167 }
168 memcpy(zNew, p->zBuf, p->nUsed);
169 p->zBuf = zNew;
170 p->bStatic = 0;
171 }else{
172 zNew = sqlite3_realloc64(p->zBuf, nTotal);
173 if( zNew==0 ){
174 jsonOom(p);
175 return SQLITE_NOMEM;
176 }
177 p->zBuf = zNew;
178 }
179 p->nAlloc = nTotal;
180 return SQLITE_OK;
181}
182
183/* Append N bytes from zIn onto the end of the Json string.
184*/
185static void jsonAppendRaw(Json *p, const char *zIn, u32 N){
186 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
187 memcpy(p->zBuf+p->nUsed, zIn, N);
188 p->nUsed += N;
189}
190
drhd0960592015-08-17 21:22:32 +0000191#ifdef SQLITE_DEBUG
drhe9c37f32015-08-15 21:25:36 +0000192/* Append the zero-terminated string zIn
193*/
194static void jsonAppend(Json *p, const char *zIn){
195 jsonAppendRaw(p, zIn, (u32)strlen(zIn));
196}
drhd0960592015-08-17 21:22:32 +0000197#endif
drhe9c37f32015-08-15 21:25:36 +0000198
drh5634cc02015-08-17 11:28:03 +0000199/* Append a single character
200*/
201static void jsonAppendChar(Json *p, char c){
202 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
203 p->zBuf[p->nUsed++] = c;
204}
205
drh301eecc2015-08-17 20:14:19 +0000206/* Append a comma separator to the output buffer, if the previous
207** character is not '[' or '{'.
208*/
209static void jsonAppendSeparator(Json *p){
210 char c;
211 if( p->nUsed==0 ) return;
212 c = p->zBuf[p->nUsed-1];
213 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
214}
215
drh5fa5c102015-08-12 16:49:40 +0000216/* Append the N-byte string in zIn to the end of the Json string
217** under construction. Enclose the string in "..." and escape
218** any double-quotes or backslash characters contained within the
219** string.
220*/
221static void jsonAppendString(Json *p, const char *zIn, u32 N){
222 u32 i;
223 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
224 p->zBuf[p->nUsed++] = '"';
225 for(i=0; i<N; i++){
226 char c = zIn[i];
227 if( c=='"' || c=='\\' ){
228 if( (p->nUsed+N+1-i > p->nAlloc) && jsonGrow(p,N+1-i)!=0 ) return;
229 p->zBuf[p->nUsed++] = '\\';
230 }
231 p->zBuf[p->nUsed++] = c;
232 }
233 p->zBuf[p->nUsed++] = '"';
234}
235
drhd0960592015-08-17 21:22:32 +0000236/*
237** Append a function parameter value to the JSON string under
238** construction.
239*/
240static void jsonAppendValue(
241 Json *p, /* Append to this JSON string */
242 sqlite3_value *pValue /* Value to append */
243){
244 switch( sqlite3_value_type(pValue) ){
245 case SQLITE_NULL: {
246 jsonAppendRaw(p, "null", 4);
247 break;
248 }
249 case SQLITE_INTEGER:
250 case SQLITE_FLOAT: {
251 const char *z = (const char*)sqlite3_value_text(pValue);
252 u32 n = (u32)sqlite3_value_bytes(pValue);
253 jsonAppendRaw(p, z, n);
254 break;
255 }
256 case SQLITE_TEXT: {
257 const char *z = (const char*)sqlite3_value_text(pValue);
258 u32 n = (u32)sqlite3_value_bytes(pValue);
259 jsonAppendString(p, z, n);
260 break;
261 }
262 default: {
263 if( p->bErr==0 ){
264 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
265 p->bErr = 1;
266 jsonReset(p);
267 }
268 break;
269 }
270 }
271}
272
273
drhbd0621b2015-08-13 13:54:59 +0000274/* Make the JSON in p the result of the SQL function.
drh5fa5c102015-08-12 16:49:40 +0000275*/
276static void jsonResult(Json *p){
drhd0960592015-08-17 21:22:32 +0000277 if( p->bErr==0 ){
drh5fa5c102015-08-12 16:49:40 +0000278 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
279 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
280 SQLITE_UTF8);
281 jsonZero(p);
282 }
283 assert( p->bStatic );
284}
285
drh5634cc02015-08-17 11:28:03 +0000286/*
287** Convert the JsonNode pNode into a pure JSON string and
288** append to pOut. Subsubstructure is also included. Return
289** the number of JsonNode objects that are encoded.
drhbd0621b2015-08-13 13:54:59 +0000290*/
drh52216ad2015-08-18 02:28:03 +0000291static void jsonRenderNode(
drhd0960592015-08-17 21:22:32 +0000292 JsonNode *pNode, /* The node to render */
293 Json *pOut, /* Write JSON here */
294 sqlite3_value **aReplace /* Replacement values */
295){
drh5634cc02015-08-17 11:28:03 +0000296 switch( pNode->eType ){
297 case JSON_NULL: {
298 jsonAppendRaw(pOut, "null", 4);
299 break;
300 }
301 case JSON_TRUE: {
302 jsonAppendRaw(pOut, "true", 4);
303 break;
304 }
305 case JSON_FALSE: {
306 jsonAppendRaw(pOut, "false", 5);
307 break;
308 }
309 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000310 if( pNode->jnFlags & JNODE_RAW ){
drh52216ad2015-08-18 02:28:03 +0000311 jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000312 break;
313 }
314 /* Fall through into the next case */
315 }
316 case JSON_REAL:
317 case JSON_INT: {
drh52216ad2015-08-18 02:28:03 +0000318 jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
drh5634cc02015-08-17 11:28:03 +0000319 break;
320 }
321 case JSON_ARRAY: {
drh52216ad2015-08-18 02:28:03 +0000322 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000323 jsonAppendChar(pOut, '[');
drh52216ad2015-08-18 02:28:03 +0000324 for(;;){
325 while( j<=pNode->n ){
326 if( pNode[j].jnFlags & (JNODE_REMOVE|JNODE_REPLACE) ){
327 if( pNode[j].jnFlags & JNODE_REPLACE ){
328 jsonAppendSeparator(pOut);
329 jsonAppendValue(pOut, aReplace[pNode[j].iVal]);
330 }
331 }else{
drhd0960592015-08-17 21:22:32 +0000332 jsonAppendSeparator(pOut);
drh52216ad2015-08-18 02:28:03 +0000333 jsonRenderNode(&pNode[j], pOut, aReplace);
drhd0960592015-08-17 21:22:32 +0000334 }
335 j += jsonSize(&pNode[j]);
drh301eecc2015-08-17 20:14:19 +0000336 }
drh52216ad2015-08-18 02:28:03 +0000337 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
338 pNode = &pNode[pNode->u.iAppend];
339 j = 1;
drh5634cc02015-08-17 11:28:03 +0000340 }
341 jsonAppendChar(pOut, ']');
342 break;
343 }
344 case JSON_OBJECT: {
drh52216ad2015-08-18 02:28:03 +0000345 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000346 jsonAppendChar(pOut, '{');
drh52216ad2015-08-18 02:28:03 +0000347 for(;;){
348 while( j<=pNode->n ){
349 if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
350 jsonAppendSeparator(pOut);
351 jsonRenderNode(&pNode[j], pOut, aReplace);
352 jsonAppendChar(pOut, ':');
353 if( pNode[j+1].jnFlags & JNODE_REPLACE ){
354 jsonAppendValue(pOut, aReplace[pNode[j+1].iVal]);
355 }else{
356 jsonRenderNode(&pNode[j+1], pOut, aReplace);
357 }
drhd0960592015-08-17 21:22:32 +0000358 }
drh52216ad2015-08-18 02:28:03 +0000359 j += 1 + jsonSize(&pNode[j+1]);
drh301eecc2015-08-17 20:14:19 +0000360 }
drh52216ad2015-08-18 02:28:03 +0000361 if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
362 pNode = &pNode[pNode->u.iAppend];
363 j = 1;
drh5634cc02015-08-17 11:28:03 +0000364 }
365 jsonAppendChar(pOut, '}');
366 break;
367 }
drhbd0621b2015-08-13 13:54:59 +0000368 }
drh5634cc02015-08-17 11:28:03 +0000369}
370
371/*
372** Make the JsonNode the return value of the function.
373*/
drhd0960592015-08-17 21:22:32 +0000374static void jsonReturn(
375 JsonNode *pNode, /* Node to return */
376 sqlite3_context *pCtx, /* Return value for this function */
377 sqlite3_value **aReplace /* Array of replacement values */
378){
drh5634cc02015-08-17 11:28:03 +0000379 switch( pNode->eType ){
380 case JSON_NULL: {
381 sqlite3_result_null(pCtx);
382 break;
383 }
384 case JSON_TRUE: {
385 sqlite3_result_int(pCtx, 1);
386 break;
387 }
388 case JSON_FALSE: {
389 sqlite3_result_int(pCtx, 0);
390 break;
391 }
drh987eb1f2015-08-17 15:17:37 +0000392 case JSON_REAL: {
drh52216ad2015-08-18 02:28:03 +0000393 double r = strtod(pNode->u.zJContent, 0);
drh987eb1f2015-08-17 15:17:37 +0000394 sqlite3_result_double(pCtx, r);
drh5634cc02015-08-17 11:28:03 +0000395 break;
396 }
drh987eb1f2015-08-17 15:17:37 +0000397 case JSON_INT: {
398 sqlite3_int64 i = 0;
drh52216ad2015-08-18 02:28:03 +0000399 const char *z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000400 if( z[0]=='-' ){ z++; }
401 while( z[0]>='0' && z[0]<='9' ){ i = i*10 + *(z++) - '0'; }
drh52216ad2015-08-18 02:28:03 +0000402 if( pNode->u.zJContent[0]=='-' ){ i = -i; }
drh987eb1f2015-08-17 15:17:37 +0000403 sqlite3_result_int64(pCtx, i);
404 break;
405 }
drh5634cc02015-08-17 11:28:03 +0000406 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000407 if( pNode->jnFlags & JNODE_RAW ){
drh52216ad2015-08-18 02:28:03 +0000408 sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
409 SQLITE_TRANSIENT);
drh301eecc2015-08-17 20:14:19 +0000410 }else if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
drh987eb1f2015-08-17 15:17:37 +0000411 /* JSON formatted without any backslash-escapes */
drh52216ad2015-08-18 02:28:03 +0000412 sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
drh987eb1f2015-08-17 15:17:37 +0000413 SQLITE_TRANSIENT);
drh5634cc02015-08-17 11:28:03 +0000414 }else{
415 /* Translate JSON formatted string into raw text */
drh987eb1f2015-08-17 15:17:37 +0000416 u32 i;
417 u32 n = pNode->n;
drh52216ad2015-08-18 02:28:03 +0000418 const char *z = pNode->u.zJContent;
drh987eb1f2015-08-17 15:17:37 +0000419 char *zOut;
420 u32 j;
421 zOut = sqlite3_malloc( n+1 );
422 if( zOut==0 ){
423 sqlite3_result_error_nomem(pCtx);
424 break;
425 }
426 for(i=1, j=0; i<n-1; i++){
427 char c = z[i];
428 if( c!='\\' && z[i+1] ){
429 zOut[j++] = c;
430 }else{
431 c = z[++i];
432 if( c=='u' && z[1] ){
433 u32 v = 0, k;
434 z++;
435 for(k=0; k<4 && z[k]; k++){
436 c = z[0];
437 if( c>='0' && c<='9' ) v = v*16 + c - '0';
438 else if( c>='A' && c<='F' ) v = v*16 + c - 'A' + 10;
439 else if( c>='a' && c<='f' ) v = v*16 + c - 'a' + 10;
440 else break;
441 z++;
442 }
443 if( v<=0x7f ){
444 zOut[j++] = v;
445 }else if( v<=0x7ff ){
446 zOut[j++] = 0xc0 | (v>>6);
447 zOut[j++] = 0x80 | (v&0x3f);
448 }else if( v<=0xffff ){
449 zOut[j++] = 0xe0 | (v>>12);
450 zOut[j++] = 0x80 | ((v>>6)&0x3f);
451 zOut[j++] = 0x80 | (v&0x3f);
452 }else if( v<=0x10ffff ){
453 zOut[j++] = 0xf0 | (v>>18);
454 zOut[j++] = 0x80 | ((v>>12)&0x3f);
455 zOut[j++] = 0x80 | ((v>>6)&0x3f);
456 zOut[j++] = 0x80 | (v&0x3f);
457 }
458 }else{
459 if( c=='b' ){
460 c = '\b';
461 }else if( c=='f' ){
462 c = '\f';
463 }else if( c=='n' ){
464 c = '\n';
465 }else if( c=='r' ){
466 c = '\r';
467 }else if( c=='t' ){
468 c = '\t';
469 }
470 zOut[j++] = c;
471 }
472 }
473 }
474 zOut[j] = 0;
475 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
drh5634cc02015-08-17 11:28:03 +0000476 }
477 break;
478 }
479 case JSON_ARRAY:
480 case JSON_OBJECT: {
481 Json s;
482 jsonInit(&s, pCtx);
drhd0960592015-08-17 21:22:32 +0000483 jsonRenderNode(pNode, &s, aReplace);
drh5634cc02015-08-17 11:28:03 +0000484 jsonResult(&s);
485 break;
486 }
487 }
drhbd0621b2015-08-13 13:54:59 +0000488}
489
drh5fa5c102015-08-12 16:49:40 +0000490/*
drhe9c37f32015-08-15 21:25:36 +0000491** Create a new JsonNode instance based on the arguments and append that
492** instance to the JsonParse. Return the index in pParse->aNode[] of the
493** new node, or -1 if a memory allocation fails.
494*/
495static int jsonParseAddNode(
496 JsonParse *pParse, /* Append the node to this object */
497 u32 eType, /* Node type */
498 u32 n, /* Content size or sub-node count */
499 const char *zContent /* Content */
500){
501 JsonNode *p;
502 if( pParse->nNode>=pParse->nAlloc ){
503 u32 nNew;
504 JsonNode *pNew;
505 if( pParse->oom ) return -1;
506 nNew = pParse->nAlloc*2 + 10;
507 if( nNew<=pParse->nNode ){
508 pParse->oom = 1;
509 return -1;
510 }
511 pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew);
512 if( pNew==0 ){
513 pParse->oom = 1;
514 return -1;
515 }
516 pParse->nAlloc = nNew;
517 pParse->aNode = pNew;
518 }
519 p = &pParse->aNode[pParse->nNode];
drh5634cc02015-08-17 11:28:03 +0000520 p->eType = (u8)eType;
drh301eecc2015-08-17 20:14:19 +0000521 p->jnFlags = 0;
drhd0960592015-08-17 21:22:32 +0000522 p->iVal = 0;
drhe9c37f32015-08-15 21:25:36 +0000523 p->n = n;
drh52216ad2015-08-18 02:28:03 +0000524 p->u.zJContent = zContent;
drhe9c37f32015-08-15 21:25:36 +0000525 return pParse->nNode++;
526}
527
528/*
529** Parse a single JSON value which begins at pParse->zJson[i]. Return the
530** index of the first character past the end of the value parsed.
531**
532** Return negative for a syntax error. Special cases: return -2 if the
533** first non-whitespace character is '}' and return -3 if the first
534** non-whitespace character is ']'.
535*/
536static int jsonParseValue(JsonParse *pParse, u32 i){
537 char c;
538 u32 j;
539 u32 iThis;
540 int x;
541 while( isspace(pParse->zJson[i]) ){ i++; }
542 if( (c = pParse->zJson[i])==0 ) return 0;
543 if( c=='{' ){
544 /* Parse object */
545 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
546 if( iThis<0 ) return -1;
547 for(j=i+1;;j++){
548 while( isspace(pParse->zJson[j]) ){ j++; }
549 x = jsonParseValue(pParse, j);
550 if( x<0 ){
551 if( x==(-2) && pParse->nNode==iThis+1 ) return j+1;
552 return -1;
553 }
554 if( pParse->aNode[pParse->nNode-1].eType!=JSON_STRING ) return -1;
555 j = x;
556 while( isspace(pParse->zJson[j]) ){ j++; }
557 if( pParse->zJson[j]!=':' ) return -1;
558 j++;
559 x = jsonParseValue(pParse, j);
560 if( x<0 ) return -1;
561 j = x;
562 while( isspace(pParse->zJson[j]) ){ j++; }
563 c = pParse->zJson[j];
564 if( c==',' ) continue;
565 if( c!='}' ) return -1;
566 break;
567 }
568 pParse->aNode[iThis].n = pParse->nNode - iThis - 1;
569 return j+1;
570 }else if( c=='[' ){
571 /* Parse array */
572 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
573 if( iThis<0 ) return -1;
574 for(j=i+1;;j++){
575 while( isspace(pParse->zJson[j]) ){ j++; }
576 x = jsonParseValue(pParse, j);
577 if( x<0 ){
578 if( x==(-3) && pParse->nNode==iThis+1 ) return j+1;
579 return -1;
580 }
581 j = x;
582 while( isspace(pParse->zJson[j]) ){ j++; }
583 c = pParse->zJson[j];
584 if( c==',' ) continue;
585 if( c!=']' ) return -1;
586 break;
587 }
588 pParse->aNode[iThis].n = pParse->nNode - iThis - 1;
589 return j+1;
590 }else if( c=='"' ){
591 /* Parse string */
drh301eecc2015-08-17 20:14:19 +0000592 u8 jnFlags = 0;
drhe9c37f32015-08-15 21:25:36 +0000593 j = i+1;
594 for(;;){
595 c = pParse->zJson[j];
596 if( c==0 ) return -1;
597 if( c=='\\' ){
598 c = pParse->zJson[++j];
599 if( c==0 ) return -1;
drh301eecc2015-08-17 20:14:19 +0000600 jnFlags = JNODE_ESCAPE;
drhe9c37f32015-08-15 21:25:36 +0000601 }else if( c=='"' ){
602 break;
603 }
604 j++;
605 }
606 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]);
drh301eecc2015-08-17 20:14:19 +0000607 pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
drhe9c37f32015-08-15 21:25:36 +0000608 return j+1;
609 }else if( c=='n'
610 && strncmp(pParse->zJson+i,"null",4)==0
drhb2cd10e2015-08-15 21:29:14 +0000611 && !isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000612 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
613 return i+4;
614 }else if( c=='t'
615 && strncmp(pParse->zJson+i,"true",4)==0
drhb2cd10e2015-08-15 21:29:14 +0000616 && !isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000617 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
618 return i+4;
619 }else if( c=='f'
620 && strncmp(pParse->zJson+i,"false",5)==0
drhb2cd10e2015-08-15 21:29:14 +0000621 && !isalnum(pParse->zJson[i+5]) ){
drhe9c37f32015-08-15 21:25:36 +0000622 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
623 return i+5;
624 }else if( c=='-' || (c>='0' && c<='9') ){
625 /* Parse number */
626 u8 seenDP = 0;
627 u8 seenE = 0;
628 j = i+1;
629 for(;; j++){
630 c = pParse->zJson[j];
631 if( c>='0' && c<='9' ) continue;
632 if( c=='.' ){
633 if( pParse->zJson[j-1]=='-' ) return -1;
634 if( seenDP ) return -1;
635 seenDP = 1;
636 continue;
637 }
638 if( c=='e' || c=='E' ){
639 if( pParse->zJson[j-1]<'0' ) return -1;
640 if( seenE ) return -1;
641 seenDP = seenE = 1;
642 c = pParse->zJson[j+1];
643 if( c=='+' || c=='-' ) j++;
644 continue;
645 }
646 break;
647 }
648 if( pParse->zJson[j-1]<'0' ) return -1;
649 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
650 j - i, &pParse->zJson[i]);
651 return j;
652 }else if( c=='}' ){
653 return -2; /* End of {...} */
654 }else if( c==']' ){
655 return -3; /* End of [...] */
656 }else{
657 return -1; /* Syntax error */
658 }
659}
660
661/*
662** Parse a complete JSON string. Return 0 on success or non-zero if there
663** are any errors. If an error occurs, free all memory associated with
664** pParse.
665**
666** pParse is uninitialized when this routine is called.
667*/
668static int jsonParse(JsonParse *pParse, const char *zJson){
669 int i;
670 if( zJson==0 ) return 1;
671 memset(pParse, 0, sizeof(*pParse));
672 pParse->zJson = zJson;
673 i = jsonParseValue(pParse, 0);
674 if( i>0 ){
675 while( isspace(zJson[i]) ) i++;
676 if( zJson[i] ) i = -1;
677 }
678 if( i<0 ){
679 sqlite3_free(pParse->aNode);
680 pParse->aNode = 0;
681 pParse->nNode = 0;
682 pParse->nAlloc = 0;
683 return 1;
684 }
685 return 0;
686}
drh301eecc2015-08-17 20:14:19 +0000687
drh52216ad2015-08-18 02:28:03 +0000688/* forward declaration */
689static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*);
690
drh987eb1f2015-08-17 15:17:37 +0000691/*
692** Search along zPath to find the node specified. Return a pointer
693** to that node, or NULL if zPath is malformed or if there is no such
694** node.
drh52216ad2015-08-18 02:28:03 +0000695**
696** If pApnd!=0, then try to append new nodes to complete zPath if it is
697** possible to do so and if no existing node corresponds to zPath. If
698** new nodes are appended *pApnd is set to 1.
drh987eb1f2015-08-17 15:17:37 +0000699*/
drh52216ad2015-08-18 02:28:03 +0000700static JsonNode *jsonLookup(
701 JsonParse *pParse, /* The JSON to search */
702 u32 iRoot, /* Begin the search at this node */
703 const char *zPath, /* The path to search */
704 int *pApnd /* Append nodes to complete path if not NULL */
705){
drh6b43cc82015-08-19 23:02:49 +0000706 u32 i, j, k, nKey;
707 const char *zKey;
drh52216ad2015-08-18 02:28:03 +0000708 JsonNode *pRoot = &pParse->aNode[iRoot];
drh987eb1f2015-08-17 15:17:37 +0000709 if( zPath[0]==0 ) return pRoot;
710 if( zPath[0]=='.' ){
711 if( pRoot->eType!=JSON_OBJECT ) return 0;
712 zPath++;
drh6b43cc82015-08-19 23:02:49 +0000713 if( zPath[0]=='"' ){
714 zKey = zPath + 1;
715 for(i=1; zPath[i] && zPath[i]!='"'; i++){}
716 nKey = i-1;
717 if( zPath[i] ) i++;
718 }else{
719 zKey = zPath;
720 for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
721 nKey = i;
722 }
723 if( nKey==0 ) return 0;
drh987eb1f2015-08-17 15:17:37 +0000724 j = 1;
drh52216ad2015-08-18 02:28:03 +0000725 for(;;){
726 while( j<=pRoot->n ){
drh6b43cc82015-08-19 23:02:49 +0000727 if( pRoot[j].n==nKey+2
728 && strncmp(&pRoot[j].u.zJContent[1],zKey,nKey)==0
drh52216ad2015-08-18 02:28:03 +0000729 ){
730 return jsonLookup(pParse, iRoot+j+1, &zPath[i], pApnd);
731 }
732 j++;
733 j += jsonSize(&pRoot[j]);
drh987eb1f2015-08-17 15:17:37 +0000734 }
drh52216ad2015-08-18 02:28:03 +0000735 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
736 iRoot += pRoot->u.iAppend;
737 pRoot = &pParse->aNode[iRoot];
738 j = 1;
739 }
740 if( pApnd ){
741 k = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
742 pRoot->u.iAppend = k - iRoot;
743 pRoot->jnFlags |= JNODE_APPEND;
744 k = jsonParseAddNode(pParse, JSON_STRING, i, zPath);
745 if( !pParse->oom ) pParse->aNode[k].jnFlags |= JNODE_RAW;
746 zPath += i;
747 return jsonLookupAppend(pParse, zPath, pApnd);
drh987eb1f2015-08-17 15:17:37 +0000748 }
749 }else if( zPath[0]=='[' && isdigit(zPath[1]) ){
750 if( pRoot->eType!=JSON_ARRAY ) return 0;
751 i = 0;
752 zPath++;
753 while( isdigit(zPath[0]) ){
754 i = i + zPath[0] - '0';
755 zPath++;
756 }
757 if( zPath[0]!=']' ) return 0;
758 zPath++;
759 j = 1;
drh52216ad2015-08-18 02:28:03 +0000760 for(;;){
761 while( i>0 && j<=pRoot->n ){
762 j += jsonSize(&pRoot[j]);
763 i--;
764 }
765 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
766 iRoot += pRoot->u.iAppend;
767 pRoot = &pParse->aNode[iRoot];
768 j = 1;
drh987eb1f2015-08-17 15:17:37 +0000769 }
770 if( j<=pRoot->n ){
drh52216ad2015-08-18 02:28:03 +0000771 return jsonLookup(pParse, iRoot+j, zPath, pApnd);
772 }
773 if( i==0 && pApnd ){
774 k = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
775 pRoot->u.iAppend = k - iRoot;
776 pRoot->jnFlags |= JNODE_APPEND;
777 return jsonLookupAppend(pParse, zPath, pApnd);
drh987eb1f2015-08-17 15:17:37 +0000778 }
779 }
780 return 0;
781}
782
drh52216ad2015-08-18 02:28:03 +0000783/*
784** Append content to pParse that will complete zPath.
785*/
786static JsonNode *jsonLookupAppend(
787 JsonParse *pParse, /* Append content to the JSON parse */
788 const char *zPath, /* Description of content to append */
789 int *pApnd /* Set this flag to 1 */
790){
791 *pApnd = 1;
792 if( zPath[0]==0 ){
793 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
794 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
795 }
796 if( zPath[0]=='.' ){
797 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
798 }else if( strncmp(zPath,"[0]",3)==0 ){
799 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
800 }else{
801 return 0;
802 }
803 if( pParse->oom ) return 0;
804 return jsonLookup(pParse, pParse->nNode-1, zPath, pApnd);
805}
806
807
drh987eb1f2015-08-17 15:17:37 +0000808/****************************************************************************
809** SQL functions used for testing and debugging
810****************************************************************************/
drhe9c37f32015-08-15 21:25:36 +0000811
drh301eecc2015-08-17 20:14:19 +0000812#ifdef SQLITE_DEBUG
drhe9c37f32015-08-15 21:25:36 +0000813/*
drh5634cc02015-08-17 11:28:03 +0000814** The json_parse(JSON) function returns a string which describes
drhe9c37f32015-08-15 21:25:36 +0000815** a parse of the JSON provided. Or it returns NULL if JSON is not
816** well-formed.
817*/
drh5634cc02015-08-17 11:28:03 +0000818static void jsonParseFunc(
drhe9c37f32015-08-15 21:25:36 +0000819 sqlite3_context *context,
820 int argc,
821 sqlite3_value **argv
822){
823 Json s; /* Output string - not real JSON */
824 JsonParse x; /* The parse */
825 u32 i;
drh301eecc2015-08-17 20:14:19 +0000826 char zBuf[100];
drhe9c37f32015-08-15 21:25:36 +0000827
828 assert( argc==1 );
829 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
830 jsonInit(&s, context);
831 for(i=0; i<x.nNode; i++){
drh301eecc2015-08-17 20:14:19 +0000832 sqlite3_snprintf(sizeof(zBuf), zBuf, "node %3u: %7s n=%d\n",
833 i, jsonType[x.aNode[i].eType], x.aNode[i].n);
drhe9c37f32015-08-15 21:25:36 +0000834 jsonAppend(&s, zBuf);
drh52216ad2015-08-18 02:28:03 +0000835 if( x.aNode[i].u.zJContent!=0 ){
drh301eecc2015-08-17 20:14:19 +0000836 jsonAppendRaw(&s, " text: ", 10);
drh52216ad2015-08-18 02:28:03 +0000837 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
drhe9c37f32015-08-15 21:25:36 +0000838 jsonAppendRaw(&s, "\n", 1);
839 }
840 }
841 sqlite3_free(x.aNode);
842 jsonResult(&s);
843}
844
drh5634cc02015-08-17 11:28:03 +0000845/*
846** The json_test1(JSON) function parses and rebuilds the JSON string.
847*/
848static void jsonTest1Func(
849 sqlite3_context *context,
850 int argc,
851 sqlite3_value **argv
852){
853 JsonParse x; /* The parse */
854 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
drhd0960592015-08-17 21:22:32 +0000855 jsonReturn(x.aNode, context, 0);
drh5634cc02015-08-17 11:28:03 +0000856 sqlite3_free(x.aNode);
857}
858
859/*
860** The json_nodecount(JSON) function returns the number of nodes in the
861** input JSON string.
862*/
863static void jsonNodeCountFunc(
864 sqlite3_context *context,
865 int argc,
866 sqlite3_value **argv
867){
868 JsonParse x; /* The parse */
869 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
870 sqlite3_result_int64(context, x.nNode);
871 sqlite3_free(x.aNode);
872}
drh301eecc2015-08-17 20:14:19 +0000873#endif /* SQLITE_DEBUG */
drh5634cc02015-08-17 11:28:03 +0000874
drh987eb1f2015-08-17 15:17:37 +0000875/****************************************************************************
876** SQL function implementations
877****************************************************************************/
878
879/*
880** Implementation of the json_array(VALUE,...) function. Return a JSON
881** array that contains all values given in arguments. Or if any argument
882** is a BLOB, throw an error.
883*/
884static void jsonArrayFunc(
885 sqlite3_context *context,
886 int argc,
887 sqlite3_value **argv
888){
889 int i;
890 Json jx;
drh987eb1f2015-08-17 15:17:37 +0000891
892 jsonInit(&jx, context);
drhd0960592015-08-17 21:22:32 +0000893 jsonAppendChar(&jx, '[');
drh987eb1f2015-08-17 15:17:37 +0000894 for(i=0; i<argc; i++){
drhd0960592015-08-17 21:22:32 +0000895 jsonAppendSeparator(&jx);
896 jsonAppendValue(&jx, argv[i]);
drh987eb1f2015-08-17 15:17:37 +0000897 }
drhd0960592015-08-17 21:22:32 +0000898 jsonAppendChar(&jx, ']');
drh987eb1f2015-08-17 15:17:37 +0000899 jsonResult(&jx);
900}
901
902
903/*
904** json_array_length(JSON)
905** json_array_length(JSON, PATH)
906**
907** Return the number of elements in the top-level JSON array.
908** Return 0 if the input is not a well-formed JSON array.
909*/
910static void jsonArrayLengthFunc(
911 sqlite3_context *context,
912 int argc,
913 sqlite3_value **argv
914){
915 JsonParse x; /* The parse */
916 sqlite3_int64 n = 0;
917 u32 i;
918 const char *zPath;
919
920 if( argc==2 ){
921 zPath = (const char*)sqlite3_value_text(argv[1]);
922 if( zPath==0 ) return;
923 if( zPath[0]!='$' ) return;
924 zPath++;
925 }else{
926 zPath = 0;
927 }
928 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0]))==0 ){
929 if( x.nNode ){
930 JsonNode *pNode = x.aNode;
drh52216ad2015-08-18 02:28:03 +0000931 if( zPath ) pNode = jsonLookup(&x, 0, zPath, 0);
drh987eb1f2015-08-17 15:17:37 +0000932 if( pNode->eType==JSON_ARRAY ){
drh52216ad2015-08-18 02:28:03 +0000933 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
drh301eecc2015-08-17 20:14:19 +0000934 for(i=1; i<=pNode->n; n++){
drhd0960592015-08-17 21:22:32 +0000935 i += jsonSize(&pNode[i]);
drh987eb1f2015-08-17 15:17:37 +0000936 }
937 }
938 }
939 sqlite3_free(x.aNode);
940 }
941 sqlite3_result_int64(context, n);
942}
943
944/*
945** json_extract(JSON, PATH)
946**
947** Return the element described by PATH. Return NULL if JSON is not
948** valid JSON or if there is no PATH element or if PATH is malformed.
949*/
950static void jsonExtractFunc(
951 sqlite3_context *context,
952 int argc,
953 sqlite3_value **argv
954){
955 JsonParse x; /* The parse */
956 JsonNode *pNode;
957 const char *zPath;
958 assert( argc==2 );
959 zPath = (const char*)sqlite3_value_text(argv[1]);
960 if( zPath==0 ) return;
961 if( zPath[0]!='$' ) return;
962 zPath++;
963 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
drh52216ad2015-08-18 02:28:03 +0000964 pNode = jsonLookup(&x, 0, zPath, 0);
drh987eb1f2015-08-17 15:17:37 +0000965 if( pNode ){
drhd0960592015-08-17 21:22:32 +0000966 jsonReturn(pNode, context, 0);
drh987eb1f2015-08-17 15:17:37 +0000967 }
968 sqlite3_free(x.aNode);
969}
970
971/*
972** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
973** object that contains all name/value given in arguments. Or if any name
974** is not a string or if any value is a BLOB, throw an error.
975*/
976static void jsonObjectFunc(
977 sqlite3_context *context,
978 int argc,
979 sqlite3_value **argv
980){
981 int i;
982 Json jx;
drh987eb1f2015-08-17 15:17:37 +0000983 const char *z;
984 u32 n;
985
986 if( argc&1 ){
987 sqlite3_result_error(context, "json_object() requires an even number "
988 "of arguments", -1);
989 return;
990 }
991 jsonInit(&jx, context);
drhd0960592015-08-17 21:22:32 +0000992 jsonAppendChar(&jx, '{');
drh987eb1f2015-08-17 15:17:37 +0000993 for(i=0; i<argc; i+=2){
drh987eb1f2015-08-17 15:17:37 +0000994 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
995 sqlite3_result_error(context, "json_object() labels must be TEXT", -1);
996 jsonZero(&jx);
997 return;
998 }
drhd0960592015-08-17 21:22:32 +0000999 jsonAppendSeparator(&jx);
drh987eb1f2015-08-17 15:17:37 +00001000 z = (const char*)sqlite3_value_text(argv[i]);
1001 n = (u32)sqlite3_value_bytes(argv[i]);
1002 jsonAppendString(&jx, z, n);
drhd0960592015-08-17 21:22:32 +00001003 jsonAppendChar(&jx, ':');
1004 jsonAppendValue(&jx, argv[i+1]);
drh987eb1f2015-08-17 15:17:37 +00001005 }
drhd0960592015-08-17 21:22:32 +00001006 jsonAppendChar(&jx, '}');
drh987eb1f2015-08-17 15:17:37 +00001007 jsonResult(&jx);
1008}
1009
1010
1011/*
drh301eecc2015-08-17 20:14:19 +00001012** json_remove(JSON, PATH, ...)
1013**
1014** Remove the named elements from JSON and return the result. Ill-formed
1015** PATH arguments are silently ignored. If JSON is ill-formed, then NULL
1016** is returned.
1017*/
1018static void jsonRemoveFunc(
1019 sqlite3_context *context,
1020 int argc,
1021 sqlite3_value **argv
1022){
1023 JsonParse x; /* The parse */
1024 JsonNode *pNode;
1025 const char *zPath;
1026 u32 i;
1027
1028 if( argc<1 ) return;
1029 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
1030 if( x.nNode ){
1031 for(i=1; i<argc; i++){
1032 zPath = (const char*)sqlite3_value_text(argv[i]);
1033 if( zPath==0 ) continue;
1034 if( zPath[0]!='$' ) continue;
drh52216ad2015-08-18 02:28:03 +00001035 pNode = jsonLookup(&x, 0, &zPath[1], 0);
drh301eecc2015-08-17 20:14:19 +00001036 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
1037 }
1038 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
drhd0960592015-08-17 21:22:32 +00001039 jsonReturn(x.aNode, context, 0);
1040 }
1041 }
1042 sqlite3_free(x.aNode);
1043}
1044
1045/*
1046** json_replace(JSON, PATH, VALUE, ...)
1047**
1048** Replace the value at PATH with VALUE. If PATH does not already exist,
1049** this routine is a no-op. If JSON is ill-formed, return NULL.
1050*/
1051static void jsonReplaceFunc(
1052 sqlite3_context *context,
1053 int argc,
1054 sqlite3_value **argv
1055){
1056 JsonParse x; /* The parse */
1057 JsonNode *pNode;
1058 const char *zPath;
1059 u32 i;
1060
1061 if( argc<1 ) return;
1062 if( (argc&1)==0 ) {
1063 sqlite3_result_error(context,
1064 "json_replace() needs an odd number of arguments", -1);
1065 return;
1066 }
1067 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
1068 if( x.nNode ){
1069 for(i=1; i<argc; i+=2){
1070 zPath = (const char*)sqlite3_value_text(argv[i]);
1071 if( zPath==0 ) continue;
1072 if( zPath[0]!='$' ) continue;
drh52216ad2015-08-18 02:28:03 +00001073 pNode = jsonLookup(&x, 0, &zPath[1], 0);
drhd0960592015-08-17 21:22:32 +00001074 if( pNode ){
1075 pNode->jnFlags |= JNODE_REPLACE;
1076 pNode->iVal = i+1;
1077 }
1078 }
1079 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
1080 sqlite3_result_value(context, argv[x.aNode[0].iVal]);
1081 }else{
1082 jsonReturn(x.aNode, context, argv);
drh301eecc2015-08-17 20:14:19 +00001083 }
1084 }
1085 sqlite3_free(x.aNode);
1086}
drh52216ad2015-08-18 02:28:03 +00001087/*
1088** json_set(JSON, PATH, VALUE, ...)
1089**
1090** Set the value at PATH to VALUE. Create the PATH if it does not already
1091** exist. Overwrite existing values that do exist.
1092** If JSON is ill-formed, return NULL.
1093**
1094** json_insert(JSON, PATH, VALUE, ...)
1095**
1096** Create PATH and initialize it to VALUE. If PATH already exists, this
1097** routine is a no-op. If JSON is ill-formed, return NULL.
1098*/
1099static void jsonSetFunc(
1100 sqlite3_context *context,
1101 int argc,
1102 sqlite3_value **argv
1103){
1104 JsonParse x; /* The parse */
1105 JsonNode *pNode;
1106 const char *zPath;
1107 u32 i;
1108 int bApnd;
1109 int bIsSet = *(int*)sqlite3_user_data(context);
1110
1111 if( argc<1 ) return;
1112 if( (argc&1)==0 ) {
1113 sqlite3_result_error(context,
1114 "json_set() needs an odd number of arguments", -1);
1115 return;
1116 }
1117 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
1118 if( x.nNode ){
1119 for(i=1; i<argc; i+=2){
1120 zPath = (const char*)sqlite3_value_text(argv[i]);
1121 if( zPath==0 ) continue;
1122 if( zPath[0]!='$' ) continue;
1123 bApnd = 0;
1124 pNode = jsonLookup(&x, 0, &zPath[1], &bApnd);
1125 if( pNode && (bApnd || bIsSet) ){
1126 pNode->jnFlags |= JNODE_REPLACE;
1127 pNode->iVal = i+1;
1128 }
1129 }
1130 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
1131 sqlite3_result_value(context, argv[x.aNode[0].iVal]);
1132 }else{
1133 jsonReturn(x.aNode, context, argv);
1134 }
1135 }
1136 sqlite3_free(x.aNode);
1137}
drh301eecc2015-08-17 20:14:19 +00001138
1139/*
drh987eb1f2015-08-17 15:17:37 +00001140** json_type(JSON)
1141** json_type(JSON, PATH)
1142**
1143** Return the top-level "type" of a JSON string. Return NULL if the
1144** input is not a well-formed JSON string.
1145*/
1146static void jsonTypeFunc(
1147 sqlite3_context *context,
1148 int argc,
1149 sqlite3_value **argv
1150){
1151 JsonParse x; /* The parse */
1152 const char *zPath;
1153
1154 if( argc==2 ){
1155 zPath = (const char*)sqlite3_value_text(argv[1]);
1156 if( zPath==0 ) return;
1157 if( zPath[0]!='$' ) return;
1158 zPath++;
1159 }else{
1160 zPath = 0;
1161 }
1162 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
1163 if( x.nNode ){
1164 JsonNode *pNode = x.aNode;
drh52216ad2015-08-18 02:28:03 +00001165 if( zPath ) pNode = jsonLookup(&x, 0, zPath, 0);
drh987eb1f2015-08-17 15:17:37 +00001166 sqlite3_result_text(context, jsonType[pNode->eType], -1, SQLITE_STATIC);
1167 }
1168 sqlite3_free(x.aNode);
1169}
drh5634cc02015-08-17 11:28:03 +00001170
drhcb6c6c62015-08-19 22:47:17 +00001171/****************************************************************************
1172** The json_each virtual table
1173****************************************************************************/
1174typedef struct JsonEachCursor JsonEachCursor;
1175struct JsonEachCursor {
1176 sqlite3_vtab_cursor base; /* Base class - must be first */
1177 u32 iRowid; /* The rowid */
1178 u32 i; /* Index in sParse.aNode[] of current row */
1179 u32 iEnd; /* EOF when i equals or exceeds this value */
1180 u8 eType; /* Type of top-level element */
1181 char *zJson; /* Input json */
1182 char *zPath; /* Path by which to filter zJson */
1183 JsonParse sParse; /* The input json */
1184};
1185
1186/* Constructor for the json_each virtual table */
1187static int jsonEachConnect(
1188 sqlite3 *db,
1189 void *pAux,
1190 int argc, const char *const*argv,
1191 sqlite3_vtab **ppVtab,
1192 char **pzErr
1193){
1194 sqlite3_vtab *pNew;
1195 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
1196 if( pNew==0 ) return SQLITE_NOMEM;
1197
1198/* Column numbers */
1199#define JEACH_KEY 0
1200#define JEACH_VALUE 1
1201#define JEACH_JSON 2
1202#define JEACH_PATH 3
1203
1204 sqlite3_declare_vtab(db, "CREATE TABLE x(key,value,json hidden,path hidden)");
1205 memset(pNew, 0, sizeof(*pNew));
1206 return SQLITE_OK;
1207}
1208
1209/* destructor for json_each virtual table */
1210static int jsonEachDisconnect(sqlite3_vtab *pVtab){
1211 sqlite3_free(pVtab);
1212 return SQLITE_OK;
1213}
1214
1215/* constructor for a JsonEachCursor object. */
1216static int jsonEachOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
1217 JsonEachCursor *pCur;
1218 pCur = sqlite3_malloc( sizeof(*pCur) );
1219 if( pCur==0 ) return SQLITE_NOMEM;
1220 memset(pCur, 0, sizeof(*pCur));
1221 *ppCursor = &pCur->base;
1222 return SQLITE_OK;
1223}
1224
1225/* Reset a JsonEachCursor back to its original state. Free any memory
1226** held. */
1227static void jsonEachCursorReset(JsonEachCursor *p){
1228 sqlite3_free(p->zJson);
1229 sqlite3_free(p->zPath);
1230 sqlite3_free(p->sParse.aNode);
1231 p->iRowid = 0;
1232 p->i = 0;
1233 p->iEnd = 0;
1234 p->eType = 0;
1235 memset(&p->sParse, 0, sizeof(p->sParse));
1236 p->zJson = 0;
1237 p->zPath = 0;
1238}
1239
1240/* Destructor for a jsonEachCursor object */
1241static int jsonEachClose(sqlite3_vtab_cursor *cur){
1242 JsonEachCursor *p = (JsonEachCursor*)cur;
1243 jsonEachCursorReset(p);
1244 sqlite3_free(cur);
1245 return SQLITE_OK;
1246}
1247
1248/* Return TRUE if the jsonEachCursor object has been advanced off the end
1249** of the JSON object */
1250static int jsonEachEof(sqlite3_vtab_cursor *cur){
1251 JsonEachCursor *p = (JsonEachCursor*)cur;
1252 return p->i >= p->iEnd;
1253}
1254
1255/* Advance the cursor to the next top-level element of the current
1256** JSON string */
1257static int jsonEachNext(sqlite3_vtab_cursor *cur){
1258 JsonEachCursor *p = (JsonEachCursor*)cur;
1259 switch( p->eType ){
1260 case JSON_ARRAY: {
1261 p->i += jsonSize(&p->sParse.aNode[p->i]);
1262 p->iRowid++;
1263 break;
1264 }
1265 case JSON_OBJECT: {
1266 p->i += 1 + jsonSize(&p->sParse.aNode[p->i+1]);
1267 p->iRowid++;
1268 break;
1269 }
1270 default: {
1271 p->i = p->iEnd;
1272 break;
1273 }
1274 }
1275 return SQLITE_OK;
1276}
1277
1278/* Return the value of a column */
1279static int jsonEachColumn(
1280 sqlite3_vtab_cursor *cur, /* The cursor */
1281 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
1282 int i /* Which column to return */
1283){
1284 JsonEachCursor *p = (JsonEachCursor*)cur;
1285 switch( i ){
1286 case JEACH_KEY: {
1287 if( p->eType==JSON_OBJECT ){
1288 jsonReturn(&p->sParse.aNode[p->i], ctx, 0);
1289 }else{
1290 sqlite3_result_int64(ctx, p->iRowid);
1291 }
1292 break;
1293 }
1294 case JEACH_VALUE: {
1295 if( p->eType==JSON_OBJECT ){
1296 jsonReturn(&p->sParse.aNode[p->i+1], ctx, 0);
1297 }else{
1298 jsonReturn(&p->sParse.aNode[p->i], ctx, 0);
1299 }
1300 break;
1301 }
1302 case JEACH_PATH: {
1303 const char *zPath = p->zPath;
1304 if( zPath==0 ) zPath = "$";
1305 sqlite3_result_text(ctx, zPath, -1, SQLITE_STATIC);
1306 break;
1307 }
1308 default: {
1309 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
1310 break;
1311 }
1312 }
1313 return SQLITE_OK;
1314}
1315
1316/* Return the current rowid value */
1317static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
1318 JsonEachCursor *p = (JsonEachCursor*)cur;
1319 *pRowid = p->iRowid;
1320 return SQLITE_OK;
1321}
1322
1323/* The query strategy is to look for an equality constraint on the json
1324** column. Without such a constraint, the table cannot operate. idxNum is
1325** 1 if the constraint is found, 3 if the constraint and zPath are found,
1326** and 0 otherwise.
1327*/
1328static int jsonEachBestIndex(
1329 sqlite3_vtab *tab,
1330 sqlite3_index_info *pIdxInfo
1331){
1332 int i;
1333 int jsonIdx = -1;
1334 int pathIdx = -1;
1335 const struct sqlite3_index_constraint *pConstraint;
1336 pConstraint = pIdxInfo->aConstraint;
1337 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
1338 if( pConstraint->usable==0 ) continue;
1339 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
1340 switch( pConstraint->iColumn ){
1341 case JEACH_JSON: jsonIdx = i; break;
1342 case JEACH_PATH: pathIdx = i; break;
1343 default: /* no-op */ break;
1344 }
1345 }
1346 if( jsonIdx<0 ){
1347 pIdxInfo->idxNum = 0;
1348 pIdxInfo->estimatedCost = (double)2000000000;
1349 }else{
1350 pIdxInfo->estimatedCost = (double)1;
1351 pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1;
1352 pIdxInfo->aConstraintUsage[jsonIdx].omit = 1;
1353 if( pathIdx<0 ){
1354 pIdxInfo->idxNum = 1;
1355 }else{
1356 pIdxInfo->aConstraintUsage[pathIdx].argvIndex = 2;
1357 pIdxInfo->aConstraintUsage[pathIdx].omit = 1;
1358 pIdxInfo->idxNum = 3;
1359 }
1360 }
1361 return SQLITE_OK;
1362}
1363
1364/* Start a search on a new JSON string */
1365static int jsonEachFilter(
1366 sqlite3_vtab_cursor *cur,
1367 int idxNum, const char *idxStr,
1368 int argc, sqlite3_value **argv
1369){
1370 JsonEachCursor *p = (JsonEachCursor*)cur;
1371 const char *z;
1372 const char *zPath;
1373 sqlite3_int64 n;
1374
1375 jsonEachCursorReset(p);
1376 if( idxNum==0 ) return SQLITE_OK;
1377 z = (const char*)sqlite3_value_text(argv[0]);
1378 if( z==0 ) return SQLITE_OK;
1379 if( idxNum&2 ){
1380 zPath = (const char*)sqlite3_value_text(argv[1]);
1381 if( zPath==0 || zPath[0]!='$' ) return SQLITE_OK;
1382 }
1383 n = sqlite3_value_bytes(argv[0]);
1384 p->zJson = sqlite3_malloc( n+1 );
1385 if( p->zJson==0 ) return SQLITE_NOMEM;
1386 memcpy(p->zJson, z, n+1);
1387 if( jsonParse(&p->sParse, p->zJson) ){
1388 jsonEachCursorReset(p);
1389 }else{
1390 JsonNode *pNode;
1391 if( idxNum==3 ){
1392 n = sqlite3_value_bytes(argv[1]);
1393 p->zPath = sqlite3_malloc( n+1 );
1394 if( p->zPath==0 ) return SQLITE_NOMEM;
1395 memcpy(p->zPath, zPath, n+1);
1396 pNode = jsonLookup(&p->sParse, 0, p->zPath+1, 0);
1397 if( pNode==0 ){
1398 jsonEachCursorReset(p);
1399 return SQLITE_OK;
1400 }
1401 }else{
1402 pNode = p->sParse.aNode;
1403 }
1404 p->i = (int)(pNode - p->sParse.aNode);
1405 p->eType = pNode->eType;
1406 if( p->eType>=JSON_ARRAY ){
1407 p->i++;
1408 p->iEnd = p->i + pNode->n;
1409 }else{
1410 p->iEnd = p->i+1;
1411 }
1412 }
1413 return SQLITE_OK;
1414}
1415
1416/* The methods of the json_each virtual table */
1417static sqlite3_module jsonEachModule = {
1418 0, /* iVersion */
1419 0, /* xCreate */
1420 jsonEachConnect, /* xConnect */
1421 jsonEachBestIndex, /* xBestIndex */
1422 jsonEachDisconnect, /* xDisconnect */
1423 0, /* xDestroy */
1424 jsonEachOpen, /* xOpen - open a cursor */
1425 jsonEachClose, /* xClose - close a cursor */
1426 jsonEachFilter, /* xFilter - configure scan constraints */
1427 jsonEachNext, /* xNext - advance a cursor */
1428 jsonEachEof, /* xEof - check for end of scan */
1429 jsonEachColumn, /* xColumn - read data */
1430 jsonEachRowid, /* xRowid - read data */
1431 0, /* xUpdate */
1432 0, /* xBegin */
1433 0, /* xSync */
1434 0, /* xCommit */
1435 0, /* xRollback */
1436 0, /* xFindMethod */
1437 0, /* xRename */
1438};
1439
1440
drh5fa5c102015-08-12 16:49:40 +00001441#ifdef _WIN32
1442__declspec(dllexport)
1443#endif
1444int sqlite3_json_init(
1445 sqlite3 *db,
1446 char **pzErrMsg,
1447 const sqlite3_api_routines *pApi
1448){
1449 int rc = SQLITE_OK;
1450 int i;
1451 static const struct {
1452 const char *zName;
1453 int nArg;
drh52216ad2015-08-18 02:28:03 +00001454 int flag;
drh5fa5c102015-08-12 16:49:40 +00001455 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
1456 } aFunc[] = {
drh52216ad2015-08-18 02:28:03 +00001457 { "json_array", -1, 0, jsonArrayFunc },
1458 { "json_array_length", 1, 0, jsonArrayLengthFunc },
1459 { "json_array_length", 2, 0, jsonArrayLengthFunc },
1460 { "json_extract", 2, 0, jsonExtractFunc },
1461 { "json_insert", -1, 0, jsonSetFunc },
1462 { "json_object", -1, 0, jsonObjectFunc },
1463 { "json_remove", -1, 0, jsonRemoveFunc },
1464 { "json_replace", -1, 0, jsonReplaceFunc },
1465 { "json_set", -1, 1, jsonSetFunc },
1466 { "json_type", 1, 0, jsonTypeFunc },
1467 { "json_type", 2, 0, jsonTypeFunc },
drh987eb1f2015-08-17 15:17:37 +00001468
drh301eecc2015-08-17 20:14:19 +00001469#if SQLITE_DEBUG
drh987eb1f2015-08-17 15:17:37 +00001470 /* DEBUG and TESTING functions */
drh52216ad2015-08-18 02:28:03 +00001471 { "json_parse", 1, 0, jsonParseFunc },
1472 { "json_test1", 1, 0, jsonTest1Func },
1473 { "json_nodecount", 1, 0, jsonNodeCountFunc },
drh301eecc2015-08-17 20:14:19 +00001474#endif
drh5fa5c102015-08-12 16:49:40 +00001475 };
1476 SQLITE_EXTENSION_INIT2(pApi);
1477 (void)pzErrMsg; /* Unused parameter */
1478 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
1479 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
drh52216ad2015-08-18 02:28:03 +00001480 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1481 (void*)&aFunc[i].flag,
drh5fa5c102015-08-12 16:49:40 +00001482 aFunc[i].xFunc, 0, 0);
1483 }
drhcb6c6c62015-08-19 22:47:17 +00001484 if( rc==SQLITE_OK ){
1485 rc = sqlite3_create_module(db, "json_each", &jsonEachModule, 0);
1486 }
drh5fa5c102015-08-12 16:49:40 +00001487 return rc;
1488}