Gson

来源:互联网 发布:计算机中文编程 编辑:程序博客网 时间:2024/06/03 22:41

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.

0 0
原创粉丝点击