Skip to content

Commit 5d8febd

Browse files
Daniel Farinarwz
authored andcommitted
Oj 2 and 3 support
By using dynamic definition, we can support both versions with only tiny cost at start-up. Oj3 has made its option hash more like the JSON gem's, but duplicate "by hand" the "pretty" option hash as it's small and slow-changing enough, and to avoid surprises with new Ruby versions.
1 parent e1f6584 commit 5d8febd

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

.travis.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,28 @@ matrix:
2323
dist: trusty
2424
env: SKIP_ADAPTERS=oj,yajl,nsjsonserialization
2525
- rvm: 2.0
26-
gemfile: gemfiles/gemfile-2
26+
gemfile: gemfiles/gemfile-2-0
2727
env: SKIP_ADAPTERS=gson,jr_jackson,nsjsonserialization
2828
- rvm: 2.1
29-
gemfile: gemfiles/gemfile-2
29+
gemfile: gemfiles/gemfile-2-0
3030
env: SKIP_ADAPTERS=gson,jr_jackson,nsjsonserialization
3131
- rvm: 2.2
32-
gemfile: gemfiles/gemfile-2
32+
gemfile: gemfiles/gemfile-2-0
3333
env: SKIP_ADAPTERS=gson,jr_jackson,nsjsonserialization
3434
- rvm: 2.3
35-
gemfile: gemfiles/gemfile-2
35+
gemfile: gemfiles/gemfile-2-3
3636
env: SKIP_ADAPTERS=gson,jr_jackson,nsjsonserialization
3737
- rvm: 2.4
38-
gemfile: gemfiles/gemfile-2
38+
gemfile: gemfiles/gemfile-2-3
3939
env: SKIP_ADAPTERS=gson,jr_jackson,nsjsonserialization
4040
- rvm: 2.5
41-
gemfile: gemfiles/gemfile-2
41+
gemfile: gemfiles/gemfile-2-3
4242
env: SKIP_ADAPTERS=gson,jr_jackson,nsjsonserialization
4343
- rvm: 2.6
44-
gemfile: gemfiles/gemfile-2
44+
gemfile: gemfiles/gemfile-2-3
4545
env: SKIP_ADAPTERS=gson,jr_jackson,nsjsonserialization
4646
- rvm: ruby-head
47-
gemfile: gemfiles/gemfile-2
47+
gemfile: gemfiles/gemfile-2-3
4848
env: SKIP_ADAPTERS=gson,jr_jackson,nsjsonserialization
4949
- rvm: jruby-9000
5050
gemfile: gemfiles/gemfile-2-jruby

gemfiles/gemfile-2 renamed to gemfiles/gemfile-2-0

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ source "https://rubygems.org"
22

33
gem "json", "~> 2.0", require: false
44
gem "json_pure", "~> 2.0", require: false
5-
gem "oj", "~> 2.18", require: false
5+
gem "oj", "~> 2.0", require: false
66
gem "yajl-ruby", "~> 1.3", require: false
77

88
gemspec path: ".."

gemfiles/gemfile-2-3

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
source "https://rubygems.org"
2+
3+
gem "json", "~> 2.0", require: false
4+
gem "json_pure", "~> 2.0", require: false
5+
gem "oj", "~> 3.0", require: false
6+
gem "yajl-ruby", "~> 1.3", require: false
7+
8+
gemspec path: ".."

lib/multi_json/adapters/oj.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,29 @@ def load(string, options = {})
3434
::Oj.load(string, options)
3535
end
3636

37-
def dump(object, options = {})
38-
options.merge!(:indent => 2) if options[:pretty]
39-
options[:indent] = options[:indent].to_i if options[:indent]
40-
::Oj.dump(object, options)
37+
case ::Oj::VERSION
38+
when /\A2\./
39+
def dump(object, options = {})
40+
options.merge!(:indent => 2) if options[:pretty]
41+
options[:indent] = options[:indent].to_i if options[:indent]
42+
::Oj.dump(object, options)
43+
end
44+
when /\A3\./
45+
PRETTY_STATE_PROTOTYPE = {
46+
:indent => " ",
47+
:space => " ",
48+
:space_before => "",
49+
:object_nl => "\n",
50+
:array_nl => "\n",
51+
:ascii_only => false,
52+
}
53+
54+
def dump(object, options = {})
55+
options.merge!(PRETTY_STATE_PROTOTYPE.dup) if options.delete(:pretty)
56+
::Oj.dump(object, options)
57+
end
58+
else
59+
fail "Unsupported Oj version: #{::Oj::VERSION}"
4160
end
4261
end
4362
end

0 commit comments

Comments
 (0)