This project is archived and is in readonly mode.

#5867 open
stonefield

ActiveResource is not parsing JSON correct

Reported by stonefield | October 26th, 2010 @ 09:01 AM

It seems that ActiveResource is encapsulating the received object, adding a parent. This makes the received object having no known attributes.
I assume the problem is in the client and not the server. Simple examples from the console (as a client):

self.format = :xml

ruby-1.9.2-p0 > Link.first
=> #<Link:0x00000101c3d4d8 @attributes={"created_at"=>2010-10-25 14:49:16 UTC, "id"=>1, "name"=>"The link", "number"=>1, "updated_at"=>2010-10-25 14:49:16 UTC}, @prefix_options={}>

self.format = :json

ruby-1.9.2-p0 > Link.first
=> #<Link:0x00000100c67680 @attributes={"link"=>#<Link:0x00000100c66cf8 @attributes={"created_at"=>"2010-10-25T14:49:16Z", "id"=>1, "name"=>"The link", "number"=>1, "updated_at"=>"2010-10-25T14:49:16Z"}, @prefix_options={}>}, @prefix_options={}>

Simple examples from the browser (from the server):
http://localhost:3000/links/1.xml

<link>
  <created-at type="datetime">2010-10-25T14:49:16Z</created-at>
  <id type="integer">1</id>
  <name>The link</name>
  <number type="integer">1</number>
  <updated-at type="datetime">2010-10-25T14:49:16Z</updated-at>
</link>

http://localhost:3000/links/1.json

{"link":{"created_at":"2010-10-25T14:49:16Z","id":1,"name":"The link","number":1,"updated_at":"2010-10-25T14:49:16Z"}}

Comments and changes to this ticket

  • Denis Odorcic

    Denis Odorcic October 28th, 2010 @ 04:09 AM

    • Tag changed from activeresource, json to activeresource, json, patch, verified

    Verified that ActiveResource JSON is not working at all. Resource.include_root_in_json was defaulted to true in #3770 but wasn't handled when decoding in ActiveResource. I've attached is a patch which does so.

    I also had to fix the tests in format_test.rb as they were incorrectly encoding objects for JSON, and relying on a specific way that XML decodes its objects to get the tests to pass. Feedback on what I did with the tests would be good.

  • stonefield

    stonefield October 28th, 2010 @ 11:04 AM

    Thanks Denis,

    I applied the patch, and it seems to work correctly.
    I also reviewed the tests and I think they look good, but I might not be the right person to verify those.

  • Rohit Arondekar

    Rohit Arondekar October 28th, 2010 @ 11:12 AM

    • State changed from “new” to “open”
    • Assigned user set to “José Valim”
    • Importance changed from “” to “Low”
  • Jeff Kreeftmeijer

    Jeff Kreeftmeijer November 1st, 2010 @ 05:05 PM

    • Tag cleared.

    Automatic cleanup of spam.

  • Denis Odorcic

    Denis Odorcic November 24th, 2010 @ 09:51 PM

    • Tag set to activeresource, json

    No love for ActiveResource and JSON eh? :( Ran into this again today at work which reminded me about the ticket.

  • rails

    rails February 26th, 2011 @ 12:00 AM

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • rails

    rails February 26th, 2011 @ 12:00 AM

    • State changed from “open” to “stale”
  • stonefield

    stonefield February 27th, 2011 @ 04:47 AM

    Add the patch, please.
    Tests viewed and seems good.
    Verified that the patch is working.
    Have system in production with this code.

  • Denis Odorcic

    Denis Odorcic February 28th, 2011 @ 07:01 PM

    • State changed from “stale” to “open”

    I guess this should still be in [state:open] then since there hasn't been any change to ActiveResource JSON decoding.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages