Uploaded image for project: 'C++ Driver'
  1. C++ Driver
  2. CXX-1753

bsoncxx:to_json causes double value to truncate

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Gone away
    • Affects Version/s: 3.0.1
    • Fix Version/s: None
    • Component/s: BSON
    • Labels:
    • Environment:
      ubuntu.16-04

      Description

      When I'm using bsoncxx::to_json function to convert prepared bsoncxx document to json all float/double values are truncated to 5 digits precision. Please check following code:

       

      #include <iostream>
      #include <bsoncxx/json.hpp>
       #include <bsoncxx/builder/basic/array.hpp>
       #include <bsoncxx/builder/stream/document.hpp>
       #include <bsoncxx/builder/basic/document.hpp>
      using namespace bsoncxx;
      using namespace std;
      using bsoncxx::builder::basic::kvp;
      using bsoncxx::builder::basic::document;
      int main(void) {
        bsoncxx::builder::stream::document d1;
        bsoncxx::builder::basic::document d2;
        cout.precision(17);
        double v1=1.0123456789;
        double v2=9.8765432111;
        d1<<"T1"<<v1;
        cout<<bsoncxx::to_json(d1.view())<<endl;
       d2.append(kvp("T2", types::b_double{v2}));
       cout<<bsoncxx::to_json(d2.view())<<endl;
        cout<<"V1: "<<v1<<endl;
        cout<<"V2: "<<v2<<endl;
        cout<<"T1: "<<d1.view()["T1"].get_double()<<endl;
        cout<<"T2: "<<d2.view()["T2"].get_double()<<endl;
      }
      

      Sample output:

       

      {
       "T1" : 1.01235
       }
       {
       "T2" : 9.87654
       }
      V1: 1.0123456789
       V2: 9.8765432110999996
       T1: 1.0123456789
       T2: 9.8765432110999996
      

      I'm expecting following output from to_json call:

       

      {
       "T1" : 1.0123456789
       }
      {
       "T2" : 9.8765432110999996
       }
      

      How to fix this issue?

      libbsoncxx -> 3.0.1 libbson -> 1.9.2

      Thank you!

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: