Forum: Ruby on Rails Rails, REST and JSON

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E49c2e77533477d3c7107f029d9d4b09?d=identicon&s=25 Erik Allik (Guest)
on 2007-03-28 23:00
(Received via mailing list)
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
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2007-03-28 23:09
(Received via mailing list)
On 3/28/07, Erik Allik <eallik@gmail.com> wrote:
> 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
E49c2e77533477d3c7107f029d9d4b09?d=identicon&s=25 eallik@gmail.com (Guest)
on 2007-03-29 00:21
(Received via mailing list)
Hey pat,

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

Erik
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2007-03-29 00:56
(Received via mailing list)
On 3/28/07, eallik@gmail.com <eallik@gmail.com> wrote:
>
> 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
8310c5a7c769345114597bcdef111488?d=identicon&s=25 Ben Munat (Guest)
on 2007-03-29 01:25
(Received via mailing list)
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
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-03-30 00:13
(Received via mailing list)
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.
4715ae8a9ac5152600ccd3012267ab6b?d=identicon&s=25 James Stewart (Guest)
on 2007-03-30 01:05
(Received via mailing list)
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...
in-rails-12/
http://jystewart.net/process/2007/03/improvements-...

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/
21f7ed21f11a809050594c82eab11d67?d=identicon&s=25 Robert Walker (Guest)
on 2007-03-30 15:27
(Received via mailing list)
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.
This topic is locked and can not be replied to.