Skip to content

Commit

Permalink
Merge pull request #33 from petergoldstein/feature/make_multi_json_op…
Browse files Browse the repository at this point in the history
…tional

Make MultiJson optional for Ruby 1.9+
  • Loading branch information
progrium committed Jan 31, 2014
2 parents 46ec006 + 2cb604a commit d84da02
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source "https://rubygems.org"

gem 'json', '>= 1.2.4'
gem 'multi_json', '~> 1.0'
gem 'multi_json', '~> 1.0', :platforms => :ruby_18
gem 'jruby-openssl', :platforms => :jruby

gem 'rubysl', '~> 2.0', :platforms => :rbx
Expand Down
1 change: 0 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Echoe.new('jwt', '0.1.11') do |p|
p.author = "Jeff Lindsay"
p.email = "[email protected]"
p.ignore_pattern = ["tmp/*"]
p.runtime_dependencies = ["multi_json >=1.5"]
p.development_dependencies = ["echoe >=4.6.3"]
p.licenses = "MIT"
end
Expand Down
30 changes: 22 additions & 8 deletions lib/jwt/json.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
module JWT
module Json
if RUBY_VERSION >= "1.9" && !defined?(MultiJson)
require 'json'

require "multi_json"
def decode_json(encoded)
JSON.parse(encoded)
rescue JSON::ParserError
raise JWT::DecodeError.new("Invalid segment encoding")
end

def decode_json(encoded)
MultiJson.decode(encoded)
rescue MultiJson::LoadError
raise JWT::DecodeError.new("Invalid segment encoding")
end
def encode_json(raw)
JSON.generate(raw)
end

else
require "multi_json"

def decode_json(encoded)
MultiJson.decode(encoded)
rescue MultiJson::LoadError
raise JWT::DecodeError.new("Invalid segment encoding")
end

def encode_json(raw)
MultiJson.encode(raw)
def encode_json(raw)
MultiJson.encode(raw)
end
end
end
end

0 comments on commit d84da02

Please sign in to comment.