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通信 最近一 ...
随机推荐
- JAVA发送手机短信
<p><span>JAVA发送手机短信,流传有几种方法:(1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册 ...
- java 中的拦截器和过滤器
区别: 1.拦截器是基于java的反射机制的,而过滤器是基于函数回调 2.过滤器依赖与servlet容器,而拦截器不依赖与servlet容器 3.拦截器只能对action请求起作用,而过滤器则可以对几 ...
- 分享知识-快乐自己:FastDFS 上传 java 源码
FastDFS 上传 java 源码:点我下载源码 首先导入 POM 文件:解决 maven 不能下载 fastdfs-client-java JAR <dependency> <g ...
- 机器学习(二十四)— 偏差Bias 与方差Variance
1.首先 Error = Bias + Variance Error反映的是整个模型的准确度, Bias反映的是模型在样本上的输出与真实值之间的误差,即模型本身的精准度, Variance反映的是模 ...
- STL defalloc.h
defalloc.h . // Filename: defalloc.h . . // Comment By: 凝霜 . // E-mail: mdl2009@vip.qq.com . // Blog ...
- 本地未安装Oracle数据库,如何连接远程Oracle数据库
方法一:用Navicat Premium连接 注意,这里用的要是黄色的版本,而不是只针对Mysql的绿色版本 工具栏选择[工具]-[选项],点击[其他-OCI] 你会发现有个OCI librar ...
- 【JVM】java棧
java棧和函数调用的关系图 [名词解释]--->java棧是一块线程的私有空间--->java的棧是先进后出的数据结构.函数返回,则该函数的棧帧被弹出.--->一个函数对应一个棧帧 ...
- Nested loops、Hash join、Sort merge join(三种连接类型原理、使用要点)
nested loop 嵌套循环(原理):oracle从较小结果集(驱动表.也可以被称为outer)中读取一行,然后和较大结果集(被侦查表,也可以叫做inner)中的所有数据逐条进行比较(也是等值连接 ...
- JS 获取json长度
var keleyijson={"plug1":"myslider","plug2":"zonemenu"," ...
- loadrunner手动生成脚本函数
1.点击insert