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? Thanks! -Paul
cpx.json
Description: application/json
#include "cpx.hh"
#include "avro/Compiler.hh"
#include "avro/Encoder.hh"
#include "avro/Decoder.hh"
#include "avro/Specific.hh"
#include "avro/Generic.hh"
#include <sstream>
#include <fstream>
using namespace std;
int main()
{
ifstream ifs("cpx.json");
avro::ValidSchema type_schema;
avro::compileJsonSchema(ifs, type_schema);
ostringstream oss (ostringstream::out);
oss.precision(30); // doesn't matter
std::auto_ptr<avro::OutputStream> out = avro::ostreamOutputStream(oss);
avro::EncoderPtr e = avro::jsonEncoder(type_schema);
e->init(*out);
c::cpx complex;
complex.re = 1.123456789;
complex.im = 2.01;
avro::encode(*e, complex);
e->flush();
cout.precision(30);
cout << oss.str() << endl;
return 0;
}
