C++中protobuf Message与JSON的互相转换

C++中protobuf Message与JSON的互相转换

环境:

复制代码
protobuf: v27.3(2024-08-01)
abseil: 20240722.0

文章目录

  • [C++中protobuf Message与JSON的互相转换](#C++中protobuf Message与JSON的互相转换)
    • 前言
    • [1. 编写通讯录addressbook.proto](#1. 编写通讯录addressbook.proto)
    • [2. 编译](#2. 编译)
    • [3. C++中测试protobuf与json的转换](#3. C++中测试protobuf与json的转换)
    • [4. 结果](#4. 结果)

前言

PB转JSON:Protocol Buffers的Message对象转换为 JSON 格式字符串

JSON转PB:JSON格式字符串解析为 Protocol Buffers 的Message对象

protobuf中可以通过MessageToJsonString和JsonStringToMessage函数完成上述转换。

注意:MessageToJsonString和JsonStringToMessage函数仅支持Message对象,不支持MessageLite对象(后面会介绍MessageLite和JSON的转换)。

1. 编写通讯录addressbook.proto

复制代码
syntax = "proto3";

package com.test;

message Person {
  string name = 1;
  int32 age = 2;
  string phone = 3;
}

message AddressBook{
    repeated Person people = 1;
}

2. 编译

复制代码
protoc -I=. --cpp_out=. addressbook.proto

tree
.
+--- addressbook.pb.cc
+--- addressbook.pb.h
+--- addressbook.proto
+--- protoc.exe

3. C++中测试protobuf与json的转换

main.cpp

复制代码
#include <iostream>

#include <google/protobuf/util/json_util.h> // MessageToJsonString JsonStringToMessage

#include "addressbook.pb.h"

int main(int argc, char *argv[])
{
    com::test::AddressBook addressbook1;
    com::test::Person* person1 = addressbook1.add_people();
    person1->set_name("xiaoming");
    person1->set_age(30);
    person1->set_phone("13012345678");

    // PB to JSON
    std::string result1;
    google::protobuf::json::MessageToJsonString(addressbook1, &result1);
    std::cout << "AddressBook From PB - " << result1 << std::endl;

    // JSON to PB
    std::string jsonStr = R"({"people":[{"name":"xiaohong","age":31,"phone":"13112345678"}]})";
    com::test::AddressBook addressbook2;
    google::protobuf::json::JsonStringToMessage(jsonStr, &addressbook2);

    std::string result2;
    google::protobuf::json::MessageToJsonString(addressbook2, &result2);
    std::cout << "AddressBook From JSON - " << result2 << std::endl;

    getchar();
    return 0;
}

CMakeLists.txt

复制代码
cmake_minimum_required(VERSION 3.10)

project(main)

include_directories(.) # addressbook.pb.h

# protobuf
add_definitions(-DPROTOBUF_USE_DLLS)
include_directories(include)
link_directories(lib)

add_executable(${PROJECT_NAME} main.cpp addressbook.pb.cc)

target_link_libraries(${PROJECT_NAME} libprotobuf abseil_dll)

目录结构

复制代码
tree
.
+--- include
+--- lib
+--- addressbook.pb.cc
+--- addressbook.pb.h
+--- addressBook.proto
+--- CMakeLists.txt
+--- main.cpp

4. 结果

复制代码
AddressBook From PB - {"people":[{"name":"xiaoming","age":30,"phone":"13012345678"}]}
AddressBook From Json - {"people":[{"name":"xiaohong","age":31,"phone":"13112345678"}]}
相关推荐
什么都想学的阿超2 小时前
【PostgreSQL 02】PostgreSQL数据类型革命:JSON、数组与地理信息让你的应用飞起来
数据库·postgresql·json
黄油奥特曼4 小时前
Sublime Text 4格式化JSON无效的解决方法
json·sublime·pretty json
趣浪吧9 小时前
【JSON-to-Video】设置背景视频片断
json·aigc·音视频·视频
白皎16 小时前
立志成为一名优秀测试开发工程师(第十一天)—Postman动态参数/变量、文件上传、断言策略、批量执行及CSV/JSON数据驱动测试
json·postman
在代码的海洋中寻找亚特兰蒂斯18 小时前
AJAX对于XML和JSON的处理
xml·ajax·json
爱出名的狗腿子1 天前
vscode + cmake + ninja+ gcc 搭建MCU开发环境
ide·vscode·单片机·c·cmake·gcc·ninja
wtsolutions1 天前
Excel to JSON 插件 2.4.0 版本更新
json·excel
线条12 天前
Flume 自定义拦截器开发实战:添加时间戳与 JSON 处理
大数据·json·flume
cykaw25902 天前
QT-JSON
qt·json
CCI3442 天前
报错SvelteKitError: Not found: /.well-known/appspecific/com.chrome.devtools.json
javascript·chrome·json