[flori/json] Fix JSON.dump overload combination

https://github.com/flori/json/commit/41c2712a3b
This commit is contained in:
tompng 2023-12-04 19:18:14 +09:00 committed by Hiroshi SHIBATA
parent e6b35e8a6d
commit 70740deea7
2 changed files with 31 additions and 24 deletions

View File

@ -612,16 +612,13 @@ module JSON
# Output: # Output:
# {"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"} # {"foo":[0,1],"bar":{"baz":2,"bat":3},"bam":"bad"}
def dump(obj, anIO = nil, limit = nil, kwargs = nil) def dump(obj, anIO = nil, limit = nil, kwargs = nil)
if anIO and limit.nil? io_limit_opt = [anIO, limit, kwargs].compact
anIO = anIO.to_io if anIO.respond_to?(:to_io) kwargs = io_limit_opt.pop if io_limit_opt.last.is_a?(Hash)
unless anIO.respond_to?(:write) anIO, limit = io_limit_opt
if kwargs.nil? and anIO.is_a?(Hash) if anIO.respond_to?(:to_io)
kwargs = anIO anIO = anIO.to_io
else elsif limit.nil? && !anIO.respond_to?(:write)
limit = anIO anIO, limit = nil, anIO
end
anIO = nil
end
end end
opts = JSON.dump_default_options opts = JSON.dump_default_options
opts = opts.merge(:max_nesting => limit) if limit opts = opts.merge(:max_nesting => limit) if limit
@ -642,12 +639,14 @@ module JSON
string.encode(to, from) string.encode(to, from)
end end
private
def merge_dump_options(opts, strict: NOT_SET) def merge_dump_options(opts, strict: NOT_SET)
opts = opts.merge(strict: strict) if NOT_SET != strict opts = opts.merge(strict: strict) if NOT_SET != strict
opts opts
end end
class << self
private :merge_dump_options
end
end end
module ::Kernel module ::Kernel

View File

@ -99,18 +99,26 @@ class JSONCommonInterfaceTest < Test::Unit::TestCase
def test_dump def test_dump
too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]' too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
assert_equal too_deep, dump(eval(too_deep)) obj = eval(too_deep)
assert_kind_of String, Marshal.dump(eval(too_deep)) assert_equal too_deep, dump(obj)
assert_raise(ArgumentError) { dump(eval(too_deep), 100) } assert_kind_of String, Marshal.dump(obj)
assert_raise(ArgumentError) { Marshal.dump(eval(too_deep), 100) } assert_raise(ArgumentError) { dump(obj, 100) }
assert_equal too_deep, dump(eval(too_deep), 101) assert_raise(ArgumentError) { Marshal.dump(obj, 100) }
assert_kind_of String, Marshal.dump(eval(too_deep), 101) assert_equal too_deep, dump(obj, 101)
output = StringIO.new assert_kind_of String, Marshal.dump(obj, 101)
dump(eval(too_deep), output)
assert_equal too_deep, output.string assert_equal too_deep, JSON.dump(obj, StringIO.new, 101, strict: false).string
output = StringIO.new assert_equal too_deep, dump(obj, StringIO.new, 101, strict: false).string
dump(eval(too_deep), output, 101) assert_raise(JSON::GeneratorError) { JSON.dump(Object.new, StringIO.new, 101, strict: true).string }
assert_equal too_deep, output.string assert_raise(JSON::GeneratorError) { dump(Object.new, StringIO.new, 101, strict: true).string }
assert_equal too_deep, dump(obj, nil, nil, strict: false)
assert_equal too_deep, dump(obj, nil, 101, strict: false)
assert_equal too_deep, dump(obj, StringIO.new, nil, strict: false).string
assert_equal too_deep, dump(obj, nil, strict: false)
assert_equal too_deep, dump(obj, 101, strict: false)
assert_equal too_deep, dump(obj, StringIO.new, strict: false).string
assert_equal too_deep, dump(obj, strict: false)
end end
def test_dump_should_modify_defaults def test_dump_should_modify_defaults