[英]Sending an Akka actorRef to json
Ok so i am writing implicit conversions for case classes in scala, using SJSON, to send messages to remote actors using the akka framework. 好的,我正在使用SJSON为scala中的case类编写隐式转换,以使用akka框架向远程actor发送消息。 One of the case classes looks like this
其中一个案例类看起来像这样
case class Example(id: String, actr: ActorRef)
How would i go about writing the implicit for this case class. 我将如何编写此案例类的隐式。
I have seen that ActorRefs do have a toBinary method but i need to send it toJson 我已经看到ActorRef确实有一个toBinary方法,但我需要将它发送到json
/** * 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]
If you want ScalaJSON serialization, instead of the default one, you should use SerializerBasedActorFormat
trait 如果您想要ScalaJSON序列化而不是默认序列化,则应使用
SerializerBasedActorFormat
特征
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)
}
with ScalaJSON serializer
. 使用
ScalaJSON serializer
。 SJSON library supports serialization of plain Scala objects out-of-box, without an additional configuration (which is enough, in the most cases). SJSON库支持开箱即用的普通Scala对象的序列化,无需额外配置(在大多数情况下这已足够)。 If you need to ignore some properties, or define serialization policy of embedded objects, read this .
如果您需要忽略某些属性或定义嵌入对象的序列化策略,请阅读此内容 。
In your case, you would need something like 在你的情况下,你需要类似的东西
@BeanInfo
case class Example(id: String,
@(JSONTypeHint @field)(value = classOf[MyActor])
actr: ActorRef)
implicit object MyActorFormat extends SerializerBasedActorFormat[MyActor] {
val serializer = Serializer.ScalaJSON
}
self.sender
, if the message was sent with !
self.sender
,如果消息是与!
一起发送的!
, or self.senderFuture
, when the messages is sent with !!
self.senderFuture
,当邮件发送!!
or !!!
!!!
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.