探索Android开源框架Android进阶笔记

探索Android开源框架 - 8. Gson使用及源码解析

2021-12-03  本文已影响0人  今阳说

使用详解

1. 基本的解析与生成

  1. 解析基本数据类型
val gson = Gson()
val i = gson.fromJson("100", Int::class.java) //100
val d = gson.fromJson("99.99", Double::class.java) //99.99
val b = gson.fromJson("true", Boolean::class.java) // true
val s = gson.fromJson("jin", String::class.java) // String
log("i=$i,d=$d,b=$b,s=$s")
  1. 生成基本数据类型
val gson = Gson()
val jsonI: String = gson.toJson(100) // 100
val jsonD: String = gson.toJson(99.99) // 100
val jsonB: String = gson.toJson(false) // false
val jsonS: String = gson.toJson("String") //"String"
log("jsonI=$jsonI,jsonD=$jsonD,jsonB=$jsonB,jsonS=$jsonS")
  1. 解析及生成自定义对象
//先定义一个数据模型类,这里需要注意的一点是在方法内部定义的类gson解析和生成都是null,所以应该放到类或单独的文件中定义
data class UserInfo(var name: String?, var age: Int?, var emailAddress: String? )
//创建自定义对象
val user = UserInfo(name = "222", age = 18, "xxx@163.com")
log("user=${user}")
//转为json字符串
val gson = Gson()
val userStr = gson.toJson(user)
log("userStr:$userStr")
//从字符串转为自定义对象
val user2: UserInfo? = gson.fromJson(userStr, UserInfo::class.java)
log("user2=${user2}")

2. 属性重命名

@SerializedName 注解的使用
  1. 为数据模型类添加注解
data class UserInfo(
    @SerializedName(value = "name", alternate = ["user_name"]) var name: String?,
    @SerializedName("age", alternate = ["user_age", "u_age", "Age"]) var age: Int?,
    @SerializedName("emailAddress") var emailAddress: String? = null,
)
  1. 解析与生成json字符串
val user3: UserInfo? =
    gson.fromJson("{\"user_age\":18,\"user_name\":\"333\"}", UserInfo::class.java)
log("user3=$user3")
val user4: UserInfo? =
    gson.fromJson("{\"u_age\":18,\"user_name\":\"444\"}", UserInfo::class.java)
log("user4=$user4")
val user5: UserInfo? = gson.fromJson(
    "{\"Age\":18,\"user_age\":19,\"u_age\":20,\"user_name\":\"555\"}",
    UserInfo::class.java
)
log("user5=$user5")
POJO与JSON的字段映射规则
  1. setFieldNamingPolicy方法与FieldNamingPolicy枚举配合使用,提供了如下几种选择
val gson = GsonBuilder()
//  .setFieldNamingPolicy(FieldNamingPolicy.IDENTITY)//emailAddress
//  .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES)//email-address
//  .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)//email_address
//  .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)//EmailAddress
    .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES)//Email Address
    .create()
val user = UserInfo(name = "222", age = 18, "xxx@163.com")
log("setFieldNamingPolicy:${gson.toJson(UserInfo3())}")
  1. setFieldNamingStrategy与Gson提供的FieldNamingStrategy接口配合使用 实现自己的规则
//1. 实现FieldNamingStrategy接口,定义自己的规则
class FieldStrategy {
    /**
     * 采用全部大写的形式
     */
    class AllUpperCaseStrategy : FieldNamingStrategy {
        override fun translateName(f: Field): String {
            return f.name.uppercase(Locale.getDefault())
        }
    }

    /**
     * 添加前置 和 后置 注解
     */
    class GsonKeyValueCaseStrategy : FieldNamingStrategy {
        override fun translateName(f: Field): String {
            val gkv: GsonKeyValue? = f.getAnnotation(GsonKeyValue::class.java)
            return if (gkv != null) {
                if (gkv.value.isNotEmpty()) {
                    gkv.prefix + gkv.value + gkv.suffix
                } else {
                    gkv.prefix + f.name + gkv.suffix
                }
            } else {
                f.name
            }
        }
    }
}
//2.使用
val gson = GsonBuilder()
    .setFieldNamingStrategy(FieldStrategy.AllUpperCaseStrategy())
    .setFieldNamingStrategy(FieldStrategy.GsonKeyValueCaseStrategy())
    .create()
val user = UserInfo(name = "222", age = 18, "xxx@163.com")
log("setFieldNamingStrategy:${gson.toJson(user)}")

使用GsonBuilder

val gsonDIY = GsonBuilder()
    //序列化null,Gson在默认情况下是不动导出值null的键的
    .serializeNulls()
    // 设置日期时间格式,另有2个重载方法
    // 在序列化和反序化时均生效
    .setDateFormat("yyyy-MM-dd")
    // 禁此序列化内部类
    .disableInnerClassSerialization()
    //生成不可执行的Json, 其实就是多了 )]}' 这4个字符
    .generateNonExecutableJson()
    //禁止转义html标签
    .disableHtmlEscaping()
    //格式化输出
    .setPrettyPrinting()
    .create() //生成配置好的Gson

//使用
val user8 = UserInfo("888", 18)
val user8Str = gsonDIY.toJson(user8)
log("user8 toJson:${user8Str}")
log("fromJson user8Str:${gsonDIY.fromJson(user8Str, UserInfo::class.java)}")

泛型

val gson = Gson()
val jsonArray = gson.toJson(arrayOf(user2, user3, user4, user5))
val userArr = gson.fromJson(jsonArray, Array<UserInfo>::class.java)
log("userArr=${gson.toJson(userArr)}")
val userList: List<UserInfo> = gson.fromJson(jsonArray, List::class.java) as List<UserInfo>
log("userList=$userList")
//Java泛型使用时要注意泛型擦除,Gson为我们提供了TypeToken来实现对泛型的支持
val userList2: List<UserInfo?>? =
    gson.fromJson(jsonArray, object : TypeToken<List<UserInfo?>?>() {}.type)
log("userList2=$userList2")
泛型的封装
fun getListType(type: Type): Type {
    return TypeToken.getParameterized(MutableList::class.java, type).type
}

fun getSetType(type: Type): Type {
    return TypeToken.getParameterized(MutableSet::class.java, type).type
}

fun getMapType(keyType: Type, valueType: Type): Type {
    return TypeToken.getParameterized(MutableMap::class.java, keyType, valueType).type
}

fun getArrayType(type: Type): Type {
    return TypeToken.getArray(type).type
}

fun getType(rawType: Type, vararg typeArguments: Type): Type {
    return TypeToken.getParameterized(rawType, *typeArguments).type
}
/**
 * 解析data是object的情况
 */
fun <T> fromJson2Object(json: String?, clazz: Class<T>): BaseHttpResponse<T>? {
    val type: Type = getType(BaseHttpResponse::class.java, clazz)
    return fromJson(json, type)
}

/**
 * 解析data是array的情况
 */
fun <T> fromJson2Array(json: String?, clazz: Class<T>): BaseHttpResponse<MutableList<T?>?>? {
    // 生成List<T> 中的 List<T>
    val listType: Type = getListType(clazz)
    // 根据List<T>生成完整的BaseHttpResponse<List<T>>
    val type: Type = getType(BaseHttpResponse::class.java, listType)
    return fromJson(json, type)
}

/**
 * 解析data是Map的情况
 */
fun <K, T> fromJson2Map(json: String?, clazz1: Class<K>, clazz2: Class<T>): BaseHttpResponse<MutableMap<K?, T?>?>? {
    // 生成BaseHttpResponse<Map<K,T>> 中的 Map<K,T>
    val mapType: Type = getMapType(clazz1, clazz2)
    // 根据Map<K,T>生成完整的BaseHttpResponse<Map<K,T>>
    val type: Type = getType(BaseHttpResponse::class.java, mapType)
    return fromJson(json, type)
}
//网络数据的基类
data class BaseHttpResponse<T>(
    @SerializedName(value = "code", alternate = ["Status", "status"]) var code: Int? = null,
    @SerializedName(value = "message", alternate = ["Msg", "msg"]) var message: String? = null,
    @SerializedName(value = "data", alternate = ["Data"]) var data: T? = null
)
//对BaseHttpResponse<UserInfo3>的解析
val baseHttpResponse1=BaseHttpResponse(111,"aaa",UserInfo3("ljy",18))
val strResponse1:String=GsonUtils.toJson(baseHttpResponse1)
log("fromJsonObject:${GsonUtils.fromJson2Object(strResponse1,UserInfo3::class.java)}")

//对List<UserInfo3>的解析
val list=mutableListOf(UserInfo3("ljy",18),UserInfo3("qwer",19))
log("fromJson(json,Type):${GsonUtils.fromJson<List<UserInfo3>>(GsonUtils.toJson(list),GsonUtils.getListType(UserInfo3::class.java))}")
//对BaseHttpResponse<List<UserInfo3>>的解析
val baseHttpResponse2=BaseHttpResponse<List<UserInfo3>>(111,"aaa",list)
val strResponse2:String=GsonUtils.toJson(baseHttpResponse2)
log("fromJsonArray:${GsonUtils.fromJson2Array(strResponse2,UserInfo3::class.java)}")

//对Map<String,UserInfo3>的解析
val map= mutableMapOf("key1" to UserInfo3("ljy",18),"key2" to UserInfo3("qwer",19))
log("fromJson(json,Type):${GsonUtils.fromJson<Map<String,UserInfo3>>(GsonUtils.toJson(map),GsonUtils.getMapType(String::class.java,UserInfo3::class.java))}")
//对BaseHttpResponse<Map<String,UserInfo3>>的解析
val baseHttpResponse3=BaseHttpResponse<List<UserInfo3>>(111,"aaa",map)
val strResponse3:String=GsonUtils.toJson(baseHttpResponse3)
log("fromJsonMap:${GsonUtils.fromJson2Map(strResponse3,String::class.java,UserInfo3::class.java)}")

Gson的序列化、反序列化

Gson的流式反序列化
  1. 手动的方式: 使用stream包下的JsonReader类来手动实现反序列化,和Android中使用pull解析XML是比较类似的
val user6 = UserInfo(null, null, null, null)
val reader = JsonReader(StringReader(userStr))
reader.beginObject()
while (reader.hasNext()) {
    when (reader.nextName()) {
        "name" ->
            user6.name = reader.nextString()
        "age" ->
            user6.age = reader.nextInt()
        "emailAddress" ->
            user6.emailAddress = reader.nextString()
        "temp" ->
            user6.temp = reader.nextString()
        "date" ->
            user6.date = Date(reader.nextString())
    }
}
reader.endObject()
log("user6=$user6")
  1. 自动方式最终都是通过JsonReader来实现的,如果第一个参数是String类型,那么Gson会创建一个StringReader转换成流操作,源码如下:
public <T> T fromJson(String json, Type typeOfT) throws JsonSyntaxException {
    if (json == null) {
      return null;
    }
    StringReader reader = new StringReader(json);
    T target = (T) fromJson(reader, typeOfT);
    return target;
}
Gson的流式序列化
  1. 手动
val user7 = UserInfo("777", 17, "7777@qq.com")
// val writer = JsonWriter(OutputStreamWriter(System.out))
val stringWriter = StringWriter()
val writer = JsonWriter(stringWriter)
writer.beginObject()
    .name("name").value(user7.name)
    .name("age").value(user7.age)
    .name("emailAddress").nullValue()
    .endObject()
writer.flush()
log("user7=$stringWriter")
  1. 自动
// PrintStream(System.out) 、StringBuilder、StringBuffer和*Writer都实现了Appendable接口。
gson.toJson(user,System.out)
val str1 = gson.toJson(user)
//toJson源码如下:
//public String toJson(Object src, Type typeOfSrc) {
//    StringWriter writer = new StringWriter();
//    toJson(src, typeOfSrc, writer);
//    return writer.toString();
//}

字段过滤的几种方法

1. @Expose注解
data class UserInfo(
    @Expose //@Expose提供了两个属性,且默认值都为true
    var name: String?,
    @Expose(deserialize = true,serialize = true) //序列化和反序列化都都生效
    var age: Int?,
    @Expose(deserialize = true,serialize = false) //反序列化时生效
    var emailAddress: String? = null,
    @Expose(deserialize = false,serialize = true) //序列化时生效
    var date: Date? = Date(),
    @Expose(deserialize = false,serialize = false) // 和不写注解一样
    var temp: String? = "没啥用的temp"
)
val gson3 = GsonBuilder()
    .excludeFieldsWithoutExposeAnnotation()
    .create()
gson3.toJson(user8)
log("user8 toJson:${gson3.toJson(user8)}")
2. 基于版本
data class UserInfo2(
    var name: String?,
    @Since(3.0)
    var age: Int?,
    @Until(6.0)
    var emailAddress: String? = null,
)
val user9 = UserInfo2("999", 19, "9999@qq.com")
log("user9 version 1.0:" + GsonBuilder().setVersion(1.0).create().toJson(user9))
log("user9 version 4.0:" + GsonBuilder().setVersion(4.0).create().toJson(user9))
log("user9 version 9.0:" + GsonBuilder().setVersion(9.0).create().toJson(user9))
val user9Str = "{\"age\":19,\"emailAddress\":\"9999@qq.com\",\"name\":\"999\"}"
log("user9Str version 1.0:" + GsonBuilder().setVersion(1.0).create().fromJson(user9Str, UserInfo2::class.java))
log("user9Str version 4.0:" + GsonBuilder().setVersion(4.0).create().fromJson(user9Str, UserInfo2::class.java))
log("user9Str version 9.0:" + GsonBuilder().setVersion(9.0).create().fromJson(user9Str, UserInfo2::class.java))
3. 基于访问修饰符
//用不同的修饰符修饰模型类Test的字段
public class Test {
    final String finalField = "final";
    static String staticField = "static";
    public String publicField = "public";
    protected String protectedField = "protected";
    String defaultField = "default";
    boolean man=true;
    private String privateField = "private";
}
//排除final,static,private修饰的字段
val gson4 = GsonBuilder()
    .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.STATIC, Modifier.PRIVATE)
    .create()
log(gson4.toJson(Test()))
4. 基于策略(自定义规则)
val gson5 = GsonBuilder()
    .addSerializationExclusionStrategy(object : ExclusionStrategy {
        override fun shouldSkipField(f: FieldAttributes): Boolean {
            // 这里作判断,决定要不要排除该字段,return true为排除
            if ("emailAddress" == f.name) return true //按字段名排除
            val expose = f.getAnnotation(Expose::class.java)
            return expose != null && !expose.deserialize //按注解排除:
        }

        override fun shouldSkipClass(clazz: Class<*>): Boolean {
            // 直接排除某个类 ,return true为排除,例如排除Boolean.kt和Boolean.java
            return clazz == Boolean::class || clazz == Boolean::class.java
        }
    })
    .create()
log("UserInfo3:${gson5.toJson(UserInfo3())}")
log("Test:${gson5.toJson(Test())}")

TypeAdapter

  1. 自定义UserInfoTypeAdapter
class UserInfoTypeAdapter : TypeAdapter<UserInfo3?>() {
    @Throws(IOException::class)
    override fun write(out: JsonWriter?, value: UserInfo3?) {
        out?.run {
            value?.let {
                beginObject()
                name("name").value(it.name)
                name("age").value(it.age)
                name("emailAddress").value(it.emailAddress)
                endObject()
            }
        }
    }

    @Throws(IOException::class)
    override fun read(`in`: JsonReader): UserInfo3 {
        val user = UserInfo3()
        `in`.beginObject()
        while (`in`.hasNext()) {
            when (`in`.nextName()) {
                "name" -> user.name = `in`.nextString()
                "age" -> user.age = `in`.nextInt()
                "email", "email_address", "emailAddress" -> user.emailAddress =
                    `in`.nextString()
            }
        }
        `in`.endObject()
        return user
    }
}
  1. 为UserInfo3注册UserInfoTypeAdapter
val gson8 = GsonBuilder()
    .registerTypeAdapter(UserInfo3::class.java, UserInfoTypeAdapter())
    .create()
log(gson8.toJson(UserInfo3()))
log(gson8.fromJson("{\"name\":\"ljy\",\"age\":20,\"email_address\":\"xx@qq.com\",\"email\":\"qqq@qq.com\"}", UserInfo3::class.java))

JsonSerializer与JsonDeserializer

  1. 用TypeAdapter实现
class NumTypeAdapter : TypeAdapter<Number>() {
    override fun write(out: JsonWriter, value: Number) {
        log("numTypeAdapter.write:$value")
        out.value(value.toString())
    }

    override fun read(`in`: JsonReader): Number {
        return try {
            val str = `in`.nextString()
            log("numTypeAdapter.read:$str")
            if (str == null || str.isEmpty()) {
                return -1
            }
            NumberFormat.getInstance().parse(str) ?: -1
        } catch (e: Exception) {
            log("e:$e")
            -1
        }
    }
}
  1. 用JsonSerializer与JsonDeserializer实现
class NumJsonSerializer : JsonSerializer<Number> {
    override fun serialize(
        src: Number?,
        typeOfSrc: Type?,
        context: JsonSerializationContext?
    ): JsonElement {
        log("typeOfSrc:$typeOfSrc")
        return JsonPrimitive(src.toString())
    }
}

class NumJsonDeserializer : JsonDeserializer<Number> {
    override fun deserialize(
        json: JsonElement?,
        typeOfT: Type?,
        context: JsonDeserializationContext?
    ): Number {
        return try {
            log("typeOfT:$typeOfT")
            json?.asNumber ?: -1
        } catch (e: Exception) {
            log("e:${e}")
            -1
        }
    }

}
  1. 使用测试
val gson9 = GsonBuilder()
    .registerTypeAdapter(Number::class.java, NumTypeAdapter())
    .registerTypeHierarchyAdapter(Number::class.java, NumJsonSerializer())
    .registerTypeHierarchyAdapter(Number::class.java, NumJsonDeserializer())
    .create()

log(gson9.toJson(100, Number::class.java))
log(gson9.toJson(6.66, Number::class.java))
log(gson9.toJson(111111111111111111, Number::class.java))
log(gson9.fromJson("\"\"", Number::class.java))
log(gson9.fromJson("", Number::class.java))
log(gson9.fromJson("88.88", Number::class.java))
log(gson9.fromJson("55", Number::class.java))
log(gson9.fromJson("111111111111111111", Number::class.java))

log(gson9.toJson(100, Int::class.java))
log(gson9.toJson(6.66, Double::class.java))
log(gson9.toJson(111111111111111111, Long::class.java))
log(gson9.fromJson("\"\"", Number::class.java))
log(gson9.fromJson("", Double::class.java))
log(gson9.fromJson("88.88", Double::class.java))
log(gson9.fromJson("55", Int::class.java))
log(gson9.fromJson("111111111111111111", Long::class.java))
实战
val gson11 = GsonBuilder().registerTypeHierarchyAdapter(
    MutableList::class.java,
    JsonDeserializer { json, typeOfT, context ->
        if (json.isJsonArray) {
            log("isJsonArray")
            val array = json.asJsonArray
            val itemType: Type =
                (typeOfT as ParameterizedType).actualTypeArguments[0]
            val list: MutableList<Any?> = mutableListOf()
            for (i in 0 until array.size()) {
                val element = array[i]
                val item = context.deserialize<Any>(element, itemType)
                list.add(item)
            }
            list
        } else {
            log("返回空List")
            Collections.EMPTY_LIST
        }
    }).create()
log(gson11.toJson(listOf(UserInfo3())))
val type = object : TypeToken<List<UserInfo3?>?>() {}.type
log(gson11.fromJson("[{\"age\":20,\"emailAddress\":\"xx@qq.com\",\"height\":180,\"man\":true,\"name\":\"ljy\"}]", type))
log(gson11.fromJson("{}", type))
log(gson11.fromJson("暂无数据", type))

TypeAdapterFactory

class GsonDefaultAdapterFactory: TypeAdapterFactory {
    override fun <T : Any> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
        if (type.type == String::class.java) {
            return createStringAdapter()
        }
        if (type.rawType == List::class.java || type.rawType == Collection::class.java) {
            return createCollectionAdapter(type, gson)
        }
        return null
    }

    /**
     * null替换成空List
     */
    private fun <T : Any> createCollectionAdapter(
        type: TypeToken<T>,
        gson: Gson
    ): TypeAdapter<T>? {
        val rawType = type.rawType
        if (!Collection::class.java.isAssignableFrom(rawType)) {
            return null
        }

        val elementType: Type = `$Gson$Types`.getCollectionElementType(type.type, rawType)
        val elementTypeAdapter: TypeAdapter<Any> =
            gson.getAdapter(TypeToken.get(elementType)) as TypeAdapter<Any>

        return object : TypeAdapter<Collection<Any>>() {
            override fun write(writer: JsonWriter, value: Collection<Any>?) {
                writer.beginArray()
                value?.forEach {
                    elementTypeAdapter.write(writer, it)
                }
                writer.endArray()
            }

            override fun read(reader: JsonReader): Collection<Any> {
                val list = mutableListOf<Any>()
                // null替换为""
                if (reader.peek() == JsonToken.NULL) {
                    reader.nextNull()
                    return list
                }
                reader.beginArray()
                while (reader.hasNext()) {
                    val element = elementTypeAdapter.read(reader)
                    list.add(element)
                }
                reader.endArray()
                return list
            }

        } as TypeAdapter<T>
    }

    /**
     * null 替换成空字符串
     */
    private fun <T : Any> createStringAdapter(): TypeAdapter<T> {
        return object : TypeAdapter<String>() {
            override fun write(writer: JsonWriter, value: String?) {
                if (value == null) {
                    writer.value("")
                } else {
                    writer.value(value)
                }
            }

            override fun read(reader: JsonReader): String {
                // null替换为""
                if (reader.peek() == JsonToken.NULL) {
                    reader.nextNull()
                    return ""
                }
                return reader.nextString()
            }

        } as TypeAdapter<T>
    }
}

//使用
val gson10 = GsonBuilder()
    .registerTypeAdapterFactory(GsonDefaultAdapterFactory())
    .create()

@JsonAdapter注解

//  @JsonAdapter(NumJsonSerializer::class)
//  @JsonAdapter(NumJsonDeserializer::class)
//  @JsonAdapter(NumTypeAdapter::class)
@JsonAdapter(KotlinAdapterFactory2::class)
data class UserInfo5(
    var name: String = "ljy",
    private var email: String? = "xxx@qq.com",
    private val mobile: String? = "12315",
)
log("UserInfo5:${Gson().toJson(UserInfo5())}")

源码解析

fromJson方法

public <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException {
    //Class<T>转为Type
    Object object = fromJson(json, (Type) classOfT);
    return Primitives.wrap(classOfT).cast(object);
}

public <T> T fromJson(String json, Type typeOfT) throws JsonSyntaxException {
    if (json == null) {
      return null;
    }
    //将String包装为StringReader
    StringReader reader = new StringReader(json);
    T target = (T) fromJson(reader, typeOfT);
    return target;
}

public <T> T fromJson(Reader json, Type typeOfT) throws JsonIOException, JsonSyntaxException {
    //将Reader包装为JsonReader
    JsonReader jsonReader = newJsonReader(json);
    T object = (T) fromJson(jsonReader, typeOfT);
    assertFullConsumption(object, jsonReader);
    return object;
}

public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException {
    boolean isEmpty = true;
    boolean oldLenient = reader.isLenient();
    reader.setLenient(true);
    try {
      reader.peek();
      isEmpty = false;
      TypeToken<T> typeToken = (TypeToken<T>) TypeToken.get(typeOfT);
      //获取TypeAdapter
      TypeAdapter<T> typeAdapter = getAdapter(typeToken);
      T object = typeAdapter.read(reader);
      return object;
    } catch //。。。省略很多异常处理
    } finally {
      reader.setLenient(oldLenient);
    }
}

getAdapter方法

public <T> TypeAdapter<T> getAdapter(TypeToken<T> type) {
    //先从缓存ConcurrentHashMap<TypeToken<?>, TypeAdapter<?>> typeTokenCache中取
    TypeAdapter<?> cached = typeTokenCache.get(type == null ? NULL_KEY_SURROGATE : type);
    if (cached != null) {
      return (TypeAdapter<T>) cached;
    }
    //再从ThreadLocal<Map<TypeToken<?>, FutureTypeAdapter<?>>> calls 中取
    Map<TypeToken<?>, FutureTypeAdapter<?>> threadCalls = calls.get();
    boolean requiresThreadLocalCleanup = false;
    if (threadCalls == null) {
      threadCalls = new HashMap<TypeToken<?>, FutureTypeAdapter<?>>();
      calls.set(threadCalls);
      requiresThreadLocalCleanup = true;
    }

    // the key and value type parameters always agree
    FutureTypeAdapter<T> ongoingCall = (FutureTypeAdapter<T>) threadCalls.get(type);
    if (ongoingCall != null) {
      return ongoingCall;
    }

    try {
      FutureTypeAdapter<T> call = new FutureTypeAdapter<T>();
      threadCalls.put(type, call);

      //缓存map和ThreadLocal中都没有,则循环List<TypeAdapterFactory> factories 集合
      for (TypeAdapterFactory factory : factories) {
        TypeAdapter<T> candidate = factory.create(this, type);
        if (candidate != null) {
          call.setDelegate(candidate);
          typeTokenCache.put(type, candidate);
          return candidate;
        }
      }
      throw new IllegalArgumentException("GSON (" + GsonBuildConfig.VERSION + ") cannot handle " + type);
    } finally {
      threadCalls.remove(type);

      if (requiresThreadLocalCleanup) {
        calls.remove();
      }
    }
}

Gson的构造方法

public Gson() {
    this(Excluder.DEFAULT, FieldNamingPolicy.IDENTITY,
        Collections.<Type, InstanceCreator<?>>emptyMap(), DEFAULT_SERIALIZE_NULLS,
        DEFAULT_COMPLEX_MAP_KEYS, DEFAULT_JSON_NON_EXECUTABLE, DEFAULT_ESCAPE_HTML,
        DEFAULT_PRETTY_PRINT, DEFAULT_LENIENT, DEFAULT_SPECIALIZE_FLOAT_VALUES,
        LongSerializationPolicy.DEFAULT, null, DateFormat.DEFAULT, DateFormat.DEFAULT,
        Collections.<TypeAdapterFactory>emptyList(), Collections.<TypeAdapterFactory>emptyList(),
        Collections.<TypeAdapterFactory>emptyList());
}

Gson(。。。) {
    。。。
    List<TypeAdapterFactory> factories = new ArrayList<TypeAdapterFactory>();
    factories.add(TypeAdapters.JSON_ELEMENT_FACTORY);
    factories.add(ObjectTypeAdapter.FACTORY);
    factories.add(excluder);
    factories.addAll(factoriesToBeAdded);
    factories.add(TypeAdapters.STRING_FACTORY);
    factories.add(TypeAdapters.INTEGER_FACTORY);
    factories.add(TypeAdapters.BOOLEAN_FACTORY);
    factories.add(TypeAdapters.BYTE_FACTORY);
    factories.add(TypeAdapters.SHORT_FACTORY);
    factories.add。。。//太多了
    this.jsonAdapterFactory = new JsonAdapterAnnotationTypeAdapterFactory(constructorConstructor);
    factories.add(jsonAdapterFactory);
    factories.add(TypeAdapters.ENUM_FACTORY);
    factories.add(new ReflectiveTypeAdapterFactory(
        constructorConstructor, fieldNamingStrategy, excluder, jsonAdapterFactory));

    this.factories = Collections.unmodifiableList(factories);
}
public static final TypeAdapterFactory STRING_FACTORY = newFactory(String.class, STRING);

public static final TypeAdapter<String> STRING = new TypeAdapter<String>() {
    @Override
    public String read(JsonReader in) throws IOException {
      JsonToken peek = in.peek();
      if (peek == JsonToken.NULL) {
        in.nextNull();
        return null;
      }
      /* coerce booleans to strings for backwards compatibility */
      if (peek == JsonToken.BOOLEAN) {
        return Boolean.toString(in.nextBoolean());
      }
      return in.nextString();
    }
    @Override
    public void write(JsonWriter out, String value) throws IOException {
      out.value(value);
    }
};
public static <TT> TypeAdapterFactory newFactory(
      final Class<TT> type, final TypeAdapter<TT> typeAdapter) {
    return new TypeAdapterFactory() {
      @SuppressWarnings("unchecked") // we use a runtime check to make sure the 'T's equal
      @Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
        return typeToken.getRawType() == type ? (TypeAdapter<T>) typeAdapter : null;
      }
      @Override public String toString() {
        return "Factory[type=" + type.getName() + ",adapter=" + typeAdapter + "]";
      }
    };
}
JsonAdapterAnnotationTypeAdapterFactory
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> targetType) {
    Class<? super T> rawType = targetType.getRawType();
    JsonAdapter annotation = rawType.getAnnotation(JsonAdapter.class);
    if (annotation == null) {
      return null;
    }
    return (TypeAdapter<T>) getTypeAdapter(constructorConstructor, gson, targetType, annotation);
}
ReflectiveTypeAdapterFactory
@Override public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
    Class<? super T> raw = type.getRawType();

    if (!Object.class.isAssignableFrom(raw)) {
      return null; // it's a primitive!
    }

    ObjectConstructor<T> constructor = constructorConstructor.get(type);
    return new Adapter<T>(constructor, getBoundFields(gson, type, raw));
}
 private Map<String, BoundField> getBoundFields(Gson context, TypeToken<?> type, Class<?> raw) {
    Map<String, BoundField> result = new LinkedHashMap<String, BoundField>();
    if (raw.isInterface()) {
      return result;
    }

    Type declaredType = type.getType();
    while (raw != Object.class) {
      //得到实体类所有的字段
      Field[] fields = raw.getDeclaredFields();
      for (Field field : fields) {
        //字段是否参与反序列化或者序列化过程
        boolean serialize = excludeField(field, true);
        boolean deserialize = excludeField(field, false);
        if (!serialize && !deserialize) {
          continue;
        }
        accessor.makeAccessible(field);
        Type fieldType = $Gson$Types.resolve(type.getType(), raw, field.getGenericType());
        List<String> fieldNames = getFieldNames(field);
        BoundField previous = null;
        for (int i = 0, size = fieldNames.size(); i < size; ++i) {
          String name = fieldNames.get(i);
          if (i != 0) serialize = false; // only serialize the default name
          //
          BoundField boundField = createBoundField(context, field, name,
              TypeToken.get(fieldType), serialize, deserialize);
          BoundField replaced = result.put(name, boundField);
          if (previous == null) previous = replaced;
        }
        if (previous != null) {
          throw new IllegalArgumentException(declaredType
              + " declares multiple JSON fields named " + previous.name);
        }
      }
      type = TypeToken.get($Gson$Types.resolve(type.getType(), raw, raw.getGenericSuperclass()));
      raw = type.getRawType();
    }
    return result;
}
public boolean excludeField(Field f, boolean serialize) {
    return excludeField(f, serialize, excluder);
}

static boolean excludeField(Field f, boolean serialize, Excluder excluder) {
    return !excluder.excludeClass(f.getType(), serialize) && !excluder.excludeField(f, serialize);
}
//excludeClassChecks(clazz)检查class类型是否符合序列化或者反序列化要求,里面用到的Since和Until注解
//excludeClassInStrategy(clazz, serialize)通过加入自己的策略来控制字段是否要参与解析
//excluder.excludeField(f, serialize)过滤字段
public boolean excludeClass(Class<?> clazz, boolean serialize) {
      return excludeClassChecks(clazz) ||
              excludeClassInStrategy(clazz, serialize);
}
createBoundField
private ReflectiveTypeAdapterFactory.BoundField createBoundField(
      final Gson context, final Field field, final String name,
      final TypeToken<?> fieldType, boolean serialize, boolean deserialize) {
    //是否是基本数据类型
    final boolean isPrimitive = Primitives.isPrimitive(fieldType.getRawType());
    // special casing primitives here saves ~5% on Android...
    //@JsonAdapter注解,如果实体类某属性使用了@JsonAdapter,那么该属性的序列化和反序列化将由指定的适配器接管。
    //如果没有这会从Gson初始化中查找对于的解析适配器
    JsonAdapter annotation = field.getAnnotation(JsonAdapter.class);
    TypeAdapter<?> mapped = null;
    if (annotation != null) {
      mapped = jsonAdapterFactory.getTypeAdapter(
          constructorConstructor, context, fieldType, annotation);
    }
    final boolean jsonAdapterPresent = mapped != null;
    if (mapped == null) mapped = context.getAdapter(fieldType);

    final TypeAdapter<?> typeAdapter = mapped;
    return new ReflectiveTypeAdapterFactory.BoundField(name, serialize, deserialize) {
      @SuppressWarnings({"unchecked", "rawtypes"}) // the type adapter and field type always agree
      @Override void write(JsonWriter writer, Object value)
          throws IOException, IllegalAccessException {
        Object fieldValue = field.get(value);
        TypeAdapter t = jsonAdapterPresent ? typeAdapter
            : new TypeAdapterRuntimeTypeWrapper(context, typeAdapter, fieldType.getType());
        t.write(writer, fieldValue);
      }
      @Override void read(JsonReader reader, Object value)
          throws IOException, IllegalAccessException {
        //typeAdapter.read
        Object fieldValue = typeAdapter.read(reader);
        if (fieldValue != null || !isPrimitive) {
          field.set(value, fieldValue);
        }
      }
      @Override public boolean writeField(Object value) throws IOException, IllegalAccessException {
        if (!serialized) return false;
        Object fieldValue = field.get(value);
        return fieldValue != value; // avoid recursion for example for Throwable.cause
      }
    };
}

参考

上一篇下一篇

猜你喜欢

热点阅读