没有可用于scala的play.api.libs.json.Format实例.隐式作用域中的字符



我有一个案例类:

case class Status(cmpId: String, stage: String, status: String, outputPath: String, message: String, delimiter: Char = ',')

我正在使用:"com.typesafe.play" %% "play-json" % "2.7.2"

并编写了以下格式:

隐式val格式=默认格式

隐式val emStatusFormat=Json.format[EmStatus]

但仍收到错误:

No instance of play.api.libs.json.Format is available for scala.Char in the implicit scope

有人能帮我解决这个问题吗。

您尝试转换此格式:

Status("cmpId value", "stage value", "status value", "outputPath value", "message value", ',')

转换为JSON。

播放JSON(带有格式生成(希望将其转换为:

{
"cmpId": "cmpId value",
"stage": "stage value",
"status": "status value",
"outputPath": "outputPath value",
"message": "message value",
"delimiter": ???
}

确切地说,Char应该如何编码?它是单个字符String吗?它是一个1字节大小的整数吗?这方面没有通用的约定,这就是为什么Play JSON没有为其提供任何编解码器的原因

但你可以:

import play.api.libs.json.Format
implicit val charFormat: Format[Char] = ... // your implementation

一旦您提供了它,编译就会成功。

您可以:

  • 手写:
    implicit val charFormat: Format[Char] = new Format[Char]{
    /* implement required methods */
    }
    
  • 使用另一个编解码器生成
    import play.api.libs.functional.syntax._
    implicit val charFormat: Format[Char] = implicitly[Format[String]].inmap[Char](_.head, _.toString)
    

最新更新