Protobuf安装

参考链接
其他版本:
3.13.0

Protobuf python接口使用

参考链接

Protobuf+Msgpack C++接口使用

写一个proto文件,plan.proto

syntax = "proto3";
package robot;
message Path {uint32 move_direct = 4; float turn_radius = 5;float move_distance = 6;
}
message Pose {float x = 3;float y = 4;
}
// topic : "planning"
message Planning {uint64 timestamp = 1; Pose start_pose = 3;repeated Path planning_path = 5;
}

生成c++文件

# 单个
protoc plan.proto --cpp_out=./
# 多个文件时
ls . |grep ".proto" |xargs -n 1 protoc -I=. --cpp_out=../proto_cpp

序列化和反序列化

#include "planning.pb.h"
using namespace std;
int mian()
{// 【1】填充数据robot::Planning planPB;// uint64 timestamp = 1; planPB.set_timestamp(165555555);// repeated Path planning_path = 5;robot::Path *p1 = planPB.add_planning_path();p1->set_move_direct(1);p1->set_turn_radius(10);p1->set_move_distance(M_PI/2*10);planPB.add_planning_path()->set_move_direct(0);// Pose start_pose = 3;robot::Pose* pose = planPB.mutable_start_pose();pose->set_x(1);pose->set_y(2);// 【2】pb序列化string enc;planPB.SerializeToString(&enc);// msgpack序列化msgpack::sbuffer sbuf;msgpack::pack(sbuf, enc); cout << sbuf.size() << endl;// 【3】msgpack序列化auto handle = msgpack::unpack(          // 反序列化sbuf.data(), sbuf.size());  // 输入二进制数据auto obj = handle.get();                // 得到反序列化对象string dec;obj.convert(dec);// 转换反序列化的数据// 【4】反序列化robot::Planning planMsg;planMsg.ParseFromString(dec);// 【5】读取数据int timestamp = planMsg.timestamp();float x = planMsg.start_pose().x();for (size_t i = 0; i < planMsg.planning_path_size(); i++){size_t moveDirect = planMsg.planning_path(i).move_direct();float turnRadius = planMsg.planning_path(i).turn_radius();float moveDistance = planMsg.planning_path(i).move_distance();}
}

编译

cmake_minimum_required(VERSION 3.0.2)
project(test)set(msgpack_INCLUDE_DIRS ./third_party/msgpack_2_1_5/include)
set(protobuf_libs_DIR ./third_party/protobuff_3_13_0/lib)
set(protobuf_INCLUDE_DIRS ./third_party/protobuff_3_13_0/include)aux_source_directory(./third_party/proto_cpp/ PROTO_CPP)include_directories(${msgpack_INCLUDE_DIRS}${protobuf_INCLUDE_DIRS}./third_party/include./third_party/proto_cpp/
)link_directories(${protobuf_libs_DIR}./third_party/lib  # lib: 库文件的相对路径
)add_executable(test mian.cpp ${PROTO_CPP})target_link_libraries(testprotobuf
)
mkdir build && cd build && cmake .. && make

msgpack

C++ 自定义数据结构时的使用用法
python接口

Protobuf 转 Json

C++

#include <fstream>
#include <string>
#include <google/protobuf/util/json_util.h> //proto2json#include "Planning.pb.h"bool Proto2Json(const google::protobuf::Message& message, std::string& json)
{google::protobuf::util::JsonPrintOptions options;options.add_whitespace = true;options.always_print_primitive_fields = true;return MessageToJsonString(message, &json, options).ok();
}
int main()
{// 【1】填充数据robot::Planning planPB;// uint64 timestamp = 1; planPB.set_timestamp(165555555);// repeated Path planning_path = 5;robot::Path *p1 = planPB.add_planning_path();p1->set_move_direct(1);p1->set_turn_radius(10);p1->set_move_distance(M_PI/2*10);planPB.add_planning_path()->set_move_direct(0);// Pose start_pose = 3;robot::Pose* pose = planPB.mutable_start_pose();pose->set_x(1);pose->set_y(2);// 【2】pb序列化string enc;planPB.SerializeToString(&enc);// msgpack序列化msgpack::sbuffer sbuf;msgpack::pack(sbuf, enc); cout << sbuf.size() << endl;// 【3】msgpack序列化auto handle = msgpack::unpack(          // 反序列化sbuf.data(), sbuf.size());  // 输入二进制数据auto obj = handle.get();                // 得到反序列化对象string dec;obj.convert(dec);// 转换反序列化的数据// 【4】反序列化robot::Planning planMsg;planMsg.ParseFromString(dec);// 【5】读取数据int timestamp = planMsg.timestamp();float x = planMsg.start_pose().x();for (size_t i = 0; i < planMsg.planning_path_size(); i++){size_t moveDirect = planMsg.planning_path(i).move_direct();float turnRadius = planMsg.planning_path(i).turn_radius();float moveDistance = planMsg.planning_path(i).move_distance();}// 【6】转json并存储std::string msg1savePath = "./msg1.json";std::ofstream msg1(msg1savePath);std::string jsonString;Proto2Json(planMsg, jsonString);msg1 << std::setw(4) << jsonString << "," << std::endl;msg1.close();return 0;
}

python

from google.protobuf import json_format
import Planning_pb2
import json#【1】填充数据
msg = Planning_pb2.Planning()
path = msg.planning_path.add()path.move_direct = 0;
path.turn_radius = -2;
path.move_direct = 3;msg.timestamp = 1666666666333
msg.start_pose.x = 0
msg.start_pose.y = 0
#【2】pb序列化
serializeToString = msg.SerializeToString()
print(serializeToString,type(serializeToString))# 【3】 反序列化
msg.ParseFromString(serializeToString)
# 【4】取值
for path in msg.planning_path:print(path.move_direct)
print(msg.timestamp)
# 【5】转json存本地
save_result = open(file = ‘./plan.txt’, mode = 'w')
plan_json = json_format.MessageToDict(msg, preserving_proto_field_name=True)
save_result.write(json.dumps(plan_json))
save_result.close()

参考

protobuf+msgpack使用笔记相关推荐

  1. 嵌入式大杂烩 | 分享几个非常实用的开源项目

    1024G 嵌入式资源大放送!包括但不限于C/C++.单片机.Linux等.关注微信公众号[嵌入式大杂烩],回复1024,即可免费获取! 前言 本次分享几个实用的.值得学习使用的嵌入式相关开源项目,下 ...

  2. 关于帧同步系统(一)

    本概述都是基于自己经历的项目见解,不一定是正确的.这是一个系列总结,以后会补充更多 帧同步的概述 帧同步,其实是基本帧更新的同步方式,但并不是要求每一帧都同步数据,而要求多端,他们执行在指定的帧号上, ...

  3. Protobuf学习笔记

    Protobuf学习笔记 Posted by iamxhuon 2012/05/22 Leave a comment (0)Go to comments Protocol buffers是什么? 首先 ...

  4. 冰冰学习笔记:简单了解protobuf

    欢迎各位大佬光临本文章!!! 还请各位大佬提出宝贵的意见,如发现文章错误请联系冰冰,冰冰一定会虚心接受,及时改正. 本系列文章为冰冰学习编程的学习笔记,如果对您也有帮助,还请各位大佬.帅哥.美女点点支 ...

  5. Go语学习笔记 - grpc server/client protobuf | 从零开始Go语言

    目录 创建Proto文件 生成proto文件对应的go文件 创建服务结构体 创建客户端测试 小结 学习笔记,写到哪是哪. 上一篇是写的redis操作来着,最近主要研究了一下grpc. 在玩grpc的过 ...

  6. protobufjs 命令执行_【原码笔记】-- protobuf.js 与 Long.js

    protobuf.js的结构和webpack的加载之后的结构很相似.这样的模块化组合是个不错的结构方式.1个是适应了不同的加载方式,2个模块直接很独立.webpack的功能更全一点.但如果自己封装js ...

  7. ProtoBuf使用笔记

    ProtoBuf 作为一种跨平台.语言无关.可扩展的序列化结构数据的方法,已广泛应用于网络数据交换及存储. 1. Proto定义 1.1. 说明 required表示该字段必须定义: optional ...

  8. protobuf java_ProtoBuf for java使用笔记 | 学步园

    三.新建项目ProtobufDemo.包名:com.protobufdemo.protobuf. 四.把上面的jar包跟exe放到工程目录下.新建文件夹:proto.在其下新建文件:msg.proto ...

  9. C++|Java工作笔记-google protobuf基本使用

    目录 前言 protoc生成 Java相关 C++相关 前言 这里主要是生成序列号,在我所做的项目中,一般是把数据序列化后扔到消息总线上,消费者读取了,自行解析. 个人感觉这种方式比Json和XML都 ...

最新文章

  1. Bootstrap笔记
  2. Linux内核TCP/IP参数分析与调优
  3. Linux上如何查看物理CPU个数,核数,线程数
  4. 虚拟机——虚拟机的初步认识
  5. VTK修炼之道17:图像基本操作_图像信息的访问与修改(vtkImageData)
  6. 在JAX-RS中处理异步请求中的超时
  7. ubuntu下android源码编译环境,ubuntu12.04 64位上搭建android源码编译环境
  8. 组合体视图的画图步骤_(完整版)组合体视图画法教案
  9. clickhouse 小结1
  10. 我使用的网址--Hadoop
  11. 21.实例 --- location
  12. Java基础面试题集(二)
  13. iOS 开发笔记-plist使用
  14. 文字排版中的字号尺寸对照表(清晰、准确(含两个版本对比)、可复制)
  15. 无人机pid调节顺口溜
  16. 那年的夏天——致毕业
  17. 1人抵1万名黑客的阿里女守护神,私底下竟然是这个样子!
  18. Flutter2 的 Sound null safety ?!以及发布pub上面的null safety标签实现
  19. 利用ArcGIS软件将csv文件转换为shp格式
  20. LinkedList一定比ArrayList的插入和删除效率高吗

热门文章

  1. Redis实现分布式锁原理(面试重点)
  2. 从七个角度诠释选购净水器的诀窍
  3. 神卓互联在 IoT 项目中的应用:分享了一位开发者如何利用神卓互联实现远程控制智能设备的案例。
  4. tplink android管理软件,TP-LINK路由器管理
  5. 安卓Android家教信息平台软件app毕业设计
  6. 职称计算机证书A级,职称计算机考试A类和B类有什么区别?
  7. 地铁换乘 java_java实现乘地铁方案的最优选择(票价,距离)
  8. HDMI转CSI转换板给你做出来了
  9. Shell整理(持续更新中)
  10. 河北省计算机能不能跨专业接本,河北专接本跨专业的要求有哪些