Skip to content

Commit acd06b2

Browse files
committed
Add initial support for Oj
TODO: Fix failing specs: https://gist.github.com/089bb90e8da1ad65f6c2
1 parent f387c70 commit acd06b2

5 files changed

Lines changed: 27 additions & 11 deletions

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
source 'https://rubygems.org'
22

33
gem 'json', '~> 1.4', :require => nil
4+
gem 'oj', '~> 1.0', :require => nil
45
gem 'yajl-ruby', '~> 1.0', :require => nil
56

67
gemspec

lib/multi_json.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def engine
2020
end
2121

2222
REQUIREMENT_MAP = [
23+
["oj", :oj],
2324
["yajl", :yajl],
2425
["json", :json_gem],
2526
["json/pure", :json_pure]
@@ -32,6 +33,7 @@ def engine
3233
# if any engines are already loaded, then checks
3334
# to see which are installed if none are loaded.
3435
def default_engine
36+
return :oj if defined?(::Oj)
3537
return :yajl if defined?(::Yajl)
3638
return :json_gem if defined?(::JSON)
3739

lib/multi_json/engines/oj.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require 'oj' unless defined?(Oj)
2+
3+
module MultiJson
4+
module Engines
5+
# Use the Oj library to encode/decode.
6+
class Oj
7+
ParseError = SyntaxError
8+
9+
def self.decode(string, options = {}) #:nodoc:
10+
::Oj.load(string, options)
11+
end
12+
13+
def self.encode(object, options = {}) #:nodoc:
14+
::Oj.dump(object, options)
15+
end
16+
end
17+
end
18+
end

spec/helper.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ def to_json(options = {})
2525
end
2626
end
2727

28-
def yajl_on_travis(engine)
29-
ENV['TRAVIS'] && engine == 'yajl' && jruby?
30-
end
31-
3228
def nsjsonserialization_on_other_than_macruby(engine)
3329
engine == 'nsjsonserialization' && !macruby?
3430
end

spec/multi_json_spec.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
context 'when no other json implementations are available' do
1010
before do
1111
@old_map = MultiJson::REQUIREMENT_MAP
12+
@old_oj = Object.const_get :Oj if Object.const_defined?(:Oj)
1213
@old_yajl = Object.const_get :Yajl if Object.const_defined?(:Yajl)
1314
@old_json = Object.const_get :JSON if Object.const_defined?(:JSON)
1415
MultiJson::REQUIREMENT_MAP.each_with_index do |(library, engine), index|
1516
MultiJson::REQUIREMENT_MAP[index] = ["foo/#{library}", engine]
1617
end
18+
Object.send :remove_const, :Oj if @old_oj
1719
Object.send :remove_const, :Yajl if @old_yajl
1820
Object.send :remove_const, :JSON if @old_json
1921
end
@@ -22,6 +24,7 @@
2224
@old_map.each_with_index do |(library, engine), index|
2325
MultiJson::REQUIREMENT_MAP[index] = [library, engine]
2426
end
27+
Object.const_set :Oj, @old_oj if @old_oj
2528
Object.const_set :Yajl, @old_yajl if @old_yajl
2629
Object.const_set :JSON, @old_json if @old_json
2730
end
@@ -38,8 +41,8 @@
3841

3942
it 'defaults to the best available gem' do
4043
unless jruby?
41-
require 'yajl'
42-
MultiJson.engine.name.should == 'MultiJson::Engines::Yajl'
44+
require 'oj'
45+
MultiJson.engine.name.should == 'MultiJson::Engines::Oj'
4346
else
4447
require 'json'
4548
MultiJson.engine.name.should == 'MultiJson::Engines::JsonGem'
@@ -57,11 +60,7 @@
5760
end
5861
end
5962

60-
%w(json_gem json_pure ok_json yajl nsjsonserialization).each do |engine|
61-
if yajl_on_travis(engine)
62-
puts "Yajl with JRuby is not tested on Travis as C-exts are turned off due to there experimental nature"
63-
next
64-
end
63+
%w(json_gem json_pure nsjsonserialization oj ok_json yajl).each do |engine|
6564
if nsjsonserialization_on_other_than_macruby(engine)
6665
puts "NSJSONSerialization is exclusively available for MacRuby only."
6766
next

0 commit comments

Comments
 (0)