Created
June 17, 2011 13:06
-
-
Save marshluca/1031377 to your computer and use it in GitHub Desktop.
Json Response without escaping in Rails 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActiveSupport | |
module JSON | |
module 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 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment