diff options
author | Brett Carter <brett@janrain.com> | 2012-12-13 15:52:19 -0800 |
---|---|---|
committer | Steve Klabnik <steve@steveklabnik.com> | 2012-12-14 18:54:32 -0500 |
commit | 8f8397e0a4ea2bbc27d4bba60088286217314807 (patch) | |
tree | 0c5bc9559b1028a2c4a2df36ddb59171d2bd1257 /activesupport/test | |
parent | f447240b0565fa48f0dbe9341a7bb0feff938dab (diff) | |
download | rails-8f8397e0a4ea2bbc27d4bba60088286217314807.tar.gz rails-8f8397e0a4ea2bbc27d4bba60088286217314807.tar.bz2 rails-8f8397e0a4ea2bbc27d4bba60088286217314807.zip |
Remove unicode character encoding from ActiveSupport::JSON.encode
The encoding scheme (e.g. ☠ -> "\u2620") was broken for characters
not in the Basic Multilingual Plane. It is possible to escape them
for json using the weird encoding scheme of a twelve-character
sequence representing the UTF-16 surrogate pair (e.g. '𠜎' ->
"\u270e\u263a") but this wasn't properly handled in the escaping code.
Since raw UTF-8 is allowed in json, it was decided to simply pass
through the raw bytes rather than attempt to escape them.
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/json/encoding_test.rb | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb index 5bb2a45c87..b6e2cd4529 100644 --- a/activesupport/test/json/encoding_test.rb +++ b/activesupport/test/json/encoding_test.rb @@ -112,21 +112,34 @@ class TestJSONEncoding < ActiveSupport::TestCase def test_utf8_string_encoded_properly result = ActiveSupport::JSON.encode('€2.99') - assert_equal '"\\u20ac2.99"', result + assert_equal '"€2.99"', result assert_equal(Encoding::UTF_8, result.encoding) result = ActiveSupport::JSON.encode('✎☺') - assert_equal '"\\u270e\\u263a"', result + assert_equal '"✎☺"', result assert_equal(Encoding::UTF_8, result.encoding) end def test_non_utf8_string_transcodes s = '二'.encode('Shift_JIS') result = ActiveSupport::JSON.encode(s) - assert_equal '"\\u4e8c"', result + assert_equal '"二"', result assert_equal Encoding::UTF_8, result.encoding end + def test_wide_utf8_chars + w = '𠜎' + result = ActiveSupport::JSON.encode(w) + assert_equal '"𠜎"', result + end + + def test_wide_utf8_roundtrip + hash = { string: "𐒑" } + json = ActiveSupport::JSON.encode(hash) + decoded_hash = ActiveSupport::JSON.decode(json) + assert_equal "𐒑", decoded_hash['string'] + end + def test_exception_raised_when_encoding_circular_reference_in_array a = [1] a << a |