Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8920 (closed enhancement: fixed)

Opened 2 years ago

Last modified 2 years ago

[PATCH] Add options to AR#to_json similar to AR#to_xml

Reported by: chuyeow Assigned to: core
Priority: normal Milestone: 1.x
Component: ActiveRecord Version: edge
Severity: normal Keywords: JSON
Cc:

Description

Adds the :only, :except, :methods, and :include options to AR#to_json. Oh, and AR#to_json won't return you "attributes" as part of the JSON - i.e. no more

{attributes: {id: 1, name: "foo"}

The 'attributes' part is often unnecessary cruft.

You get this instead:

{id: 1, name: "foo"}

Example uses are in the patch (check out the tests). It's time to treat the JSON encoding of AR objects with more respect ;). I'm tired of coding custom to_json methods or using the JSON gem to get any flexibility in respond_to blocks that wants.json.

Essentially you will be able to stuff like:

david.to_json(:include => { :posts => { :only => :title } })

The way it works is by converting AR objects into simple hashes that can be easily encoded as JSON.

Some things I'm not entirely happy with:

  • the name of the AR to Hash converter class: JsonHashifier doesn't clearly express the intent,
  • lack of the ability to remap of names (maybe a little too much to put into core Rails though). E.g. of what I mean by remapping of names:
    david.to_json(:only => :gender, :rename => { :gender => :sex }) => {:sex => "Male"}
    

BTW, I do realize that the JSON here is not strictly valid (keys are not double quoted) - I'm just writing it the way Rails produces JSON by default. To get strictly valid JSON, add

ActiveSupport::JSON.unquote_hash_key_identifiers = false

in your environment.rb (or in the Rails initializers directory if you're on edge).

Attachments

add_options_to_AR_to_json.diff (8.6 kB) - added by chuyeow on 07/10/07 08:49:30.
AR#to_json with options patch (with tests)

Change History

07/09/07 13:28:49 changed by chuyeow

I must add, remapping of attribute/method names can be worked around with proper use of aliases and the options hash to to_json. So ignore that bit I wrote about remapping of names!

07/10/07 08:47:45 changed by chuyeow

Updated patch with test on 2nd-level associations.

07/10/07 08:49:30 changed by chuyeow

  • attachment add_options_to_AR_to_json.diff added.

AR#to_json with options patch (with tests)

07/11/07 05:17:07 changed by chuyeow

08/20/07 12:05:36 changed by jbilbo

I agreed with chuyeow this must be in core, but this plugin is incomplete. To get to_json similar to to_xml is very important to support collections (array of objects), for example:

User.find(:all).to_json(:include => :posts)

This works pretty well with User.find(:all).to_xml(:include => :posts).

Also, it's important to generate by default valid json (http://dev.rubyonrails.org/ticket/8762)

08/20/07 12:13:53 changed by chuyeow

Yup I agree with you jbilbo, working on collections is really important. I've been trying to get that to work but I think I was approaching it the wrong way (I was trying to modify the ActiveSupport JSON encoders to accept options) since it runs into problems with circular references (which is to be expected). When I have more time I'll try and get that change into Jsonifier (the plugin) and then probably update my patch here.

09/27/07 16:29:36 changed by chuyeow

  • status changed from new to closed.
  • resolution set to fixed.

Closing this due to r7519. Please help verify patch #9677 to add :includes option support!