I found the reason why multibyte characters are encoded in to_json. Safari can't handle UTF-8 strings in text/javascript correctly:(
Here is new code:
class String
JSON_ESCAPED = {
"\010" => '\b',
"\f" => '\f',
"\n" => '\n',
"\r" => '\r',
"\t" => '\t',
'"' => '\"',
'\\' => '\\\\'
}
def to_json
return '"' + gsub(/[\010\f\n\r\t"\\]/) { |s|
JSON_ESCAPED[s]
}.gsub(/([\xC0-\xDF][\x80-\xBF]|
[\xE0-\xEF][\x80-\xBF]{2}|
[\xF0-\xF7][\x80-\xBF]{3})+/ux) { |s|
s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/, '\\\\u\&')
} + '"'
end
end
I created a patch for ActiveSupport (trac of Rails is down, so I can't attach this patch to the ticket).