google Protocol Buffer是用于结构化数据串行化的灵活、高效、自动的方法,有如XML,不过它更小、更快、也更简单。你可以定义自己的数据结构,然后使用代码生成器生成的代码来读写这个数据结构。你甚至可以在无需重新部署程序的情况下更新数据结构。
Protocol Buffer拥有多项比XML更高级的串行化结构数据的特性,Protocol Buffer:
· 更简单
· 小3-10倍
· 快20-100倍
· 更少的歧义
· 可以方便的生成数据存取类
下面来看看google protocol buffer例子
首先需要定义一个message文件.proto文件
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];
}
// a simple comment
repeated PhoneNumber phone = 4;
}
message AddressBook
{
repeated Person person = 1;
} |
protoc.php
<?php // just create from the proto file a pb_prot[NAME].php file
require_once('../parser/pb_parser.php');
$test = new PBParser(); $test->parse('./test.proto'); //$test = new PBParser();
//$test->parse('./test_new.proto');
var_dump('File parsing done!'); ?>
|
运行此php文件后会生产一个pb_proto_test.php的源文件,以后数据多解析和封装就可以利用此文件来进行操作
<?php class Person_PhoneType extends PBEnum { const MOBILE = 0; const HOME = 1; const WORK = 2; } class Person_PhoneNumber extends PBMessage { var $wired_type = PBMessage::WIRED_LENGTH_DELIMITED; public function __construct($reader=null) { parent::__construct($reader); $this->fields["1"] = "PBString"; $this->values["1"] = ""; $this->fields["2"] = "Person_PhoneType"; $this->values["2"] = ""; $this->values["2"] = new Person_PhoneType(); $this->values["2"]->value = Person_PhoneType::HOME; } function number() { return $this->_get_value("1"); } function set_number($value) { return $this->_set_value("1", $value); } function type() { return $this->_get_value("2"); } function set_type($value) { return $this->_set_value("2", $value); } } class Person extends PBMessage { var $wired_type = PBMessage::WIRED_LENGTH_DELIMITED; public function __construct($reader=null) { parent::__construct($reader); $this->fields["1"] = "PBString"; $this->values["1"] = ""; $this->fields["2"] = "PBInt"; $this->values["2"] = ""; $this->fields["3"] = "PBString"; $this->values["3"] = ""; $this->fields["4"] = "Person_PhoneNumber"; $this->values["4"] = array(); } function name() { return $this->_get_value("1"); } function set_name($value) { return $this->_set_value("1", $value); } function id() { return $this->_get_value("2"); } function set_id($value) { return $this->_set_value("2", $value); } function email() { return $this->_get_value("3"); } function set_email($value) { return $this->_set_value("3", $value); } function phone($offset) { return $this->_get_arr_value("4", $offset); } function add_phone() { return $this->_add_arr_value("4"); } function set_phone($index, $value) { $this->_set_arr_value("4", $index, $value); } function remove_last_phone() { $this->_remove_last_arr_value("4"); } function phone_size() { return $this->_get_arr_size("4"); } } class AddressBook extends PBMessage { var $wired_type = PBMessage::WIRED_LENGTH_DELIMITED; public function __construct($reader=null) { parent::__construct($reader); $this->fields["1"] = "Person"; $this->values["1"] = array(); } function person($offset) { return $this->_get_arr_value("1", $offset); } function add_person() { return $this->_add_arr_value("1"); } function set_person($index, $value) { $this->_set_arr_value("1", $index, $value); } function remove_last_person() { $this->_remove_last_arr_value("1"); } function person_size() { return $this->_get_arr_size("1"); } } ?>
|
google protocol buffer的数据格式,可以参考google官方文档,有点类似于json的数据格式
阅读(2348) | 评论(1) | 转发(0) |