You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Circe解码含List字段的案例类时出现DecodingFailure问题求助

问题描述

我定义了包含嵌套案例类List字段的Outer案例类,代码如下:

final case class Nested(p1: String, p2: String)
final case class Outer(str: Option[String], lst: Option[List[Nested]])

我为这两个类创建了隐式的Encoder和Decoder,执行val decodedEither = decode[Outer](jsonString)时,出现错误:DecodingFailure(Type does not match expected: Outer, List())。我的导入语句如下:

import io.circe.Decoder.Result
import io.circe._
import io.circe.parser._
import io.circe.syntax._
import cats.syntax.show._

实际类名不同,我简化了复杂场景的问题,发现只要字段是List(或Set)就会触发该错误,请问需要补充什么配置才能让Circe正常解析这类案例类?

解决方案

你遇到的问题大概率是手动编写的编解码器未正确处理集合类型(List/Set)的嵌套解析,或是缺少Circe针对集合类型的默认编解码器支持。以下是两种可行解决方式:

方式一:使用Circe自动派生编解码器(推荐)

Circe可通过自动派生为案例类生成正确的编解码器,无需手动编写。只需补充导入自动派生的核心包:

import io.circe.generic.semiauto._

再用半自动派生方式生成Nested和Outer的编解码器:

implicit val nestedDecoder: Decoder[Nested] = deriveDecoder[Nested]
implicit val nestedEncoder: Encoder[Nested] = deriveEncoder[Nested]

implicit val outerDecoder: Decoder[Outer] = deriveDecoder[Outer]
implicit val outerEncoder: Encoder[Outer] = deriveEncoder[Outer]

这种方式会自动处理List、Set、Option等容器类型的解析,无需额外配置。

方式二:修复手动编写的编解码器

若必须手动实现编解码器,需确保解析lst字段时,正确处理List[Nested]的解析逻辑。例如手动编写Outer解码器时,要明确调用对应集合类型的解码器:

implicit val outerDecoder: Decoder[Outer] = Decoder.instance(c =>
  for {
    str <- c.get[Option[String]]("str")
    lst <- c.get[Option[List[Nested]]]("lst") // 需确保Nested的解码器已正确定义
  } yield Outer(str, lst)
)

同时要保证Nested的解码器已正确实现,否则列表内的元素无法解析,也会引发类型不匹配错误。

额外注意事项

  • 检查JSON字符串格式是否正确:比如lst字段的值应为数组格式,而非单个对象,JSON格式错误也会触发类型不匹配的解码失败。
  • 若使用Circe 0.14及以上版本,自动派生包路径为io.circe.generic.semiauto;旧版本可能为io.circe.generic.auto,但半自动派生更灵活,建议优先使用semiauto。

内容的提问来源于stack exchange,提问作者user3791111

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 10:25:15