Monday, May 20, 2013

TnTonly Official Homepage

Nerdest news, reviews and tutorials by Trinh Thanh Trung

Google’s Protocol Buffer – The XML Replacement

Posted by TnTonly On November - 7 - 2010

What is Google’s Protocol Buffer? It’s a way Google uses to transfer data in their server, and now open source.
Why Google’s Protocol Buffer, and not XML? 3-10 times smaller, 20-100 times faster, less ambiguous and more structure. More important: Google’s product

http://code.google.com/p/protobuf/

Example (Refference in Gamedev.VN): Write an address book

- Write a .proto file

package tutorial;

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;
}
Compile it

protoc -I=$SRC_DIR --cpp_out=$DST_DIR addressbook.proto
and the file will automatically generated

inline bool has_name() const;
inline void clear_name();
inline const ::std::string& name() const;
inline void set_name(const ::std::string& value);
inline void set_name(const char* value);
inline ::std::string* mutable_name();

// id
inline bool has_id() const;
inline void clear_id();
inline int32_t id() const;
inline void set_id(int32_t value);

// email
inline bool has_email() const;
inline void clear_email();
inline const ::std::string& email() const;
inline void set_email(const ::std::string& value);
inline void set_email(const char* value);
inline ::std::string* mutable_email();

// phone
inline int phone_size() const;
inline void clear_phone();
inline const ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >& phone() const;
inline ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >* mutable_phone();
inline const ::tutorial::Person_PhoneNumber& phone(int index) const;
inline ::tutorial::Person_PhoneNumber* mutable_phone(int index);
inline ::tutorial::Person_PhoneNumber* add_phone();

So you can use that whatever you want, serialize/deserialize data, buff into memory, into file, stream...
cout << "Name: " << person.name() << endl;
cout << "E-mail: " << person.email() << endl;

F***ing easy!
What we have to do with XML?

  cout << "Name: "
       << person.getElementsByTagName("name")->item(0)->innerText()
       << endl;
  cout << "E-mail: "
       << person.getElementsByTagName("email")->item(0)->innerText()
       << endl;

Gotta find tag, search item, parse text... And it will be slower, difficult to read, messing code...

Tee-hee, I think I should gonna try, cause I no nuthin bout XML or Google Protocol Buffer

Popularity: 5% [?]

Add A Comment

Video Today

After the long wait, my Google Nexus 7 has finally arrived! Having seen so many “fail” unboxing videos before, I took extra precaution while unboxing the tablet. Fortunately the unboxing processed smoothly.

It is certainly worth the wait. My first impression of the Google Nexus 7 is that it is a superb build quality, amazing performance, nice screen and incredible Jelly Bean. All that for only £159. I will use this for a while and will create a review later, but for now I’m totally satisfy with it.

Popularity: 5% [?]

USER LOGIN