thrift-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rush Manbert <r...@manbert.com>
Subject Re: thrift JSON serialization question
Date Wed, 13 Mar 2013 15:46:40 GMT
Hi Magesh,
"
The standard code won't give you a "standard" JSON representation because it needs to carry
the ThriftID information. Maybe more, because it assumes that the other side of the conversation
is a piece of Thrift code that needs to reconstruct the Thrift objects.

If you want to use the JSON protocol to talk to a Thrift service, or to freeze dry objects
that can be reconstituted later, then you need that standard implementation so that the Thrift
metadata is there.

If you just want to export a Thrift object into a "non-Thrift" equivalent Javascript object,
that is possible, but you need to hack the protocol. I did this for a project of mine and
I think I still have the code around, but it's based on an old version of Thrift and I think
the JSON protocol has been changed since I wrote it. Let me know if you want it and I can
make it available.

- Rush

On Mar 13, 2013, at 2:15 AM, Magesh Dhasayyan wrote:

> Hi,
> 
> I'm trying to use thrift for an application that uses JSON. For example
> I've listed below a sample thrift struct (Foo) and it's usage in a C++
> program. I want the output of the program to be {"bar": 1024} and NOT
> {"1":{"i32":1024}}. Is it possible using thrift libraries?
> 
> """
> // file: hello_world.thrift
> struct Foo {
>  1: i32 bar;
> }
> 
> // file: hello_world.cpp
> #include <iostream>
> #include <thrift/transport/TBufferTransports.h>
> #include <thrift/protocol/TJSONProtocol.h>
> #include <thrift/protocol/TProtocolException.h>
> #include <gen-cpp/hello_world_types.h>
> int main() {
>    namespace atp = apache::thrift::protocol;
>    namespace att = apache::thrift::transport;
>    boost::shared_ptr<att::TMemoryBuffer>
>       transport(new att::TMemoryBuffer());
>    atp::TJSONProtocol protocol(transport);
>    Foo foo;
>    foo.bar = 1024;
>    foo.write(&protocol);
>    std::cout << transport->getBufferAsString()
>              << std::endl;
> }
> 
> // file: JSON-output.txt
> {"1":{"i32":1024}}
> """
> 
> Looking at the comments in
> thrift-0.9.0/lib/cpp/src/thrift/protocol/TJSONProtocol.h, I suspect it's
> not possible. But just wanted to check with thrift experts before I drop
> the idea.
> 
> 
> Thanks,
> Magesh


Mime
View raw message