Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extension field support #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ Google's Protocol Buffers and JSoN.

----------------
Example of usage:

<pre><code>
from addressbook_pb2 import AddressBook
import extend_pb2
import pbjson

# Convert python dict to Protobuf
adr_book = pbjson.dict2pb(AddressBook, adr_book_json)
adr_book = pbjson.dict2pb(AddressBook, adr_book_json, extend=extend_pb2)

# Convert Protobuf to JSoN
new_json = pbjson.pb2json(adr_book)
</code></pre>
5 changes: 5 additions & 0 deletions addressbook.proto
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
syntax = "proto2";

package tutorial;

message Person {
Expand All @@ -17,6 +19,9 @@ message Person {
}

repeated PhoneNumber phone = 4;

// Extensions.
extensions 100 to 9999;
}

message AddressBook {
Expand Down
29 changes: 25 additions & 4 deletions addressbook_example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from addressbook_pb2 import AddressBook
import extend_pb2
import pbjson

adr_book_json = {
Expand All @@ -15,7 +16,22 @@
"number": "+2345678901",
"type": 1
}
]
],
"age": 25,
"sch": [
{
"name": "peking university",
"city": "Beijing"
},
{
"name": "BHSF",
"city": "Beijing"
}
],
"used_name":["Tom", "Jerry"],
"ht": {
"city":"Shanghai"
}
},
{
"name": "Ben Bun",
Expand All @@ -31,9 +47,14 @@
]
}

#Convert JSoN to Protobuf
adr_book = pbjson.dict2pb(AddressBook, adr_book_json)
# Convert JSoN to Protobuf
adr_book = pbjson.dict2pb(AddressBook, adr_book_json, extend=extend_pb2)
# print adr_book
import simplejson as json
print json.dumps(adr_book_json, indent=4)

#Convert Protobuf to JSoN
# Convert Protobuf to JSoN
new_json = pbjson.pb2json(adr_book)
print new_json


111 changes: 68 additions & 43 deletions addressbook_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions extend.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
syntax = "proto2";

package tutorial;

import "addressbook.proto";

message Hometown {
required string city = 1;
}

message School {
required string name = 1;
optional string city = 2;
}

extend Person {
optional int32 age = 100; //simple cpp_type
repeated School sch = 101; //repeated message
repeated string used_name = 102; //repeated simple cpp_type
optional Hometown ht = 103; //optional message
}

Loading