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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 3/28/07, Erik Allik <eallik-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
eallik-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-28 22:08 UTC
Re: Rails, REST and JSON
Hey pat, My question was about JSON input and parsing, not output. Erik On Mar 29, 12:06 am, "Pat Maddox" <perg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 3/28/07, Erik Allik <eal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 3/28/07, eallik-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <eallik-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hey pat, > > My question was about JSON input and parsing, not output. > > ErikOops, 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Hey pat, > > My question was about JSON input and parsing, not output. > > Erik > > On Mar 29, 12:06 am, "Pat Maddox" <perg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> On 3/28/07, Erik Allik <eal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >>> 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 > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Mar 28, 7:23 pm, Ben Munat <bmu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > > eal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > Hey pat, > > > My question was about JSON input and parsing, not output. > > > Erik > > > On Mar 29, 12:06 am, "Pat Maddox" <perg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> On 3/28/07, Erik Allik <eal...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > >>> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 29, 2007, at 6:12 PM, Robert Walker 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. -- James Stewart Work: http://jystewart.net/process/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Mar 29, 7:04 pm, James Stewart <jystew...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mar 29, 2007, at 6:12 PM, Robert Walker 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. > > -- > James Stewart > Work:http://jystewart.net/process/--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---