Skip to content

Commit 1a48840

Browse files
committed
Merge pull request resque#580 from twinturbo/resque
--- This commit uses feature detection with MultiJson so we dont 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
2 parents 915bf7c + 67756f0 commit 1a48840

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

lib/resque/multi_json_coder.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,32 @@
33

44
# OkJson won't work because it doesn't serialize symbols
55
# in the same way yajl and json do.
6-
if MultiJson.engine.to_s == 'MultiJson::Engines::OkJson'
7-
raise "Please install the yajl-ruby or json gem"
6+
7+
if MultiJson.respond_to?(:adapter)
8+
raise "Please install the yajl-ruby or json gem" if MultiJson.adapter.to_s == 'MultiJson::Adapters::OkJson'
9+
elsif MultiJson.respond_to?(:engine)
10+
raise "Please install the yajl-ruby or json gem" if MultiJson.engine.to_s == 'MultiJson::Engines::OkJson'
811
end
912

1013
module Resque
1114
class MultiJsonCoder < Coder
1215
def encode(object)
13-
::MultiJson.encode(object)
16+
if MultiJson.respond_to?(:dump) && MultiJson.respond_to?(:load)
17+
MultiJson.load object
18+
else
19+
MultiJson.encode object
20+
end
1421
end
1522

1623
def decode(object)
1724
return unless object
1825

1926
begin
20-
::MultiJson.decode(object)
27+
if MultiJson.respond_to?(:dump) && MultiJson.respond_to?(:load)
28+
MultiJson.dump object
29+
else
30+
MultiJson.decode object
31+
end
2132
rescue ::MultiJson::DecodeError => e
2233
raise DecodeException, e.message, e.backtrace
2334
end

0 commit comments

Comments
 (0)