[CDRIVER-1945] bson_as_json outputs doubles as integers Created: 08/Dec/16  Updated: 29/Jan/24  Resolved: 05/Jan/17

Status: Closed
Project: C Driver
Component/s: libbson
Affects Version/s: 1.5.0
Fix Version/s: 1.6.0

Type: Bug Priority: Minor - P4
Reporter: David Golden 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
Problem/Incident
causes CDRIVER-4819 Allow reducing precision when convert... Backlog
Related
related to CDRIVER-3377 Double value retrieved from bson is d... Closed
Epic Link: libbson corpus

 Description   

Example code:

#include <bson.h>
#include <stdio.h>
 
int main() {
    bson_t b = BSON_INITIALIZER;
    BSON_APPEND_DOUBLE (&b, "a", 1.0);
 
    size_t len;
    char *str;
    str = bson_as_json (&b, &len);
    printf ("%s\n", str);
 
    bson_free (str);
    return 0;
}

Actual output:

{ "a" : 1 }

Possibly expected output:

{ "a" : 1.0 }

Some JSON encoders will preserve a fractional part of zero. This behavior surprised as least one user as shown on this stack overflow post.



 Comments   
Comment by Githook User [ 10/Feb/17 ]

Author:

{u'username': u'jmikola', u'name': u'Jeremy Mikola', u'email': u'jmikola@gmail.com'}

Message: Use exactly representable floating point numbers in tests

CDRIVER-1945 increased the precision when rendering floating point numbers in JavaScript. Using numbers that can be exactly represented in floating point will allow us to keep the expected output consistent with our input.
Branch: v1.2
https://github.com/mongodb/mongo-php-driver/commit/1cfffa55894eb8a95e1fd4a4e19f2f9cc442ca41

Comment by Githook User [ 10/Feb/17 ]

Author:

{u'username': u'jmikola', u'name': u'Jeremy Mikola', u'email': u'jmikola@gmail.com'}

Message: Use exactly representable floating point numbers in tests

CDRIVER-1945 increased the precision when rendering floating point numbers in JavaScript. Using numbers that can be exactly represented in floating point will allow us to keep the expected output consistent with our input.
Branch: master
https://github.com/mongodb/mongo-php-driver/commit/1cfffa55894eb8a95e1fd4a4e19f2f9cc442ca41

Comment by Githook User [ 02/Feb/17 ]

Author:

{u'username': u'jmikola', u'name': u'Jeremy Mikola', u'email': u'jmikola@gmail.com'}

Message: Use exactly representable floating point numbers in tests

CDRIVER-1945 increased the precision when rendering floating point numbers in JavaScript. Using numbers that can be exactly represented in floating point will allow us to keep the expected output consistent with our input.
Branch: master
https://github.com/mongodb/mongo-php-driver/commit/06c3f342e8db3638562eb7d3a23c903e3c2a48bf

Comment by Githook User [ 06/Jan/17 ]

Author:

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

Message: CDRIVER-1945 distinguish float from int in JSON output
Branch: master
https://github.com/mongodb/libbson/commit/9db0c61016a6ca593ebcebcea28f71c94c54660a

Comment by Jeremy Mikola [ 27/Dec/16 ]

For PHP, this has implications for the Double: -0.0 test case. If bson_as_json() emits "-0" for double value -0.0, we cannot reliably compare it with the expected canonical JSON string: {"d" : -0.0}. Were libbson to emit "-0.0", PHP's json_decode() function would be able to properly convert it to a negative zero floating point value in PHP, and it would be preserved when we call json_encode() as well.

Comment by David Golden [ 08/Dec/16 ]

I don't have strong feelings about this as Javascript doesn't distinguish numeric types. But we should make a call one way or another for the record.

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