From 3202fbabe6df3591d7e2c35727ea9c8b68df8828 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Sun, 18 Mar 2007 07:05:58 +0000 Subject: Refactor ActiveSupport::JSON to be less obtuse. Add support for JSON decoding by way of Syck with ActiveSupport::JSON.decode(json_string). Prevent hash keys that are JavaScript reserved words from being unquoted during encoding. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6443 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/test/json.rb | 87 ----------------------------- activesupport/test/json/decoding_test.rb | 28 ++++++++++ activesupport/test/json/encoding_test.rb | 96 ++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 87 deletions(-) delete mode 100644 activesupport/test/json.rb create mode 100644 activesupport/test/json/decoding_test.rb create mode 100644 activesupport/test/json/encoding_test.rb (limited to 'activesupport/test') diff --git a/activesupport/test/json.rb b/activesupport/test/json.rb deleted file mode 100644 index 0274dd073a..0000000000 --- a/activesupport/test/json.rb +++ /dev/null @@ -1,87 +0,0 @@ -require File.dirname(__FILE__) + '/abstract_unit' - -class Foo - def initialize(a, b) - @a, @b = a, b - end -end - -class TestJSONEmitters < Test::Unit::TestCase - TrueTests = [[ true, %(true) ]] - FalseTests = [[ false, %(false) ]] - NilTests = [[ nil, %(null) ]] - NumericTests = [[ 1, %(1) ], - [ 2.5, %(2.5) ]] - - StringTests = [[ 'this is the string', %("this is the string") ], - [ 'a "string" with quotes', %("a \\"string\\" with quotes") ]] - - ArrayTests = [[ ['a', 'b', 'c'], %([\"a\", \"b\", \"c\"]) ], - [ [1, 'a', :b, nil, false], %([1, \"a\", \"b\", null, false]) ]] - - SymbolTests = [[ :a, %("a") ], - [ :this, %("this") ], - [ :"a b", %("a b") ]] - - ObjectTests = [[ Foo.new(1, 2), %({\"a\": 1, \"b\": 2}) ]] - - VariableTests = [[ ActiveSupport::JSON::Variable.new('foo'), 'foo'], - [ ActiveSupport::JSON::Variable.new('alert("foo")'), 'alert("foo")']] - RegexpTests = [[ /^a/, '/^a/' ], [/^\w{1,2}[a-z]+/ix, '/^\\w{1,2}[a-z]+/ix']] - - constants.grep(/Tests$/).each do |class_tests| - define_method("test_#{class_tests[0..-6].downcase}") do - self.class.const_get(class_tests).each do |pair| - assert_equal pair.last, pair.first.to_json - 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 - 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(', ') + '}' - assert_equal %({\"a\": \"b\", \"c\": \"d\"}), sorted_json - end - - def test_utf8_string_encoded_properly_when_kcode_is_utf8 - old_kcode, $KCODE = $KCODE, 'UTF8' - assert_equal '"\\u20ac2.99"', '€2.99'.to_json - assert_equal '"\\u270e\\u263a"', '✎☺'.to_json - ensure - $KCODE = old_kcode - end - - def test_exception_raised_when_encoding_circular_reference - a = [1] - 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 diff --git a/activesupport/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb new file mode 100644 index 0000000000..77253b6dad --- /dev/null +++ b/activesupport/test/json/decoding_test.rb @@ -0,0 +1,28 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class TestJSONDecoding < Test::Unit::TestCase + TESTS = { + %({"returnTo":{"/categories":"/"}}) => {"returnTo" => {"/categories" => "/"}}, + %({returnTo:{"/categories":"/"}}) => {"returnTo" => {"/categories" => "/"}}, + %({"return\\"To\\":":{"/categories":"/"}}) => {"return\"To\":" => {"/categories" => "/"}}, + %({"returnTo":{"/categories":1}}) => {"returnTo" => {"/categories" => 1}}, + %({"returnTo":[1,"a"]}) => {"returnTo" => [1, "a"]}, + %({"returnTo":[1,"\\"a\\",", "b"]}) => {"returnTo" => [1, "\"a\",", "b"]}, + %([]) => [], + %({}) => {}, + %(1) => 1, + %("") => "", + %("\\"") => "\"", + %(null) => nil, + %(true) => true, + %(false) => false + } + + def test_json_decoding + TESTS.each do |json, expected| + assert_nothing_raised do + assert_equal expected, ActiveSupport::JSON.decode(json) + end + end + end +end diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb new file mode 100644 index 0000000000..149d11ce0f --- /dev/null +++ b/activesupport/test/json/encoding_test.rb @@ -0,0 +1,96 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class TestJSONEncoding < Test::Unit::TestCase + class Foo + def initialize(a, b) + @a, @b = a, b + end + end + + TrueTests = [[ true, %(true) ]] + FalseTests = [[ false, %(false) ]] + NilTests = [[ nil, %(null) ]] + NumericTests = [[ 1, %(1) ], + [ 2.5, %(2.5) ]] + + StringTests = [[ 'this is the string', %("this is the string") ], + [ 'a "string" with quotes', %("a \\"string\\" with quotes") ]] + + ArrayTests = [[ ['a', 'b', 'c'], %([\"a\", \"b\", \"c\"]) ], + [ [1, 'a', :b, nil, false], %([1, \"a\", \"b\", null, false]) ]] + + SymbolTests = [[ :a, %("a") ], + [ :this, %("this") ], + [ :"a b", %("a b") ]] + + ObjectTests = [[ Foo.new(1, 2), %({\"a\": 1, \"b\": 2}) ]] + + VariableTests = [[ ActiveSupport::JSON::Variable.new('foo'), 'foo'], + [ ActiveSupport::JSON::Variable.new('alert("foo")'), 'alert("foo")']] + RegexpTests = [[ /^a/, '/^a/' ], [/^\w{1,2}[a-z]+/ix, '/^\\w{1,2}[a-z]+/ix']] + + constants.grep(/Tests$/).each do |class_tests| + define_method("test_#{class_tests[0..-6].downcase}") do + self.class.const_get(class_tests).each do |pair| + assert_equal pair.last, pair.first.to_json + 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 + 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(', ') + '}' + assert_equal %({\"a\": \"b\", \"c\": \"d\"}), sorted_json + end + + def test_utf8_string_encoded_properly_when_kcode_is_utf8 + old_kcode, $KCODE = $KCODE, 'UTF8' + assert_equal '"\\u20ac2.99"', '€2.99'.to_json + assert_equal '"\\u270e\\u263a"', '✎☺'.to_json + ensure + $KCODE = old_kcode + end + + def test_exception_raised_when_encoding_circular_reference + a = [1] + 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 %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 -- cgit v1.2.3