Thrift序列化与反序列化的实现机制分析

Thrift是如何实现序死化与反序列化的,在IDL文件中,更改IDL文件中的变量序号或者[使用默认序号的情况下,新增变量时,将新增的变量不放在IDL文件的结尾,均会导致Thrift文件的反序列后无法做到向后兼容],我们只有理解Thrift是如何实现序列化的,才能了解这种现象产生的原因,才能把代码写的更让人放心

关于Thrift域的版本号的定义可以在http://thrift.apache.org/static/files/thrift-20070401.pdf这篇文章中找到说定义

1
2
3
4
5
6
7
8
Versioning  in  Thrift  is  implemented via field identifiers.
The field header  for  every member of a  struct  in  Thrift  is
encoded with a unique field identifier. The combination of
this  field identifier and its type specifier  is  used to
uniquely identify the field. The Thrift definition language
supports automatic assignment of field identifiers,
but it  is  good programming practice to always explicitly
specify field identifiers.

翻译过来,大概意思就是Thrift中每个域都有一个版本号,这个版本号是由属性的数字序号 + 属性的类型来确定的

一个简单的Thrift文件

1
2
3
4
struct  Test {
     1 : required i32 key;
     2 : required  string  value;
}

执行

1
thrift -gen java Test.thrift

将thrift文件转换成java源文件,在此不列出详细的源文件内容,只列出与序列化与反序列化相关的代码

序列化,实际上就是write,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//http://www.aiprograming.com/b/pengpeng/24<br>public void write(org.apache.thrift.protocol.TProtocol oprot, Test struct) throws org.apache.thrift.TException {
   struct .validate();
  
   oprot.writeStructBegin(STRUCT_DESC);
   oprot.writeFieldBegin(KEY_FIELD_DESC);
   oprot.writeI32( struct .key);
   oprot.writeFieldEnd();
   if  ( struct .value !=  null ) {
     oprot.writeFieldBegin(VALUE_FIELD_DESC);
     oprot.writeString( struct .value);
     oprot.writeFieldEnd();
   }
   oprot.writeFieldStop();
   oprot.writeStructEnd();
}

struct.validate()主要用来校验thrift文件中定义的required域即必传的值是不是有值,没有值就会抛出TProtocolException异常

1
2
3
4
5
6
7
public  void  validate() throws org.apache.thrift.TException {
   // check for required fields
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值