I’m using this solution to decode a top-level json...
# ktor
r
I’m using this solution to decode a top-level json array https://stackoverflow.com/a/54714777/4996236 but I get this error
Copy code
io.ktor.client.call.ReceivePipelineException: Fail to run receive pipeline: kotlinx.serialization.json.JsonParsingException: Invalid JSON at 0: Expected '[, kind: kotlinx.serialization.StructureKind$LIST@dc27766'
I’m sure that the body I’m trying to decode is correct, it does start with
[{
(I’m logging it in a
Before
interceptor). What could be the cause of this?
e
Hi @ribesg, could you receive a string and post here?
r
Copy code
[{"cityPlaceId":"03d0bb67f4a1af451d481b50f7d25b3d","city":"Grenoble","address":"Place Victor Hugo","zipCode":"38000","department":"Isère","departmentShort":"38","state":"Auvergne-Rhône-Alpes","stateShort":"Auvergne-Rhône-Alpes","country":"France","countryShort":"FR","geo":{"lat":45.1884,"lon":5.7241,"valid":true},"valid":true,"type":"ADDRESS"},{"cityPlaceId":"2e19616943c1f9907b3d1999527cff5c","city":"Grenoble","address":"Rue Lamartine","zipCode":"38000","department":"Isère","departmentShort":"38","state":"Auvergne-Rhône-Alpes","stateShort":"Auvergne-Rhône-Alpes","country":"France","countryShort":"FR","geo":{"lat":45.188,"lon":5.7248,"valid":true},"valid":true,"type":"ADDRESS"},{"cityPlaceId":"0780be60e6e5a287bc8fd09eeb135620","city":"Grenoble","address":"Rue Béranger","zipCode":"38000","department":"Isère","departmentShort":"38","state":"Auvergne-Rhône-Alpes","stateShort":"Auvergne-Rhône-Alpes","country":"France","countryShort":"FR","geo":{"lat":45.188,"lon":5.724,"valid":true},"valid":true,"type":"ADDRESS"},{"cityPlaceId":"ce418e78067392a63742b4f4ce875f7b","city":"Grenoble","address":"Rue Vauban","zipCode":"38000","department":"Isère","departmentShort":"38","state":"Auvergne-Rhône-Alpes","stateShort":"Auvergne-Rhône-Alpes","country":"France","countryShort":"FR","geo":{"lat":45.1884,"lon":5.7236,"valid":true},"valid":true,"type":"ADDRESS"},{"cityPlaceId":"9ad7984550240c9ceb762bf29598deea","city":"Grenoble","address":"Rue Paul Bert","zipCode":"38000","department":"Isère","departmentShort":"38","state":"Auvergne-Rhône-Alpes","stateShort":"Auvergne-Rhône-Alpes","country":"France","countryShort":"FR","geo":{"lat":45.1882,"lon":5.7254,"valid":true},"valid":true,"type":"ADDRESS"}]
And my model:
Copy code
@Serializable
data class PlaceApiLocation(
    val city: String?,
    val country: String
)

@Serializable
internal data class PlaceApiLocations(
    val list: List<PlaceApiLocation>
) {

    @Serializer(PlaceApiLocations::class)
    companion object : KSerializer<PlaceApiLocations> {

        override val descriptor = StringDescriptor.withName(PlaceApiLocations::class.simpleName!!)

        override fun serialize(encoder: Encoder, obj: PlaceApiLocations) {
            PlaceApiLocation.serializer().list.serialize(encoder, obj.list)
        }

        override fun deserialize(decoder: Decoder): PlaceApiLocations {
            return PlaceApiLocations(PlaceApiLocation.serializer().list.deserialize(decoder))
        }

    }

}
e
@sandwwraith
r
Ok, this code has no problem.
My problem was in my custom logger, I was doing
Copy code
receivePipeline.intercept(HttpReceivePipeline.Before) {
            HttpLogger.logResponse(subject)
        }
instead of
Copy code
ResponseObserver.install(ResponseObserver {
            HttpLogger.logResponse(it)
        }, this)
so my logger was consuming the response body before it got to the deserializer, which tried to deserialize an empty String
And I’m using a custom logger because I already got a multiplatform logger with Bugsnag integration and everything I need