Home | About | Sematext search-lucene.com search-hadoop.com
 Search Hadoop and all its subprojects:

Switch to Threaded View
Avro >> mail # user >> Avro cpp setting precision for the output stream

Copy link to this message
Avro cpp setting precision for 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

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>
10using namespace std;
11int main()
13  ifstream ifs("cpx.json");
14  avro::ValidSchema type_schema;
15  avro::compileJsonSchema(ifs, type_schema);
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;
30  return 0;

If we then compile and run this we get:
Where as I'd expect something like:
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? Thanks!