package com.bc_chat.bc_base.utils.json

import com.google.gson.Gson
import com.google.gson.TypeAdapter
import okhttp3.ResponseBody
import org.json.JSONObject
import retrofit2.Converter
import java.io.IOException

/**
 *
 */
class JsonResponseBodyConverter<T>(private val mGson: Gson, private val adapter: TypeAdapter<T>) : Converter<ResponseBody, T> {

    /**
     * 转换
     *
     * @param responseBody
     * @return
     * @throws IOException
     */
    @Throws(IOException::class)
    override fun convert(responseBody: ResponseBody): T {
        val body = responseBody.string()
        val jsonObject = JSONObject(body)
        val code = jsonObject.optInt("code")
        if (code == 0) {
            return adapter.fromJson(body)
        } else {
            return adapter.fromJson(body)
        }
    }

}