JSON Builder for Rails?

How to build JSON formatted data structures like XML’s are done with
rxml templates?

I can’t use to_json method because I need other formatting than
object’s one… probably I should play with strings within html.erb
templates if there isn’t any better solution or JSON builder?

blackflash wrote:

How to build JSON formatted data structures like XML’s are done with
rxml templates?

I can’t use to_json method because I need other formatting than
object’s one… probably I should play with strings within html.erb
templates if there isn’t any better solution or JSON builder?

I found this link which may help you.

http://blog.tagteaminteractive.com/2008/01/28/sexy-rest-in-rails/

Hims Khurana wrote:

blackflash wrote:

How to build JSON formatted data structures like XML’s are done with
rxml templates?

I can’t use to_json method because I need other formatting than
object’s one… probably I should play with strings within html.erb
templates if there isn’t any better solution or JSON builder?

I found this link which may help you.

http://blog.tagteaminteractive.com/2008/01/28/sexy-rest-in-rails/

The link was broken; but can be referred to at the Way Back Time
Machine:
http://web.archive.org/web/20080205212821/http://blog.tagteaminteractive.com/

Also, snippet below…

def render(opts = {}, &block)
if opts[:to_yaml] then
headers[“Content-Type”] = “text/plain;”
render :text => Hash.from_xml(render_to_string(:template =>
opts[:to_yaml], :layout => false)).to_yaml, :layout => false
elsif opts[:to_json] then
# headers[“Content-Type”] = “text/javascript;”
render :json => Hash.from_xml(render_to_string(:template =>
opts[:to_json], :layout => false)).to_json, :layout => false
else
super opts, &block
end
end

respond_to do |wants|
wants.html
wants.xml { render :layout => false }
wants.yaml { render :to_yaml => “nodes/show.xml.builder” }
wants.json { render :to_json => “nodes/show.xml.builder” }
end

it’s not exactly like rxml but should do the job: