/ Check-in [2a826720]
Login

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

Overview
Comment:Add an initial implementation for json_remove().
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | json
Files: files | file ages | folders
SHA1: 2a8267209dbda36a37c1b5f65000b2f763c62341
User & Date: drh 2015-08-17 20:14:19
Context
2015-08-17
21:22
Initial implementation of json_replace(). check-in: 3c4bee65 user: drh tags: json
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
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ext/misc/json.c.

60
61
62
63
64
65
66






67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
..
82
83
84
85
86
87
88









89
90
91
92
93
94
95
...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
...
166
167
168
169
170
171
172










173
174
175
176
177
178
179
...
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
...
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241

242


243

244
245
246
247
248
249
250
251

252


253
254
255

256
257
258
259
260
261
262
263
264
265
266
267
268
...
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
...
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
...
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
...
568
569
570
571
572
573
574

575
576
577
578
579
580
581
...
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629

630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
...
692
693
694
695
696
697
698

699
700
701
702
703
704
705
...
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
...
877
878
879
880
881
882
883


































884
885
886
887
888
889
890
...
930
931
932
933
934
935
936

937
938
939

940
941
942
943

944
945
946
947
948
949
950
951
952
953
/*
** Names of the various JSON types:
*/
static const char * const jsonType[] = {
  "null", "true", "false", "integer", "real", "text", "array", "object"
};








/* A single node of parsed JSON
*/
typedef struct JsonNode JsonNode;
struct JsonNode {
  u8 eType;              /* One of the JSON_ type values */
  u8 bRaw;               /* Content is raw, rather than JSON encoded */
  u8 bBackslash;         /* Formatted JSON_STRING contains \ escapes */
  u32 n;                 /* Bytes of content, or number of sub-nodes */
  const char *zJContent; /* JSON content */
};

/* A completely parsed JSON string
*/
typedef struct JsonParse JsonParse;
................................................................................
struct JsonParse {
  u32 nNode;         /* Number of slots of aNode[] used */
  u32 nAlloc;        /* Number of slots of aNode[] allocated */
  JsonNode *aNode;   /* Array of nodes containing the parse */
  const char *zJson; /* Original JSON string */
  u8 oom;            /* Set to true if out of memory */
};










/* Set the Json object to an empty string
*/
static void jsonZero(Json *p){
  p->zBuf = p->zSpace;
  p->nAlloc = sizeof(p->zSpace);
  p->nUsed = 0;
................................................................................
  jsonReset(p);
}

/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
** Return zero on success.  Return non-zero on an OOM error
*/
static int jsonGrow(Json *p, u32 N){
  u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+100;
  char *zNew;
  if( p->bStatic ){
    if( p->oom ) return SQLITE_NOMEM;
    zNew = sqlite3_malloc64(nTotal);
    if( zNew==0 ){
      jsonOom(p);
      return SQLITE_NOMEM;
................................................................................

/* Append a single character
*/
static void jsonAppendChar(Json *p, char c){
  if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
  p->zBuf[p->nUsed++] = c;
}











/* Append the N-byte string in zIn to the end of the Json string
** under construction.  Enclose the string in "..." and escape
** any double-quotes or backslash characters contained within the
** string.
*/
static void jsonAppendString(Json *p, const char *zIn, u32 N){
................................................................................

/*
** Convert the JsonNode pNode into a pure JSON string and
** append to pOut.  Subsubstructure is also included.  Return
** the number of JsonNode objects that are encoded.
*/
static int jsonRenderNode(JsonNode *pNode, Json *pOut){
  u32 j = 0;
  switch( pNode->eType ){
    case JSON_NULL: {
      jsonAppendRaw(pOut, "null", 4);
      break;
    }
    case JSON_TRUE: {
      jsonAppendRaw(pOut, "true", 4);
................................................................................
      break;
    }
    case JSON_FALSE: {
      jsonAppendRaw(pOut, "false", 5);
      break;
    }
    case JSON_STRING: {
      if( pNode->bRaw ){
        jsonAppendString(pOut, pNode->zJContent, pNode->n);
        break;
      }
      /* Fall through into the next case */
    }
    case JSON_REAL:
    case JSON_INT: {
      jsonAppendRaw(pOut, pNode->zJContent, pNode->n);
      break;
    }
    case JSON_ARRAY: {
      jsonAppendChar(pOut, '[');
      j = 0;
      while( j<pNode->n ){

        if( j>0 ) jsonAppendChar(pOut, ',');


        j += jsonRenderNode(&pNode[j+1], pOut);

      }
      jsonAppendChar(pOut, ']');
      break;
    }
    case JSON_OBJECT: {
      jsonAppendChar(pOut, '{');
      j = 0;
      while( j<pNode->n ){

        if( j>0 ) jsonAppendChar(pOut, ',');


        j += jsonRenderNode(&pNode[j+1], pOut);
        jsonAppendChar(pOut, ':');
        j += jsonRenderNode(&pNode[j+1], pOut);

      }
      jsonAppendChar(pOut, '}');
      break;
    }
  }
  return j+1;
}

/*
** Make the JsonNode the return value of the function.
*/
static void jsonReturn(JsonNode *pNode, sqlite3_context *pCtx){
  switch( pNode->eType ){
................................................................................
      if( z[0]=='-' ){ z++; }
      while( z[0]>='0' && z[0]<='9' ){ i = i*10 + *(z++) - '0'; }
      if( pNode->zJContent[0]=='-' ){ i = -i; }
      sqlite3_result_int64(pCtx, i);
      break;
    }
    case JSON_STRING: {
      if( pNode->bRaw ){
        sqlite3_result_text(pCtx, pNode->zJContent, pNode->n, SQLITE_TRANSIENT);
      }else if( !pNode->bBackslash ){
        /* JSON formatted without any backslash-escapes */
        sqlite3_result_text(pCtx, pNode->zJContent+1, pNode->n-2,
                            SQLITE_TRANSIENT);
      }else{
        /* Translate JSON formatted string into raw text */
        u32 i;
        u32 n = pNode->n;
................................................................................
      return -1;
    }
    pParse->nAlloc = nNew;
    pParse->aNode = pNew;
  }
  p = &pParse->aNode[pParse->nNode];
  p->eType = (u8)eType;
  p->bRaw = 0;
  p->bBackslash = 0;
  p->n = n;
  p->zJContent = zContent;
  return pParse->nNode++;
}

/*
** Parse a single JSON value which begins at pParse->zJson[i].  Return the
................................................................................
      if( c!=']' ) return -1;
      break;
    }
    pParse->aNode[iThis].n = pParse->nNode - iThis - 1;
    return j+1;
  }else if( c=='"' ){
    /* Parse string */
    u8 bBackslash = 0;
    j = i+1;
    for(;;){
      c = pParse->zJson[j];
      if( c==0 ) return -1;
      if( c=='\\' ){
        c = pParse->zJson[++j];
        if( c==0 ) return -1;
        bBackslash = 1;
      }else if( c=='"' ){
        break;
      }
      j++;
    }
    jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]);
    if( bBackslash ) pParse->aNode[pParse->nNode-1].bBackslash = 1;
    return j+1;
  }else if( c=='n'
         && strncmp(pParse->zJson+i,"null",4)==0
         && !isalnum(pParse->zJson[i+4]) ){
    jsonParseAddNode(pParse, JSON_NULL, 0, 0);
    return i+4;
  }else if( c=='t'
................................................................................
    pParse->aNode = 0;
    pParse->nNode = 0;
    pParse->nAlloc = 0;
    return 1;
  }
  return 0;
}

/*
** Search along zPath to find the node specified.  Return a pointer
** to that node, or NULL if zPath is malformed or if there is no such
** node.
*/
static JsonNode *jsonLookup(JsonNode *pRoot, const char *zPath){
  u32 i, j;
................................................................................
    while( j<=pRoot->n ){
      if( pRoot[j].n==i+2
       && strncmp(&pRoot[j].zJContent[1],zPath,i)==0
      ){
        return jsonLookup(&pRoot[j+1], &zPath[i]);
      }
      j++;
      if( pRoot[j].eType==JSON_ARRAY || pRoot[j].eType==JSON_OBJECT ){
        j += pRoot[j].n;
      }
      j++;
    }
  }else if( zPath[0]=='[' && isdigit(zPath[1]) ){
    if( pRoot->eType!=JSON_ARRAY ) return 0;
    i = 0;
    zPath++;
    while( isdigit(zPath[0]) ){
      i = i + zPath[0] - '0';
      zPath++;
    }
    if( zPath[0]!=']' ) return 0;
    zPath++;
    j = 1;
    while( i>0 && j<=pRoot->n ){
      if( pRoot[j].eType==JSON_ARRAY || pRoot[j].eType==JSON_OBJECT ){
        j += pRoot[j].n;
      }
      j++;
      i--;
    }
    if( j<=pRoot->n ){
      return jsonLookup(&pRoot[j], zPath);
    }
  }
  return 0;
}

/****************************************************************************
** SQL functions used for testing and debugging
****************************************************************************/


/*
** The json_parse(JSON) function returns a string which describes
** a parse of the JSON provided.  Or it returns NULL if JSON is not
** well-formed.
*/
static void jsonParseFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  Json s;       /* Output string - not real JSON */
  JsonParse x;  /* The parse */
  u32 i;
  char zBuf[50];

  assert( argc==1 );
  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
  jsonInit(&s, context);
  for(i=0; i<x.nNode; i++){
    sqlite3_snprintf(sizeof(zBuf), zBuf, "node %u:\n", i);
    jsonAppend(&s, zBuf);
    sqlite3_snprintf(sizeof(zBuf), zBuf, "  type: %s\n",
                     jsonType[x.aNode[i].eType]);
    jsonAppend(&s, zBuf);
    if( x.aNode[i].eType>=JSON_INT ){
      sqlite3_snprintf(sizeof(zBuf), zBuf, "     n: %u\n", x.aNode[i].n);
      jsonAppend(&s, zBuf);
    }
    if( x.aNode[i].zJContent!=0 ){
      sqlite3_snprintf(sizeof(zBuf), zBuf, "  ofst: %u\n",
                       (u32)(x.aNode[i].zJContent - x.zJson));
      jsonAppend(&s, zBuf);
      jsonAppendRaw(&s, "  text: ", 8);
      jsonAppendRaw(&s, x.aNode[i].zJContent, x.aNode[i].n);
      jsonAppendRaw(&s, "\n", 1);
    }
  }
  sqlite3_free(x.aNode);
  jsonResult(&s);
}
................................................................................
  sqlite3_value **argv
){
  JsonParse x;  /* The parse */
  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
  sqlite3_result_int64(context, x.nNode);
  sqlite3_free(x.aNode);
}


/****************************************************************************
** SQL function implementations
****************************************************************************/

/*
** Implementation of the json_array(VALUE,...) function.  Return a JSON
................................................................................
    zPath = 0;
  }
  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0]))==0 ){
    if( x.nNode ){
      JsonNode *pNode = x.aNode;
      if( zPath ) pNode = jsonLookup(pNode, zPath);
      if( pNode->eType==JSON_ARRAY ){
        for(i=1; i<=pNode->n; i++, n++){
          if( pNode[i].eType==JSON_ARRAY || pNode[i].eType==JSON_OBJECT ){
            i += pNode[i].n;
          }
        }
      }
    }
    sqlite3_free(x.aNode);
  }
  sqlite3_result_int64(context, n);
}
................................................................................
      }
    }
  }
  jsonAppendRaw(&jx, "}", 1);
  jsonResult(&jx);
}




































/*
** json_type(JSON)
** json_type(JSON, PATH)
**
** Return the top-level "type" of a JSON string.  Return NULL if the
** input is not a well-formed JSON string.
................................................................................
     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
  } aFunc[] = {
    { "json_array",          -1,    jsonArrayFunc         },
    { "json_array_length",    1,    jsonArrayLengthFunc   },
    { "json_array_length",    2,    jsonArrayLengthFunc   },
    { "json_extract",         2,    jsonExtractFunc       },
    { "json_object",         -1,    jsonObjectFunc        },

    { "json_type",            1,    jsonTypeFunc          },
    { "json_type",            2,    jsonTypeFunc          },


    /* DEBUG and TESTING functions */
    { "json_parse",           1,    jsonParseFunc     },
    { "json_test1",           1,    jsonTest1Func     },
    { "json_nodecount",       1,    jsonNodeCountFunc },

  };
  SQLITE_EXTENSION_INIT2(pApi);
  (void)pzErrMsg;  /* Unused parameter */
  for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
    rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
                                 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
                                 aFunc[i].xFunc, 0, 0);
  }
  return rc;
}







>
>
>
>
>
>






|
<







 







>
>
>
>
>
>
>
>
>







 







|







 







>
>
>
>
>
>
>
>
>
>







 







|







 







|












<
|
>
|
>
>
|
>






<
|
>
|
>
>
|
|
|
>





|







 







|

|







 







|
<







 







|







|






|







 







>







 







<
|
<
<













<
|
<
<













>













|





|
<
<
|

<
<
<
<

<
<
<
|







 







>







 







|
<
|
<







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>



>




>










60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

80
81
82
83
84
85
86
..
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
...
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
...
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
...
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263

264
265
266
267
268
269
270
271
272
273
274
275
276

277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
...
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
...
432
433
434
435
436
437
438
439

440
441
442
443
444
445
446
...
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
...
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
...
619
620
621
622
623
624
625

626


627
628
629
630
631
632
633
634
635
636
637
638
639

640


641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674


675
676




677



678
679
680
681
682
683
684
685
...
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
...
792
793
794
795
796
797
798
799

800

801
802
803
804
805
806
807
...
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
...
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
/*
** Names of the various JSON types:
*/
static const char * const jsonType[] = {
  "null", "true", "false", "integer", "real", "text", "array", "object"
};

/* Bit values for the JsonNode.jnFlag field
*/
#define JNODE_RAW     0x01         /* Content is raw, not JSON encoded */
#define JNODE_ESCAPE  0x02         /* Content is text with \ escapes */
#define JNODE_REMOVE  0x04         /* Do not output */


/* A single node of parsed JSON
*/
typedef struct JsonNode JsonNode;
struct JsonNode {
  u8 eType;              /* One of the JSON_ type values */
  u8 jnFlags;            /* JNODE flags */

  u32 n;                 /* Bytes of content, or number of sub-nodes */
  const char *zJContent; /* JSON content */
};

/* A completely parsed JSON string
*/
typedef struct JsonParse JsonParse;
................................................................................
struct JsonParse {
  u32 nNode;         /* Number of slots of aNode[] used */
  u32 nAlloc;        /* Number of slots of aNode[] allocated */
  JsonNode *aNode;   /* Array of nodes containing the parse */
  const char *zJson; /* Original JSON string */
  u8 oom;            /* Set to true if out of memory */
};

/*
** Return the number of consecutive JsonNode slots need to represent
** the parsed JSON at pNode.  The minimum answer is 1.  For ARRAY and
** OBJECT types, the number might be larger.
*/
static u32 jsonSizeof(JsonNode *pNode){
  return pNode->eType>=JSON_ARRAY ? pNode->n+1 : 1;
}

/* Set the Json object to an empty string
*/
static void jsonZero(Json *p){
  p->zBuf = p->zSpace;
  p->nAlloc = sizeof(p->zSpace);
  p->nUsed = 0;
................................................................................
  jsonReset(p);
}

/* Enlarge pJson->zBuf so that it can hold at least N more bytes.
** Return zero on success.  Return non-zero on an OOM error
*/
static int jsonGrow(Json *p, u32 N){
  u64 nTotal = N<p->nAlloc ? p->nAlloc*2 : p->nAlloc+N+10;
  char *zNew;
  if( p->bStatic ){
    if( p->oom ) return SQLITE_NOMEM;
    zNew = sqlite3_malloc64(nTotal);
    if( zNew==0 ){
      jsonOom(p);
      return SQLITE_NOMEM;
................................................................................

/* Append a single character
*/
static void jsonAppendChar(Json *p, char c){
  if( p->nUsed>=p->nAlloc && jsonGrow(p,1)!=0 ) return;
  p->zBuf[p->nUsed++] = c;
}

/* Append a comma separator to the output buffer, if the previous
** character is not '[' or '{'.
*/
static void jsonAppendSeparator(Json *p){
  char c;
  if( p->nUsed==0 ) return;
  c = p->zBuf[p->nUsed-1];
  if( c!='[' && c!='{' ) jsonAppendChar(p, ',');
}

/* Append the N-byte string in zIn to the end of the Json string
** under construction.  Enclose the string in "..." and escape
** any double-quotes or backslash characters contained within the
** string.
*/
static void jsonAppendString(Json *p, const char *zIn, u32 N){
................................................................................

/*
** Convert the JsonNode pNode into a pure JSON string and
** append to pOut.  Subsubstructure is also included.  Return
** the number of JsonNode objects that are encoded.
*/
static int jsonRenderNode(JsonNode *pNode, Json *pOut){
  u32 j = 1;
  switch( pNode->eType ){
    case JSON_NULL: {
      jsonAppendRaw(pOut, "null", 4);
      break;
    }
    case JSON_TRUE: {
      jsonAppendRaw(pOut, "true", 4);
................................................................................
      break;
    }
    case JSON_FALSE: {
      jsonAppendRaw(pOut, "false", 5);
      break;
    }
    case JSON_STRING: {
      if( pNode->jnFlags & JNODE_RAW ){
        jsonAppendString(pOut, pNode->zJContent, pNode->n);
        break;
      }
      /* Fall through into the next case */
    }
    case JSON_REAL:
    case JSON_INT: {
      jsonAppendRaw(pOut, pNode->zJContent, pNode->n);
      break;
    }
    case JSON_ARRAY: {
      jsonAppendChar(pOut, '[');

      while( j<=pNode->n ){
        if( pNode[j].jnFlags & JNODE_REMOVE ){
          j += jsonSizeof(&pNode[j]);
        }else{
          jsonAppendSeparator(pOut);
          j += jsonRenderNode(&pNode[j], pOut);
        }
      }
      jsonAppendChar(pOut, ']');
      break;
    }
    case JSON_OBJECT: {
      jsonAppendChar(pOut, '{');

      while( j<=pNode->n ){
        if( pNode[j+1].jnFlags & JNODE_REMOVE ){
          j += 1 + jsonSizeof(&pNode[j+1]);
        }else{
          jsonAppendSeparator(pOut);
          jsonRenderNode(&pNode[j], pOut);
          jsonAppendChar(pOut, ':');
          j += 1 + jsonRenderNode(&pNode[j+1], pOut);
        }
      }
      jsonAppendChar(pOut, '}');
      break;
    }
  }
  return j;
}

/*
** Make the JsonNode the return value of the function.
*/
static void jsonReturn(JsonNode *pNode, sqlite3_context *pCtx){
  switch( pNode->eType ){
................................................................................
      if( z[0]=='-' ){ z++; }
      while( z[0]>='0' && z[0]<='9' ){ i = i*10 + *(z++) - '0'; }
      if( pNode->zJContent[0]=='-' ){ i = -i; }
      sqlite3_result_int64(pCtx, i);
      break;
    }
    case JSON_STRING: {
      if( pNode->jnFlags & JNODE_RAW ){
        sqlite3_result_text(pCtx, pNode->zJContent, pNode->n, SQLITE_TRANSIENT);
      }else if( (pNode->jnFlags & JNODE_ESCAPE)==0 ){
        /* JSON formatted without any backslash-escapes */
        sqlite3_result_text(pCtx, pNode->zJContent+1, pNode->n-2,
                            SQLITE_TRANSIENT);
      }else{
        /* Translate JSON formatted string into raw text */
        u32 i;
        u32 n = pNode->n;
................................................................................
      return -1;
    }
    pParse->nAlloc = nNew;
    pParse->aNode = pNew;
  }
  p = &pParse->aNode[pParse->nNode];
  p->eType = (u8)eType;
  p->jnFlags = 0;

  p->n = n;
  p->zJContent = zContent;
  return pParse->nNode++;
}

/*
** Parse a single JSON value which begins at pParse->zJson[i].  Return the
................................................................................
      if( c!=']' ) return -1;
      break;
    }
    pParse->aNode[iThis].n = pParse->nNode - iThis - 1;
    return j+1;
  }else if( c=='"' ){
    /* Parse string */
    u8 jnFlags = 0;
    j = i+1;
    for(;;){
      c = pParse->zJson[j];
      if( c==0 ) return -1;
      if( c=='\\' ){
        c = pParse->zJson[++j];
        if( c==0 ) return -1;
        jnFlags = JNODE_ESCAPE;
      }else if( c=='"' ){
        break;
      }
      j++;
    }
    jsonParseAddNode(pParse, JSON_STRING, j+1-i, &pParse->zJson[i]);
    pParse->aNode[pParse->nNode-1].jnFlags = jnFlags;
    return j+1;
  }else if( c=='n'
         && strncmp(pParse->zJson+i,"null",4)==0
         && !isalnum(pParse->zJson[i+4]) ){
    jsonParseAddNode(pParse, JSON_NULL, 0, 0);
    return i+4;
  }else if( c=='t'
................................................................................
    pParse->aNode = 0;
    pParse->nNode = 0;
    pParse->nAlloc = 0;
    return 1;
  }
  return 0;
}

/*
** Search along zPath to find the node specified.  Return a pointer
** to that node, or NULL if zPath is malformed or if there is no such
** node.
*/
static JsonNode *jsonLookup(JsonNode *pRoot, const char *zPath){
  u32 i, j;
................................................................................
    while( j<=pRoot->n ){
      if( pRoot[j].n==i+2
       && strncmp(&pRoot[j].zJContent[1],zPath,i)==0
      ){
        return jsonLookup(&pRoot[j+1], &zPath[i]);
      }
      j++;

      j += jsonSizeof(&pRoot[j]);


    }
  }else if( zPath[0]=='[' && isdigit(zPath[1]) ){
    if( pRoot->eType!=JSON_ARRAY ) return 0;
    i = 0;
    zPath++;
    while( isdigit(zPath[0]) ){
      i = i + zPath[0] - '0';
      zPath++;
    }
    if( zPath[0]!=']' ) return 0;
    zPath++;
    j = 1;
    while( i>0 && j<=pRoot->n ){

      j += jsonSizeof(&pRoot[j]);


      i--;
    }
    if( j<=pRoot->n ){
      return jsonLookup(&pRoot[j], zPath);
    }
  }
  return 0;
}

/****************************************************************************
** SQL functions used for testing and debugging
****************************************************************************/

#ifdef SQLITE_DEBUG
/*
** The json_parse(JSON) function returns a string which describes
** a parse of the JSON provided.  Or it returns NULL if JSON is not
** well-formed.
*/
static void jsonParseFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  Json s;       /* Output string - not real JSON */
  JsonParse x;  /* The parse */
  u32 i;
  char zBuf[100];

  assert( argc==1 );
  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
  jsonInit(&s, context);
  for(i=0; i<x.nNode; i++){
    sqlite3_snprintf(sizeof(zBuf), zBuf, "node %3u: %7s n=%d\n",


                     i, jsonType[x.aNode[i].eType], x.aNode[i].n);
    jsonAppend(&s, zBuf);




    if( x.aNode[i].zJContent!=0 ){



      jsonAppendRaw(&s, "    text: ", 10);
      jsonAppendRaw(&s, x.aNode[i].zJContent, x.aNode[i].n);
      jsonAppendRaw(&s, "\n", 1);
    }
  }
  sqlite3_free(x.aNode);
  jsonResult(&s);
}
................................................................................
  sqlite3_value **argv
){
  JsonParse x;  /* The parse */
  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
  sqlite3_result_int64(context, x.nNode);
  sqlite3_free(x.aNode);
}
#endif /* SQLITE_DEBUG */

/****************************************************************************
** SQL function implementations
****************************************************************************/

/*
** Implementation of the json_array(VALUE,...) function.  Return a JSON
................................................................................
    zPath = 0;
  }
  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0]))==0 ){
    if( x.nNode ){
      JsonNode *pNode = x.aNode;
      if( zPath ) pNode = jsonLookup(pNode, zPath);
      if( pNode->eType==JSON_ARRAY ){
        for(i=1; i<=pNode->n; n++){

          i += jsonSizeof(&pNode[i]);

        }
      }
    }
    sqlite3_free(x.aNode);
  }
  sqlite3_result_int64(context, n);
}
................................................................................
      }
    }
  }
  jsonAppendRaw(&jx, "}", 1);
  jsonResult(&jx);
}


/*
** json_remove(JSON, PATH, ...)
**
** Remove the named elements from JSON and return the result.  Ill-formed
** PATH arguments are silently ignored.  If JSON is ill-formed, then NULL
** is returned.
*/
static void jsonRemoveFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  JsonParse x;          /* The parse */
  JsonNode *pNode;
  const char *zPath;
  u32 i;

  if( argc<1 ) return;
  if( jsonParse(&x, (const char*)sqlite3_value_text(argv[0])) ) return;
  if( x.nNode ){
    for(i=1; i<argc; i++){
      zPath = (const char*)sqlite3_value_text(argv[i]);
      if( zPath==0 ) continue;
      if( zPath[0]!='$' ) continue;
      pNode = jsonLookup(x.aNode, &zPath[1]);
      if( pNode ) pNode->jnFlags |= JNODE_REMOVE;
    }
    if( (x.aNode[0].jnFlags & JNODE_REMOVE)==0 ){
      jsonReturn(x.aNode, context);
    }
  }
  sqlite3_free(x.aNode);
}

/*
** json_type(JSON)
** json_type(JSON, PATH)
**
** Return the top-level "type" of a JSON string.  Return NULL if the
** input is not a well-formed JSON string.
................................................................................
     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
  } aFunc[] = {
    { "json_array",          -1,    jsonArrayFunc         },
    { "json_array_length",    1,    jsonArrayLengthFunc   },
    { "json_array_length",    2,    jsonArrayLengthFunc   },
    { "json_extract",         2,    jsonExtractFunc       },
    { "json_object",         -1,    jsonObjectFunc        },
    { "json_remove",         -1,    jsonRemoveFunc        },
    { "json_type",            1,    jsonTypeFunc          },
    { "json_type",            2,    jsonTypeFunc          },

#if SQLITE_DEBUG
    /* DEBUG and TESTING functions */
    { "json_parse",           1,    jsonParseFunc     },
    { "json_test1",           1,    jsonTest1Func     },
    { "json_nodecount",       1,    jsonNodeCountFunc },
#endif
  };
  SQLITE_EXTENSION_INIT2(pApi);
  (void)pzErrMsg;  /* Unused parameter */
  for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
    rc = sqlite3_create_function(db, aFunc[i].zName, aFunc[i].nArg,
                                 SQLITE_UTF8 | SQLITE_DETERMINISTIC, 0,
                                 aFunc[i].xFunc, 0, 0);
  }
  return rc;
}