Uploaded image for project: 'C++ Driver'
  1. C++ Driver
  2. CXX-587

to_json() throws exception on empty array

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 0.2.0
    • Affects Version/s: 0.1.0
    • Component/s: BSON
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      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.

            Assignee:
            mira.carey@mongodb.com Mira Carey
            Reporter:
            robert.hougen Robert Hougen
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: