diff --git a/lib/spring/json.rb b/lib/spring/json.rb index 00f0dca3..112ffeb4 100644 --- a/lib/spring/json.rb +++ b/lib/spring/json.rb @@ -321,11 +321,11 @@ def abbrev(s) # Unquote will raise an error if q contains control characters. def unquote(q) q = q[1...-1] - a = q.dup # allocate a big enough string - # In ruby >= 1.9, a[w] is a codepoint, not a byte. + # In ruby >= 1.9, q[r], a[w] is a codepoint, not a byte. if rubydoesenc? - a.force_encoding('UTF-8') + q.force_encoding('UTF-8') end + a = q.dup # allocate a big enough string r, w = 0, 0 while r < q.length c = q[r] diff --git a/test/unit/json_test.rb b/test/unit/json_test.rb index b9eecc05..79936abe 100644 --- a/test/unit/json_test.rb +++ b/test/unit/json_test.rb @@ -8,5 +8,11 @@ class JsonTest < ActiveSupport::TestCase test 'can encode' do assert_equal('{}', Spring::JSON.dump({})) - end + end + + test 'can encode and decode unicode characters' do + encoded = Spring::JSON.dump({"unicode_example"=>"©".b}) + assert_equal('{"unicode_example":"©"}'.b, encoded) + assert_equal({"unicode_example"=>"©"}, Spring::JSON.load(encoded)) + end end