6

Getting the following error on windows:

Encoding::UndefinedConversionError: "\xEF" from ASCII-8BIT to UTF-8

Code:

text = File.open(file, 'r:binary', &:read); #opens file and reads it with r:binary flag
puts text; #works i get here, outputs the below file contents
data = JSON.parse(text.force_encoding(Encoding::UTF_8)); #fails here with above error

Note: I've tried R:UTF-8 as well.


File Contents:

{
  "Environments": [
    {
      "Environment": "UT",
      "Configuration_Directory": "configs/",
      "Files": [
        {
          "Source": "Web.ENV.config",
          "Destination": "Web.config"
        }
      ]
    },
    {
      "Environment": "UAT",
      "Configuration_Directory": "configs/",
      "Files": [
        {
          "Source": "Web.ENV.config",
          "Destination": "Web.config"
        }
      ]
    },
    {
      "Environment": "Staging",
      "Configuration_Directory": "configs/",
      "Files": [
        {
          "Source": "Web.ENV.config",
          "Destination": "Web.config"
        }
      ]
    },
    {
      "Environment": "Production",
      "Configuration_Directory": "configs/",
      "Files": [
        {
          "Source": "Web.ENV.config",
          "Destination": "Web.config"
        }
      ]
    }
  ]
}
1

1 Answer 1

11

I had this problem where the original string was UTF-8 with BOM but Ruby had encoded it as ASCII-8bit. I convert the string to a byte array and then back into a string while forcing the encoding to be UTF-8.

string_value.bytes.pack("c*").force_encoding("UTF-8")
Sign up to request clarification or add additional context in comments.

1 Comment

Eric you saved me a lot of Googling <3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.