ngrunwald / ring-middleware-format

Ring middleware for parsing parameters and emitting responses in JSON or other formats
163 stars 49 forks source link

Question: how do I enable pretty-printed JSON output? #61

Closed timgilbert closed 8 years ago

timgilbert commented 8 years ago

Hi. This is more of a question than an issue - I can see that there is a way to pass options to the various specific formatters, but I can't seem to figure out the right argument structure to actually get them recognized. What I'd like to do is get the JSON response output pretty-printed. I've tried this:

(wrap-restful-format :formats [:json-kw :edn] :format-options {:json-kw {:pretty true}})

...but it doesn't seem to have an effect, and I'm getting a little lost trying to trace the arguments through the source code.

An example would be really helpful here - I'm happy to submit a PR for the README if someone can help me out. Thanks!

Deraen commented 8 years ago

:format-options works for wrap-restuful-response, use :response-options with wrap-restful-format.

https://github.com/ngrunwald/ring-middleware-format/blob/master/src/ring/middleware/format.clj#L19-L20

timgilbert commented 8 years ago

Ah, thanks for the pointer @Deraen. Digging a little deeper into the code, it looks as though there actually is no way to get pretty-printed JSON, since the value of (:pretty options) is always overridden by the first parameter to (make-json-encoder):

Any particular reason for this? If not, would you be open to a PR to revert this behavior but still default to non-pretty printed output?

Deraen commented 8 years ago

Looks like a bug I missed when introducing support for providing options to Cheshire. PR welcome.

(merge {:pretty pretty} options) should work and a test to check pretty JSON output would be great.

Deraen commented 8 years ago

Very similar to this change in fact: https://github.com/ngrunwald/ring-middleware-format/commit/8976b285aa3175f729603ec2a0067ae57db2d37e

Deraen commented 8 years ago

Fix merged.