Fossil

Diff
Login

Differences From Artifact [81c701a0a2]:

To Artifact [439b11e466]:


4538
4539
4540
4541
4542
4543
4544
4545

4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560



4561
4562

4563
4564
4565
4566
4567
4568
4569
4538
4539
4540
4541
4542
4543
4544

4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557



4558
4559
4560
4561

4562
4563
4564
4565
4566
4567
4568
4569







-
+












-
-
-
+
+
+

-
+







void test_json_deserialize_array_cmd(void){
  Blob json;
  if( g.argc!=3 ) usage("FILE");
  blob_zero(&json);
  sqlite3_open(":memory:", &g.db);
  if( blob_read_from_file(&json, g.argv[2], ExtFILE)>=0 ) {
    size_t nValues;
    char** azValues = json_deserialize_array(&nValues, blob_str(&json));
    const char** azValues = json_deserialize_array(&nValues, blob_str(&json));
    int i;
    for( i=0; i<nValues; ++i ){
      fossil_print("JSON[%d] = \"%s\"\n", i, azValues[i]);
    }
  }
  sqlite3_close(g.db);
  g.db = 0;
}

/*
** Deserializes the passed JSON array of strings as a C array of C strings.
*/
char** json_deserialize_array(size_t* pnValues, const char* zJSON){
  char** azValues;
  size_t i=0, n;
const char** json_deserialize_array(size_t* pnValues, const char* zJSON){
  const char** azValues;
  size_t i = 0, n = db_int(0, "SELECT COUNT(*) FROM json_each(%Q)", zJSON);
  Stmt q;
  n = *pnValues = db_int(0, "SELECT COUNT(*) FROM json_each(%Q)", zJSON);
  if(pnValues) *pnValues = n;
  azValues = fossil_malloc(sizeof(char*) * (n+1));
  db_prepare(&q, "SELECT json_each.value FROM json_each(%Q)", zJSON);
  while( (i<n) && (db_step(&q)==SQLITE_ROW) ){
    azValues[i++] = fossil_strdup(db_column_text(&q, 0));
  }
  azValues[i] = 0;
  db_finalize(&q);