1

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")
    }
}
Steak
  • 514
  • 3
  • 15
  • 1
    What's in `MyCardAsStringSerializer`? You're probably missing how to (de)serialize `LineChart` properly. – user3738870 Aug 29 '22 at 17:16
  • Can you also post your `MyCardAsStringSerializer` serializer? What do you mean by `with Companion object` ? – iamanbansal Aug 29 '22 at 18:33
  • Oh so it's not actually implemented. Then unfortunately you have to implement the logic that will serialize and deserialize your class. Or you can remove `lineChart` then you don't need a custom serializer. – user3738870 Aug 31 '22 at 17:59

1 Answers1

1

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?

iamanbansal
  • 2,412
  • 2
  • 12
  • 20