Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Automatically prefer Yajl or JSON backend over Yaml, if available

  • Loading branch information...
commit d5b4f4debfc26998f98af03be835805e27d9e5f5 1 parent 83b4c16
@jeremy jeremy authored
View
2  activesupport/CHANGELOG
@@ -1,6 +1,6 @@
*2.3.6 (pending)*
-* JSON backend for YAJL. #2666 [Brian Lopez]
+* JSON backend for YAJL. Preferred if available. #2666 [Brian Lopez]
* Introduce String#html_safe for rails_xss plugin and forward-compatibility with Rails 3. [Michael Koziarski, Santiago Pastorino, José Ignacio Costa]
View
17 activesupport/lib/active_support/json/decoding.rb
@@ -5,12 +5,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
@@ -30,6 +33,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
View
2  activesupport/test/json/decoding_test.rb
@@ -36,7 +36,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)
Please sign in to comment.
Something went wrong with that request. Please try again.