2011-07-16

Protocol Buffers

There are many serialization formats. For example, there are pickle for Python, Serializable API for Java, and so on. These choices are dependent on the used programming language.

On the other hand, there are some serialization formats which are independence on the used programming language. For exmaple, there are XML, JSON, Protocol Buffers, and so on.

I started to use Protocol Buffers because I need to write a schema of the data for serialization and require the faster behavior.

I wrote a simple proto code and C++ code after reading tutorial.



$ protoc person.proto --cpp_out=dist





$ g++ -o add_person add_person.cc person.pb.cc `pkg-config --cflags --libs protobuf`
$ ./add_person

$ cat -v PersonBook  # for showing nonprinting characters
^H^A^R^Esaeki^Z^Qsaeki@example.org^H^B^R^Hfurukawa^Z^Tfurukawa@example.org

$ g++ -o read_person read_person.cc person.pb.cc `pkg-config --cflags --libs protobuf`
$ ./read_person
Person ID: 2
        Name: furukawa
        Email: furukawa@example.org

I didn't set a manager class for Person. So this read_person can read an only last Person.

I wrote a Python code, too.

$ protoc person.proto --python_out=dist



$ python  read_person.py 
Person ID: 2
        Name: furukawa
        Email: furukawa@example.org



I want to represent classes inheritance. But Protocol Buffers doesn't provide the way for representing them. But there a way as to use intension; ref.Protocol Buffer Polymorphism.

Isn't there any good way?

Thanks.

0 comments:

Post a Comment