Fossil

Check-in [dac496b300]
Login

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

Overview
Comment:Added a JSON array deserializer function and a test-json-deserialize-array command to test it.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | fossil-spawn
Files: files | file ages | folders
SHA3-256: dac496b300b0ca1d4f960e5e23ccada268f990f8a74c9766ec3ede64573b8f62
User & Date: wyoung 2021-06-22 05:25:50.648
Context
2021-06-22
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
03:52
Renamed the test-* wrapper for the new JSON array serializer to better match its internal implementation function. NFC. check-in: 06d27250d5 user: wyoung tags: fossil-spawn
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/db.c.
4523
4524
4525
4526
4527
4528
4529










































4530
4531
4532
4533
4534
4535
4536
    db_finalize(&q);
    sqlite3_close(db);
    return ret;
  }else{ 
    fossil_fatal("SQLite error: %s", sqlite3_errmsg(g.db));
  }
}











































/*
** COMMAND: test-without-rowid
**
** Usage: %fossil test-without-rowid FILENAME...
**
** Change the Fossil repository FILENAME to make use of the WITHOUT ROWID







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







4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
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
4570
4571
4572
4573
4574
4575
4576
4577
4578
    db_finalize(&q);
    sqlite3_close(db);
    return ret;
  }else{ 
    fossil_fatal("SQLite error: %s", sqlite3_errmsg(g.db));
  }
}

/*
** COMMAND: test-json-deserialize-array
**
** Given the name of a file containing a JSON-encoded array of strings,
** parses it and prints one element per line.  The file name can be
** "-" to read from stdin.
*/
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);
  return azValues;
}

/*
** COMMAND: test-without-rowid
**
** Usage: %fossil test-without-rowid FILENAME...
**
** Change the Fossil repository FILENAME to make use of the WITHOUT ROWID