0

I am getting the

JSON::GeneratorError: source sequence is illegal/malformed utf-8

when I am using to_json method. I have not overridden the to_json method anywhere.

I have referred this question and also this one

But as Ruby 1.8 does not have the concept of string encodings the solution is not helping me.

How can I solve this issue without the requirement to escape the specific non-ascii characters?

I am on ruby 1.8.7

Community
  • 1
  • 1
Richa Sinha
  • 1,406
  • 15
  • 29
  • 1
    Can you also provide a trivial simple test case to clarify what exactly you're talking about? – deceze Aug 22 '16 at 09:37

1 Answers1

0

The only Rails solution I am aware of would be:

# [AM] Monkeypatch to support multibyte utf-8
module ::ActiveSupport::JSON::Encoding
  def self.escape(string)
    if string.respond_to?(:force_encoding)
      string = string.encode(
        ::Encoding::UTF_8,
        :undef => :replace
      ).force_encoding(::Encoding::BINARY)
    end
    json = string.gsub(escape_regex) { |s| ESCAPED_CHARS[s] }
    json = %("#{json}")
    json.force_encoding(::Encoding::UTF_8) if json.respond_to?(:force_encoding)
    json
  end
end

I believe there could be the same patch applied directly to JSON::GeneratorError.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160