环境要求API使用说明示例:假如需要定义protobuf定义一个数据接口,包含两个属性,一个是string,一个是int32 传统protobuf使用过程a 定义.proto说明文件. test.proto package pkg ; option java_package = "com.baidu.bjf.remoting.protobuf" ; //这里声明输出的java的类名 option java_outer_classname = "SimpleTypeTest" ; message InterClassName { required string name = 1; required int32 value = 2 ;} b 使用protoc.exe 编译.proto文件 protoc --java_out=src test.proto c 编译生成的Java文件,利用protobuf API进行序列化与反序化操作 序列化操作: InterClassName icn = InterClassName.newBuilder().setName("abc") .setValue(100).build();
byte [] bb = icn.toByteArray(); 反序化操作 byte [] bb =
; InterClassName icn = InterClassName.parseFrom(bb); 使用jprotobuf API 简化开发a 使用注解直接使用pojo对象
import com.baidu.bjf.remoting.protobuf.FieldType;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;/** * A simple jprotobuf pojo class just for demo. * xiemalin 1.0.0
*/ public class SimpleTypeTest {@Protobuf(fieldType = FieldType.STRING, order = 1, required = true )
private String name;@Protobuf(fieldType = FieldType.INT32, order = 2, required = false )private int value;public int getValue() {
return value;} public void setValue( int value) {
this .value = value;} } b 使用jprotobuf API进行序列化与反序列化操作 Codec<SimpleTypeTest> simpleTypeCodec = ProtobufProxy .create(SimpleTypeTest. class );SimpleTypeTest stt = new SimpleTypeTest();stt.name = "abc"; stt.setValue(100);
try {// 序列化 byte [] bb = simpleTypeCodec.encode(stt);// 反序列化 } catch (IOException e) {e.printStackTrace(); } 嵌套对象的开发示例public class AddressBookProtosPOJO {@Protobuf(fieldType = FieldType.OBJECT, order=1, required = false )
public Person person;@Protobuf(fieldType = FieldType.OBJECT, order=2, required = false )
public List<Person> person;} public class Person {@Protobuf(fieldType = FieldType.STRING, order=1, required = true )
public String name;@Protobuf(fieldType = FieldType.INT32, order=2, required = true )public int id;@Protobuf(fieldType = FieldType.STRING, order=3, required = false )
public String email;@Protobuf(fieldType = FieldType.DOUBLE, order=4, required = false )
public Double doubleF;@Protobuf(fieldType = FieldType.FLOAT, order=5, required = false )
public Float floatF;@Protobuf(fieldType = FieldType.BYTES, order=6, required = false )public byte [] bytesF;@Protobuf(fieldType=FieldType.BOOL, order=7, required= false )
public Boolean boolF;} 由注解对象动态生成Protobuf的IDL描述文件内容JProtobuf提供一个非常实用的功能,可以动态生成Protobuf的IDL描述文件内容 // 返回的内容即为 Protobuf的IDL描述文件 class );public class SimpleTypeTest {@Protobuf(fieldType = FieldType.STRING, order = 1, required = true )
private String name;@Protobuf(fieldType = FieldType.INT32, order = 2, required = false )private int value;public int getValue() {
return value;} public void setValue( int value) {
this .value = value;} } 增加由.proto 描述文件动态生成Protobuf操作对象的支持JProtobuf提供一个更简单的功能,可支持动态Protobuf对象的生成功能,省去了注释的使用 基本使用示例如下: @Test public void testDecode() throws Exception {// 通过 .proto描述文件生成动态解析对象 "option java_package = \"com.baidu.bjf.remoting.protobuf.simplestring\";" + "option java_outer_classname = \"StringTypeClass\"; " + "message StringMessage { " + " required string message = 1; }" ; IDLProxyObject object = ProtobufIDLProxy.createSingle(protoCotent); //
if .proto IDL defines multiple messages use as follow Map<String, IDLProxyObject> objects = ProtobufIDLProxy.create(protoCotent);
//
propogation object set object.put("sub.field", "hello world");
byte [] bb = object.encode();// protobuf 反序列化 Assert.assertEquals("hello你好", result.get("message")); //propogation object get // result.get("sub.field")} 联系我们 email:
rigel-opensource@baidu.com JProtobuf官方网址:
|
|