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 63bb955a99eb46e257655c93dd64e86ebbf05651 1 parent a96bf4a
@jeremy jeremy authored
View
2  activesupport/CHANGELOG
@@ -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)*
View
17 activesupport/lib/active_support/json/decoding.rb
@@ -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
View
2  activesupport/test/json/decoding_test.rb
@@ -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)

4 comments on commit 63bb955

@brianmario

very nice ;)

@brianmario

any chance these would apply cleanly in the 2.3 branch?

@jeremy
Owner

probably, yes

@nono

It should be nice to have the same thing for XmlMini. I wanted to make a patch for it, but I can't run the tests:
*** glibc detected *** /usr/bin/ruby1.8: free(): invalid pointer: 0x00345440 ***

Please sign in to comment.
Something went wrong with that request. Please try again.