Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-1472

NaN handling difference between C and C++ driver

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 1.7.0
    • Affects Version/s: 1.3.5
    • Component/s: json
    • Labels:
      None
    • Minor Change

      The NaN handling is different between the C driver and older versions of the C++ driver.
      We are using version 2.6.5 of c++ 26compat version.
      When we call fromjson on a json containing a NaN value, the C++ driver won't complain and will keep it as NaN.
      However, doing the same with C driver will give us an error and an empty bson document.

      for example, using this code:

      BSONObj obj = fromjson(
            "{                                                \n"
            "    \"DOUBLE\" : 2.25,                           \n"
            "    \"INT\"    : 9999,                           \n"
            "    \"ARRAY\"  : [                               \n"
            "      {  \"Name\" : \"Meier\", \"age\" : 90 },   \n"
            "      {  \"Name\" : \"Smith\", \"age\" : 80 }    \n"
            "    ] ,                                          \n"
            "    \"VEC\"    : [ NaN, 3, 5, 7, 11, 13]           \n"
            "}                                                \n"
          );
      

      we will obtain the desired BSONObj with NaN
      and we have the following implementation for fromjson using libmongoc

      
        BSONObj fromjson(const std::string& str)
        {
        
          bson_t doc;
          bson_error_t error;
      
      
          if (!bson_init_from_json(&doc, str.c_str(), static_cast<long long>(str.size()), &error))
          {
            std::cout<< "fromjson ERROR: " << error.domain << "." << error.code << ": " << error.message << std::endl;
            bson_destroy(&doc);
            return BSONObj();
          }
      
      
          return BSONObj(&doc);
        }
      

      In this case, the code will enter to the error case and doc will be empty

            Assignee:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Reporter:
            Mansuro Almansour Belleh Blanco [X]
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: