json - deserialize using Play framework ScalaJsonGenerics vs Jerkson -
i seem confused when using play framework on how deserialize json correctly. jerkson looks have define case class automatically deserializes json string (stolen jerkson docs).
case class person(id: long, name: string) parse[person]("""{"id":1,"name":"coda"}""") //=> person(1,"coda") but, play framework have write lot of boiler plate code same thing. instance documentation.
case class foo(name: string, entry: int) object foo { implicit object fopreads extends format[foo] { def reads(json: jsvalue) = foo( (json \ "name").as[string], (json \ "entry").as[int]) def writes(ts: foo) = jsobject(seq( "name" -> jsstring(ts.name), "entry" -> jsnumber(ts.entry))) } } this seems lot more work, assume i'm either not using correctly or don't quite understand advantage of doing way. there short cut don't have write of code? if not, should using jerkson in action parse incoming json string? seems though astext returning blank string, when asjson works fine...which leads me believe doing wrong.
thanks
i think there 2 answers question.
for less boiler plate, can use the play support handling case classes. here's example case class 3 fields:
implicit val samplesetformat: format[sampleset] = productformat3("sensorid", "times", "values")(sampleset)(sampleset.unapply)
i agree there more annoying boiler plate, main reason play folks seem use approach can determine correct serializer entirely @ compile time. no cost of reflecting in jerkson.
Comments
Post a Comment