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"}]}
相关推荐
Martin-Luo3 天前
Vue3 通过json配置生成查询表单
javascript·vue.js·json
星尘库3 天前
后端json数据反序列化枚举类型不匹配的错误
json
BXCQ_xuan3 天前
软件工程实践四:MyBatis-Plus 教程(连接、分页、查询)
spring boot·mysql·json·mybatis
王维志3 天前
LiteDB详解
数据库·后端·mongodb·sqlite·c#·json·database
ID_180079054733 天前
Python采集京东店铺所有商品数据,json数据返回
json
ljh5746491194 天前
mysql 必须在逗号分隔字符串和JSON字段之间二选一,怎么选
数据库·mysql·json
小孔龙4 天前
02.Kotlin Serialization 属性序列化控制
kotlin·json
Cachel wood4 天前
信息检索、推荐系统模型排序质量指标:AP@K和MAP@K
windows·搜索引擎·json·推荐系统·搜索
tebukaopu1484 天前
json文件转excel
json·excel
小孔龙5 天前
01.Kotlin Serialization - 基础用法
kotlin·json