Skip to content

Commit

Permalink
Load Oj instead of JSON.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon de Andres committed Sep 15, 2015
1 parent 7a0f5af commit 2038f11
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
9 changes: 1 addition & 8 deletions lib/rollbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -796,14 +796,7 @@ def require_core_extensions
end

def monkey_patch_socket?
return false unless defined?(ActiveSupport::VERSION::STRING)

major, minor = ActiveSupport::VERSION::STRING.split('.').map(&:to_i)

return true if major == 3
return true if major == 4 && minor == 0

false
defined?(ActiveSupport::VERSION::STRING)
end

def wrap_delayed_worker
Expand Down
31 changes: 17 additions & 14 deletions lib/rollbar/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@ module JSON
attr_accessor :dump_method
attr_accessor :load_method

def load_native_json
require 'json' unless defined?(::JSON)

if ::JSON.respond_to?(:dump_default_options)
options = ::JSON.dump_default_options
else
# Default options from json 1.1.9 up to 1.6.1
options = { :allow_nan => true, :max_nesting => false }
def load_oj
require 'oj'

options = { :mode=> :compat,
:use_to_json => false,
:symbol_keys => false,
:circular => false
}

self.dump_method = proc do |obj|
begin
Oj.dump(obj, options)
rescue
end
end

self.dump_method = proc { |obj| ::JSON.generate(obj, options) }
self.load_method = proc { |obj| ::JSON.load(obj) }
self.backend_name = :json
self.load_method = proc { |obj| Oj.load(obj) }
self.backend_name = :oj

true
rescue StandardError, ScriptError => err
Rollbar.log_debug('%p while loading JSON library: %s' % [err, err.message])
end

def dump(object)
Expand All @@ -34,7 +37,7 @@ def load(string)
end

def setup
load_native_json
load_oj
end
end
end
Expand Down
1 change: 1 addition & 0 deletions rollbar.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'delayed_job'
gem.add_development_dependency 'rake', '>= 0.9.0'
gem.add_development_dependency 'redis'
gem.add_runtime_dependency 'oj', '~> 2.12.14'
end
6 changes: 3 additions & 3 deletions spec/rollbar/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@

describe '.dump' do
it 'has JSON as backend' do
expect(Rollbar::JSON.backend_name).to be_eql(:json)
expect(Rollbar::JSON.backend_name).to be_eql(:oj)
end

it 'calls JSON.generate' do
expect(::JSON).to receive(:generate).once
expect(::Oj).to receive(:dump).once

Rollbar::JSON.dump(:foo => :bar)
end
end

describe '.load' do
it 'calls MultiJson.load' do
expect(::JSON).to receive(:load).once
expect(::Oj).to receive(:load).once

Rollbar::JSON.load(:foo => :bar)
end
Expand Down

0 comments on commit 2038f11

Please sign in to comment.