Submit Akka actorRef in json

Ok, so I am writing implicit conversions for case classes in scala using SJSON to send messages to remote executors using the akka framework. One of the case classes is as follows:

case class Example(id: String, actr: ActorRef) 

How I will write an implicit case for this class.

I saw that ActorRefs has a toBinary method, but I need to send it to json

+3
source share
1 answer
/**
 * Type class definition for Actor Serialization
 */
trait FromBinary[T <: Actor] {
  def fromBinary(bytes: Array[Byte], act: T): T
}

trait ToBinary[T <: Actor] {
  def toBinary(t: T): Array[Byte]
}

// client needs to implement Format[] for the respective actor
trait Format[T <: Actor] extends FromBinary[T] with ToBinary[T]

ScalaJSON, , SerializerBasedActorFormat trait

trait SerializerBasedActorFormat[T <: Actor] extends Format[T] {
  val serializer: Serializer
  def fromBinary(bytes: Array[Byte], act: T) = serializer.fromBinary(bytes, Some(act.self.actorClass)).asInstanceOf[T]
  def toBinary(ac: T) = serializer.toBinary(ac)
}

ScalaJSON serializer. SJSON Scala , ( , ). , .

-

@BeanInfo
case class Example(id: String, 
@(JSONTypeHint @field)(value = classOf[MyActor])
actr: ActorRef) 

implicit object MyActorFormat extends SerializerBasedActorFormat[MyActor] {
    val serializer = Serializer.ScalaJSON
}
  • , case, Akka - Akka protobufs TCP.
  • ? , , self.sender, ! self.senderFuture, !! !!!. ActorRef ( RemoteActorRef) , ( stdlib / Erlang []) , .
+3

Source: https://habr.com/ru/post/1776496/


All Articles