Is there a smarter way to deserialize the response...
# http4k
Is there a smarter way to deserialize the response body? I feel like I’m missing something 🙂
Copy code
val response = gqlClient(request).data.toString()
return ObjectMapper().readValue<RAArtistResponse>(response).data.artist
You can use a lens: Something like...
Copy code
val raArtist =<RAArtistResponse>().toLens()
val artist = raArtist(client(request))
Yes, the above is the way to go. You can also make the
part of the lens definition:
Copy code<RAArtistResponse>().map { }.toLens()
Sorry, I think I should’ve been more clear: It’s a GraphQL response and I’m not sure how to alter my lens to make it work with it:
@Philipp Mayer at the moment, there isn't really a better way - the data val of the response will be a Map. We don't currently generify on the output type when creating the handler - it's possible that we could do that. I'd maybe add an extension function to the response which contains your
and takes the type arguments of the type you want out.... that should work I think for now
Alright, thanks for the explanation! This is totally fine for me, I was just curious if I overlooked something there. For now I just went with a normal http post, makes things way easier on my end for one single query.
Another option:
Copy code
fun Response.parseArtist() =
I think Jackson will have parsed it as a map on the conversion to any, so you don't have to call toString. It might not be readValue, but I'm sure there is a call which works in that api