cheshire
cheshire copied to clipboard
Add option to omit JSON keys with null values to generate-string
As per my Stack Overflow question, I'm generating 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; e.g.
{"id": 123, "foo": "something"}
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.
To be clear, I am in no way proposing that this should be the default behaviour, simply that it could be an optional flag similar to :date-format
, :pretty
, etc.
If this is a feature that you could countenance, I'd be happy to send a pull request.
If we could add this as an option without sacrificing the performance of regular generation, I would be interested in seeing it, pull request welcome!
I was about to open an issue for this :wink: A big :+1: then!