I'm having difficulties converting a bson document to a json string using bsoncxx. The bsoncxx::to_json function return an "invalid"/corrupted std::string object.. I can't read the character in it, and it crash when the std::string is destructed..
I've rebuild everything: mongoc,libbson, mongocxx, etc...
Here's a sample code :
bsoncxx::builder::basic::document doc{};
doc.append(bsoncxx::builder::basic::kvp("test", 1));
auto string = bsoncxx::to_json(doc);
I can't extract the data from the string, because std::end(string) crash with a "read access violation" when I try to copy the content with std::copy...
I'm using mongodb everywhere in the program and accessing bson documents and everything works fine. I tried to call bsoncxx::to_json on an already existing bson document returned by a mongodb query but it had the same behavior...
I'm trying to stream a byte array ( plus, some information like how to decode the byte array ) using boost tcp sockets to a nodejs program so I thought I could simply create a document with a "binary field", convert it to json and stream it over the tcp socket...
Anyone knows how I could do that, either by fixing the bsoncxx::to_json, or by using something else ?
thanks
Edit:
For information, I'm compiling with Visual Studio 2015 on Windows 10 x64
I fixed the problem by using libbson directly, instead of bsoncxx... I took at look a the code in bsoncxx::to_json, and extracted that :
std::string ret;
bson_t bson;
auto view = document.view();
bson_init_static(&bson, view.data(), view.length());
size_t size;
auto result = bson_as_json(&bson, &size);
if (!result)
return "Error converting to json";
ret = std::string(result);
bson_free(result);
return std::move(ret);
Seems to work fine!