Steak
Steak

Reputation: 544

Kotlin Serialization with Companion object

I have a data class "MyCard" that is passed into a view object (Android Studio). Here is MyCard class:

@kotlinx.serialization.Serializable(with=MyCardAsStringSerializer::class)
@SerialName("MyCard")
data class MyCard(
    val imgResource: Int,
    val cardTitle: String,
    val cardDate: String,
    val cardDescription: String,
    // chart
    val lineChart: LineChart
)

I am trying to serialize this object. However, due to the inclusion of the lineChart object, I am having issues (Line chart from MPChart).

I am trying to recreate the object from an intent, by doing something like

val myCard = intent.getSerializableExtra(intent__MYCARD_OBJ)

I have looked at Kotlin's guide to serialization (https://kotlinlang.org/docs/serialization.html#example-json-serialization) and other SO posts (Kotlin serialization: Serializer has not been found for type 'UUID') but am not sure where to go here.

Edit (08/31/22):

object MyCardAsStringSerializer : KSerializer<MyCard>{
    override val descriptor: SerialDescriptor
        get() = TODO("Not yet implemented")

    override fun deserialize(decoder: Decoder): MyCard {
        TODO("Not yet implemented")
    }

    override fun serialize(encoder: Encoder, value: MyCard) {
        TODO("Not yet implemented")
    }
}

Upvotes: 1

Views: 1177

Answers (1)

iamanbansal
iamanbansal

Reputation: 2742

You haven't added the implementation, you need to do that first.

You should remove the LineChart from the data class. Data class should only have Modal classes.

Just use the data in the data class with which you want to build the LineChart instead of using the LineChart object.

LineChart isn't modal class, it's a View class. Why do you want to serialize the View class?

Upvotes: 1

Related Questions