@@ -613,26 +613,42 @@ class << self
613
613
# Output:
614
614
# {"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}
615
615
def dump ( obj , anIO = nil , limit = nil , kwargs = nil )
616
- io_limit_opt = [ anIO , limit , kwargs ] . compact
617
- kwargs = io_limit_opt . pop if io_limit_opt . last . is_a? ( Hash )
618
- anIO , limit = io_limit_opt
619
- if anIO . respond_to? ( :to_io )
620
- anIO = anIO . to_io
621
- elsif limit . nil? && !anIO . respond_to? ( :write )
622
- anIO , limit = nil , anIO
616
+ if kwargs . nil?
617
+ if limit . nil?
618
+ if anIO . is_a? ( Hash )
619
+ kwargs = anIO
620
+ anIO = nil
621
+ end
622
+ elsif limit . is_a? ( Hash )
623
+ kwargs = limit
624
+ limit = nil
625
+ end
623
626
end
627
+
628
+ unless anIO . nil?
629
+ if anIO . respond_to? ( :to_io )
630
+ anIO = anIO . to_io
631
+ elsif limit . nil? && !anIO . respond_to? ( :write )
632
+ anIO , limit = nil , anIO
633
+ end
634
+ end
635
+
624
636
opts = JSON . dump_default_options
625
637
opts = opts . merge ( :max_nesting => limit ) if limit
626
638
opts = merge_dump_options ( opts , **kwargs ) if kwargs
627
- result = generate ( obj , opts )
628
- if anIO
639
+
640
+ result = begin
641
+ generate ( obj , opts )
642
+ rescue JSON ::NestingError
643
+ raise ArgumentError , "exceed depth limit"
644
+ end
645
+
646
+ if anIO . nil?
647
+ result
648
+ else
629
649
anIO . write result
630
650
anIO
631
- else
632
- result
633
651
end
634
- rescue JSON ::NestingError
635
- raise ArgumentError , "exceed depth limit"
636
652
end
637
653
638
654
# Encodes string using String.encode.
0 commit comments