Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed to do force_encoding when decoding JSON. #736

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/spring/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 7 additions & 1 deletion test/unit/json_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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