package com.qimai.android.fetch.gson

import com.google.gson.*

import java.lang.reflect.Type

/**
 * 定义为long类型,如果后台返回""或者null,则返回0
 */

class StringDefault0Adapter : JsonSerializer<String>, JsonDeserializer<String> {
    @Throws(JsonParseException::class)
    override fun deserialize(
        json: JsonElement,
        typeOfT: Type,
        context: JsonDeserializationContext
    ): String? {
        try {
            if (json.asString == "null") {
                return ""
            }
        } catch (ignore: Exception) {
        }

        try {
            return json.asString
        } catch (e: NumberFormatException) {
            throw JsonSyntaxException(e)
        }

    }

    override fun serialize(
        src: String?,
        typeOfSrc: Type,
        context: JsonSerializationContext
    ): JsonElement {
        return JsonPrimitive(src!!)
    }
}