<?php
include 'vendor/autoload.php';
include 'GPBMetadata/Person.php';
include 'Test/Person.php';
$person = new TestPerson();
$person->setName("lailaiji");
$person->setAge("28");
$person->setSex(true);
$data = $person->serializeToString();
file_put_contents('data.bin',$data);
2、反序列化
<?php
include 'vendor/autoload.php';
include 'GPBMetadata/Person.php';
include 'Test/Person.php';
$bindata = file_get_contents('./data.bin');
$person = new TestPerson();
$person->mergeFromString($bindata);
echo $person->getName();
可以正常地输出lailaiji
PHP常用的使用方法:
序列化:
1、serializeToString:序列化成二进制字符串
2、serializeToJsonString:序列化成JSON字符串
反序列化:
1、mergeFromString:二进制字符串反序列化
2、mergeFromJsonString:Json字符串反序列化
.proto的message解析
1、定义:
类型 变量名=位置;
如:int32 age=1;
这里需要区分,变量名后面的数字意义为该变量内容在二进制序列中的位置而不是变量的值,该数字必须是唯一不可重复使用。
2、目前支持的类型:
double,float,int32,int64,uint32 ,uint64,sint32,sint64
fixed32,fixed64,sfixed32,sfixed64,bool,bytes
性能测试
https://github.com/eishay/jvm-serializers/wiki
1、序列号+反序列号时间:
image.png
2、存储大小:
image.pn
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易采站长站。







