[CXX-587] to_json() throws exception on empty array Created: 28/Apr/15  Updated: 15/May/15  Resolved: 13/May/15

Status: Closed
Project: C++ Driver
Component/s: BSON
Affects Version/s: 0.1.0
Fix Version/s: 0.2.0

Type: Bug Priority: Major - P3
Reporter: Robert Hougen Assignee: Mira Carey
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: PM-123

 Description   

we have a document that includes a collection with an array that is sometimes empty. If we access the database and iterate through the documents, the ones with empty arrays throw an exception when we use to_json().

example document:
{
"_id":

{ "$oid": "553ab2f6d7e9e61537efb919" }

,
"uid": "CA1236300671005",
"deviceType": "GX440",
"location": "",
"deviceData":

{ "ssid": "", "mdn": "", "imea": "990000561959919", "sim": "89148000000382974468", "firmwareVersion": "4.3.5.010B", "imsi": "311480038944334", "carrier": "VERIZON", "checkinInterval": 15 }

,
"checkinData": []
}

example code:
mongocxx::instance instance{};
std::string uri_string = "mongodb://-------.feeneywireless.com:27017";
mongocxx::client conn

{mongocxx::uri(uri_string)}

;

std::string dbname = "devices_db";
auto db = conn[dbname];

auto cursor = db["device"].find({});
for(auto&& doc : cursor) {
std::string teststring;
try

{ teststring = bsoncxx::to_json(doc); }

catch (exception e)

{ printf("error"); }

std:cout << teststring << std::endl;
}

Looking at bsoncxx->json.cpp there is no check for an empty array.
void visit_value(const types::b_array& value)

{ out << "[" << std::endl; stack.push_back(true); visit_children(value.value); pad(); out << "]"; }

I tried to add something along the lines of
void visit_value(const types::b_array& value) {
out << "[" << std::endl;
if(value.value.length != 0)

{ stack.push_back(true); visit_children(value.value); pad(); }

out << "]";
}
but couldn't figure out how to check for the empty array.

Thanks for your help.



 Comments   
Comment by Githook User [ 13/May/15 ]

Author:

{u'username': u'yhager', u'name': u'Yuval Hager', u'email': u'yhager@yhager.com'}

Message: CXX-587: Added tests for empty documents and empty array conversion to string

Closes #286

Signed-off-by: Adam Midvidy <amidvidy@gmail.com>
Branch: master
https://github.com/mongodb/mongo-cxx-driver/commit/b50b4cc63733fd467163ed83a3eb540592af8e86

Comment by Githook User [ 13/May/15 ]

Author:

{u'username': u'yhager', u'name': u'Yuval Hager', u'email': u'yhager@yhager.com'}

Message: CXX-587: validate an element before printing it as json string

Signed-off-by: Adam Midvidy <amidvidy@gmail.com>
Branch: master
https://github.com/mongodb/mongo-cxx-driver/commit/5fc10a9fa511afced424317ca4e10e77401a125d

Comment by Andrew Morrow (Inactive) [ 12/May/15 ]

https://github.com/mongodb/mongo-cxx-driver/pull/286

Comment by Yuval Hager [ 11/May/15 ]

Not the OP, but here's a similar PR to fix this: https://github.com/mongodb/mongo-cxx-driver/pull/286

Comment by Andrew Morrow (Inactive) [ 01/May/15 ]

Thanks for sending that. If you send that as a GitHub pull request we can merge it for you, or just let us know if you don't want to do that and we can fix on our side.

Comment by Robert Hougen [ 30/Apr/15 ]

I think I found a fix which works for both empty arrays and empty documents.
In bsoncxx::json.cpp the visit_children needs to check for the document it is passed to see if its empty.

void visit_children(const bsoncxx::document::view& view) {
bool first = true;
for (auto&& x : view) {
//added code
if(x.raw == 0 || x.length == 0)

{ break; }

//end of added code
if (!first)

{ out << ", " << std::endl; }

first = false;
visit_key(x.key());
switch (static_cast<int>(x.type()))

{ #define BSONCXX_ENUM(name, val) \ case val: \ visit_value(x.get_##name()); \ break; #include <bsoncxx/enums/type.hpp> #undef BSONCXX_ENUM }

}
out << std::endl;
stack.pop_back();
}

Comment by Andrew Morrow (Inactive) [ 28/Apr/15 ]

Thanks for the bug report, we will take a look. We appreciate your testing out the alpha of the new driver!

Generated at Wed Feb 07 21:59:39 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.