zoukankan      html  css  js  c++  java
  • google protobuf 使用心德


    最近我们开发过程中用到 google/protobuf,因为之前没有接触过。网上的资料都是互相复制转载的。基本上都是讲 protobuf 怎么赋值怎么取值。。。

    主要的消息映射讲的微乎其微。。。。。我根据网上说的做了下。。。问题我纠缠死了。。。-_-#  OK  看代码

     1 GOOGLE_PROTOBUF_VERIFY_VERSION;
    2 string str = "";
    3
    4 tutorial::AddressBook address_book;
    5 tutorial::Person *person = tutorial::Person::default_instance().New();
    6 person->set_name("Ambition");
    7 person->set_id(1000);
    8 person->set_email("ambitiongxb@foxmail.com");
    9 for (int i=0;i<4;i++)
    10 {
    11 tutorial::Person::PhoneNumber* phone_number = person->add_phone();
    12 phone_number->set_number("13800138000");
    13 if (i == 0)
    14 phone_number->set_type(tutorial::Person::HOME);
    15 else if(i==1)
    16 phone_number->set_type(tutorial::Person::WORK);
    17 else if(i==2)
    18 phone_number->set_type(tutorial::Person::MOBILE);
    19 }

      上面的代码就是赋值操作。。。下面说下消息映射。。。

    1 person->SerializeToString(&str); //序列化
    2 typedef tutorial::Person T;
    3 std::string type_name = T::descriptor()->full_name(); // 得到包名字
    4 ::google::protobuf::Message* prototype = createMessage(type_name); //根据包名得到(抽象)消息
    5 prototype->ParseFromString(str); //反序列化
    6 tutorial::Person *new_obj = (tutorial::Person*)prototype;

      

  • 相关阅读:
    C# UrlDecode将+替换为空格问题
    Hashtable无序,用Dictionary代替
    Oracle查找Web执行SQL
    远程连接Oracle服务器
    asp.net core网站SSL nginx配置
    Supervisor踩过的坑
    centos nginx配置支持WebSocket(signalR)
    SignalR在asp.net core下使用
    Hangfire 在asp.net core环境的使用
    liteUploader上传控件的封装使用
  • 原文地址:https://www.cnblogs.com/GameDeveloper/p/2111466.html
Copyright © 2011-2022 走看看