Lawik
04/15/2019, 1:01 PMribesg
04/15/2019, 1:03 PMkotlinx.serialization
only supports valid jsonLawik
04/15/2019, 1:06 PM[{
"id": 2,
"name": "test",
"age": 20
}, {
"id": 3,
"name": "test",
"age": 21
}, {
"id": 4,
"name": "test2",
"age": 18
}]
ribesg
04/15/2019, 1:08 PM{
{
"results": [...]
}
That’s how you should do it. There are various reasons for that... but in the real world there are APIs who return arrays. But the folks working on kotlinx.serialization
don’t really care about the real world. It’s the same for the null
problem.Lawik
04/15/2019, 1:16 PMribesg
04/15/2019, 1:18 PMLawik
04/15/2019, 1:19 PMribesg
04/15/2019, 1:19 PMLawik
04/15/2019, 1:20 PMT
is serializable, and add proper handling for request settings):
@ImplicitReflectionSerializer
suspend inline fun <reified T : Any> HttpClient.getList(): List<T> = request<HttpResponse> {
// other settings: url, etc
method = HttpMethod.Get
}.use {
if (it.status.isSuccess()) {
Json.parse(T::class.serializer().list, it.readText())
} else {
throw BadResponseStatusException(it.status, it)
}
}
This allows me to perform the request as follows:
actual suspend fun getAll(): List<PersonDTO> = client.getList(/*request settings*/)
?ribesg
04/15/2019, 2:44 PMLawik
04/15/2019, 4:18 PMr4zzz4k
04/15/2019, 5:38 PMjvassbo
04/15/2019, 8:40 PMribesg
04/16/2019, 7:57 AM42
to be returned as a valid json response. But that’s not what the untold standard is, because of various historical reasons, some of which are still valid todaykotlinx.serialization
doesn’t support top level arrays... Do you think it’s the only one?kotlinx.serialization
ever forgot that top level arrays were a thing, or thought it was not something important to implement. There are reasons behind this, conscious or not