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;
}
ProtoBuf练习(六)的更多相关文章
- Netty之ProtoBuf(六)
Protocol Buffer的基本使用(六) 一.简介 Protocol Buffer(简称ProtoBuf)是google的一个语言中立,平台中立,可扩展的对结构化的数据进行序列化的一种机制,和X ...
- 十六.maven自动化构建protobuf代码依赖
protobuf在序列化和反序列化中的优势: 1):序列化后体积相比Json和XML很小,适合网络传输2):支持跨平台多语言3):消息格式升级和兼容性还不错4):序列化反序列化速度很快,快于Json的 ...
- protobuf中文教程(第一篇)
声明:本文大部分内容翻译自官方英文文档,其中可能穿插着加入自己的语言用以辅助理解,本文禁止转载. 一.什么是protocol buffers Protocol buffers是一个灵活的.高效的.自动 ...
- Mina、Netty、Twisted一起学(六):session
开发过Web应用的同学应该都会使用session.由于HTTP协议本身是无状态的,所以一个客户端多次访问这个web应用的多个页面,服务器无法判断多次访问的客户端是否是同一个客户端.有了session就 ...
- Mina、Netty、Twisted一起学(五):整合protobuf
protobuf是谷歌的Protocol Buffers的简称,用于结构化数据和字节码之间互相转换(序列化.反序列化),一般应用于网络传输,可支持多种编程语言. protobuf如何使用这里不再介绍, ...
- 介绍开源的.net通信框架NetworkComms框架 源码分析(六)SendReceiveOptions
原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架 作者是英国人 以前是收费的 目前作者已经开源 许可是 ...
- (一)Protobuf的Java使用
学习使用Protobuf,创建java文件 windows : 步骤一:两个文件:proto.exe, protobuf-Java-2.4.1.jar 步骤二:建立一个工程CreateProtoBu ...
- Protobuf的简单介绍、使用和分析
Protobuf的简单介绍.使用和分析 一.protobuf是什么? protobuf(Google Protocol Buffers)是Google提供一个具有高效的协议数据交换格式工具库( ...
- VS下使用Google Protobuf完成SOCKET通信
如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 出处:如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 最近一 ...
随机推荐
- ios app被自己从应用商店下架后可以再恢復上架吗
好像没有企业能阻挡苹果的下架决定,毕竟这是它的地盘.不管是已经恢复上架的百度.腾讯.优酷.人人游戏,还是至今没有下文的360.金山和PPS,也不管这些企业在中国乃至全球互联网行业的地位如何,下架原因只 ...
- Codeforces 479E Riding in a Lift:前缀和/差分优化dp
题目链接:http://codeforces.com/problemset/problem/479/E 题意: 有一栋n层的房子. 还有一个无聊的人在玩电梯,每次玩电梯都会从某一层坐到另外一层. 他初 ...
- jQuery应用之eraser.js使用,实现擦除、刮刮卡效果
jquery.eraser是一款使用鼠标或触摸的动作来擦除画布显示真正图片的插件.jquery.eraser插件的原理是用一个画布遮住图片,然后根据触摸或鼠标输入来擦除画布显示图片,您可以在参数中指定 ...
- Angular Js 控制器
在Angularjs中用ng-controller指令定义了应用程序中的控制器:例如: <div ng-app="myApp" ng-controller="myC ...
- 英语发音规则---字母组合oo的发音规律
英语发音规则---字母组合oo的发音规律 一.总结 一句话总结:在英语单词中,字母组合oo多数读长音/u:/,少数读短音/ʊ/.另外,还有极少数的特殊情况读/ʌ/, 在英语单词中,字母组合oo多数读长 ...
- ZOJ 2724 Windows Message Queue (二叉堆,优先队列)
思路:用优先队列 priority_queue,简单 两种方式改变队列 的优先级 (默认的是从大到小) #include<iostream> #include<queue> # ...
- C++之this指针与另一种“多态”
一.引入 定义一个类的对象,首先系统已经给这个对象分配了空间,然后会调用构造函数(说明:假设存在构造函数--2010.9.5修正). 一个类有多个对象,当程序中调用对象的某个函数时,有可能要访问到这个 ...
- python密钥登录主机
#!/usr/bin/python # -*- coding:utf-8 -*- ################################### # # 检查主机的损坏磁盘 # ####### ...
- BZOJ4976: [Lydsy1708月赛]宝石镶嵌
BZOJ4976: [Lydsy1708月赛]宝石镶嵌 https://lydsy.com/JudgeOnline/problem.php?id=4976 分析: 本来是从\(k\le 100\)这里 ...
- 【LeetCode】066. Plus One
题目: Given a non-negative integer represented as a non-empty array of digits, plus one to the integer ...