Search code examples
cjsonstructthrift

Coding example for thrift in C, using files


I've started using thrift for C. I managed to generated the .c and .h files via the compiler. I'm looking to write to a file, preferably JSON. However there are no examples on the apache thrift site. A internet search turns up next to nothing useful. Does anybody have any sample code that I can use? I essentially have a struct has a bunch of ints and char *.


Solution

  • However there are no examples on the apache thrift site. A internet search turns up next to nothing useful.

    That's simply not true, we have a great tutorial covering a great part of the languages. You can find them quite easily via Google. The tutorial code can be found in the release tarball or in the Git repository as a top-level directory named tutorial.

    Since you are looking specifically for JSON, I recommend to have a look at the cross-language test client/server, which can be found under test or lib (a bit inconsistent right now, we are about cleaning that up). AFAIK for plain C there is no JSON available, but for C++ there is.

    In order to store things into a file, you basically choose a stream or file transport and the protocol of your choice from the available protocols. It's as simple as (pseudocode)

    var data = InitializeMyDataStructure();
    
    var trans = new TFileTransport("myfile");
    var prot = new TJSONProtocol(trans);
    
    data.write(prot);
    

    The support for plain C is somewhat limited yet, but there is all kinds of transports/protocols for C++.