drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 1 | /* |
| 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 | ** |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 18 | ** 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.) |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 21 | */ |
| 22 | #include "sqlite3ext.h" |
| 23 | SQLITE_EXTENSION_INIT1 |
| 24 | #include <assert.h> |
| 25 | #include <string.h> |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 26 | #include <ctype.h> |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 27 | #include <stdlib.h> |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 28 | |
| 29 | /* Unsigned integer types */ |
| 30 | typedef sqlite3_uint64 u64; |
| 31 | typedef unsigned int u32; |
| 32 | typedef unsigned char u8; |
| 33 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 34 | /* 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. |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 37 | */ |
| 38 | typedef struct Json Json; |
| 39 | struct Json { |
| 40 | sqlite3_context *pCtx; /* Function context - put error messages here */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 41 | char *zBuf; /* Append JSON content here */ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 42 | 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 */ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 45 | u8 bErr; /* True if an error has been encountered */ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 46 | char zSpace[100]; /* Initial static space */ |
| 47 | }; |
| 48 | |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 49 | /* JSON type values |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 50 | */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 51 | #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 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 60 | /* |
| 61 | ** Names of the various JSON types: |
| 62 | */ |
| 63 | static const char * const jsonType[] = { |
| 64 | "null", "true", "false", "integer", "real", "text", "array", "object" |
| 65 | }; |
| 66 | |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 67 | /* 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 */ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 72 | #define JNODE_REPLACE 0x08 /* Replace with JsonNode.iVal */ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 73 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 74 | |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 75 | /* A single node of parsed JSON |
| 76 | */ |
| 77 | typedef struct JsonNode JsonNode; |
| 78 | struct JsonNode { |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 79 | u8 eType; /* One of the JSON_ type values */ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 80 | u8 jnFlags; /* JNODE flags */ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 81 | u8 iVal; /* Replacement value when JNODE_REPLACE */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 82 | u32 n; /* Bytes of content, or number of sub-nodes */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 83 | const char *zJContent; /* JSON content */ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /* A completely parsed JSON string |
| 87 | */ |
| 88 | typedef struct JsonParse JsonParse; |
| 89 | struct 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 | |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 97 | /* |
| 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 | */ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 102 | static u32 jsonSize(JsonNode *pNode){ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 103 | return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1; |
| 104 | } |
| 105 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 106 | /* Set the Json object to an empty string |
| 107 | */ |
| 108 | static 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 | */ |
| 117 | static void jsonInit(Json *p, sqlite3_context *pCtx){ |
| 118 | p->pCtx = pCtx; |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 119 | p->bErr = 0; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 120 | jsonZero(p); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /* Free all allocated memory and reset the Json object back to its |
| 125 | ** initial state. |
| 126 | */ |
| 127 | static 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 | */ |
| 135 | static void jsonOom(Json *p){ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 136 | if( !p->bErr ){ |
| 137 | p->bErr = 1; |
| 138 | sqlite3_result_error_nomem(p->pCtx); |
| 139 | jsonReset(p); |
| 140 | } |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 141 | } |
| 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 | */ |
| 146 | static int jsonGrow(Json *p, u32 N){ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 147 | u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 148 | char *zNew; |
| 149 | if( p->bStatic ){ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 150 | if( p->bErr ) return 1; |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 151 | 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 | */ |
| 173 | static 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 | |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 179 | #ifdef SQLITE_DEBUG |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 180 | /* Append the zero-terminated string zIn |
| 181 | */ |
| 182 | static void jsonAppend(Json *p, const char *zIn){ |
| 183 | jsonAppendRaw(p, zIn, (u32)strlen(zIn)); |
| 184 | } |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 185 | #endif |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 186 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 187 | /* Append a single character |
| 188 | */ |
| 189 | static 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 | |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 194 | /* Append a comma separator to the output buffer, if the previous |
| 195 | ** character is not '[' or '{'. |
| 196 | */ |
| 197 | static 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 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 204 | /* 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 | */ |
| 209 | static 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 | |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 224 | /* |
| 225 | ** Append a function parameter value to the JSON string under |
| 226 | ** construction. |
| 227 | */ |
| 228 | static 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 | |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 262 | /* Make the JSON in p the result of the SQL function. |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 263 | */ |
| 264 | static void jsonResult(Json *p){ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 265 | if( p->bErr==0 ){ |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 266 | 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 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 274 | /* |
| 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. |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 278 | */ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 279 | static int jsonRenderNode( |
| 280 | JsonNode *pNode, /* The node to render */ |
| 281 | Json *pOut, /* Write JSON here */ |
| 282 | sqlite3_value **aReplace /* Replacement values */ |
| 283 | ){ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 284 | u32 j = 1; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 285 | 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: { |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 299 | if( pNode->jnFlags & JNODE_RAW ){ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 300 | 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, '['); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 312 | while( j<=pNode->n ){ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 313 | 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]); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 319 | }else{ |
| 320 | jsonAppendSeparator(pOut); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 321 | j += jsonRenderNode(&pNode[j], pOut, aReplace); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 322 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 323 | } |
| 324 | jsonAppendChar(pOut, ']'); |
| 325 | break; |
| 326 | } |
| 327 | case JSON_OBJECT: { |
| 328 | jsonAppendChar(pOut, '{'); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 329 | while( j<=pNode->n ){ |
| 330 | if( pNode[j+1].jnFlags & JNODE_REMOVE ){ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 331 | j += 1 + jsonSize(&pNode[j+1]); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 332 | }else{ |
| 333 | jsonAppendSeparator(pOut); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 334 | jsonRenderNode(&pNode[j], pOut, aReplace); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 335 | jsonAppendChar(pOut, ':'); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 336 | 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 | } |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 342 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 343 | } |
| 344 | jsonAppendChar(pOut, '}'); |
| 345 | break; |
| 346 | } |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 347 | } |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 348 | return j; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /* |
| 352 | ** Make the JsonNode the return value of the function. |
| 353 | */ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 354 | static 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 | ){ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 359 | 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 | } |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 372 | case JSON_REAL: { |
| 373 | double r = strtod(pNode->zJContent, 0); |
| 374 | sqlite3_result_double(pCtx, r); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 375 | break; |
| 376 | } |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 377 | 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 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 386 | case JSON_STRING: { |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 387 | if( pNode->jnFlags & JNODE_RAW ){ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 388 | sqlite3_result_text(pCtx, pNode->zJContent, pNode->n, SQLITE_TRANSIENT); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 389 | }else if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 390 | /* JSON formatted without any backslash-escapes */ |
| 391 | sqlite3_result_text(pCtx, pNode->zJContent+1, pNode->n-2, |
| 392 | SQLITE_TRANSIENT); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 393 | }else{ |
| 394 | /* Translate JSON formatted string into raw text */ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 395 | 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); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 455 | } |
| 456 | break; |
| 457 | } |
| 458 | case JSON_ARRAY: |
| 459 | case JSON_OBJECT: { |
| 460 | Json s; |
| 461 | jsonInit(&s, pCtx); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 462 | jsonRenderNode(pNode, &s, aReplace); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 463 | jsonResult(&s); |
| 464 | break; |
| 465 | } |
| 466 | } |
drh | bd0621b | 2015-08-13 13:54:59 +0000 | [diff] [blame] | 467 | } |
| 468 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 469 | /* |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 470 | ** 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 | */ |
| 474 | static 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]; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 499 | p->eType = (u8)eType; |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 500 | p->jnFlags = 0; |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 501 | p->iVal = 0; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 502 | p->n = n; |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 503 | p->zJContent = zContent; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 504 | 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 | */ |
| 515 | static 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 */ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 571 | u8 jnFlags = 0; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 572 | 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; |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 579 | jnFlags = JNODE_ESCAPE; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 580 | }else if( c=='"' ){ |
| 581 | break; |
| 582 | } |
| 583 | j++; |
| 584 | } |
| 585 | jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 586 | pParse->aNode[pParse->nNode-1].jnFlags = jnFlags; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 587 | return j+1; |
| 588 | }else if( c=='n' |
| 589 | && strncmp(pParse->zJson+i,"null",4)==0 |
drh | b2cd10e | 2015-08-15 21:29:14 +0000 | [diff] [blame] | 590 | && !isalnum(pParse->zJson[i+4]) ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 591 | jsonParseAddNode(pParse, JSON_NULL, 0, 0); |
| 592 | return i+4; |
| 593 | }else if( c=='t' |
| 594 | && strncmp(pParse->zJson+i,"true",4)==0 |
drh | b2cd10e | 2015-08-15 21:29:14 +0000 | [diff] [blame] | 595 | && !isalnum(pParse->zJson[i+4]) ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 596 | jsonParseAddNode(pParse, JSON_TRUE, 0, 0); |
| 597 | return i+4; |
| 598 | }else if( c=='f' |
| 599 | && strncmp(pParse->zJson+i,"false",5)==0 |
drh | b2cd10e | 2015-08-15 21:29:14 +0000 | [diff] [blame] | 600 | && !isalnum(pParse->zJson[i+5]) ){ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 601 | 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 | */ |
| 647 | static 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 | } |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 666 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 667 | /* |
| 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 | */ |
| 672 | static 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++; |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 688 | j += jsonSize(&pRoot[j]); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 689 | } |
| 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 ){ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 702 | j += jsonSize(&pRoot[j]); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 703 | 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 | ****************************************************************************/ |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 715 | |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 716 | #ifdef SQLITE_DEBUG |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 717 | /* |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 718 | ** The json_parse(JSON) function returns a string which describes |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 719 | ** a parse of the JSON provided. Or it returns NULL if JSON is not |
| 720 | ** well-formed. |
| 721 | */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 722 | static void jsonParseFunc( |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 723 | 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; |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 730 | char zBuf[100]; |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 731 | |
| 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++){ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 736 | sqlite3_snprintf(sizeof(zBuf), zBuf, "node %3u: %7s n=%d\n", |
| 737 | i, jsonType[x.aNode[i].eType], x.aNode[i].n); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 738 | jsonAppend(&s, zBuf); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 739 | if( x.aNode[i].zJContent!=0 ){ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 740 | jsonAppendRaw(&s, " text: ", 10); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 741 | jsonAppendRaw(&s, x.aNode[i].zJContent, x.aNode[i].n); |
drh | e9c37f3 | 2015-08-15 21:25:36 +0000 | [diff] [blame] | 742 | jsonAppendRaw(&s, "\n", 1); |
| 743 | } |
| 744 | } |
| 745 | sqlite3_free(x.aNode); |
| 746 | jsonResult(&s); |
| 747 | } |
| 748 | |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 749 | /* |
| 750 | ** The json_test1(JSON) function parses and rebuilds the JSON string. |
| 751 | */ |
| 752 | static 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; |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 759 | jsonReturn(x.aNode, context, 0); |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 760 | 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 | */ |
| 767 | static 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 | } |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 777 | #endif /* SQLITE_DEBUG */ |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 778 | |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 779 | /**************************************************************************** |
| 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 | */ |
| 788 | static void jsonArrayFunc( |
| 789 | sqlite3_context *context, |
| 790 | int argc, |
| 791 | sqlite3_value **argv |
| 792 | ){ |
| 793 | int i; |
| 794 | Json jx; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 795 | |
| 796 | jsonInit(&jx, context); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 797 | jsonAppendChar(&jx, '['); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 798 | for(i=0; i<argc; i++){ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 799 | jsonAppendSeparator(&jx); |
| 800 | jsonAppendValue(&jx, argv[i]); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 801 | } |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 802 | jsonAppendChar(&jx, ']'); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 803 | 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 | */ |
| 814 | static 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 ){ |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 837 | for(i=1; i<=pNode->n; n++){ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 838 | i += jsonSize(&pNode[i]); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 839 | } |
| 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 | */ |
| 853 | static 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 ){ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 869 | jsonReturn(pNode, context, 0); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 870 | } |
| 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 | */ |
| 879 | static void jsonObjectFunc( |
| 880 | sqlite3_context *context, |
| 881 | int argc, |
| 882 | sqlite3_value **argv |
| 883 | ){ |
| 884 | int i; |
| 885 | Json jx; |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 886 | 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); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 895 | jsonAppendChar(&jx, '{'); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 896 | for(i=0; i<argc; i+=2){ |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 897 | 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 | } |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 902 | jsonAppendSeparator(&jx); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 903 | z = (const char*)sqlite3_value_text(argv[i]); |
| 904 | n = (u32)sqlite3_value_bytes(argv[i]); |
| 905 | jsonAppendString(&jx, z, n); |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 906 | jsonAppendChar(&jx, ':'); |
| 907 | jsonAppendValue(&jx, argv[i+1]); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 908 | } |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 909 | jsonAppendChar(&jx, '}'); |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 910 | jsonResult(&jx); |
| 911 | } |
| 912 | |
| 913 | |
| 914 | /* |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 915 | ** 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 | */ |
| 921 | static 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 ){ |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 942 | 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 | */ |
| 954 | static 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); |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | sqlite3_free(x.aNode); |
| 989 | } |
| 990 | |
| 991 | /* |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 992 | ** 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 | */ |
| 998 | static 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 | } |
drh | 5634cc0 | 2015-08-17 11:28:03 +0000 | [diff] [blame] | 1022 | |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 1023 | #ifdef _WIN32 |
| 1024 | __declspec(dllexport) |
| 1025 | #endif |
| 1026 | int 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[] = { |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1038 | { "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 }, |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1043 | { "json_remove", -1, jsonRemoveFunc }, |
drh | d096059 | 2015-08-17 21:22:32 +0000 | [diff] [blame^] | 1044 | { "json_replace", -1, jsonReplaceFunc }, |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1045 | { "json_type", 1, jsonTypeFunc }, |
| 1046 | { "json_type", 2, jsonTypeFunc }, |
| 1047 | |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1048 | #if SQLITE_DEBUG |
drh | 987eb1f | 2015-08-17 15:17:37 +0000 | [diff] [blame] | 1049 | /* DEBUG and TESTING functions */ |
| 1050 | { "json_parse", 1, jsonParseFunc }, |
| 1051 | { "json_test1", 1, jsonTest1Func }, |
| 1052 | { "json_nodecount", 1, jsonNodeCountFunc }, |
drh | 301eecc | 2015-08-17 20:14:19 +0000 | [diff] [blame] | 1053 | #endif |
drh | 5fa5c10 | 2015-08-12 16:49:40 +0000 | [diff] [blame] | 1054 | }; |
| 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 | } |