Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

to_xml method not available in all objects as opposed to to_json

#4548
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.

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

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Dan Pickett
    Dan Pickett
    • Tag changed from rails3 to bugmash, rails3

    May 9th, 2010 @ 06:45 PM

  2. Rohit Arondekar
    Rohit Arondekar

    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?

    June 18th, 2010 @ 12:58 PM

  3. Neeraj Singh
    Neeraj Singh

    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.

    June 18th, 2010 @ 01:03 PM

  4. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to invalid

    June 18th, 2010 @ 02:33 PM