https://kotlinlang.org logo
#ktor
Title
# ktor
s

Sam Garfinkel

04/15/2020, 3:52 PM
Why can’t
io.ktor.client.features.json.JsonSerializer.read(..)
return the nullable
Any?
and why can’t the
write
function accept
Any?
? Is this just how they were designed, or is there an actual implementation constraint?
s

Shawn

04/15/2020, 3:56 PM
are you talking about, like, if the body is invalid?
s

Sam Garfinkel

04/15/2020, 3:57 PM
You might want to map an empty body to null.
Likewise you might want to write null as an empty string or something.
The point is it doesn’t follow the semantics of
kotlinx.serialization
which supports serializers for nullables.
s

Shawn

04/15/2020, 4:01 PM
as I understand it, kotlinx.serialization supports serialization for nullable fields, but I don’t see how that pertains to the JsonSerializer read/write methods
if I’m not mistaken, the intent of those methods is to read and write a complete and valid JSON blob, and
null
or a string that reads
"null"
doesn’t fit the bill
s

Sam Garfinkel

04/15/2020, 4:05 PM
Fair enough. The reason I even ask the question is because the prototype from kotlinx.serializaiton is
Json.parse(KSerializer<T>, String): T
, but because
serializer(KType)
returns a
KSerializer<Any?>
the built-in Json Serializer has to use the
!!
operator. I guess this is kinda an esoteric discussion.
Also to follow on what you said, the JsonSerializer does not support a
suspend fun foo(): Bar? = client.get("<http://example.com>")
? Because
read
can’t return null.
3 Views