lib/active_support/json/encoders/hash.rb in activesupport-2.2.3 vs lib/active_support/json/encoders/hash.rb in activesupport-2.3.2

- old
+ new

@@ -3,11 +3,11 @@ # # Without any +options+, the returned JSON string will include all # the hash keys. For example: # # { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json - # # => {"name": "Konata Izumi", 1: 2, "age": 16} + # # => {"name": "Konata Izumi", "1": 2, "age": 16} # # The keys in the JSON string are unordered due to the nature of hashes. # # The <tt>:only</tt> and <tt>:except</tt> options can be used to limit the # attributes included, and will accept 1 or more hash keys to include/exclude. @@ -29,19 +29,18 @@ # allowing the posts association in the User model to be converted to JSON # as well. def to_json(options = {}) #:nodoc: hash_keys = self.keys - if options[:except] - hash_keys = hash_keys - Array(options[:except]) - elsif options[:only] - hash_keys = hash_keys & Array(options[:only]) + if except = options[:except] + hash_keys = hash_keys - Array.wrap(except) + elsif only = options[:only] + hash_keys = hash_keys & Array.wrap(only) end - returning result = '{' do - result << hash_keys.map do |key| - "#{ActiveSupport::JSON.encode(key)}: #{ActiveSupport::JSON.encode(self[key], options)}" - end * ', ' - result << '}' - end + result = '{' + result << hash_keys.map do |key| + "#{ActiveSupport::JSON.encode(key.to_s)}: #{ActiveSupport::JSON.encode(self[key], options)}" + end * ', ' + result << '}' end end