New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Easy way to pretty print output? #301
Comments
I don't believe what you're seeking here is something that will be included into RABL since that'll require taking on another dependency just for development environments. What you seek is something that can convert any JSON (whether from RABL or not) into something prettier. Ultimately it depends on where you're outputting the JSON to. For me:
Happy parsing! |
Sweet, this was helpful thanks! I tried out some chrome plugins and they made it easier to view, but didn't actually change the underlying text, so when I copy/pasted the text to use in a an example in the documetation the pretty print was lost. I ended up installing this plugin for Sublime Text so now I can just ctrl-cmd-j single line json and it makes it pretty. Works well. Anyway, thanks again. |
Ahh thanks, I use Sublime most of the time but I rarely have the output in the editor. It's usually stuck in a console output or log file. If you want to copy/paste into your editor and then reformat, I guess that works! You gave me a new blog post idea... |
Epic. Btw, I really like your blog theme. Is it a jekyll theme running on Github pages or how did you set it up? |
Yeah, actually I had GitHub setup pages with that theme and then loaded up Jekyll on top of it. I tweaked it a little though to make the text darker and such but nothing major. |
how about this? in config/initializers/rabl_config.rb class PrettyJson
def self.dump(object)
Rails.env.development? ? JSON.pretty_generate(object, {:indent => " "}) : JSON.dump(object)
end
end
Rabl.configure do |config|
...
config.json_engine = PrettyJson
...
end |
@enrico Thanks! I think it may be better: class PrettyJson
def self.dump(object)
JSON.pretty_generate(object, {:indent => " "})
end
end
Rabl.configure do |config|
...
config.json_engine = PrettyJson if Rails.env.development?
...
end |
I would like to argue there is a legitimate reason you might want to allow indenting in production. Many people have pointed out it makes the APIs easier for developers to explore and "try" before consuming, and makes debugging easy for them without incurring too much of a performance hit. http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api I came up w/ the following which might be good for production. What do you guys think of this solution? I got it to work but i'm uncertain if its a robust solution. Rabl.configure do |config|
Oj.default_options = Oj.default_options.merge({indent: 2})
end |
This added to
|
I was wondering if there was an easy way to pretty print the generated json output? This would be really helpful in development as you are debugging/adding attributes etc. Thanks!
The text was updated successfully, but these errors were encountered: