zoukankan      html  css  js  c++  java
  • ProtoBuf练习(六)

    JSON类型

    工程目录结构

    $ ls proto/
    

    proto文件

    $ cat proto/style.proto
    syntax = "proto3";
    
    import "google/protobuf/timestamp.proto";
    
    message TIndent
    {
        uint32 length = 1;
        bool use_space = 2;
    }
    
    message style
    {
        string encoding = 1;
        repeated string plugins = 2;
        TIndent indent = 3;
        google.protobuf.Timestamp modify = 4;
    }
    

    读写源文件

    $ cat reader.cpp
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <google/protobuf/util/json_util.h>
    #include "style.pb.h"
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        fstream input("./log", ios::in | ios::binary);
        cout << "Deserialize start." << endl;
    
        style s;
        if (!s.ParseFromIstream(&input))
        {
            cout << "Deserialize failed." << endl;
            return -1;
        }
        std::string output;
        google::protobuf::util::MessageToJsonString(s, &output);
        cout << output << endl;
    
        cout << "Deserialize end." << endl;
        input.close();
        return 0;
    }
    
    $ cat writer.cpp
    #include <fstream>
    #include <iostream>
    #include <json/json.h>
    #include <google/protobuf/util/time_util.h>
    #include "style.pb.h"
    
    using namespace std;
    using namespace google::protobuf::util;
    
    const char *json_str = "{ 
        "encoding" : "UTF-8", 
        "plug-ins" : [ 
            "python", 
            "c++", 
            "ruby" 
            ], 
        "indent" : {"length" : 3, "use_space": true }, 
        "modify" : "2017-01-21T00:00:00Z" 
    }";
    
    int main(int argc, char *argv[])
    {
        style s;
        Json::Value root;
        Json::Reader reader;
        if (!reader.parse(json_str, root))
        {
            cout << "JSON parse failed." << endl;
            return -1;
        }
    
        s.set_encoding(root.get("encoding", "GBK" ).asString());
        const Json::Value plugins = root["plug-ins"];
        for (int i = 0; i < plugins.size(); ++i)
            s.add_plugins(plugins[i].asString());
        TIndent *iter = s.mutable_indent();
        iter->set_length(root["indent"]["length"].asInt());
        iter->set_use_space(root["indent"]["length"].asBool());
        google::protobuf::Timestamp *t = s.mutable_modify();
        google::protobuf::util::TimeUtil::FromString(root["modify"].asString(), t);
    
        fstream output("./log", ios::out | ios::trunc | ios::binary);
        cout << "Serialize start." << endl;
        if (!s.SerializeToOstream(&output))
            {
                    cout << "Serialize failed." << endl;
                    return -1;
            }
        output.close();
        cout << "Serialize end." << endl;
        return 0;
    }
    

    读写源文件(解析不使用jsoncpp)

    $ cat reader.cpp
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <google/protobuf/util/json_util.h>
    #include "style.pb.h"
    
    using namespace std;
    using namespace google::protobuf::util;
    
    int main(int argc, char *argv[])
    {
        fstream input("./log", ios::in | ios::binary);
        cout << "Deserialize start." << endl;
    
        style s;
        if (!s.ParseFromIstream(&input))
        {
            cout << "Deserialize failed." << endl;
            return -1;
        }
        std::string output;
        JsonPrintOptions opts;
        opts.add_whitespace = true;
        cout << MessageToJsonString(s, &output, opts) << endl;
        cout << output << endl;
    
        cout << "Deserialize end." << endl;
        input.close();
        return 0;
    }
    
    $ cat writer.cpp
    #include <fstream>
    #include <iostream>
    #include <google/protobuf/util/json_util.h>
    #include "style.pb.h"
    
    using namespace std;
    using namespace google::protobuf::util;
    
    const char *json_str = "{ 
        "encoding" : "UTF-8", 
        "plug-ins" : [ 
            "python", 
            "c++", 
            "ruby" 
            ], 
        "indent" : {"length" : 3, "use_space": true }, 
        "modify" : "2017-01-21T00:00:00Z" 
    }";
    
    int main(int argc, char *argv[])
    {
        style s;
        JsonParseOptions opts;
        opts.ignore_unknown_fields = true;
        cout << JsonStringToMessage(json_str, &s, opts) << endl;
    
        fstream output("./log", ios::out | ios::trunc | ios::binary); 
        cout << "Serialize start." << endl;
        if (!s.SerializeToOstream(&output))
            {
                    cout << "Serialize failed." << endl;
                    return -1;
            }
        output.close();
        cout << "Serialize end." << endl;
        return 0;
    }
    
  • 相关阅读:
    ubuntu 安装精简桌面; VNC; vncserver 配置
    2019-11-29-Roslyn-通过-NuGet-库修改应用程序入口函数
    2019-11-29-C#-字典-Dictionary-的-TryGetValue-与先判断-ContainsKey-然后-Get-的性能对比
    2019-11-29-WPF-测试触摸设备发送触摸按下和抬起不成对
    2019-11-29-浅谈-Windows-桌面端触摸架构演进
    2019-11-29-C#-通过编程的方法在桌面创建回收站快捷方式
    2019-11-29-C#-直接创建多个类和使用反射创建类的性能
    2019-11-29-WPF-客户端开发需要知道的触摸失效问题
    2019-11-29-asp-dotnet-core-通过图片统计-csdn-用户访问
    2019-11-29-逗比面试官成长路线-如何让被面试者觉得糟心
  • 原文地址:https://www.cnblogs.com/silvermagic/p/9087625.html
Copyright © 2011-2022 走看看