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

Add option to pretty print #195

Closed
MarkMurphy opened this issue May 28, 2014 · 9 comments
Closed

Add option to pretty print #195

MarkMurphy opened this issue May 28, 2014 · 9 comments

Comments

@MarkMurphy
Copy link

It would be nice to be able to pretty print the json generated by jbuilder. The only solution I've found online to do this is pretty ugly:

 json_string = render_to_string formats: :json    
 json_object = JSON.parse(json_string)     
 render :json => JSON.pretty_generate(json_object)   
@rwz
Copy link
Collaborator

rwz commented May 28, 2014

Not all MultiJson adapters support pretty_generate and there is no common interface for it. You could patch Jbuilder#target! method to conditionally give you pretty response when you want it to.

Something like this:

# config/initializers/jbuilder_pretty.rb

require "jbuilder"

class Jbuilder
  alias_method :_original_target, :target!

  def target!
    PRETTY_RESPONSE_ENABLED ? JSON.pretty_generate(@attributes) : _original_target
  end
end

@rwz rwz closed this as completed May 28, 2014
@MarkMurphy
Copy link
Author

Thanks!

@MarkMurphy
Copy link
Author

In the context of an API. How might I change your_condition_goes_here so that I could specify JSON.pretty_generate per request?

@rwz
Copy link
Collaborator

rwz commented May 28, 2014

# config/initializers/jbuilder_pretty.rb

require "jbuilder"

class Jbuilder
  alias_method :_original_target, :target!

  def prettify_response!
    @_pretty_response = true
  end

  def target!
    @_pretty_response ? JSON.pretty_generate(@attributes) : _original_target
  end
end
# app/views/your-jbuilder-template.json.jbuilder

if @i_want_responses_to_be_pretty
  json.prettify_response!
end

json.foo "bar"
# app/controllers/your_controller.rb

def your_action
  @i_want_responses_to_be_pretty = true
end

@MarkMurphy
Copy link
Author

MarkMurphy commented May 28, 2014

Thanks @rwz. For those who may find their way here, this is what my finial implementation ended up looking like:

# config/initializers/jbuilder_prettify.rb
require "jbuilder"

class Jbuilder
  ##
  # Allows you to set @prettify manually in your .jbuilder files. 
  # Example: 
  #   json.prettify true
  #   json.prettify false 
  #  
  attr_accessor :prettify

  alias_method :_original_target, :target!

  ##
  # A shortcut to enabling prettify.
  # Example:
  #   json.prettify!
  #
  def prettify!
    @prettify = true
  end

  def target!
    @prettify ? ::JSON.pretty_generate(@attributes) : _original_target
  end
end
# app/views/api/v1/users/show.json.jbuilder
json.prettify! if %w(1 yes true).include?(params["pretty"])

json.( @user, :id, :name, :created_at, :updated_at )

@erick8911
Copy link

Thanks! for some weird reason i just have to change

::MultiJson.dump(@attributes, pretty: true)
instead of
::JSON.pretty_generate(@attributes)

@jmoe
Copy link

jmoe commented Sep 11, 2014

FWIW, I just found a pretty simple solution for this. I'm using Jbuilder/MultiJson/Yajl. This initializer will default to pretty output unless you are in production.

https://gist.github.com/jmoe/02c7476adac24eddd969

@royratcliffe
Copy link

Alternatively, you can just stomp over Active Support's JSON-gem encoder implementation, just for development of course. Put the following in config/initializers.
active_support_pretty_json_encoding.rb

@veganstraightedge
Copy link

@MarkMurphy @rwz Thanks for putting this together years ago. It's 2018 and I just used this without a hitch! 🎉

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