scala - 使用 argonaut 解码为递归 ADT

标签 scala argonaut

我正在尝试像这样解析 json

{
  "element": "string",
  "content": "application/json"
}

element 决定了 json 的类型。但是我的代码无法解析。

http://scastie.org/15213

import scalaz._, Scalaz._
import argonaut._, Argonaut._, Shapeless._

case class ArrayAttributes(default: List[StringElement])

sealed trait Element
case class StringElement(content: String) extends Element
case class ArrayElement(attributes: ArrayAttributes, content: List[Element]) extends Element
case class Reference(element: String) extends Element { def content = element }

object Parser {

  def kindDecode[T](
    kinds: Map[String, DecodeJson[T]],
    fail: HCursor => DecodeResult[T] = { c: HCursor => DecodeResult.fail[T]("expected one of ${kind.keys}", c.history) }): DecodeJson[T] = DecodeJson(c =>
    (c --\ "element").as[String].flatMap { kind =>
      kinds.get(kind).map(_.decode(c)).getOrElse(fail(c))
    }
  )

  implicit def elementDecode: DecodeJson[Element] = kindDecode(
    Map(
      "string" -> DecodeJson.of[StringElement].map(identity[Element]),
      "array" -> arrayDecode.map(identity[Element])
    ),
    { c => DecodeJson.of[Reference].decode(c).map(identity[Element]) }
  )

  def arrayDecode: DecodeJson[ArrayElement] = jdecode2L(ArrayElement.apply)("attributes", "content")

}

最佳答案

我将用 argonaut-shapeless 的当前里程碑 (1.0.0-M1) 来回答,它自 0.3.1 版本以来已经有相关的补充.

它允许为总和类型指定所谓的 JsonSumCodec,这会驱动子类型的编码/区分方式。

通过在其伴生对象中为 Element 定义一个,例如

implicit val jsonSumCodecForElement = derive.JsonSumCodecFor[Element](
  derive.JsonSumTypeFieldCodec(
    typeField = "element",
    toTypeValue = Some(_.stripSuffix("Element").toLowerCase)
  )
)

您的示例正常工作:

> Parse.decodeEither[Element](member)
res1: (String \/ (String, CursorHistory)) \/ Element =
  \/-(StringElement(application/json))
上面的

JsonSumCodecFor是一个类型类,它为给定的类型提供了一个JsonSumCodec,这里是Element。作为 JsonSumCodec,我们选择 JsonSumTypeFieldCodec,它默认使用字段 “type” 来区分子类型。在这里,我们选择 "element" 而不是 "type",我们还转换子类型名称(toTypeValue 参数),这样这些匹配示例输入的名称。

关于scala - 使用 argonaut 解码为递归 ADT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35650764/

相关文章:

python - 编写一个 Java 服务器来对传入的 HTTP 请求进行排队并稍后处理它们?

json - Argonaut-在自定义解码器中返回错误

sql - 仅选择存在的字段(SQL 或 Scala)

scala - 在scala中同时定义类型和值别名

scala - 将 Scala 代码提交到集群

Purescript 重用 Argonaut JSON 解码用于 Affjax Respondeable

scala - 使用 Argonaut Lenses 从 JSON 对象的 JSON 数组中提取值

scala - play 框架中是否有类似 twitter finagle 的过滤器概念

json - 阿尔戈英雄 : how to rename json property for Right/Left in case class containing Either

json - 使用 Argonaut 进行 Scalaz 验证