0

I have a json string which I want to convert to a thrift object.

The thrift definition is

struct Person {
    1: optional string name;
}

My code is

#include <thrift/TBase.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/protocol/TJSONProtocol.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/transport/TTransportUtils.h>

#include <cstring>

#include "Person_types.h"

int main() {
    std::string s = R"({"name": "alice"})";

    auto transport = std::make_shared<apache::thrift::transport::TMemoryBuffer>((uint8_t *)s.data(), s.length());
    auto protocol_factory = std::make_shared<apache::thrift::protocol::TJSONProtocolFactory>();
    auto protocol = protocol_factory->getProtocol(transport);

    Person obj;
    obj.read(protocol.get());
}

But when I run it, I encountered an error with message

terminate called after throwing an instance of 'apache::thrift::protocol::TProtocolException'
  what():  Expected '"'; got 'n'.
Aborted (core dumped)

My thrift version is 0.12.0

Harith
  • 4,663
  • 1
  • 5
  • 20
Hu Zhang
  • 95
  • 1
  • 7
  • 1
    The dupe in Java https://stackoverflow.com/questions/20261519/thrift-converting-from-simple-json – 273K Mar 29 '23 at 13:33
  • By the way, why do you need `shared_ptr`? – Red.Wave Mar 29 '23 at 13:57
  • Does this answer your question? [Thrift - converting from simple JSON](https://stackoverflow.com/questions/20261519/thrift-converting-from-simple-json) – JensG Mar 30 '23 at 14:27
  • The dupe is real. The issue is really not limited to Java. It's the same for all languages thrift supports. – JensG Mar 30 '23 at 14:30

0 Answers0