Thrift: Is it possible to do only serialization with C++ Thrift library?

后端 未结 5 1521
执笔经年
执笔经年 2021-01-02 05:21

With C++ Apache Thrift library, is it possible to use only Serialization/Deserialization and not use RPC services?

As I understand from this page, it is possible to

相关标签:
5条回答
  • 2021-01-02 05:32

    In c++ you can use the TFileTransport:

    boost::shared_ptr<TFileTransport> transport(new TFileTransport(filename));
    boost::shared_ptr<TBinaryProtocol> protocol(new TBinaryProtocol(transport));
    yourObj.write(protocol.get()); // to write
    

    or

    yourObj.read(protocol.get()); // to read
    
    0 讨论(0)
  • 2021-01-02 05:32

    I realize that this question was asked a while ago. I encountered a similar use case recently (with an additional requirement - sending the serialized bytes to a Kafka cluster). Here's my answer with more complete code snippets that illustrate how the serialization and sending tasks can be done.

    0 讨论(0)
  • 2021-01-02 05:41

    Yes it is possible. Thrift lacks documentation about this subject. Well, about anything really.

    Here i found this:

    http://mail-archives.apache.org/mod_mbox/thrift-user/201010.mbox/%3C5EF8F634-79A2-45C4-9A04-6D96D3B7A84F@manbert.com%3E

    i personally use boost::serialization if there is no need to transfer data over network. Much clear syntax and supports JSON, XML and binary output/input.

    0 讨论(0)
  • 2021-01-02 05:54

    Here's a different mailing list post with some attached code showing how to use thrift to serialize to a file in C++.

    http://mail-archives.apache.org/mod_mbox/thrift-user/201203.mbox/%3C90483D01-ED25-4707-9DB2-5BB3627301FC@manbert.com%3E

    0 讨论(0)
  • 2021-01-02 05:56

    If you simply want to serialize into bytes (without having to write to a file), you can use TMemoryBuffer.

    boost::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
    boost::shared_ptr<TBinaryProtocol> binaryProtcol(new TBinaryProtocol(buffer));
    
    obj->write(binaryProtcol.get());
    obj->read((binaryProtcol.get()));
    
    0 讨论(0)
提交回复
热议问题