Gson

Serialize Object

  • GetAdapter from basic adapter factories
  • Not found basic adapter, ReflectiveTypeAdapterFactory will create this object type adapter, RefectiveTypeAdapterFactory::Adapter
    • Recursive object  create boundfields, every filed is basic type with basic adapter
  • RefectiveTypeAdapterFactory::Adapter  write will call every boundfield's adapter to write.

 

Deserialize Object

  • Get Adapter just likes Serialize Object
  • Contructor instance
  • Set value to instance

 

Serialize Collecion<Object>

  • GetAdapter from basic adapter: get collectionAdapter
  • When write every element with objectAdapter
  • ObjectAdapter will check the value type then get sub type adapter or collection adapter. it will check element run time type, then find the reasonable adatper

 

Deserialize Collection<Object>

  • GetAdapter from basic adapter: get collectionAdapter
  • When read every element, it constructor Map to read element value, as it can't get element type. Just assign value to Collection<Object> with Collection<Map<String, Object>>
  • But it will throw exception when you get the element with Object type, as it can't convert Map to Object
    • For solve this problem, Gson define class TypeToken to get element type, in typeToken, it returns ParameterizedType it will includes collection type and element  type , collection adapter will include element type adatper

 

 

RefectiveTypeAdapterFactory::Adapterhas boundFields, every boundField connects to a Adapter via closure. ThisAdapter may be Collection adapter or RefectiveTypeAdapterFactory::Adapter orsome basic adapter in builtin factories

 

 

 

Summary:

Serialize object: gson  willfind adapter , then call adapter read. Collection adapter read function willfind element adapter then call element adapter. Imbeded object adapter will been fetched recursively at the firsttime  .

Derialize object: gson will find adatper, then constructer object ,then call adapter write. The process is the same as serialize object.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值