Why can’t `io.ktor.client.features.json.JsonSerial...
# ktor
s
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
are you talking about, like, if the body is invalid?
s
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
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
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.