1

I'd like to parse JSON Array(in String) to Kotlin Array of multiple classes. Is it possible to do serialization like below? I'm using kotlinx.serialization, but can use other libraries if needed.

abstract class DataTypeBase(val dataType: DataTypeEnum) {
    abstract fun doSomething()
}

@Serializable
data class DataTypeA(dataType: DataTypeEnum, val a: String): DataTypeBase(dataType) {}

@Serializable
data class DataTypeB(dataType: DataTypeEnum, val b: Int) : DataTypeBase(dataType) {}

fun customSerializer(): KSerializer {
    when (json.dataType) {
        DataTypeEnum.A -> return DataTypeA.serializer()
        DataTypeEnum.B -> return DataTypeB.serializer()
    }
}

fun run() {
    val dataArray: Array<DataTypeBase> = 
         Json().parse(ArrayListSerializer(customSerializer(), jsonArrayInString) 
}

For example,

[{ dataType: "A", a: "text" }, { dataType: "B", b: 123 }]

becomes

Array[0] : DataTypeA("A", "text")
Array[1] : DataTypeB("B", 123)
rhythm
  • 469
  • 9
  • 24

1 Answers1

2

You may use the following:

@Serializable
abstract class DataTypeBase()

@Serializable
data class DataTypeA(val a: String) : DataTypeBase()

@Serializable
data class DataTypeB(val b: Int) : DataTypeBase()

fun main() {
    val format = Json(context = SerializersModule {
        polymorphic(DataTypeBase::class) {
            DataTypeA::class with DataTypeA.serializer()
            DataTypeB::class with DataTypeB.serializer()
        }
    })
    val string = format.stringify(DataTypeBase.serializer().list, listOf(DataTypeA("text"), DataTypeB(123)))
    println(string)
    println(format.parse(PolymorphicSerializer(DataTypeBase::class).list, string))
}

output

[{"type":"DataTypeA","a":"text"},{"type":"DataTypeB","b":123}]
[DataTypeA(a=text), DataTypeB(b=123)]
Markus
  • 46
  • 3
  • What are the required imports except for `serialization.*` and `serialization.json.*`? This gives me error saying `Cannot access '': it is protected in 'Json'`. – Fureeish Mar 03 '22 at 14:04