- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在处理一些超出我直接控制范围的代码,我需要对一个选项 [Thing] 进行编码,如果 Thing 存在,则情况与正常情况一样,但是 None 情况必须返回“false”而不是 null。这能轻易完成吗?我正在查看文档,但没有取得太大的成功。
我的代码是这样的:
case class Thing(name: String)
case class BiggerThing(stuff: String, thing: Option[Thing])
implict val ThingEncodeJson: EncodeJson[Thing] =
EncodeJson(t => ("name" := t.name ) ->: jEmptyObject)
和 BiggerThing 的等价物,json 需要看起来像:
对于一些人:
"thing":{"name": "bob"}
对于无:
"thing": false
但目前 None 情况给出:
"thing":null
如何让它返回 false?有人能给我指出正确的方向吗?
干杯
最佳答案
您只需要为 Option[Thing]
定制一个 CodecJson
实例:
object Example {
import argonaut._, Argonaut._
case class Thing(name: String)
case class BiggerThing(stuff: String, thing: Option[Thing])
implicit val encodeThingOption: CodecJson[Option[Thing]] =
CodecJson(
(thing: Option[Thing]) => thing.map(_.asJson).getOrElse(jFalse),
json =>
// Adopt the easy approach when parsing, that is, if there's no
// `name` property, assume it was `false` and map it to a `None`.
json.get[Thing]("name").map(Some(_)) ||| DecodeResult.ok(None)
)
implicit val encodeThing: CodecJson[Thing] =
casecodec1(Thing.apply, Thing.unapply)("name")
implicit val encodeBiggerThing: CodecJson[BiggerThing] =
casecodec2(BiggerThing.apply, BiggerThing.unapply)("stuff", "thing")
def main(args: Array[String]): Unit = {
val a = BiggerThing("stuff", Some(Thing("name")))
println(a.asJson.nospaces) // {"stuff":"stuff","thing":{"name":"name"}}
val b = BiggerThing("stuff", None)
println(b.asJson.nospaces) // {"stuff":"stuff","thing":false}
}
}
当 thing
为 None
时,如何在没有 thing
属性的情况下对 BiggerThing
进行编码。然后你需要一个自定义的 EncodeJson[BiggerThing]
实例:
implicit val decodeBiggerThing: DecodeJson[BiggerThing] =
jdecode2L(BiggerThing.apply)("stuff", "thing")
implicit val encodeBiggerThing: EncodeJson[BiggerThing] =
EncodeJson { biggerThing =>
val thing = biggerThing.thing.map(t => Json("thing" := t))
("stuff" := biggerThing.stuff) ->: thing.getOrElse(jEmptyObject)
}
关于json - Scala argonaut 将 jEmtpyObject 编码为 'false' 而不是 'null',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28574518/
我正在尝试实现一个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
我是一名优秀的程序员,十分优秀!