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

Changeset 7736

Show
Ignore:
Timestamp:
10/04/07 03:28:42 (10 months ago)
Author:
bitsweat
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/serializers/json_serializer.rb

    r7519 r7736  
    11module ActiveRecord #:nodoc: 
    22  module Serialization 
    3     def to_json(options = {}, &block
     3    def to_json(options = {}
    44      JsonSerializer.new(self, options).to_s 
    55    end 
  • trunk/activerecord/test/json_serialization_test.rb

    r7697 r7736  
    6969  def setup 
    7070    @david = authors(:david) 
     71    @mary = authors(:mary) 
    7172  end 
    7273 
    7374  def test_includes_uses_association_name 
    7475    json = @david.to_json(:include => :posts) 
    75      
     76 
    7677    assert_match %r{"posts": \[}, json 
    7778 
     
    141142    assert_equal %r{"favorite_quote": }.match(json).size, 1 
    142143  end 
     144 
     145  def test_should_allow_only_option_for_list_of_authors 
     146    authors = [@david, @mary] 
     147 
     148    assert_equal %([{"name": "David"}, {"name": "Mary"}]), authors.to_json(:only => :name) 
     149  end 
     150 
     151  def test_should_allow_except_option_for_list_of_authors 
     152    authors = [@david, @mary] 
     153 
     154    assert_equal %([{"id": 1}, {"id": 2}]), authors.to_json(:except => [:name, :author_address_id]) 
     155  end 
     156 
     157  def test_should_allow_includes_for_list_of_authors 
     158    authors = [@david, @mary] 
     159    json = authors.to_json( 
     160      :only => :name, 
     161      :include => { 
     162        :posts => { :only => :id } 
     163      } 
     164    ) 
     165 
     166    assert_equal %([{"name": "David", "posts": [{"id": 1}, {"id": 2}, {"id": 4}, {"id": 5}, {"id": 6}]}, {"name": "Mary", "posts": [{"id": 7}]}]), json 
     167  end 
     168 
     169  def test_should_allow_options_for_hash_of_authors 
     170    authors_hash = { 
     171      1 => @david, 
     172      2 => @mary 
     173    } 
     174 
     175    assert_equal %({1: {"name": "David"}}), authors_hash.to_json(:only => [1, :name]) 
     176  end 
    143177end 
  • trunk/activesupport/CHANGELOG

    r7732 r7736  
    11*SVN* 
    22 
     3* 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.  #9751 [Chu Yeow] 
     4 
    35* BufferedLogger#auto_flushing = N flushes the log every N messages. Buffers with an array instead of string.  [Jeremy Kemper] 
    46 
    5 * Fixed Date#xmlschema for dates outside the range of what can be created with Time #9744 [gbuesing] 
     7* Fixed Date#xmlschema for dates outside the range of what can be created with Time #9744 [Geoff Buesing] 
    68 
    79 
  • trunk/activesupport/lib/active_support/json/encoders/date_time.rb

    r6773 r7736  
    11class DateTime 
    2   def to_json #:nodoc: 
     2  def to_json(options = nil) #:nodoc: 
    33    %("#{strftime("%m/%d/%Y %H:%M:%S %Z")}") 
    44  end 
  • trunk/activesupport/lib/active_support/json/encoders/date.rb

    r6773 r7736  
    11class Date 
    2   def to_json #:nodoc: 
     2  def to_json(options = nil) #:nodoc: 
    33    %("#{strftime("%m/%d/%Y")}") 
    44  end 
  • trunk/activesupport/lib/active_support/json/encoders/enumerable.rb

    r6443 r7736  
    11module Enumerable 
    2   def to_json #:nodoc: 
    3     "[#{map { |value| ActiveSupport::JSON.encode(value) } * ', '}]" 
     2  def to_json(options = {}) #:nodoc: 
     3    "[#{map { |value| ActiveSupport::JSON.encode(value, options) } * ', '}]" 
    44  end 
    55end 
  • trunk/activesupport/lib/active_support/json/encoders/false_class.rb

    r6443 r7736  
    11class FalseClass 
    2   def to_json #:nodoc: 
     2  def to_json(options = nil) #:nodoc: 
    33    'false' 
    44  end 
  • trunk/activesupport/lib/active_support/json/encoders/hash.rb

    r7697 r7736  
    11class Hash 
    2   def to_json #:nodoc: 
     2  def to_json(options = {}) #:nodoc: 
     3    hash_keys = self.keys 
     4 
     5    if options[:except] 
     6      hash_keys = hash_keys - Array(options[:except]) 
     7    elsif options[:only] 
     8      hash_keys = hash_keys & Array(options[:only]) 
     9    end 
     10 
    311    returning result = '{' do 
    4       result << map do |key, value
    5         "#{ActiveSupport::JSON.encode(key)}: #{ActiveSupport::JSON.encode(value)}" 
     12      result << hash_keys.map do |key
     13        "#{ActiveSupport::JSON.encode(key)}: #{ActiveSupport::JSON.encode(self[key], options)}" 
    614      end * ', ' 
    715      result << '}' 
  • trunk/activesupport/lib/active_support/json/encoders/nil_class.rb

    r6443 r7736  
    11class NilClass 
    2   def to_json #:nodoc: 
     2  def to_json(options = nil) #:nodoc: 
    33    'null' 
    44  end 
  • trunk/activesupport/lib/active_support/json/encoders/numeric.rb

    r6443 r7736  
    11class Numeric 
    2   def to_json #:nodoc: 
     2  def to_json(options = nil) #:nodoc: 
    33    to_s 
    44  end 
  • trunk/activesupport/lib/active_support/json/encoders/object.rb

    r6443 r7736  
    11class Object 
    22  # Dumps object in JSON (JavaScript Object Notation).  See www.json.org for more info. 
    3   # 
    4   #   Account.find(1).to_json 
    5   #   => "{attributes: {username: \"foo\", id: \"1\", password: \"bar\"}}" 
    6   # 
    7   def to_json 
    8     ActiveSupport::JSON.encode(instance_values) 
     3  def to_json(options = {}) 
     4    ActiveSupport::JSON.encode(instance_values, options) 
    95  end 
    106end 
  • trunk/activesupport/lib/active_support/json/encoders/regexp.rb

    r6443 r7736  
    11class Regexp 
    2   def to_json #:nodoc: 
     2  def to_json(options = nil) #:nodoc: 
    33    inspect 
    44  end 
  • trunk/activesupport/lib/active_support/json/encoders/string.rb

    r6893 r7736  
    1818 
    1919class String 
    20   def to_json #:nodoc: 
     20  def to_json(options = nil) #:nodoc: 
    2121    '"' + gsub(/[\010\f\n\r\t"\\><]/) { |s| 
    2222      ActiveSupport::JSON::Encoding::ESCAPED_CHARS[s] 
  • trunk/activesupport/lib/active_support/json/encoders/symbol.rb

    r6443 r7736  
    11class Symbol 
    2   def to_json #:nodoc: 
    3     ActiveSupport::JSON.encode(to_s
     2  def to_json(options = {}) #:nodoc: 
     3    ActiveSupport::JSON.encode(to_s, options
    44  end 
    55end 
  • trunk/activesupport/lib/active_support/json/encoders/time.rb

    r6773 r7736  
    11class Time 
    2   def to_json #:nodoc: 
     2  def to_json(options = nil) #:nodoc: 
    33    %("#{strftime("%m/%d/%Y %H:%M:%S %Z")}") 
    44  end 
  • trunk/activesupport/lib/active_support/json/encoders/true_class.rb

    r6443 r7736  
    11class TrueClass 
    2   def to_json #:nodoc: 
     2  def to_json(options = nil) #:nodoc: 
    33    'true' 
    44  end 
  • trunk/activesupport/lib/active_support/json/encoding.rb

    r7697 r7736  
    1818 
    1919      # Converts a Ruby object into a JSON string. 
    20       def encode(value
     20      def encode(value, options = {}
    2121        raise_on_circular_reference(value) do 
    22           value.send(:to_json
     22          value.send(:to_json, options
    2323        end 
    2424      end 
  • trunk/activesupport/test/json/encoding_test.rb

    r7697 r7736  
    7070  end 
    7171 
     72  def test_hash_should_allow_key_filtering_with_only 
     73    assert_equal %({"a": 1}), { 'a' => 1, :b => 2, :c => 3 }.to_json(:only => 'a') 
     74  end 
     75 
     76  def test_hash_should_allow_key_filtering_with_except 
     77    assert_equal %({"b": 2}), { 'foo' => 'bar', :b => 2, :c => 3 }.to_json(:except => ['foo', :c]) 
     78  end 
     79 
    7280  protected 
    7381    def object_keys(json_object) 
     
    7583    end 
    7684end 
     85 
     86uses_mocha 'JsonOptionsTests' do 
     87  class JsonOptionsTests < Test::Unit::TestCase 
     88    def test_enumerable_should_passthrough_options_to_elements 
     89      json_options = { :include => :posts } 
     90      ActiveSupport::JSON.expects(:encode).with(1, json_options) 
     91      ActiveSupport::JSON.expects(:encode).with(2, json_options) 
     92      ActiveSupport::JSON.expects(:encode).with('foo', json_options) 
     93 
     94      [1, 2, 'foo'].to_json(json_options) 
     95    end 
     96  end 
     97end