This project is archived and is in readonly mode.

#1895 new
Damien MATHIEU

Export OrderedHash to XML and JSON

Reported by Damien MATHIEU | February 6th, 2009 @ 01:51 PM | in 3.x

When exporting OrderedHash objects with to_json or to_xml, they become arrays and doesn't stay as hashes. With those methods, they're exported as if they were hashes.

Comments and changes to this ticket

  • Damien MATHIEU

    Damien MATHIEU February 7th, 2009 @ 10:35 PM

    Did I use the inappropriate method to suggest a patch ?

  • Damien MATHIEU

    Damien MATHIEU February 10th, 2009 @ 08:40 AM

    So I guess not. Whatever. Thanks anyway for not even taking a few seconds to look at my patch.

  • Eloy Duran

    Eloy Duran February 10th, 2009 @ 09:40 AM

    How about doing something like the following?

    
    def to_json
      to_hash.to_json
    end
    
    def to_xml
      to_hash.to_xml
    end
    

    This way we won't need to maintain the code for cerating JSON and XML in multiple places.

  • Eloy Duran

    Eloy Duran February 10th, 2009 @ 09:43 AM

    Oh btw, that attitude is not very nice. I would have not replied if I had seen your last comment.

    People are busy with work and busy creating/fixing code that you can use, for free. It's also stated very clear in the explanation in the right hand bar. But I guess you didn't read that.

    “Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too"..”

  • Damien MATHIEU

    Damien MATHIEU February 10th, 2009 @ 09:43 AM

    Because if we move from an OrderedHash to an hash, we're losing the order in 1.8 ?

  • Damien MATHIEU

    Damien MATHIEU February 10th, 2009 @ 09:45 AM

    And sorry for the harsh message. I must admit that it's quite frustrating to see other tickets being assigned or at least having someone show an interest to it and feel alone with his ticket not being replied ;)

  • Eloy Duran

    Eloy Duran February 10th, 2009 @ 09:51 AM

    Because if we move from an OrderedHash to an hash, we're losing the order in 1.8 ?

    Yes that's true, but is it important for JSON and XML? (I don't know)

    And sorry for the harsh message. I must admit that it's quite frustrating to see other tickets being assigned or at least having someone show an interest to it and feel alone with his ticket not being replied ;)

    I understand, but such emotional messages never help. But enough about this.

  • Damien MATHIEU

    Damien MATHIEU February 10th, 2009 @ 09:53 AM

    Well in my case it was. I have a date for every key of the hash (in the format "YYYY-MM-DD) and I need it to be ordered by date, even when I give it in json or xml (otherwise the application using the feed need to reorder it).

  • Eloy Duran

    Eloy Duran February 10th, 2009 @ 10:05 AM

    At least in JSON the spec (json.org) says clearly:

    “An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).”

    From googling it seems that in XML the same applies, but I haven't found a definitive answer yet so you'd have to look this up more thoroughly.

  • Eloy Duran
  • Nate Wiger

    Nate Wiger February 10th, 2009 @ 01:03 PM

    In XML, order may or may not matter. It's application-dependent.

    My opinion: writing your app to rely on Rails to spit out XML/JSON automatically in some order (ASCII order?) is a bad idea. It's not actually part of either the JSON or XML specs to do so. It's a fragile approach.

    I'm not against this patch, but kazhar, I would strongly suggest a different approach for your app. It is quite likely, since this is NOT a formally-defined behavior for JSON/XML, that sometime in the future a subsequent patch will get applied which will revert this behavior.

    Just write a .rxml and spit it out ordered by date. Or create an ActiveRecord::Base subclass with "abstract_class = true" that overrides the to_xml/to_json methods. Use the Ruby sort method and spit out what you need.

  • Eloy Duran

    Eloy Duran February 10th, 2009 @ 01:31 PM

    Also, I just remembered OrderedHash is a subclass of Hash nowadays, so this probably already works as it should in 2.3. (That is, dumping as an unordered hash).

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • bingbing

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>

People watching this ticket

Attachments

Pages