/ Check-in [39983204]
Login

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

Overview
Comment:Initial implementation for json_array_length(), json_extract(), and json_type().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | json
Files: files | file ages | folders
SHA1: 39983204515837e7bd574cf47918e493acc03d1f
User & Date: drh 2015-08-17 15:17:37
Context
2015-08-17
20:14
Add an initial implementation for json_remove(). check-in: 2a826720 user: drh tags: json
15:17
Initial implementation for json_array_length(), json_extract(), and json_type(). check-in: 39983204 user: drh tags: json
11:28
Abandon the JSONB format for now. (We may return to it in the future.) Add a function to render a JSON parse. check-in: 9703c0aa user: drh tags: json
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to ext/misc/json.c.

    20     20   ** a BLOB, but there is no support for JSONB in the current implementation.)
    21     21   */
    22     22   #include "sqlite3ext.h"
    23     23   SQLITE_EXTENSION_INIT1
    24     24   #include <assert.h>
    25     25   #include <string.h>
    26     26   #include <ctype.h>
           27  +#include <stdlib.h>
    27     28   
    28     29   /* Unsigned integer types */
    29     30   typedef sqlite3_uint64 u64;
    30     31   typedef unsigned int u32;
    31     32   typedef unsigned char u8;
    32     33   
    33     34   /* An instance of this object represents a JSON string
................................................................................
    51     52   #define JSON_TRUE     1
    52     53   #define JSON_FALSE    2
    53     54   #define JSON_INT      3
    54     55   #define JSON_REAL     4
    55     56   #define JSON_STRING   5
    56     57   #define JSON_ARRAY    6
    57     58   #define JSON_OBJECT   7
           59  +
           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  +
    58     67   
    59     68   /* A single node of parsed JSON
    60     69   */
    61     70   typedef struct JsonNode JsonNode;
    62     71   struct JsonNode {
    63     72     u8 eType;              /* One of the JSON_ type values */
    64     73     u8 bRaw;               /* Content is raw, rather than JSON encoded */
           74  +  u8 bBackslash;         /* Formatted JSON_STRING contains \ escapes */
    65     75     u32 n;                 /* Bytes of content, or number of sub-nodes */
    66     76     const char *zJContent; /* JSON content */
    67     77   };
    68     78   
    69     79   /* A completely parsed JSON string
    70     80   */
    71     81   typedef struct JsonParse JsonParse;
................................................................................
   264    274         sqlite3_result_int(pCtx, 1);
   265    275         break;
   266    276       }
   267    277       case JSON_FALSE: {
   268    278         sqlite3_result_int(pCtx, 0);
   269    279         break;
   270    280       }
   271         -
   272         -    /* FIXME:  We really want to do text->numeric conversion on these.
   273         -    ** Doing so would be easy if these were internal routines, but the
   274         -    ** necessary interfaces are not exposed for doing it as a loadable
   275         -    ** extension. */
   276         -    case JSON_REAL:
          281  +    case JSON_REAL: {
          282  +      double r = strtod(pNode->zJContent, 0);
          283  +      sqlite3_result_double(pCtx, r);
          284  +      break;
          285  +    }
   277    286       case JSON_INT: {
   278         -      sqlite3_result_text(pCtx, pNode->zJContent, pNode->n, SQLITE_TRANSIENT);
          287  +      sqlite3_int64 i = 0;
          288  +      const char *z = pNode->zJContent;
          289  +      if( z[0]=='-' ){ z++; }
          290  +      while( z[0]>='0' && z[0]<='9' ){ i = i*10 + *(z++) - '0'; }
          291  +      if( pNode->zJContent[0]=='-' ){ i = -i; }
          292  +      sqlite3_result_int64(pCtx, i);
   279    293         break;
   280    294       }
   281         -
   282    295       case JSON_STRING: {
   283    296         if( pNode->bRaw ){
   284    297           sqlite3_result_text(pCtx, pNode->zJContent, pNode->n, SQLITE_TRANSIENT);
          298  +      }else if( !pNode->bBackslash ){
          299  +        /* JSON formatted without any backslash-escapes */
          300  +        sqlite3_result_text(pCtx, pNode->zJContent+1, pNode->n-2,
          301  +                            SQLITE_TRANSIENT);
   285    302         }else{
   286    303           /* Translate JSON formatted string into raw text */
          304  +        u32 i;
          305  +        u32 n = pNode->n;
          306  +        const char *z = pNode->zJContent;
          307  +        char *zOut;
          308  +        u32 j;
          309  +        zOut = sqlite3_malloc( n+1 );
          310  +        if( zOut==0 ){
          311  +          sqlite3_result_error_nomem(pCtx);
          312  +          break;
          313  +        }
          314  +        for(i=1, j=0; i<n-1; i++){
          315  +          char c = z[i];
          316  +          if( c!='\\' && z[i+1] ){
          317  +            zOut[j++] = c;
          318  +          }else{
          319  +            c = z[++i];
          320  +            if( c=='u' && z[1] ){
          321  +              u32 v = 0, k;
          322  +              z++;
          323  +              for(k=0; k<4 && z[k]; k++){
          324  +                c = z[0];
          325  +                if( c>='0' && c<='9' ) v = v*16 + c - '0';
          326  +                else if( c>='A' && c<='F' ) v = v*16 + c - 'A' + 10;
          327  +                else if( c>='a' && c<='f' ) v = v*16 + c - 'a' + 10;
          328  +                else break;
          329  +                z++;
          330  +              }
          331  +              if( v<=0x7f ){
          332  +                zOut[j++] = v;
          333  +              }else if( v<=0x7ff ){
          334  +                zOut[j++] = 0xc0 | (v>>6);
          335  +                zOut[j++] = 0x80 | (v&0x3f);
          336  +              }else if( v<=0xffff ){
          337  +                zOut[j++] = 0xe0 | (v>>12);
          338  +                zOut[j++] = 0x80 | ((v>>6)&0x3f);
          339  +                zOut[j++] = 0x80 | (v&0x3f);
          340  +              }else if( v<=0x10ffff ){
          341  +                zOut[j++] = 0xf0 | (v>>18);
          342  +                zOut[j++] = 0x80 | ((v>>12)&0x3f);
          343  +                zOut[j++] = 0x80 | ((v>>6)&0x3f);
          344  +                zOut[j++] = 0x80 | (v&0x3f);
          345  +              }
          346  +            }else{
          347  +              if( c=='b' ){
          348  +                c = '\b';
          349  +              }else if( c=='f' ){
          350  +                c = '\f';
          351  +              }else if( c=='n' ){
          352  +                c = '\n';
          353  +              }else if( c=='r' ){
          354  +                c = '\r';
          355  +              }else if( c=='t' ){
          356  +                c = '\t';
          357  +              }
          358  +              zOut[j++] = c;
          359  +            }
          360  +          }
          361  +        }
          362  +        zOut[j] = 0;
          363  +        sqlite3_result_text(pCtx, zOut, j, sqlite3_free);
   287    364         }
   288    365         break;
   289    366       }
   290    367       case JSON_ARRAY:
   291    368       case JSON_OBJECT: {
   292    369         Json s;
   293    370         jsonInit(&s, pCtx);
................................................................................
   294    371         jsonRenderNode(pNode, &s);
   295    372         jsonResult(&s);
   296    373         break;
   297    374       }
   298    375     }
   299    376   }
   300    377   
   301         -/*
   302         -** Implementation of the json_array(VALUE,...) function.  Return a JSON
   303         -** array that contains all values given in arguments.  Or if any argument
   304         -** is a BLOB, throw an error.
   305         -*/
   306         -static void jsonArrayFunc(
   307         -  sqlite3_context *context,
   308         -  int argc,
   309         -  sqlite3_value **argv
   310         -){
   311         -  int i;
   312         -  Json jx;
   313         -  char cSep = '[';
   314         -
   315         -  jsonInit(&jx, context);
   316         -  for(i=0; i<argc; i++){
   317         -    jsonAppendRaw(&jx, &cSep, 1);
   318         -    cSep = ',';
   319         -    switch( sqlite3_value_type(argv[i]) ){
   320         -      case SQLITE_NULL: {
   321         -        jsonAppendRaw(&jx, "null", 4);
   322         -        break;
   323         -      }
   324         -      case SQLITE_INTEGER:
   325         -      case SQLITE_FLOAT: {
   326         -        const char *z = (const char*)sqlite3_value_text(argv[i]);
   327         -        u32 n = (u32)sqlite3_value_bytes(argv[i]);
   328         -        jsonAppendRaw(&jx, z, n);
   329         -        break;
   330         -      }
   331         -      case SQLITE_TEXT: {
   332         -        const char *z = (const char*)sqlite3_value_text(argv[i]);
   333         -        u32 n = (u32)sqlite3_value_bytes(argv[i]);
   334         -        jsonAppendString(&jx, z, n);
   335         -        break;
   336         -      }
   337         -      default: {
   338         -        jsonZero(&jx);
   339         -        sqlite3_result_error(context, "JSON cannot hold BLOB values", -1);
   340         -        return;
   341         -      }
   342         -    }
   343         -  }
   344         -  jsonAppendRaw(&jx, "]", 1);
   345         -  jsonResult(&jx);
   346         -}
   347         -
   348         -/*
   349         -** Implementation of the json_object(NAME,VALUE,...) function.  Return a JSON
   350         -** object that contains all name/value given in arguments.  Or if any name
   351         -** is not a string or if any value is a BLOB, throw an error.
   352         -*/
   353         -static void jsonObjectFunc(
   354         -  sqlite3_context *context,
   355         -  int argc,
   356         -  sqlite3_value **argv
   357         -){
   358         -  int i;
   359         -  Json jx;
   360         -  char cSep = '{';
   361         -  const char *z;
   362         -  u32 n;
   363         -
   364         -  if( argc&1 ){
   365         -    sqlite3_result_error(context, "json_object() requires an even number "
   366         -                                  "of arguments", -1);
   367         -    return;
   368         -  }
   369         -  jsonInit(&jx, context);
   370         -  for(i=0; i<argc; i+=2){
   371         -    jsonAppendRaw(&jx, &cSep, 1);
   372         -    cSep = ',';
   373         -    if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
   374         -      sqlite3_result_error(context, "json_object() labels must be TEXT", -1);
   375         -      jsonZero(&jx);
   376         -      return;
   377         -    }
   378         -    z = (const char*)sqlite3_value_text(argv[i]);
   379         -    n = (u32)sqlite3_value_bytes(argv[i]);
   380         -    jsonAppendString(&jx, z, n);
   381         -    jsonAppendRaw(&jx, ":", 1);
   382         -    switch( sqlite3_value_type(argv[i+1]) ){
   383         -      case SQLITE_NULL: {
   384         -        jsonAppendRaw(&jx, "null", 4);
   385         -        break;
   386         -      }
   387         -      case SQLITE_INTEGER:
   388         -      case SQLITE_FLOAT: {
   389         -        z = (const char*)sqlite3_value_text(argv[i+1]);
   390         -        n = (u32)sqlite3_value_bytes(argv[i+1]);
   391         -        jsonAppendRaw(&jx, z, n);
   392         -        break;
   393         -      }
   394         -      case SQLITE_TEXT: {
   395         -        z = (const char*)sqlite3_value_text(argv[i+1]);
   396         -        n = (u32)sqlite3_value_bytes(argv[i+1]);
   397         -        jsonAppendString(&jx, z, n);
   398         -        break;
   399         -      }
   400         -      default: {
   401         -        jsonZero(&jx);
   402         -        sqlite3_result_error(context, "JSON cannot hold BLOB values", -1);
   403         -        return;
   404         -      }
   405         -    }
   406         -  }
   407         -  jsonAppendRaw(&jx, "}", 1);
   408         -  jsonResult(&jx);
   409         -}
   410         -
   411    378   /*
   412    379   ** Create a new JsonNode instance based on the arguments and append that
   413    380   ** instance to the JsonParse.  Return the index in pParse->aNode[] of the
   414    381   ** new node, or -1 if a memory allocation fails.
   415    382   */
   416    383   static int jsonParseAddNode(
   417    384     JsonParse *pParse,        /* Append the node to this object */
................................................................................
   436    403       }
   437    404       pParse->nAlloc = nNew;
   438    405       pParse->aNode = pNew;
   439    406     }
   440    407     p = &pParse->aNode[pParse->nNode];
   441    408     p->eType = (u8)eType;
   442    409     p->bRaw = 0;
          410  +  p->bBackslash = 0;
   443    411     p->n = n;
   444    412     p->zJContent = zContent;
   445    413     return pParse->nNode++;
   446    414   }
   447    415   
   448    416   /*
   449    417   ** Parse a single JSON value which begins at pParse->zJson[i].  Return the
................................................................................
   505    473         if( c!=']' ) return -1;
   506    474         break;
   507    475       }
   508    476       pParse->aNode[iThis].n = pParse->nNode - iThis - 1;
   509    477       return j+1;
   510    478     }else if( c=='"' ){
   511    479       /* Parse string */
          480  +    u8 bBackslash = 0;
   512    481       j = i+1;
   513    482       for(;;){
   514    483         c = pParse->zJson[j];
   515    484         if( c==0 ) return -1;
   516    485         if( c=='\\' ){
   517    486           c = pParse->zJson[++j];
   518    487           if( c==0 ) return -1;
          488  +        bBackslash = 1;
   519    489         }else if( c=='"' ){
   520    490           break;
   521    491         }
   522    492         j++;
   523    493       }
   524    494       jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]);
          495  +    if( bBackslash ) pParse->aNode[pParse->nNode-1].bBackslash = 1;
   525    496       return j+1;
   526    497     }else if( c=='n'
   527    498            && strncmp(pParse->zJson+i,"null",4)==0
   528    499            && !isalnum(pParse->zJson[i+4]) ){
   529    500       jsonParseAddNode(pParse, JSON_NULL, 0, 0);
   530    501       return i+4;
   531    502     }else if( c=='t'
................................................................................
   597    568       pParse->aNode = 0;
   598    569       pParse->nNode = 0;
   599    570       pParse->nAlloc = 0;
   600    571       return 1;
   601    572     }
   602    573     return 0;
   603    574   }
          575  +/*
          576  +** Search along zPath to find the node specified.  Return a pointer
          577  +** to that node, or NULL if zPath is malformed or if there is no such
          578  +** node.
          579  +*/
          580  +static JsonNode *jsonLookup(JsonNode *pRoot, const char *zPath){
          581  +  u32 i, j;
          582  +  if( zPath[0]==0 ) return pRoot;
          583  +  if( zPath[0]=='.' ){
          584  +    if( pRoot->eType!=JSON_OBJECT ) return 0;
          585  +    zPath++;
          586  +    for(i=0; isalnum(zPath[i]); i++){}
          587  +    if( i==0 ) return 0;
          588  +    j = 1;
          589  +    while( j<=pRoot->n ){
          590  +      if( pRoot[j].n==i+2
          591  +       && strncmp(&pRoot[j].zJContent[1],zPath,i)==0
          592  +      ){
          593  +        return jsonLookup(&pRoot[j+1], &zPath[i]);
          594  +      }
          595  +      j++;
          596  +      if( pRoot[j].eType==JSON_ARRAY || pRoot[j].eType==JSON_OBJECT ){
          597  +        j += pRoot[j].n;
          598  +      }
          599  +      j++;
          600  +    }
          601  +  }else if( zPath[0]=='[' && isdigit(zPath[1]) ){
          602  +    if( pRoot->eType!=JSON_ARRAY ) return 0;
          603  +    i = 0;
          604  +    zPath++;
          605  +    while( isdigit(zPath[0]) ){
          606  +      i = i + zPath[0] - '0';
          607  +      zPath++;
          608  +    }
          609  +    if( zPath[0]!=']' ) return 0;
          610  +    zPath++;
          611  +    j = 1;
          612  +    while( i>0 && j<=pRoot->n ){
          613  +      if( pRoot[j].eType==JSON_ARRAY || pRoot[j].eType==JSON_OBJECT ){
          614  +        j += pRoot[j].n;
          615  +      }
          616  +      j++;
          617  +      i--;
          618  +    }
          619  +    if( j<=pRoot->n ){
          620  +      return jsonLookup(&pRoot[j], zPath);
          621  +    }
          622  +  }
          623  +  return 0;
          624  +}
          625  +
          626  +/****************************************************************************
          627  +** SQL functions used for testing and debugging
          628  +****************************************************************************/
   604    629   
   605    630   /*
   606    631   ** The json_parse(JSON) function returns a string which describes
   607    632   ** a parse of the JSON provided.  Or it returns NULL if JSON is not
   608    633   ** well-formed.
   609    634   */
   610    635   static void jsonParseFunc(
................................................................................
   612    637     int argc,
   613    638     sqlite3_value **argv
   614    639   ){
   615    640     Json s;       /* Output string - not real JSON */
   616    641     JsonParse x;  /* The parse */
   617    642     u32 i;
   618    643     char zBuf[50];
   619         -  static const char *azType[] = {
   620         -    "NULL", "TRUE", "FALSE", "INT", "REAL", "STRING", "ARRAY", "OBJECT"
   621         -  };
   622    644   
   623    645     assert( argc==1 );
   624    646     if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
   625    647     jsonInit(&s, context);
   626    648     for(i=0; i<x.nNode; i++){
   627    649       sqlite3_snprintf(sizeof(zBuf), zBuf, "node %u:\n", i);
   628    650       jsonAppend(&s, zBuf);
   629    651       sqlite3_snprintf(sizeof(zBuf), zBuf, "  type: %s\n",
   630         -                     azType[x.aNode[i].eType]);
          652  +                     jsonType[x.aNode[i].eType]);
   631    653       jsonAppend(&s, zBuf);
   632    654       if( x.aNode[i].eType>=JSON_INT ){
   633    655         sqlite3_snprintf(sizeof(zBuf), zBuf, "     n: %u\n", x.aNode[i].n);
   634    656         jsonAppend(&s, zBuf);
   635    657       }
   636    658       if( x.aNode[i].zJContent!=0 ){
   637    659         sqlite3_snprintf(sizeof(zBuf), zBuf, "  ofst: %u\n",
................................................................................
   671    693   ){
   672    694     JsonParse x;  /* The parse */
   673    695     if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
   674    696     sqlite3_result_int64(context, x.nNode);
   675    697     sqlite3_free(x.aNode);
   676    698   }
   677    699   
          700  +/****************************************************************************
          701  +** SQL function implementations
          702  +****************************************************************************/
          703  +
          704  +/*
          705  +** Implementation of the json_array(VALUE,...) function.  Return a JSON
          706  +** array that contains all values given in arguments.  Or if any argument
          707  +** is a BLOB, throw an error.
          708  +*/
          709  +static void jsonArrayFunc(
          710  +  sqlite3_context *context,
          711  +  int argc,
          712  +  sqlite3_value **argv
          713  +){
          714  +  int i;
          715  +  Json jx;
          716  +  char cSep = '[';
          717  +
          718  +  jsonInit(&jx, context);
          719  +  for(i=0; i<argc; i++){
          720  +    jsonAppendRaw(&jx, &cSep, 1);
          721  +    cSep = ',';
          722  +    switch( sqlite3_value_type(argv[i]) ){
          723  +      case SQLITE_NULL: {
          724  +        jsonAppendRaw(&jx, "null", 4);
          725  +        break;
          726  +      }
          727  +      case SQLITE_INTEGER:
          728  +      case SQLITE_FLOAT: {
          729  +        const char *z = (const char*)sqlite3_value_text(argv[i]);
          730  +        u32 n = (u32)sqlite3_value_bytes(argv[i]);
          731  +        jsonAppendRaw(&jx, z, n);
          732  +        break;
          733  +      }
          734  +      case SQLITE_TEXT: {
          735  +        const char *z = (const char*)sqlite3_value_text(argv[i]);
          736  +        u32 n = (u32)sqlite3_value_bytes(argv[i]);
          737  +        jsonAppendString(&jx, z, n);
          738  +        break;
          739  +      }
          740  +      default: {
          741  +        jsonZero(&jx);
          742  +        sqlite3_result_error(context, "JSON cannot hold BLOB values", -1);
          743  +        return;
          744  +      }
          745  +    }
          746  +  }
          747  +  jsonAppendRaw(&jx, "]", 1);
          748  +  jsonResult(&jx);
          749  +}
          750  +
          751  +
          752  +/*
          753  +** json_array_length(JSON)
          754  +** json_array_length(JSON, PATH)
          755  +**
          756  +** Return the number of elements in the top-level JSON array.  
          757  +** Return 0 if the input is not a well-formed JSON array.
          758  +*/
          759  +static void jsonArrayLengthFunc(
          760  +  sqlite3_context *context,
          761  +  int argc,
          762  +  sqlite3_value **argv
          763  +){
          764  +  JsonParse x;          /* The parse */
          765  +  sqlite3_int64 n = 0;
          766  +  u32 i;
          767  +  const char *zPath;
          768  +
          769  +  if( argc==2 ){
          770  +    zPath = (const char*)sqlite3_value_text(argv[1]);
          771  +    if( zPath==0 ) return;
          772  +    if( zPath[0]!='$' ) return;
          773  +    zPath++;
          774  +  }else{
          775  +    zPath = 0;
          776  +  }
          777  +  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0]))==0 ){
          778  +    if( x.nNode ){
          779  +      JsonNode *pNode = x.aNode;
          780  +      if( zPath ) pNode = jsonLookup(pNode, zPath);
          781  +      if( pNode->eType==JSON_ARRAY ){
          782  +        for(i=1; i<=pNode->n; i++, n++){
          783  +          if( pNode[i].eType==JSON_ARRAY || pNode[i].eType==JSON_OBJECT ){
          784  +            i += pNode[i].n;
          785  +          }
          786  +        }
          787  +      }
          788  +    }
          789  +    sqlite3_free(x.aNode);
          790  +  }
          791  +  sqlite3_result_int64(context, n);
          792  +}
          793  +
          794  +/*
          795  +** json_extract(JSON, PATH)
          796  +**
          797  +** Return the element described by PATH.  Return NULL if JSON is not
          798  +** valid JSON or if there is no PATH element or if PATH is malformed.
          799  +*/
          800  +static void jsonExtractFunc(
          801  +  sqlite3_context *context,
          802  +  int argc,
          803  +  sqlite3_value **argv
          804  +){
          805  +  JsonParse x;          /* The parse */
          806  +  JsonNode *pNode;
          807  +  const char *zPath;
          808  +  assert( argc==2 );
          809  +  zPath = (const char*)sqlite3_value_text(argv[1]);
          810  +  if( zPath==0 ) return;
          811  +  if( zPath[0]!='$' ) return;
          812  +  zPath++;
          813  +  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
          814  +  pNode = jsonLookup(x.aNode, zPath);
          815  +  if( pNode ){
          816  +    jsonReturn(pNode, context);
          817  +  }
          818  +  sqlite3_free(x.aNode);
          819  +}
          820  +
          821  +/*
          822  +** Implementation of the json_object(NAME,VALUE,...) function.  Return a JSON
          823  +** object that contains all name/value given in arguments.  Or if any name
          824  +** is not a string or if any value is a BLOB, throw an error.
          825  +*/
          826  +static void jsonObjectFunc(
          827  +  sqlite3_context *context,
          828  +  int argc,
          829  +  sqlite3_value **argv
          830  +){
          831  +  int i;
          832  +  Json jx;
          833  +  char cSep = '{';
          834  +  const char *z;
          835  +  u32 n;
          836  +
          837  +  if( argc&1 ){
          838  +    sqlite3_result_error(context, "json_object() requires an even number "
          839  +                                  "of arguments", -1);
          840  +    return;
          841  +  }
          842  +  jsonInit(&jx, context);
          843  +  for(i=0; i<argc; i+=2){
          844  +    jsonAppendRaw(&jx, &cSep, 1);
          845  +    cSep = ',';
          846  +    if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
          847  +      sqlite3_result_error(context, "json_object() labels must be TEXT", -1);
          848  +      jsonZero(&jx);
          849  +      return;
          850  +    }
          851  +    z = (const char*)sqlite3_value_text(argv[i]);
          852  +    n = (u32)sqlite3_value_bytes(argv[i]);
          853  +    jsonAppendString(&jx, z, n);
          854  +    jsonAppendRaw(&jx, ":", 1);
          855  +    switch( sqlite3_value_type(argv[i+1]) ){
          856  +      case SQLITE_NULL: {
          857  +        jsonAppendRaw(&jx, "null", 4);
          858  +        break;
          859  +      }
          860  +      case SQLITE_INTEGER:
          861  +      case SQLITE_FLOAT: {
          862  +        z = (const char*)sqlite3_value_text(argv[i+1]);
          863  +        n = (u32)sqlite3_value_bytes(argv[i+1]);
          864  +        jsonAppendRaw(&jx, z, n);
          865  +        break;
          866  +      }
          867  +      case SQLITE_TEXT: {
          868  +        z = (const char*)sqlite3_value_text(argv[i+1]);
          869  +        n = (u32)sqlite3_value_bytes(argv[i+1]);
          870  +        jsonAppendString(&jx, z, n);
          871  +        break;
          872  +      }
          873  +      default: {
          874  +        jsonZero(&jx);
          875  +        sqlite3_result_error(context, "JSON cannot hold BLOB values", -1);
          876  +        return;
          877  +      }
          878  +    }
          879  +  }
          880  +  jsonAppendRaw(&jx, "}", 1);
          881  +  jsonResult(&jx);
          882  +}
          883  +
          884  +
          885  +/*
          886  +** json_type(JSON)
          887  +** json_type(JSON, PATH)
          888  +**
          889  +** Return the top-level "type" of a JSON string.  Return NULL if the
          890  +** input is not a well-formed JSON string.
          891  +*/
          892  +static void jsonTypeFunc(
          893  +  sqlite3_context *context,
          894  +  int argc,
          895  +  sqlite3_value **argv
          896  +){
          897  +  JsonParse x;          /* The parse */
          898  +  const char *zPath;
          899  +
          900  +  if( argc==2 ){
          901  +    zPath = (const char*)sqlite3_value_text(argv[1]);
          902  +    if( zPath==0 ) return;
          903  +    if( zPath[0]!='$' ) return;
          904  +    zPath++;
          905  +  }else{
          906  +    zPath = 0;
          907  +  }
          908  +  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
          909  +  if( x.nNode ){
          910  +    JsonNode *pNode = x.aNode;
          911  +    if( zPath ) pNode = jsonLookup(pNode, zPath);
          912  +    sqlite3_result_text(context, jsonType[pNode->eType], -1, SQLITE_STATIC);
          913  +  }
          914  +  sqlite3_free(x.aNode);
          915  +}
   678    916   
   679    917   #ifdef _WIN32
   680    918   __declspec(dllexport)
   681    919   #endif
   682    920   int sqlite3_json_init(
   683    921     sqlite3 *db, 
   684    922     char **pzErrMsg, 
................................................................................
   687    925     int rc = SQLITE_OK;
   688    926     int i;
   689    927     static const struct {
   690    928        const char *zName;
   691    929        int nArg;
   692    930        void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
   693    931     } aFunc[] = {
   694         -    { "json_array",     -1,    jsonArrayFunc     },
   695         -    { "json_object",    -1,    jsonObjectFunc    },
   696         -    { "json_parse",      1,    jsonParseFunc     },  /* DEBUG */
   697         -    { "json_test1",      1,    jsonTest1Func     },  /* DEBUG */
   698         -    { "json_nodecount",  1,    jsonNodeCountFunc },  /* DEBUG */
          932  +    { "json_array",          -1,    jsonArrayFunc         },
          933  +    { "json_array_length",    1,    jsonArrayLengthFunc   },
          934  +    { "json_array_length",    2,    jsonArrayLengthFunc   },
          935  +    { "json_extract",         2,    jsonExtractFunc       },
          936  +    { "json_object",         -1,    jsonObjectFunc        },
          937  +    { "json_type",            1,    jsonTypeFunc          },
          938  +    { "json_type",            2,    jsonTypeFunc          },
          939  +
          940  +    /* DEBUG and TESTING functions */
          941  +    { "json_parse",           1,    jsonParseFunc     },
          942  +    { "json_test1",           1,    jsonTest1Func     },
          943  +    { "json_nodecount",       1,    jsonNodeCountFunc },
   699    944     };
   700    945     SQLITE_EXTENSION_INIT2(pApi);
   701    946     (void)pzErrMsg;  /* Unused parameter */
   702    947     for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
   703    948       rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
   704    949                                    SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
   705    950                                    aFunc[i].xFunc, 0, 0);
   706    951     }
   707    952     return rc;
   708    953   }