Method: JSON#dump

Defined in:
lib/extensions/json/json/common.rb,
lib/framework/autocomplete/JSON.rb

#dump(obj, anIO, limit) ⇒ Object

Dumps obj as a JSON string, i.e. calls generate on the object and returns the result.

If anIO (an IO like object or an object that responds to the write method) was given, the resulting JSON is written to it.

If the number of nested arrays or objects exceeds limit an ArgumentError exception is raised. This argument is similar (but not exactly the same!) to the limit argument in Marshal.dump.

This method is part of the implementation of the load/dump interface of Marshal and YAML.



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/extensions/json/json/common.rb', line 295

def dump(obj, anIO = nil, limit = nil)
  if anIO and limit.nil?
    anIO = anIO.to_io if anIO.respond_to?(:to_io)
    unless anIO.respond_to?(:write)
      limit = anIO
      anIO = nil
    end
  end
  limit ||= 0
  result = generate(obj, :allow_nan => true, :max_nesting => limit)
  if anIO
    anIO.write result
    anIO
  else
    result
  end
rescue JSON::NestingError
  raise ArgumentError, "exceed depth limit"
end