#631 ✓resolved
steve ross

[FIXED] DataMapper::Collection Should Serialize to JSON

Reported by steve ross | November 2nd, 2008 @ 12:20 PM

DataMapper 0.9.6 can serialize attributes to JSON, e.g.:

Post.first.attributes.to_json

but not collections, e.g.:

Post.all.to_json

This is a monkeypatch proof of concept:

class DataMapper::Collection def to_collected_json

['[', self.collect{|o| o.attributes.to_json}.join(', '), ']'].join

end end

It makes returning arrays of JSON for Ajax calls much simpler and you probably already thought of it but I just couldn't find it :)

Comments and changes to this ticket

  • steve ross

    steve ross November 2nd, 2008 @ 12:22 PM

    Gobbled up my code. Maybe I got it right this time.

    @@@ruby class DataMapper::Collection def to_collected_json

    ['[', self.collect{|o| o.attributes.to_json}.join(', '), ']'].join
    
    

    end end

    
    
  • steve ross

    steve ross November 2nd, 2008 @ 12:23 PM

    One more try. Otherwise, see: http://pastie.org/305933.

    >
    
    class DataMapper::Collection
      def to_collected_json
        ['[', self.collect{|o| o.attributes.to_json}.join(', '), ']'].join
      end
    end
    
  • Xavier Shay

    Xavier Shay November 2nd, 2008 @ 03:37 PM

    • Tag changed from collection, json, serialization to collection, dm-serializer, json, serialization

    A better way at the moment is Post.all.to_a.to_json

    That way it keeps the to_json options interface (you can specify :methods, :only, etc...)

  • steve ross

    steve ross November 2nd, 2008 @ 05:46 PM

    I must be doing something wrong.

    
    >> Post.all.to_a.to_json
     ~ SELECT "id", "url", "title" FROM "posts" ORDER BY "id", "title"
    => "[\"#<Post:0x286f4dc>\"]"
    
  • Xavier Shay

    Xavier Shay November 2nd, 2008 @ 05:56 PM

    
    >> Food.all(:limit => 2).to_a.to_json
    => "[{ "food_id": "01A10026", "name": "Beer, Ale", "description": "Alcoholic beverage made by brewing and fermenting " },{ "food_id": "01A10027", "name": "Beer, Bitter", "description": "Alcoholic beverage made by brewing and fermenting " }]"
    
    
    $ gem list | grep dm-serializer
    dm-serializer (0.9.6, 0.9.2)
    

    (though it's possible that isn't the actual 0.9.6 - I have a bit of a frakenstein setup at the moment because I've been hacking dm-serializer)

    as an aside - this only really works for JSON at the moment.

  • steve ross

    steve ross November 2nd, 2008 @ 08:56 PM

    Would merb-datamapper (0.9.9) be getting in the way of the behavior you're showing? I have the same dm-serializer gem installed: 0.9.6.

  • Xavier Shay

    Xavier Shay November 2nd, 2008 @ 09:08 PM

    I wouldn't have thought so

    
    $ gem list | grep merb_datamapper
    merb_datamapper (0.9.12, 0.9.10)
    

    Let me double check what I actually have installed, I'll get back to you. I suspect my hack works with the edge version of dm-serializer.

    Aside from that, we still need to get it working normally though :]

  • Xavier Shay

    Xavier Shay November 10th, 2008 @ 12:52 AM

    I have fixed in my branch:

    http://github.com/xaviershay/dm-...

    in short, the json gem was adding Object#to_json which meant the Proxy that sits infront of Collection was not delegating it.

  • Xavier Shay

    Xavier Shay November 10th, 2008 @ 12:52 AM

    • Title changed from “DataMapper::Collection Should Serialize to JSON” to “[PATCH] DataMapper::Collection Should Serialize to JSON”
    • Tag changed from collection, dm-serializer, json, serialization to collection, dm-serializer, json, patch, serialization
  • Xavier Shay

    Xavier Shay November 21st, 2008 @ 07:49 PM

    • Title changed from “[PATCH] DataMapper::Collection Should Serialize to JSON” to “[FIXED] DataMapper::Collection Should Serialize to JSON”
    • Tag changed from collection, dm-serializer, json, patch, serialization to collection, dm-serializer, json, serialization

    This is now fixed in sam/dm-more, so will be in the 0.9.8 gem

    http://github.com/sam/dm-more/co...

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) November 26th, 2008 @ 12:38 AM

    • State changed from “new” to “resolved”
    • Assigned user cleared.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

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

People watching this ticket

Pages