/ Check-in [178f9a35]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add extension functions for processing JSON.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 178f9a352c6c9e15e809e1a47530c6592d18578d
User & Date: drh 2015-08-21 20:43:32
Context
2015-08-22
01:32
Do not apply the WHERE-clause pushdown optimization to terms that originate in the ON or USING clause of a LEFT JOIN. Fix for ticket [c2a19d81652f40568c]. check-in: 351bc22f user: drh tags: trunk
2015-08-21
20:43
Add extension functions for processing JSON. check-in: 178f9a35 user: drh tags: trunk
20:37
Fixes for compiler warnings and errors in the makefiles. Rename the one test script to json101.test. Closed-Leaf check-in: 9ff6ccde user: drh tags: json
19:53
Add a missing #define for sqlite3_vsnprintf to sqlite3ext.h. check-in: da3c9df0 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to Makefile.in.

   413    413     $(TOP)/ext/misc/closure.c \
   414    414     $(TOP)/ext/misc/eval.c \
   415    415     $(TOP)/ext/misc/fileio.c \
   416    416     $(TOP)/ext/misc/fuzzer.c \
   417    417     $(TOP)/ext/fts5/fts5_tcl.c \
   418    418     $(TOP)/ext/fts5/fts5_test_mi.c \
   419    419     $(TOP)/ext/misc/ieee754.c \
          420  +  $(TOP)/ext/misc/json1.c \
   420    421     $(TOP)/ext/misc/nextchar.c \
   421    422     $(TOP)/ext/misc/percentile.c \
   422    423     $(TOP)/ext/misc/regexp.c \
   423    424     $(TOP)/ext/misc/series.c \
   424    425     $(TOP)/ext/misc/spellfix.c \
   425    426     $(TOP)/ext/misc/totype.c \
   426    427     $(TOP)/ext/misc/wholenumber.c

Changes to Makefile.msc.

  1079   1079     $(TOP)\ext\misc\eval.c \
  1080   1080     $(TOP)\ext\misc\fileio.c \
  1081   1081     $(TOP)\ext\misc\fuzzer.c \
  1082   1082     fts5.c \
  1083   1083     $(TOP)\ext\fts5\fts5_tcl.c \
  1084   1084     $(TOP)\ext\fts5\fts5_test_mi.c \
  1085   1085     $(TOP)\ext\misc\ieee754.c \
         1086  +  $(TOP)\ext\misc\json1.c \
  1086   1087     $(TOP)\ext\misc\nextchar.c \
  1087   1088     $(TOP)\ext\misc\percentile.c \
  1088   1089     $(TOP)\ext\misc\regexp.c \
  1089   1090     $(TOP)\ext\misc\series.c \
  1090   1091     $(TOP)\ext\misc\spellfix.c \
  1091   1092     $(TOP)\ext\misc\totype.c \
  1092   1093     $(TOP)\ext\misc\wholenumber.c

Added ext/misc/json1.c.

            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  +**
           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.
           21  +** This implementation parses JSON text at 250 MB/s, so it is hard to see
           22  +** how JSONB might improve on that.)
           23  +*/
           24  +#include "sqlite3ext.h"
           25  +SQLITE_EXTENSION_INIT1
           26  +#include <assert.h>
           27  +#include <string.h>
           28  +#include <ctype.h>
           29  +#include <stdlib.h>
           30  +#include <stdarg.h>
           31  +
           32  +#define UNUSED_PARAM(X)  (void)(X)
           33  +
           34  +/* Unsigned integer types */
           35  +typedef sqlite3_uint64 u64;
           36  +typedef unsigned int u32;
           37  +typedef unsigned char u8;
           38  +
           39  +/* Objects */
           40  +typedef struct JsonString JsonString;
           41  +typedef struct JsonNode JsonNode;
           42  +typedef struct JsonParse JsonParse;
           43  +
           44  +/* An instance of this object represents a JSON string
           45  +** under construction.  Really, this is a generic string accumulator
           46  +** that can be and is used to create strings other than JSON.
           47  +*/
           48  +struct JsonString {
           49  +  sqlite3_context *pCtx;   /* Function context - put error messages here */
           50  +  char *zBuf;              /* Append JSON content here */
           51  +  u64 nAlloc;              /* Bytes of storage available in zBuf[] */
           52  +  u64 nUsed;               /* Bytes of zBuf[] currently used */
           53  +  u8 bStatic;              /* True if zBuf is static space */
           54  +  u8 bErr;                 /* True if an error has been encountered */
           55  +  char zSpace[100];        /* Initial static space */
           56  +};
           57  +
           58  +/* JSON type values
           59  +*/
           60  +#define JSON_NULL     0
           61  +#define JSON_TRUE     1
           62  +#define JSON_FALSE    2
           63  +#define JSON_INT      3
           64  +#define JSON_REAL     4
           65  +#define JSON_STRING   5
           66  +#define JSON_ARRAY    6
           67  +#define JSON_OBJECT   7
           68  +
           69  +/*
           70  +** Names of the various JSON types:
           71  +*/
           72  +static const char * const jsonType[] = {
           73  +  "null", "true", "false", "integer", "real", "text", "array", "object"
           74  +};
           75  +
           76  +/* Bit values for the JsonNode.jnFlag field
           77  +*/
           78  +#define JNODE_RAW     0x01         /* Content is raw, not JSON encoded */
           79  +#define JNODE_ESCAPE  0x02         /* Content is text with \ escapes */
           80  +#define JNODE_REMOVE  0x04         /* Do not output */
           81  +#define JNODE_REPLACE 0x08         /* Replace with JsonNode.iVal */
           82  +#define JNODE_APPEND  0x10         /* More ARRAY/OBJECT entries at u.iAppend */
           83  +
           84  +
           85  +/* A single node of parsed JSON
           86  +*/
           87  +struct JsonNode {
           88  +  u8 eType;              /* One of the JSON_ type values */
           89  +  u8 jnFlags;            /* JNODE flags */
           90  +  u8 iVal;               /* Replacement value when JNODE_REPLACE */
           91  +  u32 n;                 /* Bytes of content, or number of sub-nodes */
           92  +  union {
           93  +    const char *zJContent; /* Content for INT, REAL, and STRING */
           94  +    u32 iAppend;           /* More terms for ARRAY and OBJECT */
           95  +    u32 iKey;              /* Key for ARRAY objects in json_tree() */
           96  +  } u;
           97  +};
           98  +
           99  +/* A completely parsed JSON string
          100  +*/
          101  +struct JsonParse {
          102  +  u32 nNode;         /* Number of slots of aNode[] used */
          103  +  u32 nAlloc;        /* Number of slots of aNode[] allocated */
          104  +  JsonNode *aNode;   /* Array of nodes containing the parse */
          105  +  const char *zJson; /* Original JSON string */
          106  +  u32 *aUp;          /* Index of parent of each node */
          107  +  u8 oom;            /* Set to true if out of memory */
          108  +};
          109  +
          110  +/**************************************************************************
          111  +** Utility routines for dealing with JsonString objects
          112  +**************************************************************************/
          113  +
          114  +/* Set the JsonString object to an empty string
          115  +*/
          116  +static void jsonZero(JsonString *p){
          117  +  p->zBuf = p->zSpace;
          118  +  p->nAlloc = sizeof(p->zSpace);
          119  +  p->nUsed = 0;
          120  +  p->bStatic = 1;
          121  +}
          122  +
          123  +/* Initialize the JsonString object
          124  +*/
          125  +static void jsonInit(JsonString *p, sqlite3_context *pCtx){
          126  +  p->pCtx = pCtx;
          127  +  p->bErr = 0;
          128  +  jsonZero(p);
          129  +}
          130  +
          131  +
          132  +/* Free all allocated memory and reset the JsonString object back to its
          133  +** initial state.
          134  +*/
          135  +static void jsonReset(JsonString *p){
          136  +  if( !p->bStatic ) sqlite3_free(p->zBuf);
          137  +  jsonZero(p);
          138  +}
          139  +
          140  +
          141  +/* Report an out-of-memory (OOM) condition 
          142  +*/
          143  +static void jsonOom(JsonString *p){
          144  +  if( !p->bErr ){
          145  +    p->bErr = 1;
          146  +    sqlite3_result_error_nomem(p->pCtx);
          147  +    jsonReset(p);
          148  +  }
          149  +}
          150  +
          151  +/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
          152  +** Return zero on success.  Return non-zero on an OOM error
          153  +*/
          154  +static int jsonGrow(JsonString *p, u32 N){
          155  +  u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
          156  +  char *zNew;
          157  +  if( p->bStatic ){
          158  +    if( p->bErr ) return 1;
          159  +    zNew = sqlite3_malloc64(nTotal);
          160  +    if( zNew==0 ){
          161  +      jsonOom(p);
          162  +      return SQLITE_NOMEM;
          163  +    }
          164  +    memcpy(zNew, p->zBuf, (size_t)p->nUsed);
          165  +    p->zBuf = zNew;
          166  +    p->bStatic = 0;
          167  +  }else{
          168  +    zNew = sqlite3_realloc64(p->zBuf, nTotal);
          169  +    if( zNew==0 ){
          170  +      jsonOom(p);
          171  +      return SQLITE_NOMEM;
          172  +    }
          173  +    p->zBuf = zNew;
          174  +  }
          175  +  p->nAlloc = nTotal;
          176  +  return SQLITE_OK;
          177  +}
          178  +
          179  +/* Append N bytes from zIn onto the end of the JsonString string.
          180  +*/
          181  +static void jsonAppendRaw(JsonString *p, const char *zIn, u32 N){
          182  +  if( (N+p->nUsed >= p->nAlloc) && jsonGrow(p,N)!=0 ) return;
          183  +  memcpy(p->zBuf+p->nUsed, zIn, N);
          184  +  p->nUsed += N;
          185  +}
          186  +
          187  +/* Append formatted text (not to exceed N bytes) to the JsonString.
          188  +*/
          189  +static void jsonPrintf(int N, JsonString *p, const char *zFormat, ...){
          190  +  va_list ap;
          191  +  if( (p->nUsed + N >= p->nAlloc) && jsonGrow(p, N) ) return;
          192  +  va_start(ap, zFormat);
          193  +  sqlite3_vsnprintf(N, p->zBuf+p->nUsed, zFormat, ap);
          194  +  va_end(ap);
          195  +  p->nUsed += (int)strlen(p->zBuf+p->nUsed);
          196  +}
          197  +
          198  +#ifdef SQLITE_DEBUG
          199  +/* Append the zero-terminated string zIn
          200  +*/
          201  +static void jsonAppend(JsonString *p, const char *zIn){
          202  +  jsonAppendRaw(p, zIn, (u32)strlen(zIn));
          203  +}
          204  +#endif
          205  +
          206  +/* Append a single character
          207  +*/
          208  +static void jsonAppendChar(JsonString *p, char c){
          209  +  if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
          210  +  p->zBuf[p->nUsed++] = c;
          211  +}
          212  +
          213  +/* Append a comma separator to the output buffer, if the previous
          214  +** character is not '[' or '{'.
          215  +*/
          216  +static void jsonAppendSeparator(JsonString *p){
          217  +  char c;
          218  +  if( p->nUsed==0 ) return;
          219  +  c = p->zBuf[p->nUsed-1];
          220  +  if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
          221  +}
          222  +
          223  +/* Append the N-byte string in zIn to the end of the JsonString string
          224  +** under construction.  Enclose the string in "..." and escape
          225  +** any double-quotes or backslash characters contained within the
          226  +** string.
          227  +*/
          228  +static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
          229  +  u32 i;
          230  +  if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
          231  +  p->zBuf[p->nUsed++] = '"';
          232  +  for(i=0; i<N; i++){
          233  +    char c = zIn[i];
          234  +    if( c=='"' || c=='\\' ){
          235  +      if( (p->nUsed+N+1-i > p->nAlloc) && jsonGrow(p,N+1-i)!=0 ) return;
          236  +      p->zBuf[p->nUsed++] = '\\';
          237  +    }
          238  +    p->zBuf[p->nUsed++] = c;
          239  +  }
          240  +  p->zBuf[p->nUsed++] = '"';
          241  +}
          242  +
          243  +/*
          244  +** Append a function parameter value to the JSON string under 
          245  +** construction.
          246  +*/
          247  +static void jsonAppendValue(
          248  +  JsonString *p,                 /* Append to this JSON string */
          249  +  sqlite3_value *pValue          /* Value to append */
          250  +){
          251  +  switch( sqlite3_value_type(pValue) ){
          252  +    case SQLITE_NULL: {
          253  +      jsonAppendRaw(p, "null", 4);
          254  +      break;
          255  +    }
          256  +    case SQLITE_INTEGER:
          257  +    case SQLITE_FLOAT: {
          258  +      const char *z = (const char*)sqlite3_value_text(pValue);
          259  +      u32 n = (u32)sqlite3_value_bytes(pValue);
          260  +      jsonAppendRaw(p, z, n);
          261  +      break;
          262  +    }
          263  +    case SQLITE_TEXT: {
          264  +      const char *z = (const char*)sqlite3_value_text(pValue);
          265  +      u32 n = (u32)sqlite3_value_bytes(pValue);
          266  +      jsonAppendString(p, z, n);
          267  +      break;
          268  +    }
          269  +    default: {
          270  +      if( p->bErr==0 ){
          271  +        sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
          272  +        p->bErr = 1;
          273  +        jsonReset(p);
          274  +      }
          275  +      break;
          276  +    }
          277  +  }
          278  +}
          279  +
          280  +
          281  +/* Make the JSON in p the result of the SQL function.
          282  +*/
          283  +static void jsonResult(JsonString *p){
          284  +  if( p->bErr==0 ){
          285  +    sqlite3_result_text64(p->pCtx, p->zBuf, p->nUsed, 
          286  +                          p->bStatic ? SQLITE_TRANSIENT : sqlite3_free,
          287  +                          SQLITE_UTF8);
          288  +    jsonZero(p);
          289  +  }
          290  +  assert( p->bStatic );
          291  +}
          292  +
          293  +/**************************************************************************
          294  +** Utility routines for dealing with JsonNode and JsonParse objects
          295  +**************************************************************************/
          296  +
          297  +/*
          298  +** Return the number of consecutive JsonNode slots need to represent
          299  +** the parsed JSON at pNode.  The minimum answer is 1.  For ARRAY and
          300  +** OBJECT types, the number might be larger.
          301  +**
          302  +** Appended elements are not counted.  The value returned is the number
          303  +** by which the JsonNode counter should increment in order to go to the
          304  +** next peer value.
          305  +*/
          306  +static u32 jsonNodeSize(JsonNode *pNode){
          307  +  return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
          308  +}
          309  +
          310  +/*
          311  +** Reclaim all memory allocated by a JsonParse object.  But do not
          312  +** delete the JsonParse object itself.
          313  +*/
          314  +static void jsonParseReset(JsonParse *pParse){
          315  +  sqlite3_free(pParse->aNode);
          316  +  pParse->aNode = 0;
          317  +  pParse->nNode = 0;
          318  +  pParse->nAlloc = 0;
          319  +  sqlite3_free(pParse->aUp);
          320  +  pParse->aUp = 0;
          321  +}
          322  +
          323  +/*
          324  +** Convert the JsonNode pNode into a pure JSON string and
          325  +** append to pOut.  Subsubstructure is also included.  Return
          326  +** the number of JsonNode objects that are encoded.
          327  +*/
          328  +static void jsonRenderNode(
          329  +  JsonNode *pNode,               /* The node to render */
          330  +  JsonString *pOut,              /* Write JSON here */
          331  +  sqlite3_value **aReplace       /* Replacement values */
          332  +){
          333  +  switch( pNode->eType ){
          334  +    case JSON_NULL: {
          335  +      jsonAppendRaw(pOut, "null", 4);
          336  +      break;
          337  +    }
          338  +    case JSON_TRUE: {
          339  +      jsonAppendRaw(pOut, "true", 4);
          340  +      break;
          341  +    }
          342  +    case JSON_FALSE: {
          343  +      jsonAppendRaw(pOut, "false", 5);
          344  +      break;
          345  +    }
          346  +    case JSON_STRING: {
          347  +      if( pNode->jnFlags & JNODE_RAW ){
          348  +        jsonAppendString(pOut, pNode->u.zJContent, pNode->n);
          349  +        break;
          350  +      }
          351  +      /* Fall through into the next case */
          352  +    }
          353  +    case JSON_REAL:
          354  +    case JSON_INT: {
          355  +      jsonAppendRaw(pOut, pNode->u.zJContent, pNode->n);
          356  +      break;
          357  +    }
          358  +    case JSON_ARRAY: {
          359  +      u32 j = 1;
          360  +      jsonAppendChar(pOut, '[');
          361  +      for(;;){
          362  +        while( j<=pNode->n ){
          363  +          if( pNode[j].jnFlags & (JNODE_REMOVE|JNODE_REPLACE) ){
          364  +            if( pNode[j].jnFlags & JNODE_REPLACE ){
          365  +              jsonAppendSeparator(pOut);
          366  +              jsonAppendValue(pOut, aReplace[pNode[j].iVal]);
          367  +            }
          368  +          }else{
          369  +            jsonAppendSeparator(pOut);
          370  +            jsonRenderNode(&pNode[j], pOut, aReplace);
          371  +          }
          372  +          j += jsonNodeSize(&pNode[j]);
          373  +        }
          374  +        if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
          375  +        pNode = &pNode[pNode->u.iAppend];
          376  +        j = 1;
          377  +      }
          378  +      jsonAppendChar(pOut, ']');
          379  +      break;
          380  +    }
          381  +    case JSON_OBJECT: {
          382  +      u32 j = 1;
          383  +      jsonAppendChar(pOut, '{');
          384  +      for(;;){
          385  +        while( j<=pNode->n ){
          386  +          if( (pNode[j+1].jnFlags & JNODE_REMOVE)==0 ){
          387  +            jsonAppendSeparator(pOut);
          388  +            jsonRenderNode(&pNode[j], pOut, aReplace);
          389  +            jsonAppendChar(pOut, ':');
          390  +            if( pNode[j+1].jnFlags & JNODE_REPLACE ){
          391  +              jsonAppendValue(pOut, aReplace[pNode[j+1].iVal]);
          392  +            }else{
          393  +              jsonRenderNode(&pNode[j+1], pOut, aReplace);
          394  +            }
          395  +          }
          396  +          j += 1 + jsonNodeSize(&pNode[j+1]);
          397  +        }
          398  +        if( (pNode->jnFlags & JNODE_APPEND)==0 ) break;
          399  +        pNode = &pNode[pNode->u.iAppend];
          400  +        j = 1;
          401  +      }
          402  +      jsonAppendChar(pOut, '}');
          403  +      break;
          404  +    }
          405  +  }
          406  +}
          407  +
          408  +/*
          409  +** Make the JsonNode the return value of the function.
          410  +*/
          411  +static void jsonReturn(
          412  +  JsonNode *pNode,            /* Node to return */
          413  +  sqlite3_context *pCtx,      /* Return value for this function */
          414  +  sqlite3_value **aReplace    /* Array of replacement values */
          415  +){
          416  +  switch( pNode->eType ){
          417  +    case JSON_NULL: {
          418  +      sqlite3_result_null(pCtx);
          419  +      break;
          420  +    }
          421  +    case JSON_TRUE: {
          422  +      sqlite3_result_int(pCtx, 1);
          423  +      break;
          424  +    }
          425  +    case JSON_FALSE: {
          426  +      sqlite3_result_int(pCtx, 0);
          427  +      break;
          428  +    }
          429  +    case JSON_REAL: {
          430  +      double r = strtod(pNode->u.zJContent, 0);
          431  +      sqlite3_result_double(pCtx, r);
          432  +      break;
          433  +    }
          434  +    case JSON_INT: {
          435  +      sqlite3_int64 i = 0;
          436  +      const char *z = pNode->u.zJContent;
          437  +      if( z[0]=='-' ){ z++; }
          438  +      while( z[0]>='0' && z[0]<='9' ){ i = i*10 + *(z++) - '0'; }
          439  +      if( pNode->u.zJContent[0]=='-' ){ i = -i; }
          440  +      sqlite3_result_int64(pCtx, i);
          441  +      break;
          442  +    }
          443  +    case JSON_STRING: {
          444  +      if( pNode->jnFlags & JNODE_RAW ){
          445  +        sqlite3_result_text(pCtx, pNode->u.zJContent, pNode->n,
          446  +                            SQLITE_TRANSIENT);
          447  +      }else if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
          448  +        /* JSON formatted without any backslash-escapes */
          449  +        sqlite3_result_text(pCtx, pNode->u.zJContent+1, pNode->n-2,
          450  +                            SQLITE_TRANSIENT);
          451  +      }else{
          452  +        /* Translate JSON formatted string into raw text */
          453  +        u32 i;
          454  +        u32 n = pNode->n;
          455  +        const char *z = pNode->u.zJContent;
          456  +        char *zOut;
          457  +        u32 j;
          458  +        zOut = sqlite3_malloc( n+1 );
          459  +        if( zOut==0 ){
          460  +          sqlite3_result_error_nomem(pCtx);
          461  +          break;
          462  +        }
          463  +        for(i=1, j=0; i<n-1; i++){
          464  +          char c = z[i];
          465  +          if( c!='\\' && z[i+1] ){
          466  +            zOut[j++] = c;
          467  +          }else{
          468  +            c = z[++i];
          469  +            if( c=='u' && z[1] ){
          470  +              u32 v = 0, k;
          471  +              z++;
          472  +              for(k=0; k<4 && z[k]; k++){
          473  +                c = z[0];
          474  +                if( c>='0' && c<='9' ) v = v*16 + c - '0';
          475  +                else if( c>='A' && c<='F' ) v = v*16 + c - 'A' + 10;
          476  +                else if( c>='a' && c<='f' ) v = v*16 + c - 'a' + 10;
          477  +                else break;
          478  +                z++;
          479  +              }
          480  +              if( v<=0x7f ){
          481  +                zOut[j++] = v;
          482  +              }else if( v<=0x7ff ){
          483  +                zOut[j++] = 0xc0 | (v>>6);
          484  +                zOut[j++] = 0x80 | (v&0x3f);
          485  +              }else if( v<=0xffff ){
          486  +                zOut[j++] = 0xe0 | (v>>12);
          487  +                zOut[j++] = 0x80 | ((v>>6)&0x3f);
          488  +                zOut[j++] = 0x80 | (v&0x3f);
          489  +              }else if( v<=0x10ffff ){
          490  +                zOut[j++] = 0xf0 | (v>>18);
          491  +                zOut[j++] = 0x80 | ((v>>12)&0x3f);
          492  +                zOut[j++] = 0x80 | ((v>>6)&0x3f);
          493  +                zOut[j++] = 0x80 | (v&0x3f);
          494  +              }
          495  +            }else{
          496  +              if( c=='b' ){
          497  +                c = '\b';
          498  +              }else if( c=='f' ){
          499  +                c = '\f';
          500  +              }else if( c=='n' ){
          501  +                c = '\n';
          502  +              }else if( c=='r' ){
          503  +                c = '\r';
          504  +              }else if( c=='t' ){
          505  +                c = '\t';
          506  +              }
          507  +              zOut[j++] = c;
          508  +            }
          509  +          }
          510  +        }
          511  +        zOut[j] = 0;
          512  +        sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
          513  +      }
          514  +      break;
          515  +    }
          516  +    case JSON_ARRAY:
          517  +    case JSON_OBJECT: {
          518  +      JsonString s;
          519  +      jsonInit(&s, pCtx);
          520  +      jsonRenderNode(pNode, &s, aReplace);
          521  +      jsonResult(&s);
          522  +      break;
          523  +    }
          524  +  }
          525  +}
          526  +
          527  +/*
          528  +** Create a new JsonNode instance based on the arguments and append that
          529  +** instance to the JsonParse.  Return the index in pParse->aNode[] of the
          530  +** new node, or -1 if a memory allocation fails.
          531  +*/
          532  +static int jsonParseAddNode(
          533  +  JsonParse *pParse,        /* Append the node to this object */
          534  +  u32 eType,                /* Node type */
          535  +  u32 n,                    /* Content size or sub-node count */
          536  +  const char *zContent      /* Content */
          537  +){
          538  +  JsonNode *p;
          539  +  if( pParse->nNode>=pParse->nAlloc ){
          540  +    u32 nNew;
          541  +    JsonNode *pNew;
          542  +    if( pParse->oom ) return -1;
          543  +    nNew = pParse->nAlloc*2 + 10;
          544  +    if( nNew<=pParse->nNode ){
          545  +      pParse->oom = 1;
          546  +      return -1;
          547  +    }
          548  +    pNew = sqlite3_realloc(pParse->aNode, sizeof(JsonNode)*nNew);
          549  +    if( pNew==0 ){
          550  +      pParse->oom = 1;
          551  +      return -1;
          552  +    }
          553  +    pParse->nAlloc = nNew;
          554  +    pParse->aNode = pNew;
          555  +  }
          556  +  p = &pParse->aNode[pParse->nNode];
          557  +  p->eType = (u8)eType;
          558  +  p->jnFlags = 0;
          559  +  p->iVal = 0;
          560  +  p->n = n;
          561  +  p->u.zJContent = zContent;
          562  +  return pParse->nNode++;
          563  +}
          564  +
          565  +/*
          566  +** Parse a single JSON value which begins at pParse->zJson[i].  Return the
          567  +** index of the first character past the end of the value parsed.
          568  +**
          569  +** Return negative for a syntax error.  Special cases:  return -2 if the
          570  +** first non-whitespace character is '}' and return -3 if the first
          571  +** non-whitespace character is ']'.
          572  +*/
          573  +static int jsonParseValue(JsonParse *pParse, u32 i){
          574  +  char c;
          575  +  u32 j;
          576  +  u32 iThis;
          577  +  int x;
          578  +  while( isspace(pParse->zJson[i]) ){ i++; }
          579  +  if( (c = pParse->zJson[i])==0 ) return 0;
          580  +  if( c=='{' ){
          581  +    /* Parse object */
          582  +    iThis = jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
          583  +    for(j=i+1;;j++){
          584  +      while( isspace(pParse->zJson[j]) ){ j++; }
          585  +      x = jsonParseValue(pParse, j);
          586  +      if( x<0 ){
          587  +        if( x==(-2) && pParse->nNode==iThis+1 ) return j+1;
          588  +        return -1;
          589  +      }
          590  +      if( pParse->aNode[pParse->nNode-1].eType!=JSON_STRING ) return -1;
          591  +      j = x;
          592  +      while( isspace(pParse->zJson[j]) ){ j++; }
          593  +      if( pParse->zJson[j]!=':' ) return -1;
          594  +      j++;
          595  +      x = jsonParseValue(pParse, j);
          596  +      if( x<0 ) return -1;
          597  +      j = x;
          598  +      while( isspace(pParse->zJson[j]) ){ j++; }
          599  +      c = pParse->zJson[j];
          600  +      if( c==',' ) continue;
          601  +      if( c!='}' ) return -1;
          602  +      break;
          603  +    }
          604  +    pParse->aNode[iThis].n = pParse->nNode - iThis - 1;
          605  +    return j+1;
          606  +  }else if( c=='[' ){
          607  +    /* Parse array */
          608  +    iThis = jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
          609  +    for(j=i+1;;j++){
          610  +      while( isspace(pParse->zJson[j]) ){ j++; }
          611  +      x = jsonParseValue(pParse, j);
          612  +      if( x<0 ){
          613  +        if( x==(-3) && pParse->nNode==iThis+1 ) return j+1;
          614  +        return -1;
          615  +      }
          616  +      j = x;
          617  +      while( isspace(pParse->zJson[j]) ){ j++; }
          618  +      c = pParse->zJson[j];
          619  +      if( c==',' ) continue;
          620  +      if( c!=']' ) return -1;
          621  +      break;
          622  +    }
          623  +    pParse->aNode[iThis].n = pParse->nNode - iThis - 1;
          624  +    return j+1;
          625  +  }else if( c=='"' ){
          626  +    /* Parse string */
          627  +    u8 jnFlags = 0;
          628  +    j = i+1;
          629  +    for(;;){
          630  +      c = pParse->zJson[j];
          631  +      if( c==0 ) return -1;
          632  +      if( c=='\\' ){
          633  +        c = pParse->zJson[++j];
          634  +        if( c==0 ) return -1;
          635  +        jnFlags = JNODE_ESCAPE;
          636  +      }else if( c=='"' ){
          637  +        break;
          638  +      }
          639  +      j++;
          640  +    }
          641  +    jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]);
          642  +    pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
          643  +    return j+1;
          644  +  }else if( c=='n'
          645  +         && strncmp(pParse->zJson+i,"null",4)==0
          646  +         && !isalnum(pParse->zJson[i+4]) ){
          647  +    jsonParseAddNode(pParse, JSON_NULL, 0, 0);
          648  +    return i+4;
          649  +  }else if( c=='t'
          650  +         && strncmp(pParse->zJson+i,"true",4)==0
          651  +         && !isalnum(pParse->zJson[i+4]) ){
          652  +    jsonParseAddNode(pParse, JSON_TRUE, 0, 0);
          653  +    return i+4;
          654  +  }else if( c=='f'
          655  +         && strncmp(pParse->zJson+i,"false",5)==0
          656  +         && !isalnum(pParse->zJson[i+5]) ){
          657  +    jsonParseAddNode(pParse, JSON_FALSE, 0, 0);
          658  +    return i+5;
          659  +  }else if( c=='-' || (c>='0' && c<='9') ){
          660  +    /* Parse number */
          661  +    u8 seenDP = 0;
          662  +    u8 seenE = 0;
          663  +    j = i+1;
          664  +    for(;; j++){
          665  +      c = pParse->zJson[j];
          666  +      if( c>='0' && c<='9' ) continue;
          667  +      if( c=='.' ){
          668  +        if( pParse->zJson[j-1]=='-' ) return -1;
          669  +        if( seenDP ) return -1;
          670  +        seenDP = 1;
          671  +        continue;
          672  +      }
          673  +      if( c=='e' || c=='E' ){
          674  +        if( pParse->zJson[j-1]<'0' ) return -1;
          675  +        if( seenE ) return -1;
          676  +        seenDP = seenE = 1;
          677  +        c = pParse->zJson[j+1];
          678  +        if( c=='+' || c=='-' ) j++;
          679  +        continue;
          680  +      }
          681  +      break;
          682  +    }
          683  +    if( pParse->zJson[j-1]<'0' ) return -1;
          684  +    jsonParseAddNode(pParse, seenDP ? JSON_REAL : JSON_INT,
          685  +                        j - i, &pParse->zJson[i]);
          686  +    return j;
          687  +  }else if( c=='}' ){
          688  +    return -2;  /* End of {...} */
          689  +  }else if( c==']' ){
          690  +    return -3;  /* End of [...] */
          691  +  }else{
          692  +    return -1;  /* Syntax error */
          693  +  }
          694  +}
          695  +
          696  +/*
          697  +** Parse a complete JSON string.  Return 0 on success or non-zero if there
          698  +** are any errors.  If an error occurs, free all memory associated with
          699  +** pParse.
          700  +**
          701  +** pParse is uninitialized when this routine is called.
          702  +*/
          703  +static int jsonParse(JsonParse *pParse, const char *zJson){
          704  +  int i;
          705  +  if( zJson==0 ) return 1;
          706  +  memset(pParse, 0, sizeof(*pParse));
          707  +  pParse->zJson = zJson;
          708  +  i = jsonParseValue(pParse, 0);
          709  +  if( i>0 ){
          710  +    while( isspace(zJson[i]) ) i++;
          711  +    if( zJson[i] ) i = -1;
          712  +  }
          713  +  if( i<0 ){
          714  +    jsonParseReset(pParse);
          715  +    return 1;
          716  +  }
          717  +  return 0;
          718  +}
          719  +
          720  +/* Mark node i of pParse as being a child of iParent.  Call recursively
          721  +** to fill in all the descendants of node i.
          722  +*/
          723  +static void jsonParseFillInParentage(JsonParse *pParse, u32 i, u32 iParent){
          724  +  JsonNode *pNode = &pParse->aNode[i];
          725  +  u32 j;
          726  +  pParse->aUp[i] = iParent;
          727  +  switch( pNode->eType ){
          728  +    case JSON_ARRAY: {
          729  +      for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j)){
          730  +        jsonParseFillInParentage(pParse, i+j, i);
          731  +      }
          732  +      break;
          733  +    }
          734  +    case JSON_OBJECT: {
          735  +      for(j=1; j<=pNode->n; j += jsonNodeSize(pNode+j+1)+1){
          736  +        pParse->aUp[i+j] = i;
          737  +        jsonParseFillInParentage(pParse, i+j+1, i);
          738  +      }
          739  +      break;
          740  +    }
          741  +    default: {
          742  +      break;
          743  +    }
          744  +  }
          745  +}
          746  +
          747  +/*
          748  +** Compute the parentage of all nodes in a completed parse.
          749  +*/
          750  +static int jsonParseFindParents(JsonParse *pParse){
          751  +  u32 *aUp;
          752  +  assert( pParse->aUp==0 );
          753  +  aUp = pParse->aUp = sqlite3_malloc( sizeof(u32)*pParse->nNode );
          754  +  if( aUp==0 ) return SQLITE_NOMEM;
          755  +  jsonParseFillInParentage(pParse, 0, 0);
          756  +  return SQLITE_OK;
          757  +}
          758  +
          759  +/* forward declaration */
          760  +static JsonNode *jsonLookupAppend(JsonParse*,const char*,int*);
          761  +
          762  +/*
          763  +** Search along zPath to find the node specified.  Return a pointer
          764  +** to that node, or NULL if zPath is malformed or if there is no such
          765  +** node.
          766  +**
          767  +** If pApnd!=0, then try to append new nodes to complete zPath if it is
          768  +** possible to do so and if no existing node corresponds to zPath.  If
          769  +** new nodes are appended *pApnd is set to 1.
          770  +*/
          771  +static JsonNode *jsonLookup(
          772  +  JsonParse *pParse,      /* The JSON to search */
          773  +  u32 iRoot,              /* Begin the search at this node */
          774  +  const char *zPath,      /* The path to search */
          775  +  int *pApnd              /* Append nodes to complete path if not NULL */
          776  +){
          777  +  u32 i, j, k, nKey;
          778  +  const char *zKey;
          779  +  JsonNode *pRoot = &pParse->aNode[iRoot];
          780  +  if( zPath[0]==0 ) return pRoot;
          781  +  if( zPath[0]=='.' ){
          782  +    if( pRoot->eType!=JSON_OBJECT ) return 0;
          783  +    zPath++;
          784  +    if( zPath[0]=='"' ){
          785  +      zKey = zPath + 1;
          786  +      for(i=1; zPath[i] && zPath[i]!='"'; i++){}
          787  +      nKey = i-1;
          788  +      if( zPath[i] ) i++;
          789  +    }else{
          790  +      zKey = zPath;
          791  +      for(i=0; zPath[i] && zPath[i]!='.' && zPath[i]!='['; i++){}
          792  +      nKey = i;
          793  +    }
          794  +    if( nKey==0 ) return 0;
          795  +    j = 1;
          796  +    for(;;){
          797  +      while( j<=pRoot->n ){
          798  +        if( pRoot[j].n==nKey+2
          799  +         && strncmp(&pRoot[j].u.zJContent[1],zKey,nKey)==0
          800  +        ){
          801  +          return jsonLookup(pParse, iRoot+j+1, &zPath[i], pApnd);
          802  +        }
          803  +        j++;
          804  +        j += jsonNodeSize(&pRoot[j]);
          805  +      }
          806  +      if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
          807  +      iRoot += pRoot->u.iAppend;
          808  +      pRoot = &pParse->aNode[iRoot];
          809  +      j = 1;
          810  +    }
          811  +    if( pApnd ){
          812  +      k = jsonParseAddNode(pParse, JSON_OBJECT, 2, 0);
          813  +      pRoot->u.iAppend = k - iRoot;
          814  +      pRoot->jnFlags |= JNODE_APPEND;
          815  +      k = jsonParseAddNode(pParse, JSON_STRING, i, zPath);
          816  +      if( !pParse->oom ) pParse->aNode[k].jnFlags |= JNODE_RAW;
          817  +      zPath += i;
          818  +      return jsonLookupAppend(pParse, zPath, pApnd);
          819  +    }
          820  +  }else if( zPath[0]=='[' && isdigit(zPath[1]) ){
          821  +    if( pRoot->eType!=JSON_ARRAY ) return 0;
          822  +    i = 0;
          823  +    zPath++;
          824  +    while( isdigit(zPath[0]) ){
          825  +      i = i + zPath[0] - '0';
          826  +      zPath++;
          827  +    }
          828  +    if( zPath[0]!=']' ) return 0;
          829  +    zPath++;
          830  +    j = 1;
          831  +    for(;;){
          832  +      while( i>0 && j<=pRoot->n ){
          833  +        j += jsonNodeSize(&pRoot[j]);
          834  +        i--;
          835  +      }
          836  +      if( (pRoot->jnFlags & JNODE_APPEND)==0 ) break;
          837  +      iRoot += pRoot->u.iAppend;
          838  +      pRoot = &pParse->aNode[iRoot];
          839  +      j = 1;
          840  +    }
          841  +    if( j<=pRoot->n ){
          842  +      return jsonLookup(pParse, iRoot+j, zPath, pApnd);
          843  +    }
          844  +    if( i==0 && pApnd ){
          845  +      k = jsonParseAddNode(pParse, JSON_ARRAY, 1, 0);
          846  +      pRoot->u.iAppend = k - iRoot;
          847  +      pRoot->jnFlags |= JNODE_APPEND;
          848  +      return jsonLookupAppend(pParse, zPath, pApnd);
          849  +    }
          850  +  }
          851  +  return 0;
          852  +}
          853  +
          854  +/*
          855  +** Append content to pParse that will complete zPath.
          856  +*/
          857  +static JsonNode *jsonLookupAppend(
          858  +  JsonParse *pParse,     /* Append content to the JSON parse */
          859  +  const char *zPath,     /* Description of content to append */
          860  +  int *pApnd             /* Set this flag to 1 */
          861  +){
          862  +  *pApnd = 1;
          863  +  if( zPath[0]==0 ){
          864  +    jsonParseAddNode(pParse, JSON_NULL, 0, 0);
          865  +    return pParse->oom ? 0 : &pParse->aNode[pParse->nNode-1];
          866  +  }
          867  +  if( zPath[0]=='.' ){
          868  +    jsonParseAddNode(pParse, JSON_OBJECT, 0, 0);
          869  +  }else if( strncmp(zPath,"[0]",3)==0 ){
          870  +    jsonParseAddNode(pParse, JSON_ARRAY, 0, 0);
          871  +  }else{
          872  +    return 0;
          873  +  }
          874  +  if( pParse->oom ) return 0;
          875  +  return jsonLookup(pParse, pParse->nNode-1, zPath, pApnd);
          876  +}
          877  +
          878  +
          879  +/****************************************************************************
          880  +** SQL functions used for testing and debugging
          881  +****************************************************************************/
          882  +
          883  +#ifdef SQLITE_DEBUG
          884  +/*
          885  +** The json_parse(JSON) function returns a string which describes
          886  +** a parse of the JSON provided.  Or it returns NULL if JSON is not
          887  +** well-formed.
          888  +*/
          889  +static void jsonParseFunc(
          890  +  sqlite3_context *context,
          891  +  int argc,
          892  +  sqlite3_value **argv
          893  +){
          894  +  JsonString s;       /* Output string - not real JSON */
          895  +  JsonParse x;        /* The parse */
          896  +  u32 i;
          897  +  char zBuf[100];
          898  +
          899  +  assert( argc==1 );
          900  +  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
          901  +  jsonInit(&s, context);
          902  +  for(i=0; i<x.nNode; i++){
          903  +    sqlite3_snprintf(sizeof(zBuf), zBuf, "node %3u: %7s n=%d\n",
          904  +                     i, jsonType[x.aNode[i].eType], x.aNode[i].n);
          905  +    jsonAppend(&s, zBuf);
          906  +    if( x.aNode[i].u.zJContent!=0 ){
          907  +      jsonAppendRaw(&s, "    text: ", 10);
          908  +      jsonAppendRaw(&s, x.aNode[i].u.zJContent, x.aNode[i].n);
          909  +      jsonAppendRaw(&s, "\n", 1);
          910  +    }
          911  +  }
          912  +  jsonParseReset(&x);
          913  +  jsonResult(&s);
          914  +}
          915  +
          916  +/*
          917  +** The json_test1(JSON) function parses and rebuilds the JSON string.
          918  +*/
          919  +static void jsonTest1Func(
          920  +  sqlite3_context *context,
          921  +  int argc,
          922  +  sqlite3_value **argv
          923  +){
          924  +  JsonParse x;  /* The parse */
          925  +  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
          926  +  jsonReturn(x.aNode, context, 0);
          927  +  jsonParseReset(&x);
          928  +}
          929  +
          930  +/*
          931  +** The json_nodecount(JSON) function returns the number of nodes in the
          932  +** input JSON string.
          933  +*/
          934  +static void jsonNodeCountFunc(
          935  +  sqlite3_context *context,
          936  +  int argc,
          937  +  sqlite3_value **argv
          938  +){
          939  +  JsonParse x;  /* The parse */
          940  +  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
          941  +  sqlite3_result_int64(context, (sqlite3_int64)x.nNode);
          942  +  jsonParseReset(&x);
          943  +}
          944  +#endif /* SQLITE_DEBUG */
          945  +
          946  +/****************************************************************************
          947  +** SQL function implementations
          948  +****************************************************************************/
          949  +
          950  +/*
          951  +** Implementation of the json_array(VALUE,...) function.  Return a JSON
          952  +** array that contains all values given in arguments.  Or if any argument
          953  +** is a BLOB, throw an error.
          954  +*/
          955  +static void jsonArrayFunc(
          956  +  sqlite3_context *context,
          957  +  int argc,
          958  +  sqlite3_value **argv
          959  +){
          960  +  int i;
          961  +  JsonString jx;
          962  +
          963  +  jsonInit(&jx, context);
          964  +  jsonAppendChar(&jx, '[');
          965  +  for(i=0; i<argc; i++){
          966  +    jsonAppendSeparator(&jx);
          967  +    jsonAppendValue(&jx, argv[i]);
          968  +  }
          969  +  jsonAppendChar(&jx, ']');
          970  +  jsonResult(&jx);
          971  +}
          972  +
          973  +
          974  +/*
          975  +** json_array_length(JSON)
          976  +** json_array_length(JSON, PATH)
          977  +**
          978  +** Return the number of elements in the top-level JSON array.  
          979  +** Return 0 if the input is not a well-formed JSON array.
          980  +*/
          981  +static void jsonArrayLengthFunc(
          982  +  sqlite3_context *context,
          983  +  int argc,
          984  +  sqlite3_value **argv
          985  +){
          986  +  JsonParse x;          /* The parse */
          987  +  sqlite3_int64 n = 0;
          988  +  u32 i;
          989  +  const char *zPath;
          990  +
          991  +  if( argc==2 ){
          992  +    zPath = (const char*)sqlite3_value_text(argv[1]);
          993  +    if( zPath==0 ) return;
          994  +    if( zPath[0]!='$' ) return;
          995  +    zPath++;
          996  +  }else{
          997  +    zPath = 0;
          998  +  }
          999  +  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0]))==0 ){
         1000  +    if( x.nNode ){
         1001  +      JsonNode *pNode = x.aNode;
         1002  +      if( zPath ) pNode = jsonLookup(&x, 0, zPath, 0);
         1003  +      if( pNode->eType==JSON_ARRAY ){
         1004  +        assert( (pNode->jnFlags & JNODE_APPEND)==0 );
         1005  +        for(i=1; i<=pNode->n; n++){
         1006  +          i += jsonNodeSize(&pNode[i]);
         1007  +        }
         1008  +      }
         1009  +    }
         1010  +    jsonParseReset(&x);
         1011  +  }
         1012  +  sqlite3_result_int64(context, n);
         1013  +}
         1014  +
         1015  +/*
         1016  +** json_extract(JSON, PATH)
         1017  +**
         1018  +** Return the element described by PATH.  Return NULL if JSON is not
         1019  +** valid JSON or if there is no PATH element or if PATH is malformed.
         1020  +*/
         1021  +static void jsonExtractFunc(
         1022  +  sqlite3_context *context,
         1023  +  int argc,
         1024  +  sqlite3_value **argv
         1025  +){
         1026  +  JsonParse x;          /* The parse */
         1027  +  JsonNode *pNode;
         1028  +  const char *zPath;
         1029  +  assert( argc==2 );
         1030  +  zPath = (const char*)sqlite3_value_text(argv[1]);
         1031  +  if( zPath==0 ) return;
         1032  +  if( zPath[0]!='$' ) return;
         1033  +  zPath++;
         1034  +  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
         1035  +  pNode = jsonLookup(&x, 0, zPath, 0);
         1036  +  if( pNode ){
         1037  +    jsonReturn(pNode, context, 0);
         1038  +  }
         1039  +  jsonParseReset(&x);
         1040  +}
         1041  +
         1042  +/*
         1043  +** Implementation of the json_object(NAME,VALUE,...) function.  Return a JSON
         1044  +** object that contains all name/value given in arguments.  Or if any name
         1045  +** is not a string or if any value is a BLOB, throw an error.
         1046  +*/
         1047  +static void jsonObjectFunc(
         1048  +  sqlite3_context *context,
         1049  +  int argc,
         1050  +  sqlite3_value **argv
         1051  +){
         1052  +  int i;
         1053  +  JsonString jx;
         1054  +  const char *z;
         1055  +  u32 n;
         1056  +
         1057  +  if( argc&1 ){
         1058  +    sqlite3_result_error(context, "json_object() requires an even number "
         1059  +                                  "of arguments", -1);
         1060  +    return;
         1061  +  }
         1062  +  jsonInit(&jx, context);
         1063  +  jsonAppendChar(&jx, '{');
         1064  +  for(i=0; i<argc; i+=2){
         1065  +    if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
         1066  +      sqlite3_result_error(context, "json_object() labels must be TEXT", -1);
         1067  +      jsonZero(&jx);
         1068  +      return;
         1069  +    }
         1070  +    jsonAppendSeparator(&jx);
         1071  +    z = (const char*)sqlite3_value_text(argv[i]);
         1072  +    n = (u32)sqlite3_value_bytes(argv[i]);
         1073  +    jsonAppendString(&jx, z, n);
         1074  +    jsonAppendChar(&jx, ':');
         1075  +    jsonAppendValue(&jx, argv[i+1]);
         1076  +  }
         1077  +  jsonAppendChar(&jx, '}');
         1078  +  jsonResult(&jx);
         1079  +}
         1080  +
         1081  +
         1082  +/*
         1083  +** json_remove(JSON, PATH, ...)
         1084  +**
         1085  +** Remove the named elements from JSON and return the result.  Ill-formed
         1086  +** PATH arguments are silently ignored.  If JSON is ill-formed, then NULL
         1087  +** is returned.
         1088  +*/
         1089  +static void jsonRemoveFunc(
         1090  +  sqlite3_context *context,
         1091  +  int argc,
         1092  +  sqlite3_value **argv
         1093  +){
         1094  +  JsonParse x;          /* The parse */
         1095  +  JsonNode *pNode;
         1096  +  const char *zPath;
         1097  +  u32 i;
         1098  +
         1099  +  if( argc<1 ) return;
         1100  +  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
         1101  +  if( x.nNode ){
         1102  +    for(i=1; i<(u32)argc; i++){
         1103  +      zPath = (const char*)sqlite3_value_text(argv[i]);
         1104  +      if( zPath==0 ) continue;
         1105  +      if( zPath[0]!='$' ) continue;
         1106  +      pNode = jsonLookup(&x, 0, &zPath[1], 0);
         1107  +      if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
         1108  +    }
         1109  +    if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
         1110  +      jsonReturn(x.aNode, context, 0);
         1111  +    }
         1112  +  }
         1113  +  jsonParseReset(&x);
         1114  +}
         1115  +
         1116  +/*
         1117  +** json_replace(JSON, PATH, VALUE, ...)
         1118  +**
         1119  +** Replace the value at PATH with VALUE.  If PATH does not already exist,
         1120  +** this routine is a no-op.  If JSON is ill-formed, return NULL.
         1121  +*/
         1122  +static void jsonReplaceFunc(
         1123  +  sqlite3_context *context,
         1124  +  int argc,
         1125  +  sqlite3_value **argv
         1126  +){
         1127  +  JsonParse x;          /* The parse */
         1128  +  JsonNode *pNode;
         1129  +  const char *zPath;
         1130  +  u32 i;
         1131  +
         1132  +  if( argc<1 ) return;
         1133  +  if( (argc&1)==0 ) {
         1134  +    sqlite3_result_error(context,
         1135  +                         "json_replace() needs an odd number of arguments", -1);
         1136  +    return;
         1137  +  }
         1138  +  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
         1139  +  if( x.nNode ){
         1140  +    for(i=1; i<(u32)argc; i+=2){
         1141  +      zPath = (const char*)sqlite3_value_text(argv[i]);
         1142  +      if( zPath==0 ) continue;
         1143  +      if( zPath[0]!='$' ) continue;
         1144  +      pNode = jsonLookup(&x, 0, &zPath[1], 0);
         1145  +      if( pNode ){
         1146  +        pNode->jnFlags |= JNODE_REPLACE;
         1147  +        pNode->iVal = i+1;
         1148  +      }
         1149  +    }
         1150  +    if( x.aNode[0].jnFlags & JNODE_REPLACE ){
         1151  +      sqlite3_result_value(context, argv[x.aNode[0].iVal]);
         1152  +    }else{
         1153  +      jsonReturn(x.aNode, context, argv);
         1154  +    }
         1155  +  }
         1156  +  jsonParseReset(&x);
         1157  +}
         1158  +
         1159  +/*
         1160  +** json_set(JSON, PATH, VALUE, ...)
         1161  +**
         1162  +** Set the value at PATH to VALUE.  Create the PATH if it does not already
         1163  +** exist.  Overwrite existing values that do exist.
         1164  +** If JSON is ill-formed, return NULL.
         1165  +**
         1166  +** json_insert(JSON, PATH, VALUE, ...)
         1167  +**
         1168  +** Create PATH and initialize it to VALUE.  If PATH already exists, this
         1169  +** routine is a no-op.  If JSON is ill-formed, return NULL.
         1170  +*/
         1171  +static void jsonSetFunc(
         1172  +  sqlite3_context *context,
         1173  +  int argc,
         1174  +  sqlite3_value **argv
         1175  +){
         1176  +  JsonParse x;          /* The parse */
         1177  +  JsonNode *pNode;
         1178  +  const char *zPath;
         1179  +  u32 i;
         1180  +  int bApnd;
         1181  +  int bIsSet = *(int*)sqlite3_user_data(context);
         1182  +
         1183  +  if( argc<1 ) return;
         1184  +  if( (argc&1)==0 ) {
         1185  +    sqlite3_result_error(context,
         1186  +                         "json_set() needs an odd number of arguments", -1);
         1187  +    return;
         1188  +  }
         1189  +  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
         1190  +  if( x.nNode ){
         1191  +    for(i=1; i<(u32)argc; i+=2){
         1192  +      zPath = (const char*)sqlite3_value_text(argv[i]);
         1193  +      if( zPath==0 ) continue;
         1194  +      if( zPath[0]!='$' ) continue;
         1195  +      bApnd = 0;
         1196  +      pNode = jsonLookup(&x, 0, &zPath[1], &bApnd);
         1197  +      if( pNode && (bApnd || bIsSet) ){
         1198  +        pNode->jnFlags |= JNODE_REPLACE;
         1199  +        pNode->iVal = i+1;
         1200  +      }
         1201  +    }
         1202  +    if( x.aNode[0].jnFlags & JNODE_REPLACE ){
         1203  +      sqlite3_result_value(context, argv[x.aNode[0].iVal]);
         1204  +    }else{
         1205  +      jsonReturn(x.aNode, context, argv);
         1206  +    }
         1207  +  }
         1208  +  jsonParseReset(&x);
         1209  +}
         1210  +
         1211  +/*
         1212  +** json_type(JSON)
         1213  +** json_type(JSON, PATH)
         1214  +**
         1215  +** Return the top-level "type" of a JSON string.  Return NULL if the
         1216  +** input is not a well-formed JSON string.
         1217  +*/
         1218  +static void jsonTypeFunc(
         1219  +  sqlite3_context *context,
         1220  +  int argc,
         1221  +  sqlite3_value **argv
         1222  +){
         1223  +  JsonParse x;          /* The parse */
         1224  +  const char *zPath;
         1225  +
         1226  +  if( argc==2 ){
         1227  +    zPath = (const char*)sqlite3_value_text(argv[1]);
         1228  +    if( zPath==0 ) return;
         1229  +    if( zPath[0]!='$' ) return;
         1230  +    zPath++;
         1231  +  }else{
         1232  +    zPath = 0;
         1233  +  }
         1234  +  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
         1235  +  if( x.nNode ){
         1236  +    JsonNode *pNode = x.aNode;
         1237  +    if( zPath ) pNode = jsonLookup(&x, 0, zPath, 0);
         1238  +    sqlite3_result_text(context, jsonType[pNode->eType], -1, SQLITE_STATIC);
         1239  +  }
         1240  +  jsonParseReset(&x);
         1241  +}
         1242  +
         1243  +/****************************************************************************
         1244  +** The json_each virtual table
         1245  +****************************************************************************/
         1246  +typedef struct JsonEachCursor JsonEachCursor;
         1247  +struct JsonEachCursor {
         1248  +  sqlite3_vtab_cursor base;  /* Base class - must be first */
         1249  +  u32 iRowid;                /* The rowid */
         1250  +  u32 i;                     /* Index in sParse.aNode[] of current row */
         1251  +  u32 iEnd;                  /* EOF when i equals or exceeds this value */
         1252  +  u8 eType;                  /* Type of top-level element */
         1253  +  u8 bRecursive;             /* True for json_tree().  False for json_each() */
         1254  +  char *zJson;               /* Input JSON */
         1255  +  char *zPath;               /* Path by which to filter zJson */
         1256  +  JsonParse sParse;          /* Parse of the input JSON */
         1257  +};
         1258  +
         1259  +/* Constructor for the json_each virtual table */
         1260  +static int jsonEachConnect(
         1261  +  sqlite3 *db,
         1262  +  void *pAux,
         1263  +  int argc, const char *const*argv,
         1264  +  sqlite3_vtab **ppVtab,
         1265  +  char **pzErr
         1266  +){
         1267  +  sqlite3_vtab *pNew;
         1268  +  int rc;
         1269  +
         1270  +/* Column numbers */
         1271  +#define JEACH_KEY     0
         1272  +#define JEACH_VALUE   1
         1273  +#define JEACH_TYPE    2
         1274  +#define JEACH_ATOM    3
         1275  +#define JEACH_ID      4
         1276  +#define JEACH_PARENT  5
         1277  +#define JEACH_FULLKEY 6
         1278  +#define JEACH_JSON    7
         1279  +#define JEACH_PATH    8
         1280  +
         1281  +  UNUSED_PARAM(pzErr);
         1282  +  UNUSED_PARAM(argv);
         1283  +  UNUSED_PARAM(argc);
         1284  +  UNUSED_PARAM(pAux);
         1285  +  rc = sqlite3_declare_vtab(db, 
         1286  +     "CREATE TABLE x(key,value,type,atom,id,parent,fullkey,"
         1287  +                    "json HIDDEN,path HIDDEN)");
         1288  +  if( rc==SQLITE_OK ){
         1289  +    pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
         1290  +    if( pNew==0 ) return SQLITE_NOMEM;
         1291  +    memset(pNew, 0, sizeof(*pNew));
         1292  +  }
         1293  +  return rc;
         1294  +}
         1295  +
         1296  +/* destructor for json_each virtual table */
         1297  +static int jsonEachDisconnect(sqlite3_vtab *pVtab){
         1298  +  sqlite3_free(pVtab);
         1299  +  return SQLITE_OK;
         1300  +}
         1301  +
         1302  +/* constructor for a JsonEachCursor object for json_each(). */
         1303  +static int jsonEachOpenEach(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
         1304  +  JsonEachCursor *pCur;
         1305  +
         1306  +  UNUSED_PARAM(p);
         1307  +  pCur = sqlite3_malloc( sizeof(*pCur) );
         1308  +  if( pCur==0 ) return SQLITE_NOMEM;
         1309  +  memset(pCur, 0, sizeof(*pCur));
         1310  +  *ppCursor = &pCur->base;
         1311  +  return SQLITE_OK;
         1312  +}
         1313  +
         1314  +/* constructor for a JsonEachCursor object for json_tree(). */
         1315  +static int jsonEachOpenTree(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
         1316  +  int rc = jsonEachOpenEach(p, ppCursor);
         1317  +  if( rc==SQLITE_OK ){
         1318  +    JsonEachCursor *pCur = (JsonEachCursor*)*ppCursor;
         1319  +    pCur->bRecursive = 1;
         1320  +  }
         1321  +  return rc;
         1322  +}
         1323  +
         1324  +/* Reset a JsonEachCursor back to its original state.  Free any memory
         1325  +** held. */
         1326  +static void jsonEachCursorReset(JsonEachCursor *p){
         1327  +  sqlite3_free(p->zJson);
         1328  +  sqlite3_free(p->zPath);
         1329  +  jsonParseReset(&p->sParse);
         1330  +  p->iRowid = 0;
         1331  +  p->i = 0;
         1332  +  p->iEnd = 0;
         1333  +  p->eType = 0;
         1334  +  p->zJson = 0;
         1335  +  p->zPath = 0;
         1336  +}
         1337  +
         1338  +/* Destructor for a jsonEachCursor object */
         1339  +static int jsonEachClose(sqlite3_vtab_cursor *cur){
         1340  +  JsonEachCursor *p = (JsonEachCursor*)cur;
         1341  +  jsonEachCursorReset(p);
         1342  +  sqlite3_free(cur);
         1343  +  return SQLITE_OK;
         1344  +}
         1345  +
         1346  +/* Return TRUE if the jsonEachCursor object has been advanced off the end
         1347  +** of the JSON object */
         1348  +static int jsonEachEof(sqlite3_vtab_cursor *cur){
         1349  +  JsonEachCursor *p = (JsonEachCursor*)cur;
         1350  +  return p->i >= p->iEnd;
         1351  +}
         1352  +
         1353  +/* Advance the cursor to the next element for json_tree() */
         1354  +static int jsonEachNext(sqlite3_vtab_cursor *cur){
         1355  +  JsonEachCursor *p = (JsonEachCursor*)cur;
         1356  +  if( p->bRecursive ){
         1357  +    if( p->i==0 ){
         1358  +      p->i = 1;
         1359  +    }else if( p->sParse.aNode[p->sParse.aUp[p->i]].eType==JSON_OBJECT ){
         1360  +      p->i += 2;
         1361  +    }else{
         1362  +      p->i++;
         1363  +    }
         1364  +    p->iRowid++;
         1365  +    if( p->i<p->sParse.nNode ){
         1366  +      JsonNode *pUp = &p->sParse.aNode[p->sParse.aUp[p->i]];
         1367  +      p->eType = pUp->eType;
         1368  +      if( pUp->eType==JSON_ARRAY ) pUp->u.iKey++;
         1369  +      if( p->sParse.aNode[p->i].eType==JSON_ARRAY ){
         1370  +        p->sParse.aNode[p->i].u.iKey = 0;
         1371  +      }
         1372  +    }
         1373  +  }else{
         1374  +    switch( p->eType ){
         1375  +      case JSON_ARRAY: {
         1376  +        p->i += jsonNodeSize(&p->sParse.aNode[p->i]);
         1377  +        p->iRowid++;
         1378  +        break;
         1379  +      }
         1380  +      case JSON_OBJECT: {
         1381  +        p->i += 1 + jsonNodeSize(&p->sParse.aNode[p->i+1]);
         1382  +        p->iRowid++;
         1383  +        break;
         1384  +      }
         1385  +      default: {
         1386  +        p->i = p->iEnd;
         1387  +        break;
         1388  +      }
         1389  +    }
         1390  +  }
         1391  +  return SQLITE_OK;
         1392  +}
         1393  +
         1394  +/* Append the name of the path for element i to pStr
         1395  +*/
         1396  +static void jsonEachComputePath(
         1397  +  JsonEachCursor *p,       /* The cursor */
         1398  +  JsonString *pStr,        /* Write the path here */
         1399  +  u32 i                    /* Path to this element */
         1400  +){
         1401  +  JsonNode *pNode, *pUp;
         1402  +  u32 iUp;
         1403  +  if( i==0 ){
         1404  +    jsonAppendChar(pStr, '$');
         1405  +    return;
         1406  +  }
         1407  +  iUp = p->sParse.aUp[i];
         1408  +  jsonEachComputePath(p, pStr, iUp);
         1409  +  pNode = &p->sParse.aNode[i];
         1410  +  pUp = &p->sParse.aNode[iUp];
         1411  +  if( pUp->eType==JSON_ARRAY ){
         1412  +    jsonPrintf(30, pStr, "[%d]", pUp->u.iKey);
         1413  +  }else{
         1414  +    assert( pUp->eType==JSON_OBJECT );
         1415  +    if( pNode->eType>=JSON_ARRAY ) pNode--;
         1416  +    assert( pNode->eType==JSON_STRING );
         1417  +    jsonPrintf(pNode->n+1, pStr, ".%.*s", pNode->n-2, pNode->u.zJContent+1);
         1418  +  }
         1419  +}
         1420  +
         1421  +/* Return the value of a column */
         1422  +static int jsonEachColumn(
         1423  +  sqlite3_vtab_cursor *cur,   /* The cursor */
         1424  +  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
         1425  +  int i                       /* Which column to return */
         1426  +){
         1427  +  JsonEachCursor *p = (JsonEachCursor*)cur;
         1428  +  JsonNode *pThis = &p->sParse.aNode[p->i];
         1429  +  switch( i ){
         1430  +    case JEACH_KEY: {
         1431  +      if( p->eType==JSON_OBJECT ){
         1432  +        jsonReturn(pThis, ctx, 0);
         1433  +      }else if( p->eType==JSON_ARRAY ){
         1434  +        u32 iKey;
         1435  +        if( p->bRecursive ){
         1436  +          if( p->iRowid==0 ) break;
         1437  +          iKey = p->sParse.aNode[p->sParse.aUp[p->i]].u.iKey - 1;
         1438  +        }else{
         1439  +          iKey = p->iRowid;
         1440  +        }
         1441  +        sqlite3_result_int64(ctx, (sqlite3_int64)iKey);
         1442  +      }
         1443  +      break;
         1444  +    }
         1445  +    case JEACH_VALUE: {
         1446  +      if( p->eType==JSON_OBJECT ) pThis++;
         1447  +      jsonReturn(pThis, ctx, 0);
         1448  +      break;
         1449  +    }
         1450  +    case JEACH_TYPE: {
         1451  +      if( p->eType==JSON_OBJECT ) pThis++;
         1452  +      sqlite3_result_text(ctx, jsonType[pThis->eType], -1, SQLITE_STATIC);
         1453  +      break;
         1454  +    }
         1455  +    case JEACH_ATOM: {
         1456  +      if( p->eType==JSON_OBJECT ) pThis++;
         1457  +      if( pThis->eType>=JSON_ARRAY ) break;
         1458  +      jsonReturn(pThis, ctx, 0);
         1459  +      break;
         1460  +    }
         1461  +    case JEACH_ID: {
         1462  +      sqlite3_result_int64(ctx, (sqlite3_int64)p->i + (p->eType==JSON_OBJECT));
         1463  +      break;
         1464  +    }
         1465  +    case JEACH_PARENT: {
         1466  +      if( p->i>0 && p->bRecursive ){
         1467  +        sqlite3_result_int64(ctx, (sqlite3_int64)p->sParse.aUp[p->i]);
         1468  +      }
         1469  +      break;
         1470  +    }
         1471  +    case JEACH_FULLKEY: {
         1472  +      JsonString x;
         1473  +      jsonInit(&x, ctx);
         1474  +      if( p->bRecursive ){
         1475  +        jsonEachComputePath(p, &x, p->i);
         1476  +      }else{
         1477  +        if( p->zPath ){
         1478  +          jsonAppendRaw(&x, p->zPath, (int)strlen(p->zPath));
         1479  +        }else{
         1480  +          jsonAppendChar(&x, '$');
         1481  +        }
         1482  +        if( p->eType==JSON_ARRAY ){
         1483  +          jsonPrintf(30, &x, "[%d]", p->iRowid);
         1484  +        }else{
         1485  +          jsonPrintf(pThis->n, &x, ".%.*s", pThis->n-2, pThis->u.zJContent+1);
         1486  +        }
         1487  +      }
         1488  +      jsonResult(&x);
         1489  +      break;
         1490  +    }
         1491  +    case JEACH_PATH: {
         1492  +      const char *zPath = p->zPath;
         1493  +       if( zPath==0 ){
         1494  +        if( p->bRecursive ){
         1495  +          JsonString x;
         1496  +          jsonInit(&x, ctx);
         1497  +          jsonEachComputePath(p, &x, p->sParse.aUp[p->i]);
         1498  +          jsonResult(&x);
         1499  +          break;
         1500  +        }
         1501  +        zPath = "$";
         1502  +      }
         1503  +      sqlite3_result_text(ctx, zPath, -1, SQLITE_STATIC);
         1504  +      break;
         1505  +    }
         1506  +    default: {
         1507  +      assert( i==JEACH_JSON );
         1508  +      sqlite3_result_text(ctx, p->sParse.zJson, -1, SQLITE_STATIC);
         1509  +      break;
         1510  +    }
         1511  +  }
         1512  +  return SQLITE_OK;
         1513  +}
         1514  +
         1515  +/* Return the current rowid value */
         1516  +static int jsonEachRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
         1517  +  JsonEachCursor *p = (JsonEachCursor*)cur;
         1518  +  *pRowid = p->iRowid;
         1519  +  return SQLITE_OK;
         1520  +}
         1521  +
         1522  +/* The query strategy is to look for an equality constraint on the json
         1523  +** column.  Without such a constraint, the table cannot operate.  idxNum is
         1524  +** 1 if the constraint is found, 3 if the constraint and zPath are found,
         1525  +** and 0 otherwise.
         1526  +*/
         1527  +static int jsonEachBestIndex(
         1528  +  sqlite3_vtab *tab,
         1529  +  sqlite3_index_info *pIdxInfo
         1530  +){
         1531  +  int i;
         1532  +  int jsonIdx = -1;
         1533  +  int pathIdx = -1;
         1534  +  const struct sqlite3_index_constraint *pConstraint;
         1535  +
         1536  +  UNUSED_PARAM(tab);
         1537  +  pConstraint = pIdxInfo->aConstraint;
         1538  +  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
         1539  +    if( pConstraint->usable==0 ) continue;
         1540  +    if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
         1541  +    switch( pConstraint->iColumn ){
         1542  +      case JEACH_JSON:   jsonIdx = i;    break;
         1543  +      case JEACH_PATH:   pathIdx = i;    break;
         1544  +      default:           /* no-op */     break;
         1545  +    }
         1546  +  }
         1547  +  if( jsonIdx<0 ){
         1548  +    pIdxInfo->idxNum = 0;
         1549  +    pIdxInfo->estimatedCost = 1e99;
         1550  +  }else{
         1551  +    pIdxInfo->estimatedCost = 1.0;
         1552  +    pIdxInfo->aConstraintUsage[jsonIdx].argvIndex = 1;
         1553  +    pIdxInfo->aConstraintUsage[jsonIdx].omit = 1;
         1554  +    if( pathIdx<0 ){
         1555  +      pIdxInfo->idxNum = 1;
         1556  +    }else{
         1557  +      pIdxInfo->aConstraintUsage[pathIdx].argvIndex = 2;
         1558  +      pIdxInfo->aConstraintUsage[pathIdx].omit = 1;
         1559  +      pIdxInfo->idxNum = 3;
         1560  +    }
         1561  +  }
         1562  +  return SQLITE_OK;
         1563  +}
         1564  +
         1565  +/* Start a search on a new JSON string */
         1566  +static int jsonEachFilter(
         1567  +  sqlite3_vtab_cursor *cur,
         1568  +  int idxNum, const char *idxStr,
         1569  +  int argc, sqlite3_value **argv
         1570  +){
         1571  +  JsonEachCursor *p = (JsonEachCursor*)cur;
         1572  +  const char *z;
         1573  +  const char *zPath;
         1574  +  sqlite3_int64 n;
         1575  +
         1576  +  UNUSED_PARAM(idxStr);
         1577  +  UNUSED_PARAM(argc);
         1578  +  jsonEachCursorReset(p);
         1579  +  if( idxNum==0 ) return SQLITE_OK;
         1580  +  z = (const char*)sqlite3_value_text(argv[0]);
         1581  +  if( z==0 ) return SQLITE_OK;
         1582  +  if( idxNum&2 ){
         1583  +    zPath = (const char*)sqlite3_value_text(argv[1]);
         1584  +    if( zPath==0 || zPath[0]!='$' ) return SQLITE_OK;
         1585  +  }
         1586  +  n = sqlite3_value_bytes(argv[0]);
         1587  +  p->zJson = sqlite3_malloc64( n+1 );
         1588  +  if( p->zJson==0 ) return SQLITE_NOMEM;
         1589  +  memcpy(p->zJson, z, (size_t)n+1);
         1590  +  if( jsonParse(&p->sParse, p->zJson) 
         1591  +   || (p->bRecursive && jsonParseFindParents(&p->sParse))
         1592  +  ){
         1593  +    jsonEachCursorReset(p);
         1594  +  }else{
         1595  +    JsonNode *pNode;
         1596  +    if( idxNum==3 ){
         1597  +      p->bRecursive = 0;
         1598  +      n = sqlite3_value_bytes(argv[1]);
         1599  +      p->zPath = sqlite3_malloc64( n+1 );
         1600  +      if( p->zPath==0 ) return SQLITE_NOMEM;
         1601  +      memcpy(p->zPath, zPath, (size_t)n+1);
         1602  +      pNode = jsonLookup(&p->sParse, 0, p->zPath+1, 0);
         1603  +      if( pNode==0 ){
         1604  +        jsonEachCursorReset(p);
         1605  +        return SQLITE_OK;
         1606  +      }
         1607  +    }else{
         1608  +      pNode = p->sParse.aNode;
         1609  +    }
         1610  +    p->i = (int)(pNode - p->sParse.aNode);
         1611  +    p->eType = pNode->eType;
         1612  +    if( p->eType>=JSON_ARRAY ){
         1613  +      p->i++;
         1614  +      p->iEnd = p->i + pNode->n;
         1615  +    }else{
         1616  +      p->iEnd = p->i+1;
         1617  +    }
         1618  +  }
         1619  +  return SQLITE_OK;
         1620  +}
         1621  +
         1622  +/* The methods of the json_each virtual table */
         1623  +static sqlite3_module jsonEachModule = {
         1624  +  0,                         /* iVersion */
         1625  +  0,                         /* xCreate */
         1626  +  jsonEachConnect,           /* xConnect */
         1627  +  jsonEachBestIndex,         /* xBestIndex */
         1628  +  jsonEachDisconnect,        /* xDisconnect */
         1629  +  0,                         /* xDestroy */
         1630  +  jsonEachOpenEach,          /* xOpen - open a cursor */
         1631  +  jsonEachClose,             /* xClose - close a cursor */
         1632  +  jsonEachFilter,            /* xFilter - configure scan constraints */
         1633  +  jsonEachNext,              /* xNext - advance a cursor */
         1634  +  jsonEachEof,               /* xEof - check for end of scan */
         1635  +  jsonEachColumn,            /* xColumn - read data */
         1636  +  jsonEachRowid,             /* xRowid - read data */
         1637  +  0,                         /* xUpdate */
         1638  +  0,                         /* xBegin */
         1639  +  0,                         /* xSync */
         1640  +  0,                         /* xCommit */
         1641  +  0,                         /* xRollback */
         1642  +  0,                         /* xFindMethod */
         1643  +  0,                         /* xRename */
         1644  +  0,                         /* xSavepoint */
         1645  +  0,                         /* xRelease */
         1646  +  0                          /* xRollbackTo */
         1647  +};
         1648  +
         1649  +/* The methods of the json_tree virtual table. */
         1650  +static sqlite3_module jsonTreeModule = {
         1651  +  0,                         /* iVersion */
         1652  +  0,                         /* xCreate */
         1653  +  jsonEachConnect,           /* xConnect */
         1654  +  jsonEachBestIndex,         /* xBestIndex */
         1655  +  jsonEachDisconnect,        /* xDisconnect */
         1656  +  0,                         /* xDestroy */
         1657  +  jsonEachOpenTree,          /* xOpen - open a cursor */
         1658  +  jsonEachClose,             /* xClose - close a cursor */
         1659  +  jsonEachFilter,            /* xFilter - configure scan constraints */
         1660  +  jsonEachNext,              /* xNext - advance a cursor */
         1661  +  jsonEachEof,               /* xEof - check for end of scan */
         1662  +  jsonEachColumn,            /* xColumn - read data */
         1663  +  jsonEachRowid,             /* xRowid - read data */
         1664  +  0,                         /* xUpdate */
         1665  +  0,                         /* xBegin */
         1666  +  0,                         /* xSync */
         1667  +  0,                         /* xCommit */
         1668  +  0,                         /* xRollback */
         1669  +  0,                         /* xFindMethod */
         1670  +  0,                         /* xRename */
         1671  +  0,                         /* xSavepoint */
         1672  +  0,                         /* xRelease */
         1673  +  0                          /* xRollbackTo */
         1674  +};
         1675  +
         1676  +/****************************************************************************
         1677  +** The following routine is the only publically visible identifier in this
         1678  +** file.  Call the following routine in order to register the various SQL
         1679  +** functions and the virtual table implemented by this file.
         1680  +****************************************************************************/
         1681  +
         1682  +#ifdef _WIN32
         1683  +__declspec(dllexport)
         1684  +#endif
         1685  +int sqlite3_json_init(
         1686  +  sqlite3 *db, 
         1687  +  char **pzErrMsg, 
         1688  +  const sqlite3_api_routines *pApi
         1689  +){
         1690  +  int rc = SQLITE_OK;
         1691  +  unsigned int i;
         1692  +  static const struct {
         1693  +     const char *zName;
         1694  +     int nArg;
         1695  +     int flag;
         1696  +     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
         1697  +  } aFunc[] = {
         1698  +    { "json_array",          -1, 0,   jsonArrayFunc         },
         1699  +    { "json_array_length",    1, 0,   jsonArrayLengthFunc   },
         1700  +    { "json_array_length",    2, 0,   jsonArrayLengthFunc   },
         1701  +    { "json_extract",         2, 0,   jsonExtractFunc       },
         1702  +    { "json_insert",         -1, 0,   jsonSetFunc           },
         1703  +    { "json_object",         -1, 0,   jsonObjectFunc        },
         1704  +    { "json_remove",         -1, 0,   jsonRemoveFunc        },
         1705  +    { "json_replace",        -1, 0,   jsonReplaceFunc       },
         1706  +    { "json_set",            -1, 1,   jsonSetFunc           },
         1707  +    { "json_type",            1, 0,   jsonTypeFunc          },
         1708  +    { "json_type",            2, 0,   jsonTypeFunc          },
         1709  +
         1710  +#if SQLITE_DEBUG
         1711  +    /* DEBUG and TESTING functions */
         1712  +    { "json_parse",           1, 0,   jsonParseFunc         },
         1713  +    { "json_test1",           1, 0,   jsonTest1Func         },
         1714  +    { "json_nodecount",       1, 0,   jsonNodeCountFunc     },
         1715  +#endif
         1716  +  };
         1717  +  static const struct {
         1718  +     const char *zName;
         1719  +     sqlite3_module *pModule;
         1720  +  } aMod[] = {
         1721  +    { "json_each",            &jsonEachModule               },
         1722  +    { "json_tree",            &jsonTreeModule               },
         1723  +  };
         1724  +  SQLITE_EXTENSION_INIT2(pApi);
         1725  +  (void)pzErrMsg;  /* Unused parameter */
         1726  +  for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
         1727  +    rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
         1728  +                                 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 
         1729  +                                 (void*)&aFunc[i].flag,
         1730  +                                 aFunc[i].xFunc, 0, 0);
         1731  +  }
         1732  +  for(i=0; i<sizeof(aMod)/sizeof(aMod[0]) && rc==SQLITE_OK; i++){
         1733  +    rc = sqlite3_create_module(db, aMod[i].zName, aMod[i].pModule, 0);
         1734  +  }
         1735  +  return rc;
         1736  +}

Changes to main.mk.

   293    293   TESTSRC += \
   294    294     $(TOP)/ext/misc/amatch.c \
   295    295     $(TOP)/ext/misc/closure.c \
   296    296     $(TOP)/ext/misc/eval.c \
   297    297     $(TOP)/ext/misc/fileio.c \
   298    298     $(TOP)/ext/misc/fuzzer.c \
   299    299     $(TOP)/ext/misc/ieee754.c \
          300  +  $(TOP)/ext/misc/json1.c \
   300    301     $(TOP)/ext/misc/nextchar.c \
   301    302     $(TOP)/ext/misc/percentile.c \
   302    303     $(TOP)/ext/misc/regexp.c \
   303    304     $(TOP)/ext/misc/series.c \
   304    305     $(TOP)/ext/misc/spellfix.c \
   305    306     $(TOP)/ext/misc/totype.c \
   306    307     $(TOP)/ext/misc/wholenumber.c \

Changes to src/test1.c.

  6373   6373   ){
  6374   6374     extern int sqlite3_amatch_init(sqlite3*,char**,const sqlite3_api_routines*);
  6375   6375     extern int sqlite3_closure_init(sqlite3*,char**,const sqlite3_api_routines*);
  6376   6376     extern int sqlite3_eval_init(sqlite3*,char**,const sqlite3_api_routines*);
  6377   6377     extern int sqlite3_fileio_init(sqlite3*,char**,const sqlite3_api_routines*);
  6378   6378     extern int sqlite3_fuzzer_init(sqlite3*,char**,const sqlite3_api_routines*);
  6379   6379     extern int sqlite3_ieee_init(sqlite3*,char**,const sqlite3_api_routines*);
         6380  +  extern int sqlite3_json_init(sqlite3*,char**,const sqlite3_api_routines*);
  6380   6381     extern int sqlite3_nextchar_init(sqlite3*,char**,const sqlite3_api_routines*);
  6381   6382     extern int sqlite3_percentile_init(sqlite3*,char**,const sqlite3_api_routines*);
  6382   6383     extern int sqlite3_regexp_init(sqlite3*,char**,const sqlite3_api_routines*);
  6383   6384     extern int sqlite3_series_init(sqlite3*,char**,const sqlite3_api_routines*);
  6384   6385     extern int sqlite3_spellfix_init(sqlite3*,char**,const sqlite3_api_routines*);
  6385   6386     extern int sqlite3_totype_init(sqlite3*,char**,const sqlite3_api_routines*);
  6386   6387     extern int sqlite3_wholenumber_init(sqlite3*,char**,const sqlite3_api_routines*);
................................................................................
  6394   6395       { "eval",                  sqlite3_eval_init                 },
  6395   6396   #ifdef SQLITE_ENABLE_FTS5
  6396   6397       { "fts5",                  sqlite3_fts5_init                 },
  6397   6398   #endif
  6398   6399       { "fileio",                sqlite3_fileio_init               },
  6399   6400       { "fuzzer",                sqlite3_fuzzer_init               },
  6400   6401       { "ieee754",               sqlite3_ieee_init                 },
         6402  +    { "json",                  sqlite3_json_init                 },
  6401   6403       { "nextchar",              sqlite3_nextchar_init             },
  6402   6404       { "percentile",            sqlite3_percentile_init           },
  6403   6405       { "regexp",                sqlite3_regexp_init               },
  6404   6406       { "series",                sqlite3_series_init               },
  6405   6407       { "spellfix",              sqlite3_spellfix_init             },
  6406   6408       { "totype",                sqlite3_totype_init               },
  6407   6409       { "wholenumber",           sqlite3_wholenumber_init          },

Added test/json101.test.

            1  +# 2015-08-12
            2  +#
            3  +# The author disclaims copyright to this source code.  In place of
            4  +# a legal notice, here is a blessing:
            5  +#
            6  +#    May you do good and not evil.
            7  +#    May you find forgiveness for yourself and forgive others.
            8  +#    May you share freely, never taking more than you give.
            9  +#
           10  +#***********************************************************************
           11  +# This file implements tests for JSON SQL functions extension to the
           12  +# SQLite library.
           13  +#
           14  +
           15  +set testdir [file dirname $argv0]
           16  +source $testdir/tester.tcl
           17  +
           18  +load_static_extension db json
           19  +do_execsql_test json1-1.1 {
           20  +  SELECT json_array(1,2.5,null,'hello');
           21  +} {[1,2.5,null,"hello"]}
           22  +do_execsql_test json1-1.2 {
           23  +  SELECT hex(json_array('String "\ Test'));
           24  +} {5B22537472696E67205C225C5C2054657374225D}
           25  +do_catchsql_test json1-1.3 {
           26  +  SELECT json_array(1,2,x'abcd',3);
           27  +} {1 {JSON cannot hold BLOB values}}
           28  +do_execsql_test json1-1.4 {
           29  +  SELECT json_array(-9223372036854775808,9223372036854775807,0,1,-1,
           30  +                    0.0, 1.0, -1.0, -1e99, +2e100,
           31  +                    'one','two','three',
           32  +                    4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
           33  +                    19, NULL, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
           34  +                    'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ',
           35  +                    'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ',
           36  +                    'abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ',
           37  +                    99);
           38  +} {[-9223372036854775808,9223372036854775807,0,1,-1,0.0,1.0,-1.0,-1.0e+99,2.0e+100,"one","two","three",4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,null,21,22,23,24,25,26,27,28,29,30,31,"abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ",99]}
           39  +
           40  +do_execsql_test json1-2.1 {
           41  +  SELECT json_object('a',1,'b',2.5,'c',null,'d','String Test');
           42  +} {{{"a":1,"b":2.5,"c":null,"d":"String Test"}}}
           43  +do_catchsql_test json1-2.2 {
           44  +  SELECT json_object('a',1,2,2.5);
           45  +} {1 {json_object() labels must be TEXT}}
           46  +do_catchsql_test json1-2.3 {
           47  +  SELECT json_object('a',1,'b');
           48  +} {1 {json_object() requires an even number of arguments}}
           49  +do_catchsql_test json1-2.4 {
           50  +  SELECT json_object('a',1,'b',x'abcd');
           51  +} {1 {JSON cannot hold BLOB values}}
           52  +
           53  +
           54  +finish_test