@@ -590,21 +590,33 @@ class << self
590
590
:escape_slash => false ,
591
591
}
592
592
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.
595
599
#
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+.
598
604
#
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
+ # ---
602
606
#
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\"}"
605
611
#
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"}
608
620
def dump ( obj , anIO = nil , limit = nil )
609
621
if anIO and limit . nil?
610
622
anIO = anIO . to_io if anIO . respond_to? ( :to_io )
0 commit comments