iOS decode json

This commit is contained in:
Syer10
2023-02-14 22:30:51 -05:00
parent c42310e5a0
commit 125d9b7a3e
6 changed files with 61 additions and 16 deletions

View File

@@ -26,10 +26,10 @@ import me.tatarka.inject.annotations.Provides
interface DataComponent {
@Provides
fun ktorfit(http: Http, serverPreferences: ServerPreferences) = Ktorfit
fun ktorfit(http: Http, serverPreferences: ServerPreferences, flowIOResponseConverter: FlowIOResponseConverter) = Ktorfit
.Builder()
.httpClient(http)
.responseConverter(FlowIOResponseConverter())
.responseConverter(flowIOResponseConverter)
.baseUrl(serverPreferences.serverUrl().get().toString().addSuffix('/'))
.build()

View File

@@ -10,14 +10,15 @@ import ca.gosyer.jui.core.lang.IO
import de.jensklingenberg.ktorfit.Ktorfit
import de.jensklingenberg.ktorfit.converter.request.ResponseConverter
import de.jensklingenberg.ktorfit.internal.TypeData
import io.ktor.client.call.body
import io.ktor.client.statement.HttpResponse
import io.ktor.util.reflect.TypeInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.serialization.json.Json
import me.tatarka.inject.annotations.Inject
class FlowIOResponseConverter : ResponseConverter {
class FlowIOResponseConverter @Inject constructor(private val json: Json) : ResponseConverter {
override fun supportedType(typeData: TypeData, isSuspend: Boolean): Boolean {
return typeData.qualifiedName == "kotlinx.coroutines.flow.Flow"
@@ -34,7 +35,7 @@ class FlowIOResponseConverter : ResponseConverter {
if (info.type == HttpResponse::class) {
emit(response!!)
} else {
emit(response!!.body(info))
emit(decodeType(response!!, info, json))
}
} catch (exception: Exception) {
throw exception
@@ -42,3 +43,5 @@ class FlowIOResponseConverter : ResponseConverter {
}.flowOn(Dispatchers.IO)
}
}
expect suspend fun decodeType(response: HttpResponse, typeInfo: TypeInfo, json: Json): Any

View File

@@ -0,0 +1,17 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package ca.gosyer.jui.data
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsText
import io.ktor.util.reflect.TypeInfo
import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer
actual suspend fun decodeType(response: HttpResponse, typeInfo: TypeInfo, json: Json): Any {
return json.decodeFromString(serializer(typeInfo.kotlinType!!), response.bodyAsText())!!
}

View File

@@ -0,0 +1,16 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package ca.gosyer.jui.data
import io.ktor.client.call.body
import io.ktor.client.statement.HttpResponse
import io.ktor.util.reflect.TypeInfo
import kotlinx.serialization.json.Json
actual suspend fun decodeType(response: HttpResponse, typeInfo: TypeInfo, json: Json): Any {
return response.body(typeInfo)
}