package com.abbidot.baselibrary.network.interceptor

import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter

/**
 *Created  by .yzq on 2022/2/18/018.
 * @link
 * @description:处理gson解析服务器返回null,变成""
 */
class StringNullAdapter : TypeAdapter<String>() {

    override fun write(writer: JsonWriter?, value: String?) {
        if (value == null) {
            writer!!.nullValue()
            return
        }
        writer!!.value(value)
    }

    override fun read(reader: JsonReader): String {
        if (reader.peek() === JsonToken.NULL) {
            reader.nextNull()
            //原先是返回Null，这里改为返回空字符串
            return ""
        }
        val jsonStr = reader.nextString()
        return if (jsonStr == "null") {
            ""
        } else {
            jsonStr
        }
    }
}