【发布时间】:2015-02-01 20:16:24
【问题描述】:
我正在尝试使用 play-reactivemongo 和 reactivemongo-extensions 将 Rails/Mongodb 应用程序迁移到 Play 2.3。在对我的数据进行建模时,我遇到了对 Map[Int,Boolean] 进行序列化和反序列化的问题。
当我尝试像这样通过宏定义我的格式时
implicit val myCaseClass = Json.format[MyCaseClass]
其中 MyCaseClass 有几个字符串字段、一个 BSONObjectID 字段和一个编译器抱怨的 Map[Int,Boolean] 字段:
No Json serializer found for type Map[Int,Boolean]. Try to implement an implicit Writes or Format for this type.
No Json deserializer found for type Map[Int,Boolean]. Try to implement an implicit Reads or Format for this type.
查看 Reads.scala 中 Play 的源代码,我看到为 Map[String,_] 定义了一个读取,但没有为 Map[Int,_] 定义一个读取。
为什么 Play 对字符串映射具有默认读/写但对其他简单类型没有默认读/写,这是有原因的吗?
我不完全理解 play 定义的 Map[String,_] 因为我对 scala 还很陌生。我将如何将其转换为 Map[Int,_]?如果由于某种技术原因无法做到这一点,我将如何为 Map[Int,Boolean] 定义读/写?
【问题讨论】:
标签: json scala playframework playframework-2.3