将 Akka actorRef 发送到 json
好的,我正在使用 SJSON 为 scala 中的案例类编写隐式转换,以使用 akka 框架向远程参与者发送消息。其中一个案例类看起来像这样
case class Example(id: String, actr: ActorRef)
我将如何为该案例类编写隐式内容。
我已经看到 ActorRefs 确实有一个 toBinary 方法,但我需要将其发送到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. 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要 ScalaJSON 序列化,而不是默认序列化,您应该使用
SerializerBasedActorFormat
特征和
ScalaJSON 序列化器
。SJSON 库支持开箱即用的普通 Scala 对象的序列化,无需额外配置(在大多数情况下这就足够了)。如果您需要忽略某些属性,或定义嵌入对象的序列化策略,请阅读 这个。
在你的情况下,你需要类似的东西
self.sender
(如果消息是通过!
或发送的) self.senderFuture
,当消息使用!!
或!!!
发送时。 ActorRef(或 RemoteActorRef)本身只是一个 Actor 的抽象接口,用于封装内部 Actor 的实现并让外部仅通过消息与 Actor 进行通信(与 stdlib Actors 相比/很像在 Erlang [进程] 中所做的那样)和保存非常少量的数据,这对于序列化和通过网络发送是有意义的。If you want ScalaJSON serialization, instead of the default one, you should use
SerializerBasedActorFormat
traitwith
ScalaJSON serializer
.SJSON library supports serialization of plain Scala objects out-of-box, without an additional configuration (which is enough, in the most cases). If you need to ignore some properties, or define serialization policy of embedded objects, read this.
In your case, you would need something like
self.sender
, if the message was sent with!
, orself.senderFuture
, when the messages is sent with!!
or!!!
. ActorRef (or RemoteActorRef) on itself is just an abstract interface to an actor, used to encapsulate internal actor's implementation and letting externals communicate with the actor only via messages (in contrast to stdlib Actors / much like it's done in Erlang [processes]) and holds very small amount of data that makes sense to serialize and send over wire.