Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add initial support for Oj

  • Loading branch information...
commit acd06b233edabe6c44f226873db7b49dab560c60 1 parent f387c70
@sferik sferik authored
View
1  Gemfile
@@ -1,6 +1,7 @@
source 'https://rubygems.org'
gem 'json', '~> 1.4', :require => nil
+gem 'oj', '~> 1.0', :require => nil
gem 'yajl-ruby', '~> 1.0', :require => nil
gemspec
View
2  lib/multi_json.rb
@@ -20,6 +20,7 @@ def engine
end
REQUIREMENT_MAP = [
+ ["oj", :oj],
["yajl", :yajl],
["json", :json_gem],
["json/pure", :json_pure]
@@ -32,6 +33,7 @@ def engine
# if any engines are already loaded, then checks
# to see which are installed if none are loaded.
def default_engine
+ return :oj if defined?(::Oj)
return :yajl if defined?(::Yajl)
return :json_gem if defined?(::JSON)
View
18 lib/multi_json/engines/oj.rb
@@ -0,0 +1,18 @@
+require 'oj' unless defined?(Oj)
+
+module MultiJson
+ module Engines
+ # Use the Oj library to encode/decode.
+ class Oj
+ ParseError = SyntaxError
+
+ def self.decode(string, options = {}) #:nodoc:
+ ::Oj.load(string, options)
+ end
+
+ def self.encode(object, options = {}) #:nodoc:
+ ::Oj.dump(object, options)
+ end
+ end
+ end
+end
View
4 spec/helper.rb
@@ -25,10 +25,6 @@ def to_json(options = {})
end
end
-def yajl_on_travis(engine)
- ENV['TRAVIS'] && engine == 'yajl' && jruby?
-end
-
def nsjsonserialization_on_other_than_macruby(engine)
engine == 'nsjsonserialization' && !macruby?
end
View
13 spec/multi_json_spec.rb
@@ -9,11 +9,13 @@
context 'when no other json implementations are available' do
before do
@old_map = MultiJson::REQUIREMENT_MAP
+ @old_oj = Object.const_get :Oj if Object.const_defined?(:Oj)
@old_yajl = Object.const_get :Yajl if Object.const_defined?(:Yajl)
@old_json = Object.const_get :JSON if Object.const_defined?(:JSON)
MultiJson::REQUIREMENT_MAP.each_with_index do |(library, engine), index|
MultiJson::REQUIREMENT_MAP[index] = ["foo/#{library}", engine]
end
+ Object.send :remove_const, :Oj if @old_oj
Object.send :remove_const, :Yajl if @old_yajl
Object.send :remove_const, :JSON if @old_json
end
@@ -22,6 +24,7 @@
@old_map.each_with_index do |(library, engine), index|
MultiJson::REQUIREMENT_MAP[index] = [library, engine]
end
+ Object.const_set :Oj, @old_oj if @old_oj
Object.const_set :Yajl, @old_yajl if @old_yajl
Object.const_set :JSON, @old_json if @old_json
end
@@ -38,8 +41,8 @@
it 'defaults to the best available gem' do
unless jruby?
- require 'yajl'
- MultiJson.engine.name.should == 'MultiJson::Engines::Yajl'
+ require 'oj'
+ MultiJson.engine.name.should == 'MultiJson::Engines::Oj'
else
require 'json'
MultiJson.engine.name.should == 'MultiJson::Engines::JsonGem'
@@ -57,11 +60,7 @@
end
end
- %w(json_gem json_pure ok_json yajl nsjsonserialization).each do |engine|
- if yajl_on_travis(engine)
- puts "Yajl with JRuby is not tested on Travis as C-exts are turned off due to there experimental nature"
- next
- end
+ %w(json_gem json_pure nsjsonserialization oj ok_json yajl).each do |engine|
if nsjsonserialization_on_other_than_macruby(engine)
puts "NSJSONSerialization is exclusively available for MacRuby only."
next
Please sign in to comment.
Something went wrong with that request. Please try again.