Lightweight JSON builder for Rails
Ruby
Latest commit 42f769c Sep 1, 2009 @jbr document xml functionality
Failed to load latest commit information.
lib Argonaut can handle .xml.argonaut Sep 1, 2009
README.textile document xml functionality Sep 1, 2009
init.rb initial commit Aug 31, 2009

README.textile

Argonaut is a lightweight JSON builder template handler for Rails

Installing it

It’s as easy as:

script/plugin install git://github.com/jbr/argonaut.git

Usage

It’s a whole lot like .xml.builder, only it’s .json.argonaut. Make an appropriately named view, like show.json.argonaut and put something in it like:

for idea in @ideas
  doc.ideas do |i|
    i.title idea.title
    for comment in idea.comments
      i.comments do |c|
        c.author comment.author.to_s
        c.text comment.text
      end
    end
  end
end

Which is identical to

{
  :ideas => @ideas.map do |idea|
    {
      :title => idea.title,
      :comments => idea.comments.map do |comment|
        {
          :author => comment.author.to_s,
          :text => comment.text
        }
      end
    }
  end
}.to_json
end

Just a little code kata, not really sure that the former is actually prettier.

XML too

The name makes less sense and it’s not really better than Builder, but you can do .xml.argonaut as well now. It’s the equivalent of calling to_xml(:root => 'response') on the above hash. In theory, the same file could be used for .xml.argonaut and .json.argonaut, but it’s not obvious how to tell the template handler system to reuse a template. Maybe soon.