使用play json问题序列化LocalDateTime
我有个人案例课:使用play json问题序列化LocalDateTime,json,scala,playframework,playframework-2.0,Json,Scala,Playframework,Playframework 2.0,我有个人案例课: case class Person(name: String, createdAt: LocalDateTime) 能够将person对象序列化为json,以便将其返回给用户 我有一个服务者: object PersonSerializer { implicit val PersonFormat: OFormat[Person] = Json.format[Person] } 我在控制器中导入这个序列化程序,这样当我可以将结果作为json返回给用户时,如下所示: def
case class Person(name: String, createdAt: LocalDateTime)
能够将person对象序列化为json,以便将其返回给用户
我有一个服务者:
object PersonSerializer {
implicit val PersonFormat: OFormat[Person] = Json.format[Person]
}
我在控制器中导入这个序列化程序,这样当我可以将结果作为json返回给用户时,如下所示:
def getPeople: Action[AnyContent] = Action.async {
peopleDao.getAllPeople.map(people => Ok(Json.toJson(res)))
}
但是,我得到了这个错误:
错误:(39,55)play.api.libs.json.Format的实例不可用
对于隐式作用域中的org.joda.time.LocalDateTime(提示:如果
在同一个文件中声明,请确保它在(隐式)之前声明
val-AFormat:OFormat[Account]=Json.format[Account]
我怎样才能解决这个问题?你的答案几乎在堆栈跟踪中。基本上,为了格式化
人物
,Play的序列化程序需要知道如何序列化LocalDateTime
。您应该尝试以下方法:
object PersonSerializer {
implicit val LocalDateFormat: OFormat[LocalDateFormat] =
new OFormat[LocalDateFormat](){ /*...*/ }
implicit val PersonFormat: OFormat[Person] = Json.format[Person]
}
我建议你看看这个,这个,还有这个。你的答案基本上在堆栈跟踪中。基本上,为了格式化
人物
,Play的序列化程序需要知道如何序列化LocalDateTime
。您应该尝试以下方法:
object PersonSerializer {
implicit val LocalDateFormat: OFormat[LocalDateFormat] =
new OFormat[LocalDateFormat](){ /*...*/ }
implicit val PersonFormat: OFormat[Person] = Json.format[Person]
}
我建议您看看这个、这个和。另一个选项是使用jsoniter scala: 您将获得对
java.time.*
类的内置支持,其解析和序列化吞吐量将提高10倍以上
只需查看Jsoniter scala与Circe、Jackson和Play JSON的ArrayOfLocalDateFormatBenchmark对比结果:另一个选项是使用Jsoniter scala: 您将获得对
java.time.*
类的内置支持,其解析和序列化吞吐量将提高10倍以上
只需查看Jsoniter scala与Circe、Jackson和Play JSON的ArrayOfLocalDateFormatBenchmark结果: