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)

1 Answer 1

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)]
Sign up to request clarification or add additional context in comments.

1 Comment

What are the required imports except for serialization.* and serialization.json.*? This gives me error saying Cannot access '<init>': it is protected in 'Json'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.