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;
    }
    
  • 相关阅读:
    C# 模拟浏览器请求
    关于获取时间后,时间格式为几天前,几小时前格式转化
    关于通用的C#后台获取前台页面的标签的正则表达式
    关于getHTML()方法和getHtmlAjax()方法 GetHttpLength, 清除HTML标签
    性能测试术语
    聚合报告中90% Line涉及到百分位数的概念
    使用Windows的cmd运行(或通过批处理启动)Python项目(多目录项目)提示找不到模块的解决办法
    OSError: [WinError 6] 句柄无效的解决办法
    python中日志输出重复的解决办法
    截图方法get_screenshot_as_file()注意点
  • 原文地址:https://www.cnblogs.com/silvermagic/p/9087625.html
Copyright © 2011-2022 走看看