Fossil

Check-in [765acbc080]
Login

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

Overview
Comment:Changed the new json_deserialize_array() interface to make it easier to call correctly.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | fossil-spawn
Files: files | file ages | folders
SHA3-256: 765acbc080804aa61066b9c15a7bb355b883053784258a1d4f477ad545f592f1
User & Date: wyoung 2021-06-22 07:52:29.757
Context
2021-06-22
07:56
Constness fix to the new fossil_spawn() function to avoid compiler complaints on current macOS. check-in: eefb8e64ed user: wyoung tags: fossil-spawn
07:52
Changed the new json_deserialize_array() interface to make it easier to call correctly. check-in: 765acbc080 user: wyoung tags: fossil-spawn
05:25
Added a JSON array deserializer function and a test-json-deserialize-array command to test it. check-in: dac496b300 user: wyoung tags: fossil-spawn
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/db.c.
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));
    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;
  Stmt q;
  n = *pnValues = db_int(0, "SELECT COUNT(*) FROM json_each(%Q)", zJSON);
  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);







|












|
|
|

|







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;
    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.
*/
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;
  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);