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练习(六)的更多相关文章

  1. Netty之ProtoBuf(六)

    Protocol Buffer的基本使用(六) 一.简介 Protocol Buffer(简称ProtoBuf)是google的一个语言中立,平台中立,可扩展的对结构化的数据进行序列化的一种机制,和X ...

  2. 十六.maven自动化构建protobuf代码依赖

    protobuf在序列化和反序列化中的优势: 1):序列化后体积相比Json和XML很小,适合网络传输2):支持跨平台多语言3):消息格式升级和兼容性还不错4):序列化反序列化速度很快,快于Json的 ...

  3. protobuf中文教程(第一篇)

    声明:本文大部分内容翻译自官方英文文档,其中可能穿插着加入自己的语言用以辅助理解,本文禁止转载. 一.什么是protocol buffers Protocol buffers是一个灵活的.高效的.自动 ...

  4. Mina、Netty、Twisted一起学(六):session

    开发过Web应用的同学应该都会使用session.由于HTTP协议本身是无状态的,所以一个客户端多次访问这个web应用的多个页面,服务器无法判断多次访问的客户端是否是同一个客户端.有了session就 ...

  5. Mina、Netty、Twisted一起学(五):整合protobuf

    protobuf是谷歌的Protocol Buffers的简称,用于结构化数据和字节码之间互相转换(序列化.反序列化),一般应用于网络传输,可支持多种编程语言. protobuf如何使用这里不再介绍, ...

  6. 介绍开源的.net通信框架NetworkComms框架 源码分析(六)SendReceiveOptions

    原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架  作者是英国人  以前是收费的 目前作者已经开源  许可是 ...

  7. (一)Protobuf的Java使用

    学习使用Protobuf,创建java文件 windows : 步骤一:两个文件:proto.exe,  protobuf-Java-2.4.1.jar 步骤二:建立一个工程CreateProtoBu ...

  8. Protobuf的简单介绍、使用和分析

      Protobuf的简单介绍.使用和分析   一.protobuf是什么? protobuf(Google Protocol Buffers)是Google提供一个具有高效的协议数据交换格式工具库( ...

  9. VS下使用Google Protobuf完成SOCKET通信

    如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 出处:如何在Windows环境下的VS中安装使用Google Protobuf完成SOCKET通信 最近一 ...

随机推荐

  1. codevs1281 Xn数列

    题目描述 Description 给你6个数,m, a, c, x0, n, g Xn+1 = ( aXn + c ) mod m,求Xn m, a, c, x0, n, g<=10^18 输入 ...

  2. Hive- 大数据仓库Hive

    什么是 Hive? Hive 是由 FaceBook 开源用于解决少量数据结构化日志的数据统计.Hive是基于 Hadoop 的一个数据仓库工具,可以将结构化的数据文件映射成一张表,并提供类SQL查询 ...

  3. ajax实现聊天室功能

    需求如下: 先死后活. 需求分析,分析思路如图所示: 1.创建数据库 create database chat; create table messages( id int unsigned prim ...

  4. spring boot: Bean的初始化和销毁 (一般注入说明(三) AnnotationConfigApplicationContext容器 JSR250注解)

    import org.springframework.context.annotation.AnnotationConfigApplicationContext; 使用AnnotationConfig ...

  5. html5 tab横向滚动,无滚动条(transform:translate)

    html5 横向滚动,用到了 touchstart.touchmove.touchend 控制修改transform:translate属性;[手机端或者浏览器模拟手机模式才有效果] [转载请注明出处 ...

  6. 【二叉树的递归】03判断二叉树中有没有和为给定值的路径【Path Sum】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树和一个和,判断这个树 ...

  7. Smali文件添加try/catch语句,出现“invalid use of move-exception”异常

    插入代码如下: 捕获到以下异常: 2019-03-18 21:09:35.431 8272-8272/com.xxxx.xxxx E/AndroidRuntime: FATAL EXCEPTION: ...

  8. 使用WindowsAPI实现播放PCM音频的方法

    这篇文章主要介绍了使用WindowsAPI实现播放PCM音频的方法,很实用的一个功能,需要的朋友可以参考下 本文介绍了使用WindowsAPI实现播放PCM音频的方法,同前面一篇使用WindowsAP ...

  9. linux日常管理-查看系统负载

    查看系统的负载常用命令w 16:32::15是系统时间 up 16 min 是开机使用时间 1 user 是登录的用户数 重要 load average:0.00 0.00 0.00 负载分别表示1分 ...

  10. Matlab数据类型的转换

    Matlab中有15种基本数据类型,主要是整型.浮点.逻辑.字符.日期和时间.结构数组.单元格数组以及函数句柄等. 1.整型:(int8:uint8:int16:uint16:int32:uint32 ...