Getting the following error on windows:
Encoding::UndefinedConversionError: "\xEF" from ASCII-8BIT to UTF-8
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.
{
"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"
}
]
}
]
}
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With