JSON formatting that is easy on eyes
ActiveRecord makes it easy to get JSON output. All you need to do is to call to_json on the model.
Here is the snippet of the code from the blog engine that powers this blog.
def show
@article = Article.find(params[:id], :include => :comments)
respond_to do |format|
format.html
format.rss
format.js do
render :text => @article.to_json, :layout => false
end
end
end
You can see the output here .
If you are working on an API or if you need to refer to the JSON output time and again then the horizontal scrolling becomes a pain point. It would be really nice if the JSON output had line break like this .
Rails makes use of to_json method added to Hash in ActiveSupport. You can overwrite to_json method to get the desired output. This is what I have.
class Hash
def to_json(options = {}) #:nodoc:
options.reverse_merge! :spacer_size => 1
hash_keys = self.keys
if except = options[:except]
hash_keys = hash_keys - Array.wrap(except)
elsif only = options[:only]
hash_keys = hash_keys & Array.wrap(only)
end
spacer = ' '
result = "\r\n #{spacer * options[:spacer_size]}{"
result << hash_keys.map do |key|
"#{ActiveSupport::JSON.encode(key.to_s)}: #{ActiveSupport::JSON.encode(self[key], options.merge(:spacer_size => options[:spacer_size]+7))}"
end * " ,\r\n #{spacer * (options[:spacer_size]+1)}"
result << '}'
end
end
Now all json outputs are nicely displayed on the browser. No horizontal scrolling.