Fossil SCM

Added a JSON array deserializer function and a test-json-deserialize-array command to test it.

wyoung 2021-06-22 05:25 fossil-spawn
Commit dac496b300b0ca1d4f960e5e23ccada268f990f8a74c9766ec3ede64573b8f62
1 file changed +42
+42
--- src/db.c
+++ src/db.c
@@ -4525,10 +4525,52 @@
45254525
return ret;
45264526
}else{
45274527
fossil_fatal("SQLite error: %s", sqlite3_errmsg(g.db));
45284528
}
45294529
}
4530
+
4531
+/*
4532
+** COMMAND: test-json-deserialize-array
4533
+**
4534
+** Given the name of a file containing a JSON-encoded array of strings,
4535
+** parses it and prints one element per line. The file name can be
4536
+** "-" to read from stdin.
4537
+*/
4538
+void test_json_deserialize_array_cmd(void){
4539
+ Blob json;
4540
+ if( g.argc!=3 ) usage("FILE");
4541
+ blob_zero(&json);
4542
+ sqlite3_open(":memory:", &g.db);
4543
+ if( blob_read_from_file(&json, g.argv[2], ExtFILE)>=0 ) {
4544
+ size_t nValues;
4545
+ char** azValues = json_deserialize_array(&nValues, blob_str(&json));
4546
+ int i;
4547
+ for( i=0; i<nValues; ++i ){
4548
+ fossil_print("JSON[%d] = \"%s\"\n", i, azValues[i]);
4549
+ }
4550
+ }
4551
+ sqlite3_close(g.db);
4552
+ g.db = 0;
4553
+}
4554
+
4555
+/*
4556
+** Deserializes the passed JSON array of strings as a C array of C strings.
4557
+*/
4558
+char** json_deserialize_array(size_t* pnValues, const char* zJSON){
4559
+ char** azValues;
4560
+ size_t i=0, n;
4561
+ Stmt q;
4562
+ n = *pnValues = db_int(0, "SELECT COUNT(*) FROM json_each(%Q)", zJSON);
4563
+ azValues = fossil_malloc(sizeof(char*) * (n+1));
4564
+ db_prepare(&q, "SELECT json_each.value FROM json_each(%Q)", zJSON);
4565
+ while( (i<n) && (db_step(&q)==SQLITE_ROW) ){
4566
+ azValues[i++] = fossil_strdup(db_column_text(&q, 0));
4567
+ }
4568
+ azValues[i] = 0;
4569
+ db_finalize(&q);
4570
+ return azValues;
4571
+}
45304572
45314573
/*
45324574
** COMMAND: test-without-rowid
45334575
**
45344576
** Usage: %fossil test-without-rowid FILENAME...
45354577
--- src/db.c
+++ src/db.c
@@ -4525,10 +4525,52 @@
4525 return ret;
4526 }else{
4527 fossil_fatal("SQLite error: %s", sqlite3_errmsg(g.db));
4528 }
4529 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4530
4531 /*
4532 ** COMMAND: test-without-rowid
4533 **
4534 ** Usage: %fossil test-without-rowid FILENAME...
4535
--- src/db.c
+++ src/db.c
@@ -4525,10 +4525,52 @@
4525 return ret;
4526 }else{
4527 fossil_fatal("SQLite error: %s", sqlite3_errmsg(g.db));
4528 }
4529 }
4530
4531 /*
4532 ** COMMAND: test-json-deserialize-array
4533 **
4534 ** Given the name of a file containing a JSON-encoded array of strings,
4535 ** parses it and prints one element per line. The file name can be
4536 ** "-" to read from stdin.
4537 */
4538 void test_json_deserialize_array_cmd(void){
4539 Blob json;
4540 if( g.argc!=3 ) usage("FILE");
4541 blob_zero(&json);
4542 sqlite3_open(":memory:", &g.db);
4543 if( blob_read_from_file(&json, g.argv[2], ExtFILE)>=0 ) {
4544 size_t nValues;
4545 char** azValues = json_deserialize_array(&nValues, blob_str(&json));
4546 int i;
4547 for( i=0; i<nValues; ++i ){
4548 fossil_print("JSON[%d] = \"%s\"\n", i, azValues[i]);
4549 }
4550 }
4551 sqlite3_close(g.db);
4552 g.db = 0;
4553 }
4554
4555 /*
4556 ** Deserializes the passed JSON array of strings as a C array of C strings.
4557 */
4558 char** json_deserialize_array(size_t* pnValues, const char* zJSON){
4559 char** azValues;
4560 size_t i=0, n;
4561 Stmt q;
4562 n = *pnValues = db_int(0, "SELECT COUNT(*) FROM json_each(%Q)", zJSON);
4563 azValues = fossil_malloc(sizeof(char*) * (n+1));
4564 db_prepare(&q, "SELECT json_each.value FROM json_each(%Q)", zJSON);
4565 while( (i<n) && (db_step(&q)==SQLITE_ROW) ){
4566 azValues[i++] = fossil_strdup(db_column_text(&q, 0));
4567 }
4568 azValues[i] = 0;
4569 db_finalize(&q);
4570 return azValues;
4571 }
4572
4573 /*
4574 ** COMMAND: test-without-rowid
4575 **
4576 ** Usage: %fossil test-without-rowid FILENAME...
4577

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button