Circe 无法将原始 json 转换为案例类 错误:找不到 io.circe.generic.decoding.DerivedDecoder 类型的延迟隐式值



我已经为JSON表示定义了几个案例类,但我不确定我是否做得正确,因为有很多嵌套的案例类。spec、meta等实体的类型与Custom对象本身的类型一样,都是JSONObject。

以下是我定义的所有类:

case class CustomObject(apiVersion: String,kind: String, metadata: Metadata,spec: Spec,labels: Object,version: String)
case class Metadata(creationTimestamp: String, generation: Int, uid: String,resourceVersion: String,name: String,namespace: String,selfLink: String)
case class Spec(mode: String,image: String,imagePullPolicy: String, mainApplicationFile: String,mainClass: String,deps: Deps,driver: Driver,executor: Executor,subresources: Subresources)
case class Driver(cores: Double,coreLimit: String,memory: String,serviceAccount: String,labels: Labels)
case class Executor(cores: Double,instances: Double,memory: String,labels: Labels)
case class Labels(version: String)
case class Subresources(status: Status)
case class Status()
case class Deps()

这是我需要转换的自定义K8s对象的JSON结构:

{
"apiVersion": "sparkoperator.k8s.io/v1alpha1",
"kind": "SparkApplication",
"metadata": {
"creationTimestamp": "2019-01-11T15:58:45Z",
"generation": 1,
"name": "spark-example",
"namespace": "default",
"resourceVersion": "268972",
"selfLink": "/apis/sparkoperator.k8s.io/v1alpha1/namespaces/default/sparkapplications/spark-example",
"uid": "uid"
},
"spec": {
"deps": {},
"driver": {
"coreLimit": "1000m",
"cores": 0.1,
"labels": {
"version": "2.4.0"
},
"memory": "1024m",
"serviceAccount": "default"
},
"executor": {
"cores": 1,
"instances": 1,
"labels": {
"version": "2.4.0"
},
"memory": "1024m"
},
"image": "gcr.io/ynli-k8s/spark:v2.4.0,
"imagePullPolicy": "Always",
"mainApplicationFile": "http://localhost:8089/spark_k8s_airflow.jar",
"mainClass": "org.apache.spark.examples.SparkExample",
"mode": "cluster",
"subresources": {
"status": {}
},
"type": "Scala"
}
}

更新:我想用Circe将JSON转换为case类,然而,对于这样的类,我会面临以下错误:

Error: could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder[dataModel.CustomObject]
implicit val customObjectDecoder: Decoder[CustomObject] = deriveDecoder[CustomObject]

我已经为所有案例类定义了隐式解码器:

implicit val customObjectLabelsDecoder: Decoder[Labels] = deriveDecoder[Labels]
implicit val customObjectSubresourcesDecoder: Decoder[Subresources] = deriveDecoder[Subresources]
implicit val customObjectDepsDecoder: Decoder[Deps] = deriveDecoder[Deps]
implicit val customObjectStatusDecoder: Decoder[Status] = deriveDecoder[Status]
implicit val customObjectExecutorDecoder: Decoder[Executor] = deriveDecoder[Executor]
implicit val customObjectDriverDecoder: Decoder[Driver] = deriveDecoder[Driver]
implicit val customObjectSpecDecoder: Decoder[Spec] = deriveDecoder[Spec]
implicit val customObjectMetadataDecoder: Decoder[Metadata] = deriveDecoder[Metadata]
implicit val customObjectDecoder: Decoder[CustomObject] = deriveDecoder[CustomObject]

不能派生CustomObject的解码的原因是因为labels: Object成员。

在circe中,所有解码都是由静态类型驱动的,circe不为ObjectAny等类型提供编码器或解码器,这些类型没有有用的静态信息。

如果您将该case类更改为以下内容:

case class CustomObject(apiVersion: String, kind: String, metadata: Metadata, spec: Spec)

…并保持代码的其余部分不变,导入:

import io.circe.Decoder, io.circe.generic.semiauto.deriveDecoder

并将您的JSON文档定义为doc(在"image": "gcr.io/ynli-k8s/spark:v2.4.0,行添加引号使其成为有效的JSON之后),以下操作应该很好:

scala> io.circe.jawn.decode[CustomObject](doc)
res0: Either[io.circe.Error,CustomObject] = Right(CustomObject(sparkoperator.k8s.io/v1alpha1,SparkApplication,Metadata(2019-01-11T15:58:45Z,1,uid,268972,spark-example,default,/apis/sparkoperator.k8s.io/v1alpha1/namespaces/default/sparkapplications/spark-example),Spec(cluster,gcr.io/ynli-k8s/spark:v2.4.0,Always,http://localhost:8089/spark_k8s_airflow.jar,org.apache.spark.examples.SparkExample,Deps(),Driver(0.1,1000m,1024m,default,Labels(2.4.0)),Executor(1.0,1.0,1024m,Labels(2.4.0)),Subresources(Status()))))

不管其他答案是什么,cire肯定可以为没有成员的case类派生编码器和解码器——这绝对不是问题所在。

顺便说一句,我希望有比这更好的错误消息:

Error: could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder[dataModel.CustomObject

但考虑到circe generic现在必须使用Shapeless的Lazy,这是我们能得到的最好的。你可以尝试circe派生,作为circe generic半自动派生的主要替代方案,它具有更好的错误消息(以及一些其他优势),或者你可以使用splain这样的编译器插件,它是专门设计的,即使在shapeless.Lazy这样的情况下也能提供更好的错误信息。

最后要注意的是,您可以通过推断deriveDecoder上的类型参数来清理半自动定义:

implicit val customObjectLabelsDecoder: Decoder[Labels] = deriveDecoder

这完全是品味的问题,但我觉得读起来不那么吵了。

最新更新