【发布时间】:2014-07-02 18:59:42
【问题描述】:
我正在使用Cheshire 为这样的数据结构生成一些 JSON:
(require '[cheshire.core :refer [generate-string])
(generate-string {:id 123, :foo "something", :bar nil})
生成这样的 JSON:
{"id": 123, "foo": "something", "bar": null}
我希望 JSON 省略没有值的键;例如
{"id": 123, "foo": "something"}
柴郡能做到这一点吗?我当然可以在调用 generate-string 之前对地图进行预过滤,但是由于 Cheshire 无论如何都必须遍历我的数据结构,我认为指示 Cheshire 进行过滤会更好。
【问题讨论】:
-
不,
null是一个有效的 JSON 值,因此您应该自己过滤nil值。 See this question for more info. -
Leonid,您介意将您的评论作为答案吗? :)
-
我知道 null 是一个有效的 JSON 值,我当然不认为这应该是 Cheshire 的默认行为。我认为这将是一个不错的可选功能,有点像在解析时在 JSON 键上运行
(keyword)。 -
@JoshGlover 这绝对不应该是默认行为,因为这意味着重新解析 JSON 会返回不同的地图。