README.md

Jbuilder

Jbuilder gives you a simple DSL for declaring JSON structures that beats
massaging giant hash structures. This is particularly helpful when the
generation process is fraught with conditionals and loops. Here's a simple
example:

You can either use Jbuilder stand-alone or directly as an ActionView template
language. When required in Rails, you can create views ala show.json.jbuilder
(the json is already yielded):

# Any helpers available to views are available to the builder
json.content format_content(@message.content)
json.(@message, :created_at, :updated_at)
json.author do
json.name @message.creator.name.familiar
json.email_address @message.creator.email_address_with_name
json.url url_for(@message.creator, format::json)
endif current_user.admin?
json.visitors calculate_visitors(@message)
end

You can use partials as well. The following will render the file
views/comments/_comments.json.jbuilder, and set a local variable
comments with all this message's comments, which you can use inside
the partial.

You can set this globally with the class method key_format (from inside your
environment.rb for example):

Jbuilder.key_format camelize::lower

Faster JSON backends

Jbuilder uses MultiJson, which by default will use the JSON gem. That gem is
currently tangled with ActiveSupport's all-Ruby #to_json implementation,
which is slow (fixed in Rails >= 4.1). For faster Jbuilder rendering, you can
specify something like the Yajl JSON generator instead. You'll need to include
the yajl-ruby gem in your Gemfile and then set the following configuration
for MultiJson: