Module: ActiveSupport::JSON::Encoding

Defined in:
lib/patch_utils/active_support_encoding.rb

Class Method Summary collapse

Class Method Details

.escape(string) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/patch_utils/active_support_encoding.rb', line 6

def 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