From 2516063f61cedf4eee36036a0b4bc9fb8c443ba1 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Sat, 11 Nov 2006 18:25:26 +0000 Subject: 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 --- activesupport/test/json.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'activesupport/test') 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 -- cgit v1.2.3