Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/json/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -699,10 +699,10 @@ def load(source, proc = nil, allow_blank: true, **options)
unless source.is_a?(String)
if source.respond_to? :to_str
source = source.to_str
elsif source.respond_to? :read
source = source.read
elsif source.respond_to? :to_io
source = source.to_io.read
elsif source.respond_to?(:read)
source = source.read
end
end

Expand Down
14 changes: 12 additions & 2 deletions test/json/json_common_interface_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,25 @@ def o.to_s

def test_load
assert_equal @hash, JSON.load(@json)
assert_equal nil, JSON.load(nil)
assert_equal nil, JSON.load('')

tempfile = Tempfile.open('@json')
tempfile.write @json
tempfile.rewind
assert_equal @hash, JSON.load(tempfile)

stringio = StringIO.new(@json)
stringio.rewind
assert_equal @hash, JSON.load(stringio)
assert_equal nil, JSON.load(nil)
assert_equal nil, JSON.load('')

tempfile = Tempfile.open('@json.gzip')
gzw = Zlib::GzipWriter.new(tempfile)
gzw.write(@json)
gzw.close
tempfile.open
gzr = Zlib::GzipReader.new(tempfile)
assert_equal @hash, JSON.load(gzr)
ensure
tempfile.close!
end
Expand Down
1 change: 1 addition & 0 deletions test/json/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
require 'test/unit'
require 'stringio'
require 'tempfile'
require 'zlib'
begin
require 'bigdecimal'
rescue LoadError
Expand Down
Loading