C++中protobuf 动态加载.proto文件

C++中protobuf 动态加载.proto文件

环境:

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

文章目录

  • [C++中protobuf 动态加载.proto文件](#C++中protobuf 动态加载.proto文件)
    • 前言
    • [1. 通讯录 addressbook.proto](#1. 通讯录 addressbook.proto)
    • [2. C++中测试动态加载.proto文件并输出json](#2. C++中测试动态加载.proto文件并输出json)
    • [3. 结果](#3. 结果)

前言

protobuf动态加载.proto文件,可以不生成cpp文件的情况下操作Message对象。

动态加载方式的性能会稍差一些。

1. 通讯录 addressbook.proto

addressbook.proto

复制代码
syntax = "proto3";

option optimize_for = LITE_RUNTIME;  // MessageLite

package com.test;

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

message AddressBook {
  repeated Person people = 1;
}

2. C++中测试动态加载.proto文件并输出json

main.cpp

复制代码
#include <iostream>

#include <google/protobuf/compiler/importer.h>
#include <google/protobuf/dynamic_message.h>

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


int main(int argc, char *argv[])
{
    // 1. Setup Importer to dynamically load .proto files
    google::protobuf::compiler::DiskSourceTree sourceTree;
    sourceTree.MapPath("", ".");
    google::protobuf::compiler::MultiFileErrorCollector* errorCollector = nullptr;
    google::protobuf::compiler::Importer importer(&sourceTree, errorCollector);

    // 2. Import the .proto file
    const google::protobuf::FileDescriptor* addressBookFileDescriptor = importer.Import("addressbook.proto");
    if (!addressBookFileDescriptor) 
    {
    std::cerr << "Failed to import .proto file." << std::endl;
    return -1;
    }

    // 3. Find the message descriptor
    const google::protobuf::Descriptor* addressBookMessageDescriptor = addressBookFileDescriptor->FindMessageTypeByName("AddressBook");
    if (!addressBookMessageDescriptor) {
    std::cerr << "Message type 'AddressBook' not found in .proto file." << std::endl;
    return -1;
    }

    // 4. Create a dynamic message
    google::protobuf::DynamicMessageFactory factory;
    const google::protobuf::Message* prototype = factory.GetPrototype(addressBookMessageDescriptor);
    google::protobuf::Message* message = prototype->New();

    // 5. JSON to PB Message
    std::string jsonStr = R"({"people":[{"name":"xiaoming","age":30,"phone":"13012345678"}]})";
    google::protobuf::json::JsonStringToMessage(jsonStr, message);
    
    // 6. printf PB JSON
    std::string result;
    google::protobuf::json::MessageToJsonString(*message, &result);
    std::cout << "Dynamic Message From JSON - " << result << std::endl;

    // 7. clear
    delete message;

    getchar();
    return 0;
}

CMakeLists.txt

动态加载依赖libprotoc

复制代码
cmake_minimum_required(VERSION 3.10)

project(main)

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

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} libprotoc libprotobuf abseil_dll)

目录结构

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

3. 结果

复制代码
Dynamic Message From JSON - {"people":[{"name":"xiaoming","age":30,"phone":"13012345678"}]}
相关推荐
渡我白衣8 分钟前
计算机组成原理(8):各种码的作用详解
c++·人工智能·深度学习·神经网络·其他·机器学习
小李小李快乐不已21 分钟前
二叉树理论基础
数据结构·c++·算法·leetcode
仰泳的熊猫25 分钟前
1149 Dangerous Goods Packaging
数据结构·c++·算法·pat考试
ALex_zry25 分钟前
现代C++如何解决传统内存分配器的核心痛点
java·c++·spring
wangnaisheng38 分钟前
彩虹编码映射实现:C++与C#
c++·c#
waves浪游40 分钟前
进程控制(下)
linux·运维·服务器·开发语言·c++
兵哥工控44 分钟前
mfc两个线程的创建、启动、安全结束实例
c++·mfc·多线程·线程安全退出
小龙报1 小时前
【算法通关指南:算法基础篇 】双指针专题:1.唯一的雪花 2.逛画展 3.字符串 4.丢手绢
c语言·数据结构·c++·人工智能·深度学习·算法·信息与通信
Yusei_05231 小时前
Redis核心特性与应用全解析
开发语言·数据库·c++·redis·缓存
Larry_Yanan9 小时前
Qt多进程(一)进程间通信概括
开发语言·c++·qt·学习