nebenmir nebenmir - 1 year ago 98
JSON Question

How to turn json to case class when case class has only one field

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])

Answer Source

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

Pascal (writer of this API) has explained this situation here!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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download