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;
}
原文地址:https://www.cnblogs.com/silvermagic/p/9087625.html