Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding::UndefinedConversionError dump to json

I'm working with a gem fie and ran into an issue with this gem that I would like to solve however I'm having trouble doing so. Fie is a gem for Rails. In it, it has some lines where it stores a marshal dump of an ActiveRecord::Base in json however I'm running in to an encoding error. I Have been able to replicate this across different machines and versions of ROR, although Rails 5.2 and greater.

Easiest way to reproduce is:

[5] pry(main)> Marshal.dump(User.first).to_json
  User Load (29.8ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1  [["LIMIT", 1]]
Encoding::UndefinedConversionError: "\x80" from ASCII-8BIT to UTF-8
from /home/chris/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/core_ext/object/json.rb:38:in `encode'

Digging In I tried a few things but was unable to make it work. It seems that a marshal dump is ASCII-8BIT but Json ants UTF-8bit. I was unable to force the encoding.

> User.first.to_json.encoding
=> #<Encoding:UTF-8>
> Marshal.dump(User.first).encoding
=> #<Encoding:ASCII-8BIT>
> { foo: Marshal.dump(object).force_encoding("ASCII-8BIT").encode("UTF-8") }.to_json
Encoding::UndefinedConversionError: "\x80" from ASCII-8BIT to UTF-8
from (pry):139:in `encode'
> { foo: Marshal.dump(object).force_encoding("ISO-8859-1").encode("ASCII-8BIT") }.to_json
Encoding::UndefinedConversionError: U+0080 to ASCII-8BIT in conversion from ISO-8859-1 to UTF-8 to ASCII-8BIT

ruby 2.5.1 Rails 5.2.1

git issue I opened

like image 380
ChrisCPO Avatar asked Feb 25 '26 15:02

ChrisCPO


1 Answers

I had this issue and fixed it by using:

Marshal.dump(value).force_encoding("ISO-8859-1").encode("UTF-8")

I hope this help!

But as Tom Lord suggested you should be a bit more specific with your question to help us know what you are trying to achieve.

like image 70
Stefano Nepa Avatar answered Feb 28 '26 11:02

Stefano Nepa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!