Rails, REST and JSON

Hi everyone,

I’m writing a lightweight AJAX application using Rails on the server side as a RESTful web service provider. I need the web service to support both JSON and XML I/O. Outputting data in XML and JSON is easy (using to_xml and to_json),

and it’s also easy to do XML input as Rails does it for you automatically. Is it possible to somehow have the same automatic parsing with JSON?

Erik

Hey Erik,

afaik, no. I only have to output stuff to json one place in my app, so I just wrote a basic to_json method:

  def to_json     video = attributes.reject { |k, v| ignored_attributes.include?(k) }     video["video_id"] = video_id     video["tags"] = tags.collect { |t| t.name }     video["created_at"] = created_at.to_s     video["assets"] = assets     video["screenshots"] = screenshots     video["site-name"] = site_name     video.to_json   end

That's taken us as far as we need to so far.

Pat

Hey pat,

My question was about JSON input and parsing, not output.

Erik

Oops, I'm sorry.

Well the first thing you should know is that json is super close to yaml. The only difference afaik is that yaml cares about whitespace, json doesn't.

So basically what you'd want to do is read the json, normalize it so that the whitespace is good, and then treat it as yaml.

I also think I remember reading something from _why a while back, you might try checking out his site.

Pat

Doesn't look like it... If I grep from_xml in my gems dir, I find a bunch of results, including the modification to Hash that I'm pretty sure rails uses to convert incoming xml into a params hash. But grepping for "from_json" doesn't return anything. Not very scientific, I know, but...

Unfortunately you'll probably have to modify rails to allow submitting json. It would probably be less work to write the javascript to build the xml... or better yet, just create the param names in the nested-hash notation that rails would expect (like "account[name]=foo").

Or maybe some rails guru out there knows something I don't....

b

eallik@gmail.com wrote:

If you are building a RESTful application why are you pushing JSON to the server? If this is truly RESTful then you should be simply posting/putting data to your already available REST controller actions.

Same thing with XML. This is not SOAP. You don't have to send XML in both directions. Just use REST like it is intended to be used and you're good to go.

I’m having trouble following this response. If you’re building a RESTful application you have to be able to send and receive representations of your response, whether that’s in XML, JSON, HTML, or any other format. It seems like the original poster was just asking how best to understand input sent as JSON?

It’s actually quite easy to get rails to accept JSON as an input format, provided the data structures in the JSON match what the rails app expects. I wrote a few blog entries which cover that:

http://jystewart.net/process/2007/02/input-formats-and-content-types-in-rails-12/

http://jystewart.net/process/2007/03/improvements-to-rails-json-support/

and a piece over at InfoQ that talks about how to build RESTful apps that speak XML, JSON and microformatted-HTML:

http://www.infoq.com/articles/rails-rest-and-microformats

James.

I am in the process of writing a REST based interface between my RoR application and a Cocoa desktop application. While I receive data in XML I send data directly to the Rails application using a simple POST/ PUT with data encoded into the HTTP message body as standard form data.

Sorry if my previous post was unclear. I just don't see the need to encode the data as XML or JSON for sending. But maybe this is completely different for AJAX, but I don't see why it would be. I've not done much AJAX yet, but hope to in the future.