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. C# Stopwatch与TimeSpan详解

    http://www.cnblogs.com/newstart/archive/2012/09/21/2696884.html

  2. DIV CSS 笔记

    /*针对谷歌浏览器内核支持的CSS样式*/ <style type="text/css"> @media screen and (-webkit-min-device- ...

  3. DELPHI 动态 创建和释放 多个 EDIT 控件

    unit Unit1;   interface   uses   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, ...

  4. Codeforces Round #377 (Div. 2) F - Tourist Reform

    前言:关于如何求双连通分量,我们可以在tarjan搜索时标记下所有桥的位置(双连通分量(可以认为是没有桥的无向图图)即可通过删去所有桥得到),那么怎么找桥呢,对于每一条搜索到的边u->x,如果l ...

  5. I.MX6 Python3 OpenCV

    /************************************************************************* * I.MX6 Python3 OpenCV * ...

  6. LeetCode 510. Inorder Successor in BST II

    原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst-ii/ 题目: Given a binary search tree an ...

  7. properties使用

    properties可以load store 注释可以采用 "#" 或者"!" 分隔采用"="或者":" 分行采用&qu ...

  8. RTSP协议分析(二)

    以下是某省IPTV的RTSP协商过程: DESCRIBE rtsp://118.122.89.27:554/live/ch10083121594790060557.sdp?playtype=1& ...

  9. 洛谷 2822 组合数问题——质因数有关的dp

    题目:https://www.luogu.org/problemnew/show/P2822 发现 k 都是一样的.所以可以设dp[ i ][ j ]表示 n<=i,m<=j 的答案.发现 ...

  10. JVM体系结构之五:本地方法栈

    对于一个运行中的Java程序而言,它还可能会用到一些跟本地方法相关的数据区.当某个线程调用一个本地方法时,它就进入了一个全新的并且不再受虚拟机限制的世界.本地方法可以通过本地方法接口来访问虚拟机的运行 ...