Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use oj if available instead ActiveSupport::JSON #398

Merged
merged 1 commit into from Nov 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -7,3 +7,4 @@ pkg
.bundle
.rvmrc
Gemfile.lock
gemfiles/*.lock
1 change: 1 addition & 0 deletions gemfiles/Gemfile.rails-3.2.x
Expand Up @@ -7,3 +7,4 @@ gem 'mocha'
gem 'test_declarative'
gem 'rake'
gem 'minitest'
gem 'oj'
1 change: 1 addition & 0 deletions gemfiles/Gemfile.rails-4.0.x
Expand Up @@ -7,3 +7,4 @@ gem 'mocha'
gem 'test_declarative'
gem 'rake'
gem 'minitest'
gem 'oj'
1 change: 1 addition & 0 deletions gemfiles/Gemfile.rails-4.1.x
Expand Up @@ -7,3 +7,4 @@ gem 'mocha'
gem 'test_declarative'
gem 'rake'
gem 'minitest'
gem 'oj'
1 change: 1 addition & 0 deletions gemfiles/Gemfile.rails-4.2.x
Expand Up @@ -7,3 +7,4 @@ gem 'mocha'
gem 'test_declarative'
gem 'rake'
gem 'minitest'
gem 'oj'
1 change: 1 addition & 0 deletions gemfiles/Gemfile.rails-5.0.x
Expand Up @@ -7,3 +7,4 @@ gem 'mocha'
gem 'test_declarative'
gem 'rake'
gem 'minitest'
gem 'oj'
1 change: 1 addition & 0 deletions gemfiles/Gemfile.rails-5.1.x
Expand Up @@ -7,3 +7,4 @@ gem 'mocha'
gem 'test_declarative'
gem 'rake'
gem 'minitest'
gem 'oj'
1 change: 1 addition & 0 deletions gemfiles/Gemfile.rails-master
Expand Up @@ -7,3 +7,4 @@ gem 'mocha'
gem 'test_declarative'
gem 'rake'
gem 'minitest'
gem 'oj'
25 changes: 21 additions & 4 deletions lib/i18n/backend/key_value.rb
@@ -1,7 +1,24 @@
require 'i18n/backend/base'
require 'active_support/json'

module I18n

begin
require 'oj'
class JSON
class << self
def encode(value)
Oj::Rails.encode(value)
end
def decode(value)
Oj.load(value)
end
end
end
rescue LoadError
require 'active_support/json'
JSON = ActiveSupport::JSON
end

module Backend
# This is a basic backend for key value stores. It receives on
# initialization the store, which should respond to three methods:
Expand Down Expand Up @@ -65,14 +82,14 @@ def store_translations(locale, data, options = {})
case value
when Hash
if @subtrees && (old_value = @store[key])
old_value = ActiveSupport::JSON.decode(old_value)
old_value = JSON.decode(old_value)
value = old_value.deep_symbolize_keys.deep_merge!(value) if old_value.is_a?(Hash)
end
when Proc
raise "Key-value stores cannot handle procs"
end

@store[key] = ActiveSupport::JSON.encode(value) unless value.is_a?(Symbol)
@store[key] = JSON.encode(value) unless value.is_a?(Symbol)
end
end

Expand All @@ -89,7 +106,7 @@ def available_locales
def lookup(locale, key, scope = [], options = {})
key = normalize_flat_keys(locale, key, scope, options[:separator])
value = @store["#{locale}.#{key}"]
value = ActiveSupport::JSON.decode(value) if value
value = JSON.decode(value) if value
value.is_a?(Hash) ? value.deep_symbolize_keys : value
end
end
Expand Down