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

  • 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

  • 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 with Collection>
  • 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.

    你可能感兴趣的:(Gson)