blob: dc27349c0f61ec0e0830d4b49cff1adfa64a9ab6 [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
20** a BLOB, but there is no support for JSONB in the current implementation.)
drh5fa5c102015-08-12 16:49:40 +000021*/
22#include "sqlite3ext.h"
23SQLITE_EXTENSION_INIT1
24#include <assert.h>
25#include <string.h>
drhe9c37f32015-08-15 21:25:36 +000026#include <ctype.h>
drh987eb1f2015-08-17 15:17:37 +000027#include <stdlib.h>
drh5fa5c102015-08-12 16:49:40 +000028
29/* Unsigned integer types */
30typedef sqlite3_uint64 u64;
31typedef unsigned int u32;
32typedef unsigned char u8;
33
drh5634cc02015-08-17 11:28:03 +000034/* An instance of this object represents a JSON string
35** under construction. Really, this is a generic string accumulator
36** that can be and is used to create strings other than JSON.
drh5fa5c102015-08-12 16:49:40 +000037*/
38typedef struct Json Json;
39struct Json {
40 sqlite3_context *pCtx; /* Function context - put error messages here */
drh5634cc02015-08-17 11:28:03 +000041 char *zBuf; /* Append JSON content here */
drh5fa5c102015-08-12 16:49:40 +000042 u64 nAlloc; /* Bytes of storage available in zBuf[] */
43 u64 nUsed; /* Bytes of zBuf[] currently used */
44 u8 bStatic; /* True if zBuf is static space */
drhe9c37f32015-08-15 21:25:36 +000045 u8 oom; /* True if an OOM has been encountered */
drh5fa5c102015-08-12 16:49:40 +000046 char zSpace[100]; /* Initial static space */
47};
48
drhe9c37f32015-08-15 21:25:36 +000049/* JSON type values
drhbd0621b2015-08-13 13:54:59 +000050*/
drhe9c37f32015-08-15 21:25:36 +000051#define JSON_NULL 0
52#define JSON_TRUE 1
53#define JSON_FALSE 2
54#define JSON_INT 3
55#define JSON_REAL 4
56#define JSON_STRING 5
57#define JSON_ARRAY 6
58#define JSON_OBJECT 7
59
drh987eb1f2015-08-17 15:17:37 +000060/*
61** Names of the various JSON types:
62*/
63static const char * const jsonType[] = {
64 "null", "true", "false", "integer", "real", "text", "array", "object"
65};
66
drh301eecc2015-08-17 20:14:19 +000067/* Bit values for the JsonNode.jnFlag field
68*/
69#define JNODE_RAW 0x01 /* Content is raw, not JSON encoded */
70#define JNODE_ESCAPE 0x02 /* Content is text with \ escapes */
71#define JNODE_REMOVE 0x04 /* Do not output */
72
drh987eb1f2015-08-17 15:17:37 +000073
drhe9c37f32015-08-15 21:25:36 +000074/* A single node of parsed JSON
75*/
76typedef struct JsonNode JsonNode;
77struct JsonNode {
drh5634cc02015-08-17 11:28:03 +000078 u8 eType; /* One of the JSON_ type values */
drh301eecc2015-08-17 20:14:19 +000079 u8 jnFlags; /* JNODE flags */
drhe9c37f32015-08-15 21:25:36 +000080 u32 n; /* Bytes of content, or number of sub-nodes */
drh5634cc02015-08-17 11:28:03 +000081 const char *zJContent; /* JSON content */
drhe9c37f32015-08-15 21:25:36 +000082};
83
84/* A completely parsed JSON string
85*/
86typedef struct JsonParse JsonParse;
87struct JsonParse {
88 u32 nNode; /* Number of slots of aNode[] used */
89 u32 nAlloc; /* Number of slots of aNode[] allocated */
90 JsonNode *aNode; /* Array of nodes containing the parse */
91 const char *zJson; /* Original JSON string */
92 u8 oom; /* Set to true if out of memory */
93};
94
drh301eecc2015-08-17 20:14:19 +000095/*
96** Return the number of consecutive JsonNode slots need to represent
97** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
98** OBJECT types, the number might be larger.
99*/
100static u32 jsonSizeof(JsonNode *pNode){
101 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
102}
103
drh5fa5c102015-08-12 16:49:40 +0000104/* Set the Json object to an empty string
105*/
106static void jsonZero(Json *p){
107 p->zBuf = p->zSpace;
108 p->nAlloc = sizeof(p->zSpace);
109 p->nUsed = 0;
110 p->bStatic = 1;
111}
112
113/* Initialize the Json object
114*/
115static void jsonInit(Json *p, sqlite3_context *pCtx){
116 p->pCtx = pCtx;
drhe9c37f32015-08-15 21:25:36 +0000117 p->oom = 0;
drh5fa5c102015-08-12 16:49:40 +0000118 jsonZero(p);
119}
120
121
122/* Free all allocated memory and reset the Json object back to its
123** initial state.
124*/
125static void jsonReset(Json *p){
126 if( !p->bStatic ) sqlite3_free(p->zBuf);
127 jsonZero(p);
128}
129
130
131/* Report an out-of-memory (OOM) condition
132*/
133static void jsonOom(Json *p){
drhe9c37f32015-08-15 21:25:36 +0000134 p->oom = 1;
drh5fa5c102015-08-12 16:49:40 +0000135 sqlite3_result_error_nomem(p->pCtx);
136 jsonReset(p);
137}
138
139/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
140** Return zero on success. Return non-zero on an OOM error
141*/
142static int jsonGrow(Json *p, u32 N){
drh301eecc2015-08-17 20:14:19 +0000143 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
drh5fa5c102015-08-12 16:49:40 +0000144 char *zNew;
145 if( p->bStatic ){
drhe9c37f32015-08-15 21:25:36 +0000146 if( p->oom ) return SQLITE_NOMEM;
drh5fa5c102015-08-12 16:49:40 +0000147 zNew = sqlite3_malloc64(nTotal);
148 if( zNew==0 ){
149 jsonOom(p);
150 return SQLITE_NOMEM;
151 }
152 memcpy(zNew, p->zBuf, p->nUsed);
153 p->zBuf = zNew;
154 p->bStatic = 0;
155 }else{
156 zNew = sqlite3_realloc64(p->zBuf, nTotal);
157 if( zNew==0 ){
158 jsonOom(p);
159 return SQLITE_NOMEM;
160 }
161 p->zBuf = zNew;
162 }
163 p->nAlloc = nTotal;
164 return SQLITE_OK;
165}
166
167/* Append N bytes from zIn onto the end of the Json string.
168*/
169static void jsonAppendRaw(Json *p, const char *zIn, u32 N){
170 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
171 memcpy(p->zBuf+p->nUsed, zIn, N);
172 p->nUsed += N;
173}
174
drhe9c37f32015-08-15 21:25:36 +0000175/* Append the zero-terminated string zIn
176*/
177static void jsonAppend(Json *p, const char *zIn){
178 jsonAppendRaw(p, zIn, (u32)strlen(zIn));
179}
180
drh5634cc02015-08-17 11:28:03 +0000181/* Append a single character
182*/
183static void jsonAppendChar(Json *p, char c){
184 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
185 p->zBuf[p->nUsed++] = c;
186}
187
drh301eecc2015-08-17 20:14:19 +0000188/* Append a comma separator to the output buffer, if the previous
189** character is not '[' or '{'.
190*/
191static void jsonAppendSeparator(Json *p){
192 char c;
193 if( p->nUsed==0 ) return;
194 c = p->zBuf[p->nUsed-1];
195 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
196}
197
drh5fa5c102015-08-12 16:49:40 +0000198/* Append the N-byte string in zIn to the end of the Json string
199** under construction. Enclose the string in "..." and escape
200** any double-quotes or backslash characters contained within the
201** string.
202*/
203static void jsonAppendString(Json *p, const char *zIn, u32 N){
204 u32 i;
205 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
206 p->zBuf[p->nUsed++] = '"';
207 for(i=0; i<N; i++){
208 char c = zIn[i];
209 if( c=='"' || c=='\\' ){
210 if( (p->nUsed+N+1-i > p->nAlloc) && jsonGrow(p,N+1-i)!=0 ) return;
211 p->zBuf[p->nUsed++] = '\\';
212 }
213 p->zBuf[p->nUsed++] = c;
214 }
215 p->zBuf[p->nUsed++] = '"';
216}
217
drhbd0621b2015-08-13 13:54:59 +0000218/* Make the JSON in p the result of the SQL function.
drh5fa5c102015-08-12 16:49:40 +0000219*/
220static void jsonResult(Json *p){
drhe9c37f32015-08-15 21:25:36 +0000221 if( p->oom==0 ){
drh5fa5c102015-08-12 16:49:40 +0000222 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
223 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
224 SQLITE_UTF8);
225 jsonZero(p);
226 }
227 assert( p->bStatic );
228}
229
drh5634cc02015-08-17 11:28:03 +0000230/*
231** Convert the JsonNode pNode into a pure JSON string and
232** append to pOut. Subsubstructure is also included. Return
233** the number of JsonNode objects that are encoded.
drhbd0621b2015-08-13 13:54:59 +0000234*/
drh5634cc02015-08-17 11:28:03 +0000235static int jsonRenderNode(JsonNode *pNode, Json *pOut){
drh301eecc2015-08-17 20:14:19 +0000236 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000237 switch( pNode->eType ){
238 case JSON_NULL: {
239 jsonAppendRaw(pOut, "null", 4);
240 break;
241 }
242 case JSON_TRUE: {
243 jsonAppendRaw(pOut, "true", 4);
244 break;
245 }
246 case JSON_FALSE: {
247 jsonAppendRaw(pOut, "false", 5);
248 break;
249 }
250 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000251 if( pNode->jnFlags & JNODE_RAW ){
drh5634cc02015-08-17 11:28:03 +0000252 jsonAppendString(pOut, pNode->zJContent, pNode->n);
253 break;
254 }
255 /* Fall through into the next case */
256 }
257 case JSON_REAL:
258 case JSON_INT: {
259 jsonAppendRaw(pOut, pNode->zJContent, pNode->n);
260 break;
261 }
262 case JSON_ARRAY: {
263 jsonAppendChar(pOut, '[');
drh301eecc2015-08-17 20:14:19 +0000264 while( j<=pNode->n ){
265 if( pNode[j].jnFlags & JNODE_REMOVE ){
266 j += jsonSizeof(&pNode[j]);
267 }else{
268 jsonAppendSeparator(pOut);
269 j += jsonRenderNode(&pNode[j], pOut);
270 }
drh5634cc02015-08-17 11:28:03 +0000271 }
272 jsonAppendChar(pOut, ']');
273 break;
274 }
275 case JSON_OBJECT: {
276 jsonAppendChar(pOut, '{');
drh301eecc2015-08-17 20:14:19 +0000277 while( j<=pNode->n ){
278 if( pNode[j+1].jnFlags & JNODE_REMOVE ){
279 j += 1 + jsonSizeof(&pNode[j+1]);
280 }else{
281 jsonAppendSeparator(pOut);
282 jsonRenderNode(&pNode[j], pOut);
283 jsonAppendChar(pOut, ':');
284 j += 1 + jsonRenderNode(&pNode[j+1], pOut);
285 }
drh5634cc02015-08-17 11:28:03 +0000286 }
287 jsonAppendChar(pOut, '}');
288 break;
289 }
drhbd0621b2015-08-13 13:54:59 +0000290 }
drh301eecc2015-08-17 20:14:19 +0000291 return j;
drh5634cc02015-08-17 11:28:03 +0000292}
293
294/*
295** Make the JsonNode the return value of the function.
296*/
297static void jsonReturn(JsonNode *pNode, sqlite3_context *pCtx){
298 switch( pNode->eType ){
299 case JSON_NULL: {
300 sqlite3_result_null(pCtx);
301 break;
302 }
303 case JSON_TRUE: {
304 sqlite3_result_int(pCtx, 1);
305 break;
306 }
307 case JSON_FALSE: {
308 sqlite3_result_int(pCtx, 0);
309 break;
310 }
drh987eb1f2015-08-17 15:17:37 +0000311 case JSON_REAL: {
312 double r = strtod(pNode->zJContent, 0);
313 sqlite3_result_double(pCtx, r);
drh5634cc02015-08-17 11:28:03 +0000314 break;
315 }
drh987eb1f2015-08-17 15:17:37 +0000316 case JSON_INT: {
317 sqlite3_int64 i = 0;
318 const char *z = pNode->zJContent;
319 if( z[0]=='-' ){ z++; }
320 while( z[0]>='0' && z[0]<='9' ){ i = i*10 + *(z++) - '0'; }
321 if( pNode->zJContent[0]=='-' ){ i = -i; }
322 sqlite3_result_int64(pCtx, i);
323 break;
324 }
drh5634cc02015-08-17 11:28:03 +0000325 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000326 if( pNode->jnFlags & JNODE_RAW ){
drh5634cc02015-08-17 11:28:03 +0000327 sqlite3_result_text(pCtx, pNode->zJContent, pNode->n, SQLITE_TRANSIENT);
drh301eecc2015-08-17 20:14:19 +0000328 }else if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
drh987eb1f2015-08-17 15:17:37 +0000329 /* JSON formatted without any backslash-escapes */
330 sqlite3_result_text(pCtx, pNode->zJContent+1, pNode->n-2,
331 SQLITE_TRANSIENT);
drh5634cc02015-08-17 11:28:03 +0000332 }else{
333 /* Translate JSON formatted string into raw text */
drh987eb1f2015-08-17 15:17:37 +0000334 u32 i;
335 u32 n = pNode->n;
336 const char *z = pNode->zJContent;
337 char *zOut;
338 u32 j;
339 zOut = sqlite3_malloc( n+1 );
340 if( zOut==0 ){
341 sqlite3_result_error_nomem(pCtx);
342 break;
343 }
344 for(i=1, j=0; i<n-1; i++){
345 char c = z[i];
346 if( c!='\\' && z[i+1] ){
347 zOut[j++] = c;
348 }else{
349 c = z[++i];
350 if( c=='u' && z[1] ){
351 u32 v = 0, k;
352 z++;
353 for(k=0; k<4 && z[k]; k++){
354 c = z[0];
355 if( c>='0' && c<='9' ) v = v*16 + c - '0';
356 else if( c>='A' && c<='F' ) v = v*16 + c - 'A' + 10;
357 else if( c>='a' && c<='f' ) v = v*16 + c - 'a' + 10;
358 else break;
359 z++;
360 }
361 if( v<=0x7f ){
362 zOut[j++] = v;
363 }else if( v<=0x7ff ){
364 zOut[j++] = 0xc0 | (v>>6);
365 zOut[j++] = 0x80 | (v&0x3f);
366 }else if( v<=0xffff ){
367 zOut[j++] = 0xe0 | (v>>12);
368 zOut[j++] = 0x80 | ((v>>6)&0x3f);
369 zOut[j++] = 0x80 | (v&0x3f);
370 }else if( v<=0x10ffff ){
371 zOut[j++] = 0xf0 | (v>>18);
372 zOut[j++] = 0x80 | ((v>>12)&0x3f);
373 zOut[j++] = 0x80 | ((v>>6)&0x3f);
374 zOut[j++] = 0x80 | (v&0x3f);
375 }
376 }else{
377 if( c=='b' ){
378 c = '\b';
379 }else if( c=='f' ){
380 c = '\f';
381 }else if( c=='n' ){
382 c = '\n';
383 }else if( c=='r' ){
384 c = '\r';
385 }else if( c=='t' ){
386 c = '\t';
387 }
388 zOut[j++] = c;
389 }
390 }
391 }
392 zOut[j] = 0;
393 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
drh5634cc02015-08-17 11:28:03 +0000394 }
395 break;
396 }
397 case JSON_ARRAY:
398 case JSON_OBJECT: {
399 Json s;
400 jsonInit(&s, pCtx);
401 jsonRenderNode(pNode, &s);
402 jsonResult(&s);
403 break;
404 }
405 }
drhbd0621b2015-08-13 13:54:59 +0000406}
407
drh5fa5c102015-08-12 16:49:40 +0000408/*
drhe9c37f32015-08-15 21:25:36 +0000409** Create a new JsonNode instance based on the arguments and append that
410** instance to the JsonParse. Return the index in pParse->aNode[] of the
411** new node, or -1 if a memory allocation fails.
412*/
413static int jsonParseAddNode(
414 JsonParse *pParse, /* Append the node to this object */
415 u32 eType, /* Node type */
416 u32 n, /* Content size or sub-node count */
417 const char *zContent /* Content */
418){
419 JsonNode *p;
420 if( pParse->nNode>=pParse->nAlloc ){
421 u32 nNew;
422 JsonNode *pNew;
423 if( pParse->oom ) return -1;
424 nNew = pParse->nAlloc*2 + 10;
425 if( nNew<=pParse->nNode ){
426 pParse->oom = 1;
427 return -1;
428 }
429 pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew);
430 if( pNew==0 ){
431 pParse->oom = 1;
432 return -1;
433 }
434 pParse->nAlloc = nNew;
435 pParse->aNode = pNew;
436 }
437 p = &pParse->aNode[pParse->nNode];
drh5634cc02015-08-17 11:28:03 +0000438 p->eType = (u8)eType;
drh301eecc2015-08-17 20:14:19 +0000439 p->jnFlags = 0;
drhe9c37f32015-08-15 21:25:36 +0000440 p->n = n;
drh5634cc02015-08-17 11:28:03 +0000441 p->zJContent = zContent;
drhe9c37f32015-08-15 21:25:36 +0000442 return pParse->nNode++;
443}
444
445/*
446** Parse a single JSON value which begins at pParse->zJson[i]. Return the
447** index of the first character past the end of the value parsed.
448**
449** Return negative for a syntax error. Special cases: return -2 if the
450** first non-whitespace character is '}' and return -3 if the first
451** non-whitespace character is ']'.
452*/
453static int jsonParseValue(JsonParse *pParse, u32 i){
454 char c;
455 u32 j;
456 u32 iThis;
457 int x;
458 while( isspace(pParse->zJson[i]) ){ i++; }
459 if( (c = pParse->zJson[i])==0 ) return 0;
460 if( c=='{' ){
461 /* Parse object */
462 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
463 if( iThis<0 ) return -1;
464 for(j=i+1;;j++){
465 while( isspace(pParse->zJson[j]) ){ j++; }
466 x = jsonParseValue(pParse, j);
467 if( x<0 ){
468 if( x==(-2) && pParse->nNode==iThis+1 ) return j+1;
469 return -1;
470 }
471 if( pParse->aNode[pParse->nNode-1].eType!=JSON_STRING ) return -1;
472 j = x;
473 while( isspace(pParse->zJson[j]) ){ j++; }
474 if( pParse->zJson[j]!=':' ) return -1;
475 j++;
476 x = jsonParseValue(pParse, j);
477 if( x<0 ) return -1;
478 j = x;
479 while( isspace(pParse->zJson[j]) ){ j++; }
480 c = pParse->zJson[j];
481 if( c==',' ) continue;
482 if( c!='}' ) return -1;
483 break;
484 }
485 pParse->aNode[iThis].n = pParse->nNode - iThis - 1;
486 return j+1;
487 }else if( c=='[' ){
488 /* Parse array */
489 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
490 if( iThis<0 ) return -1;
491 for(j=i+1;;j++){
492 while( isspace(pParse->zJson[j]) ){ j++; }
493 x = jsonParseValue(pParse, j);
494 if( x<0 ){
495 if( x==(-3) && pParse->nNode==iThis+1 ) return j+1;
496 return -1;
497 }
498 j = x;
499 while( isspace(pParse->zJson[j]) ){ j++; }
500 c = pParse->zJson[j];
501 if( c==',' ) continue;
502 if( c!=']' ) return -1;
503 break;
504 }
505 pParse->aNode[iThis].n = pParse->nNode - iThis - 1;
506 return j+1;
507 }else if( c=='"' ){
508 /* Parse string */
drh301eecc2015-08-17 20:14:19 +0000509 u8 jnFlags = 0;
drhe9c37f32015-08-15 21:25:36 +0000510 j = i+1;
511 for(;;){
512 c = pParse->zJson[j];
513 if( c==0 ) return -1;
514 if( c=='\\' ){
515 c = pParse->zJson[++j];
516 if( c==0 ) return -1;
drh301eecc2015-08-17 20:14:19 +0000517 jnFlags = JNODE_ESCAPE;
drhe9c37f32015-08-15 21:25:36 +0000518 }else if( c=='"' ){
519 break;
520 }
521 j++;
522 }
523 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]);
drh301eecc2015-08-17 20:14:19 +0000524 pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
drhe9c37f32015-08-15 21:25:36 +0000525 return j+1;
526 }else if( c=='n'
527 && strncmp(pParse->zJson+i,"null",4)==0
drhb2cd10e2015-08-15 21:29:14 +0000528 && !isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000529 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
530 return i+4;
531 }else if( c=='t'
532 && strncmp(pParse->zJson+i,"true",4)==0
drhb2cd10e2015-08-15 21:29:14 +0000533 && !isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000534 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
535 return i+4;
536 }else if( c=='f'
537 && strncmp(pParse->zJson+i,"false",5)==0
drhb2cd10e2015-08-15 21:29:14 +0000538 && !isalnum(pParse->zJson[i+5]) ){
drhe9c37f32015-08-15 21:25:36 +0000539 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
540 return i+5;
541 }else if( c=='-' || (c>='0' && c<='9') ){
542 /* Parse number */
543 u8 seenDP = 0;
544 u8 seenE = 0;
545 j = i+1;
546 for(;; j++){
547 c = pParse->zJson[j];
548 if( c>='0' && c<='9' ) continue;
549 if( c=='.' ){
550 if( pParse->zJson[j-1]=='-' ) return -1;
551 if( seenDP ) return -1;
552 seenDP = 1;
553 continue;
554 }
555 if( c=='e' || c=='E' ){
556 if( pParse->zJson[j-1]<'0' ) return -1;
557 if( seenE ) return -1;
558 seenDP = seenE = 1;
559 c = pParse->zJson[j+1];
560 if( c=='+' || c=='-' ) j++;
561 continue;
562 }
563 break;
564 }
565 if( pParse->zJson[j-1]<'0' ) return -1;
566 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
567 j - i, &pParse->zJson[i]);
568 return j;
569 }else if( c=='}' ){
570 return -2; /* End of {...} */
571 }else if( c==']' ){
572 return -3; /* End of [...] */
573 }else{
574 return -1; /* Syntax error */
575 }
576}
577
578/*
579** Parse a complete JSON string. Return 0 on success or non-zero if there
580** are any errors. If an error occurs, free all memory associated with
581** pParse.
582**
583** pParse is uninitialized when this routine is called.
584*/
585static int jsonParse(JsonParse *pParse, const char *zJson){
586 int i;
587 if( zJson==0 ) return 1;
588 memset(pParse, 0, sizeof(*pParse));
589 pParse->zJson = zJson;
590 i = jsonParseValue(pParse, 0);
591 if( i>0 ){
592 while( isspace(zJson[i]) ) i++;
593 if( zJson[i] ) i = -1;
594 }
595 if( i<0 ){
596 sqlite3_free(pParse->aNode);
597 pParse->aNode = 0;
598 pParse->nNode = 0;
599 pParse->nAlloc = 0;
600 return 1;
601 }
602 return 0;
603}
drh301eecc2015-08-17 20:14:19 +0000604
drh987eb1f2015-08-17 15:17:37 +0000605/*
606** Search along zPath to find the node specified. Return a pointer
607** to that node, or NULL if zPath is malformed or if there is no such
608** node.
609*/
610static JsonNode *jsonLookup(JsonNode *pRoot, const char *zPath){
611 u32 i, j;
612 if( zPath[0]==0 ) return pRoot;
613 if( zPath[0]=='.' ){
614 if( pRoot->eType!=JSON_OBJECT ) return 0;
615 zPath++;
616 for(i=0; isalnum(zPath[i]); i++){}
617 if( i==0 ) return 0;
618 j = 1;
619 while( j<=pRoot->n ){
620 if( pRoot[j].n==i+2
621 && strncmp(&pRoot[j].zJContent[1],zPath,i)==0
622 ){
623 return jsonLookup(&pRoot[j+1], &zPath[i]);
624 }
625 j++;
drh301eecc2015-08-17 20:14:19 +0000626 j += jsonSizeof(&pRoot[j]);
drh987eb1f2015-08-17 15:17:37 +0000627 }
628 }else if( zPath[0]=='[' && isdigit(zPath[1]) ){
629 if( pRoot->eType!=JSON_ARRAY ) return 0;
630 i = 0;
631 zPath++;
632 while( isdigit(zPath[0]) ){
633 i = i + zPath[0] - '0';
634 zPath++;
635 }
636 if( zPath[0]!=']' ) return 0;
637 zPath++;
638 j = 1;
639 while( i>0 && j<=pRoot->n ){
drh301eecc2015-08-17 20:14:19 +0000640 j += jsonSizeof(&pRoot[j]);
drh987eb1f2015-08-17 15:17:37 +0000641 i--;
642 }
643 if( j<=pRoot->n ){
644 return jsonLookup(&pRoot[j], zPath);
645 }
646 }
647 return 0;
648}
649
650/****************************************************************************
651** SQL functions used for testing and debugging
652****************************************************************************/
drhe9c37f32015-08-15 21:25:36 +0000653
drh301eecc2015-08-17 20:14:19 +0000654#ifdef SQLITE_DEBUG
drhe9c37f32015-08-15 21:25:36 +0000655/*
drh5634cc02015-08-17 11:28:03 +0000656** The json_parse(JSON) function returns a string which describes
drhe9c37f32015-08-15 21:25:36 +0000657** a parse of the JSON provided. Or it returns NULL if JSON is not
658** well-formed.
659*/
drh5634cc02015-08-17 11:28:03 +0000660static void jsonParseFunc(
drhe9c37f32015-08-15 21:25:36 +0000661 sqlite3_context *context,
662 int argc,
663 sqlite3_value **argv
664){
665 Json s; /* Output string - not real JSON */
666 JsonParse x; /* The parse */
667 u32 i;
drh301eecc2015-08-17 20:14:19 +0000668 char zBuf[100];
drhe9c37f32015-08-15 21:25:36 +0000669
670 assert( argc==1 );
671 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
672 jsonInit(&s, context);
673 for(i=0; i<x.nNode; i++){
drh301eecc2015-08-17 20:14:19 +0000674 sqlite3_snprintf(sizeof(zBuf), zBuf, "node %3u: %7s n=%d\n",
675 i, jsonType[x.aNode[i].eType], x.aNode[i].n);
drhe9c37f32015-08-15 21:25:36 +0000676 jsonAppend(&s, zBuf);
drh5634cc02015-08-17 11:28:03 +0000677 if( x.aNode[i].zJContent!=0 ){
drh301eecc2015-08-17 20:14:19 +0000678 jsonAppendRaw(&s, " text: ", 10);
drh5634cc02015-08-17 11:28:03 +0000679 jsonAppendRaw(&s, x.aNode[i].zJContent, x.aNode[i].n);
drhe9c37f32015-08-15 21:25:36 +0000680 jsonAppendRaw(&s, "\n", 1);
681 }
682 }
683 sqlite3_free(x.aNode);
684 jsonResult(&s);
685}
686
drh5634cc02015-08-17 11:28:03 +0000687/*
688** The json_test1(JSON) function parses and rebuilds the JSON string.
689*/
690static void jsonTest1Func(
691 sqlite3_context *context,
692 int argc,
693 sqlite3_value **argv
694){
695 JsonParse x; /* The parse */
696 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
697 jsonReturn(x.aNode, context);
698 sqlite3_free(x.aNode);
699}
700
701/*
702** The json_nodecount(JSON) function returns the number of nodes in the
703** input JSON string.
704*/
705static void jsonNodeCountFunc(
706 sqlite3_context *context,
707 int argc,
708 sqlite3_value **argv
709){
710 JsonParse x; /* The parse */
711 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
712 sqlite3_result_int64(context, x.nNode);
713 sqlite3_free(x.aNode);
714}
drh301eecc2015-08-17 20:14:19 +0000715#endif /* SQLITE_DEBUG */
drh5634cc02015-08-17 11:28:03 +0000716
drh987eb1f2015-08-17 15:17:37 +0000717/****************************************************************************
718** SQL function implementations
719****************************************************************************/
720
721/*
722** Implementation of the json_array(VALUE,...) function. Return a JSON
723** array that contains all values given in arguments. Or if any argument
724** is a BLOB, throw an error.
725*/
726static void jsonArrayFunc(
727 sqlite3_context *context,
728 int argc,
729 sqlite3_value **argv
730){
731 int i;
732 Json jx;
733 char cSep = '[';
734
735 jsonInit(&jx, context);
736 for(i=0; i<argc; i++){
737 jsonAppendRaw(&jx, &cSep, 1);
738 cSep = ',';
739 switch( sqlite3_value_type(argv[i]) ){
740 case SQLITE_NULL: {
741 jsonAppendRaw(&jx, "null", 4);
742 break;
743 }
744 case SQLITE_INTEGER:
745 case SQLITE_FLOAT: {
746 const char *z = (const char*)sqlite3_value_text(argv[i]);
747 u32 n = (u32)sqlite3_value_bytes(argv[i]);
748 jsonAppendRaw(&jx, z, n);
749 break;
750 }
751 case SQLITE_TEXT: {
752 const char *z = (const char*)sqlite3_value_text(argv[i]);
753 u32 n = (u32)sqlite3_value_bytes(argv[i]);
754 jsonAppendString(&jx, z, n);
755 break;
756 }
757 default: {
758 jsonZero(&jx);
759 sqlite3_result_error(context, "JSON cannot hold BLOB values", -1);
760 return;
761 }
762 }
763 }
764 jsonAppendRaw(&jx, "]", 1);
765 jsonResult(&jx);
766}
767
768
769/*
770** json_array_length(JSON)
771** json_array_length(JSON, PATH)
772**
773** Return the number of elements in the top-level JSON array.
774** Return 0 if the input is not a well-formed JSON array.
775*/
776static void jsonArrayLengthFunc(
777 sqlite3_context *context,
778 int argc,
779 sqlite3_value **argv
780){
781 JsonParse x; /* The parse */
782 sqlite3_int64 n = 0;
783 u32 i;
784 const char *zPath;
785
786 if( argc==2 ){
787 zPath = (const char*)sqlite3_value_text(argv[1]);
788 if( zPath==0 ) return;
789 if( zPath[0]!='$' ) return;
790 zPath++;
791 }else{
792 zPath = 0;
793 }
794 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0]))==0 ){
795 if( x.nNode ){
796 JsonNode *pNode = x.aNode;
797 if( zPath ) pNode = jsonLookup(pNode, zPath);
798 if( pNode->eType==JSON_ARRAY ){
drh301eecc2015-08-17 20:14:19 +0000799 for(i=1; i<=pNode->n; n++){
800 i += jsonSizeof(&pNode[i]);
drh987eb1f2015-08-17 15:17:37 +0000801 }
802 }
803 }
804 sqlite3_free(x.aNode);
805 }
806 sqlite3_result_int64(context, n);
807}
808
809/*
810** json_extract(JSON, PATH)
811**
812** Return the element described by PATH. Return NULL if JSON is not
813** valid JSON or if there is no PATH element or if PATH is malformed.
814*/
815static void jsonExtractFunc(
816 sqlite3_context *context,
817 int argc,
818 sqlite3_value **argv
819){
820 JsonParse x; /* The parse */
821 JsonNode *pNode;
822 const char *zPath;
823 assert( argc==2 );
824 zPath = (const char*)sqlite3_value_text(argv[1]);
825 if( zPath==0 ) return;
826 if( zPath[0]!='$' ) return;
827 zPath++;
828 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
829 pNode = jsonLookup(x.aNode, zPath);
830 if( pNode ){
831 jsonReturn(pNode, context);
832 }
833 sqlite3_free(x.aNode);
834}
835
836/*
837** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
838** object that contains all name/value given in arguments. Or if any name
839** is not a string or if any value is a BLOB, throw an error.
840*/
841static void jsonObjectFunc(
842 sqlite3_context *context,
843 int argc,
844 sqlite3_value **argv
845){
846 int i;
847 Json jx;
848 char cSep = '{';
849 const char *z;
850 u32 n;
851
852 if( argc&1 ){
853 sqlite3_result_error(context, "json_object() requires an even number "
854 "of arguments", -1);
855 return;
856 }
857 jsonInit(&jx, context);
858 for(i=0; i<argc; i+=2){
859 jsonAppendRaw(&jx, &cSep, 1);
860 cSep = ',';
861 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
862 sqlite3_result_error(context, "json_object() labels must be TEXT", -1);
863 jsonZero(&jx);
864 return;
865 }
866 z = (const char*)sqlite3_value_text(argv[i]);
867 n = (u32)sqlite3_value_bytes(argv[i]);
868 jsonAppendString(&jx, z, n);
869 jsonAppendRaw(&jx, ":", 1);
870 switch( sqlite3_value_type(argv[i+1]) ){
871 case SQLITE_NULL: {
872 jsonAppendRaw(&jx, "null", 4);
873 break;
874 }
875 case SQLITE_INTEGER:
876 case SQLITE_FLOAT: {
877 z = (const char*)sqlite3_value_text(argv[i+1]);
878 n = (u32)sqlite3_value_bytes(argv[i+1]);
879 jsonAppendRaw(&jx, z, n);
880 break;
881 }
882 case SQLITE_TEXT: {
883 z = (const char*)sqlite3_value_text(argv[i+1]);
884 n = (u32)sqlite3_value_bytes(argv[i+1]);
885 jsonAppendString(&jx, z, n);
886 break;
887 }
888 default: {
889 jsonZero(&jx);
890 sqlite3_result_error(context, "JSON cannot hold BLOB values", -1);
891 return;
892 }
893 }
894 }
895 jsonAppendRaw(&jx, "}", 1);
896 jsonResult(&jx);
897}
898
899
900/*
drh301eecc2015-08-17 20:14:19 +0000901** json_remove(JSON, PATH, ...)
902**
903** Remove the named elements from JSON and return the result. Ill-formed
904** PATH arguments are silently ignored. If JSON is ill-formed, then NULL
905** is returned.
906*/
907static void jsonRemoveFunc(
908 sqlite3_context *context,
909 int argc,
910 sqlite3_value **argv
911){
912 JsonParse x; /* The parse */
913 JsonNode *pNode;
914 const char *zPath;
915 u32 i;
916
917 if( argc<1 ) return;
918 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
919 if( x.nNode ){
920 for(i=1; i<argc; i++){
921 zPath = (const char*)sqlite3_value_text(argv[i]);
922 if( zPath==0 ) continue;
923 if( zPath[0]!='$' ) continue;
924 pNode = jsonLookup(x.aNode, &zPath[1]);
925 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
926 }
927 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
928 jsonReturn(x.aNode, context);
929 }
930 }
931 sqlite3_free(x.aNode);
932}
933
934/*
drh987eb1f2015-08-17 15:17:37 +0000935** json_type(JSON)
936** json_type(JSON, PATH)
937**
938** Return the top-level "type" of a JSON string. Return NULL if the
939** input is not a well-formed JSON string.
940*/
941static void jsonTypeFunc(
942 sqlite3_context *context,
943 int argc,
944 sqlite3_value **argv
945){
946 JsonParse x; /* The parse */
947 const char *zPath;
948
949 if( argc==2 ){
950 zPath = (const char*)sqlite3_value_text(argv[1]);
951 if( zPath==0 ) return;
952 if( zPath[0]!='$' ) return;
953 zPath++;
954 }else{
955 zPath = 0;
956 }
957 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
958 if( x.nNode ){
959 JsonNode *pNode = x.aNode;
960 if( zPath ) pNode = jsonLookup(pNode, zPath);
961 sqlite3_result_text(context, jsonType[pNode->eType], -1, SQLITE_STATIC);
962 }
963 sqlite3_free(x.aNode);
964}
drh5634cc02015-08-17 11:28:03 +0000965
drh5fa5c102015-08-12 16:49:40 +0000966#ifdef _WIN32
967__declspec(dllexport)
968#endif
969int sqlite3_json_init(
970 sqlite3 *db,
971 char **pzErrMsg,
972 const sqlite3_api_routines *pApi
973){
974 int rc = SQLITE_OK;
975 int i;
976 static const struct {
977 const char *zName;
978 int nArg;
979 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
980 } aFunc[] = {
drh987eb1f2015-08-17 15:17:37 +0000981 { "json_array", -1, jsonArrayFunc },
982 { "json_array_length", 1, jsonArrayLengthFunc },
983 { "json_array_length", 2, jsonArrayLengthFunc },
984 { "json_extract", 2, jsonExtractFunc },
985 { "json_object", -1, jsonObjectFunc },
drh301eecc2015-08-17 20:14:19 +0000986 { "json_remove", -1, jsonRemoveFunc },
drh987eb1f2015-08-17 15:17:37 +0000987 { "json_type", 1, jsonTypeFunc },
988 { "json_type", 2, jsonTypeFunc },
989
drh301eecc2015-08-17 20:14:19 +0000990#if SQLITE_DEBUG
drh987eb1f2015-08-17 15:17:37 +0000991 /* DEBUG and TESTING functions */
992 { "json_parse", 1, jsonParseFunc },
993 { "json_test1", 1, jsonTest1Func },
994 { "json_nodecount", 1, jsonNodeCountFunc },
drh301eecc2015-08-17 20:14:19 +0000995#endif
drh5fa5c102015-08-12 16:49:40 +0000996 };
997 SQLITE_EXTENSION_INIT2(pApi);
998 (void)pzErrMsg; /* Unused parameter */
999 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
1000 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
1001 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
1002 aFunc[i].xFunc, 0, 0);
1003 }
1004 return rc;
1005}