This project is archived and is in readonly mode.

#4548 ✓invalid
Neeraj Singh

to_xml method not available in all objects as opposed to to_json

Reported by Neeraj Singh | May 7th, 2010 @ 10:56 AM

class Me; def foo;end;end
puts Me.to_json # works
puts Me.to_xml # undefined method `to_xml'

Is there a particular reason why to_xml is not present in all objects.

In my view to_json and to_xml , the two mirror image methods, should be as close in behavior as possible.

I am willing to work on a patch. However it would be nice to know before hand if this behavior is not already baked in for a particular reason.

Comments and changes to this ticket

  • Dan Pickett

    Dan Pickett May 9th, 2010 @ 06:45 PM

    • Tag changed from rails3 to bugmash, rails3
  • Rohit Arondekar

    Rohit Arondekar June 18th, 2010 @ 12:58 PM

    Neeraj, your example doesn't work anymore. Me.to_json also gives undefined method. I'm guessing something has changed recently because of the conflict with json_pure gem.

    Also why are you calling to_json or to_xml on a class?

    I'm guessing it was done like that because XML was meant to be a data interchange format while JSON was meant to be for serializing objects.

    The way to_xml works is, it calls the .to_xml method of each item in the collection and groups them together. This reflects xml's data interchange purpose.

    Whereas any object can be serialized into JSON.

    You should discuss this in #rails-contrib to get a better answer maybe. Or do you want me to close this ticket?

  • Neeraj Singh

    Neeraj Singh June 18th, 2010 @ 01:03 PM

    I was just pointing out inconsistency between to_json and to_xml. As you mentioned to_json is also reporting the same problem now. So this ticket can be closed.

    Thanks for the follow up.

  • Rohit Arondekar

    Rohit Arondekar June 18th, 2010 @ 02:33 PM

    • State changed from “new” to “invalid”

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