blob: d865fb6152d57463f22207ae79fad77affd4ed6b [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 */
drhd0960592015-08-17 21:22:32 +000045 u8 bErr; /* True if an error 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 */
drhd0960592015-08-17 21:22:32 +000072#define JNODE_REPLACE 0x08 /* Replace with JsonNode.iVal */
drh301eecc2015-08-17 20:14:19 +000073
drh987eb1f2015-08-17 15:17:37 +000074
drhe9c37f32015-08-15 21:25:36 +000075/* A single node of parsed JSON
76*/
77typedef struct JsonNode JsonNode;
78struct JsonNode {
drh5634cc02015-08-17 11:28:03 +000079 u8 eType; /* One of the JSON_ type values */
drh301eecc2015-08-17 20:14:19 +000080 u8 jnFlags; /* JNODE flags */
drhd0960592015-08-17 21:22:32 +000081 u8 iVal; /* Replacement value when JNODE_REPLACE */
drhe9c37f32015-08-15 21:25:36 +000082 u32 n; /* Bytes of content, or number of sub-nodes */
drh5634cc02015-08-17 11:28:03 +000083 const char *zJContent; /* JSON content */
drhe9c37f32015-08-15 21:25:36 +000084};
85
86/* A completely parsed JSON string
87*/
88typedef struct JsonParse JsonParse;
89struct JsonParse {
90 u32 nNode; /* Number of slots of aNode[] used */
91 u32 nAlloc; /* Number of slots of aNode[] allocated */
92 JsonNode *aNode; /* Array of nodes containing the parse */
93 const char *zJson; /* Original JSON string */
94 u8 oom; /* Set to true if out of memory */
95};
96
drh301eecc2015-08-17 20:14:19 +000097/*
98** Return the number of consecutive JsonNode slots need to represent
99** the parsed JSON at pNode. The minimum answer is 1. For ARRAY and
100** OBJECT types, the number might be larger.
101*/
drhd0960592015-08-17 21:22:32 +0000102static u32 jsonSize(JsonNode *pNode){
drh301eecc2015-08-17 20:14:19 +0000103 return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
104}
105
drh5fa5c102015-08-12 16:49:40 +0000106/* Set the Json object to an empty string
107*/
108static void jsonZero(Json *p){
109 p->zBuf = p->zSpace;
110 p->nAlloc = sizeof(p->zSpace);
111 p->nUsed = 0;
112 p->bStatic = 1;
113}
114
115/* Initialize the Json object
116*/
117static void jsonInit(Json *p, sqlite3_context *pCtx){
118 p->pCtx = pCtx;
drhd0960592015-08-17 21:22:32 +0000119 p->bErr = 0;
drh5fa5c102015-08-12 16:49:40 +0000120 jsonZero(p);
121}
122
123
124/* Free all allocated memory and reset the Json object back to its
125** initial state.
126*/
127static void jsonReset(Json *p){
128 if( !p->bStatic ) sqlite3_free(p->zBuf);
129 jsonZero(p);
130}
131
132
133/* Report an out-of-memory (OOM) condition
134*/
135static void jsonOom(Json *p){
drhd0960592015-08-17 21:22:32 +0000136 if( !p->bErr ){
137 p->bErr = 1;
138 sqlite3_result_error_nomem(p->pCtx);
139 jsonReset(p);
140 }
drh5fa5c102015-08-12 16:49:40 +0000141}
142
143/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
144** Return zero on success. Return non-zero on an OOM error
145*/
146static int jsonGrow(Json *p, u32 N){
drh301eecc2015-08-17 20:14:19 +0000147 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
drh5fa5c102015-08-12 16:49:40 +0000148 char *zNew;
149 if( p->bStatic ){
drhd0960592015-08-17 21:22:32 +0000150 if( p->bErr ) return 1;
drh5fa5c102015-08-12 16:49:40 +0000151 zNew = sqlite3_malloc64(nTotal);
152 if( zNew==0 ){
153 jsonOom(p);
154 return SQLITE_NOMEM;
155 }
156 memcpy(zNew, p->zBuf, p->nUsed);
157 p->zBuf = zNew;
158 p->bStatic = 0;
159 }else{
160 zNew = sqlite3_realloc64(p->zBuf, nTotal);
161 if( zNew==0 ){
162 jsonOom(p);
163 return SQLITE_NOMEM;
164 }
165 p->zBuf = zNew;
166 }
167 p->nAlloc = nTotal;
168 return SQLITE_OK;
169}
170
171/* Append N bytes from zIn onto the end of the Json string.
172*/
173static void jsonAppendRaw(Json *p, const char *zIn, u32 N){
174 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
175 memcpy(p->zBuf+p->nUsed, zIn, N);
176 p->nUsed += N;
177}
178
drhd0960592015-08-17 21:22:32 +0000179#ifdef SQLITE_DEBUG
drhe9c37f32015-08-15 21:25:36 +0000180/* Append the zero-terminated string zIn
181*/
182static void jsonAppend(Json *p, const char *zIn){
183 jsonAppendRaw(p, zIn, (u32)strlen(zIn));
184}
drhd0960592015-08-17 21:22:32 +0000185#endif
drhe9c37f32015-08-15 21:25:36 +0000186
drh5634cc02015-08-17 11:28:03 +0000187/* Append a single character
188*/
189static void jsonAppendChar(Json *p, char c){
190 if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
191 p->zBuf[p->nUsed++] = c;
192}
193
drh301eecc2015-08-17 20:14:19 +0000194/* Append a comma separator to the output buffer, if the previous
195** character is not '[' or '{'.
196*/
197static void jsonAppendSeparator(Json *p){
198 char c;
199 if( p->nUsed==0 ) return;
200 c = p->zBuf[p->nUsed-1];
201 if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
202}
203
drh5fa5c102015-08-12 16:49:40 +0000204/* Append the N-byte string in zIn to the end of the Json string
205** under construction. Enclose the string in "..." and escape
206** any double-quotes or backslash characters contained within the
207** string.
208*/
209static void jsonAppendString(Json *p, const char *zIn, u32 N){
210 u32 i;
211 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
212 p->zBuf[p->nUsed++] = '"';
213 for(i=0; i<N; i++){
214 char c = zIn[i];
215 if( c=='"' || c=='\\' ){
216 if( (p->nUsed+N+1-i > p->nAlloc) && jsonGrow(p,N+1-i)!=0 ) return;
217 p->zBuf[p->nUsed++] = '\\';
218 }
219 p->zBuf[p->nUsed++] = c;
220 }
221 p->zBuf[p->nUsed++] = '"';
222}
223
drhd0960592015-08-17 21:22:32 +0000224/*
225** Append a function parameter value to the JSON string under
226** construction.
227*/
228static void jsonAppendValue(
229 Json *p, /* Append to this JSON string */
230 sqlite3_value *pValue /* Value to append */
231){
232 switch( sqlite3_value_type(pValue) ){
233 case SQLITE_NULL: {
234 jsonAppendRaw(p, "null", 4);
235 break;
236 }
237 case SQLITE_INTEGER:
238 case SQLITE_FLOAT: {
239 const char *z = (const char*)sqlite3_value_text(pValue);
240 u32 n = (u32)sqlite3_value_bytes(pValue);
241 jsonAppendRaw(p, z, n);
242 break;
243 }
244 case SQLITE_TEXT: {
245 const char *z = (const char*)sqlite3_value_text(pValue);
246 u32 n = (u32)sqlite3_value_bytes(pValue);
247 jsonAppendString(p, z, n);
248 break;
249 }
250 default: {
251 if( p->bErr==0 ){
252 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
253 p->bErr = 1;
254 jsonReset(p);
255 }
256 break;
257 }
258 }
259}
260
261
drhbd0621b2015-08-13 13:54:59 +0000262/* Make the JSON in p the result of the SQL function.
drh5fa5c102015-08-12 16:49:40 +0000263*/
264static void jsonResult(Json *p){
drhd0960592015-08-17 21:22:32 +0000265 if( p->bErr==0 ){
drh5fa5c102015-08-12 16:49:40 +0000266 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
267 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
268 SQLITE_UTF8);
269 jsonZero(p);
270 }
271 assert( p->bStatic );
272}
273
drh5634cc02015-08-17 11:28:03 +0000274/*
275** Convert the JsonNode pNode into a pure JSON string and
276** append to pOut. Subsubstructure is also included. Return
277** the number of JsonNode objects that are encoded.
drhbd0621b2015-08-13 13:54:59 +0000278*/
drhd0960592015-08-17 21:22:32 +0000279static int jsonRenderNode(
280 JsonNode *pNode, /* The node to render */
281 Json *pOut, /* Write JSON here */
282 sqlite3_value **aReplace /* Replacement values */
283){
drh301eecc2015-08-17 20:14:19 +0000284 u32 j = 1;
drh5634cc02015-08-17 11:28:03 +0000285 switch( pNode->eType ){
286 case JSON_NULL: {
287 jsonAppendRaw(pOut, "null", 4);
288 break;
289 }
290 case JSON_TRUE: {
291 jsonAppendRaw(pOut, "true", 4);
292 break;
293 }
294 case JSON_FALSE: {
295 jsonAppendRaw(pOut, "false", 5);
296 break;
297 }
298 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000299 if( pNode->jnFlags & JNODE_RAW ){
drh5634cc02015-08-17 11:28:03 +0000300 jsonAppendString(pOut, pNode->zJContent, pNode->n);
301 break;
302 }
303 /* Fall through into the next case */
304 }
305 case JSON_REAL:
306 case JSON_INT: {
307 jsonAppendRaw(pOut, pNode->zJContent, pNode->n);
308 break;
309 }
310 case JSON_ARRAY: {
311 jsonAppendChar(pOut, '[');
drh301eecc2015-08-17 20:14:19 +0000312 while( j<=pNode->n ){
drhd0960592015-08-17 21:22:32 +0000313 if( pNode[j].jnFlags & (JNODE_REMOVE|JNODE_REPLACE) ){
314 if( pNode[j].jnFlags & JNODE_REPLACE ){
315 jsonAppendSeparator(pOut);
316 jsonAppendValue(pOut, aReplace[pNode[j].iVal]);
317 }
318 j += jsonSize(&pNode[j]);
drh301eecc2015-08-17 20:14:19 +0000319 }else{
320 jsonAppendSeparator(pOut);
drhd0960592015-08-17 21:22:32 +0000321 j += jsonRenderNode(&pNode[j], pOut, aReplace);
drh301eecc2015-08-17 20:14:19 +0000322 }
drh5634cc02015-08-17 11:28:03 +0000323 }
324 jsonAppendChar(pOut, ']');
325 break;
326 }
327 case JSON_OBJECT: {
328 jsonAppendChar(pOut, '{');
drh301eecc2015-08-17 20:14:19 +0000329 while( j<=pNode->n ){
330 if( pNode[j+1].jnFlags & JNODE_REMOVE ){
drhd0960592015-08-17 21:22:32 +0000331 j += 1 + jsonSize(&pNode[j+1]);
drh301eecc2015-08-17 20:14:19 +0000332 }else{
333 jsonAppendSeparator(pOut);
drhd0960592015-08-17 21:22:32 +0000334 jsonRenderNode(&pNode[j], pOut, aReplace);
drh301eecc2015-08-17 20:14:19 +0000335 jsonAppendChar(pOut, ':');
drhd0960592015-08-17 21:22:32 +0000336 if( pNode[j+1].jnFlags & JNODE_REPLACE ){
337 jsonAppendValue(pOut, aReplace[pNode[j+1].iVal]);
338 j += 1 + jsonSize(&pNode[j+1]);
339 }else{
340 j += 1 + jsonRenderNode(&pNode[j+1], pOut, aReplace);
341 }
drh301eecc2015-08-17 20:14:19 +0000342 }
drh5634cc02015-08-17 11:28:03 +0000343 }
344 jsonAppendChar(pOut, '}');
345 break;
346 }
drhbd0621b2015-08-13 13:54:59 +0000347 }
drh301eecc2015-08-17 20:14:19 +0000348 return j;
drh5634cc02015-08-17 11:28:03 +0000349}
350
351/*
352** Make the JsonNode the return value of the function.
353*/
drhd0960592015-08-17 21:22:32 +0000354static void jsonReturn(
355 JsonNode *pNode, /* Node to return */
356 sqlite3_context *pCtx, /* Return value for this function */
357 sqlite3_value **aReplace /* Array of replacement values */
358){
drh5634cc02015-08-17 11:28:03 +0000359 switch( pNode->eType ){
360 case JSON_NULL: {
361 sqlite3_result_null(pCtx);
362 break;
363 }
364 case JSON_TRUE: {
365 sqlite3_result_int(pCtx, 1);
366 break;
367 }
368 case JSON_FALSE: {
369 sqlite3_result_int(pCtx, 0);
370 break;
371 }
drh987eb1f2015-08-17 15:17:37 +0000372 case JSON_REAL: {
373 double r = strtod(pNode->zJContent, 0);
374 sqlite3_result_double(pCtx, r);
drh5634cc02015-08-17 11:28:03 +0000375 break;
376 }
drh987eb1f2015-08-17 15:17:37 +0000377 case JSON_INT: {
378 sqlite3_int64 i = 0;
379 const char *z = pNode->zJContent;
380 if( z[0]=='-' ){ z++; }
381 while( z[0]>='0' && z[0]<='9' ){ i = i*10 + *(z++) - '0'; }
382 if( pNode->zJContent[0]=='-' ){ i = -i; }
383 sqlite3_result_int64(pCtx, i);
384 break;
385 }
drh5634cc02015-08-17 11:28:03 +0000386 case JSON_STRING: {
drh301eecc2015-08-17 20:14:19 +0000387 if( pNode->jnFlags & JNODE_RAW ){
drh5634cc02015-08-17 11:28:03 +0000388 sqlite3_result_text(pCtx, pNode->zJContent, pNode->n, SQLITE_TRANSIENT);
drh301eecc2015-08-17 20:14:19 +0000389 }else if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
drh987eb1f2015-08-17 15:17:37 +0000390 /* JSON formatted without any backslash-escapes */
391 sqlite3_result_text(pCtx, pNode->zJContent+1, pNode->n-2,
392 SQLITE_TRANSIENT);
drh5634cc02015-08-17 11:28:03 +0000393 }else{
394 /* Translate JSON formatted string into raw text */
drh987eb1f2015-08-17 15:17:37 +0000395 u32 i;
396 u32 n = pNode->n;
397 const char *z = pNode->zJContent;
398 char *zOut;
399 u32 j;
400 zOut = sqlite3_malloc( n+1 );
401 if( zOut==0 ){
402 sqlite3_result_error_nomem(pCtx);
403 break;
404 }
405 for(i=1, j=0; i<n-1; i++){
406 char c = z[i];
407 if( c!='\\' && z[i+1] ){
408 zOut[j++] = c;
409 }else{
410 c = z[++i];
411 if( c=='u' && z[1] ){
412 u32 v = 0, k;
413 z++;
414 for(k=0; k<4 && z[k]; k++){
415 c = z[0];
416 if( c>='0' && c<='9' ) v = v*16 + c - '0';
417 else if( c>='A' && c<='F' ) v = v*16 + c - 'A' + 10;
418 else if( c>='a' && c<='f' ) v = v*16 + c - 'a' + 10;
419 else break;
420 z++;
421 }
422 if( v<=0x7f ){
423 zOut[j++] = v;
424 }else if( v<=0x7ff ){
425 zOut[j++] = 0xc0 | (v>>6);
426 zOut[j++] = 0x80 | (v&0x3f);
427 }else if( v<=0xffff ){
428 zOut[j++] = 0xe0 | (v>>12);
429 zOut[j++] = 0x80 | ((v>>6)&0x3f);
430 zOut[j++] = 0x80 | (v&0x3f);
431 }else if( v<=0x10ffff ){
432 zOut[j++] = 0xf0 | (v>>18);
433 zOut[j++] = 0x80 | ((v>>12)&0x3f);
434 zOut[j++] = 0x80 | ((v>>6)&0x3f);
435 zOut[j++] = 0x80 | (v&0x3f);
436 }
437 }else{
438 if( c=='b' ){
439 c = '\b';
440 }else if( c=='f' ){
441 c = '\f';
442 }else if( c=='n' ){
443 c = '\n';
444 }else if( c=='r' ){
445 c = '\r';
446 }else if( c=='t' ){
447 c = '\t';
448 }
449 zOut[j++] = c;
450 }
451 }
452 }
453 zOut[j] = 0;
454 sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
drh5634cc02015-08-17 11:28:03 +0000455 }
456 break;
457 }
458 case JSON_ARRAY:
459 case JSON_OBJECT: {
460 Json s;
461 jsonInit(&s, pCtx);
drhd0960592015-08-17 21:22:32 +0000462 jsonRenderNode(pNode, &s, aReplace);
drh5634cc02015-08-17 11:28:03 +0000463 jsonResult(&s);
464 break;
465 }
466 }
drhbd0621b2015-08-13 13:54:59 +0000467}
468
drh5fa5c102015-08-12 16:49:40 +0000469/*
drhe9c37f32015-08-15 21:25:36 +0000470** Create a new JsonNode instance based on the arguments and append that
471** instance to the JsonParse. Return the index in pParse->aNode[] of the
472** new node, or -1 if a memory allocation fails.
473*/
474static int jsonParseAddNode(
475 JsonParse *pParse, /* Append the node to this object */
476 u32 eType, /* Node type */
477 u32 n, /* Content size or sub-node count */
478 const char *zContent /* Content */
479){
480 JsonNode *p;
481 if( pParse->nNode>=pParse->nAlloc ){
482 u32 nNew;
483 JsonNode *pNew;
484 if( pParse->oom ) return -1;
485 nNew = pParse->nAlloc*2 + 10;
486 if( nNew<=pParse->nNode ){
487 pParse->oom = 1;
488 return -1;
489 }
490 pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew);
491 if( pNew==0 ){
492 pParse->oom = 1;
493 return -1;
494 }
495 pParse->nAlloc = nNew;
496 pParse->aNode = pNew;
497 }
498 p = &pParse->aNode[pParse->nNode];
drh5634cc02015-08-17 11:28:03 +0000499 p->eType = (u8)eType;
drh301eecc2015-08-17 20:14:19 +0000500 p->jnFlags = 0;
drhd0960592015-08-17 21:22:32 +0000501 p->iVal = 0;
drhe9c37f32015-08-15 21:25:36 +0000502 p->n = n;
drh5634cc02015-08-17 11:28:03 +0000503 p->zJContent = zContent;
drhe9c37f32015-08-15 21:25:36 +0000504 return pParse->nNode++;
505}
506
507/*
508** Parse a single JSON value which begins at pParse->zJson[i]. Return the
509** index of the first character past the end of the value parsed.
510**
511** Return negative for a syntax error. Special cases: return -2 if the
512** first non-whitespace character is '}' and return -3 if the first
513** non-whitespace character is ']'.
514*/
515static int jsonParseValue(JsonParse *pParse, u32 i){
516 char c;
517 u32 j;
518 u32 iThis;
519 int x;
520 while( isspace(pParse->zJson[i]) ){ i++; }
521 if( (c = pParse->zJson[i])==0 ) return 0;
522 if( c=='{' ){
523 /* Parse object */
524 iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
525 if( iThis<0 ) return -1;
526 for(j=i+1;;j++){
527 while( isspace(pParse->zJson[j]) ){ j++; }
528 x = jsonParseValue(pParse, j);
529 if( x<0 ){
530 if( x==(-2) && pParse->nNode==iThis+1 ) return j+1;
531 return -1;
532 }
533 if( pParse->aNode[pParse->nNode-1].eType!=JSON_STRING ) return -1;
534 j = x;
535 while( isspace(pParse->zJson[j]) ){ j++; }
536 if( pParse->zJson[j]!=':' ) return -1;
537 j++;
538 x = jsonParseValue(pParse, j);
539 if( x<0 ) return -1;
540 j = x;
541 while( isspace(pParse->zJson[j]) ){ j++; }
542 c = pParse->zJson[j];
543 if( c==',' ) continue;
544 if( c!='}' ) return -1;
545 break;
546 }
547 pParse->aNode[iThis].n = pParse->nNode - iThis - 1;
548 return j+1;
549 }else if( c=='[' ){
550 /* Parse array */
551 iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
552 if( iThis<0 ) return -1;
553 for(j=i+1;;j++){
554 while( isspace(pParse->zJson[j]) ){ j++; }
555 x = jsonParseValue(pParse, j);
556 if( x<0 ){
557 if( x==(-3) && pParse->nNode==iThis+1 ) return j+1;
558 return -1;
559 }
560 j = x;
561 while( isspace(pParse->zJson[j]) ){ j++; }
562 c = pParse->zJson[j];
563 if( c==',' ) continue;
564 if( c!=']' ) return -1;
565 break;
566 }
567 pParse->aNode[iThis].n = pParse->nNode - iThis - 1;
568 return j+1;
569 }else if( c=='"' ){
570 /* Parse string */
drh301eecc2015-08-17 20:14:19 +0000571 u8 jnFlags = 0;
drhe9c37f32015-08-15 21:25:36 +0000572 j = i+1;
573 for(;;){
574 c = pParse->zJson[j];
575 if( c==0 ) return -1;
576 if( c=='\\' ){
577 c = pParse->zJson[++j];
578 if( c==0 ) return -1;
drh301eecc2015-08-17 20:14:19 +0000579 jnFlags = JNODE_ESCAPE;
drhe9c37f32015-08-15 21:25:36 +0000580 }else if( c=='"' ){
581 break;
582 }
583 j++;
584 }
585 jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]);
drh301eecc2015-08-17 20:14:19 +0000586 pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
drhe9c37f32015-08-15 21:25:36 +0000587 return j+1;
588 }else if( c=='n'
589 && strncmp(pParse->zJson+i,"null",4)==0
drhb2cd10e2015-08-15 21:29:14 +0000590 && !isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000591 jsonParseAddNode(pParse, JSON_NULL, 0, 0);
592 return i+4;
593 }else if( c=='t'
594 && strncmp(pParse->zJson+i,"true",4)==0
drhb2cd10e2015-08-15 21:29:14 +0000595 && !isalnum(pParse->zJson[i+4]) ){
drhe9c37f32015-08-15 21:25:36 +0000596 jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
597 return i+4;
598 }else if( c=='f'
599 && strncmp(pParse->zJson+i,"false",5)==0
drhb2cd10e2015-08-15 21:29:14 +0000600 && !isalnum(pParse->zJson[i+5]) ){
drhe9c37f32015-08-15 21:25:36 +0000601 jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
602 return i+5;
603 }else if( c=='-' || (c>='0' && c<='9') ){
604 /* Parse number */
605 u8 seenDP = 0;
606 u8 seenE = 0;
607 j = i+1;
608 for(;; j++){
609 c = pParse->zJson[j];
610 if( c>='0' && c<='9' ) continue;
611 if( c=='.' ){
612 if( pParse->zJson[j-1]=='-' ) return -1;
613 if( seenDP ) return -1;
614 seenDP = 1;
615 continue;
616 }
617 if( c=='e' || c=='E' ){
618 if( pParse->zJson[j-1]<'0' ) return -1;
619 if( seenE ) return -1;
620 seenDP = seenE = 1;
621 c = pParse->zJson[j+1];
622 if( c=='+' || c=='-' ) j++;
623 continue;
624 }
625 break;
626 }
627 if( pParse->zJson[j-1]<'0' ) return -1;
628 jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
629 j - i, &pParse->zJson[i]);
630 return j;
631 }else if( c=='}' ){
632 return -2; /* End of {...} */
633 }else if( c==']' ){
634 return -3; /* End of [...] */
635 }else{
636 return -1; /* Syntax error */
637 }
638}
639
640/*
641** Parse a complete JSON string. Return 0 on success or non-zero if there
642** are any errors. If an error occurs, free all memory associated with
643** pParse.
644**
645** pParse is uninitialized when this routine is called.
646*/
647static int jsonParse(JsonParse *pParse, const char *zJson){
648 int i;
649 if( zJson==0 ) return 1;
650 memset(pParse, 0, sizeof(*pParse));
651 pParse->zJson = zJson;
652 i = jsonParseValue(pParse, 0);
653 if( i>0 ){
654 while( isspace(zJson[i]) ) i++;
655 if( zJson[i] ) i = -1;
656 }
657 if( i<0 ){
658 sqlite3_free(pParse->aNode);
659 pParse->aNode = 0;
660 pParse->nNode = 0;
661 pParse->nAlloc = 0;
662 return 1;
663 }
664 return 0;
665}
drh301eecc2015-08-17 20:14:19 +0000666
drh987eb1f2015-08-17 15:17:37 +0000667/*
668** Search along zPath to find the node specified. Return a pointer
669** to that node, or NULL if zPath is malformed or if there is no such
670** node.
671*/
672static JsonNode *jsonLookup(JsonNode *pRoot, const char *zPath){
673 u32 i, j;
674 if( zPath[0]==0 ) return pRoot;
675 if( zPath[0]=='.' ){
676 if( pRoot->eType!=JSON_OBJECT ) return 0;
677 zPath++;
678 for(i=0; isalnum(zPath[i]); i++){}
679 if( i==0 ) return 0;
680 j = 1;
681 while( j<=pRoot->n ){
682 if( pRoot[j].n==i+2
683 && strncmp(&pRoot[j].zJContent[1],zPath,i)==0
684 ){
685 return jsonLookup(&pRoot[j+1], &zPath[i]);
686 }
687 j++;
drhd0960592015-08-17 21:22:32 +0000688 j += jsonSize(&pRoot[j]);
drh987eb1f2015-08-17 15:17:37 +0000689 }
690 }else if( zPath[0]=='[' && isdigit(zPath[1]) ){
691 if( pRoot->eType!=JSON_ARRAY ) return 0;
692 i = 0;
693 zPath++;
694 while( isdigit(zPath[0]) ){
695 i = i + zPath[0] - '0';
696 zPath++;
697 }
698 if( zPath[0]!=']' ) return 0;
699 zPath++;
700 j = 1;
701 while( i>0 && j<=pRoot->n ){
drhd0960592015-08-17 21:22:32 +0000702 j += jsonSize(&pRoot[j]);
drh987eb1f2015-08-17 15:17:37 +0000703 i--;
704 }
705 if( j<=pRoot->n ){
706 return jsonLookup(&pRoot[j], zPath);
707 }
708 }
709 return 0;
710}
711
712/****************************************************************************
713** SQL functions used for testing and debugging
714****************************************************************************/
drhe9c37f32015-08-15 21:25:36 +0000715
drh301eecc2015-08-17 20:14:19 +0000716#ifdef SQLITE_DEBUG
drhe9c37f32015-08-15 21:25:36 +0000717/*
drh5634cc02015-08-17 11:28:03 +0000718** The json_parse(JSON) function returns a string which describes
drhe9c37f32015-08-15 21:25:36 +0000719** a parse of the JSON provided. Or it returns NULL if JSON is not
720** well-formed.
721*/
drh5634cc02015-08-17 11:28:03 +0000722static void jsonParseFunc(
drhe9c37f32015-08-15 21:25:36 +0000723 sqlite3_context *context,
724 int argc,
725 sqlite3_value **argv
726){
727 Json s; /* Output string - not real JSON */
728 JsonParse x; /* The parse */
729 u32 i;
drh301eecc2015-08-17 20:14:19 +0000730 char zBuf[100];
drhe9c37f32015-08-15 21:25:36 +0000731
732 assert( argc==1 );
733 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
734 jsonInit(&s, context);
735 for(i=0; i<x.nNode; i++){
drh301eecc2015-08-17 20:14:19 +0000736 sqlite3_snprintf(sizeof(zBuf), zBuf, "node %3u: %7s n=%d\n",
737 i, jsonType[x.aNode[i].eType], x.aNode[i].n);
drhe9c37f32015-08-15 21:25:36 +0000738 jsonAppend(&s, zBuf);
drh5634cc02015-08-17 11:28:03 +0000739 if( x.aNode[i].zJContent!=0 ){
drh301eecc2015-08-17 20:14:19 +0000740 jsonAppendRaw(&s, " text: ", 10);
drh5634cc02015-08-17 11:28:03 +0000741 jsonAppendRaw(&s, x.aNode[i].zJContent, x.aNode[i].n);
drhe9c37f32015-08-15 21:25:36 +0000742 jsonAppendRaw(&s, "\n", 1);
743 }
744 }
745 sqlite3_free(x.aNode);
746 jsonResult(&s);
747}
748
drh5634cc02015-08-17 11:28:03 +0000749/*
750** The json_test1(JSON) function parses and rebuilds the JSON string.
751*/
752static void jsonTest1Func(
753 sqlite3_context *context,
754 int argc,
755 sqlite3_value **argv
756){
757 JsonParse x; /* The parse */
758 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
drhd0960592015-08-17 21:22:32 +0000759 jsonReturn(x.aNode, context, 0);
drh5634cc02015-08-17 11:28:03 +0000760 sqlite3_free(x.aNode);
761}
762
763/*
764** The json_nodecount(JSON) function returns the number of nodes in the
765** input JSON string.
766*/
767static void jsonNodeCountFunc(
768 sqlite3_context *context,
769 int argc,
770 sqlite3_value **argv
771){
772 JsonParse x; /* The parse */
773 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
774 sqlite3_result_int64(context, x.nNode);
775 sqlite3_free(x.aNode);
776}
drh301eecc2015-08-17 20:14:19 +0000777#endif /* SQLITE_DEBUG */
drh5634cc02015-08-17 11:28:03 +0000778
drh987eb1f2015-08-17 15:17:37 +0000779/****************************************************************************
780** SQL function implementations
781****************************************************************************/
782
783/*
784** Implementation of the json_array(VALUE,...) function. Return a JSON
785** array that contains all values given in arguments. Or if any argument
786** is a BLOB, throw an error.
787*/
788static void jsonArrayFunc(
789 sqlite3_context *context,
790 int argc,
791 sqlite3_value **argv
792){
793 int i;
794 Json jx;
drh987eb1f2015-08-17 15:17:37 +0000795
796 jsonInit(&jx, context);
drhd0960592015-08-17 21:22:32 +0000797 jsonAppendChar(&jx, '[');
drh987eb1f2015-08-17 15:17:37 +0000798 for(i=0; i<argc; i++){
drhd0960592015-08-17 21:22:32 +0000799 jsonAppendSeparator(&jx);
800 jsonAppendValue(&jx, argv[i]);
drh987eb1f2015-08-17 15:17:37 +0000801 }
drhd0960592015-08-17 21:22:32 +0000802 jsonAppendChar(&jx, ']');
drh987eb1f2015-08-17 15:17:37 +0000803 jsonResult(&jx);
804}
805
806
807/*
808** json_array_length(JSON)
809** json_array_length(JSON, PATH)
810**
811** Return the number of elements in the top-level JSON array.
812** Return 0 if the input is not a well-formed JSON array.
813*/
814static void jsonArrayLengthFunc(
815 sqlite3_context *context,
816 int argc,
817 sqlite3_value **argv
818){
819 JsonParse x; /* The parse */
820 sqlite3_int64 n = 0;
821 u32 i;
822 const char *zPath;
823
824 if( argc==2 ){
825 zPath = (const char*)sqlite3_value_text(argv[1]);
826 if( zPath==0 ) return;
827 if( zPath[0]!='$' ) return;
828 zPath++;
829 }else{
830 zPath = 0;
831 }
832 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0]))==0 ){
833 if( x.nNode ){
834 JsonNode *pNode = x.aNode;
835 if( zPath ) pNode = jsonLookup(pNode, zPath);
836 if( pNode->eType==JSON_ARRAY ){
drh301eecc2015-08-17 20:14:19 +0000837 for(i=1; i<=pNode->n; n++){
drhd0960592015-08-17 21:22:32 +0000838 i += jsonSize(&pNode[i]);
drh987eb1f2015-08-17 15:17:37 +0000839 }
840 }
841 }
842 sqlite3_free(x.aNode);
843 }
844 sqlite3_result_int64(context, n);
845}
846
847/*
848** json_extract(JSON, PATH)
849**
850** Return the element described by PATH. Return NULL if JSON is not
851** valid JSON or if there is no PATH element or if PATH is malformed.
852*/
853static void jsonExtractFunc(
854 sqlite3_context *context,
855 int argc,
856 sqlite3_value **argv
857){
858 JsonParse x; /* The parse */
859 JsonNode *pNode;
860 const char *zPath;
861 assert( argc==2 );
862 zPath = (const char*)sqlite3_value_text(argv[1]);
863 if( zPath==0 ) return;
864 if( zPath[0]!='$' ) return;
865 zPath++;
866 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
867 pNode = jsonLookup(x.aNode, zPath);
868 if( pNode ){
drhd0960592015-08-17 21:22:32 +0000869 jsonReturn(pNode, context, 0);
drh987eb1f2015-08-17 15:17:37 +0000870 }
871 sqlite3_free(x.aNode);
872}
873
874/*
875** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
876** object that contains all name/value given in arguments. Or if any name
877** is not a string or if any value is a BLOB, throw an error.
878*/
879static void jsonObjectFunc(
880 sqlite3_context *context,
881 int argc,
882 sqlite3_value **argv
883){
884 int i;
885 Json jx;
drh987eb1f2015-08-17 15:17:37 +0000886 const char *z;
887 u32 n;
888
889 if( argc&1 ){
890 sqlite3_result_error(context, "json_object() requires an even number "
891 "of arguments", -1);
892 return;
893 }
894 jsonInit(&jx, context);
drhd0960592015-08-17 21:22:32 +0000895 jsonAppendChar(&jx, '{');
drh987eb1f2015-08-17 15:17:37 +0000896 for(i=0; i<argc; i+=2){
drh987eb1f2015-08-17 15:17:37 +0000897 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
898 sqlite3_result_error(context, "json_object() labels must be TEXT", -1);
899 jsonZero(&jx);
900 return;
901 }
drhd0960592015-08-17 21:22:32 +0000902 jsonAppendSeparator(&jx);
drh987eb1f2015-08-17 15:17:37 +0000903 z = (const char*)sqlite3_value_text(argv[i]);
904 n = (u32)sqlite3_value_bytes(argv[i]);
905 jsonAppendString(&jx, z, n);
drhd0960592015-08-17 21:22:32 +0000906 jsonAppendChar(&jx, ':');
907 jsonAppendValue(&jx, argv[i+1]);
drh987eb1f2015-08-17 15:17:37 +0000908 }
drhd0960592015-08-17 21:22:32 +0000909 jsonAppendChar(&jx, '}');
drh987eb1f2015-08-17 15:17:37 +0000910 jsonResult(&jx);
911}
912
913
914/*
drh301eecc2015-08-17 20:14:19 +0000915** json_remove(JSON, PATH, ...)
916**
917** Remove the named elements from JSON and return the result. Ill-formed
918** PATH arguments are silently ignored. If JSON is ill-formed, then NULL
919** is returned.
920*/
921static void jsonRemoveFunc(
922 sqlite3_context *context,
923 int argc,
924 sqlite3_value **argv
925){
926 JsonParse x; /* The parse */
927 JsonNode *pNode;
928 const char *zPath;
929 u32 i;
930
931 if( argc<1 ) return;
932 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
933 if( x.nNode ){
934 for(i=1; i<argc; i++){
935 zPath = (const char*)sqlite3_value_text(argv[i]);
936 if( zPath==0 ) continue;
937 if( zPath[0]!='$' ) continue;
938 pNode = jsonLookup(x.aNode, &zPath[1]);
939 if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
940 }
941 if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
drhd0960592015-08-17 21:22:32 +0000942 jsonReturn(x.aNode, context, 0);
943 }
944 }
945 sqlite3_free(x.aNode);
946}
947
948/*
949** json_replace(JSON, PATH, VALUE, ...)
950**
951** Replace the value at PATH with VALUE. If PATH does not already exist,
952** this routine is a no-op. If JSON is ill-formed, return NULL.
953*/
954static void jsonReplaceFunc(
955 sqlite3_context *context,
956 int argc,
957 sqlite3_value **argv
958){
959 JsonParse x; /* The parse */
960 JsonNode *pNode;
961 const char *zPath;
962 u32 i;
963
964 if( argc<1 ) return;
965 if( (argc&1)==0 ) {
966 sqlite3_result_error(context,
967 "json_replace() needs an odd number of arguments", -1);
968 return;
969 }
970 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
971 if( x.nNode ){
972 for(i=1; i<argc; i+=2){
973 zPath = (const char*)sqlite3_value_text(argv[i]);
974 if( zPath==0 ) continue;
975 if( zPath[0]!='$' ) continue;
976 pNode = jsonLookup(x.aNode, &zPath[1]);
977 if( pNode ){
978 pNode->jnFlags |= JNODE_REPLACE;
979 pNode->iVal = i+1;
980 }
981 }
982 if( x.aNode[0].jnFlags & JNODE_REPLACE ){
983 sqlite3_result_value(context, argv[x.aNode[0].iVal]);
984 }else{
985 jsonReturn(x.aNode, context, argv);
drh301eecc2015-08-17 20:14:19 +0000986 }
987 }
988 sqlite3_free(x.aNode);
989}
990
991/*
drh987eb1f2015-08-17 15:17:37 +0000992** json_type(JSON)
993** json_type(JSON, PATH)
994**
995** Return the top-level "type" of a JSON string. Return NULL if the
996** input is not a well-formed JSON string.
997*/
998static void jsonTypeFunc(
999 sqlite3_context *context,
1000 int argc,
1001 sqlite3_value **argv
1002){
1003 JsonParse x; /* The parse */
1004 const char *zPath;
1005
1006 if( argc==2 ){
1007 zPath = (const char*)sqlite3_value_text(argv[1]);
1008 if( zPath==0 ) return;
1009 if( zPath[0]!='$' ) return;
1010 zPath++;
1011 }else{
1012 zPath = 0;
1013 }
1014 if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
1015 if( x.nNode ){
1016 JsonNode *pNode = x.aNode;
1017 if( zPath ) pNode = jsonLookup(pNode, zPath);
1018 sqlite3_result_text(context, jsonType[pNode->eType], -1, SQLITE_STATIC);
1019 }
1020 sqlite3_free(x.aNode);
1021}
drh5634cc02015-08-17 11:28:03 +00001022
drh5fa5c102015-08-12 16:49:40 +00001023#ifdef _WIN32
1024__declspec(dllexport)
1025#endif
1026int sqlite3_json_init(
1027 sqlite3 *db,
1028 char **pzErrMsg,
1029 const sqlite3_api_routines *pApi
1030){
1031 int rc = SQLITE_OK;
1032 int i;
1033 static const struct {
1034 const char *zName;
1035 int nArg;
1036 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
1037 } aFunc[] = {
drh987eb1f2015-08-17 15:17:37 +00001038 { "json_array", -1, jsonArrayFunc },
1039 { "json_array_length", 1, jsonArrayLengthFunc },
1040 { "json_array_length", 2, jsonArrayLengthFunc },
1041 { "json_extract", 2, jsonExtractFunc },
1042 { "json_object", -1, jsonObjectFunc },
drh301eecc2015-08-17 20:14:19 +00001043 { "json_remove", -1, jsonRemoveFunc },
drhd0960592015-08-17 21:22:32 +00001044 { "json_replace", -1, jsonReplaceFunc },
drh987eb1f2015-08-17 15:17:37 +00001045 { "json_type", 1, jsonTypeFunc },
1046 { "json_type", 2, jsonTypeFunc },
1047
drh301eecc2015-08-17 20:14:19 +00001048#if SQLITE_DEBUG
drh987eb1f2015-08-17 15:17:37 +00001049 /* DEBUG and TESTING functions */
1050 { "json_parse", 1, jsonParseFunc },
1051 { "json_test1", 1, jsonTest1Func },
1052 { "json_nodecount", 1, jsonNodeCountFunc },
drh301eecc2015-08-17 20:14:19 +00001053#endif
drh5fa5c102015-08-12 16:49:40 +00001054 };
1055 SQLITE_EXTENSION_INIT2(pApi);
1056 (void)pzErrMsg; /* Unused parameter */
1057 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
1058 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
1059 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
1060 aFunc[i].xFunc, 0, 0);
1061 }
1062 return rc;
1063}