0

I want to deserialize a JSON file to data object in Kotlin.

data class user(
   val name: String,
   val accounts: List<Account>
)

Account is an abstract class with generic parameters.

I am not sure I can deserialize with a Json file with a abstract class inside.

enzo
  • 9,861
  • 3
  • 15
  • 38
Nan Li
  • 1
  • I wouldn't recommend using Gson with Kotlin in general, though. [Kotlinx Serialization](https://github.com/Kotlin/kotlinx.serialization), [Moshi](https://github.com/square/moshi), or even Jackson with [the Kotlin module](https://github.com/FasterXML/jackson-module-kotlin) would be more appropriate. – Joffrey Aug 18 '21 at 17:24
  • @Joffrey Thanks for the answer. I see the document, as far as I see, Gson and others like Moshi , Kotlinx Serialization are quite similar. Is there a real benefit to change from Gson to others? – Nan Li Aug 19 '21 at 09:04
  • Gson is not really maintained anymore, and uses reflection to set fields, which can cause nullability issues in Kotlin. Moshi is the natural successor of Gson (it's kinda unofficially Gson 3.0), and has improved [several things](https://stackoverflow.com/a/54242017/1540818) - performance in particular. Kotlinx Serialization is codegen-based and made by the Kotlin team, so it supports all Kotlin-specific stuff (like sealed/value classes) out of the box. It's also multiplatform so it's definitely a must if you intend to share your classes between platforms. – Joffrey Aug 19 '21 at 09:50

0 Answers0