blob: 4f3e1c02bbc89a4a1436881e13d9c2396351b029 [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**
18** JSON is pure text. JSONB is a binary encoding that is smaller and easier
19** to parse but which holds the equivalent information. Conversions between
20** JSON and JSONB are lossless.
21**
22** Most of the functions here will accept either JSON or JSONB input. The
23** input is understood to be JSONB if it a BLOB and JSON if the input is
24** of any other type. Functions that begin with the "json_" prefix return
25** JSON and functions that begin with "jsonb_" return JSONB.
drhbd0621b2015-08-13 13:54:59 +000026**
27** JSONB format:
28**
29** A JSONB blob is a sequence of terms. Each term begins with a single
30** variable length integer X which determines the type and size of the term.
31**
32** type = X%8
33** size = X>>3
34**
35** Term types are 0 through 7 for null, true, false, integer, real, string,
36** array, and object. The meaning of size depends on the type.
37**
38** For null, true, and false terms, the size is always 0.
39**
40** For integer terms, the size is the number of bytes that contains the
41** integer value. The value is stored as big-endian twos-complement.
42**
43** For real terms, the size is always 8 and the value is a big-ending
44** double-precision floating-point number.
45**
46** For string terms, the size is the number of bytes in the string. The
47** string itself immediately follows the X integer. There are no escapes
48** and the string is not zero-terminated. The string is always stored as
49** UTF8.
50**
51** For array terms, the size is the number of bytes in content. The
52** content consists of zero or more additional terms that are the elements
53** of the array.
54**
55** For object terms, the size is the number of bytes of content. The
56** content is zero or more pairs of terms. The first element of each
57** pair is a string term which is the label and the second element is
58** the value.
59**
60** Variable Length Integers:
61**
62** The variable length integer encoding is the 64-bit unsigned integer encoding
63** originally developed for SQLite4. The encoding for each integer is between
64** 1 and 9 bytes. Call those bytes A0 through A8. The encoding is as follows:
65**
66** If A0 is between 0 and 240 inclusive, then the value is A0.
67**
68** If A0 is between 241 and 248 inclusive, then the value is
69** 240+256*(A0-241)+A1.
70**
71** If A0 is 249 then the value is 2288+256*A1+A2.
72**
73** If A0 is 250 or more then the value is a (A0-247)-byte big-endian
74** integer taken from starting at A1.
drh5fa5c102015-08-12 16:49:40 +000075*/
76#include "sqlite3ext.h"
77SQLITE_EXTENSION_INIT1
78#include <assert.h>
79#include <string.h>
80
81/* Unsigned integer types */
82typedef sqlite3_uint64 u64;
83typedef unsigned int u32;
84typedef unsigned char u8;
85
drhbd0621b2015-08-13 13:54:59 +000086/* An instance of this object represents a JSON string or
87** JSONB blob under construction.
drh5fa5c102015-08-12 16:49:40 +000088*/
89typedef struct Json Json;
90struct Json {
91 sqlite3_context *pCtx; /* Function context - put error messages here */
drhbd0621b2015-08-13 13:54:59 +000092 char *zBuf; /* Append JSON or JSONB content here */
drh5fa5c102015-08-12 16:49:40 +000093 u64 nAlloc; /* Bytes of storage available in zBuf[] */
94 u64 nUsed; /* Bytes of zBuf[] currently used */
95 u8 bStatic; /* True if zBuf is static space */
96 u8 mallocFailed; /* True if an OOM has been encountered */
97 char zSpace[100]; /* Initial static space */
98};
99
drhbd0621b2015-08-13 13:54:59 +0000100/* JSONB type values
101*/
102#define JSONB_NULL 0
103#define JSONB_TRUE 1
104#define JSONB_FALSE 2
105#define JSONB_INT 3
106#define JSONB_REAL 4
107#define JSONB_STRING 5
108#define JSONB_ARRAY 6
109#define JSONB_OBJECT 7
110
111#if 0
112/*
113** Decode the varint in the first n bytes z[]. Write the integer value
114** into *pResult and return the number of bytes in the varint.
115**
116** If the decode fails because there are not enough bytes in z[] then
117** return 0;
118*/
119static int jsonGetVarint64(
120 const unsigned char *z,
121 int n,
122 u64 *pResult
123){
124 unsigned int x;
125 if( n<1 ) return 0;
126 if( z[0]<=240 ){
127 *pResult = z[0];
128 return 1;
129 }
130 if( z[0]<=248 ){
131 if( n<2 ) return 0;
132 *pResult = (z[0]-241)*256 + z[1] + 240;
133 return 2;
134 }
135 if( n<z[0]-246 ) return 0;
136 if( z[0]==249 ){
137 *pResult = 2288 + 256*z[1] + z[2];
138 return 3;
139 }
140 if( z[0]==250 ){
141 *pResult = (z[1]<<16) + (z[2]<<8) + z[3];
142 return 4;
143 }
144 x = (z[1]<<24) + (z[2]<<16) + (z[3]<<8) + z[4];
145 if( z[0]==251 ){
146 *pResult = x;
147 return 5;
148 }
149 if( z[0]==252 ){
150 *pResult = (((u64)x)<<8) + z[5];
151 return 6;
152 }
153 if( z[0]==253 ){
154 *pResult = (((u64)x)<<16) + (z[5]<<8) + z[6];
155 return 7;
156 }
157 if( z[0]==254 ){
158 *pResult = (((u64)x)<<24) + (z[5]<<16) + (z[6]<<8) + z[7];
159 return 8;
160 }
161 *pResult = (((u64)x)<<32) +
162 (0xffffffff & ((z[5]<<24) + (z[6]<<16) + (z[7]<<8) + z[8]));
163 return 9;
164}
165#endif
166
drh5fa5c102015-08-12 16:49:40 +0000167/* Set the Json object to an empty string
168*/
169static void jsonZero(Json *p){
170 p->zBuf = p->zSpace;
171 p->nAlloc = sizeof(p->zSpace);
172 p->nUsed = 0;
173 p->bStatic = 1;
174}
175
176/* Initialize the Json object
177*/
178static void jsonInit(Json *p, sqlite3_context *pCtx){
179 p->pCtx = pCtx;
180 p->mallocFailed = 0;
181 jsonZero(p);
182}
183
184
185/* Free all allocated memory and reset the Json object back to its
186** initial state.
187*/
188static void jsonReset(Json *p){
189 if( !p->bStatic ) sqlite3_free(p->zBuf);
190 jsonZero(p);
191}
192
193
194/* Report an out-of-memory (OOM) condition
195*/
196static void jsonOom(Json *p){
197 p->mallocFailed = 1;
198 sqlite3_result_error_nomem(p->pCtx);
199 jsonReset(p);
200}
201
202/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
203** Return zero on success. Return non-zero on an OOM error
204*/
205static int jsonGrow(Json *p, u32 N){
206 u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+100;
207 char *zNew;
208 if( p->bStatic ){
209 if( p->mallocFailed ) return SQLITE_NOMEM;
210 zNew = sqlite3_malloc64(nTotal);
211 if( zNew==0 ){
212 jsonOom(p);
213 return SQLITE_NOMEM;
214 }
215 memcpy(zNew, p->zBuf, p->nUsed);
216 p->zBuf = zNew;
217 p->bStatic = 0;
218 }else{
219 zNew = sqlite3_realloc64(p->zBuf, nTotal);
220 if( zNew==0 ){
221 jsonOom(p);
222 return SQLITE_NOMEM;
223 }
224 p->zBuf = zNew;
225 }
226 p->nAlloc = nTotal;
227 return SQLITE_OK;
228}
229
230/* Append N bytes from zIn onto the end of the Json string.
231*/
232static void jsonAppendRaw(Json *p, const char *zIn, u32 N){
233 if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
234 memcpy(p->zBuf+p->nUsed, zIn, N);
235 p->nUsed += N;
236}
237
238/* Append the N-byte string in zIn to the end of the Json string
239** under construction. Enclose the string in "..." and escape
240** any double-quotes or backslash characters contained within the
241** string.
242*/
243static void jsonAppendString(Json *p, const char *zIn, u32 N){
244 u32 i;
245 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
246 p->zBuf[p->nUsed++] = '"';
247 for(i=0; i<N; i++){
248 char c = zIn[i];
249 if( c=='"' || c=='\\' ){
250 if( (p->nUsed+N+1-i > p->nAlloc) && jsonGrow(p,N+1-i)!=0 ) return;
251 p->zBuf[p->nUsed++] = '\\';
252 }
253 p->zBuf[p->nUsed++] = c;
254 }
255 p->zBuf[p->nUsed++] = '"';
256}
257
drhbd0621b2015-08-13 13:54:59 +0000258/*
259** Write a 32-bit unsigned integer as 4 big-endian bytes.
260*/
261static void jsonPutInt32(unsigned char *z, unsigned int y){
262 z[0] = (unsigned char)(y>>24);
263 z[1] = (unsigned char)(y>>16);
264 z[2] = (unsigned char)(y>>8);
265 z[3] = (unsigned char)(y);
266}
267
268
269/* Write integer X as a variable-length integer into the buffer z[].
270** z[] is guaranteed to be at least 9 bytes in length. Return the
271** number of bytes written.
272*/
273int jsonPutVarint64(char *zIn, u64 x){
274 unsigned char *z = (unsigned char*)zIn;
275 unsigned int w, y;
276 if( x<=240 ){
277 z[0] = (unsigned char)x;
278 return 1;
279 }
280 if( x<=2287 ){
281 y = (unsigned int)(x - 240);
282 z[0] = (unsigned char)(y/256 + 241);
283 z[1] = (unsigned char)(y%256);
284 return 2;
285 }
286 if( x<=67823 ){
287 y = (unsigned int)(x - 2288);
288 z[0] = 249;
289 z[1] = (unsigned char)(y/256);
290 z[2] = (unsigned char)(y%256);
291 return 3;
292 }
293 y = (unsigned int)x;
294 w = (unsigned int)(x>>32);
295 if( w==0 ){
296 if( y<=16777215 ){
297 z[0] = 250;
298 z[1] = (unsigned char)(y>>16);
299 z[2] = (unsigned char)(y>>8);
300 z[3] = (unsigned char)(y);
301 return 4;
302 }
303 z[0] = 251;
304 jsonPutInt32(z+1, y);
305 return 5;
306 }
307 if( w<=255 ){
308 z[0] = 252;
309 z[1] = (unsigned char)w;
310 jsonPutInt32(z+2, y);
311 return 6;
312 }
313 if( w<=65535 ){
314 z[0] = 253;
315 z[1] = (unsigned char)(w>>8);
316 z[2] = (unsigned char)w;
317 jsonPutInt32(z+3, y);
318 return 7;
319 }
320 if( w<=16777215 ){
321 z[0] = 254;
322 z[1] = (unsigned char)(w>>16);
323 z[2] = (unsigned char)(w>>8);
324 z[3] = (unsigned char)w;
325 jsonPutInt32(z+4, y);
326 return 8;
327 }
328 z[0] = 255;
329 jsonPutInt32(z+1, w);
330 jsonPutInt32(z+5, y);
331 return 9;
332}
333
334
335/* Append integer X as a variable-length integer on the JSONB currently
336** under construction in p.
337*/
338static void jsonAppendVarint(Json *p, u64 X){
339 if( (p->nUsed+9 > p->nAlloc) && jsonGrow(p,9)!=0 ) return;
340 p->nUsed += jsonPutVarint64(p->zBuf+p->nUsed, X);
341}
342
343/* Make the JSON in p the result of the SQL function.
drh5fa5c102015-08-12 16:49:40 +0000344*/
345static void jsonResult(Json *p){
346 if( p->mallocFailed==0 ){
347 sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed,
348 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
349 SQLITE_UTF8);
350 jsonZero(p);
351 }
352 assert( p->bStatic );
353}
354
drhbd0621b2015-08-13 13:54:59 +0000355/* Make the JSONB in p the result of the SQL function.
356*/
357static void jsonbResult(Json *p){
358 if( p->mallocFailed==0 ){
359 sqlite3_result_blob(p->pCtx, p->zBuf, p->nUsed,
360 p->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
361 jsonZero(p);
362 }
363 assert( p->bStatic );
364}
365
drh5fa5c102015-08-12 16:49:40 +0000366/*
367** Implementation of the json_array(VALUE,...) function. Return a JSON
368** array that contains all values given in arguments. Or if any argument
369** is a BLOB, throw an error.
370*/
371static void jsonArrayFunc(
372 sqlite3_context *context,
373 int argc,
374 sqlite3_value **argv
375){
376 int i;
377 Json jx;
378 char cSep = '[';
379
380 jsonInit(&jx, context);
381 for(i=0; i<argc; i++){
382 jsonAppendRaw(&jx, &cSep, 1);
383 cSep = ',';
384 switch( sqlite3_value_type(argv[i]) ){
385 case SQLITE_NULL: {
386 jsonAppendRaw(&jx, "null", 4);
387 break;
388 }
389 case SQLITE_INTEGER:
390 case SQLITE_FLOAT: {
391 const char *z = (const char*)sqlite3_value_text(argv[i]);
392 u32 n = (u32)sqlite3_value_bytes(argv[i]);
393 jsonAppendRaw(&jx, z, n);
394 break;
395 }
396 case SQLITE_TEXT: {
397 const char *z = (const char*)sqlite3_value_text(argv[i]);
398 u32 n = (u32)sqlite3_value_bytes(argv[i]);
399 jsonAppendString(&jx, z, n);
400 break;
401 }
402 default: {
403 jsonZero(&jx);
404 sqlite3_result_error(context, "JSON cannot hold BLOB values", -1);
405 return;
406 }
407 }
408 }
409 jsonAppendRaw(&jx, "]", 1);
drh2032d602015-08-12 17:23:34 +0000410 jsonResult(&jx);
411}
412
413/*
drhbd0621b2015-08-13 13:54:59 +0000414** Implementation of the jsonb_array(VALUE,...) function. Return a JSON
415** array that contains all values given in arguments. Or if any argument
416** is a BLOB, throw an error.
417*/
418static void jsonbArrayFunc(
419 sqlite3_context *context,
420 int argc,
421 sqlite3_value **argv
422){
423 int i;
424 Json jx;
425
426 jsonInit(&jx, context);
427 jx.nUsed = 5;
428 for(i=0; i<argc; i++){
429 switch( sqlite3_value_type(argv[i]) ){
430 case SQLITE_NULL: {
431 jsonAppendVarint(&jx, JSONB_NULL);
432 break;
433 }
434 case SQLITE_INTEGER:
435 case SQLITE_FLOAT: {
436 const char *z = (const char*)sqlite3_value_text(argv[i]);
437 u32 n = (u32)sqlite3_value_bytes(argv[i]);
438 jsonAppendRaw(&jx, z, n);
439 break;
440 }
441 case SQLITE_TEXT: {
442 const char *z = (const char*)sqlite3_value_text(argv[i]);
443 u32 n = (u32)sqlite3_value_bytes(argv[i]);
444 jsonAppendVarint(&jx, JSONB_STRING + 4*(u64)n);
445 jsonAppendString(&jx, z, n);
446 break;
447 }
448 default: {
449 jsonZero(&jx);
450 sqlite3_result_error(context, "JSON cannot hold BLOB values", -1);
451 return;
452 }
453 }
454 }
455 if( jx.mallocFailed==0 ){
456 jx.zBuf[0] = 251;
457 jsonPutInt32((unsigned char*)(jx.zBuf+1), jx.nUsed-5);
458 jsonbResult(&jx);
459 }
460}
461
462/*
drh2032d602015-08-12 17:23:34 +0000463** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
464** object that contains all name/value given in arguments. Or if any name
465** is not a string or if any value is a BLOB, throw an error.
466*/
467static void jsonObjectFunc(
468 sqlite3_context *context,
469 int argc,
470 sqlite3_value **argv
471){
472 int i;
473 Json jx;
474 char cSep = '{';
475 const char *z;
476 u32 n;
477
478 if( argc&1 ){
479 sqlite3_result_error(context, "json_object() requires an even number "
480 "of arguments", -1);
481 return;
482 }
483 jsonInit(&jx, context);
484 for(i=0; i<argc; i+=2){
485 jsonAppendRaw(&jx, &cSep, 1);
486 cSep = ',';
487 if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
488 sqlite3_result_error(context, "json_object() labels must be TEXT", -1);
489 jsonZero(&jx);
490 return;
491 }
492 z = (const char*)sqlite3_value_text(argv[i]);
493 n = (u32)sqlite3_value_bytes(argv[i]);
494 jsonAppendString(&jx, z, n);
495 jsonAppendRaw(&jx, ":", 1);
496 switch( sqlite3_value_type(argv[i+1]) ){
497 case SQLITE_NULL: {
498 jsonAppendRaw(&jx, "null", 4);
499 break;
500 }
501 case SQLITE_INTEGER:
502 case SQLITE_FLOAT: {
503 z = (const char*)sqlite3_value_text(argv[i+1]);
504 n = (u32)sqlite3_value_bytes(argv[i+1]);
505 jsonAppendRaw(&jx, z, n);
506 break;
507 }
508 case SQLITE_TEXT: {
509 z = (const char*)sqlite3_value_text(argv[i+1]);
510 n = (u32)sqlite3_value_bytes(argv[i+1]);
511 jsonAppendString(&jx, z, n);
512 break;
513 }
514 default: {
515 jsonZero(&jx);
516 sqlite3_result_error(context, "JSON cannot hold BLOB values", -1);
517 return;
518 }
519 }
520 }
521 jsonAppendRaw(&jx, "}", 1);
522 jsonResult(&jx);
drh5fa5c102015-08-12 16:49:40 +0000523}
524
525#ifdef _WIN32
526__declspec(dllexport)
527#endif
528int sqlite3_json_init(
529 sqlite3 *db,
530 char **pzErrMsg,
531 const sqlite3_api_routines *pApi
532){
533 int rc = SQLITE_OK;
534 int i;
535 static const struct {
536 const char *zName;
537 int nArg;
538 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
539 } aFunc[] = {
drh2032d602015-08-12 17:23:34 +0000540 { "json_array", -1, jsonArrayFunc },
drhbd0621b2015-08-13 13:54:59 +0000541 { "jsonb_array", -1, jsonbArrayFunc },
drh2032d602015-08-12 17:23:34 +0000542 { "json_object", -1, jsonObjectFunc },
drh5fa5c102015-08-12 16:49:40 +0000543 };
544 SQLITE_EXTENSION_INIT2(pApi);
545 (void)pzErrMsg; /* Unused parameter */
546 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
547 rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
548 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
549 aFunc[i].xFunc, 0, 0);
550 }
551 return rc;
552}