Automatically prefer Yajl or JSON backend over Yaml, if available

This commit is contained in:
Jeremy Kemper 2010-02-05 10:16:30 -08:00
parent a96bf4ab5e
commit 63bb955a99
3 changed files with 18 additions and 3 deletions

@ -1,6 +1,6 @@
*Rails 3.0 (pending)*
* JSON backend for YAJL. #2666 [Brian Lopez]
* JSON backend for YAJL. Preferred if available. #2666 [Brian Lopez]
*Rails 3.0.0 [beta] (February 4, 2010)*

@ -6,12 +6,15 @@ module ActiveSupport
mattr_accessor :parse_json_times
module JSON
# Listed in order of preference.
DECODERS = %w(Yajl JSONGem Yaml)
class << self
attr_reader :parse_error
delegate :decode, :to => :backend
def backend
self.backend = "Yaml" unless defined?(@backend)
set_default_backend unless defined?(@backend)
@backend
end
@ -31,6 +34,18 @@ def with_backend(name)
ensure
self.backend = old_backend
end
def set_default_backend
DECODERS.find do |name|
begin
self.backend = name
true
rescue LoadError
# Try next decoder.
false
end
end
end
end
end
end

@ -45,7 +45,7 @@ class TestJSONDecoding < ActiveSupport::TestCase
}
# load the default JSON backend
ActiveSupport::JSON.backend
ActiveSupport::JSON.backend = 'Yaml'
backends = %w(Yaml)
backends << "JSONGem" if defined?(::JSON)