- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试为其编写 DecodeJson[T]
的 JSON 对象包含不同“类型”的数组(意味着其元素的 JSON 结构是变化的)。唯一的共同特征是 type
字段,可用于区分类型。所有其他领域都不同。示例:
{
...,
array: [
{ type: "a", a1: ..., a2: ...},
{ type: "b", b1: ...},
{ type: "c", c1: ..., c2: ..., c3: ...},
{ type: "a", a1: ..., a2: ...},
...
],
...
}
使用 argonaut,是否可以将 JSON 数组映射到 Scala Seq[Element]
,其中 Element
是 ElementA 类型的合适案例类的父类(super class)型
、ElementB
等等?
我用 play-json
做了同样的事情,这很容易(基本上是一个评估 type
字段的 Reads[Element]
并相应地转发到更具体的 Reads
)。但是,我找不到使用 argonaut 执行此操作的方法。
编辑:示例
Scala 类型(我希望使用):
case class Container(id: Int, events: List[Event])
sealed trait Event
case class Birthday(name: String, age: Int) extends Event
case class Appointment(start: Long, participants: List[String]) extends Event
case class ... extends Event
JSON 实例(不在我的控制之下):
{
"id":1674,
"events": {
"data": [
{
"type": "birthday",
"name": "Jones Clinton",
"age": 34
},
{
"type": "appointment",
"start": 1675156665555,
"participants": [
"John Doe",
"Jane Doe",
"Foo Bar"
]
}
]
}
}
最佳答案
您可以创建一个小函数来帮助您构建处理这种格式的解码器。
请参阅下面的示例。
import argonaut._, Argonaut._
def decodeByType[T](encoders: (String, DecodeJson[_ <: T])*) = {
val encMap = encoders.toMap
def decoder(h: CursorHistory, s: String) =
encMap.get(s).fold(DecodeResult.fail[DecodeJson[_ <: T]](s"Unknown type: $s", h))(d => DecodeResult.ok(d))
DecodeJson[T] { c: HCursor =>
val tf = c.downField("type")
for {
tv <- tf.as[String]
dec <- decoder(tf.history, tv)
data <- dec(c).map[T](identity)
} yield data
}
}
case class Container(id: Int, events: ContainerData)
case class ContainerData(data: List[Event])
sealed trait Event
case class Birthday(name: String, age: Int) extends Event
case class Appointment(start: Long, participants: List[String]) extends Event
implicit val eventDecoder: DecodeJson[Event] = decodeByType[Event](
"birthday" -> DecodeJson.derive[Birthday],
"appointment" -> DecodeJson.derive[Appointment]
)
implicit val containerDataDecoder: DecodeJson[ContainerData] = DecodeJson.derive[ContainerData]
implicit val containerDecoder: DecodeJson[Container] = DecodeJson.derive[Container]
val goodJsonStr =
"""
{
"id":1674,
"events": {
"data": [
{
"type": "birthday",
"name": "Jones Clinton",
"age": 34
},
{
"type": "appointment",
"start": 1675156665555,
"participants": [
"John Doe",
"Jane Doe",
"Foo Bar"
]
}
]
}
}
"""
def main(args: Array[String]) = {
println(goodJsonStr.decode[Container])
// \/-(Container(1674,ContainerData(List(Birthday(Jones Clinton,34), Appointment(1675156665555,List(John Doe, Jane Doe, Foo Bar))))))
}
关于json - Argonaut:解码多态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40751321/
我正在尝试实现一个Argonaut JSON解码器实例,该实例将JSON字符串转换为我的枚举QuestionType的实例。问题是,如果字符串无效,则返回的DecodeResult应该是错误,并且我不
我尝试为其编写 DecodeJson[T] 的 JSON 对象包含不同“类型”的数组(意味着其元素的 JSON 结构是变化的)。唯一的共同特征是 type 字段,可用于区分类型。所有其他领域都不同。示
我正在尝试包装 Argonaut ( http://argonaut.io ) 以便在 Scala 项目中序列化/反序列化 JSON。我们之前使用过 Jerkson,但由于它已停产,我们正在寻找替代方
我定义了一个有“视频”信息的用户: case class User(name:String, video: Option[Video]) case class Video(title:String,
如何编写扩展方法以删除带有 null 的所有键来自 Argonaut 的值(value) Json目的: 我试过这个: package object Extensions { implicit c
我正在尝试像这样解析 json { "element": "string", "content": "application/json" } element 决定了 json 的类型。但是我的
我很难浏览 Argonaut 文档,所以我想我只需要一个简单的例子。 val input = """{"a":[{"b":4},{"b":5}]}""" val output = ??? // des
我有一个案例类和伴随对象: case class Person private(name: String, age: Int) object Person { def validAge(age:
我是 Scala 新手,在这里我尝试创建一个基于 Argonaut 的通用 json 转换器。我尝试在 google 和 stackoverflow 上搜索,但到目前为止我还没有任何线索。 这是我的代
假设我有这个小 Argonaut Json实例: import argonaut._, Argonaut._ Json.obj( "id" := 42, "viewed" := fal
出于向后兼容性的原因,我正在编写一个库来转换来自 API 的 JSON 响应。我需要做的是接受任意 JSON,并更改某些字段名称。我正在使用 scala 和 argonaut,但我在文档或更改字段名称
我目前正在做最崇高的编程工作,为 Json 编码/解码编写测试。我正在使用 Argonaut.io为 Json和 Scalatest对于我的测试框架。在scalatest下,使用===如果发生故障,断
我正在使用 Scala 和 Argonaut,试图解析以下 JSON: [ { "name": "apple", "type": "fruit",
假设我有这样的json { "sha": "some sha", "parents": [{ "url": "some url", "sha": "some parent sh
我正在尝试从 Haskell 服务器获取一些 JSON 数据,但我在 Respondeable 实例以及一般的 Affjax 方面遇到了问题。我已经使用 Data.Argonaut.Generic.A
我知道我是否有带有特定已知字段的 JSON,例如“日期”和“标题” { "date": "any string", "title": "any string" } 然后,我可以通过定义来解码它
在给定不完整的 json(缺少某些字段)的情况下,我需要从案例类实例创建更新的实例(隐式派生任何所需的 DecodeJson)。如何通过 Argonaut(最好)或 Circe(如果必须的话)来完成此
我正在尝试使用这样的 Play json 库更改此函数的实现 def apply[T](action: => ApiResponse[T])(implicit tjs: Writes[T], ec:
我正在处理一些超出我直接控制范围的代码,我需要对一个选项 [Thing] 进行编码,如果 Thing 存在,则情况与正常情况一样,但是 None 情况必须返回“false”而不是 null。这能轻易完
这是我在 java 中访问的 json 字符串: json = [ {"id":"123456","Data":"skill2","dmlcrud":false}, {"id":"12
我是一名优秀的程序员,十分优秀!