<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 156,  comments - 601,  trackbacks - 0

    jprotobuf

    A very useful utility library for java programmer using google protobuf  JProtobuf官方網(wǎng)址:https://github.com/jhunters/jprotobuf 
    jprotobuf是針對(duì)Java程序開(kāi)發(fā)一套簡(jiǎn)易類庫(kù),目的是簡(jiǎn)化java語(yǔ)言對(duì)protobuf類庫(kù)的使用
    使用jprotobuf可以無(wú)需再去了解.proto文件操作與語(yǔ)法,直接使用java注解定義字段類型即可。

    1.0.0 支持普通類型,嵌套對(duì)象以及對(duì)象數(shù)組的Protobuf協(xié)議的序列化與反序列化實(shí)現(xiàn)。
    1.0.1 由注解對(duì)象動(dòng)態(tài)生成Protobuf的IDL描述文件內(nèi)容。
    1.0.3 增加由.proto 描述文件動(dòng)態(tài)生成Protobuf操作對(duì)象的支持,詳見(jiàn)下面使用說(shuō)明。

    環(huán)境要求

    J DK 6 或以上版本

     

    API使用說(shuō)明

    示例:假如需要定義protobuf定義一個(gè)數(shù)據(jù)接口,包含兩個(gè)屬性,一個(gè)是string,一個(gè)是int32

    傳統(tǒng)protobuf使用過(guò)程

    a 定義.proto說(shuō)明文件. 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進(jìn)行序列化與反序化操作
    序列化操作:

    InterClassName icn = InterClassName.newBuilder().setName("abc")
            .setValue(100).build();

            byte[] bb = icn.toByteArray();

    反序化操作

    byte[] bb = ;
    InterClassName icn = InterClassName.parseFrom(bb);


     

    使用jprotobuf API 簡(jiǎn)化開(kāi)發(fā)

    a 使用注解直接使用pojo對(duì)象

    import com.baidu.bjf.remoting.protobuf.FieldType;
    import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;


    /**
     * A simple jprotobuf pojo class just for demo.
     * 
     * 
    @author xiemalin
     * 
    @since 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進(jìn)行序列化與反序列化操作

     Codec<SimpleTypeTest> simpleTypeCodec = ProtobufProxy
                    .create(SimpleTypeTest.class);

            SimpleTypeTest stt = new SimpleTypeTest();
            stt.name = "abc";
            stt.setValue(100);
            try {
                // 序列化
                byte[] bb = simpleTypeCodec.encode(stt);
                // 反序列化
                SimpleTypeTest newStt = simpleTypeCodec.decode(bb);
            } catch (IOException e) {
                e.printStackTrace();
            }


    嵌套對(duì)象的開(kāi)發(fā)示例

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


     

    由注解對(duì)象動(dòng)態(tài)生成Protobuf的IDL描述文件內(nèi)容

    JProtobuf提供一個(gè)非常實(shí)用的功能,可以動(dòng)態(tài)生成Protobuf的IDL描述文件內(nèi)容

    //返回的內(nèi)容即為 Protobuf的IDL描述文件
    String code = ProtobufIDLGenerator.getIDL(SimpleTypeTest.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 描述文件動(dòng)態(tài)生成Protobuf操作對(duì)象的支持

    JProtobuf提供一個(gè)更簡(jiǎn)單的功能,可支持動(dòng)態(tài)Protobuf對(duì)象的生成功能,省去了注釋的使用 基本使用示例如下:

    @Test
    public void testDecode() throws Exception {
        // 通過(guò) .proto描述文件生成動(dòng)態(tài)解析對(duì)象
        String protoCotent = "package mypackage.test; " +
                "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);
        
    // 動(dòng)態(tài)設(shè)置字段值
        object.put("message", "hello你好");
        //propogation object set
        
    //object.put("sub.field", "hello world");
        
    // protobuf 序列化
        byte[] bb = object.encode();

        // protobuf 反序列化
        IDLProxyObject result = object.decode(bb);
        Assert.assertEquals("hello你好", result.get("message"));
        //propogation object get
        
    //result.get("sub.field")
    }


    聯(lián)系我們

    email: rigel-opensource@baidu.com
    JProtobuf官方網(wǎng)址:
    https://github.com/jhunters/jprotobuf 

    posted on 2014-09-30 11:43 x.matthew 閱讀(4912) 評(píng)論(4)  編輯  收藏

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 外国成人网在线观看免费视频| 亚洲产国偷V产偷V自拍色戒| 国产青草亚洲香蕉精品久久| 午夜电影免费观看| 亚洲 暴爽 AV人人爽日日碰| www.黄色免费网站| 亚洲人精品亚洲人成在线| 亚洲免费综合色在线视频| 精品亚洲成在人线AV无码| 无限动漫网在线观看免费| 亚洲色欲啪啪久久WWW综合网| 毛片a级毛片免费观看品善网| 亚洲第一成年网站视频| 国产精品无码免费视频二三区| 国产精品亚洲一区二区无码| 亚洲成av人片在线观看天堂无码| 人妻18毛片a级毛片免费看| 久久亚洲2019中文字幕| 在线观看免费视频一区| 无码乱人伦一区二区亚洲一 | av免费不卡国产观看| 亚洲人成网站在线观看播放动漫| 在线看片免费不卡人成视频| 亚洲 欧洲 视频 伦小说| 国产免费观看黄AV片| 一级毛片在线免费视频| 亚洲日韩精品无码一区二区三区| 国产成人久久AV免费| 亚洲成综合人影院在院播放| 大地资源二在线观看免费高清| 亚洲av日韩av永久在线观看| 亚洲电影日韩精品 | jizz日本免费| 国产精品国产亚洲精品看不卡| 久久国产高潮流白浆免费观看| 亚洲av专区无码观看精品天堂 | 色吊丝免费观看网站| 亚洲一区二区三区影院| 99爱视频99爱在线观看免费| 亚洲一区二区三区无码国产| 国产精品嫩草影院免费|