Skip to content

Commit

Permalink
Oj 2 and 3 support
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Daniel Farina authored and rwz committed Oct 14, 2019
1 parent e1f6584 commit 5d8febd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
16 changes: 8 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,28 @@ matrix:
dist: trusty
env: SKIP_ADAPTERS=oj,yajl,nsjsonserialization
- rvm: 2.0
gemfile: gemfiles/gemfile-2
gemfile: gemfiles/gemfile-2-0
env: SKIP_ADAPTERS=gson,jr_jackson,nsjsonserialization
- rvm: 2.1
gemfile: gemfiles/gemfile-2
gemfile: gemfiles/gemfile-2-0
env: SKIP_ADAPTERS=gson,jr_jackson,nsjsonserialization
- rvm: 2.2
gemfile: gemfiles/gemfile-2
gemfile: gemfiles/gemfile-2-0
env: SKIP_ADAPTERS=gson,jr_jackson,nsjsonserialization
- rvm: 2.3
gemfile: gemfiles/gemfile-2
gemfile: gemfiles/gemfile-2-3
env: SKIP_ADAPTERS=gson,jr_jackson,nsjsonserialization
- rvm: 2.4
gemfile: gemfiles/gemfile-2
gemfile: gemfiles/gemfile-2-3
env: SKIP_ADAPTERS=gson,jr_jackson,nsjsonserialization
- rvm: 2.5
gemfile: gemfiles/gemfile-2
gemfile: gemfiles/gemfile-2-3
env: SKIP_ADAPTERS=gson,jr_jackson,nsjsonserialization
- rvm: 2.6
gemfile: gemfiles/gemfile-2
gemfile: gemfiles/gemfile-2-3
env: SKIP_ADAPTERS=gson,jr_jackson,nsjsonserialization
- rvm: ruby-head
gemfile: gemfiles/gemfile-2
gemfile: gemfiles/gemfile-2-3
env: SKIP_ADAPTERS=gson,jr_jackson,nsjsonserialization
- rvm: jruby-9000
gemfile: gemfiles/gemfile-2-jruby
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/gemfile-2 → gemfiles/gemfile-2-0
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ source "https://rubygems.org"

gem "json", "~> 2.0", require: false
gem "json_pure", "~> 2.0", require: false
gem "oj", "~> 2.18", require: false
gem "oj", "~> 2.0", require: false
gem "yajl-ruby", "~> 1.3", require: false

gemspec path: ".."
8 changes: 8 additions & 0 deletions gemfiles/gemfile-2-3
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source "https://rubygems.org"

gem "json", "~> 2.0", require: false
gem "json_pure", "~> 2.0", require: false
gem "oj", "~> 3.0", require: false
gem "yajl-ruby", "~> 1.3", require: false

gemspec path: ".."
27 changes: 23 additions & 4 deletions lib/multi_json/adapters/oj.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,29 @@ def load(string, options = {})
::Oj.load(string, options)
end

def dump(object, options = {})
options.merge!(:indent => 2) if options[:pretty]
options[:indent] = options[:indent].to_i if options[:indent]
::Oj.dump(object, options)
case ::Oj::VERSION
when /\A2\./
def dump(object, options = {})
options.merge!(:indent => 2) if options[:pretty]
options[:indent] = options[:indent].to_i if options[:indent]
::Oj.dump(object, options)
end
when /\A3\./
PRETTY_STATE_PROTOTYPE = {
:indent => " ",
:space => " ",
:space_before => "",
:object_nl => "\n",
:array_nl => "\n",
:ascii_only => false,
}

def dump(object, options = {})
options.merge!(PRETTY_STATE_PROTOTYPE.dup) if options.delete(:pretty)
::Oj.dump(object, options)
end
else
fail "Unsupported Oj version: #{::Oj::VERSION}"
end
end
end
Expand Down

0 comments on commit 5d8febd

Please sign in to comment.