Comment 1 for bug 331990

Revision history for this message
Philipp Kempgen (philipp-kempgen) wrote :

Here's roughly how to escape the string:

String.prototype.toJSON = function()
{
 return '\''+ this.replace(/[\\"'\x00-\x1F\u0080-\uFFFF]/g, function(c) {
  switch (c) {
   case "\b": return '\\b';
   case "\t": return '\\t';
   case "\n": return '\\n';
   case "\f": return '\\f';
   case "\r": return '\\r';
   case '"' : return '\\"';
   case '\'': return '\\\'';
   case '\\': return '\\\\';
  }
  return '\\u' + ('0000' + c.charCodeAt(0).toString(16)).slice(-4);
 }) +'\'';
}