aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/json/encoding_test.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2007-09-30 20:57:50 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2007-09-30 20:57:50 +0000
commit7275d2749cb829d89bffe7e6aa87c99084351f6a (patch)
treeea1ffa76f30699cb84827bf6d21dbdb6d1201c1e /activesupport/test/json/encoding_test.rb
parent66d05f5e2c7ac6b18220956fbcf34efcd32638fc (diff)
downloadrails-7275d2749cb829d89bffe7e6aa87c99084351f6a.tar.gz
rails-7275d2749cb829d89bffe7e6aa87c99084351f6a.tar.bz2
rails-7275d2749cb829d89bffe7e6aa87c99084351f6a.zip
Fixed JSON encoding to use quoted keys according to the JSON standard (closes #8762) [choonkat/chuyeow]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7697 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/test/json/encoding_test.rb')
-rw-r--r--activesupport/test/json/encoding_test.rb34
1 files changed, 5 insertions, 29 deletions
diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb
index 9ed7683b86..ba468141b4 100644
--- a/activesupport/test/json/encoding_test.rb
+++ b/activesupport/test/json/encoding_test.rb
@@ -41,21 +41,12 @@ class TestJSONEncoding < Test::Unit::TestCase
end
end
- def setup
- unquote(false)
- end
-
- def teardown
- unquote(true)
- end
-
def test_hash_encoding
assert_equal %({\"a\": \"b\"}), { :a => :b }.to_json
assert_equal %({\"a\": 1}), { 'a' => 1 }.to_json
assert_equal %({\"a\": [1, 2]}), { 'a' => [1,2] }.to_json
-
- sorted_json =
- '{' + {:a => :b, :c => :d}.to_json[1..-2].split(', ').sort.join(', ') + '}'
+
+ sorted_json = '{' + {:a => :b, :c => :d}.to_json[1..-2].split(', ').sort.join(', ') + '}'
assert_equal %({\"a\": \"b\", \"c\": \"d\"}), sorted_json
end
@@ -72,29 +63,14 @@ class TestJSONEncoding < Test::Unit::TestCase
a << a
assert_raises(ActiveSupport::JSON::CircularReferenceError) { a.to_json }
end
-
- def test_unquote_hash_key_identifiers
+
+ def test_hash_key_identifiers_are_always_quoted
values = {0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B"}
assert_equal %w( "$" "A" "A0" "A0B" "_" "a" 0 1 ), object_keys(values.to_json)
- unquote(true) { assert_equal %w( $ 0 1 A A0 A0B _ a ), object_keys(values.to_json) }
end
-
- def test_unquote_hash_key_identifiers_ignores_javascript_reserved_words
- values = {"hello" => "world", "this" => "that", "with" => "foo"}
- unquote(true) { assert_equal %w( "this" "with" hello ), object_keys(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
-
def object_keys(json_object)
json_object[1..-2].scan(/([^{}:,\s]+):/).flatten.sort
end
-
end