[CDRIVER-1472] NaN handling difference between C and C++ driver Created: 11/Aug/16  Updated: 25/Apr/17  Resolved: 25/Apr/17

Status: Closed
Project: C Driver
Component/s: json
Affects Version/s: 1.3.5
Fix Version/s: 1.7.0

Type: New Feature Priority: Minor - P4
Reporter: Almansour Belleh Blanco [X] Assignee: A. Jesse Jiryu Davis
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on CDRIVER-1370 Replace libyajl with jsonsl Closed
Related
is related to CDRIVER-1336 Define constants for special strings ... Closed
Backwards Compatibility: Minor Change

 Description   

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



 Comments   
Comment by Githook User [ 25/Apr/17 ]

Author:

{'name': 'A. Jesse Jiryu Davis', 'username': 'ajdavis', 'email': 'jesse@mongodb.com'}

Message: CDRIVER-1472 parse Infinity from JSON
Branch: master
https://github.com/mongodb/libbson/commit/23f40261fc919cad9b6b3b93b82506fb04f613d9

Comment by Githook User [ 11/Apr/17 ]

Author:

{'username': 'ajdavis', 'email': 'jesse@mongodb.com', 'name': 'A. Jesse Jiryu Davis'}

Message: CDRIVER-1472 parse NaN from legacy JSON
Branch: master
https://github.com/mongodb/libbson/commit/daca03cbe2e15a8469a2cc877b6ea91af8645dd9

Comment by Githook User [ 11/Apr/17 ]

Author:

{u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}

Message: CDRIVER-1472 parse NaN from legacy JSON
Branch: master
https://github.com/mongodb/libbson/commit/daca03cbe2e15a8469a2cc877b6ea91af8645dd9

Comment by A. Jesse Jiryu Davis [ 11/Apr/17 ]

Closed too soon: Must still consider Infinity and -Infinity.

Comment by A. Jesse Jiryu Davis [ 06/Apr/17 ]

I've updated JSONSL's behavior upstream for NaN:

https://github.com/mnunberg/jsonsl/issues/24

Maybe we should do the same for Infinity and -Infinity.

Comment by Luke Lovett [ 11/Oct/16 ]

jesse The spec will discuss non-JSONable numerics like NaN and Inf. It's likely that parsers and generators will be required to deal with them.

Comment by A. Jesse Jiryu Davis [ 10/Oct/16 ]

luke.lovett do you expect the forthcoming Extended JSON Spec to tell us whether to parse or generate NaN? Will the spec's ruling be that we must deal with NaN, or must not deal with NaN, or will the spec not have any rule about Nan?

Comment by A. Jesse Jiryu Davis [ 11/Aug/16 ]

I am well aware that YAJL isn't maintained! We plan to migrate from it this quarter, CDRIVER-1370. I'll consider this feature afterwards.

Comment by Almansour Belleh Blanco [X] [ 11/Aug/16 ]

Isn't there a workaround in the meantime, even the yajl library doesn't seem to be actively maintained

Comment by A. Jesse Jiryu Davis [ 11/Aug/16 ]

Not all MongoDB drivers' JSON parsers support NaN, since NaN is not included in the formal JSON spec. This may have to sit in the backlog for a bit, but I do understand that it would be useful to parse NaN.

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