Method: Hash#as_json

Defined in:
activesupport/lib/active_support/core_ext/object/json.rb

#as_json(options = nil) ⇒ Object

:nodoc:



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'activesupport/lib/active_support/core_ext/object/json.rb', line 175

def as_json(options = nil) # :nodoc:
  # create a subset of the hash by applying :only or :except
  subset = if options
    if attrs = options[:only]
      slice(*Array(attrs))
    elsif attrs = options[:except]
      except(*Array(attrs))
    else
      self
    end
  else
    self
  end

  result = {}
  if options
    options = options.dup.freeze unless options.frozen?
    subset.each { |k, v| result[k.to_s] = v.as_json(options) }
  else
    subset.each { |k, v| result[k.to_s] = v.as_json }
  end
  result
end