-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·43 lines (34 loc) · 820 Bytes
/
test.py
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
#!/usr/bin/python
import os, sys, json
from pprint import pprint
import protobuf_json
import test_pb2 as pb_test
# create and fill test message
pb=pb_test.TestMessage()
pb.id=123
pb.b=b"\x08\xc8\x03\x12"
pb.query="some text"
pb.flag=True
pb.test_enum=2
msg=pb.nested_msg
msg.id=1010
msg.title="test title"
msg.url="http://example.com/"
msgs=pb.nested_msgs.add()
msgs.id=456
msgs.title="test title"
msgs.url="http://localhost/"
pb.rep_int.append(1)
pb.rep_int.append(2)
pb.bs.append("\x00\x01\x02\x03\x04");
pb.bs.append("\x05\x06\x07\x08\x09");
# convert it to JSON and back
pprint(pb.SerializeToString())
json_obj=protobuf_json.pb2json(pb)
print json_obj
pb2=protobuf_json.json2pb(pb_test.TestMessage(), json_obj)
pprint(pb2.SerializeToString())
if pb == pb2:
print "Test passed."
else:
print "Test FAILED!"