简体   繁体   中英

Can Cheshire omit keys without values?

I'm using Cheshire to generate some JSON for data structures like this:

(require '[cheshire.core :refer [generate-string])
(generate-string {:id 123, :foo "something", :bar nil})

Which produces JSON like this:

{"id": 123, "foo": "something", "bar": null}

What I'd like is for the JSON to omit the keys without values; eg

{"id": 123, "foo": "something"}

Can Cheshire do this? I can certainly pre-filter the map before calling generate-string , but since Cheshire has to traverse my data structure anyway, I thought it would be more performant to instruct Cheshire to do the filtering.

No, null is a valid JSON value, so you should filter nil values yourself.

See this question for more info .

You may propose this feature to Cheshire team.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM