Is it true that ``call.receive`` doesn't work with...
# ktor
c
Is it true that ``call.receive`` doesn't work with generics because of erasure? I have trouble with this:
Copy code
call.receive<List<Variable>>
It looks like the JacksonConverter ends up doing something like ``objectmapper.readValue(reader, List::class.java)``, which will obviously won't work. It my suspicions are true, this is a big design issue in the Ktor API. Jackson accepts a ``TypeReference`` instead of a ``Class`` precisely to suport this use case. I guess my question is what are my options? - writing my own converter is impossible, since I can't pass a TypeReference to call.receive - changing the REST API is unacceptable Is reading the request body as String and using Jackson manually my only option?
o
I think we did something with that, @e5l? Or was it only on http client side?
n
i knot the clientside can do post just fine, and i had issues with call.receive until i switched from List to Array using jackson in ktor-server (usecase was testing the client json features)
e
The server doesn't use
typeInfo
because of performance reason. We consider introducing the separate way to use it in the server.
c
thanks So I have to deserialize the body myself. Does anyone know how I can obtain a request InputStream?
c
thanks a lot
for the record (in case other need this), this is what I did:
Copy code
val variables: List<Variable> = restApiObjectMapper.readValue(call.receiveStream(), LIST_OF_VARIABLES)
, where
Copy code
companion object {
    private val LIST_OF_VARIABLES = object : TypeReference<List<Variable>>(){}
}
r
Was discussing how to handle this last week for Moshi. Originally thought a content converter could add an extension function to call to handle this, like
call.receive(listTypeInfo)
, but that would preclude the whole "content negotiation" idea (using the content type header to determine how to deserialize, this would restrict to only JSON, which might be okay in your instance)
c
where would I store ``listTypeInfo`` so I can retrieve it in the converter?
r
That's where this breaks the flow. This would have been something that the converted would add to the call class, which would basically do the deserialization from the call manually like you did, given the typeInfo passed in. The problem here is that, while it looks like it's following normal ContentNegotiation procedure, it would actually bypass the negotiation part of it, and only do JSON conversion, as opposed to taking into account the
Accepts
header of the client request.
That's why I didn't move forward with this approach.
m
What is the performance issue with
typeInfo
? I use a similar approach but try to cache instances based on `ParametrizedType`: https://github.com/fluidsonic/fluid-json/blob/c3bda8e59adb6dbb5cf3fe4f8edace9f422c033a/Sources/JSONCodableType.kt#L75 I'd love to see better generics support for
.receive()
!
e
Each typeInfo callSite generates new classfile
m
How does that affect performance? Doesn't it affect just the memory footprint?