
The unsafe cast in Kotlin is done by the infix operator as. Usually, the cast operator throws an exception if the cast isn't possible. Var properties - never, because the variable can be modified at any time by other code.

Var local variables - if the variable is not modified between the check and the usage, is not captured in a lambda that modifies it, and is not a local delegated property. Smart casts cannot be used on open properties or properties that have custom getters. Val properties - if the property is private or internal or if the check is performed in the same module where the property is declared. Val local variables - always, with the exception of local delegated properties. More specifically, smart casts can be used under the following conditions: For my project I'm using rialization and the issue at hand will be solved for that converter, but the concept is the same for the others.Note that smart casts work only when the compiler can guarantee that the variable won't change between the check and the usage.

The Ktor docs are great as a start and they even show you how to use three built-in converters ( Gson, Jackson and rialization). Eventually you will run into a situation where you need to either customize your serialization logic or provide a serialization implementation for a non-primitive value. python class json serializable parse json to dart model rails to json. The process of turning objects into JSON data is a common task and most of the time it's a non-issue turning an Int into a JSON number is simple, turning a String into a JSON string is simple and so on. If you're into Kotlin and you haven't tried Ktor yet, I highly suggest you try it out.


This blog post will focus on an issue I ran into recently concerning to JSON serialization when using Ktor (adding contextual serialization). Applying Serializable to the Kotlin class instructs the serialization plugin to automatically generate implementation of KSerializer for the current class, that can be used to serialize and deserialize the class. Ktor is a Kotlin framework for building web applications and HTTP services. The main entry point to the serialization process.
