Skip to content

Commit c44f9c9

Browse files
committed
Raise MultiJson::LoadError on blank input
1 parent 46684cf commit c44f9c9

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

lib/multi_json/adapter.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def defaults(action, value)
1616
end
1717

1818
def load(string, options={})
19+
raise self::ParseError if blank?(string)
1920
instance.load(string, collect_load_options(options).clone)
2021
end
2122

@@ -43,6 +44,12 @@ def cache(method, options)
4344
MultiJson.cached_options[cache_key] ||= yield
4445
end
4546

47+
private
48+
49+
def blank?(input)
50+
input.nil? || /\A\s*\z/ === input
51+
end
52+
4653
end
4754
end
4855
end

spec/shared/adapter.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ def to_json(*)
169169
expect{MultiJson.load('{"abc"}')}.to raise_error(MultiJson::LoadError)
170170
end
171171

172+
it 'raises MultiJson::LoadError on blank input' do
173+
[nil, ' ', "\t\t\t", "\n"].each do |input|
174+
expect{MultiJson.load(input)}.to raise_error(MultiJson::LoadError)
175+
end
176+
end
177+
172178
it 'raises MultiJson::LoadError with data on invalid JSON' do
173179
data = '{invalid}'
174180
begin

0 commit comments

Comments
 (0)