protobuf下载与安装+ protobuf 与json相互转换方法

2023-10-27

WIN环境 下载与安装

下载

https://github.com/protocolbuffers/protobuf/releases
官方git地址,目前最新的是3.8版本;
我是c++环境,选择cpp下载包

protobuf-cpp-3.8.0.tar.gz

安装

解压后,打开protobuf-3.8.0\README.md 文件,里边明确指出了c++的安装参考文件:

Protocol Compiler Installation

The protocol compiler is written in C++. If you are using C++, please follow the C++ Installation Instructions to install protoc along with the C++ runtime.

链到如下文件:
protobuf-3.8.0\src\README.md

区分linux和win的安装方法;win又链到了另外一个文件:

To build from source using Microsoft Visual C++, see cmake/README.md.

protobuf-3.8.0\cmake\README.md

执行完最后一步安装后,会再并行目录生成一个\install文件夹,里边包含了win要用的库、头文件、pb转换工具 三项;

官方API指南

https://developers.google.cn/protocol-buffers/docs/reference/overview

protocol buffer 转json 方案

转json功能官方API

https://developers.google.cn/protocol-buffers/docs/reference/cpp/google.protobuf.util.json_util

.proto 文件编译方法及编译类生成的成员方法规律

https://developers.google.cn/protocol-buffers/docs/reference/cpp-generated

TMT.proto文件示例

syntax = "proto3";

package cmu_cmdp;
message CMU_TMt   
{   
    uint32 main_type = 1;   
    uint32 sub_type = 2; 
    uint32 mcu_id = 3;   
    uint32 eqp_id = 4; 
    uint32 confdri_id = 5;   
    uint32 conf_idx = 6; 
}

message CMU_TBasicConfInfo
{
    CMU_TMt speaker = 1;
	CMU_TMt chairman = 2;
	string e164str = 4;
	repeated CMU_TMt tmtarr = 6;
	reserved 3,5;
}

把.proto文件转成c++的.h.cc文件

启用cmd,调用安装时生成的protobuf.exe 可执行文件,执行命令 protobuf.exe --cpp_out=./ TMT.proto,会在本目录下生成对应的TMt.pb.h、TMt.pb.cc

c++实例代码

#include "TMt.pb.h" // 包含.proto生成的头文件
#include <google/protobuf/util/json_util.h>  // 要使用转json功能,需要包该头文件
using namespace std;
using namespace google::protobuf::util;  // 引入命名空间
using namespace cmu_cmdp;

int main()
{

       CMU_TMt tmt1;
       tmt1.set_conf_idx(1);
       tmt1.set_eqp_id(11);
       tmt1.set_main_type(9);
       tmt1.set_confdri_id(3);
       tmt1.set_mcu_id(33);
       tmt1.set_sub_type(7);

       CMU_TMt tmt2;
       tmt2.set_conf_idx(2);
       tmt2.set_eqp_id(22);
       tmt2.set_main_type(22);
       tmt2.set_confdri_id(2);
       tmt2.set_mcu_id(22);
       tmt2.set_sub_type(2);

       CMU_TBasicConfInfo cSimConfInfo;
       cSimConfInfo.set_allocated_speaker(&tmt1);
       cSimConfInfo.set_allocated_chairman(&tmt2);
       cSimConfInfo.set_e164str("0512110");
       CMU_TMt *ptmt = cSimConfInfo.add_tmtarr();
       *ptmt = tmt1;
       ptmt = cSimConfInfo.add_tmtarr();
       *ptmt = tmt2;

       std::string strmsg;
       MessageToJsonString(cSimConfInfo, &strmsg);  // 结构体转json
       cout << strmsg.c_str() << endl;

       CMU_TBasicConfInfo cSimConfInfo2;
       JsonStringToMessage(strmsg, &cSimConfInfo2);  // json 反转结构体
       cout << cSimConfInfo2.speaker().mcu_id() << endl;
       cout << cSimConfInfo2.chairman().mcu_id() << endl;
      return 0;
}

输出结果


{"speaker":{"mainType":9,"subType":7,"mcuId":33,"eqpId":11,"confdriId":3,"confIdx":1},"chairman":{"mainType":22,"subType":2,"mcuId":22,"eqpId":22,"confdriId":2,"confIdx":2},"e164str":"0512110","tmtarr":[{"mainType":9,"subType":7,"mcuId":33,"eqpId":11,"confdriId":3,"confIdx":1},{"mainType":22,"subType":2,"mcuId":22,"eqpId":22,"confdriId":2,"confIdx":2}]}

33

22


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

protobuf下载与安装+ protobuf 与json相互转换方法 的相关文章

  • protobuf的ParseFromArray 解析失败的问题

    前段时间 xff0c 在解析定义的Message时 xff0c 总是提示解析失败 xff0c 刚开始以为是消息号与消息没有对应上 xff0c 检查后发现消息号与消息是对应的 后来发现消息的一个字段定义为required 但是没有赋值 xff
  • ProtoBuf 与 gRPC 你需要知道的知识

    ProtoBuf 是一套接口描述语言 xff08 IDL xff09 和相关工具集 xff08 主要是 protoc xff0c 基于 C 43 43 实现 xff09 xff0c 类似 Apache 的 Thrift xff09 用户写好
  • ubuntu 18.04安装protobuf

    今天需要安装protobuf 在网上搜了一篇教程 xff0c 但是篇幅太长 xff0c 于是对其进行简化一下 原文 1 96 96 96 git clone https github com protocolbuffers protobuf
  • 通讯协议序列化解读(一) Protobuf详解教程

    https www cnblogs com tohxyblog p 8974641 html
  • vs2015的OpenCV3.2.0编译

    我们希望添加第三方功能模块和库或者针对特定cpu和gpu的编译调整优化选项 这样的需求就需要自己去编译opencv了 准备东西 opencv opencv contrib cmake 还有两个文件 因为可能是国内的原因 在configure
  • protobuf快速上手

    protobuf快速上手 一 序列化与反序列化 序列化与反序列化的场景 常用的工具 二 protobuf工作原理 三 快速上手 protobuf中的数据类型 proto文件格式 编译选项 快速上手 四 通讯录demo 编写proto文件 编
  • protobuf的序列化和反序列化的分析

    一 protobuf的optional 数据类型序列化分析 1 optional 的protobuf的文件 格式 syntax proto2 message test proto optional int32 proto1 1 option
  • Protobuf如何集成到C++环境中(VS2019)

    文章目录 前言 下载安装Protocol Compiler和Protobuf Runtime 下载 编译安装Protobuf Runtime 使用Protocol Compiler生成 cc和 h文件 VS工程的配置 测试 前言 Proto
  • gRPC那点事

    什么是 gRPC gRPC gRPC Remote Procedure Call 是一种高性能 开源的远程过程调用 RPC 框架 它允许分布在不同计算机上的应用程序能够像调用本地方法一样进行通信 从而实现了在分布式系统中进行高效的通信 传统
  • 解决protobuf内存泄漏的3种办法

    1 protobuf对象是如何释放 数组 内存的 毫无疑问是 通过调用析构函数 只要让protobuf定义的对象调用析构函数 无论嵌套了多少层数据 包含了多少个数组都可以释放new出来的内存 2 protobuf对象Clear 接口和STL
  • 【Protobuf速成指南】enum类型的使用

    文章目录 2 1枚举类型 一 如何定义枚举类型 二 语法规范 三 重定义问题 四 enum类型相关函数 五 Contact 2 1 改写 六 总结 2 1枚举类型 本系列文章将通过对通讯录项目的不断完善 带大家由浅入深的学习Protobuf
  • protobuf c++编程笔记

    文章目录 字段内容的定义 修饰符 字段类型 引用方式 不同字段的方法 1 optional修饰的基本类型 2 optional修饰的对象类型 3 repeated修饰的基本类型 4 repeated修饰的对象类型 序列化 反序列化 字段内容
  • gRPC的介绍、安装与使用

    1 gRPC是什么 在 gRPC里客户端应用可以像调用本地对象一样直接调用另一台不同的机器上服务端应用的方法 使得您能够更容易地创建分布式应用和服务 与许多 RPC系统类似 gRPC也是基于以下理念 定义一个服务 指定其能够被远程调用的方法
  • protobuf 中复合数据类型的读写

    背景 protobuf 在生成的 C 代码中为 proto 文件中的每个 message 生成了对应的 C 类 并提供了数据成员的读写方法 message 类型读写 message 示例 message Point double lng 1
  • ProtocolBuffers-3.0.0 For Objective C 的快速集成指南

    一 前言 最近调研 Google的Protocol Buffer 在网上看了几篇相关博客 发现他们讲的都比较复杂 所以就想写一篇简单点的文章 配置环境 mac OS 10 11 5 Xcode7 3 二 Protocol Buffer简介
  • protobuf-IOS简单总结(编译、环境搭建)

    什么是protobuf Protocol Buffers are a way of encoding structured data in an efficient yet extensible format Google uses Pro
  • protobuf的介绍、安装与使用

    1 protobuf是什么 protobuf是google旗下的一款平台无关 语言无关 可扩展的序列化结构数据格式 所以很适合用做数据存储和作为不同应用 不同语言之间相互通信的数据交换格式 只要实现相同的协议格式即同一 proto文件被编译
  • Ubuntu 下配置protobuf

    首先得到 protobuf 相应的包文件 在终端上输入如下 wget http protobuf googlecode com files protobuf 2 5 0 tar gz 由于 现在 protobuf 2 5 0 tar gz已
  • Hello! protobuf——编译与安装(c++版)

    目录 前言 一 protobuf是什么 二 protobuf官方路径 三 protobuf编译安装 1 安装前建议 2 cmake使用 1 cmake介绍 2 命令行介绍 3 编译选项介绍 4 cmake安装 3 windows下使用nma
  • 2. IDEA + maven + protobuf配置(on mac)

    1 絮絮叨叨 都说懒惰是人类进步的源泉 有时候想想还真就那么回事 学习了如何使用protoc命令编译 重度依赖IDEA且已经习惯了maven的我 就在想是否能在IDEA中一键编译 proto文件 2 vscode配置protobuf编辑环境

随机推荐