Add the json_object() function.

FossilOrigin-Name: 414a95f3b79359f167858b2325fd2be223569c66
This commit is contained in:
drh 2015-08-12 17:23:34 +00:00
parent 5fa5c10784
commit 2032d60793
4 changed files with 88 additions and 13 deletions

View File

@ -194,7 +194,70 @@ static void jsonArrayFunc(
}
}
jsonAppendRaw(&jx, "]", 1);
jsonResult(&jx);
jsonResult(&jx);
}
/*
** Implementation of the json_object(NAME,VALUE,...) function. Return a JSON
** object that contains all name/value given in arguments. Or if any name
** is not a string or if any value is a BLOB, throw an error.
*/
static void jsonObjectFunc(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
int i;
Json jx;
char cSep = '{';
const char *z;
u32 n;
if( argc&1 ){
sqlite3_result_error(context, "json_object() requires an even number "
"of arguments", -1);
return;
}
jsonInit(&jx, context);
for(i=0; i<argc; i+=2){
jsonAppendRaw(&jx, &cSep, 1);
cSep = ',';
if( sqlite3_value_type(argv[i])!=SQLITE_TEXT ){
sqlite3_result_error(context, "json_object() labels must be TEXT", -1);
jsonZero(&jx);
return;
}
z = (const char*)sqlite3_value_text(argv[i]);
n = (u32)sqlite3_value_bytes(argv[i]);
jsonAppendString(&jx, z, n);
jsonAppendRaw(&jx, ":", 1);
switch( sqlite3_value_type(argv[i+1]) ){
case SQLITE_NULL: {
jsonAppendRaw(&jx, "null", 4);
break;
}
case SQLITE_INTEGER:
case SQLITE_FLOAT: {
z = (const char*)sqlite3_value_text(argv[i+1]);
n = (u32)sqlite3_value_bytes(argv[i+1]);
jsonAppendRaw(&jx, z, n);
break;
}
case SQLITE_TEXT: {
z = (const char*)sqlite3_value_text(argv[i+1]);
n = (u32)sqlite3_value_bytes(argv[i+1]);
jsonAppendString(&jx, z, n);
break;
}
default: {
jsonZero(&jx);
sqlite3_result_error(context, "JSON cannot hold BLOB values", -1);
return;
}
}
}
jsonAppendRaw(&jx, "}", 1);
jsonResult(&jx);
}
#ifdef _WIN32
@ -212,7 +275,8 @@ int sqlite3_json_init(
int nArg;
void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
} aFunc[] = {
{ "json_array", -1, jsonArrayFunc },
{ "json_array", -1, jsonArrayFunc },
{ "json_object", -1, jsonObjectFunc },
};
SQLITE_EXTENSION_INIT2(pApi);
(void)pzErrMsg; /* Unused parameter */

View File

@ -1,5 +1,5 @@
C Begin\sadding\san\sextension\sthat\sprovides\sJSON\sSQL\sfunctions.
D 2015-08-12T16:49:40.032
C Add\sthe\sjson_object()\sfunction.
D 2015-08-12T17:23:34.617
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 7669f34c487f5b328de6b508f374ee1e56558bb0
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@ -192,7 +192,7 @@ F ext/misc/eval.c f971962e92ebb8b0a4e6b62949463ee454d88fa2
F ext/misc/fileio.c d4171c815d6543a9edef8308aab2951413cd8d0f
F ext/misc/fuzzer.c 4c84635c71c26cfa7c2e5848cf49fe2d2cfcd767
F ext/misc/ieee754.c b0362167289170627659e84173f5d2e8fee8566e
F ext/misc/json.c 172375d024d799aa6506b8a329128ee0b67e1a94
F ext/misc/json.c 9df3054186b0b84dc7b3ce7e8694ebad4fdc5f7c
F ext/misc/nextchar.c 35c8b8baacb96d92abbb34a83a997b797075b342
F ext/misc/percentile.c bcbee3c061b884eccb80e21651daaae8e1e43c63
F ext/misc/regexp.c af92cdaa5058fcec1451e49becc7ba44dba023dc
@ -808,7 +808,7 @@ F test/journal3.test ff8af941f9e06161d3db1b46bb9f965ff0e7f307
F test/jrnlmode.test 7864d59cf7f6e552b9b99ba0f38acd167edc10fa
F test/jrnlmode2.test 81610545a4e6ed239ea8fa661891893385e23a1d
F test/jrnlmode3.test 556b447a05be0e0963f4311e95ab1632b11c9eaa
F test/json1.test f09587c250426291faff60cacc5aae759216dbd7
F test/json1.test 950ed4e8deb8ad4c10bd4fbc858eb54143de9867
F test/keyword1.test 37ef6bba5d2ed5b07ecdd6810571de2956599dff
F test/lastinsert.test 42e948fd6442f07d60acbd15d33fb86473e0ef63
F test/laststmtchanges.test ae613f53819206b3222771828d024154d51db200
@ -1374,10 +1374,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh 48bd54594752d5be3337f12c72f28d2080cb630b
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P f7682435278419829a46bb4cc9b5625d46549e22
R 400e916c0bc56219e72f7a73e2b83997
T *branch * json
T *sym-json *
T -sym-trunk *
P dde8afdd8dba1d92560326dca7c1cdfedbe5e070
R 75cab18873f077d21ed527f3798bc75c
U drh
Z 44e10dc14fd52cdaf44120cd6aeb4786
Z 14906f22419c39d5dff2c5387892aa6a

View File

@ -1 +1 @@
dde8afdd8dba1d92560326dca7c1cdfedbe5e070
414a95f3b79359f167858b2325fd2be223569c66

View File

@ -37,4 +37,18 @@ do_execsql_test json1-1.4 {
99);
} {[-9223372036854775808,9223372036854775807,0,1,-1,0.0,1.0,-1.0,-1.0e+99,2.0e+100,"one","two","three",4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,null,21,22,23,24,25,26,27,28,29,30,31,"abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ",99]}
do_execsql_test json1-2.1 {
SELECT json_object('a',1,'b',2.5,'c',null,'d','String Test');
} {{{"a":1,"b":2.5,"c":null,"d":"String Test"}}}
do_catchsql_test json1-2.2 {
SELECT json_object('a',1,2,2.5);
} {1 {json_object() labels must be TEXT}}
do_catchsql_test json1-2.3 {
SELECT json_object('a',1,'b');
} {1 {json_object() requires an even number of arguments}}
do_catchsql_test json1-2.4 {
SELECT json_object('a',1,'b',x'abcd');
} {1 {JSON cannot hold BLOB values}}
finish_test