【转载】在erlang项目中使用protobuf
来源:互联网 发布:nginx body filter 编辑:程序博客网 时间:2024/04/30 09:28
protobuf是google的一个序列化框架,类似XML,JSON,其特点是基于二进制,比XML表示同样一段内容要短小得多,还可以定义一些可选字段,广泛用于服务端与客户端通信。文章将着重介绍在erlang中如何使用protobuf。
首先google没有提供对erlang语言的直接支持,所以这里使用到的第三方的protobuf库(erlang_protobuffs)
定义一个protobuf结构,保存为test.proto,如下:
- message Person {
- required int32 age = 1;
- required string name = 2;
- }
- message Family {
- repeated Person person = 1;
- }
- % 生成相应的erl和hrl文件
- protobuffs_compile:scan_file_src("test.proto").
- % 生成相应的beam和hrl文件
- protobuffs_compile:scan_file("test.proto").
下面我们以例子简单说明如何使用:
- -module(test).
- -compile([export_all]).
- -include("test_pb.hrl").
- encode() ->
- Person = #person{age=25, name="John"},
- test_pb:encode_person(Person).
- decode() ->
- Data = encode(),
- test_pb:decode_person(Data).
- encode_repeat() ->
- RepeatData =
- [
- #person{age=25, name="John"},
- #person{age=23, name="Lucy"},
- #person{age=2, name="Tony"}
- ],
- Family = #family{person=RepeatData},
- test_pb:encode_family(Family).
- decode_repeat() ->
- Data = encode_repeat(),
- test_pb:decode_family(Data).
- 6> c(test).
- {ok,test}
- 7> test:encode().
- <<8,25,18,4,74,111,104,110>>
- 8> test:decode().
- {person,25,"John"}
- 9> test:encode_repeat().
- <<10,8,8,25,18,4,74,111,104,110,10,8,8,23,18,4,76,117,99,
- 121,10,8,8,2,18,4,84,111,110,...>>
- 10> test:decode_repeat().
- {family,[{person,25,"John"},
- {person,23,"Lucy"},
- {person,2,"Tony"}]}
作者:【没有开花的树】
出处:http://blog.csdn.net/mycwq/article/details/42060641
0 0
- 【转载】在erlang项目中使用protobuf
- 在erlang项目中使用protobuf
- 在Egret项目中使用protobuf
- erlang中使用google protobuf进行通信
- [Erlang]如何在Erlang中使用SSL
- 转 erlang中使用google protobuf进行通信
- 在c++中使用protobuf
- 在 Golang 中使用 Protobuf
- 在 Golang 中使用 Protobuf
- 在Unity5中使用Protobuf
- 在egret中使用protobuf
- 在VS中使用Protobuf
- 在Unity5中使用Protobuf
- 【转载】改进erlang版本的protobuf
- protobuf在C#项目中的使用
- Protobuf在java项目中的使用
- 在lua环境中使用protobuf ,编译protobuf文件
- Beetle在Tcp通讯中使用Protobuf
- 蓝桥杯 历届试题 九宫重排 (八数码问题--康托展开去重 + bfs搜索)
- nyoj 973 天下第一
- Vue入门知识点—初识vue
- [转]POJ 1001 Exponentiation Java大数处理高精度小数
- NGUI学习笔记(六):ScrollView、Grid和Table
- 【转载】在erlang项目中使用protobuf
- CodeForces 627 B.Factory Repairs(BIT)
- 如何用递归算法判断一个数组是否递增
- bzoj 4031: [HEOI2015]小Z的房间 矩阵树定理
- android获取周围基站信息
- 对象反序列化时,如果父类未实现序列化接口,则反序列出的对象会再次调用父类的构造函数来完成属于父类那部分内容的初始化
- 第六天-网页的head制作——明天继续写下面的
- bash,awk分离出文件夹目录下最后的文件
- 机试算法题(1)--排序算法