[ 
https://issues.apache.org/jira/browse/AVRO-1216?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15307801#comment-15307801
 ] 

John McClean commented on AVRO-1216:
------------------------------------

[~thiru_mg]: can you take a look at this patch when you get a chance?

> Setting precision for the output stream
> ---------------------------------------
>
>                 Key: AVRO-1216
>                 URL: https://issues.apache.org/jira/browse/AVRO-1216
>             Project: Avro
>          Issue Type: Bug
>          Components: c++
>    Affects Versions: 1.7.2
>         Environment: Linux x86_64
>            Reporter: Paul Jacobs
>            Assignee: John McClean
>         Attachments: AVRO-1216.patch, cpx.json, precision.cpp
>
>
> 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
> {noformat}
> 00001 {
> 00002     "type": "record", 
> 00003     "name": "cpx",
> 00004     "fields" : [
> 00005         {"name": "re", "type": "double"},    
> 00006         {"name": "im", "type" : "double"}
> 00007     ]
> 00008 }
> {noformat}
> Generating cpx.hh by 
> avrogencpp -i cpx.json -o cpx.hh -n c
> The test program is then:
>  {code:java}
>  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}
> {code}
> 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?



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to