package com.cheng.blzb.net

import com.google.gson.Gson
import com.google.gson.TypeAdapter
import com.google.gson.TypeAdapterFactory
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
import com.google.gson.stream.JsonWriter
import java.io.IOException

/**
 * @date 2020-06-05 13:53
 * @desc gson数据类型转换处理
 */
class MyTypeAdapterFactory : TypeAdapterFactory {

    @Suppress("UNCHECKED_CAST")
    override fun <T> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {
        //type.rawType 这个类型是实体的类型 不是服务器返回的数据类型
        return when (type.rawType) {
            String::class.java -> StringNullAdapter() as TypeAdapter<T>
            Int::class.java -> IntNullAdapter() as TypeAdapter<T>
            Long::class.java -> LongNullAdapter() as TypeAdapter<T>
            Double::class.java -> DoubleNullAdapter() as TypeAdapter<T>
            else -> null
        }
    }

    //字符串处理
    class StringNullAdapter : TypeAdapter<String>() {
        @Throws(IOException::class)
        override fun read(reader: JsonReader): String {
            if (reader.peek() == JsonToken.NULL) {
                reader.nextNull()
                return ""
            }
            return reader.nextString()
        }

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

    //Int处理
    inner class IntNullAdapter : TypeAdapter<Int>() {
        @Throws(IOException::class)
        override fun read(reader: JsonReader): Int {
            val jt = reader.peek()
            if (jt == JsonToken.NULL) {
                reader.nextNull()
                return 0
            }
            if (jt == JsonToken.STRING) {
                return reader.nextString().toIntOrNull() ?: 0
            }
            return reader.nextInt()
        }

        @Throws(IOException::class)
        override fun write(writer: JsonWriter, value: Int?) {
            if (value == null) {
                writer.nullValue()
                return
            }
            writer.value(value)
        }
    }

    //Long处理
    inner class LongNullAdapter : TypeAdapter<Long>() {
        @Throws(IOException::class)
        override fun read(reader: JsonReader): Long {
            val jt = reader.peek()
            if (jt == JsonToken.NULL) {
                reader.nextNull()
                return 0
            }
            if (jt == JsonToken.STRING) {
                return reader.nextString().toLongOrNull() ?: 0L
            }
            return reader.nextLong()
        }

        @Throws(IOException::class)
        override fun write(writer: JsonWriter, value: Long?) {
            if (value == null) {
                writer.nullValue()
                return
            }
            writer.value(value)
        }
    }

    //Double处理
    inner class DoubleNullAdapter : TypeAdapter<Double>() {
        @Throws(IOException::class)
        override fun read(reader: JsonReader): Double {
            val jt = reader.peek()
            if (jt == JsonToken.NULL) {
                reader.nextNull()
                return 0.0
            }
            if (jt == JsonToken.STRING) {
                return reader.nextString().toDoubleOrNull() ?: 0.0
            }
            return reader.nextDouble()
        }

        @Throws(IOException::class)
        override fun write(writer: JsonWriter, value: Double?) {
            if (value == null) {
                writer.nullValue()
                return
            }
            writer.value(value)
        }
    }


}

