当case类只有一个字段时如何将json转换为case类

当case类只有一个字段时如何将json转换为case类

问题描述:

在游戏2.1中,读取操作用于将Json编组为对象.但是,当案例类只有一个字段时,我该怎么办呢?适用于更多字段的意识形态无效,因为不使用一个字段"and".因此,我没有获得FunctionBuilder.

In play 2.1 reads are used to marshall Json to objects. But how can I do this when the case class has only one field. The ideom that works for more fields does not work, as with one field 'and' is not used. Thus I do not get a FunctionBuilder.

以下代码为我提供了类型不匹配的信息.我该如何解决?

The following code gives me a type mismatch. How can I fix this?

case class Data(stamm: Seq[String])


implicit val dataReads  = (
  (__ \ "stamm").read(Reads.list[String])
)(Data)

Json组合器不适用于单个字段案例类.

Json combinators doesn't work for single field case class.

Pascal(此API的编写者)已在此处说明了这种情况 https://groups.google.com/forum/?fromgroups=#!starred/play-framework/hGrveOkbJ6U

Pascal (writer of this API) has explained this situation here https://groups.google.com/forum/?fromgroups=#!starred/play-framework/hGrveOkbJ6U

有些可行的解决方法,例如以下一种方法:

There are some workarounds which works, like this one:

case class A(value: List[Int])
val areads = (__ \ 'value).read[List[Int]].map{ l => A(l) } // covariant map