cheshire icon indicating copy to clipboard operation
cheshire copied to clipboard

Add option to omit JSON keys with null values to generate-string

Open jmglov opened this issue 10 years ago • 2 comments

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.

jmglov avatar May 15 '14 08:05 jmglov

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!

dakrone avatar May 15 '14 08:05 dakrone

I was about to open an issue for this :wink: A big :+1: then!

ddossot avatar Nov 20 '14 01:11 ddossot