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

Ticket #9751 (closed enhancement: fixed)

Opened 8 months ago

Last modified 8 months ago

[PATCH] to_json should accept options (allow filtering on Hashes)

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

Description

Currently it's not possible to do something like this in controllers:

@authors = Author.find(:all)

render :json => @authors.to_json(:only => :name)

This is expected, since Enumerable#to_json doesn't accept an options param. It would be really nice to be able to do this (like can be achieved with to_xml.

This patch changes the types in json/encoders so that their to_json methods accepts an optional options argument. Enumerables will pass on the options to its elements. Hashes will respect :only and :except. The primitive types don't actually do anything to the options at this point.

Attachments

to_json_accepts_options.diff (10.2 kB) - added by chuyeow on 10/01/07 18:01:24.
Change to_json method to accept options for JSON-encodable types
to_json_accepts_options.2.diff (10.3 kB) - added by chuyeow on 10/02/07 17:38:05.
Change to_json method to accept options for JSON-encodable types (using hash keys instead of Hash# slice and Hash#except)

Change History

10/01/07 18:01:24 changed by chuyeow

  • attachment to_json_accepts_options.diff added.

Change to_json method to accept options for JSON-encodable types

10/02/07 12:07:10 changed by mislav

  • type changed from defect to enhancement.
  • summary changed from [PATCH] Enumerable#to_json, Hash#to_json should accept options (for rendering collection of AR objects as JSON) to [PATCH] to_json should accept options (allow filtering on Hashes).

+1 from me, I just have a tiny remark:

if options[:only] 
  filtered = slice(*Array(options[:only])) 
else 
  filtered = except(*Array(options[:except])) 
end

Shouldn't it explicitly check for :except presence and delete the options afterwards?

if options[:only]
  filtered = slice(*Array(options.delete(:only))) 
elsif options[:except] 
  filtered = except(*Array(options.delete(:except))) 
end

10/02/07 17:38:05 changed by chuyeow

  • attachment to_json_accepts_options.2.diff added.

Change to_json method to accept options for JSON-encodable types (using hash keys instead of Hash# slice and Hash#except)

10/02/07 17:43:39 changed by chuyeow

Very good point about explicitly checking for the presence of :except. But, I'm deliberately keeping the options around instead of deleting them since I want them to filter down to the hash values (which kinda like how :only and :except apply down the chain to associations that are included using :include in ActiveRecord::Base#to_json).

I've patched in your 1st suggestion, and also changed the Hash#to_json implementation to use Hash#keys instead of relying on the Hash#except and Hash#slice extensions. Plus it's probably really much more efficient this way.

10/04/07 03:28:45 changed by bitsweat

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

(In [7736]) Hash#to_json takes :only or :except options to specific or omit certain hash keys. Enumerable#to_json passes through its options to each element. Closes #9751.