Index: library/mongodb/mongoAPI.decls =================================================================== diff -u -r9e8610ba86e16a518747bd25907d04b2aaf19dd5 -re3e8f375df1313380cd13a6cb97028569f99d729 --- library/mongodb/mongoAPI.decls (.../mongoAPI.decls) (revision 9e8610ba86e16a518747bd25907d04b2aaf19dd5) +++ library/mongodb/mongoAPI.decls (.../mongoAPI.decls) (revision e3e8f375df1313380cd13a6cb97028569f99d729) @@ -15,9 +15,14 @@ mongoc_gridfs_t 1 } -cmd json NsfMongoJson { +# produce json string from triples +cmd json::generate NsfMongoJsonGenerate { {-argName "list" -required 1 -type tclobj} } +# parse json string into triples +cmd json::parse NsfMongoJsonParse { + {-argName "json" -required 1 -type tclobj} +} cmd close NsfMongoClose { {-argName "conn" -required 1 -type mongoc_client_t -withObj 1} Index: library/mongodb/mongoAPI.h =================================================================== diff -u -r62beaf105e751bb647d88153700253b4c6723127 -re3e8f375df1313380cd13a6cb97028569f99d729 --- library/mongodb/mongoAPI.h (.../mongoAPI.h) (revision 62beaf105e751bb647d88153700253b4c6723127) +++ library/mongodb/mongoAPI.h (.../mongoAPI.h) (revision e3e8f375df1313380cd13a6cb97028569f99d729) @@ -85,7 +85,7 @@ /* just to define the symbol */ -static Nsf_methodDefinition method_definitions[30]; +static Nsf_methodDefinition method_definitions[31]; static const char *method_command_namespace_names[] = { "::mongo" @@ -142,8 +142,10 @@ NSF_nonnull(2) NSF_nonnull(4); static int NsfMongoGridFileSeekStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST* objv) NSF_nonnull(2) NSF_nonnull(4); -static int NsfMongoJsonStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST* objv) +static int NsfMongoJsonGenerateStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST* objv) NSF_nonnull(2) NSF_nonnull(4); +static int NsfMongoJsonParseStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST* objv) + NSF_nonnull(2) NSF_nonnull(4); static int NsfMongoRunCmdStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST* objv) NSF_nonnull(2) NSF_nonnull(4); static int NsfMongoStatusStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST* objv) @@ -201,8 +203,10 @@ NSF_nonnull(1) NSF_nonnull(2); static int NsfMongoGridFileSeek(Tcl_Interp *interp, mongoc_gridfs_file_t *gridfilePtr, int offset) NSF_nonnull(1) NSF_nonnull(2); -static int NsfMongoJson(Tcl_Interp *interp, Tcl_Obj *list) +static int NsfMongoJsonGenerate(Tcl_Interp *interp, Tcl_Obj *list) NSF_nonnull(1) NSF_nonnull(2); +static int NsfMongoJsonParse(Tcl_Interp *interp, Tcl_Obj *json) + NSF_nonnull(1) NSF_nonnull(2); static int NsfMongoRunCmd(Tcl_Interp *interp, int withNocomplain, mongoc_client_t *connPtr, const char *db, Tcl_Obj *cmd) NSF_nonnull(1) NSF_nonnull(3) NSF_nonnull(4) NSF_nonnull(5); static int NsfMongoStatus(Tcl_Interp *interp, mongoc_client_t *connPtr, Tcl_Obj *connObj) @@ -235,7 +239,8 @@ NsfMongoGridFileOpenIdx, NsfMongoGridFileReadIdx, NsfMongoGridFileSeekIdx, - NsfMongoJsonIdx, + NsfMongoJsonGenerateIdx, + NsfMongoJsonParseIdx, NsfMongoRunCmdIdx, NsfMongoStatusIdx } NsfMethods; @@ -804,22 +809,38 @@ } static int -NsfMongoJsonStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST* objv) { +NsfMongoJsonGenerateStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST* objv) { (void)clientData; if (objc != 2) { return NsfArgumentError(interp, "wrong # of arguments:", - method_definitions[NsfMongoJsonIdx].paramDefs, + method_definitions[NsfMongoJsonGenerateIdx].paramDefs, NULL, objv[0]); } - return NsfMongoJson(interp, objv[1]); + return NsfMongoJsonGenerate(interp, objv[1]); } static int +NsfMongoJsonParseStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST* objv) { + (void)clientData; + + + + if (objc != 2) { + return NsfArgumentError(interp, "wrong # of arguments:", + method_definitions[NsfMongoJsonParseIdx].paramDefs, + NULL, objv[0]); + } + + return NsfMongoJsonParse(interp, objv[1]); + +} + +static int NsfMongoRunCmdStub(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *CONST* objv) { ParseContext pc; (void)clientData; @@ -862,7 +883,7 @@ } } -static Nsf_methodDefinition method_definitions[30] = { +static Nsf_methodDefinition method_definitions[31] = { {"::mongo::collection::close", NsfCollectionCloseStub, 1, { {"collection", NSF_ARG_REQUIRED, 1, Nsf_ConvertTo_Pointer, NULL,NULL,"mongoc_collection_t",NULL,NULL,NULL,NULL,NULL}} }, @@ -983,9 +1004,12 @@ {"gridfile", NSF_ARG_REQUIRED, 1, Nsf_ConvertTo_Pointer, NULL,NULL,"mongoc_gridfs_file_t",NULL,NULL,NULL,NULL,NULL}, {"offset", NSF_ARG_REQUIRED, 1, Nsf_ConvertTo_Int32, NULL,NULL,"int32",NULL,NULL,NULL,NULL,NULL}} }, -{"::mongo::json", NsfMongoJsonStub, 1, { +{"::mongo::json::generate", NsfMongoJsonGenerateStub, 1, { {"list", NSF_ARG_REQUIRED, 1, Nsf_ConvertTo_Tclobj, NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}} }, +{"::mongo::json::parse", NsfMongoJsonParseStub, 1, { + {"json", NSF_ARG_REQUIRED, 1, Nsf_ConvertTo_Tclobj, NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}} +}, {"::mongo::run", NsfMongoRunCmdStub, 4, { {"-nocomplain", 0, 0, Nsf_ConvertTo_String, NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}, {"conn", NSF_ARG_REQUIRED, 1, Nsf_ConvertTo_Pointer, NULL,NULL,"mongoc_client_t",NULL,NULL,NULL,NULL,NULL}, Index: library/mongodb/nsfmongo.c =================================================================== diff -u -r4ca97cad5be85ed8ed29ec29189812215dbe5693 -re3e8f375df1313380cd13a6cb97028569f99d729 --- library/mongodb/nsfmongo.c (.../nsfmongo.c) (revision 4ca97cad5be85ed8ed29ec29189812215dbe5693) +++ library/mongodb/nsfmongo.c (.../nsfmongo.c) (revision e3e8f375df1313380cd13a6cb97028569f99d729) @@ -513,12 +513,12 @@ * Define the api functions ***********************************************************************/ /* -cmd json NsfMongoJson { +cmd json::generate NsfMongoJsonGenerate { {-argName "list" -required 1 -type tclobj} } */ static int -NsfMongoJson(Tcl_Interp *interp, Tcl_Obj *listObj) { +NsfMongoJsonGenerate(Tcl_Interp *interp, Tcl_Obj *listObj) { bson_t list, *listPtr = &list; size_t length; char *jsonString; @@ -545,7 +545,31 @@ return result; } +/* +cmd json::parse NsfMongoJsonParse { + {-argName "json" -required 1 -type tclobj} +} +*/ +static int +NsfMongoJsonParse(Tcl_Interp *interp, Tcl_Obj *jsonObj) { + bson_t bson, *bsonPtr = &bson; + const char *jsonString; + int result, jsonLength; + bson_error_t bsonError; + jsonString = Tcl_GetStringFromObj(jsonObj, &jsonLength); + + if (bson_init_from_json (bsonPtr, jsonString,jsonLength, &bsonError) == true) { + Tcl_SetObjResult(interp, BsonToList(interp, bsonPtr, 0)); + bson_destroy( bsonPtr ); + result = TCL_OK; + } else { + result = NsfPrintError(interp, "mongo::json::parse: error: %s", bsonError.message); + } + + return result; +} + /* cmd close NsfMongoClose { {-argName "conn" -required 1 -type mongoc_client_t -withObj 1}