load the JSON Backend lazily. If the JSON gem is already loaded, use the JSONGem backend by default.

This commit is contained in:
rick 2009-05-17 19:16:11 -07:00
parent b0de061e7b
commit e89241c92f
4 changed files with 17 additions and 5 deletions

@ -40,9 +40,15 @@ def self.escape(string)
end
class << self
attr_reader :backend
delegate :decode, :to => :backend
def backend
@backend || begin
self.backend = defined?(::JSON) ? "JSONGem" : "Yaml"
@backend
end
end
def backend=(name)
if name.is_a?(Module)
@backend = name
@ -77,6 +83,5 @@ def escape_html_entities_in_json=(value)
end
ActiveSupport.escape_html_entities_in_json = true
ActiveSupport::JSON.backend = 'Yaml'
require 'active_support/json/encoding'

@ -1,6 +1,8 @@
require 'json' unless defined?(JSON)
module ActiveSupport
module JSON
ParseError = ::JSON::ParserError
ParseError = ::JSON::ParserError unless const_defined?(:ParseError)
module Backends
module JSONGem

@ -2,7 +2,9 @@
module ActiveSupport
module JSON
class ParseError < StandardError
unless const_defined?(:ParseError)
class ParseError < StandardError
end
end
module Backends

@ -36,6 +36,9 @@ class TestJSONDecoding < ActiveSupport::TestCase
%q({"b":["\u003ci\u003e","\u003cb\u003e","\u003cu\u003e"]}) => {'b' => ["<i>","<b>","<u>"]}
}
# load the default JSON backend
ActiveSupport::JSON.backend
backends = %w(Yaml)
begin
gem 'json', '>= 1.1'