Browse files

fix bug where JSON.generate(["foo"], nil) raises NoMethodError: undef…

…ined method "has_key?" for nil:NilClass
  • Loading branch information...
1 parent b08b3f7 commit e9557263d0895c676de3d7059aa06636ddfccb09 @f1sherman f1sherman committed Aug 3, 2012
Showing with 9 additions and 10 deletions.
  1. +9 −10 lib/yajl/json_gem/encoding.rb
View
19 lib/yajl/json_gem/encoding.rb
@@ -17,17 +17,16 @@ module JSON
class JSONError < StandardError; end unless defined?(JSON::JSONError)
class GeneratorError < JSONError; end unless defined?(JSON::GeneratorError)
- def self.generate(obj, opts={})
- begin
- options_map = {}
- if opts.has_key?(:indent)
- options_map[:pretty] = true
- options_map[:indent] = opts[:indent]
- end
- Yajl::Encoder.encode(obj, options_map)
- rescue Yajl::EncodeError => e
- raise JSON::GeneratorError, e.message
+ def self.generate(obj, opts=nil)
+ opts ||= {}
+ options_map = {}
+ if opts.has_key?(:indent)
+ options_map[:pretty] = true
+ options_map[:indent] = opts[:indent]
end
+ Yajl::Encoder.encode(obj, options_map)
+ rescue Yajl::EncodeError => e
+ raise JSON::GeneratorError, e.message
end
def self.pretty_generate(obj, opts={})

0 comments on commit e955726

Please sign in to comment.