Search code examples
angulartypescriptthrift

How to parse thrift objects in Angular app?


I have a c++ server which send objects to an ActiveMq broker through mqtt protocol. The classes of these objects are generate by thrift :

thrift -r --gen cpp:pure_enums filame.thrift

Thrift generates me the js classes and the typings ts files.

thrift -r --gen js:ts filame.thrift

The Angular app get the data from the broker. My problem is that I don't know how to parse the data to typescript objects.

I didn't find npm librairies for typescript that resolve my problem. That seems to me that Apache thrift library can't help me (https://www.npmjs.com/package/thrift).

Angular & Typescript version : 6.5.0


Solution

  • I don't have a complete, ready-to-use piece of code at hand, but I can provide a generic outline.

    First, set up transport/protocol stack as usual, e.g.

    var transport = new Thrift.Transport(...); 
    var protocol  = new Thrift.Protocol(transport); 
    

    Now create an empty instance of the root object class that's serialized in the file data (you need to know what that is exactly):

    var mydata = new Some.Thrift.DataType()
    

    Read the data:

    mydata.Read(protocol);
    

    Profit!