It’s actually not a question about Akka, but I really can’t solve it by myself.
- I declare a container
Tree[T]
.
trait NodeId {
def id: Int
}
trait Tree[T <: NodeId] {
def find(id: Int): Option[T]
def size: Int
def root: T
def append(node: T, to: T): Tree[T]
def remove(id: Int): Tree[T]
}
object Tree {
val RootId : Int = 0
val EmptyId: Int = -1
def apply[T <: NodeId](root: T): Tree[T] = {
if (root == null || root.id != RootId)
throw new IllegalArgumentException("")
else
TreeImpl(root :: Nil)
}
private case class Node(id: Int, child: Int = EmptyId, sibling: Int = EmptyId)
private case class TreeImpl[T <: NodeId](list: List[T], private val tree: List[Node] = Node(RootId) :: Nil)
extends Tree[T] {
override def find(id: Int): Option[T] = list.find(_.id == id)
def size: Int = list.size
def root: T = this.find(RootId).get
override def append(node: T, to: T): Tree[T] = { ... }
override def remove(id: Int): Tree[T] = { ... }
}
}
- I delcare a drived class.
case class Stage(id: Int, title: String) extends NodeId
object Stage {
val Root: Stage = Stage(id = RootId, title = "ROOT")
}
- I use Tree[Stage] to store the state of entity.
trait Plan extends Aggregate {
type State = Plan
def projectId: String
def stages: Tree[Stage]
}
final case class PlacedPlan(id: String, projectId: String, stages: Tree[Stage] = Tree[Stage](Root)) extends Plan { ... }
Now, I get the error message: State [PlacedPlan(...,TreeImpl(List(...),List(...))] isn't serializable.
I think it should put @JsonTypeInfo
to somewhere of Tree[T]
, TreeImpl
or trait Plan
. I read the reference of @JsonTypeInfo. But Tree
isn’t a collection like List
or Array
, even @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
or @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type") @JsonSubTypes(Array(new JsonSubTypes.Type(value = classOf[Tree[_]], name = "tree")))
not going,
I don’t know how to do, give me some help…
Thanks.