Skip to content

Commit 03f1699

Browse files
Enhanced RDoc for JSON.dump (#443)
* Enhanced RDoc for JSON.dump
1 parent ef00f58 commit 03f1699

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

lib/json/common.rb

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -590,21 +590,33 @@ class << self
590590
:escape_slash => false,
591591
}
592592

593-
# Dumps _obj_ as a JSON string, i.e. calls generate on the object and returns
594-
# the result.
593+
# :call-seq:
594+
# JSON.dump(obj, io = nil, limit = nil)
595+
#
596+
# Dumps +obj+ as a \JSON string, i.e. calls generate on the object and returns the result.
597+
#
598+
# The default options can be changed via method JSON.dump_default_options.
595599
#
596-
# If anIO (an IO-like object or an object that responds to the write method)
597-
# was given, the resulting JSON is written to it.
600+
# - Argument +io+, if given, should respond to method +write+;
601+
# the \JSON \String is written to +io+, and +io+ is returned.
602+
# If +io+ is not given, the \JSON \String is returned.
603+
# - Argument +limit+, if given, is passed to JSON.generate as option +max_nesting+.
598604
#
599-
# If the number of nested arrays or objects exceeds _limit_, an ArgumentError
600-
# exception is raised. This argument is similar (but not exactly the
601-
# same!) to the _limit_ argument in Marshal.dump.
605+
# ---
602606
#
603-
# The default options for the generator can be changed via the
604-
# dump_default_options method.
607+
# When argument +io+ is not given, returns the \JSON \String generated from +obj+:
608+
# obj = {foo: [0, 1], bar: {baz: 2, bat: 3}, bam: :bad}
609+
# json = JSON.dump(obj)
610+
# json # => "{\"foo\":[0,1],\"bar\":{\"baz\":2,\"bat\":3},\"bam\":\"bad\"}"
605611
#
606-
# This method is part of the implementation of the load/dump interface of
607-
# Marshal and YAML.
612+
# When argument +io+ is given, writes the \JSON \String to +io+ and returns +io+:
613+
# path = 't.json'
614+
# File.open(path, 'w') do |file|
615+
# JSON.dump(obj, file)
616+
# end # => #<File:t.json (closed)>
617+
# puts File.read(path)
618+
# Output:
619+
# {"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}
608620
def dump(obj, anIO = nil, limit = nil)
609621
if anIO and limit.nil?
610622
anIO = anIO.to_io if anIO.respond_to?(:to_io)

0 commit comments

Comments
 (0)