Skip to content

Commit 1f225fc

Browse files
committed
Fixed to do force_encoding when decoding JSON.
1 parent e5fc6b2 commit 1f225fc

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

lib/spring/json.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,11 @@ def abbrev(s)
321321
# Unquote will raise an error if q contains control characters.
322322
def unquote(q)
323323
q = q[1...-1]
324-
a = q.dup # allocate a big enough string
325-
# In ruby >= 1.9, a[w] is a codepoint, not a byte.
324+
# In ruby >= 1.9, q[r], a[w] is a codepoint, not a byte.
326325
if rubydoesenc?
327-
a.force_encoding('UTF-8')
326+
q.force_encoding('UTF-8')
328327
end
328+
a = q.dup # allocate a big enough string
329329
r, w = 0, 0
330330
while r < q.length
331331
c = q[r]

test/unit/json_test.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,11 @@ class JsonTest < ActiveSupport::TestCase
88

99
test 'can encode' do
1010
assert_equal('{}', Spring::JSON.dump({}))
11-
end
11+
end
12+
13+
test 'can encode and decode unicode characters' do
14+
encoded = Spring::JSON.dump({"unicode_example"=>"©".b})
15+
assert_equal('{"unicode_example":"©"}'.b, encoded)
16+
assert_equal({"unicode_example"=>"©"}, Spring::JSON.load(encoded))
17+
end
1218
end

0 commit comments

Comments
 (0)