package com.abbidot.baselibrary.network.interceptor

import android.text.TextUtils
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 2024/9/10/010.
 * @link
 * @description:处理gson解析服务器Double返回null或""
 */
class DoubleDefault0Adapter : TypeAdapter<Double>() {
    override fun write(writer: JsonWriter?, value: Double?) {
        if (value == null) {
            writer!!.nullValue()
            return
        }
        writer!!.value(value)
    }

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