How do I make a JsonSerializer for the Ktor client...
# ktor
d
How do I make a JsonSerializer for the Ktor client JsonFeature for Klaxon? I have this:
Copy code
class KlaxonSerializer(block: Klaxon.() -> Unit = {}) : JsonSerializer {

	private val backend: Klaxon = Klaxon().apply(block)

	override fun write(data: Any): OutgoingContent = TextContent(backend.toJsonString(data), ContentType.Application.Json)

	override suspend fun read(info: TypeInfo, response: HttpResponse): Any {
		val text= response.readText()
		return backend.parse(text) as info.type
	}
}
but obviously info.type won't work there and the parse function takes a reified type...
type.javaObjectType?
d
The info is red there...
And I mean for the Client not server...
d
Oh
let me check
.reifiedType?
why do you need the as?
d
Where do I put it? I can't do parse<type.reifiedType>(json)
I need to tell Klaxon what class to parse to somehow..
And Any return type won't help...
d
has klaxon a non-reified version of the method?
receiving a KClass or KType?
and why don’t use gson directly?
d
It looks not so easy... I think they call internally... Klaxon has a handy feature to annotate a field and link that to a custom field converter...
Klaxon.parser(clazz).parse(StringReader(json))
d
So like this:
backend.parser(info.type).parse(StringReader(text))!!
, right?
d
I guess so
worked for you?
d
I still have one more problem to solve before I'll know... Thanks!
👍 1