Hi, is there any library or a way to use Kotlinx Serialization with a serializer that converts the M...
a
Hi, is there any library or a way to use Kotlinx Serialization with a serializer that converts the MutableStates to their values (same with lists/maps) ?
d
Probably. Can you provide an example class you want to serialize?
a
Copy code
interface Template {
    @Composable
    fun Content()
}
 
abstract class VanillaCraft : Template

class ItemTemplate : VanillaTemplate() {
	val id = mutableStateOf("")
	val tag = mutableStateOf("")

    override fun Content() { /* ... */ }
}
d
Yes you can annonate
ItemTemplate
with
@Serializable
and annotate
id
and
tag
with a small custom serializer to do the wrap/unwrap for you.
a
But I have a small problem, I have this :
Copy code
@Serializable
abstract class CookingRecipeSingle(
   type: RecipeType,
) : RecipeTemplate() {
   @SerialName("cookingtime")
   val cookingTime = mutableStateOf(0)
   val experience = mutableStateOf(0.0)
   val group = mutableStateOf("")
   val ingredient = mutableStateOf(ItemTemplate())
   val result = mutableStateOf("")
   
   init {
      this.type.value = type
   }

    override fun Content() { /* ... */ }
}
This class is used by multiple other classes RecipeType is an enum Exemple :
Copy code
@Serializable
class BlastingRecipeSingleTemplate : CookingRecipeSingle(RecipeType.BLASTING) {
	init {
		cookingTime.value = 100
	}
}
but the @Serializable annotation doesn't work as it gives me this error
Copy code
This class is not serializable automatically because it has primary constructor parameters that are not properties
So how can I do ?
d
Do you really need it as a ctor parameter?
You could make it an abstract property no?
a
Ah yes you're right, it should work then