Quantcast
Channel: Akka Libraries - Discussion Forum for Akka technologies
Viewing all articles
Browse latest Browse all 1362

Question about serialization with @JsonTypeInfo

$
0
0

It’s actually not a question about Akka, but I really can’t solve it by myself.


  1. 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] = { ... }      
  }
}
  1. I delcare a drived class.
case class Stage(id: Int, title: String) extends NodeId

object Stage {
  val Root: Stage = Stage(id = RootId, title = "ROOT")
}
  1. 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, so I don’t know how to do.

Thanks.

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 1362