aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test
diff options
context:
space:
mode:
authorSam Stephenson <sam@37signals.com>2006-11-11 18:25:26 +0000
committerSam Stephenson <sam@37signals.com>2006-11-11 18:25:26 +0000
commit2516063f61cedf4eee36036a0b4bc9fb8c443ba1 (patch)
treea0a33d0550008ba582a3404e16ee727ce0533577 /activesupport/test
parent2a92995d2ab33c630271746b7ec172bcd07daddf (diff)
downloadrails-2516063f61cedf4eee36036a0b4bc9fb8c443ba1.tar.gz
rails-2516063f61cedf4eee36036a0b4bc9fb8c443ba1.tar.bz2
rails-2516063f61cedf4eee36036a0b4bc9fb8c443ba1.zip
Don't quote hash keys in Hash#to_json if they're valid JavaScript identifiers.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5486 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/test')
-rw-r--r--activesupport/test/json.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/activesupport/test/json.rb b/activesupport/test/json.rb
index 33c5a64ba5..0274dd073a 100644
--- a/activesupport/test/json.rb
+++ b/activesupport/test/json.rb
@@ -36,6 +36,14 @@ class TestJSONEmitters < Test::Unit::TestCase
end
end
end
+
+ def setup
+ unquote(false)
+ end
+
+ def teardown
+ unquote(true)
+ end
def test_hash_encoding
assert_equal %({\"a\": \"b\"}), { :a => :b }.to_json
@@ -60,4 +68,20 @@ class TestJSONEmitters < Test::Unit::TestCase
a << a
assert_raises(ActiveSupport::JSON::CircularReferenceError) { a.to_json }
end
+
+ def test_unquote_hash_key_identifiers
+ values = {0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B"}
+ assert_equal %({"a": "a", 0: 0, "_": "_", 1: 1, "$": "$", "A": "A", "A0B": "A0B", "A0": "A0"}), values.to_json
+ unquote(true) { assert_equal %({a: "a", 0: 0, _: "_", 1: 1, $: "$", A: "A", A0B: "A0B", A0: "A0"}), values.to_json }
+ end
+
+ protected
+ def unquote(value)
+ previous_value = ActiveSupport::JSON.unquote_hash_key_identifiers
+ ActiveSupport::JSON.unquote_hash_key_identifiers = value
+ yield if block_given?
+ ensure
+ ActiveSupport::JSON.unquote_hash_key_identifiers = previous_value if block_given?
+ end
+
end