从这里开始,Protobuf引入C++11

Protobuf有很多版本了,现在项目组使用的Visual Studio还是2005,所以不支持C++11,在引入Protobuf时既想找到一个proto3的可用的最新版本,又要不使用C++11,在连续下载和尝试,和阅读Protobuf的每个版本的发行日志时,发现了这样一段话

  • Starting from this release, we now require C++11. For those we cannot yet upgrade to C++11, we will try to keep the 3.5.x branch updated with critical bug fixes only. If you have any concerns about this, please comment on issue #2780.
  • Moved to C++11 types like std::atomic and std::unique_ptr and away from our old custom-built equivalents.

有需要关注的关键字we noew require C++ 11。是哪个版本呢?

Protocol Buffers v3.6.0

也即大家在项目中不支持C++11的,用v.3.6.0之前的protobuf就好。不会编译不过。

PS:亲自尝试了下,3.5.0和3.4.0在VS2005下还是会有报错,直到3.30才编译通过。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用第三方库,如protobuf-json-util来实现C++中JSON转Protobuf的功能。个库提供了一些方便的函数和接口,可以帮助你在Protobuf和JSON之间进行转换。 首先,你需要在你的C++项目中引入protobuf-json-util库。你可以在GitHub上找到该库的源代码并进行下载和安装。 然后,你需要定义你的Protobuf消息和相应的JSON格式。在Protobuf中,你可以使用.proto文件定义消息结构,在JSON中,你可以使用类似的结构来表示相同的数据。 接下来,你可以使用protobuf-json-util库提供的函数来实现JSON到Protobuf的转换。你可以使用`ParseFromJsonString()`函数将JSON字符串解析为Protobuf消息对象,使用`SerializeToJsonString()`函数将Protobuf消息对象序列化为JSON字符串。 下面是一个简单的示例代码: ```cpp #include <iostream> #include <string> #include <protobuf-json-util/json_util.h> #include "your_protobuf_message.pb.h" int main() { // 定义一个Protobuf消息对象 YourProtobufMessage message; // 从JSON字符串解析Protobuf消息对象 std::string json = R"( { "field1": 123, "field2": "hello", "field3": true } )"; json_util::Status status = json_util::ParseFromJsonString(json, &message); if (!status.ok()) { std::cerr << "Failed to parse JSON: " << status.error_message() << std::endl; return 1; } // 将Protobuf消息对象序列化为JSON字符串 std::string serialized_json = json_util::SerializeToJsonString(message); std::cout << "Serialized JSON: " << serialized_json << std::endl; return 0; } ``` 请确保按照protobuf-json-util库的文档进行正确的安装和配置,并根据你的实际需求修改示例代码中的消息类型和字段名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值