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. node向html模板发送数据

    node向html模板发送数据 给模板传递数据 router.get('/', function(req, res, next) { res.render('index', { title: '张三' ...

  2. linux应用之ntpdate命令联网同步时间

    当Linux服务器的时间不对的时候,可以使用ntpdate工具来校正时间. 安装:yum install ntpdate ntpdate简单用法: # ntpdate ip # ntpdate 210 ...

  3. 使用IDEA创建一个springboot项目

    工欲善其事,必先利其器. 不难发现,还是有很多小朋友在使用eclipse开发java项目.当你接触IDEA后,一切都变得美好了. 使用IDEA创建一个springboot项目是一件极其简单的事情.界面 ...

  4. C# Task的用法

    C# Task 的用法 其实Task跟线程池ThreadPool的功能类似,不过写起来更为简单,直观.代码更简洁了,使用Task来进行操作.可以跟线程一样可以轻松的对执行的方法进行控制. 顺便提一下, ...

  5. [深入学习C#]C#实现多线程的方法:线程(Thread类)和线程池(ThreadPool)

    简介 使用线程的主要原因:应用程序中一些操作需要消耗一定的时间,比如对文件.数据库.网络的访问等等,而我们不希望用户一直等待到操作结束,而是在此同时可以进行一些其他的操作.  这就可以使用线程来实现. ...

  6. PS 滤镜— —Twirl Filter

    clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...

  7. 【leetcode刷题笔记】Sqrt(x)

    Implement int sqrt(int x). Compute and return the square root of x. 题解:二分的方法,从0,1,2.....x搜索sqrt(x)的值 ...

  8. C# 性能总结

    尽量使用using语句块和finally (实现IDisposable) 尽量使用单个大程序集而不是多个小程序集 (使用NGen.exe) 使用sealed关键字 权衡虚函数 使用弱引用

  9. 机器学习:scikit-learn中算法的调用、封装并使用自己所写的算法

    一.scikit-learn库中的kNN算法 scikit-learn库中,所有机器学习算法都是以面向对象的形式进行包装的: 所有scikit-learn库中机器学习算法的使用过程:调用.实例化.fi ...

  10. 【转】 Pro Android学习笔记(四十):Fragment(5):适应不同屏幕或排版

    目录(?)[-] 设置横排和竖排的不同排版风格 改写代码 对于fragment,经常涉及不同屏幕尺寸和不同的排版风格.我们在基础小例子上做一下改动,在横排的时候,仍是现实左右两个fragment,在竖 ...