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

Always dup argument to preserve original encoding for force_encoding #537

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aeroastro
Copy link
Contributor

This fixes #529.

In order to avoid changing original encoding,
this patch always dups argument for ASCII-8BIT encoding.

I have also added a test test_argument_encoding_for_binary.
The structure is following test_argument_encoding.

Considering the following facts, IMHO, I believe this patch meets the original intention of this gem.

  • JSON::Pure::Parser.new(str) does not change the encoding of original argument for ASCII-8BIT
  • There already exists a test to guarantee that the encoding of argument is not changed for non ASCII-8BIT encodings.

@Dani-beef
Copy link

require 'json'

str = '{ "hoge": "fuga" }'.force_encoding('ASCII-8BIT')

puts 'JSON'
puts "Before: #{str.encoding}"
puts JSON.parse(str)
puts "After: #{str.encoding}"

puts

str = '{ "hoge": "fuga" }'.force_encoding('ASCII-8BIT')

puts 'JSON::Ext::Parser'
puts "Before: #{str.encoding}"
puts JSON::Ext::Parser.new(str).parse
puts "After: #{str.encoding}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

JSON.parse Alters Encoding of Passed String Unexpectedly
2 participants