C ++在json中转换protobuf

时间:2018-03-05 16:47:15

标签: json protocol-buffers protobuf-c

我正在尝试将一些数据从protobuf连接到json。

这是我的代码的相关部分:

Message* m;
std::string json;
std::string binary_s;
...fill the message...
m->serializeToString(&binary_s);
MessageToJsonString(*m, &json);

下面的结构工作正常并产生了预期的输出。

m->serializeToString(&binary_s); 

但是当我尝试将其转换为json时,总会出现此错误:

Program received signal SIGSEGV, Segmentation fault.
google::protobuf::DescriptorPool::FindFileByName (this=0x0,     name="FLATBUFFER_DEFINITION.proto") at google/protobuf/descriptor.cc:1415
1415      MutexLockMaybe lock(mutex_);
(gdb) backtrace
#0  google::protobuf::DescriptorPool::FindFileByName (this=0x0,     name="AutoPilot.proto")
    at google/protobuf/descriptor.cc:1415
#1  0x00007ffff7aba42d in google::protobuf::internal::AssignDescriptors     (filename="AutoPilot.proto", 
    schemas=0x4463a0 <protobuf_AutoPilot_2eproto::schemas>, 
    default_instances_=0x446500     <protobuf_AutoPilot_2eproto::file_default_instances>, 
    offsets=0x445c20 <protobuf_AutoPilot_2eproto::TableStruct::offsets>, factory=0x0, 
    file_level_metadata=0x665020 <protobuf_AutoPilot_2eproto::file_level_metadata>, 
    file_level_enum_descriptors=0x6651e0 <protobuf_AutoPilot_2eproto::file_level_enum_descriptors>, 
    file_level_service_descriptors=0x0) at google/protobuf    /generated_message_reflection.cc:2316
#2  0x000000000040730c in         protobuf_AutoPilot_2eproto::protobuf_AssignDescriptors() ()
#3  0x00007ffff7a24bd5 in google::protobuf::internal::FunctionClosure0::Run (this=0x7fffffffda90)
    at ./google/protobuf/stubs/callback.h:129
#4  google::protobuf::GoogleOnceInitImpl (
once=0x665388     <protobuf_AutoPilot_2eproto::protobuf_AssignDescriptorsOnce()::once>,     closure=0x7fffffffda90)
    at google/protobuf/stubs/once.cc:83
#5  0x0000000000433abb in google::protobuf::GoogleOnceInit(long*, void     (*)()) ()
#6  0x0000000000407383 in protobuf_AutoPilot_2eproto::protobuf_AssignDescriptorsOnce() ()
#7  0x0000000000431503 in Message::GetMetadata() const ()
#8  0x00007ffff7b536cc in google::protobuf::Message::GetDescriptor (this=0x68e0b0) at ./google/protobuf/message.h:336
#9  google::protobuf::util::MessageToJsonString (message=warning: RTTI     symbol not found for class 'Message'
..., output=0x7fffffffdd60, options=...)
    at google/protobuf/util/json_util.cc:217

似乎无法找到文件“FLATBUFFER_DEFINITION.proto”,但它位于可执行文件的同一目录中。

1 个答案:

答案 0 :(得分:0)

google::protobuf::DescriptorPool::FindFileByName **(**this=0x0**,** name="FLATBUFFER_DEFINITION.proto") at google/protobuf/descriptor.cc:1415
1415      MutexLockMaybe lock(mutex_);

对象指针为空。我打电话给ShutdownProtobufLibrary();之后,继续使用他的职能。