JSON text -> Data class instance val myThingInJsonText: String = Json.stringify(rializer(), myThing) val myThing: MyThing = Json.parse(rializer(), myThingInJsonText) Serialize/DeserializeĬonverting from and to JSON is quite easy. Notice that if we are serializing a data class that refers to other data classes, the "child" classes should be as well. You would need to annotate your data classes with Note that you can annotate regular classes as well, but there is more limitation on what is supported. Implementation ":kotlinx-serialization-runtime:0.20.0"Ĭlasspath ":kotlin-serialization:$kotlin_version"Ĭheck out the official doc for the latest runtime version. You would need to modify your adle a bit, but after that is super easy. Kotlin's own way is a compiler plugin with a runtime dependency. And Kotlin, since version 1.3, provides its own way way for serializing to and from JSON (and other formats, like protobuf - maybe in another post). There are other modern JSON libraries out there (e.g. But lately, especially with Kotlin's null-aware type system, the library fell out of grace. In the past, this used to be the Gson library. In Android (and in the Java world in general) this was traditionally "outsourced" to a dedicated library. So you want to quickly convert your Data classes to JSON and vice-versa.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |