Changeset 7736
- Timestamp:
- 10/04/07 03:28:42 (10 months ago)
- Files:
-
- trunk/activerecord/lib/active_record/serializers/json_serializer.rb (modified) (1 diff)
- trunk/activerecord/test/json_serialization_test.rb (modified) (2 diffs)
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/json/encoders/date_time.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/json/encoders/date.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/json/encoders/enumerable.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/json/encoders/false_class.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/json/encoders/hash.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/json/encoders/nil_class.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/json/encoders/numeric.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/json/encoders/object.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/json/encoders/regexp.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/json/encoders/string.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/json/encoders/symbol.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/json/encoders/time.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/json/encoders/true_class.rb (modified) (1 diff)
- trunk/activesupport/lib/active_support/json/encoding.rb (modified) (1 diff)
- trunk/activesupport/test/json/encoding_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/lib/active_record/serializers/json_serializer.rb
r7519 r7736 1 1 module ActiveRecord #:nodoc: 2 2 module Serialization 3 def to_json(options = {} , &block)3 def to_json(options = {}) 4 4 JsonSerializer.new(self, options).to_s 5 5 end trunk/activerecord/test/json_serialization_test.rb
r7697 r7736 69 69 def setup 70 70 @david = authors(:david) 71 @mary = authors(:mary) 71 72 end 72 73 73 74 def test_includes_uses_association_name 74 75 json = @david.to_json(:include => :posts) 75 76 76 77 assert_match %r{"posts": \[}, json 77 78 … … 141 142 assert_equal %r{"favorite_quote": }.match(json).size, 1 142 143 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 143 177 end trunk/activesupport/CHANGELOG
r7732 r7736 1 1 *SVN* 2 2 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 3 5 * BufferedLogger#auto_flushing = N flushes the log every N messages. Buffers with an array instead of string. [Jeremy Kemper] 4 6 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] 6 8 7 9 trunk/activesupport/lib/active_support/json/encoders/date_time.rb
r6773 r7736 1 1 class DateTime 2 def to_json #:nodoc:2 def to_json(options = nil) #:nodoc: 3 3 %("#{strftime("%m/%d/%Y %H:%M:%S %Z")}") 4 4 end trunk/activesupport/lib/active_support/json/encoders/date.rb
r6773 r7736 1 1 class Date 2 def to_json #:nodoc:2 def to_json(options = nil) #:nodoc: 3 3 %("#{strftime("%m/%d/%Y")}") 4 4 end trunk/activesupport/lib/active_support/json/encoders/enumerable.rb
r6443 r7736 1 1 module 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) } * ', '}]" 4 4 end 5 5 end trunk/activesupport/lib/active_support/json/encoders/false_class.rb
r6443 r7736 1 1 class FalseClass 2 def to_json #:nodoc:2 def to_json(options = nil) #:nodoc: 3 3 'false' 4 4 end trunk/activesupport/lib/active_support/json/encoders/hash.rb
r7697 r7736 1 1 class 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 3 11 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)}" 6 14 end * ', ' 7 15 result << '}' trunk/activesupport/lib/active_support/json/encoders/nil_class.rb
r6443 r7736 1 1 class NilClass 2 def to_json #:nodoc:2 def to_json(options = nil) #:nodoc: 3 3 'null' 4 4 end trunk/activesupport/lib/active_support/json/encoders/numeric.rb
r6443 r7736 1 1 class Numeric 2 def to_json #:nodoc:2 def to_json(options = nil) #:nodoc: 3 3 to_s 4 4 end trunk/activesupport/lib/active_support/json/encoders/object.rb
r6443 r7736 1 1 class Object 2 2 # 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) 9 5 end 10 6 end trunk/activesupport/lib/active_support/json/encoders/regexp.rb
r6443 r7736 1 1 class Regexp 2 def to_json #:nodoc:2 def to_json(options = nil) #:nodoc: 3 3 inspect 4 4 end trunk/activesupport/lib/active_support/json/encoders/string.rb
r6893 r7736 18 18 19 19 class String 20 def to_json #:nodoc:20 def to_json(options = nil) #:nodoc: 21 21 '"' + gsub(/[\010\f\n\r\t"\\><]/) { |s| 22 22 ActiveSupport::JSON::Encoding::ESCAPED_CHARS[s] trunk/activesupport/lib/active_support/json/encoders/symbol.rb
r6443 r7736 1 1 class 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) 4 4 end 5 5 end trunk/activesupport/lib/active_support/json/encoders/time.rb
r6773 r7736 1 1 class Time 2 def to_json #:nodoc:2 def to_json(options = nil) #:nodoc: 3 3 %("#{strftime("%m/%d/%Y %H:%M:%S %Z")}") 4 4 end trunk/activesupport/lib/active_support/json/encoders/true_class.rb
r6443 r7736 1 1 class TrueClass 2 def to_json #:nodoc:2 def to_json(options = nil) #:nodoc: 3 3 'true' 4 4 end trunk/activesupport/lib/active_support/json/encoding.rb
r7697 r7736 18 18 19 19 # Converts a Ruby object into a JSON string. 20 def encode(value )20 def encode(value, options = {}) 21 21 raise_on_circular_reference(value) do 22 value.send(:to_json )22 value.send(:to_json, options) 23 23 end 24 24 end trunk/activesupport/test/json/encoding_test.rb
r7697 r7736 70 70 end 71 71 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 72 80 protected 73 81 def object_keys(json_object) … … 75 83 end 76 84 end 85 86 uses_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 97 end