json - Circe 无法将原始 json 转换为案例类错误 : could not find Lazy implicit value of type io. circe.generic.decoding.DerivedDecoder

标签 json scala kubernetes case-class

我已经为 JSON 表示定义了几个 case 类,但我不确定我是否做得正确,因为有很多嵌套的 case 类。
spec、meta 等实体属于 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 不为 Object 等类型提供编码器或解码器。或 Any ,没有有用的静态信息。

如果您将该案例类更改为如下所示:

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()))))

不管其他答案是什么,circe 绝对可以为没有成员的案例类派生编码器和解码器——这绝对不是这里的问题。

作为旁注,我希望有比这更好的错误消息:
Error: could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder[dataModel.CustomObject

但考虑到 circe-generic 必须使用 Shapeless 的 Lazy现在,这是我们能得到的最好的结果。你可以试试circe-derivation对于 circe-generic 的半自动派生的主要替代方案,它具有更好的错误消息(和一些其他优点),或者您可以使用像 splain 这样的编译器插件它专门设计用于提供更好的错误消息,即使在出现 shapeless.Lazy 之类的情况下也是如此。 .

最后要注意的是,您可以通过在 deriveDecoder 上设置类型参数来稍微清理一下您的半自动定义。推断:
implicit val customObjectLabelsDecoder: Decoder[Labels] = deriveDecoder

这完全是一个品味问题,但我发现阅读起来不那么嘈杂。

关于json - Circe 无法将原始 json 转换为案例类错误 : could not find Lazy implicit value of type io. circe.generic.decoding.DerivedDecoder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54150001/

相关文章:

java - 如何处理通过 Scala/Java ProcessBuilder 发送的 SSH 命令上的空格?

amazon-web-services - "eksctl create iamserviceaccount"在 EKS 集群上做了什么?

scala - 更新spark循环中的列值

kubernetes - 我可以在 Kubernetes Pod 内挂载 GCS 存储桶吗?

kubernetes - Kubernetes Ingress + Apache Airflow

javascript - SQLite 使用 JSON.stringify 插入表。用引号引起来的字符串中断

javascript - 如何使用 jquery 和 ajax 更新 json 中的数据

javascript - 无法在运行时使用 json 数据填充 Highchart

java - JSoup:请求 JSON 响应

java - 使用 statement.setArray() 将 Scala Option[List[_]] 传递给 Java JDBC Array