我的JSON看起来像:
created_at
createdAt
我试着让circe通用的附加功能做了这个:
隐式val customConfig:配置=Configuration.default.withsnakecassembernames
Actual: Left(DecodingFailure(Attempt to decode value on failed cursor, List(DownField(createdAt))))
我的case类中有太多的属性,所以我不想手动编写这些属性来解决这个问题,而是寻找一种自动/懒惰的方法:)
import io.circe._
import io.circe.generic.semiauto._
import io.circe.generic.extras.Configuration
case class MyComponent(
createdAt: String
)
object MyComponent {
implicit val customConfig: Configuration = Configuration.default.withSnakeCaseMemberNames
implicit val componentDecoder: Decoder[MyComponent] = deriveDecoder[MyComponent]
implicit val componentEncoder: Encoder[MyComponent] = deriveEncoder[MyComponent]
}
我的spec2测试:
val decodedComponent = parser.decode[MyComponent](jsonString)
decodedComponent must_=== Right(expected)