Protocol Buffers(protobuf)在Java开发中使用
来源:互联网 发布:centos 6.5 iso下载 编辑:程序博客网 时间:2024/06/16 06:29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package tutorial;
option java_package = "com.example.tutorial";
option java_outer_classname = "AddressBookProtos";
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 4;
}
message AddressBook {
repeated Person person = 1;
}
1
D:\__dev\jar\protobuff>protoc.exe -h
1
2
D:\__dev\jar\protobuff>protoc.exe --proto_path=F:\__eclipse\test\proto
--java_out=F:\__eclipse\test\src F:\__eclipse\test\proto\addressbook.proto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package
com.example.test;
import
java.util.Arrays;
import
com.example.tutorial.AddressBookProtos.AddressBook;
import
com.example.tutorial.AddressBookProtos.Person;
import
com.google.protobuf.InvalidProtocolBufferException;
public
class
AddressBookProtoUse {
public
static
void
main(String[] args) {
//构建一个Person对象
Person person = Person
.newBuilder()
.setEmail(
"zhangsan@163.com"
)
.setId(
10086
)
.setName(
"zhangsan"
)
.addPhone(
Person.PhoneNumber.newBuilder().setNumber(
"186"
)
.setType(Person.PhoneType.HOME).build())
.build();
System.out.println(
"打印输出Person对象信息:"
);
System.out.println(person);
System.out.println(
"Person对象调用toString()方法:"
);
System.out.println(person.toString());
System.out.println(
"Person对象字段是否初始化:"
+ person.isInitialized());
// 序列号
System.out.println(
"Person对象调用toByteString()方法:"
);
System.out.println(person.toByteString());
System.out.println(
"Person对象调用toByteArray()方法:"
);
System.out.println(Arrays.toString(person.toByteArray()));
try
{
System.out.println(
"反序列化后的对象信息:"
);
// 反序列化
Person newPerson = Person.parseFrom(person.toByteArray());
System.out.println(newPerson);
newPerson = Person.parseFrom(person.toByteString());
System.out.println(newPerson);
}
catch
(InvalidProtocolBufferException e) {
e.printStackTrace();
}
// 向地址簿添加两条Person信息
AddressBook.Builder books = AddressBook.newBuilder();
books.addPerson(person);
books.addPerson(Person.newBuilder(person).setEmail(
"tom@163.com"
)
.build());
System.out.println(
"AddressBook对象信息:"
);
System.out.println(books.build());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
打印输出Person对象信息:
name: "zhangsan"
id: 10086
email: "zhangsan@163.com"
phone {
number: "186"
type: HOME
}
Person对象调用toString()方法:
name: "zhangsan"
id: 10086
email: "zhangsan@163.com"
phone {
number: "186"
type: HOME
}
Person对象字段是否初始化:true
Person对象调用toByteString()方法:
<ByteString@89ec59 size=40>
Person对象调用toByteArray()方法:
[10, 8, 122, 104, 97, 110, 103, 115, 97, 110, 16, -26, 78, 26, 16, 122, 104, 97, 110, 103, 115, 97, 110, 64, 49, 54, 51, 46, 99, 111, 109, 34, 7, 10, 3, 49, 56, 54, 16, 1]
反序列化后的对象信息:
name: "zhangsan"
id: 10086
email: "zhangsan@163.com"
phone {
number: "186"
type: HOME
}
name: "zhangsan"
id: 10086
email: "zhangsan@163.com"
phone {
number: "186"
type: HOME
}
AddressBook对象信息:
person {
name: "zhangsan"
id: 10086
email: "zhangsan@163.com"
phone {
number: "186"
type: HOME
}
}
person {
name: "zhangsan"
id: 10086
email: "tom@163.com"
phone {
number: "186"
type: HOME
}
}
0 0
- Protocol Buffers(protobuf)在Java开发中使用
- Protocol Buffers(protobuf)在Java开发中使用
- Protocol Buffers(protobuf)在Java开发中使用测试
- Protocol Buffers(protobuf)在Java开发中使用测试
- Protocol Buffers(protobuf)在Java开发中使用
- 在Java中使用Protocol Buffers
- 【Protocol Buffers】在Flash中使用Google Protocol Buffers
- 在Flash中使用Google Protocol Buffers
- 在C#中使用Google Protocol Buffers
- 在 iOS 中使用 Protocol Buffers
- 在C++中使用Protocol Buffers
- 在Android中使用Protocol Buffers
- Java 使用 Protocol Buffers
- C#下使用protobuf(Google Protocol Buffers)
- C#下使用protobuf(Google Protocol Buffers)
- C#下使用protobuf(Google Protocol Buffers)
- C#下使用protobuf(Google Protocol Buffers)
- C#下使用protobuf(Google Protocol Buffers)
- php 操作 mongodb
- 51nod 算法马拉松 集合计数
- 在Windows中如何用SSH_Client_Shell与Linux实现通讯以及共享
- Visual Studio show lineNumber
- 汉诺塔问题
- Protocol Buffers(protobuf)在Java开发中使用
- 无法从磁盘使用应用程序 将此应用程序
- leetcode--Majority Element
- 网络基本功----五层协议
- Android实战简易教程-第十一枪(树形组件:ExpandableListView显示和动态添加删除)
- ubuntu中flashplugin-installer:downloading无反映的问题
- 后缀数组求最长重复子串(可重叠的)
- 51nod 算法马拉松 天堂里的游戏
- 设计模式C++实现(13)——中介者模式(Mediator)