From 63bb955a99eb46e257655c93dd64e86ebbf05651 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 5 Feb 2010 10:16:30 -0800 Subject: Automatically prefer Yajl or JSON backend over Yaml, if available --- activesupport/CHANGELOG | 2 +- activesupport/lib/active_support/json/decoding.rb | 17 ++++++++++++++++- activesupport/test/json/decoding_test.rb | 2 +- 3 files changed, 18 insertions(+), 3 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 71eeff3a53..8ec903e376 100644 --- a/activesupport/CHANGELOG +++ b/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)* diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb index a5908365af..e357b6837a 100644 --- a/activesupport/lib/active_support/json/decoding.rb +++ b/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 @@ module ActiveSupport 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 diff --git a/activesupport/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb index fbd75a8966..d2e3efaa6b 100644 --- a/activesupport/test/json/decoding_test.rb +++ b/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) -- cgit v1.2.3