From bcfa013399fd2c5b5d25c38912cba3560de1cc57 Mon Sep 17 00:00:00 2001 From: Erich Menge Date: Wed, 30 May 2012 11:29:09 -0500 Subject: Deprecate ActiveSupport::JSON::Variable --- activesupport/CHANGELOG.md | 3 +++ activesupport/lib/active_support/json/variable.rb | 10 +++++++++- activesupport/test/json/encoding_test.rb | 11 ++++++++--- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 8d8a9d8ad8..fce7d34918 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -3,6 +3,9 @@ * Added #beginning_of_hour and #end_of_hour to Time and DateTime core extensions. *Mark J. Titorenko* +* ActiveSupport::JSON::Variable is deprecated. Define your own #as_json and #encode_json methods + for custom JSON string literals. *Erich Menge* + ## Rails 3.2.3 (March 30, 2012) ## diff --git a/activesupport/lib/active_support/json/variable.rb b/activesupport/lib/active_support/json/variable.rb index 5685ed18b7..17b709decc 100644 --- a/activesupport/lib/active_support/json/variable.rb +++ b/activesupport/lib/active_support/json/variable.rb @@ -1,7 +1,15 @@ +require 'active_support/deprecation' + module ActiveSupport module JSON - # A string that returns itself as its JSON-encoded form. + # Deprecated: A string that returns itself as its JSON-encoded form. class Variable < String + def initialize(*args) + ActiveSupport::Deprecation.warn 'ActiveSupport::JSON::Variable is deprecated and will be removed in Rails 4.0. ' \ + 'For your own custom JSON literals, define #as_json and #encode_json yourself.' + super + end + def as_json(options = nil) self end #:nodoc: def encode_json(encoder) self end #:nodoc: end diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index a9590f164f..d327f34140 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -3,7 +3,7 @@ require 'abstract_unit' require 'active_support/core_ext/string/inflections' require 'active_support/json' -class TestJSONEncoding < Test::Unit::TestCase +class TestJSONEncoding < ActiveSupport::TestCase class Foo def initialize(a, b) @a, @b = a, b @@ -46,8 +46,6 @@ class TestJSONEncoding < Test::Unit::TestCase HashlikeTests = [[ Hashlike.new, %({\"a\":1}) ]] CustomTests = [[ Custom.new, '"custom"' ]] - VariableTests = [[ ActiveSupport::JSON::Variable.new('foo'), 'foo'], - [ ActiveSupport::JSON::Variable.new('alert("foo")'), 'alert("foo")']] RegexpTests = [[ /^a/, '"(?-mix:^a)"' ], [/^\w{1,2}[a-z]+/ix, '"(?ix-m:^\\\\w{1,2}[a-z]+)"']] DateTests = [[ Date.new(2005,2,1), %("2005/02/01") ]] @@ -79,6 +77,13 @@ class TestJSONEncoding < Test::Unit::TestCase end end + def test_json_variable + assert_deprecated do + assert_equal ActiveSupport::JSON::Variable.new('foo'), 'foo' + assert_equal ActiveSupport::JSON::Variable.new('alert("foo")'), 'alert("foo")' + end + end + def test_hash_encoding assert_equal %({\"a\":\"b\"}), ActiveSupport::JSON.encode(:a => :b) assert_equal %({\"a\":1}), ActiveSupport::JSON.encode('a' => 1) -- cgit v1.2.3