Skip to content

Commit

Permalink
Close resque#576, Close resque#573, Close resque#571, Close resque#570
Browse files Browse the repository at this point in the history
This commit uses feature detection with MultiJson so we don't have
to lock to specific version which would impact other projects.

The commit also handles the edge case where OkJson is loaded
and may be referenced in an unknown namspace.

Idea: https://github.com/jonleighton/poltergeist/pull/52/files
  • Loading branch information
twinturbo committed Apr 25, 2012
1 parent 1d89dc6 commit 67756f0
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/resque/multi_json_coder.rb
Expand Up @@ -3,21 +3,32 @@

# OkJson won't work because it doesn't serialize symbols
# in the same way yajl and json do.
if MultiJson.engine.to_s == 'MultiJson::Engines::OkJson'
raise "Please install the yajl-ruby or json gem"

if MultiJson.respond_to?(:adapter)
raise "Please install the yajl-ruby or json gem" if MultiJson.adapter.to_s == 'MultiJson::Adapters::OkJson'
elsif MultiJson.respond_to?(:engine)
raise "Please install the yajl-ruby or json gem" if MultiJson.engine.to_s == 'MultiJson::Engines::OkJson'
end

module Resque
class MultiJsonCoder < Coder
def encode(object)
::MultiJson.encode(object)
if MultiJson.respond_to?(:dump) && MultiJson.respond_to?(:load)
MultiJson.load object
else
MultiJson.encode object
end
end

def decode(object)
return unless object

begin
::MultiJson.decode(object)
if MultiJson.respond_to?(:dump) && MultiJson.respond_to?(:load)
MultiJson.dump object
else
MultiJson.decode object
end
rescue ::MultiJson::DecodeError => e
raise DecodeException, e.message, e.backtrace
end
Expand Down

0 comments on commit 67756f0

Please sign in to comment.