07-20-2023, 04:11 PM
Is there a way of deserializing json using
sealed class Layer
data class ShapeLayer(var type: LayerType) : Layer
data class TextLayer(var type: LayerType) : Layer
data class ImageLayer(var type: LayerType) : Layer
LayerType is just some enum which can be used to distinguish which type should this object have.
I thought I could add Adapter this way:
class LayerAdapter{
@FromJson
fun fromJson(layerJson: LayerJson): Layer {
return when (layerJson.layerType) {
LayerType.SHAPE -> PreCompLayer()
LayerType.SOLID -> SolidLayer()
LayerType.Text -> TextLayer()
}
}
}
Where LayerJson would be object which has every possible field of all LayerTypes.
Now the problem is:
> Cannot serialize abstract class com.example.models.layers.Layer
I could try to use interface, but I don't think it would be correct to use empty interface in this.
sealed class Layer
data class ShapeLayer(var type: LayerType) : Layer
data class TextLayer(var type: LayerType) : Layer
data class ImageLayer(var type: LayerType) : Layer
LayerType is just some enum which can be used to distinguish which type should this object have.
I thought I could add Adapter this way:
class LayerAdapter{
@FromJson
fun fromJson(layerJson: LayerJson): Layer {
return when (layerJson.layerType) {
LayerType.SHAPE -> PreCompLayer()
LayerType.SOLID -> SolidLayer()
LayerType.Text -> TextLayer()
}
}
}
Where LayerJson would be object which has every possible field of all LayerTypes.
Now the problem is:
> Cannot serialize abstract class com.example.models.layers.Layer
I could try to use interface, but I don't think it would be correct to use empty interface in this.