silkapp / rest

Packages for defining APIs, running them, generating client code and documentation.
http://silkapp.github.io/rest
390 stars 52 forks source link

Pretty-print JSON output #132

Open thomasvnoort opened 9 years ago

thomasvnoort commented 9 years ago

It would be great if there would be a way to instruct rest to pretty-print JSON output. Many web services offer this by passing a pretty query parameter. I understand that this will reserve a name, so it should definitely be optional and configurable.

hesselink commented 9 years ago

Sounds good! Note that if you're on the command line, you can pipe your output through e.g. python -m json.tool or something like jq. Browsers generally also pretty-print json in their debuggers. What's your use case for this?

thomasvnoort commented 9 years ago

It's just a matter of convenience. I use Safari mostly and I don't think I can get Safari to pretty-print, but having a query parameter saves me from opening the debugger all the time anyway. Also, I use curl a lot and I can pipe it through some pretty-printer indeed. However, any output that is not JSON will fail in the pretty-printer with a weird error message. Then I have to think about getting curl to also print the error code somewhere, etc. Having a pretty query parameter makes life just a little bit easier :)

octopuscabbage commented 9 years ago

I'll look into this.

octopuscabbage commented 9 years ago

Can you better explain what you would like to see? I'm not sure I'm quite getting it.

bergmark commented 9 years ago

@thomasvnoort is on vacation...

We discussed it a bit. We would like something that adds a pretty parameter (name might be customizable) to every end point with json output.

One idea is to have prettyJsonO/prettyXmlO combinators. When writing an api you can make sure to just use that instead of the normal combinators. I think this needs #20. This means you could forget it, but in practice you can have a re-export module that only exposes the combinators you want to us.

Another way is to implement it directly in the driver, that could be done right now. It would require some way to configure this, probably a parameter to apiToHandler. This would apply it to all matching handlers.