Skip to content
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

Closed
barmstrong opened this issue Jul 30, 2012 · 9 comments
Closed

Easy way to pretty print output? #301

barmstrong opened this issue Jul 30, 2012 · 9 comments

Comments

@barmstrong
Copy link

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!

@databyte
Copy link
Collaborator

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!

@barmstrong
Copy link
Author

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.
https://github.com/dzhibas/SublimePrettyJson

Works well. Anyway, thanks again.

@databyte
Copy link
Collaborator

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...

Pretty JSON

@barmstrong
Copy link
Author

Epic. Btw, I really like your blog theme. Is it a jekyll theme running on Github pages or how did you set it up?

@databyte
Copy link
Collaborator

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.

@enrico
Copy link

enrico commented Nov 5, 2012

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

@hooopo
Copy link

hooopo commented Jul 23, 2013

@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

@poori
Copy link

poori commented Oct 1, 2013

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

@jimflood
Copy link

jimflood commented Sep 4, 2015

This added to config/initializers/rabl_init.rb formats datetime in ISO 8601 in case you need to validate against JSON schemas:

ActiveSupport::TimeWithZone.class_eval do
  alias_method :orig_to_s, :to_s
  def to_s(format = :default)
    format == :default ? iso8601 : orig_to_s(format)
  end
end

Repository owner deleted a comment Jul 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants