aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/json/encoders/hash.rb
blob: 14b91a76a1bf41e81089e00b925f56d09555e12c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Hash
  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 << hash_keys.map do |key|
        "#{ActiveSupport::JSON.encode(key)}: #{ActiveSupport::JSON.encode(self[key], options)}"
      end * ', '
      result << '}'
    end
  end
end