5

私はNode.jsアプリケーションで作業しており、次のように.thriftファイルで定義された構造体のインスタンスをシリアル化および逆シリアル化する必要があります。

 struct Notification {
   1: string subject,
   2: string message
 }

http://www.gettingcirrius.com/2011/03/rabbitmq-with-thrift-serialization.htmlのチュートリアルによると、これはJavaで簡単に実行できます。

    Notification notification = new Notification();
    TDeserializer deserializer = new TDeserializer();
    deserializer.deserialize(notification, serializedNotification);
    System.out.println("Received "+ notification.toString());

しかし、Thriftのnodejsライブラリを使用してこれがどのように行われるかを見つけることができません。誰か助けてもらえますか?

4

3 に答える 3

8

OK、研究に多くの時間を費やし、さまざまな解決策を試した後、私は最終的に自分の質問に対する答えにたどり着きました:

//SERIALIZATION:
var buffer = new Buffer(notification);
var transport = new thrift.TFramedTransport(buffer);
var binaryProt = new thrift.TBinaryProtocol(transport);
notification.write(binaryProt);

notification はシリアライズしたいオブジェクトです。この時点で、バイト配列は transport.outBuffers フィールドで見つけることができます。

逆シリアル化の場合:

var tTransport = new thrift.TFramedTransport(byteArray);
var tProtocol = new thrift.TBinaryProtocol(tTransport);
var receivedNotif = new notification_type.Notification();
receivedNotif.read(tProtocol);

次の行が、thrift 用の nodejs ライブラリから index.js ファイルに追加されていると仮定します。

exports.TFramedTransport = require('./transport').TFramedTransport;
exports.TBufferedTransport = require('./transport').TBufferedTransport;
exports.TBinaryProtocol = require('./protocol').TBinaryProtocol;
于 2012-11-20T17:57:30.583 に答える