I have created ApiBuilder which is a Ruby on Rails plugin/template engine that allows for multiple formats being laid out in a single specification, currently XML and JSON. You lay out your spec in one file and it automatically generates both XML and JSON from that specification.
Install it using
$ rails plugin install git://[github.com/lassebunk/api_builder.git](http://github.com/lassebunk/api_builder.git)
And then, in e.g. app/views/api/users/index.apibuilder:
array :users do
@users.each do |user|
element :user do
id @[user.id](http://user.id)
name @[user.name](http://user.name)
end
end
end
Returns:
[
{
"id": 1234,
"name": "Peter Jackson"
},
{
"id": 1235,
"name": "Marilyn Monroe"
}
]
And the equivalent XML.
In app/views/api/users/show.apibuilder:
element :user do
id @[user.id](http://user.id)
name @[user.name](http://user.name)
address do
street @user.street
city @user.city
end
array :interests do
@user.interests.each do |interest|
element :interest, [interest.name](http://interest.name)
end
end
end
Returns:
{
"id": 1234,
"name": "Peter Jackson",
"address": {
"street": "123 High Way",
"city": "Gotham City"
},
"interests": [
"Movies",
"Computers",
"Internet"
]
}
And the equivalent XML.
You can then call your API like this:
[http://example.com/api/users?format=json](http://example.com/api/users?format=json)
or
[http://example.com/api/users?format=xml](http://example.com/api/users?format=xml)
and so on.
What do you think?
More information at GitHub (https://github.com/lassebunk/api_builder) and my website (http://lassebunk.dk/2011/09/12/introducing-apibuilder-xml-and-json-builder-in-one/).
/Lasse Bunk