[CDRIVER-3626] Creating bson from json: Losing decimal digits in QT application Created: 16/Apr/20  Updated: 27/Oct/23  Resolved: 30/Jun/20

Status: Closed
Project: C Driver
Component/s: libbson
Affects Version/s: 1.17.0-beta
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Christoph Mönch Assignee: Roberto Sanchez
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Ubuntu 16.04 with Qt


Issue Links:
Related
is related to CDRIVER-2063 JSON export prints insignificant digi... Closed

 Description   

When creating a bson from a json string, decimal digits get lost when running in a QT environment in release mode.

Input string is

"{\"d\": 1.987}"

Output string (converted back to Json for readability) when running in debug mode with gdb enabled is

"{ "d" : { "$numberDouble" : "1.9870000000000000995" } }"

, which is fine, but when running in release mode it is

"{ "d" : { "$numberDouble" : "1.0" } }".

Tested with Qt 5.9.4 and current version 5.14.2.

This is the minimal example:

#include <QCoreApplication>
#include <bson.h>
#include <iostream>
 
#define BSON_JSON_DEFAULT_BUF_SIZE (1 << 14)
 
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
 
std::string json = "{\"d\": 1.987}";
bson_t * bson;
bson_error_t err;
const uint8_t* data = (const uint8_t *)(json.c_str());
int len = -1;
 
///1
//bson = bson_new_from_json(data, len, &err);
 
///inside bson_new_from_json:
if (len < 0) {
len = (ssize_t) strlen ((const char *) data);
}
bson_json_reader_t *reader;
bson = bson_new ();
reader = bson_json_data_reader_new (false, BSON_JSON_DEFAULT_BUF_SIZE);
bson_json_data_reader_ingest (reader, data, len);
int r = bson_json_reader_read (reader, bson, &err);
 
///test:
char* jsonD;
if ((jsonD = bson_as_canonical_extended_json (bson, NULL))) {
std::cout << "json from bson: " << jsonD << std::endl;
}
 
return a.exec();
}

 
When removing QCoreApplication and a.exec() everything works fine, so this is connected to Qt, but I cannot find out what is exactly going on in bson_json_reader_read(). I guess that Qt may define something in debug mode, which affects the bson library.



 Comments   
Comment by Roberto Sanchez [ 30/Jun/20 ]

It seems like the issue was a local environment configuration. Additional information on significant digits in JSON export can be found in CDRIVER-2063.

Comment by Christoph Mönch [ 14/May/20 ]

This fixes the issue:

const char *result = setlocale(LC_NUMERIC, "C");
if (!result)
    fprintf(stderr, "Warning: cannot change LC_NUMERIC to 'C'!");

Must be included after creating QCoreApplication to set printf output numeric dots instead commas. Thanks to aha_1980! ( https://forum.qt.io/topic/114725/mongo-c-driver-loosing-decimal-digits-in-qt-application/3 )

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