PersistentStateComponent: getState/loadState with sealed class

已回答

I have an implementation of PersistentStateComponent with the state class below. It worked fine until I decided to make EndpointConfig a sealed class with two subclasses. I learned that getState/loadState doesn't support a sealed class in the state out-of-the-box.

Is there a way to make getState/loadState work with EndpointConfig being a sealed class? Maybe with kotlinx.serialization?

data class State(var endpoints: MutableMap<String, EndpointConfig> = mutableMapOf())

Thank you in advance!
Maksim

0

Hi Maksim,

I guess you get an error when the EndpointConfig class is being instantiated, and it cannot, as sealed classes are abstract.

I don't see any extension points in the IntelliJ serialization mechanism that allows customizing behavior.

You can try holding both types of endpoints in separate private collections and expose them as a merged collection. I mean something like:

data class State(
   @MapAnnotation private var endpointsA: MutableMap<String, EndpointConfigA> = mutableMapOf(),
   @MapAnnotation private var endpointsB: MutableMap<String, EndpointConfigB> = mutableMapOf()
) {

@Transient
var endpoints: Map<String, EndpointConfig>
get() = <map combining endpointsA and endpointsB>

}

 

0

请先登录再写评论。