[CDRIVER-4819] Allow reducing precision when converting BSON double values to JSON Created: 29/Jan/24 Updated: 29/Jan/24 |
|
| Status: | Backlog |
| Project: | C Driver |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Task | Priority: | Unknown |
| Reporter: | Andreas Braun | Assignee: | Unassigned |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||||||||||||||||||||||
| Assigned Teams: |
C Drivers
|
||||||||||||||||||||||||||||||||||||
| Description |
|
In _bson_as_json_visit_double, BSON double values are converted to strings with a precision of 20 decimal digits. This can lead to issue for some values when they are rounded at the default double precision (~16 digits) but not at the extended precision. An example for such a value is 1.99, which translates to 1.9899999999999999911 when 20 digits precision are available. This is problematic, as users using a value like above are presented with a different value in the resulting JSON. I'll note that reading the resulting JSON into BSON results in the original value, as the 20 digit precision is not available in a C double. A possible solution is changing the precision to a lower value: the 53 bits of precision in a double result in 16 digits of decimal precision. If this can be considered a BC break, adding a precision option to _bson_json_opts_t for use in bson_as_json_with_opts could provide a workaround for affected users. |
| Comments |
| Comment by Kevin Albertson [ 29/Jan/24 ] | |||||||||||||
|
I am hesitant to change the default out of concern of possible backwards breaking, but it may be well motivated. The C driver skips BSON corpus tests related to double precision. Open questions: Since double can only represent < 16 decimal digits, does reducing the precision to 16 digits have no impact on round-tripping? Example:
If there is no impact on round-tripping, that may suggest less risk of impact. Though the output of libbson could be input to different JSON parsers. What do other JSON libraries use? https://github.com/json-c/json-c/blob/dabed80523fa5101e30f0ee57ba06b02beae73eb/json_object.c#L1016 shows 17:
https://bugs.python.org/issue1580 also references 17. |