blob: 7e1d210e3701acd6cf34f70f5a099ef58b2a1f80 [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){
706 u32 i, j, k;
707 JsonNode *pRoot = &pParse->aNode[iRoot];
drh987eb1f2015-08-17 15:17:37 +0000708 if( zPath[0]==0 ) return pRoot;
709 if( zPath[0]=='.' ){
710 if( pRoot->eType!=JSON_OBJECT ) return 0;
711 zPath++;
712 for(i=0; isalnum(zPath[i]); i++){}
713 if( i==0 ) return 0;
714 j = 1;
drh52216ad2015-08-18 02:28:03 +0000715 for(;;){
716 while( j<=pRoot->n ){
717 if( pRoot[j].n==i+2
718 && strncmp(&pRoot[j].u.zJContent[1],zPath,i)==0
719 ){
720 return jsonLookup(pParse, iRoot+j+1, &zPath[i], pApnd);
721 }
722 j++;
723 j += jsonSize(&pRoot[j]);
drh987eb1f2015-08-17 15:17:37 +0000724 }
drh52216ad2015-08-18 02:28:03 +0000725 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
726 iRoot += pRoot->u.iAppend;
727 pRoot = &pParse->aNode[iRoot];
728 j = 1;
729 }
730 if( pApnd ){
731 k = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
732 pRoot->u.iAppend = k - iRoot;
733 pRoot->jnFlags |= JNODE_APPEND;
734 k = jsonParseAddNode(pParse, JSON_STRING, i, zPath);
735 if( !pParse->oom ) pParse->aNode[k].jnFlags |= JNODE_RAW;
736 zPath += i;
737 return jsonLookupAppend(pParse, zPath, pApnd);
drh987eb1f2015-08-17 15:17:37 +0000738 }
739 }else if( zPath[0]=='[' && isdigit(zPath[1]) ){
740 if( pRoot->eType!=JSON_ARRAY ) return 0;
741 i = 0;
742 zPath++;
743 while( isdigit(zPath[0]) ){
744 i = i + zPath[0] - '0';
745 zPath++;
746 }
747 if( zPath[0]!=']' ) return 0;
748 zPath++;
749 j = 1;
drh52216ad2015-08-18 02:28:03 +0000750 for(;;){
751 while( i>0 && j<=pRoot->n ){
752 j += jsonSize(&pRoot[j]);
753 i--;
754 }
755 if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
756 iRoot += pRoot->u.iAppend;
757 pRoot = &pParse->aNode[iRoot];
758 j = 1;
drh987eb1f2015-08-17 15:17:37 +0000759 }
760 if( j<=pRoot->n ){
drh52216ad2015-08-18 02:28:03 +0000761 return jsonLookup(pParse, iRoot+j, zPath, pApnd);
762 }
763 if( i==0 && pApnd ){
764 k = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
765 pRoot->u.iAppend = k - iRoot;
766 pRoot->jnFlags |= JNODE_APPEND;
767 return jsonLookupAppend(pParse, zPath, pApnd);
drh987eb1f2015-08-17 15:17:37 +0000768 }
769 }
770 return 0;
771}
772
drh52216ad2015-08-18 02:28:03 +0000773/*
774** Append content to pParse that will complete zPath.
775*/
776static JsonNode *jsonLookupAppend(
777 JsonParse *pParse, /* Append content to the JSON parse */
778 const char *zPath, /* Description of content to append */
779 int *pApnd /* Set this flag to 1 */
780){
781 *pApnd = 1;
782 if( zPath[0]==0 ){
783 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
784 return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
785 }
786 if( zPath[0]=='.' ){
787 jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
788 }else if( strncmp(zPath,"[0]",3)==0 ){
789 jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
790 }else{
791 return 0;
792 }
793 if( pParse->oom ) return 0;
794 return jsonLookup(pParse, pParse->nNode-1, zPath, pApnd);
795}
796
797
drh987eb1f2015-08-17 15:17:37 +0000798/****************************************************************************
799** SQL functions used for testing and debugging
800****************************************************************************/
drhe9c37f32015-08-15 21:25:36 +0000801
drh301eecc2015-08-17 20:14:19 +0000802#ifdef SQLITE_DEBUG
drhe9c37f32015-08-15 21:25:36 +0000803/*
drh5634cc02015-08-17 11:28:03 +0000804** The json_parse(JSON) function returns a string which describes
drhe9c37f32015-08-15 21:25:36 +0000805** a parse of the JSON provided. Or it returns NULL if JSON is not
806** well-formed.
807*/
drh5634cc02015-08-17 11:28:03 +0000808static void jsonParseFunc(
drhe9c37f32015-08-15 21:25:36 +0000809 sqlite3_context *context,
810 int argc,
811 sqlite3_value **argv
812){
813 Json s; /* Output string - not real JSON */
814 JsonParse x; /* The parse */
815 u32 i;
drh301eecc2015-08-17 20:14:19 +0000816 char zBuf[100];
drhe9c37f32015-08-15 21:25:36 +0000817
818 assert( argc==1 );
819 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
820 jsonInit(&s, context);
821 for(i=0; i<x.nNode; i++){
drh301eecc2015-08-17 20:14:19 +0000822 sqlite3_snprintf(sizeof(zBuf), zBuf, "node %3u: %7s n=%d\n",
823 i, jsonType[x.aNode[i].eType], x.aNode[i].n);
drhe9c37f32015-08-15 21:25:36 +0000824 jsonAppend(&s, zBuf);
drh52216ad2015-08-18 02:28:03 +0000825 if( x.aNode[i].u.zJContent!=0 ){
drh301eecc2015-08-17 20:14:19 +0000826 jsonAppendRaw(&s, " text: ", 10);
drh52216ad2015-08-18 02:28:03 +0000827 jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
drhe9c37f32015-08-15 21:25:36 +0000828 jsonAppendRaw(&s, "\n", 1);
829 }
830 }
831 sqlite3_free(x.aNode);
832 jsonResult(&s);
833}
834
drh5634cc02015-08-17 11:28:03 +0000835/*
836** The json_test1(JSON) function parses and rebuilds the JSON string.
837*/
838static void jsonTest1Func(
839 sqlite3_context *context,
840 int argc,
841 sqlite3_value **argv
842){
843 JsonParse x; /* The parse */
844 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
drhd0960592015-08-17 21:22:32 +0000845 jsonReturn(x.aNode, context, 0);
drh5634cc02015-08-17 11:28:03 +0000846 sqlite3_free(x.aNode);
847}
848
849/*
850** The json_nodecount(JSON) function returns the number of nodes in the
851** input JSON string.
852*/
853static void jsonNodeCountFunc(
854 sqlite3_context *context,
855 int argc,
856 sqlite3_value **argv
857){
858 JsonParse x; /* The parse */
859 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
860 sqlite3_result_int64(context, x.nNode);
861 sqlite3_free(x.aNode);
862}
drh301eecc2015-08-17 20:14:19 +0000863#endif /* SQLITE_DEBUG */
drh5634cc02015-08-17 11:28:03 +0000864
drh987eb1f2015-08-17 15:17:37 +0000865/****************************************************************************
866** SQL function implementations
867****************************************************************************/
868
869/*
870** Implementation of the json_array(VALUE,...) function. Return a JSON
871** array that contains all values given in arguments. Or if any argument
872** is a BLOB, throw an error.
873*/
874static void jsonArrayFunc(
875 sqlite3_context *context,
876 int argc,
877 sqlite3_value **argv
878){
879 int i;
880 Json jx;
drh987eb1f2015-08-17 15:17:37 +0000881
882 jsonInit(&jx, context);
drhd0960592015-08-17 21:22:32 +0000883 jsonAppendChar(&jx, '[');
drh987eb1f2015-08-17 15:17:37 +0000884 for(i=0; i<argc; i++){
drhd0960592015-08-17 21:22:32 +0000885 jsonAppendSeparator(&jx);
886 jsonAppendValue(&jx, argv[i]);
drh987eb1f2015-08-17 15:17:37 +0000887 }
drhd0960592015-08-17 21:22:32 +0000888 jsonAppendChar(&jx, ']');
drh987eb1f2015-08-17 15:17:37 +0000889 jsonResult(&jx);
890}
891
892
893/*
894** json_array_length(JSON)
895** json_array_length(JSON, PATH)
896**
897** Return the number of elements in the top-level JSON array.
898** Return 0 if the input is not a well-formed JSON array.
899*/
900static void jsonArrayLengthFunc(
901 sqlite3_context *context,
902 int argc,
903 sqlite3_value **argv
904){
905 JsonParse x; /* The parse */
906 sqlite3_int64 n = 0;
907 u32 i;
908 const char *zPath;
909
910 if( argc==2 ){
911 zPath = (const char*)sqlite3_value_text(argv[1]);
912 if( zPath==0 ) return;
913 if( zPath[0]!='$' ) return;
914 zPath++;
915 }else{
916 zPath = 0;
917 }
918 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0]))==0 ){
919 if( x.nNode ){
920 JsonNode *pNode = x.aNode;
drh52216ad2015-08-18 02:28:03 +0000921 if( zPath ) pNode = jsonLookup(&x, 0, zPath, 0);
drh987eb1f2015-08-17 15:17:37 +0000922 if( pNode->eType==JSON_ARRAY ){
drh52216ad2015-08-18 02:28:03 +0000923 assert( (pNode->jnFlags & JNODE_APPEND)==0 );
drh301eecc2015-08-17 20:14:19 +0000924 for(i=1; i<=pNode->n; n++){
drhd0960592015-08-17 21:22:32 +0000925 i += jsonSize(&pNode[i]);
drh987eb1f2015-08-17 15:17:37 +0000926 }
927 }
928 }
929 sqlite3_free(x.aNode);
930 }
931 sqlite3_result_int64(context, n);
932}
933
934/*
935** json_extract(JSON, PATH)
936**
937** Return the element described by PATH. Return NULL if JSON is not
938** valid JSON or if there is no PATH element or if PATH is malformed.
939*/
940static void jsonExtractFunc(
941 sqlite3_context *context,
942 int argc,
943 sqlite3_value **argv
944){
945 JsonParse x; /* The parse */
946 JsonNode *pNode;
947 const char *zPath;
948 assert( argc==2 );
949 zPath = (const char*)sqlite3_value_text(argv[1]);
950 if( zPath==0 ) return;
951 if( zPath[0]!='$' ) return;
952 zPath++;
953 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
drh52216ad2015-08-18 02:28:03 +0000954 pNode = jsonLookup(&x, 0, zPath, 0);
drh987eb1f2015-08-17 15:17:37 +0000955 if( pNode ){
drhd0960592015-08-17 21:22:32 +0000956 jsonReturn(pNode, context, 0);
drh987eb1f2015-08-17 15:17:37 +0000957 }
958 sqlite3_free(x.aNode);
959}
960
961/*
962** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
963** object that contains all name/value given in arguments. Or if any name
964** is not a string or if any value is a BLOB, throw an error.
965*/
966static void jsonObjectFunc(
967 sqlite3_context *context,
968 int argc,
969 sqlite3_value **argv
970){
971 int i;
972 Json jx;
drh987eb1f2015-08-17 15:17:37 +0000973 const char *z;
974 u32 n;
975
976 if( argc&1 ){
977 sqlite3_result_error(context, "json_object() requires an even number "
978 "of arguments", -1);
979 return;
980 }
981 jsonInit(&jx, context);
drhd0960592015-08-17 21:22:32 +0000982 jsonAppendChar(&jx, '{');
drh987eb1f2015-08-17 15:17:37 +0000983 for(i=0; i<argc; i+=2){
drh987eb1f2015-08-17 15:17:37 +0000984 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
985 sqlite3_result_error(context, "json_object() labels must be TEXT", -1);
986 jsonZero(&jx);
987 return;
988 }
drhd0960592015-08-17 21:22:32 +0000989 jsonAppendSeparator(&jx);
drh987eb1f2015-08-17 15:17:37 +0000990 z = (const char*)sqlite3_value_text(argv[i]);
991 n = (u32)sqlite3_value_bytes(argv[i]);
992 jsonAppendString(&jx, z, n);
drhd0960592015-08-17 21:22:32 +0000993 jsonAppendChar(&jx, ':');
994 jsonAppendValue(&jx, argv[i+1]);
drh987eb1f2015-08-17 15:17:37 +0000995 }
drhd0960592015-08-17 21:22:32 +0000996 jsonAppendChar(&jx, '}');
drh987eb1f2015-08-17 15:17:37 +0000997 jsonResult(&jx);
998}
999
1000
1001/*
drh301eecc2015-08-17 20:14:19 +00001002** json_remove(JSON, PATH, ...)
1003**
1004** Remove the named elements from JSON and return the result. Ill-formed
1005** PATH arguments are silently ignored. If JSON is ill-formed, then NULL
1006** is returned.
1007*/
1008static void jsonRemoveFunc(
1009 sqlite3_context *context,
1010 int argc,
1011 sqlite3_value **argv
1012){
1013 JsonParse x; /* The parse */
1014 JsonNode *pNode;
1015 const char *zPath;
1016 u32 i;
1017
1018 if( argc<1 ) return;
1019 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
1020 if( x.nNode ){
1021 for(i=1; i<argc; i++){
1022 zPath = (const char*)sqlite3_value_text(argv[i]);
1023 if( zPath==0 ) continue;
1024 if( zPath[0]!='$' ) continue;
drh52216ad2015-08-18 02:28:03 +00001025 pNode = jsonLookup(&x, 0, &zPath[1], 0);
drh301eecc2015-08-17 20:14:19 +00001026 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
1027 }
1028 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
drhd0960592015-08-17 21:22:32 +00001029 jsonReturn(x.aNode, context, 0);
1030 }
1031 }
1032 sqlite3_free(x.aNode);
1033}
1034
1035/*
1036** json_replace(JSON, PATH, VALUE, ...)
1037**
1038** Replace the value at PATH with VALUE. If PATH does not already exist,
1039** this routine is a no-op. If JSON is ill-formed, return NULL.
1040*/
1041static void jsonReplaceFunc(
1042 sqlite3_context *context,
1043 int argc,
1044 sqlite3_value **argv
1045){
1046 JsonParse x; /* The parse */
1047 JsonNode *pNode;
1048 const char *zPath;
1049 u32 i;
1050
1051 if( argc<1 ) return;
1052 if( (argc&1)==0 ) {
1053 sqlite3_result_error(context,
1054 "json_replace() needs an odd number of arguments", -1);
1055 return;
1056 }
1057 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
1058 if( x.nNode ){
1059 for(i=1; i<argc; i+=2){
1060 zPath = (const char*)sqlite3_value_text(argv[i]);
1061 if( zPath==0 ) continue;
1062 if( zPath[0]!='$' ) continue;
drh52216ad2015-08-18 02:28:03 +00001063 pNode = jsonLookup(&x, 0, &zPath[1], 0);
drhd0960592015-08-17 21:22:32 +00001064 if( pNode ){
1065 pNode->jnFlags |= JNODE_REPLACE;
1066 pNode->iVal = i+1;
1067 }
1068 }
1069 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
1070 sqlite3_result_value(context, argv[x.aNode[0].iVal]);
1071 }else{
1072 jsonReturn(x.aNode, context, argv);
drh301eecc2015-08-17 20:14:19 +00001073 }
1074 }
1075 sqlite3_free(x.aNode);
1076}
drh52216ad2015-08-18 02:28:03 +00001077/*
1078** json_set(JSON, PATH, VALUE, ...)
1079**
1080** Set the value at PATH to VALUE. Create the PATH if it does not already
1081** exist. Overwrite existing values that do exist.
1082** If JSON is ill-formed, return NULL.
1083**
1084** json_insert(JSON, PATH, VALUE, ...)
1085**
1086** Create PATH and initialize it to VALUE. If PATH already exists, this
1087** routine is a no-op. If JSON is ill-formed, return NULL.
1088*/
1089static void jsonSetFunc(
1090 sqlite3_context *context,
1091 int argc,
1092 sqlite3_value **argv
1093){
1094 JsonParse x; /* The parse */
1095 JsonNode *pNode;
1096 const char *zPath;
1097 u32 i;
1098 int bApnd;
1099 int bIsSet = *(int*)sqlite3_user_data(context);
1100
1101 if( argc<1 ) return;
1102 if( (argc&1)==0 ) {
1103 sqlite3_result_error(context,
1104 "json_set() needs an odd number of arguments", -1);
1105 return;
1106 }
1107 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
1108 if( x.nNode ){
1109 for(i=1; i<argc; i+=2){
1110 zPath = (const char*)sqlite3_value_text(argv[i]);
1111 if( zPath==0 ) continue;
1112 if( zPath[0]!='$' ) continue;
1113 bApnd = 0;
1114 pNode = jsonLookup(&x, 0, &zPath[1], &bApnd);
1115 if( pNode && (bApnd || bIsSet) ){
1116 pNode->jnFlags |= JNODE_REPLACE;
1117 pNode->iVal = i+1;
1118 }
1119 }
1120 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
1121 sqlite3_result_value(context, argv[x.aNode[0].iVal]);
1122 }else{
1123 jsonReturn(x.aNode, context, argv);
1124 }
1125 }
1126 sqlite3_free(x.aNode);
1127}
drh301eecc2015-08-17 20:14:19 +00001128
1129/*
drh987eb1f2015-08-17 15:17:37 +00001130** json_type(JSON)
1131** json_type(JSON, PATH)
1132**
1133** Return the top-level "type" of a JSON string. Return NULL if the
1134** input is not a well-formed JSON string.
1135*/
1136static void jsonTypeFunc(
1137 sqlite3_context *context,
1138 int argc,
1139 sqlite3_value **argv
1140){
1141 JsonParse x; /* The parse */
1142 const char *zPath;
1143
1144 if( argc==2 ){
1145 zPath = (const char*)sqlite3_value_text(argv[1]);
1146 if( zPath==0 ) return;
1147 if( zPath[0]!='$' ) return;
1148 zPath++;
1149 }else{
1150 zPath = 0;
1151 }
1152 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
1153 if( x.nNode ){
1154 JsonNode *pNode = x.aNode;
drh52216ad2015-08-18 02:28:03 +00001155 if( zPath ) pNode = jsonLookup(&x, 0, zPath, 0);
drh987eb1f2015-08-17 15:17:37 +00001156 sqlite3_result_text(context, jsonType[pNode->eType], -1, SQLITE_STATIC);
1157 }
1158 sqlite3_free(x.aNode);
1159}
drh5634cc02015-08-17 11:28:03 +00001160
drhcb6c6c62015-08-19 22:47:17 +00001161/****************************************************************************
1162** The json_each virtual table
1163****************************************************************************/
1164typedef struct JsonEachCursor JsonEachCursor;
1165struct JsonEachCursor {
1166 sqlite3_vtab_cursor base; /* Base class - must be first */
1167 u32 iRowid; /* The rowid */
1168 u32 i; /* Index in sParse.aNode[] of current row */
1169 u32 iEnd; /* EOF when i equals or exceeds this value */
1170 u8 eType; /* Type of top-level element */
1171 char *zJson; /* Input json */
1172 char *zPath; /* Path by which to filter zJson */
1173 JsonParse sParse; /* The input json */
1174};
1175
1176/* Constructor for the json_each virtual table */
1177static int jsonEachConnect(
1178 sqlite3 *db,
1179 void *pAux,
1180 int argc, const char *const*argv,
1181 sqlite3_vtab **ppVtab,
1182 char **pzErr
1183){
1184 sqlite3_vtab *pNew;
1185 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
1186 if( pNew==0 ) return SQLITE_NOMEM;
1187
1188/* Column numbers */
1189#define JEACH_KEY 0
1190#define JEACH_VALUE 1
1191#define JEACH_JSON 2
1192#define JEACH_PATH 3
1193
1194 sqlite3_declare_vtab(db, "CREATE TABLE x(key,value,json hidden,path hidden)");
1195 memset(pNew, 0, sizeof(*pNew));
1196 return SQLITE_OK;
1197}
1198
1199/* destructor for json_each virtual table */
1200static int jsonEachDisconnect(sqlite3_vtab *pVtab){
1201 sqlite3_free(pVtab);
1202 return SQLITE_OK;
1203}
1204
1205/* constructor for a JsonEachCursor object. */
1206static int jsonEachOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
1207 JsonEachCursor *pCur;
1208 pCur = sqlite3_malloc( sizeof(*pCur) );
1209 if( pCur==0 ) return SQLITE_NOMEM;
1210 memset(pCur, 0, sizeof(*pCur));
1211 *ppCursor = &pCur->base;
1212 return SQLITE_OK;
1213}
1214
1215/* Reset a JsonEachCursor back to its original state. Free any memory
1216** held. */
1217static void jsonEachCursorReset(JsonEachCursor *p){
1218 sqlite3_free(p->zJson);
1219 sqlite3_free(p->zPath);
1220 sqlite3_free(p->sParse.aNode);
1221 p->iRowid = 0;
1222 p->i = 0;
1223 p->iEnd = 0;
1224 p->eType = 0;
1225 memset(&p->sParse, 0, sizeof(p->sParse));
1226 p->zJson = 0;
1227 p->zPath = 0;
1228}
1229
1230/* Destructor for a jsonEachCursor object */
1231static int jsonEachClose(sqlite3_vtab_cursor *cur){
1232 JsonEachCursor *p = (JsonEachCursor*)cur;
1233 jsonEachCursorReset(p);
1234 sqlite3_free(cur);
1235 return SQLITE_OK;
1236}
1237
1238/* Return TRUE if the jsonEachCursor object has been advanced off the end
1239** of the JSON object */
1240static int jsonEachEof(sqlite3_vtab_cursor *cur){
1241 JsonEachCursor *p = (JsonEachCursor*)cur;
1242 return p->i >= p->iEnd;
1243}
1244
1245/* Advance the cursor to the next top-level element of the current
1246** JSON string */
1247static int jsonEachNext(sqlite3_vtab_cursor *cur){
1248 JsonEachCursor *p = (JsonEachCursor*)cur;
1249 switch( p->eType ){
1250 case JSON_ARRAY: {
1251 p->i += jsonSize(&p->sParse.aNode[p->i]);
1252 p->iRowid++;
1253 break;
1254 }
1255 case JSON_OBJECT: {
1256 p->i += 1 + jsonSize(&p->sParse.aNode[p->i+1]);
1257 p->iRowid++;
1258 break;
1259 }
1260 default: {
1261 p->i = p->iEnd;
1262 break;
1263 }
1264 }
1265 return SQLITE_OK;
1266}
1267
1268/* Return the value of a column */
1269static int jsonEachColumn(
1270 sqlite3_vtab_cursor *cur, /* The cursor */
1271 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
1272 int i /* Which column to return */
1273){
1274 JsonEachCursor *p = (JsonEachCursor*)cur;
1275 switch( i ){
1276 case JEACH_KEY: {
1277 if( p->eType==JSON_OBJECT ){
1278 jsonReturn(&p->sParse.aNode[p->i], ctx, 0);
1279 }else{
1280 sqlite3_result_int64(ctx, p->iRowid);
1281 }
1282 break;
1283 }
1284 case JEACH_VALUE: {
1285 if( p->eType==JSON_OBJECT ){
1286 jsonReturn(&p->sParse.aNode[p->i+1], ctx, 0);
1287 }else{
1288 jsonReturn(&p->sParse.aNode[p->i], ctx, 0);
1289 }
1290 break;
1291 }
1292 case JEACH_PATH: {
1293 const char *zPath = p->zPath;
1294 if( zPath==0 ) zPath = "$";
1295 sqlite3_result_text(ctx, zPath, -1, SQLITE_STATIC);
1296 break;
1297 }
1298 default: {
1299 sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
1300 break;
1301 }
1302 }
1303 return SQLITE_OK;
1304}
1305
1306/* Return the current rowid value */
1307static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
1308 JsonEachCursor *p = (JsonEachCursor*)cur;
1309 *pRowid = p->iRowid;
1310 return SQLITE_OK;
1311}
1312
1313/* The query strategy is to look for an equality constraint on the json
1314** column. Without such a constraint, the table cannot operate. idxNum is
1315** 1 if the constraint is found, 3 if the constraint and zPath are found,
1316** and 0 otherwise.
1317*/
1318static int jsonEachBestIndex(
1319 sqlite3_vtab *tab,
1320 sqlite3_index_info *pIdxInfo
1321){
1322 int i;
1323 int jsonIdx = -1;
1324 int pathIdx = -1;
1325 const struct sqlite3_index_constraint *pConstraint;
1326 pConstraint = pIdxInfo->aConstraint;
1327 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
1328 if( pConstraint->usable==0 ) continue;
1329 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
1330 switch( pConstraint->iColumn ){
1331 case JEACH_JSON: jsonIdx = i; break;
1332 case JEACH_PATH: pathIdx = i; break;
1333 default: /* no-op */ break;
1334 }
1335 }
1336 if( jsonIdx<0 ){
1337 pIdxInfo->idxNum = 0;
1338 pIdxInfo->estimatedCost = (double)2000000000;
1339 }else{
1340 pIdxInfo->estimatedCost = (double)1;
1341 pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1;
1342 pIdxInfo->aConstraintUsage[jsonIdx].omit = 1;
1343 if( pathIdx<0 ){
1344 pIdxInfo->idxNum = 1;
1345 }else{
1346 pIdxInfo->aConstraintUsage[pathIdx].argvIndex = 2;
1347 pIdxInfo->aConstraintUsage[pathIdx].omit = 1;
1348 pIdxInfo->idxNum = 3;
1349 }
1350 }
1351 return SQLITE_OK;
1352}
1353
1354/* Start a search on a new JSON string */
1355static int jsonEachFilter(
1356 sqlite3_vtab_cursor *cur,
1357 int idxNum, const char *idxStr,
1358 int argc, sqlite3_value **argv
1359){
1360 JsonEachCursor *p = (JsonEachCursor*)cur;
1361 const char *z;
1362 const char *zPath;
1363 sqlite3_int64 n;
1364
1365 jsonEachCursorReset(p);
1366 if( idxNum==0 ) return SQLITE_OK;
1367 z = (const char*)sqlite3_value_text(argv[0]);
1368 if( z==0 ) return SQLITE_OK;
1369 if( idxNum&2 ){
1370 zPath = (const char*)sqlite3_value_text(argv[1]);
1371 if( zPath==0 || zPath[0]!='$' ) return SQLITE_OK;
1372 }
1373 n = sqlite3_value_bytes(argv[0]);
1374 p->zJson = sqlite3_malloc( n+1 );
1375 if( p->zJson==0 ) return SQLITE_NOMEM;
1376 memcpy(p->zJson, z, n+1);
1377 if( jsonParse(&p->sParse, p->zJson) ){
1378 jsonEachCursorReset(p);
1379 }else{
1380 JsonNode *pNode;
1381 if( idxNum==3 ){
1382 n = sqlite3_value_bytes(argv[1]);
1383 p->zPath = sqlite3_malloc( n+1 );
1384 if( p->zPath==0 ) return SQLITE_NOMEM;
1385 memcpy(p->zPath, zPath, n+1);
1386 pNode = jsonLookup(&p->sParse, 0, p->zPath+1, 0);
1387 if( pNode==0 ){
1388 jsonEachCursorReset(p);
1389 return SQLITE_OK;
1390 }
1391 }else{
1392 pNode = p->sParse.aNode;
1393 }
1394 p->i = (int)(pNode - p->sParse.aNode);
1395 p->eType = pNode->eType;
1396 if( p->eType>=JSON_ARRAY ){
1397 p->i++;
1398 p->iEnd = p->i + pNode->n;
1399 }else{
1400 p->iEnd = p->i+1;
1401 }
1402 }
1403 return SQLITE_OK;
1404}
1405
1406/* The methods of the json_each virtual table */
1407static sqlite3_module jsonEachModule = {
1408 0, /* iVersion */
1409 0, /* xCreate */
1410 jsonEachConnect, /* xConnect */
1411 jsonEachBestIndex, /* xBestIndex */
1412 jsonEachDisconnect, /* xDisconnect */
1413 0, /* xDestroy */
1414 jsonEachOpen, /* xOpen - open a cursor */
1415 jsonEachClose, /* xClose - close a cursor */
1416 jsonEachFilter, /* xFilter - configure scan constraints */
1417 jsonEachNext, /* xNext - advance a cursor */
1418 jsonEachEof, /* xEof - check for end of scan */
1419 jsonEachColumn, /* xColumn - read data */
1420 jsonEachRowid, /* xRowid - read data */
1421 0, /* xUpdate */
1422 0, /* xBegin */
1423 0, /* xSync */
1424 0, /* xCommit */
1425 0, /* xRollback */
1426 0, /* xFindMethod */
1427 0, /* xRename */
1428};
1429
1430
drh5fa5c102015-08-12 16:49:40 +00001431#ifdef _WIN32
1432__declspec(dllexport)
1433#endif
1434int sqlite3_json_init(
1435 sqlite3 *db,
1436 char **pzErrMsg,
1437 const sqlite3_api_routines *pApi
1438){
1439 int rc = SQLITE_OK;
1440 int i;
1441 static const struct {
1442 const char *zName;
1443 int nArg;
drh52216ad2015-08-18 02:28:03 +00001444 int flag;
drh5fa5c102015-08-12 16:49:40 +00001445 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
1446 } aFunc[] = {
drh52216ad2015-08-18 02:28:03 +00001447 { "json_array", -1, 0, jsonArrayFunc },
1448 { "json_array_length", 1, 0, jsonArrayLengthFunc },
1449 { "json_array_length", 2, 0, jsonArrayLengthFunc },
1450 { "json_extract", 2, 0, jsonExtractFunc },
1451 { "json_insert", -1, 0, jsonSetFunc },
1452 { "json_object", -1, 0, jsonObjectFunc },
1453 { "json_remove", -1, 0, jsonRemoveFunc },
1454 { "json_replace", -1, 0, jsonReplaceFunc },
1455 { "json_set", -1, 1, jsonSetFunc },
1456 { "json_type", 1, 0, jsonTypeFunc },
1457 { "json_type", 2, 0, jsonTypeFunc },
drh987eb1f2015-08-17 15:17:37 +00001458
drh301eecc2015-08-17 20:14:19 +00001459#if SQLITE_DEBUG
drh987eb1f2015-08-17 15:17:37 +00001460 /* DEBUG and TESTING functions */
drh52216ad2015-08-18 02:28:03 +00001461 { "json_parse", 1, 0, jsonParseFunc },
1462 { "json_test1", 1, 0, jsonTest1Func },
1463 { "json_nodecount", 1, 0, jsonNodeCountFunc },
drh301eecc2015-08-17 20:14:19 +00001464#endif
drh5fa5c102015-08-12 16:49:40 +00001465 };
1466 SQLITE_EXTENSION_INIT2(pApi);
1467 (void)pzErrMsg; /* Unused parameter */
1468 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
1469 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
drh52216ad2015-08-18 02:28:03 +00001470 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
1471 (void*)&aFunc[i].flag,
drh5fa5c102015-08-12 16:49:40 +00001472 aFunc[i].xFunc, 0, 0);
1473 }
drhcb6c6c62015-08-19 22:47:17 +00001474 if( rc==SQLITE_OK ){
1475 rc = sqlite3_create_module(db, "json_each", &jsonEachModule, 0);
1476 }
drh5fa5c102015-08-12 16:49:40 +00001477 return rc;
1478}