在Play框架中将Argonaut与读取和写入配合使用 [英] Use Argonaut in Play framework with Reads and Writes
本文介绍了在Play框架中将Argonaut与读取和写入配合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何使用Play JSON库对Play应用程序进行JSON解析.例如,我有以下代码:
I know how to do json parsing using play json library for play application. For example I have following code:
class PersonController extends Controller {
case class Person(age: Int, name: String)
implicit val personReads = Json.reads[Person]
implicit val personWrites = Json.writes[Person]
def create = Action(parse.json) { implicit request =>
rs.body.validate[Person] match {
case s: JsSuccess => ...
case e: JsError => ...
}
}
}
我应该如何使用Argonaut而不是Play json编写类似读写的代码?
How should I write the code like Reads and Writes using Argonaut instead of Play json?
推荐答案
也许这就是您要寻找的?
May be this is what you are looking for?
class PersonController extends Controller {
case class Person(age: Int, name: String)
implicit val PersonCodecJson: CodecJson[Person] =
casecodec2(Person.apply, Person.unapply)("age", "name")
val argonautParser = new BodyParser[Person] {
override def apply(v1: RequestHeader): Iteratee[Array[Byte], Either[Result, Person]] =
}
def argonautParser[T]()(implicit decodeJson: DecodeJson[T]) = parse.text map { body =>
Parse.decodeOption[T](body)
}
def create = Action(argonautParser) { implicit request =>
Ok(request.body.name) //request.body is the decoded value of type Person
}
}
实际上,您需要一个主体解析器,该主体解析器使用argonaut而不是play json解析请求主体.希望这会有所帮助.
Actually, you need a body parser that parses the request body using argonaut instead of play json. Hope this helps.
这篇关于在Play框架中将Argonaut与读取和写入配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文