Skip to content

Commit f11c570

Browse files
authored
Merge pull request #558 from tompng/json_dump_args
Fix JSON.dump overload combination
2 parents 3a5f2d2 + 41c2712 commit f11c570

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

lib/json/common.rb

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -612,16 +612,13 @@ class << self
612612
# Output:
613613
# {"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}
614614
def dump(obj, anIO = nil, limit = nil, kwargs = nil)
615-
if anIO and limit.nil?
616-
anIO = anIO.to_io if anIO.respond_to?(:to_io)
617-
unless anIO.respond_to?(:write)
618-
if kwargs.nil? and anIO.is_a?(Hash)
619-
kwargs = anIO
620-
else
621-
limit = anIO
622-
end
623-
anIO = nil
624-
end
615+
io_limit_opt = [anIO, limit, kwargs].compact
616+
kwargs = io_limit_opt.pop if io_limit_opt.last.is_a?(Hash)
617+
anIO, limit = io_limit_opt
618+
if anIO.respond_to?(:to_io)
619+
anIO = anIO.to_io
620+
elsif limit.nil? && !anIO.respond_to?(:write)
621+
anIO, limit = nil, anIO
625622
end
626623
opts = JSON.dump_default_options
627624
opts = opts.merge(:max_nesting => limit) if limit
@@ -642,12 +639,14 @@ def self.iconv(to, from, string)
642639
string.encode(to, from)
643640
end
644641

645-
private
646-
647642
def merge_dump_options(opts, strict: NOT_SET)
648643
opts = opts.merge(strict: strict) if NOT_SET != strict
649644
opts
650645
end
646+
647+
class << self
648+
private :merge_dump_options
649+
end
651650
end
652651

653652
module ::Kernel

tests/json_common_interface_test.rb

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,26 @@ def test_load_null
9999

100100
def test_dump
101101
too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
102-
assert_equal too_deep, dump(eval(too_deep))
103-
assert_kind_of String, Marshal.dump(eval(too_deep))
104-
assert_raise(ArgumentError) { dump(eval(too_deep), 100) }
105-
assert_raise(ArgumentError) { Marshal.dump(eval(too_deep), 100) }
106-
assert_equal too_deep, dump(eval(too_deep), 101)
107-
assert_kind_of String, Marshal.dump(eval(too_deep), 101)
108-
output = StringIO.new
109-
dump(eval(too_deep), output)
110-
assert_equal too_deep, output.string
111-
output = StringIO.new
112-
dump(eval(too_deep), output, 101)
113-
assert_equal too_deep, output.string
102+
obj = eval(too_deep)
103+
assert_equal too_deep, dump(obj)
104+
assert_kind_of String, Marshal.dump(obj)
105+
assert_raise(ArgumentError) { dump(obj, 100) }
106+
assert_raise(ArgumentError) { Marshal.dump(obj, 100) }
107+
assert_equal too_deep, dump(obj, 101)
108+
assert_kind_of String, Marshal.dump(obj, 101)
109+
110+
assert_equal too_deep, JSON.dump(obj, StringIO.new, 101, strict: false).string
111+
assert_equal too_deep, dump(obj, StringIO.new, 101, strict: false).string
112+
assert_raise(JSON::GeneratorError) { JSON.dump(Object.new, StringIO.new, 101, strict: true).string }
113+
assert_raise(JSON::GeneratorError) { dump(Object.new, StringIO.new, 101, strict: true).string }
114+
115+
assert_equal too_deep, dump(obj, nil, nil, strict: false)
116+
assert_equal too_deep, dump(obj, nil, 101, strict: false)
117+
assert_equal too_deep, dump(obj, StringIO.new, nil, strict: false).string
118+
assert_equal too_deep, dump(obj, nil, strict: false)
119+
assert_equal too_deep, dump(obj, 101, strict: false)
120+
assert_equal too_deep, dump(obj, StringIO.new, strict: false).string
121+
assert_equal too_deep, dump(obj, strict: false)
114122
end
115123

116124
def test_dump_should_modify_defaults

0 commit comments

Comments
 (0)