SerializationConverter.kt 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. @file:Suppress("UNCHECKED_CAST", "MemberVisibilityCanBePrivate")
  2. package com.rdiot.yx485.net
  3. import com.drake.net.NetConfig
  4. import com.drake.net.convert.NetConverter
  5. import com.drake.net.exception.ConvertException
  6. import com.drake.net.exception.RequestParamsException
  7. import com.drake.net.exception.ResponseException
  8. import com.drake.net.exception.ServerResponseException
  9. import com.drake.net.request.kType
  10. import kotlinx.serialization.SerializationException
  11. import kotlinx.serialization.json.Json
  12. import kotlinx.serialization.serializer
  13. import okhttp3.Response
  14. import org.json.JSONObject
  15. import java.lang.reflect.Type
  16. import kotlin.reflect.KType
  17. class SerializationConverter(
  18. val success: String = "0",
  19. val code: String = "code",
  20. val message: String = "message",
  21. ) : NetConverter {
  22. companion object {
  23. val jsonDecoder = Json {
  24. ignoreUnknownKeys = true // JSON和数据模型字段可以不匹配
  25. coerceInputValues = true // 如果JSON字段是Null则使用默认值
  26. allowStructuredMapKeys = true //允许结构化映射(map的key可以使用对象)
  27. prettyPrint = true
  28. }
  29. const val tempJson = "{\"data\":\"\"}"
  30. }
  31. override fun <R> onConvert(succeed: Type, response: Response): R? {
  32. try {
  33. return NetConverter.onConvert<R>(succeed, response)
  34. } catch (e: ConvertException) {
  35. val code = response.code
  36. when {
  37. code in 200..299 -> { // 请求成功
  38. val bodyString = response.body?.string() ?: return null
  39. val kType = response.request.kType
  40. ?: throw ConvertException(response, "Request does not contain KType")
  41. return try {
  42. val json = JSONObject(bodyString) // 获取JSON中后端定义的错误码和错误信息
  43. val srvCode = json.getString(this.code)
  44. if (srvCode == success) { // 对比后端自定义错误码
  45. val dataStr= json.getString("data")
  46. // LogUtils.e(dataStr)
  47. dataStr.parseBody<R>(kType)
  48. // if (!dataStr.isNullOrBlank()){
  49. // json.getString("data").parseBody<R>(kType)
  50. // }else{
  51. // null
  52. // }
  53. } else { // 错误码匹配失败, 开始写入错误异常
  54. val errorMessage = json.optString(message, NetConfig.app.getString(com.drake.net.R.string.no_error_message))
  55. throw ResponseException(response, errorMessage, tag = srvCode) // 将业务错误码作为tag传递
  56. }
  57. } catch (e: SerializationException) { // 固定格式JSON分析失败直接解析JSON
  58. bodyString.parseBody<R>(kType)
  59. }
  60. }
  61. code in 400..499 -> throw RequestParamsException(response, response.message) // 请求参数错误
  62. code >= 500 -> throw ServerResponseException(response, response.message) // 服务器异常错误
  63. else -> throw ConvertException(response)
  64. }
  65. }
  66. }
  67. fun <R> String.parseBody(succeed: KType): R? {
  68. return jsonDecoder.decodeFromString(Json.serializersModule.serializer(succeed), this) as R
  69. }
  70. }