-
Notifications
You must be signed in to change notification settings - Fork 635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation for serializer(KType)
doesn't mention that it's unsound
#2948
Comments
The function's return type is |
Not really. The general contract of |
Still, it doesn't imply that |
I would agree if the signature were But today, the situation is different: val mySerializer: KSerializer<Any?> = ... // I'm in a sauna, and this part of the screen is foggy
println(Json.encodeToString(mySerializer, "myValue")) What is the reasonable behavior of this code? According to the documentation of
Is that correct? Yet if |
From my perspective it would perhaps be better to have the function return |
|
@dkhalanskyjb This function is for those cases where the type of the serializer is determined dynamically. The whole point is that you don't know the type of the resulting serializer. Returning |
Having I do not see the point of adding |
It's possible to use
Me neither. I think
Unchecked casts in user code emit a warning. Here, the issue is a combination of this library's entities that compiles without any errors or warnings, but fails at runtime with an exception that's not declared anywhere at all. |
compiles just fine, but throws
The documentation doesn't mention the intended usage pattern (which is probably to do an unchecked cast of the result into a
KSerializer
of the required type?) and doesn't explain that the returned value is prone to crashes (or surprising results!) otherwise.Some fun combinations:
Json.encodeToString(serializer(typeOf<Int>()), Long.MAX_VALUE) == "-1"
Json.encodeToString(serializer(typeOf<Int>()), 3.1415) == "3"
The text was updated successfully, but these errors were encountered: