Skip to content

Commit

Permalink
Raise MultiJson::LoadError on blank input
Browse files Browse the repository at this point in the history
fixes #135
  • Loading branch information
rwz committed Sep 8, 2013
1 parent 46684cf commit 49aa2d8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/multi_json/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def defaults(action, value)
end

def load(string, options={})
raise self::ParseError if blank?(string)
instance.load(string, collect_load_options(options).clone)
end

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

private

def blank?(input)
input.nil? || /\A\s*\z/ === input
end

end
end
end
6 changes: 6 additions & 0 deletions spec/shared/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ def to_json(*)
expect{MultiJson.load('{"abc"}')}.to raise_error(MultiJson::LoadError)
end

it 'raises MultiJson::LoadError in blank input' do
[nil, ' ', "\t\t\t", "\n"].each do |input|
expect{MultiJson.load(input)}.to raise_error(MultiJson::LoadError)
end
end

it 'raises MultiJson::LoadError with data on invalid JSON' do
data = '{invalid}'
begin
Expand Down

0 comments on commit 49aa2d8

Please sign in to comment.