From 2cb604a0f0b2b3df306b3d36e0f4ca7c0679dab5 Mon Sep 17 00:00:00 2001 From: "Peter M. Goldstein" Date: Thu, 30 Jan 2014 17:39:36 -0800 Subject: [PATCH] Make MultiJson optional for Ruby 1.9+ --- Gemfile | 2 +- Rakefile | 1 - lib/jwt/json.rb | 30 ++++++++++++++++++++++-------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Gemfile b/Gemfile index 0e42ff0b..5098cfcd 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Rakefile b/Rakefile index f910e907..12eea148 100644 --- a/Rakefile +++ b/Rakefile @@ -8,7 +8,6 @@ Echoe.new('jwt', '0.1.11') do |p| p.author = "Jeff Lindsay" p.email = "progrium@gmail.com" p.ignore_pattern = ["tmp/*"] - p.runtime_dependencies = ["multi_json >=1.5"] p.development_dependencies = ["echoe >=4.6.3"] p.licenses = "MIT" end diff --git a/lib/jwt/json.rb b/lib/jwt/json.rb index 3e787c95..49e1f92e 100644 --- a/lib/jwt/json.rb +++ b/lib/jwt/json.rb @@ -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 \ No newline at end of file