Fossil SCM
Changed the new json_deserialize_array() interface to make it easier to call correctly.
Commit
765acbc080804aa61066b9c15a7bb355b883053784258a1d4f477ad545f592f1
Parent
dac496b300b0ca1…
1 file changed
+5
-5
M
src/db.c
+5
-5
| --- src/db.c | ||
| +++ src/db.c | ||
| @@ -4540,11 +4540,11 @@ | ||
| 4540 | 4540 | if( g.argc!=3 ) usage("FILE"); |
| 4541 | 4541 | blob_zero(&json); |
| 4542 | 4542 | sqlite3_open(":memory:", &g.db); |
| 4543 | 4543 | if( blob_read_from_file(&json, g.argv[2], ExtFILE)>=0 ) { |
| 4544 | 4544 | size_t nValues; |
| 4545 | - char** azValues = json_deserialize_array(&nValues, blob_str(&json)); | |
| 4545 | + const char** azValues = json_deserialize_array(&nValues, blob_str(&json)); | |
| 4546 | 4546 | int i; |
| 4547 | 4547 | for( i=0; i<nValues; ++i ){ |
| 4548 | 4548 | fossil_print("JSON[%d] = \"%s\"\n", i, azValues[i]); |
| 4549 | 4549 | } |
| 4550 | 4550 | } |
| @@ -4553,15 +4553,15 @@ | ||
| 4553 | 4553 | } |
| 4554 | 4554 | |
| 4555 | 4555 | /* |
| 4556 | 4556 | ** Deserializes the passed JSON array of strings as a C array of C strings. |
| 4557 | 4557 | */ |
| 4558 | -char** json_deserialize_array(size_t* pnValues, const char* zJSON){ | |
| 4559 | - char** azValues; | |
| 4560 | - size_t i=0, n; | |
| 4558 | +const char** json_deserialize_array(size_t* pnValues, const char* zJSON){ | |
| 4559 | + const char** azValues; | |
| 4560 | + size_t i = 0, n = db_int(0, "SELECT COUNT(*) FROM json_each(%Q)", zJSON); | |
| 4561 | 4561 | Stmt q; |
| 4562 | - n = *pnValues = db_int(0, "SELECT COUNT(*) FROM json_each(%Q)", zJSON); | |
| 4562 | + if(pnValues) *pnValues = n; | |
| 4563 | 4563 | azValues = fossil_malloc(sizeof(char*) * (n+1)); |
| 4564 | 4564 | db_prepare(&q, "SELECT json_each.value FROM json_each(%Q)", zJSON); |
| 4565 | 4565 | while( (i<n) && (db_step(&q)==SQLITE_ROW) ){ |
| 4566 | 4566 | azValues[i++] = fossil_strdup(db_column_text(&q, 0)); |
| 4567 | 4567 | } |
| 4568 | 4568 |
| --- src/db.c | |
| +++ src/db.c | |
| @@ -4540,11 +4540,11 @@ | |
| 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 | } |
| @@ -4553,15 +4553,15 @@ | |
| 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 |
| --- src/db.c | |
| +++ src/db.c | |
| @@ -4540,11 +4540,11 @@ | |
| 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 | const 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 | } |
| @@ -4553,15 +4553,15 @@ | |
| 4553 | } |
| 4554 | |
| 4555 | /* |
| 4556 | ** Deserializes the passed JSON array of strings as a C array of C strings. |
| 4557 | */ |
| 4558 | const char** json_deserialize_array(size_t* pnValues, const char* zJSON){ |
| 4559 | const char** azValues; |
| 4560 | size_t i = 0, n = db_int(0, "SELECT COUNT(*) FROM json_each(%Q)", zJSON); |
| 4561 | Stmt q; |
| 4562 | if(pnValues) *pnValues = n; |
| 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 |