diff options
Diffstat (limited to 'activesupport/lib/active_support/json/encoders/hash.rb')
-rw-r--r-- | activesupport/lib/active_support/json/encoders/hash.rb | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/activesupport/lib/active_support/json/encoders/hash.rb b/activesupport/lib/active_support/json/encoders/hash.rb index 9a9f074847..14b91a76a1 100644 --- a/activesupport/lib/active_support/json/encoders/hash.rb +++ b/activesupport/lib/active_support/json/encoders/hash.rb @@ -1,8 +1,16 @@ class Hash - def to_json #:nodoc: + 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]) + end + returning result = '{' do - result << map do |key, value| - "#{ActiveSupport::JSON.encode(key)}: #{ActiveSupport::JSON.encode(value)}" + result << hash_keys.map do |key| + "#{ActiveSupport::JSON.encode(key)}: #{ActiveSupport::JSON.encode(self[key], options)}" end * ', ' result << '}' end |