Uploaded image for project: 'Apache Avro'
  1. Apache Avro
  2. AVRO-1216

Setting precision for the output stream

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 1.7.2, 1.7.3, 1.7.4, 1.7.5, 1.7.6, 1.7.7, 1.8.0, 1.8.1
    • 1.8.2
    • c++
    • None
    • Linux x86_64

    Description

      There's no way, that I know of, to set the precision level on the output stream.

      I want to take an avro object, with double fields, and json encode it into a string. However, when I do this the encoded doubles have lost precision. I suspect this is because the precision I set on the ostringstream and resulting avro::ostreamOutputStream isn't working.

      Here's an example inspired by the tutorial (http://avro.apache.org/docs/1.7.2/api/cpp/html/index.html). Using their cpx.json

      00001 {
      00002     "type": "record", 
      00003     "name": "cpx",
      00004     "fields" : [
      00005         {"name": "re", "type": "double"},    
      00006         {"name": "im", "type" : "double"}
      00007     ]
      00008 }
      

      Generating cpx.hh by
      avrogencpp -i cpx.json -o cpx.hh -n c

      The test program is then:

       1#include "cpx.hh"
       2#include "avro/Compiler.hh"
       3#include "avro/Encoder.hh"
       4#include "avro/Decoder.hh"
       5#include "avro/Specific.hh"
       6#include "avro/Generic.hh"
       7#include <sstream>
       8#include <fstream>
       9
      10using namespace std;
      11int main()
      12{
      13  ifstream ifs("cpx.json");
      14  avro::ValidSchema type_schema;
      15  avro::compileJsonSchema(ifs, type_schema);
      16
      17  ostringstream oss (ostringstream::out);
      18  oss.precision(30); // doesn't matter                                                                                                                                                                     
      19  std::auto_ptr<avro::OutputStream> out = avro::ostreamOutputStream(oss);
      20  avro::EncoderPtr e = avro::jsonEncoder(type_schema);
      21  e->init(*out);
      22  c::cpx complex;
      23  complex.re = 1.123456789;
      24  complex.im = 2.01;
      25  avro::encode(*e, complex);
      26  e->flush();
      27  cout.precision(30);
      28  cout << oss.str() << endl;
      29
      30  return 0;
      31}
      

      If we then compile and run this we get:

      {"re":1.12346,"im":2.01}

      Where as I'd expect something like:

      {"re":1.12346789,"im":2.01}

      It doesn't seem to matter where/if we set the precision on the output stream (line 18).

      Is there a different way to handle this? How do I keep the precision from getting stripped off double when doing json encoding?

      Attachments

        1. AVRO-1216-A.patch
          4 kB
          Thiruvalluvan M. G.
        2. AVRO-1216.patch
          3 kB
          John McClean
        3. precision.cpp
          0.7 kB
          Paul Jacobs
        4. cpx.json
          0.2 kB
          Paul Jacobs

        Activity

          People

            comm@squotd.net John McClean
            pjacobs Paul Jacobs
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: