You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by "andrew watts (JIRA)" <ji...@apache.org> on 2012/06/28 22:52:42 UTC

[jira] [Created] (THRIFT-1639) Java/Python: Serialization/Deserialization of double type using CompactProtocol

andrew watts created THRIFT-1639:
------------------------------------

             Summary: Java/Python: Serialization/Deserialization of double type using CompactProtocol
                 Key: THRIFT-1639
                 URL: https://issues.apache.org/jira/browse/THRIFT-1639
             Project: Thrift
          Issue Type: Bug
          Components: Java - Library, Python - Library
    Affects Versions: 0.8
            Reporter: andrew watts


Using the Compact Protocol double values are not properly serialized/deserialized with Java or Python.  

I have a java server that writes a thrift blob to log file as base64 encoded string, then python reads the base64 encoded string from the log.  During development I discovered double values are not deserialized properly.

In the mailing list there is speculation of mismatch between serialization/deserialization in the languages and recommended opening a ticket.


Example:
{code}
» java -cp .:../lib/libthrift-0.8.0.jar:../lib/slf4j-api-1.6.4.jar ThriftTest
fooObj.bar: 3.456
base64String: F9nO91PjpQtAAA==

» python ../python/tdouble_test.py
base64 string:  F9nO91PjpQtAAA==
foo_obj.bar:  -4.09406819342e+124    # expect 3.456
{code}


Thrift Definition:
{code}
struct FooObj {
    1: double bar
}
{code}

Java Code:
{code}
import org.apache.thrift.TException;
import org.apache.thrift.TSerializer;
import org.apache.thrift.protocol.TCompactProtocol;

import javax.xml.bind.DatatypeConverter;


public class ThriftTest {

   public static void main(String[] args) {

       final TSerializer serializer = new TSerializer(new
TCompactProtocol.Factory());

       // create a FooObj with double
       final FooObj fooObj = new FooObj(3.456);
       System.out.println("fooObj.bar: " + fooObj.bar);

       // serialize to bytes
       byte[] fooObjBlob = null;
       try {
           fooObjBlob = serializer.serialize(fooObj);
       } catch (TException e) {
           e.printStackTrace();
       }

       // encode to base64 string
       final String base64String =
DatatypeConverter.printBase64Binary(fooObjBlob);
       System.out.println("base64String: " + base64String);
   }

}
{code}


Python Code

{code}
#!/bin/env python

import base64

from thrift.protocol import TCompactProtocol
from thrift.TSerialization import deserialize

from foo.ttypes import FooObj


def main():

   protocol_factory = TCompactProtocol.TCompactProtocolFactory
   base64_string = 'F9nO91PjpQtAAA=='

   print 'base64 string: ', base64_string

   # deserialize the string back into an object
   foo_blob = base64.urlsafe_b64decode(base64_string)
   foo_obj = FooObj()
   deserialize(foo_obj, foo_blob, protocol_factory=protocol_factory())
   print 'foo_obj.bar: ', foo_obj.bar

if __name__ == '__main__':
   main()

{code}


--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira